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