Annotation of src/usr.bin/lock/lock.c, Revision 1.42
1.42 ! cheloha 1: /* $OpenBSD: lock.c,v 1.41 2017/09/06 21:02:31 tb 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/stat.h>
45: #include <sys/time.h>
46: #include <signal.h>
47:
48: #include <ctype.h>
49: #include <err.h>
50: #include <pwd.h>
1.13 millert 51: #include <readpassphrase.h>
1.1 deraadt 52: #include <stdio.h>
1.2 deraadt 53: #include <stdlib.h>
1.1 deraadt 54: #include <string.h>
55: #include <termios.h>
1.2 deraadt 56: #include <unistd.h>
1.28 deraadt 57: #include <limits.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.42 ! cheloha 66: void usage(void);
1.1 deraadt 67:
1.42 ! cheloha 68: int custom_timeout;
1.9 millert 69: int no_timeout; /* lock terminal forever */
1.1 deraadt 70:
1.9 millert 71: int
1.18 deraadt 72: main(int argc, char *argv[])
1.1 deraadt 73: {
1.28 deraadt 74: char hostname[HOST_NAME_MAX+1], s[BUFSIZ], s1[BUFSIZ], date[256];
1.38 tedu 75: char hash[_PASSWORD_LEN];
1.18 deraadt 76: char *p, *style, *nstyle, *ttynam;
77: struct itimerval ntimer, otimer;
1.37 tedu 78: struct timeval timeout;
1.24 martynas 79: int ch, sectimeout, usemine, cnt, tries = 10, backoff = 3;
1.22 deraadt 80: const char *errstr;
1.1 deraadt 81: struct passwd *pw;
82: struct tm *timp;
83: time_t curtime;
1.13 millert 84: login_cap_t *lc;
1.1 deraadt 85:
86: sectimeout = TIMEOUT;
1.13 millert 87: style = NULL;
1.1 deraadt 88: usemine = 0;
1.42 ! cheloha 89: custom_timeout = no_timeout = 0;
1.37 tedu 90: memset(&timeout, 0, sizeof(timeout));
1.29 deraadt 91:
1.32 tedu 92: if (pledge("stdio rpath wpath getpw tty proc exec", NULL) == -1)
1.30 deraadt 93: err(1, "pledge");
1.1 deraadt 94:
95: if (!(pw = getpwuid(getuid())))
1.17 deraadt 96: errx(1, "unknown uid %u.", getuid());
1.13 millert 97:
98: lc = login_getclass(pw->pw_class);
1.24 martynas 99: if (lc != NULL) {
100: /*
101: * We allow "login-tries" attempts to login but start
102: * slowing down after "login-backoff" attempts.
103: */
1.40 tedu 104: tries = login_getcapnum(lc, "login-tries", 10, 10);
105: backoff = login_getcapnum(lc, "login-backoff", 3, 3);
1.24 martynas 106: }
107:
1.33 tedu 108: while ((ch = getopt(argc, argv, "a:npt:")) != -1) {
1.19 deraadt 109: switch (ch) {
1.13 millert 110: case 'a':
111: if (lc) {
112: style = login_getstyle(lc, optarg, "auth-lock");
113: if (style == NULL)
114: errx(1,
115: "invalid authentication style: %s",
116: optarg);
117: }
118: usemine = 1;
119: break;
1.1 deraadt 120: case 't':
1.42 ! cheloha 121: if (no_timeout)
! 122: usage();
1.40 tedu 123: sectimeout = strtonum(optarg, 1, INT_MAX, &errstr);
1.22 deraadt 124: if (errstr)
125: errx(1, "timeout %s: %s", errstr, optarg);
1.42 ! cheloha 126: custom_timeout = 1;
1.1 deraadt 127: break;
128: case 'p':
129: usemine = 1;
130: break;
1.6 downsj 131: case 'n':
1.42 ! cheloha 132: if (custom_timeout)
! 133: usage();
1.6 downsj 134: no_timeout = 1;
135: break;
1.1 deraadt 136: default:
1.42 ! cheloha 137: usage();
1.33 tedu 138: }
1.1 deraadt 139: }
140: timeout.tv_sec = sectimeout * 60;
141:
142: gethostname(hostname, sizeof(hostname));
1.26 tobias 143: if (usemine && lc == NULL)
144: errx(1, "login class not found");
1.13 millert 145: if (!(ttynam = ttyname(STDIN_FILENO)))
1.1 deraadt 146: errx(1, "not a terminal?");
1.13 millert 147: curtime = time(NULL);
1.1 deraadt 148: timp = localtime(&curtime);
1.13 millert 149: strftime(date, sizeof(date), "%c", timp);
1.1 deraadt 150:
1.13 millert 151: if (!usemine) {
1.1 deraadt 152: /* get key and check again */
1.13 millert 153: if (!readpassphrase("Key: ", s, sizeof(s), RPP_ECHO_OFF) ||
154: *s == '\0')
155: exit(0);
1.1 deraadt 156: /*
157: * Don't need EOF test here, if we get EOF, then s1 != s
158: * and the right things will happen.
159: */
1.40 tedu 160: readpassphrase("Again: ", s1, sizeof(s1), RPP_ECHO_OFF);
1.1 deraadt 161: if (strcmp(s1, s)) {
1.13 millert 162: warnx("\apasswords didn't match.");
1.1 deraadt 163: exit(1);
164: }
1.38 tedu 165: crypt_newhash(s, "bcrypt", hash, sizeof(hash));
1.34 mestre 166: explicit_bzero(s, sizeof(s));
1.38 tedu 167: explicit_bzero(s1, sizeof(s1));
1.1 deraadt 168: }
169:
170: /* set signal handlers */
1.40 tedu 171: signal(SIGINT, hi);
172: signal(SIGQUIT, hi);
173: signal(SIGTSTP, hi);
174: signal(SIGALRM, bye);
1.1 deraadt 175:
1.37 tedu 176: memset(&ntimer, 0, sizeof(ntimer));
1.1 deraadt 177: ntimer.it_value = timeout;
1.6 downsj 178: if (!no_timeout)
179: setitimer(ITIMER_REAL, &ntimer, &otimer);
1.1 deraadt 180:
181: /* header info */
1.6 downsj 182: if (no_timeout) {
1.40 tedu 183: fprintf(stderr,
1.13 millert 184: "%s: %s on %s. no timeout\ntime now is %s\n",
1.39 tedu 185: getprogname(), ttynam, hostname, date);
1.6 downsj 186: } else {
1.40 tedu 187: fprintf(stderr,
1.13 millert 188: "%s: %s on %s. timeout in %d minutes\ntime now is %s\n",
1.39 tedu 189: getprogname(), ttynam, hostname, sectimeout, date);
1.6 downsj 190: }
1.1 deraadt 191:
1.24 martynas 192: for (cnt = 0;;) {
1.35 tedu 193: if (!readpassphrase("Key: ", s, sizeof(s), RPP_ECHO_OFF))
194: continue;
195: if (strlen(s) == 0) {
1.9 millert 196: hi(0);
1.1 deraadt 197: continue;
198: }
199: if (usemine) {
1.13 millert 200: /*
201: * If user entered 's/key' or the style specified via
202: * the '-a' argument, auth_userokay() will prompt
203: * for a new password. Otherwise, use what we have.
204: */
205: if ((strcmp(s, "s/key") == 0 &&
206: (nstyle = login_getstyle(lc, "skey", "auth-lock")))
207: || ((nstyle = style) && strcmp(s, nstyle) == 0))
208: p = NULL;
209: else
210: p = s;
1.34 mestre 211: if (auth_userokay(pw->pw_name, nstyle, "auth-lock",
212: p)) {
213: explicit_bzero(s, sizeof(s));
1.1 deraadt 214: break;
1.34 mestre 215: }
1.38 tedu 216: } else if (crypt_checkpass(s, hash) == 0) {
1.34 mestre 217: explicit_bzero(s, sizeof(s));
1.41 tb 218: explicit_bzero(hash, sizeof(hash));
1.1 deraadt 219: break;
1.34 mestre 220: }
1.40 tedu 221: putc('\a', stderr);
1.24 martynas 222: cnt %= tries;
223: if (++cnt > backoff) {
224: sigset_t set, oset;
225: sigfillset(&set);
226: sigprocmask(SIG_BLOCK, &set, &oset);
227: sleep((u_int)((cnt - backoff) * tries / 2));
228: sigprocmask(SIG_SETMASK, &oset, NULL);
229: }
1.1 deraadt 230: }
1.9 millert 231:
1.13 millert 232: exit(0);
1.1 deraadt 233: }
234:
235: void
1.23 deraadt 236: hi(int signo)
1.1 deraadt 237: {
1.36 tedu 238: struct itimerval left;
1.1 deraadt 239:
1.40 tedu 240: dprintf(STDERR_FILENO, "%s: type in the unlock key.",
1.39 tedu 241: getprogname());
1.36 tedu 242: if (!no_timeout) {
1.40 tedu 243: getitimer(ITIMER_REAL, &left);
244: dprintf(STDERR_FILENO, " timeout in %lld:%02d minutes",
1.36 tedu 245: (long long)(left.it_value.tv_sec / 60),
246: (int)(left.it_value.tv_sec % 60));
247: }
1.40 tedu 248: dprintf(STDERR_FILENO, "\n");
1.1 deraadt 249: }
250:
251: void
1.23 deraadt 252: bye(int signo)
1.1 deraadt 253: {
254:
1.13 millert 255: if (!no_timeout)
1.16 millert 256: warnx("timeout");
257: _exit(1);
1.42 ! cheloha 258: }
! 259:
! 260: void
! 261: usage(void)
! 262: {
! 263: fprintf(stderr, "usage: %s [-np] [-a style] [-t timeout]\n",
! 264: getprogname());
! 265: exit(1);
1.1 deraadt 266: }