lock.c revision 1.33 1 1.33 christos /* $NetBSD: lock.c,v 1.33 2013/10/18 20:47:06 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.30 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1993\
38 1.30 lukem The Regents of the University of California. All rights reserved.");
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.33 christos __RCSID("$NetBSD: lock.c,v 1.33 2013/10/18 20:47:06 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 <err.h>
62 1.1 cgd #include <pwd.h>
63 1.22 christos #include <errno.h>
64 1.1 cgd #include <stdio.h>
65 1.8 jtc #include <stdlib.h>
66 1.1 cgd #include <string.h>
67 1.5 mycroft #include <termios.h>
68 1.11 kleink #include <time.h>
69 1.8 jtc #include <unistd.h>
70 1.28 lukem
71 1.28 lukem #ifdef USE_PAM
72 1.28 lukem #include <security/pam_appl.h>
73 1.28 lukem #include <security/openpam.h> /* for openpam_ttyconv() */
74 1.28 lukem #endif
75 1.28 lukem
76 1.10 lukem #ifdef SKEY
77 1.10 lukem #include <skey.h>
78 1.10 lukem #endif
79 1.1 cgd
80 1.28 lukem
81 1.1 cgd #define TIMEOUT 15
82 1.1 cgd
83 1.26 hubertf int main(int, char **);
84 1.23 christos
85 1.27 perry static void bye(int) __dead;
86 1.23 christos static void hi(int);
87 1.27 perry static void quit(int) __dead;
88 1.10 lukem #ifdef SKEY
89 1.23 christos static int skey_auth(const char *);
90 1.10 lukem #endif
91 1.1 cgd
92 1.23 christos static struct timeval timeout;
93 1.23 christos static struct timeval zerotime;
94 1.23 christos static struct termios tty, ntty;
95 1.23 christos static int notimeout; /* no timeout at all */
96 1.23 christos static long nexttime; /* keep the timeout time */
97 1.1 cgd
98 1.10 lukem int
99 1.23 christos main(int argc, char **argv)
100 1.1 cgd {
101 1.1 cgd struct passwd *pw;
102 1.1 cgd struct timeval timval;
103 1.1 cgd struct itimerval ntimer, otimer;
104 1.1 cgd struct tm *timp;
105 1.6 cgd time_t curtime;
106 1.23 christos int ch, usemine;
107 1.23 christos long sectimeout;
108 1.33 christos char *ap, *ttynam;
109 1.14 mycroft const char *tzn;
110 1.23 christos uid_t uid = getuid();
111 1.13 mrg char hostname[MAXHOSTNAMELEN + 1], s[BUFSIZ], s1[BUFSIZ];
112 1.28 lukem #ifdef USE_PAM
113 1.28 lukem pam_handle_t *pamh = NULL;
114 1.28 lukem static const struct pam_conv pamc = { &openpam_ttyconv, NULL };
115 1.28 lukem int pam_err;
116 1.33 christos #else
117 1.33 christos char *mypw = NULL;
118 1.28 lukem #endif
119 1.1 cgd
120 1.23 christos if ((pw = getpwuid(getuid())) == NULL)
121 1.23 christos errx(1, "unknown uid %lu.", (u_long)uid);
122 1.12 mrg
123 1.17 tron notimeout = 0;
124 1.1 cgd sectimeout = TIMEOUT;
125 1.1 cgd usemine = 0;
126 1.3 deraadt
127 1.17 tron while ((ch = getopt(argc, argv, "npt:")) != -1)
128 1.12 mrg switch ((char)ch) {
129 1.17 tron case 'n':
130 1.17 tron notimeout = 1;
131 1.17 tron break;
132 1.1 cgd case 't':
133 1.25 liamjfoy errno = 0;
134 1.23 christos if (((sectimeout = strtol(optarg, &ap, 0)) == LONG_MAX
135 1.23 christos || sectimeout == LONG_MIN)
136 1.23 christos && errno == ERANGE)
137 1.23 christos err(1, "illegal timeout value: %s", optarg);
138 1.23 christos if (optarg == ap || *ap || sectimeout <= 0)
139 1.5 mycroft errx(1, "illegal timeout value: %s", optarg);
140 1.23 christos if (sectimeout >= INT_MAX / 60)
141 1.23 christos errx(1, "too large timeout value: %ld",
142 1.23 christos sectimeout);
143 1.1 cgd break;
144 1.1 cgd case 'p':
145 1.1 cgd usemine = 1;
146 1.28 lukem #ifndef USE_PAM
147 1.1 cgd mypw = strdup(pw->pw_passwd);
148 1.20 itojun if (!mypw)
149 1.20 itojun err(1, "strdup");
150 1.28 lukem #endif
151 1.1 cgd break;
152 1.1 cgd case '?':
153 1.1 cgd default:
154 1.1 cgd (void)fprintf(stderr,
155 1.24 wiz "usage: %s [-np] [-t timeout]\n", getprogname());
156 1.1 cgd exit(1);
157 1.12 mrg }
158 1.28 lukem
159 1.28 lukem #if defined(USE_PAM) || defined(SKEY)
160 1.28 lukem if (! usemine) { /* -p with PAM or S/key needs privs */
161 1.28 lukem #endif
162 1.28 lukem if (setuid(uid) == -1) /* discard privs */
163 1.28 lukem err(1, "setuid failed");
164 1.28 lukem #if defined(USE_PAM) || defined(SKEY)
165 1.28 lukem }
166 1.28 lukem #endif
167 1.28 lukem
168 1.23 christos timeout.tv_sec = (int)sectimeout * 60;
169 1.1 cgd
170 1.23 christos if (tcgetattr(STDIN_FILENO, &tty) < 0) /* get information for header */
171 1.23 christos err(1, "tcgetattr failed");
172 1.1 cgd gethostname(hostname, sizeof(hostname));
173 1.12 mrg hostname[sizeof(hostname) - 1] = '\0';
174 1.23 christos if (!(ttynam = ttyname(STDIN_FILENO)))
175 1.23 christos err(1, "ttyname failed");
176 1.23 christos if (gettimeofday(&timval, NULL) == -1)
177 1.23 christos err(1, "gettimeofday failed");
178 1.6 cgd curtime = timval.tv_sec;
179 1.23 christos nexttime = timval.tv_sec + ((int)sectimeout * 60);
180 1.6 cgd timp = localtime(&curtime);
181 1.1 cgd ap = asctime(timp);
182 1.16 christos #ifdef __SVR4
183 1.16 christos tzn = tzname[0];
184 1.16 christos #else
185 1.1 cgd tzn = timp->tm_zone;
186 1.16 christos #endif
187 1.1 cgd
188 1.23 christos if (signal(SIGINT, quit) == SIG_ERR)
189 1.23 christos err(1, "signal failed");
190 1.23 christos if (signal(SIGQUIT, quit) == SIG_ERR)
191 1.23 christos err(1, "signal failed");
192 1.5 mycroft ntty = tty; ntty.c_lflag &= ~ECHO;
193 1.23 christos if (tcsetattr(STDIN_FILENO, TCSADRAIN, &ntty) == -1)
194 1.23 christos err(1, "tcsetattr");
195 1.1 cgd
196 1.28 lukem if (!usemine) {
197 1.1 cgd /* get key and check again */
198 1.1 cgd (void)printf("Key: ");
199 1.1 cgd if (!fgets(s, sizeof(s), stdin) || *s == '\n')
200 1.10 lukem quit(0);
201 1.1 cgd (void)printf("\nAgain: ");
202 1.1 cgd /*
203 1.1 cgd * Don't need EOF test here, if we get EOF, then s1 != s
204 1.1 cgd * and the right things will happen.
205 1.1 cgd */
206 1.1 cgd (void)fgets(s1, sizeof(s1), stdin);
207 1.1 cgd (void)putchar('\n');
208 1.1 cgd if (strcmp(s1, s)) {
209 1.5 mycroft (void)printf("\alock: passwords didn't match.\n");
210 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
211 1.1 cgd exit(1);
212 1.1 cgd }
213 1.9 pk s[0] = '\0';
214 1.33 christos #ifndef USE_PAM
215 1.1 cgd mypw = s1;
216 1.33 christos #endif
217 1.1 cgd }
218 1.28 lukem #ifdef USE_PAM
219 1.28 lukem if (usemine) {
220 1.28 lukem pam_err = pam_start("lock", pw->pw_name, &pamc, &pamh);
221 1.28 lukem if (pam_err != PAM_SUCCESS)
222 1.28 lukem err(1, "pam_start: %s", pam_strerror(NULL, pam_err));
223 1.28 lukem }
224 1.28 lukem #endif
225 1.1 cgd
226 1.1 cgd /* set signal handlers */
227 1.23 christos if (signal(SIGINT, hi) == SIG_ERR)
228 1.23 christos err(1, "signal failed");
229 1.23 christos if (signal(SIGQUIT, hi) == SIG_ERR)
230 1.23 christos err(1, "signal failed");
231 1.23 christos if (signal(SIGTSTP, hi) == SIG_ERR)
232 1.23 christos err(1, "signal failed");
233 1.1 cgd
234 1.17 tron if (notimeout) {
235 1.23 christos if (signal(SIGALRM, hi) == SIG_ERR)
236 1.23 christos err(1, "signal failed");
237 1.23 christos (void)printf("lock: %s on %s. no timeout.\n"
238 1.23 christos "time now is %.20s%s%s",
239 1.17 tron ttynam, hostname, ap, tzn, ap + 19);
240 1.17 tron }
241 1.17 tron else {
242 1.23 christos if (signal(SIGALRM, bye) == SIG_ERR)
243 1.23 christos err(1, "signal failed");
244 1.17 tron
245 1.17 tron ntimer.it_interval = zerotime;
246 1.17 tron ntimer.it_value = timeout;
247 1.23 christos if (setitimer(ITIMER_REAL, &ntimer, &otimer) == -1)
248 1.23 christos err(1, "setitimer failed");
249 1.1 cgd
250 1.17 tron /* header info */
251 1.23 christos (void)printf("lock: %s on %s. timeout in %ld minutes\n"
252 1.23 christos "time now is %.20s%s%s",
253 1.23 christos ttynam, hostname, sectimeout, ap, tzn, ap + 19);
254 1.17 tron }
255 1.1 cgd
256 1.1 cgd for (;;) {
257 1.28 lukem #ifdef USE_PAM
258 1.28 lukem if (usemine) {
259 1.28 lukem pam_err = pam_authenticate(pamh, 0);
260 1.28 lukem if (pam_err == PAM_SUCCESS)
261 1.28 lukem break;
262 1.28 lukem goto tryagain;
263 1.28 lukem }
264 1.28 lukem #endif
265 1.1 cgd (void)printf("Key: ");
266 1.1 cgd if (!fgets(s, sizeof(s), stdin)) {
267 1.1 cgd clearerr(stdin);
268 1.10 lukem hi(0);
269 1.22 christos goto tryagain;
270 1.1 cgd }
271 1.29 christos #ifndef USE_PAM
272 1.1 cgd if (usemine) {
273 1.1 cgd s[strlen(s) - 1] = '\0';
274 1.3 deraadt #ifdef SKEY
275 1.3 deraadt if (strcasecmp(s, "s/key") == 0) {
276 1.3 deraadt if (skey_auth(pw->pw_name))
277 1.3 deraadt break;
278 1.3 deraadt }
279 1.3 deraadt #endif
280 1.1 cgd if (!strcmp(mypw, crypt(s, mypw)))
281 1.1 cgd break;
282 1.1 cgd }
283 1.29 christos else
284 1.29 christos #endif
285 1.29 christos if (!strcmp(s, s1))
286 1.29 christos break;
287 1.5 mycroft (void)printf("\a\n");
288 1.28 lukem tryagain:
289 1.23 christos if (tcsetattr(STDIN_FILENO, TCSADRAIN, &ntty) == -1
290 1.23 christos && errno != EINTR)
291 1.23 christos err(1, "tcsetattr failed");
292 1.1 cgd }
293 1.28 lukem #ifdef USE_PAM
294 1.28 lukem if (usemine) {
295 1.28 lukem (void)pam_end(pamh, pam_err);
296 1.28 lukem }
297 1.28 lukem #endif
298 1.10 lukem quit(0);
299 1.10 lukem /* NOTREACHED */
300 1.23 christos return 0;
301 1.1 cgd }
302 1.3 deraadt
303 1.3 deraadt #ifdef SKEY
304 1.3 deraadt /*
305 1.3 deraadt * We can't use libskey's skey_authenticate() since it
306 1.3 deraadt * handles signals in a way that's inappropriate
307 1.3 deraadt * for our needs. Instead we roll our own.
308 1.3 deraadt */
309 1.23 christos static int
310 1.23 christos skey_auth(const char *user)
311 1.3 deraadt {
312 1.19 martin char s[128];
313 1.19 martin const char *ask;
314 1.3 deraadt int ret = 0;
315 1.3 deraadt
316 1.3 deraadt if (!skey_haskey(user) && (ask = skey_keyinfo(user))) {
317 1.23 christos (void)printf("\n[%s]\nResponse: ", ask);
318 1.3 deraadt if (!fgets(s, sizeof(s), stdin) || *s == '\n')
319 1.3 deraadt clearerr(stdin);
320 1.3 deraadt else {
321 1.3 deraadt s[strlen(s) - 1] = '\0';
322 1.3 deraadt if (skey_passcheck(user, s) != -1)
323 1.3 deraadt ret = 1;
324 1.3 deraadt }
325 1.3 deraadt } else
326 1.23 christos (void)printf("Sorry, you have no s/key.\n");
327 1.3 deraadt return ret;
328 1.3 deraadt }
329 1.3 deraadt #endif
330 1.1 cgd
331 1.23 christos static void
332 1.32 matt hi(int dummy)
333 1.1 cgd {
334 1.1 cgd struct timeval timval;
335 1.1 cgd
336 1.17 tron if (notimeout)
337 1.17 tron (void)printf("lock: type in the unlock key.\n");
338 1.23 christos else {
339 1.23 christos if (gettimeofday(&timval, NULL) == -1)
340 1.23 christos err(1, "gettimeofday failed");
341 1.23 christos (void)printf("lock: type in the unlock key. "
342 1.31 christos "timeout in %lld:%lld minutes\n",
343 1.31 christos (long long)(nexttime - timval.tv_sec) / 60,
344 1.31 christos (long long)(nexttime - timval.tv_sec) % 60);
345 1.23 christos }
346 1.1 cgd }
347 1.1 cgd
348 1.23 christos static void
349 1.23 christos quit(int dummy)
350 1.1 cgd {
351 1.1 cgd (void)putchar('\n');
352 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
353 1.1 cgd exit(0);
354 1.1 cgd }
355 1.1 cgd
356 1.23 christos static void
357 1.23 christos bye(int dummy)
358 1.1 cgd {
359 1.23 christos (void)tcsetattr(STDIN_FILENO, TCSADRAIN, &tty);
360 1.1 cgd (void)printf("lock: timeout\n");
361 1.1 cgd exit(1);
362 1.1 cgd }
363