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