Annotation of src/usr.bin/lock/lock.c, Revision 1.25
1.25 ! deraadt 1: /* $OpenBSD: lock.c,v 1.24 2009/09/18 20:58:35 martynas Exp $ */
1.2 deraadt 2: /* $NetBSD: lock.c,v 1.8 1996/05/07 18:32:31 jtc Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1980, 1987, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Bob Toxen.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.20 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: /*
37: * Lock a terminal up until the given key is entered, until the root
38: * password is entered, or the given interval times out.
39: *
40: * Timeout interval is by default TIMEOUT, it can be changed with
41: * an argument of the form -time where time is in minutes
42: */
43:
44: #include <sys/param.h>
45: #include <sys/stat.h>
46: #include <sys/time.h>
47: #include <signal.h>
48:
49: #include <ctype.h>
50: #include <err.h>
51: #include <pwd.h>
1.13 millert 52: #include <readpassphrase.h>
1.1 deraadt 53: #include <stdio.h>
1.2 deraadt 54: #include <stdlib.h>
1.1 deraadt 55: #include <string.h>
56: #include <termios.h>
1.2 deraadt 57: #include <unistd.h>
1.1 deraadt 58:
1.13 millert 59: #include <login_cap.h>
60: #include <bsd_auth.h>
1.7 millert 61:
1.1 deraadt 62: #define TIMEOUT 15
63:
1.15 millert 64: void bye(int);
65: void hi(int);
1.1 deraadt 66:
67: struct timeval timeout;
68: struct timeval zerotime;
1.13 millert 69: time_t nexttime; /* keep the timeout time */
1.9 millert 70: int no_timeout; /* lock terminal forever */
1.1 deraadt 71:
1.13 millert 72: extern char *__progname;
73:
1.1 deraadt 74: /*ARGSUSED*/
1.9 millert 75: int
1.18 deraadt 76: main(int argc, char *argv[])
1.1 deraadt 77: {
1.18 deraadt 78: char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ], date[256];
79: char *p, *style, *nstyle, *ttynam;
80: struct itimerval ntimer, otimer;
1.24 martynas 81: int ch, sectimeout, usemine, cnt, tries = 10, backoff = 3;
1.22 deraadt 82: const char *errstr;
1.1 deraadt 83: struct passwd *pw;
84: struct tm *timp;
85: time_t curtime;
1.13 millert 86: login_cap_t *lc;
1.1 deraadt 87:
88: sectimeout = TIMEOUT;
1.13 millert 89: style = NULL;
1.1 deraadt 90: usemine = 0;
1.6 downsj 91: no_timeout = 0;
1.1 deraadt 92:
93: if (!(pw = getpwuid(getuid())))
1.17 deraadt 94: errx(1, "unknown uid %u.", getuid());
1.13 millert 95:
96: lc = login_getclass(pw->pw_class);
1.24 martynas 97: if (lc != NULL) {
98: /*
99: * We allow "login-tries" attempts to login but start
100: * slowing down after "login-backoff" attempts.
101: */
102: tries = (int)login_getcapnum(lc, "login-tries", 10, 10);
103: backoff = (int)login_getcapnum(lc, "login-backoff", 3, 3);
104: }
105:
1.13 millert 106: while ((ch = getopt(argc, argv, "a:npt:")) != -1)
1.19 deraadt 107: switch (ch) {
1.13 millert 108: case 'a':
109: if (lc) {
110: style = login_getstyle(lc, optarg, "auth-lock");
111: if (style == NULL)
112: errx(1,
113: "invalid authentication style: %s",
114: optarg);
115: }
116: usemine = 1;
117: break;
1.1 deraadt 118: case 't':
1.23 deraadt 119: sectimeout = (int)strtonum(optarg, 1, INT_MAX, &errstr);
1.22 deraadt 120: if (errstr)
121: errx(1, "timeout %s: %s", errstr, optarg);
1.1 deraadt 122: break;
123: case 'p':
124: usemine = 1;
125: break;
1.6 downsj 126: case 'n':
127: no_timeout = 1;
128: break;
1.1 deraadt 129: default:
1.10 millert 130: (void)fprintf(stderr,
1.21 jmc 131: "usage: %s [-np] [-a style] [-t timeout]\n",
1.13 millert 132: __progname);
1.1 deraadt 133: exit(1);
134: }
135: timeout.tv_sec = sectimeout * 60;
136:
137: gethostname(hostname, sizeof(hostname));
1.13 millert 138: if (!(ttynam = ttyname(STDIN_FILENO)))
1.1 deraadt 139: errx(1, "not a terminal?");
1.13 millert 140: curtime = time(NULL);
141: nexttime = curtime + (sectimeout * 60);
1.1 deraadt 142: timp = localtime(&curtime);
1.13 millert 143: strftime(date, sizeof(date), "%c", timp);
1.1 deraadt 144:
1.13 millert 145: if (!usemine) {
1.1 deraadt 146: /* get key and check again */
1.13 millert 147: if (!readpassphrase("Key: ", s, sizeof(s), RPP_ECHO_OFF) ||
148: *s == '\0')
149: exit(0);
1.1 deraadt 150: /*
151: * Don't need EOF test here, if we get EOF, then s1 != s
152: * and the right things will happen.
153: */
1.13 millert 154: (void)readpassphrase("Again: ", s1, sizeof(s1), RPP_ECHO_OFF);
1.1 deraadt 155: if (strcmp(s1, s)) {
1.13 millert 156: warnx("\apasswords didn't match.");
1.1 deraadt 157: exit(1);
158: }
1.5 deraadt 159: s[0] = '\0';
1.1 deraadt 160: }
161:
162: /* set signal handlers */
163: (void)signal(SIGINT, hi);
164: (void)signal(SIGQUIT, hi);
165: (void)signal(SIGTSTP, hi);
166: (void)signal(SIGALRM, bye);
167:
168: ntimer.it_interval = zerotime;
169: ntimer.it_value = timeout;
1.6 downsj 170: if (!no_timeout)
171: setitimer(ITIMER_REAL, &ntimer, &otimer);
1.1 deraadt 172:
173: /* header info */
1.6 downsj 174: if (no_timeout) {
1.13 millert 175: (void)fprintf(stderr,
176: "%s: %s on %s. no timeout\ntime now is %s\n",
177: __progname, ttynam, hostname, date);
1.6 downsj 178: } else {
1.13 millert 179: (void)fprintf(stderr,
180: "%s: %s on %s. timeout in %d minutes\ntime now is %s\n",
181: __progname, ttynam, hostname, sectimeout, date);
1.6 downsj 182: }
1.1 deraadt 183:
1.24 martynas 184: for (cnt = 0;;) {
1.13 millert 185: if (!readpassphrase("Key: ", s, sizeof(s), RPP_ECHO_OFF) ||
186: *s == '\0') {
1.9 millert 187: hi(0);
1.1 deraadt 188: continue;
189: }
190: if (usemine) {
1.13 millert 191: /*
192: * If user entered 's/key' or the style specified via
193: * the '-a' argument, auth_userokay() will prompt
194: * for a new password. Otherwise, use what we have.
195: */
196: if ((strcmp(s, "s/key") == 0 &&
197: (nstyle = login_getstyle(lc, "skey", "auth-lock")))
198: || ((nstyle = style) && strcmp(s, nstyle) == 0))
199: p = NULL;
200: else
201: p = s;
202: if (auth_userokay(pw->pw_name, nstyle, "auth-lock", p))
1.1 deraadt 203: break;
1.13 millert 204: } else if (strcmp(s, s1) == 0)
1.1 deraadt 205: break;
1.13 millert 206: (void)putc('\a', stderr);
1.24 martynas 207: cnt %= tries;
208: if (++cnt > backoff) {
209: sigset_t set, oset;
210: sigfillset(&set);
211: sigprocmask(SIG_BLOCK, &set, &oset);
212: sleep((u_int)((cnt - backoff) * tries / 2));
213: sigprocmask(SIG_SETMASK, &oset, NULL);
214: }
1.1 deraadt 215: }
1.9 millert 216:
1.13 millert 217: exit(0);
1.1 deraadt 218: }
219:
1.23 deraadt 220: /*ARGSUSED*/
1.1 deraadt 221: void
1.23 deraadt 222: hi(int signo)
1.1 deraadt 223: {
1.14 deraadt 224: char buf[1024], buf2[1024];
1.13 millert 225: time_t now;
1.1 deraadt 226:
1.14 deraadt 227: if (no_timeout)
228: buf2[0] = '\0';
229: else {
1.13 millert 230: now = time(NULL);
1.14 deraadt 231: (void)snprintf(buf2, sizeof buf2, " timeout in %d:%d minutes",
232: (nexttime - now) / 60, (nexttime - now) % 60);
1.6 downsj 233: }
1.23 deraadt 234: (void)snprintf(buf, sizeof buf, "%s: type in the unlock key.%s\n",
1.14 deraadt 235: __progname, buf2);
236: (void) write(STDERR_FILENO, buf, strlen(buf));
1.1 deraadt 237: }
238:
1.23 deraadt 239: /*ARGSUSED*/
1.1 deraadt 240: void
1.23 deraadt 241: bye(int signo)
1.1 deraadt 242: {
243:
1.13 millert 244: if (!no_timeout)
1.16 millert 245: warnx("timeout");
246: _exit(1);
1.1 deraadt 247: }