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