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 static const char rcsid[] = 48 "$Id: lock.c,v 1.4 1997/07/21 12:09:34 charnier Exp $"; 49 #endif /* not lint */ 50 51 /* 52 * Lock a terminal up until the given key is entered, until the root 53 * password is entered, or the given 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 <err.h> 64 #include <ctype.h> 65 #include <pwd.h> 66 #include <sgtty.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <unistd.h> 71 72 #define TIMEOUT 15 73 74 void quit(), bye(), hi(); 75 static void usage __P((void)); 76 77 struct timeval timeout; 78 struct timeval zerotime; 79 struct sgttyb tty, ntty; 80 long nexttime; /* keep the timeout time */ 81 int no_timeout; /* lock terminal forever */ 82 83 /*ARGSUSED*/ 84 int 85 main(argc, argv) 86 int argc; 87 char **argv; 88 { 89 struct passwd *pw; 90 struct timeval timval; 91 time_t timval_sec; 92 struct itimerval ntimer, otimer; 93 struct tm *timp; 94 int ch, sectimeout, usemine; 95 char *ap, *mypw, *ttynam, *tzn; 96 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ]; 97 char *crypt(), *ttyname(); 98 99 sectimeout = TIMEOUT; 100 mypw = NULL; 101 usemine = 0; 102 no_timeout = 0; 103 while ((ch = getopt(argc, argv, "npt:")) != -1) 104 switch((char)ch) { 105 case 't': 106 if ((sectimeout = atoi(optarg)) <= 0) 107 errx(1, "illegal timeout value"); 108 break; 109 case 'p': 110 usemine = 1; 111 if (!(pw = getpwuid(getuid()))) 112 errx(1, "unknown uid %d", getuid()); 113 mypw = strdup(pw->pw_passwd); 114 break; 115 case 'n': 116 no_timeout = 1; 117 break; 118 case '?': 119 default: 120 usage(); 121 } 122 timeout.tv_sec = sectimeout * 60; 123 124 setuid(getuid()); /* discard privs */ 125 126 if (ioctl(0, TIOCGETP, &tty)) /* get information for header */ 127 exit(1); 128 gethostname(hostname, sizeof(hostname)); 129 if (!(ttynam = ttyname(0))) 130 errx(1, "not a terminal?"); 131 if (gettimeofday(&timval, (struct timezone *)NULL)) 132 err(1, "gettimeofday"); 133 nexttime = timval.tv_sec + (sectimeout * 60); 134 timval_sec = timval.tv_sec; 135 timp = localtime(&timval_sec); 136 ap = asctime(timp); 137 tzn = timp->tm_zone; 138 139 (void)signal(SIGINT, quit); 140 (void)signal(SIGQUIT, quit); 141 ntty = tty; ntty.sg_flags &= ~ECHO; 142 (void)ioctl(0, TIOCSETP, &ntty); 143 144 if (!mypw) { 145 /* get key and check again */ 146 (void)printf("Key: "); 147 if (!fgets(s, sizeof(s), stdin) || *s == '\n') 148 quit(); 149 (void)printf("\nAgain: "); 150 /* 151 * Don't need EOF test here, if we get EOF, then s1 != s 152 * and the right things will happen. 153 */ 154 (void)fgets(s1, sizeof(s1), stdin); 155 (void)putchar('\n'); 156 if (strcmp(s1, s)) { 157 (void)printf("\07lock: passwords didn't match.\n"); 158 ioctl(0, TIOCSETP, &tty); 159 exit(1); 160 } 161 s[0] = '\0'; 162 mypw = s1; 163 } 164 165 /* set signal handlers */ 166 (void)signal(SIGINT, hi); 167 (void)signal(SIGQUIT, hi); 168 (void)signal(SIGTSTP, hi); 169 (void)signal(SIGALRM, bye); 170 171 ntimer.it_interval = zerotime; 172 ntimer.it_value = timeout; 173 if (!no_timeout) 174 setitimer(ITIMER_REAL, &ntimer, &otimer); 175 176 /* header info */ 177 if (no_timeout) { 178 (void)printf("lock: %s on %s. no timeout\ntime now is %.20s%s%s", 179 ttynam, hostname, ap, tzn, ap + 19); 180 } else { 181 (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s", 182 ttynam, hostname, sectimeout, ap, tzn, ap + 19); 183 } 184 185 for (;;) { 186 (void)printf("Key: "); 187 if (!fgets(s, sizeof(s), stdin)) { 188 clearerr(stdin); 189 hi(); 190 continue; 191 } 192 if (usemine) { 193 s[strlen(s) - 1] = '\0'; 194 if (!strcmp(mypw, crypt(s, mypw))) 195 break; 196 } 197 else if (!strcmp(s, s1)) 198 break; 199 (void)printf("\07\n"); 200 if (ioctl(0, TIOCGETP, &ntty)) 201 exit(1); 202 } 203 quit(); 204 return(0); /* not reached */ 205 } 206 207 208 static void 209 usage() 210 { 211 (void)fprintf(stderr, "usage: lock [-n] [-p] [-t timeout]\n"); 212 exit(1); 213 } 214 215 void 216 hi() 217 { 218 struct timeval timval; 219 220 if (!gettimeofday(&timval, (struct timezone *)NULL)) { 221 (void)printf("lock: type in the unlock key. "); 222 if (no_timeout) { 223 (void)putchar('\n'); 224 } else { 225 (void)printf("timeout in %ld:%ld minutes\n", 226 (nexttime - timval.tv_sec) / 60, 227 (nexttime - timval.tv_sec) % 60); 228 } 229 } 230 } 231 232 void 233 quit() 234 { 235 (void)putchar('\n'); 236 (void)ioctl(0, TIOCSETP, &tty); 237 exit(0); 238 } 239 240 void 241 bye() 242 { 243 if (!no_timeout) { 244 (void)ioctl(0, TIOCSETP, &tty); 245 (void)printf("lock: timeout\n"); 246 exit(1); 247 } 248 } 249