| 1 | /* |
| 2 | * Copyright (c) 1980, 1987 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | char copyright[] = |
| 36 | "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\ |
| 37 | All rights reserved.\n"; |
| 38 | #endif /* not lint */ |
| 39 | |
| 40 | #ifndef lint |
| 41 | static char sccsid[] = "@(#)lock.c 5.13 (Berkeley) 6/1/90"; |
| 42 | #endif /* not lint */ |
| 43 | |
| 44 | /* |
| 45 | * Lock a terminal up until the given key is entered, until the root |
| 46 | * password is entered, or the given 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/time.h> |
| 55 | #include <sys/signal.h> |
| 56 | #include <sgtty.h> |
| 57 | #include <pwd.h> |
| 58 | #include <stdio.h> |
| 59 | #include <ctype.h> |
| 60 | #include <string.h> |
| 61 | |
| 62 | #define TIMEOUT 15 |
| 63 | |
| 64 | void quit(), bye(), hi(); |
| 65 | |
| 66 | struct timeval timeout; |
| 67 | struct timeval zerotime; |
| 68 | struct sgttyb tty, ntty; |
| 69 | long nexttime; /* keep the timeout time */ |
| 70 | |
| 71 | /*ARGSUSED*/ |
| 72 | main(argc, argv) |
| 73 | int argc; |
| 74 | char **argv; |
| 75 | { |
| 76 | extern char *optarg; |
| 77 | extern int errno, optind; |
| 78 | struct passwd *pw; |
| 79 | struct timeval timval; |
| 80 | struct itimerval ntimer, otimer; |
| 81 | struct tm *timp; |
| 82 | int ch, sectimeout, usemine; |
| 83 | char *ap, *mypw, *ttynam, *tzn; |
| 84 | char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ]; |
| 85 | char *crypt(), *ttyname(); |
| 86 | |
| 87 | sectimeout = TIMEOUT; |
| 88 | mypw = NULL; |
| 89 | usemine = 0; |
| 90 | while ((ch = getopt(argc, argv, "pt:")) != EOF) |
| 91 | switch((char)ch) { |
| 92 | case 't': |
| 93 | if ((sectimeout = atoi(optarg)) <= 0) { |
| 94 | (void)fprintf(stderr, |
| 95 | "lock: illegal timeout value.\n"); |
| 96 | exit(1); |
| 97 | } |
| 98 | break; |
| 99 | case 'p': |
| 100 | usemine = 1; |
| 101 | if (!(pw = getpwuid(getuid()))) { |
| 102 | (void)fprintf(stderr, |
| 103 | "lock: unknown uid %d.\n", getuid()); |
| 104 | exit(1); |
| 105 | } |
| 106 | mypw = strdup(pw->pw_passwd); |
| 107 | break; |
| 108 | case '?': |
| 109 | default: |
| 110 | (void)fprintf(stderr, |
| 111 | "usage: lock [-p] [-t timeout]\n"); |
| 112 | exit(1); |
| 113 | } |
| 114 | timeout.tv_sec = sectimeout * 60; |
| 115 | |
| 116 | setuid(getuid()); /* discard privs */ |
| 117 | |
| 118 | if (ioctl(0, TIOCGETP, &tty)) /* get information for header */ |
| 119 | exit(1); |
| 120 | gethostname(hostname, sizeof(hostname)); |
| 121 | if (!(ttynam = ttyname(0))) { |
| 122 | (void)printf("lock: not a terminal?\n"); |
| 123 | exit(1); |
| 124 | } |
| 125 | if (gettimeofday(&timval, (struct timezone *)NULL)) { |
| 126 | (void)fprintf(stderr, |
| 127 | "lock: gettimeofday: %s\n", strerror(errno)); |
| 128 | exit(1); |
| 129 | } |
| 130 | nexttime = timval.tv_sec + (sectimeout * 60); |
| 131 | timp = localtime(&timval.tv_sec); |
| 132 | ap = asctime(timp); |
| 133 | tzn = timp->tm_zone; |
| 134 | |
| 135 | (void)signal(SIGINT, quit); |
| 136 | (void)signal(SIGQUIT, quit); |
| 137 | ntty = tty; ntty.sg_flags &= ~ECHO; |
| 138 | (void)ioctl(0, TIOCSETP, &ntty); |
| 139 | |
| 140 | if (!mypw) { |
| 141 | /* get key and check again */ |
| 142 | (void)printf("Key: "); |
| 143 | if (!fgets(s, sizeof(s), stdin) || *s == '\n') |
| 144 | quit(); |
| 145 | (void)printf("\nAgain: "); |
| 146 | /* |
| 147 | * Don't need EOF test here, if we get EOF, then s1 != s |
| 148 | * and the right things will happen. |
| 149 | */ |
| 150 | (void)fgets(s1, sizeof(s1), stdin); |
| 151 | (void)putchar('\n'); |
| 152 | if (strcmp(s1, s)) { |
| 153 | (void)printf("\07lock: passwords didn't match.\n"); |
| 154 | ioctl(0, TIOCSETP, &tty); |
| 155 | exit(1); |
| 156 | } |
| 157 | s[0] = NULL; |
| 158 | mypw = s1; |
| 159 | } |
| 160 | |
| 161 | /* set signal handlers */ |
| 162 | (void)signal(SIGINT, hi); |
| 163 | (void)signal(SIGQUIT, hi); |
| 164 | (void)signal(SIGTSTP, hi); |
| 165 | (void)signal(SIGALRM, bye); |
| 166 | |
| 167 | ntimer.it_interval = zerotime; |
| 168 | ntimer.it_value = timeout; |
| 169 | setitimer(ITIMER_REAL, &ntimer, &otimer); |
| 170 | |
| 171 | /* header info */ |
| 172 | (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s", |
| 173 | ttynam, hostname, sectimeout, ap, tzn, ap + 19); |
| 174 | |
| 175 | for (;;) { |
| 176 | (void)printf("Key: "); |
| 177 | if (!fgets(s, sizeof(s), stdin)) { |
| 178 | clearerr(stdin); |
| 179 | hi(); |
| 180 | continue; |
| 181 | } |
| 182 | if (usemine) { |
| 183 | s[strlen(s) - 1] = '\0'; |
| 184 | if (!strcmp(mypw, crypt(s, mypw))) |
| 185 | break; |
| 186 | } |
| 187 | else if (!strcmp(s, s1)) |
| 188 | break; |
| 189 | (void)printf("\07\n"); |
| 190 | if (ioctl(0, TIOCGETP, &ntty)) |
| 191 | exit(1); |
| 192 | } |
| 193 | quit(); |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | hi() |
| 198 | { |
| 199 | struct timeval timval; |
| 200 | |
| 201 | if (!gettimeofday(&timval, (struct timezone *)NULL)) |
| 202 | (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n", |
| 203 | (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | quit() |
| 208 | { |
| 209 | (void)putchar('\n'); |
| 210 | (void)ioctl(0, TIOCSETP, &tty); |
| 211 | exit(0); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | bye() |
| 216 | { |
| 217 | (void)ioctl(0, TIOCSETP, &tty); |
| 218 | (void)printf("lock: timeout\n"); |
| 219 | exit(1); |
| 220 | } |