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