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