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