1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1987, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Bob Toxen. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1980, 1987, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif /* not lint */ 40 41 #ifndef lint 42 #endif /* not lint */ 43 #include <sys/cdefs.h> 44 /* 45 * Lock a terminal up until the given key is entered or the given 46 * interval times out. 47 * 48 * Timeout interval is by default TIMEOUT, it can be changed with 49 * an argument of the form -time where time is in minutes 50 */ 51 52 #include <sys/param.h> 53 #include <sys/stat.h> 54 #include <sys/signal.h> 55 #include <sys/consio.h> 56 57 #include <err.h> 58 #include <ctype.h> 59 #include <errno.h> 60 #include <paths.h> 61 #include <pwd.h> 62 #include <stdint.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <syslog.h> 67 #include <termios.h> 68 #include <time.h> 69 #include <unistd.h> 70 71 #include <security/pam_appl.h> 72 #include <security/openpam.h> /* for openpam_ttyconv() */ 73 74 #define TIMEOUT 15 75 76 static void quit(int); 77 static void bye(int); 78 static void hi(int); 79 static void usage(void) __dead2; 80 81 static struct timeval timeout; 82 static struct timeval zerotime; 83 static struct termios tty, ntty; 84 static long nexttime; /* keep the timeout time */ 85 static int no_timeout; /* lock terminal forever */ 86 static int vtyunlock; /* Unlock flag and code. */ 87 88 /*ARGSUSED*/ 89 int 90 main(int argc, char **argv) 91 { 92 static const struct pam_conv pamc = { &openpam_ttyconv, NULL }; 93 pam_handle_t *pamh; 94 struct passwd *pw; 95 struct itimerval ntimer, otimer; 96 struct tm *timp; 97 time_t timval; 98 int ch, failures, pam_err, sectimeout, usemine, vtylock; 99 char *ap, *ttynam, *tzn; 100 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ]; 101 102 openlog("lock", 0, LOG_AUTH); 103 104 pam_err = PAM_SYSTEM_ERR; /* pacify GCC */ 105 106 sectimeout = TIMEOUT; 107 pamh = NULL; 108 pw = NULL; 109 usemine = 0; 110 no_timeout = 0; 111 vtylock = 0; 112 while ((ch = getopt(argc, argv, "npt:v")) != -1) 113 switch((char)ch) { 114 case 't': 115 if ((sectimeout = atoi(optarg)) <= 0) 116 errx(1, "illegal timeout value"); 117 break; 118 case 'p': 119 usemine = 1; 120 if (!(pw = getpwuid(getuid()))) 121 errx(1, "unknown uid %d", getuid()); 122 break; 123 case 'n': 124 no_timeout = 1; 125 break; 126 case 'v': 127 vtylock = 1; 128 break; 129 case '?': 130 default: 131 usage(); 132 } 133 timeout.tv_sec = sectimeout * 60; 134 135 if (!usemine) { /* -p with PAM or S/key needs privs */ 136 /* discard privs */ 137 if (setuid(getuid()) != 0) 138 errx(1, "setuid failed"); 139 } 140 141 if (tcgetattr(0, &tty)) /* get information for header */ 142 exit(1); 143 gethostname(hostname, sizeof(hostname)); 144 if (!(ttynam = ttyname(0))) 145 errx(1, "not a terminal?"); 146 if (strncmp(ttynam, _PATH_DEV, strlen(_PATH_DEV)) == 0) 147 ttynam += strlen(_PATH_DEV); 148 timval = time(NULL); 149 nexttime = timval + (sectimeout * 60); 150 timp = localtime(&timval); 151 ap = asctime(timp); 152 tzn = timp->tm_zone; 153 154 (void)signal(SIGINT, quit); 155 (void)signal(SIGQUIT, quit); 156 ntty = tty; ntty.c_lflag &= ~ECHO; 157 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &ntty); 158 159 if (usemine) { 160 pam_err = pam_start("lock", pw->pw_name, &pamc, &pamh); 161 if (pam_err != PAM_SUCCESS) 162 err(1, "pam_start: %s", pam_strerror(NULL, pam_err)); 163 } else { 164 /* get key and check again */ 165 (void)printf("Key: "); 166 if (!fgets(s, sizeof(s), stdin) || *s == '\n') 167 quit(0); 168 (void)printf("\nAgain: "); 169 /* 170 * Don't need EOF test here, if we get EOF, then s1 != s 171 * and the right things will happen. 172 */ 173 (void)fgets(s1, sizeof(s1), stdin); 174 (void)putchar('\n'); 175 if (strcmp(s1, s)) { 176 (void)printf("\07lock: passwords didn't match.\n"); 177 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); 178 exit(1); 179 } 180 s[0] = '\0'; 181 } 182 183 /* set signal handlers */ 184 (void)signal(SIGINT, hi); 185 (void)signal(SIGQUIT, hi); 186 (void)signal(SIGTSTP, hi); 187 (void)signal(SIGALRM, bye); 188 189 ntimer.it_interval = zerotime; 190 ntimer.it_value = timeout; 191 if (!no_timeout) 192 setitimer(ITIMER_REAL, &ntimer, &otimer); 193 if (vtylock) { 194 /* 195 * If this failed, we want to err out; warn isn't good 196 * enough, since we don't want the user to think that 197 * everything is nice and locked because they got a 198 * "Key:" prompt. 199 */ 200 if (ioctl(0, VT_LOCKSWITCH, &vtylock) == -1) { 201 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); 202 err(1, "locking vty"); 203 } 204 vtyunlock = 0x2; 205 } 206 207 /* header info */ 208 if (pw != NULL) 209 (void)printf("lock: %s using %s on %s.", pw->pw_name, 210 ttynam, hostname); 211 else 212 (void)printf("lock: %s on %s.", ttynam, hostname); 213 if (no_timeout) 214 (void)printf(" no timeout."); 215 else 216 (void)printf(" timeout in %d minute%s.", sectimeout, 217 sectimeout != 1 ? "s" : ""); 218 if (vtylock) 219 (void)printf(" vty locked."); 220 (void)printf("\ntime now is %.20s%s%s", ap, tzn, ap + 19); 221 222 failures = 0; 223 224 for (;;) { 225 if (usemine) { 226 pam_err = pam_authenticate(pamh, 0); 227 if (pam_err == PAM_SUCCESS) 228 break; 229 230 if (pam_err != PAM_AUTH_ERR && 231 pam_err != PAM_USER_UNKNOWN && 232 pam_err != PAM_MAXTRIES) { 233 syslog(LOG_ERR, "pam_authenticate: %s", 234 pam_strerror(pamh, pam_err)); 235 } 236 237 goto tryagain; 238 } 239 (void)printf("Key: "); 240 if (!fgets(s, sizeof(s), stdin)) { 241 clearerr(stdin); 242 hi(0); 243 goto tryagain; 244 } 245 if (!strcmp(s, s1)) 246 break; 247 (void)printf("\07\n"); 248 failures++; 249 if (getuid() == 0) 250 syslog(LOG_NOTICE, "%d ROOT UNLOCK FAILURE%s (%s on %s)", 251 failures, failures > 1 ? "S": "", ttynam, hostname); 252 tryagain: 253 if (tcgetattr(0, &ntty) && (errno != EINTR)) 254 exit(1); 255 sleep(1); /* to discourage guessing */ 256 } 257 if (getuid() == 0) 258 syslog(LOG_NOTICE, "ROOT UNLOCK ON hostname %s port %s", 259 hostname, ttynam); 260 if (usemine) 261 (void)pam_end(pamh, pam_err); 262 quit(0); 263 return(0); /* not reached */ 264 } 265 266 267 static void 268 usage(void) 269 { 270 (void)fprintf(stderr, "usage: lock [-npv] [-t timeout]\n"); 271 exit(1); 272 } 273 274 static void 275 hi(int signo __unused) 276 { 277 time_t timval; 278 279 timval = time(NULL); 280 (void)printf("lock: type in the unlock key. "); 281 if (no_timeout) { 282 (void)putchar('\n'); 283 } else { 284 (void)printf("timeout in %jd:%jd minutes\n", 285 (intmax_t)(nexttime - timval) / 60, 286 (intmax_t)(nexttime - timval) % 60); 287 } 288 } 289 290 static void 291 quit(int signo __unused) 292 { 293 (void)putchar('\n'); 294 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); 295 if (vtyunlock) 296 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock); 297 exit(0); 298 } 299 300 static void 301 bye(int signo __unused) 302 { 303 if (!no_timeout) { 304 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty); 305 if (vtyunlock) 306 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock); 307 (void)printf("lock: timeout\n"); 308 exit(1); 309 } 310 } 311