lock.c revision 1.22 1 /* $NetBSD: lock.c,v 1.22 2004/03/17 17:01:31 christos Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Bob Toxen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1993\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)lock.c 8.1 (Berkeley) 6/6/93";
44 #endif
45 __RCSID("$NetBSD: lock.c,v 1.22 2004/03/17 17:01:31 christos Exp $");
46 #endif /* not lint */
47
48 /*
49 * Lock a terminal up until the given key is entered, until the root
50 * password is entered, or the given interval times out.
51 *
52 * Timeout interval is by default TIMEOUT, it can be changed with
53 * an argument of the form -time where time is in minutes
54 */
55
56 #include <sys/param.h>
57 #include <sys/stat.h>
58 #include <sys/time.h>
59 #include <signal.h>
60
61 #include <ctype.h>
62 #include <err.h>
63 #include <pwd.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <termios.h>
69 #include <time.h>
70 #include <unistd.h>
71 #ifdef SKEY
72 #include <skey.h>
73 #endif
74
75 #define TIMEOUT 15
76
77 void bye __P((int));
78 void hi __P((int));
79 int main __P((int, char **));
80 void quit __P((int));
81 #ifdef SKEY
82 int skey_auth __P((const char *));
83 #endif
84
85 struct timeval timeout;
86 struct timeval zerotime;
87 struct termios tty, ntty;
88 int notimeout; /* no timeout at all */
89 long nexttime; /* keep the timeout time */
90
91 int
92 main(argc, argv)
93 int argc;
94 char **argv;
95 {
96 struct passwd *pw;
97 struct timeval timval;
98 struct itimerval ntimer, otimer;
99 struct tm *timp;
100 time_t curtime;
101 int ch, sectimeout, usemine;
102 char *ap, *mypw, *ttynam;
103 const char *tzn;
104 char hostname[MAXHOSTNAMELEN + 1], s[BUFSIZ], s1[BUFSIZ];
105
106 if (!(pw = getpwuid(getuid())))
107 errx(1, "unknown uid %ld.", (u_long) getuid());
108
109 setuid(getuid()); /* discard privs */
110
111 notimeout = 0;
112 sectimeout = TIMEOUT;
113 mypw = NULL;
114 usemine = 0;
115
116 while ((ch = getopt(argc, argv, "npt:")) != -1)
117 switch ((char)ch) {
118 case 'n':
119 notimeout = 1;
120 break;
121 case 't':
122 if ((sectimeout = atoi(optarg)) <= 0)
123 errx(1, "illegal timeout value: %s", optarg);
124 break;
125 case 'p':
126 usemine = 1;
127 mypw = strdup(pw->pw_passwd);
128 if (!mypw)
129 err(1, "strdup");
130 break;
131 case '?':
132 default:
133 (void)fprintf(stderr,
134 "usage: lock [-p] [-t timeout]\n");
135 exit(1);
136 }
137 timeout.tv_sec = sectimeout * 60;
138
139 if (tcgetattr(0, &tty) < 0) /* get information for header */
140 exit(1);
141 gethostname(hostname, sizeof(hostname));
142 hostname[sizeof(hostname) - 1] = '\0';
143 if (!(ttynam = ttyname(0)))
144 errx(1, "not a terminal?");
145 if (gettimeofday(&timval, (struct timezone *)NULL))
146 err(1, "gettimeofday");
147 curtime = timval.tv_sec;
148 nexttime = timval.tv_sec + (sectimeout * 60);
149 timp = localtime(&curtime);
150 ap = asctime(timp);
151 #ifdef __SVR4
152 tzn = tzname[0];
153 #else
154 tzn = timp->tm_zone;
155 #endif
156
157 (void)signal(SIGINT, quit);
158 (void)signal(SIGQUIT, quit);
159 ntty = tty; ntty.c_lflag &= ~ECHO;
160 (void)tcsetattr(0, TCSADRAIN, &ntty);
161
162 if (!mypw) {
163 /* get key and check again */
164 (void)printf("Key: ");
165 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
166 quit(0);
167 (void)printf("\nAgain: ");
168 /*
169 * Don't need EOF test here, if we get EOF, then s1 != s
170 * and the right things will happen.
171 */
172 (void)fgets(s1, sizeof(s1), stdin);
173 (void)putchar('\n');
174 if (strcmp(s1, s)) {
175 (void)printf("\alock: passwords didn't match.\n");
176 (void)tcsetattr(0, TCSADRAIN, &tty);
177 exit(1);
178 }
179 s[0] = '\0';
180 mypw = s1;
181 }
182
183 /* set signal handlers */
184 (void)signal(SIGINT, hi);
185 (void)signal(SIGQUIT, hi);
186 (void)signal(SIGTSTP, hi);
187
188 if (notimeout) {
189 (void)signal(SIGALRM, hi);
190 (void)printf("lock: %s on %s. no timeout.\ntime now is %.20s%s%s",
191 ttynam, hostname, ap, tzn, ap + 19);
192 }
193 else {
194 (void)signal(SIGALRM, bye);
195
196 ntimer.it_interval = zerotime;
197 ntimer.it_value = timeout;
198 setitimer(ITIMER_REAL, &ntimer, &otimer);
199
200 /* header info */
201 (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
202 ttynam, hostname, sectimeout, ap, tzn, ap + 19);
203 }
204
205 for (;;) {
206 (void)printf("Key: ");
207 if (!fgets(s, sizeof(s), stdin)) {
208 clearerr(stdin);
209 hi(0);
210 goto tryagain;
211 }
212 if (usemine) {
213 s[strlen(s) - 1] = '\0';
214 #ifdef SKEY
215 if (strcasecmp(s, "s/key") == 0) {
216 if (skey_auth(pw->pw_name))
217 break;
218 }
219 #endif
220 if (!strcmp(mypw, crypt(s, mypw)))
221 break;
222 }
223 else if (!strcmp(s, s1))
224 break;
225 (void)printf("\a\n");
226 tryagain:
227 if (tcsetattr(0, TCSADRAIN, &ntty) == -1 && errno != EINTR)
228 exit(1);
229 }
230 quit(0);
231 /* NOTREACHED */
232 return (0);
233 }
234
235 #ifdef SKEY
236 /*
237 * We can't use libskey's skey_authenticate() since it
238 * handles signals in a way that's inappropriate
239 * for our needs. Instead we roll our own.
240 */
241 int
242 skey_auth(user)
243 const char *user;
244 {
245 char s[128];
246 const char *ask;
247 int ret = 0;
248
249 if (!skey_haskey(user) && (ask = skey_keyinfo(user))) {
250 printf("\n[%s]\nResponse: ", ask);
251 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
252 clearerr(stdin);
253 else {
254 s[strlen(s) - 1] = '\0';
255 if (skey_passcheck(user, s) != -1)
256 ret = 1;
257 }
258 } else
259 printf("Sorry, you have no s/key.\n");
260 return ret;
261 }
262 #endif
263
264 void
265 hi(dummy)
266 int dummy;
267 {
268 struct timeval timval;
269
270 if (notimeout)
271 (void)printf("lock: type in the unlock key.\n");
272 else if (!gettimeofday(&timval, (struct timezone *)NULL))
273 (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
274 (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
275 }
276
277 void
278 quit(dummy)
279 int dummy;
280 {
281 (void)putchar('\n');
282 (void)tcsetattr(0, TCSADRAIN, &tty);
283 exit(0);
284 }
285
286 void
287 bye(dummy)
288 int dummy;
289 {
290 (void)tcsetattr(0, TCSADRAIN, &tty);
291 (void)printf("lock: timeout\n");
292 exit(1);
293 }
294