lock.c revision 1.25 1 1.25 liamjfoy /* $NetBSD: lock.c,v 1.25 2006/05/11 12:02:08 liamjfoy 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.25 liamjfoy __RCSID("$NetBSD: lock.c,v 1.25 2006/05/11 12:02:08 liamjfoy 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 int main __P((int, char **));
78 1.23 christos
79 1.23 christos static void bye(int) __attribute__((__noreturn__));
80 1.23 christos static void hi(int);
81 1.23 christos static void quit(int) __attribute__((__noreturn__));
82 1.10 lukem #ifdef SKEY
83 1.23 christos static int skey_auth(const char *);
84 1.10 lukem #endif
85 1.1 cgd
86 1.23 christos static struct timeval timeout;
87 1.23 christos static struct timeval zerotime;
88 1.23 christos static struct termios tty, ntty;
89 1.23 christos static int notimeout; /* no timeout at all */
90 1.23 christos static long nexttime; /* keep the timeout time */
91 1.1 cgd
92 1.10 lukem int
93 1.23 christos main(int argc, char **argv)
94 1.1 cgd {
95 1.1 cgd struct passwd *pw;
96 1.1 cgd struct timeval timval;
97 1.1 cgd struct itimerval ntimer, otimer;
98 1.1 cgd struct tm *timp;
99 1.6 cgd time_t curtime;
100 1.23 christos int ch, usemine;
101 1.23 christos long sectimeout;
102 1.14 mycroft char *ap, *mypw, *ttynam;
103 1.14 mycroft const char *tzn;
104 1.23 christos uid_t uid = getuid();
105 1.13 mrg char hostname[MAXHOSTNAMELEN + 1], s[BUFSIZ], s1[BUFSIZ];
106 1.1 cgd
107 1.23 christos if ((pw = getpwuid(getuid())) == NULL)
108 1.23 christos errx(1, "unknown uid %lu.", (u_long)uid);
109 1.12 mrg
110 1.23 christos if (setuid(uid) == -1) /* discard privs */
111 1.23 christos err(1, "setuid failed");
112 1.12 mrg
113 1.17 tron notimeout = 0;
114 1.1 cgd sectimeout = TIMEOUT;
115 1.1 cgd mypw = NULL;
116 1.1 cgd usemine = 0;
117 1.3 deraadt
118 1.17 tron while ((ch = getopt(argc, argv, "npt:")) != -1)
119 1.12 mrg switch ((char)ch) {
120 1.17 tron case 'n':
121 1.17 tron notimeout = 1;
122 1.17 tron break;
123 1.1 cgd case 't':
124 1.25 liamjfoy errno = 0;
125 1.23 christos if (((sectimeout = strtol(optarg, &ap, 0)) == LONG_MAX
126 1.23 christos || sectimeout == LONG_MIN)
127 1.23 christos && errno == ERANGE)
128 1.23 christos err(1, "illegal timeout value: %s", optarg);
129 1.23 christos if (optarg == ap || *ap || sectimeout <= 0)
130 1.5 mycroft errx(1, "illegal timeout value: %s", optarg);
131 1.23 christos if (sectimeout >= INT_MAX / 60)
132 1.23 christos errx(1, "too large timeout value: %ld",
133 1.23 christos sectimeout);
134 1.1 cgd break;
135 1.1 cgd case 'p':
136 1.1 cgd usemine = 1;
137 1.1 cgd mypw = strdup(pw->pw_passwd);
138 1.20 itojun if (!mypw)
139 1.20 itojun err(1, "strdup");
140 1.1 cgd break;
141 1.1 cgd case '?':
142 1.1 cgd default:
143 1.1 cgd (void)fprintf(stderr,
144 1.24 wiz "usage: %s [-np] [-t timeout]\n", getprogname());
145 1.1 cgd exit(1);
146 1.12 mrg }
147 1.23 christos timeout.tv_sec = (int)sectimeout * 60;
148 1.1 cgd
149 1.23 christos if (tcgetattr(STDIN_FILENO, &tty) < 0) /* get information for header */
150 1.23 christos err(1, "tcgetattr failed");
151 1.1 cgd gethostname(hostname, sizeof(hostname));
152 1.12 mrg hostname[sizeof(hostname) - 1] = '\0';
153 1.23 christos if (!(ttynam = ttyname(STDIN_FILENO)))
154 1.23 christos err(1, "ttyname failed");
155 1.23 christos if (gettimeofday(&timval, NULL) == -1)
156 1.23 christos err(1, "gettimeofday failed");
157 1.6 cgd curtime = timval.tv_sec;
158 1.23 christos nexttime = timval.tv_sec + ((int)sectimeout * 60);
159 1.6 cgd timp = localtime(&curtime);
160 1.1 cgd ap = asctime(timp);
161 1.16 christos #ifdef __SVR4
162 1.16 christos tzn = tzname[0];
163 1.16 christos #else
164 1.1 cgd tzn = timp->tm_zone;
165 1.16 christos #endif
166 1.1 cgd
167 1.23 christos if (signal(SIGINT, quit) == SIG_ERR)
168 1.23 christos err(1, "signal failed");
169 1.23 christos if (signal(SIGQUIT, quit) == SIG_ERR)
170 1.23 christos err(1, "signal failed");
171 1.5 mycroft ntty = tty; ntty.c_lflag &= ~ECHO;
172 1.23 christos if (tcsetattr(STDIN_FILENO, TCSADRAIN, &ntty) == -1)
173 1.23 christos err(1, "tcsetattr");
174 1.1 cgd
175 1.1 cgd if (!mypw) {
176 1.1 cgd /* get key and check again */
177 1.1 cgd (void)printf("Key: ");
178 1.1 cgd if (!fgets(s, sizeof(s), stdin) || *s == '\n')
179 1.10 lukem quit(0);
180 1.1 cgd (void)printf("\nAgain: ");
181 1.1 cgd /*
182 1.1 cgd * Don't need EOF test here, if we get EOF, then s1 != s
183 1.1 cgd * and the right things will happen.
184 1.1 cgd */
185 1.1 cgd (void)fgets(s1, sizeof(s1), stdin);
186 1.1 cgd (void)putchar('\n');
187 1.1 cgd if (strcmp(s1, s)) {
188 1.5 mycroft (void)printf("\alock: passwords didn't match.\n");
189 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
190 1.1 cgd exit(1);
191 1.1 cgd }
192 1.9 pk s[0] = '\0';
193 1.1 cgd mypw = s1;
194 1.1 cgd }
195 1.1 cgd
196 1.1 cgd /* set signal handlers */
197 1.23 christos if (signal(SIGINT, hi) == SIG_ERR)
198 1.23 christos err(1, "signal failed");
199 1.23 christos if (signal(SIGQUIT, hi) == SIG_ERR)
200 1.23 christos err(1, "signal failed");
201 1.23 christos if (signal(SIGTSTP, hi) == SIG_ERR)
202 1.23 christos err(1, "signal failed");
203 1.1 cgd
204 1.17 tron if (notimeout) {
205 1.23 christos if (signal(SIGALRM, hi) == SIG_ERR)
206 1.23 christos err(1, "signal failed");
207 1.23 christos (void)printf("lock: %s on %s. no timeout.\n"
208 1.23 christos "time now is %.20s%s%s",
209 1.17 tron ttynam, hostname, ap, tzn, ap + 19);
210 1.17 tron }
211 1.17 tron else {
212 1.23 christos if (signal(SIGALRM, bye) == SIG_ERR)
213 1.23 christos err(1, "signal failed");
214 1.17 tron
215 1.17 tron ntimer.it_interval = zerotime;
216 1.17 tron ntimer.it_value = timeout;
217 1.23 christos if (setitimer(ITIMER_REAL, &ntimer, &otimer) == -1)
218 1.23 christos err(1, "setitimer failed");
219 1.1 cgd
220 1.17 tron /* header info */
221 1.23 christos (void)printf("lock: %s on %s. timeout in %ld minutes\n"
222 1.23 christos "time now is %.20s%s%s",
223 1.23 christos ttynam, hostname, sectimeout, ap, tzn, ap + 19);
224 1.17 tron }
225 1.1 cgd
226 1.1 cgd for (;;) {
227 1.1 cgd (void)printf("Key: ");
228 1.1 cgd if (!fgets(s, sizeof(s), stdin)) {
229 1.1 cgd clearerr(stdin);
230 1.10 lukem hi(0);
231 1.22 christos goto tryagain;
232 1.1 cgd }
233 1.1 cgd if (usemine) {
234 1.1 cgd s[strlen(s) - 1] = '\0';
235 1.3 deraadt #ifdef SKEY
236 1.3 deraadt if (strcasecmp(s, "s/key") == 0) {
237 1.3 deraadt if (skey_auth(pw->pw_name))
238 1.3 deraadt break;
239 1.3 deraadt }
240 1.3 deraadt #endif
241 1.1 cgd if (!strcmp(mypw, crypt(s, mypw)))
242 1.1 cgd break;
243 1.1 cgd }
244 1.1 cgd else if (!strcmp(s, s1))
245 1.1 cgd break;
246 1.5 mycroft (void)printf("\a\n");
247 1.22 christos tryagain:
248 1.23 christos if (tcsetattr(STDIN_FILENO, TCSADRAIN, &ntty) == -1
249 1.23 christos && errno != EINTR)
250 1.23 christos err(1, "tcsetattr failed");
251 1.1 cgd }
252 1.10 lukem quit(0);
253 1.10 lukem /* NOTREACHED */
254 1.23 christos return 0;
255 1.1 cgd }
256 1.3 deraadt
257 1.3 deraadt #ifdef SKEY
258 1.3 deraadt /*
259 1.3 deraadt * We can't use libskey's skey_authenticate() since it
260 1.3 deraadt * handles signals in a way that's inappropriate
261 1.3 deraadt * for our needs. Instead we roll our own.
262 1.3 deraadt */
263 1.23 christos static int
264 1.23 christos skey_auth(const char *user)
265 1.3 deraadt {
266 1.19 martin char s[128];
267 1.19 martin const char *ask;
268 1.3 deraadt int ret = 0;
269 1.3 deraadt
270 1.3 deraadt if (!skey_haskey(user) && (ask = skey_keyinfo(user))) {
271 1.23 christos (void)printf("\n[%s]\nResponse: ", ask);
272 1.3 deraadt if (!fgets(s, sizeof(s), stdin) || *s == '\n')
273 1.3 deraadt clearerr(stdin);
274 1.3 deraadt else {
275 1.3 deraadt s[strlen(s) - 1] = '\0';
276 1.3 deraadt if (skey_passcheck(user, s) != -1)
277 1.3 deraadt ret = 1;
278 1.3 deraadt }
279 1.3 deraadt } else
280 1.23 christos (void)printf("Sorry, you have no s/key.\n");
281 1.3 deraadt return ret;
282 1.3 deraadt }
283 1.3 deraadt #endif
284 1.1 cgd
285 1.23 christos static void
286 1.10 lukem hi(dummy)
287 1.10 lukem int dummy;
288 1.1 cgd {
289 1.1 cgd struct timeval timval;
290 1.1 cgd
291 1.17 tron if (notimeout)
292 1.17 tron (void)printf("lock: type in the unlock key.\n");
293 1.23 christos else {
294 1.23 christos if (gettimeofday(&timval, NULL) == -1)
295 1.23 christos err(1, "gettimeofday failed");
296 1.23 christos (void)printf("lock: type in the unlock key. "
297 1.23 christos "timeout in %ld:%ld minutes\n",
298 1.23 christos (nexttime - timval.tv_sec) / 60,
299 1.23 christos (nexttime - timval.tv_sec) % 60);
300 1.23 christos }
301 1.1 cgd }
302 1.1 cgd
303 1.23 christos static void
304 1.23 christos quit(int dummy)
305 1.1 cgd {
306 1.1 cgd (void)putchar('\n');
307 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
308 1.1 cgd exit(0);
309 1.1 cgd }
310 1.1 cgd
311 1.23 christos static void
312 1.23 christos bye(int dummy)
313 1.1 cgd {
314 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
315 1.1 cgd (void)printf("lock: timeout\n");
316 1.1 cgd exit(1);
317 1.1 cgd }
318