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