lock.c revision 1.5 1 /* $NetBSD: lock.c,v 1.5 1995/05/02 01:22:58 mycroft 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.5 1995/05/02 01:22:58 mycroft 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
65 #include <ctype.h>
66 #include <err.h>
67 #include <pwd.h>
68 #include <stdio.h>
69 #include <string.h>
70 #include <termios.h>
71
72 #define TIMEOUT 15
73
74 void quit(), bye(), hi();
75
76 struct timeval timeout;
77 struct timeval zerotime;
78 struct termios tty, ntty;
79 long nexttime; /* keep the timeout time */
80
81 /*ARGSUSED*/
82 main(argc, argv)
83 int argc;
84 char **argv;
85 {
86 extern char *optarg;
87 extern int errno, optind;
88 struct passwd *pw;
89 struct timeval timval;
90 struct itimerval ntimer, otimer;
91 struct tm *timp;
92 int ch, sectimeout, usemine;
93 char *ap, *mypw, *ttynam, *tzn;
94 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
95 char *crypt(), *ttyname();
96
97 sectimeout = TIMEOUT;
98 mypw = NULL;
99 usemine = 0;
100
101 if (!(pw = getpwuid(getuid())))
102 errx(1, "unknown uid %d.", getuid());
103
104 while ((ch = getopt(argc, argv, "pt:")) != EOF)
105 switch((char)ch) {
106 case 't':
107 if ((sectimeout = atoi(optarg)) <= 0)
108 errx(1, "illegal timeout value: %s", optarg);
109 break;
110 case 'p':
111 usemine = 1;
112 mypw = strdup(pw->pw_passwd);
113 break;
114 case '?':
115 default:
116 (void)fprintf(stderr,
117 "usage: lock [-p] [-t timeout]\n");
118 exit(1);
119 }
120 timeout.tv_sec = sectimeout * 60;
121
122 setuid(getuid()); /* discard privs */
123
124 if (tcgetattr(0, &tty) < 0) /* get information for header */
125 exit(1);
126 gethostname(hostname, sizeof(hostname));
127 if (!(ttynam = ttyname(0)))
128 errx(1, "not a terminal?");
129 if (gettimeofday(&timval, (struct timezone *)NULL))
130 err(1, "gettimeofday");
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.c_lflag &= ~ECHO;
139 (void)tcsetattr(0, TCSADRAIN, &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("\alock: passwords didn't match.\n");
155 (void)tcsetattr(0, TCSADRAIN, &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 #ifdef SKEY
186 if (strcasecmp(s, "s/key") == 0) {
187 if (skey_auth(pw->pw_name))
188 break;
189 }
190 #endif
191 if (!strcmp(mypw, crypt(s, mypw)))
192 break;
193 }
194 else if (!strcmp(s, s1))
195 break;
196 (void)printf("\a\n");
197 if (tcsetattr(0, TCSADRAIN, &ntty) < 0)
198 exit(1);
199 }
200 quit();
201 }
202
203 #ifdef SKEY
204 /*
205 * We can't use libskey's skey_authenticate() since it
206 * handles signals in a way that's inappropriate
207 * for our needs. Instead we roll our own.
208 */
209 int
210 skey_auth(char *user)
211 {
212 char s[128], *ask, *skey_keyinfo __P((char *name));
213 int ret = 0;
214
215 if (!skey_haskey(user) && (ask = skey_keyinfo(user))) {
216 printf("\n[%s]\nResponse: ", ask);
217 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
218 clearerr(stdin);
219 else {
220 s[strlen(s) - 1] = '\0';
221 if (skey_passcheck(user, s) != -1)
222 ret = 1;
223 }
224 } else
225 printf("Sorry, you have no s/key.\n");
226 return ret;
227 }
228 #endif
229
230 void
231 hi()
232 {
233 struct timeval timval;
234
235 if (!gettimeofday(&timval, (struct timezone *)NULL))
236 (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
237 (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
238 }
239
240 void
241 quit()
242 {
243 (void)putchar('\n');
244 (void)tcsetattr(0, TCSADRAIN, &tty);
245 exit(0);
246 }
247
248 void
249 bye()
250 {
251 (void)tcsetattr(0, TCSADRAIN, &tty);
252 (void)printf("lock: timeout\n");
253 exit(1);
254 }
255