Commit | Line | Data |
---|---|---|
22e155fc | 1 | /* |
750362f3 KB |
2 | * Copyright (c) 1980, 1987 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
f15db449 | 5 | * %sccs.include.redist.c% |
22e155fc DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
750362f3 | 10 | "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\ |
22e155fc | 11 | All rights reserved.\n"; |
750362f3 | 12 | #endif /* not lint */ |
22e155fc | 13 | |
4ca10280 | 14 | #ifndef lint |
f15db449 | 15 | static char sccsid[] = "@(#)lock.c 5.13 (Berkeley) %G%"; |
750362f3 | 16 | #endif /* not lint */ |
4ca10280 SL |
17 | |
18 | /* | |
f84e1e5b KB |
19 | * Lock a terminal up until the given key is entered, until the root |
20 | * password is entered, or the given interval times out. | |
d523b17b RC |
21 | * |
22 | * Timeout interval is by default TIMEOUT, it can be changed with | |
23 | * an argument of the form -time where time is in minutes | |
4ca10280 | 24 | */ |
d523b17b | 25 | |
654949dc | 26 | #include <sys/param.h> |
36e4dcd1 | 27 | #include <sys/stat.h> |
d523b17b | 28 | #include <sys/time.h> |
654949dc | 29 | #include <sys/signal.h> |
d97d0c53 | 30 | #include <sgtty.h> |
92d691ba | 31 | #include <pwd.h> |
654949dc | 32 | #include <stdio.h> |
2cbfd2f5 | 33 | #include <ctype.h> |
38dde0cd | 34 | #include <string.h> |
d97d0c53 | 35 | |
f84e1e5b | 36 | #define TIMEOUT 15 |
d523b17b | 37 | |
eb8ba43e | 38 | void quit(), bye(), hi(); |
d523b17b | 39 | |
654949dc KB |
40 | struct timeval timeout; |
41 | struct timeval zerotime; | |
d523b17b | 42 | struct sgttyb tty, ntty; |
f84e1e5b | 43 | long nexttime; /* keep the timeout time */ |
d97d0c53 | 44 | |
f84e1e5b KB |
45 | /*ARGSUSED*/ |
46 | main(argc, argv) | |
fcc2be53 KB |
47 | int argc; |
48 | char **argv; | |
d97d0c53 | 49 | { |
fcc2be53 | 50 | extern char *optarg; |
92d691ba KB |
51 | extern int errno, optind; |
52 | struct passwd *pw; | |
fcc2be53 KB |
53 | struct timeval timval; |
54 | struct itimerval ntimer, otimer; | |
55 | struct tm *timp; | |
92d691ba | 56 | int ch, sectimeout, usemine; |
d91fe873 | 57 | char *ap, *mypw, *ttynam, *tzn; |
fcc2be53 | 58 | char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ]; |
92d691ba | 59 | char *crypt(), *ttyname(); |
fcc2be53 | 60 | |
fcc2be53 | 61 | sectimeout = TIMEOUT; |
92d691ba KB |
62 | mypw = NULL; |
63 | usemine = 0; | |
fcc2be53 KB |
64 | while ((ch = getopt(argc, argv, "pt:")) != EOF) |
65 | switch((char)ch) { | |
66 | case 't': | |
eb8ba43e KB |
67 | if ((sectimeout = atoi(optarg)) <= 0) { |
68 | (void)fprintf(stderr, | |
69 | "lock: illegal timeout value.\n"); | |
92d691ba | 70 | exit(1); |
eb8ba43e | 71 | } |
fcc2be53 KB |
72 | break; |
73 | case 'p': | |
92d691ba KB |
74 | usemine = 1; |
75 | if (!(pw = getpwuid(getuid()))) { | |
eb8ba43e KB |
76 | (void)fprintf(stderr, |
77 | "lock: unknown uid %d.\n", getuid()); | |
92d691ba KB |
78 | exit(1); |
79 | } | |
80 | mypw = strdup(pw->pw_passwd); | |
fcc2be53 KB |
81 | break; |
82 | case '?': | |
83 | default: | |
eb8ba43e KB |
84 | (void)fprintf(stderr, |
85 | "usage: lock [-p] [-t timeout]\n"); | |
fcc2be53 | 86 | exit(1); |
d523b17b RC |
87 | } |
88 | timeout.tv_sec = sectimeout * 60; | |
89 | ||
92d691ba KB |
90 | setuid(getuid()); /* discard privs */ |
91 | ||
92 | if (ioctl(0, TIOCGETP, &tty)) /* get information for header */ | |
d97d0c53 | 93 | exit(1); |
d523b17b | 94 | gethostname(hostname, sizeof(hostname)); |
654949dc | 95 | if (!(ttynam = ttyname(0))) { |
eb8ba43e | 96 | (void)printf("lock: not a terminal?\n"); |
f84e1e5b | 97 | exit(1); |
654949dc KB |
98 | } |
99 | if (gettimeofday(&timval, (struct timezone *)NULL)) { | |
eb8ba43e KB |
100 | (void)fprintf(stderr, |
101 | "lock: gettimeofday: %s\n", strerror(errno)); | |
f84e1e5b | 102 | exit(1); |
d523b17b | 103 | } |
d523b17b RC |
104 | nexttime = timval.tv_sec + (sectimeout * 60); |
105 | timp = localtime(&timval.tv_sec); | |
106 | ap = asctime(timp); | |
654949dc | 107 | tzn = timp->tm_zone; |
d523b17b | 108 | |
654949dc KB |
109 | (void)signal(SIGINT, quit); |
110 | (void)signal(SIGQUIT, quit); | |
d97d0c53 | 111 | ntty = tty; ntty.sg_flags &= ~ECHO; |
654949dc KB |
112 | (void)ioctl(0, TIOCSETP, &ntty); |
113 | ||
92d691ba | 114 | if (!mypw) { |
f84e1e5b | 115 | /* get key and check again */ |
eb8ba43e | 116 | (void)printf("Key: "); |
92d691ba | 117 | if (!fgets(s, sizeof(s), stdin) || *s == '\n') |
f84e1e5b | 118 | quit(); |
eb8ba43e | 119 | (void)printf("\nAgain: "); |
f84e1e5b KB |
120 | /* |
121 | * Don't need EOF test here, if we get EOF, then s1 != s | |
122 | * and the right things will happen. | |
123 | */ | |
92d691ba | 124 | (void)fgets(s1, sizeof(s1), stdin); |
eb8ba43e | 125 | (void)putchar('\n'); |
f84e1e5b | 126 | if (strcmp(s1, s)) { |
eb8ba43e | 127 | (void)printf("\07lock: passwords didn't match.\n"); |
f84e1e5b KB |
128 | ioctl(0, TIOCSETP, &tty); |
129 | exit(1); | |
130 | } | |
131 | s[0] = NULL; | |
92d691ba | 132 | mypw = s1; |
d97d0c53 | 133 | } |
d523b17b | 134 | |
f84e1e5b | 135 | /* set signal handlers */ |
654949dc KB |
136 | (void)signal(SIGINT, hi); |
137 | (void)signal(SIGQUIT, hi); | |
138 | (void)signal(SIGTSTP, hi); | |
139 | (void)signal(SIGALRM, bye); | |
140 | ||
d523b17b RC |
141 | ntimer.it_interval = zerotime; |
142 | ntimer.it_value = timeout; | |
143 | setitimer(ITIMER_REAL, &ntimer, &otimer); | |
144 | ||
f84e1e5b | 145 | /* header info */ |
eb8ba43e KB |
146 | (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s", |
147 | ttynam, hostname, sectimeout, ap, tzn, ap + 19); | |
d523b17b | 148 | |
f84e1e5b | 149 | for (;;) { |
eb8ba43e | 150 | (void)printf("Key: "); |
92d691ba | 151 | if (!fgets(s, sizeof(s), stdin)) { |
3e58f554 RE |
152 | clearerr(stdin); |
153 | hi(); | |
154 | continue; | |
155 | } | |
92d691ba | 156 | if (usemine) { |
2a36a68c | 157 | s[strlen(s) - 1] = '\0'; |
92d691ba | 158 | if (!strcmp(mypw, crypt(s, mypw))) |
f84e1e5b KB |
159 | break; |
160 | } | |
92d691ba | 161 | else if (!strcmp(s, s1)) |
f84e1e5b | 162 | break; |
eb8ba43e | 163 | (void)printf("\07\n"); |
4ca10280 | 164 | if (ioctl(0, TIOCGETP, &ntty)) |
d97d0c53 BJ |
165 | exit(1); |
166 | } | |
654949dc | 167 | quit(); |
d523b17b RC |
168 | } |
169 | ||
eb8ba43e | 170 | void |
654949dc | 171 | hi() |
d523b17b | 172 | { |
fcc2be53 | 173 | struct timeval timval; |
d523b17b | 174 | |
654949dc | 175 | if (!gettimeofday(&timval, (struct timezone *)NULL)) |
eb8ba43e | 176 | (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n", |
f84e1e5b | 177 | (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60); |
d523b17b RC |
178 | } |
179 | ||
eb8ba43e | 180 | void |
654949dc | 181 | quit() |
d523b17b | 182 | { |
eb8ba43e | 183 | (void)putchar('\n'); |
654949dc | 184 | (void)ioctl(0, TIOCSETP, &tty); |
f84e1e5b | 185 | exit(0); |
654949dc | 186 | } |
d523b17b | 187 | |
eb8ba43e | 188 | void |
654949dc KB |
189 | bye() |
190 | { | |
191 | (void)ioctl(0, TIOCSETP, &tty); | |
eb8ba43e | 192 | (void)printf("lock: timeout\n"); |
f84e1e5b KB |
193 | exit(1); |
194 | } |