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