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