lock.c revision 1.19
1/*	$NetBSD: lock.c,v 1.19 2000/07/29 08:40:38 martin 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#include <sys/cdefs.h>
40#ifndef lint
41__COPYRIGHT("@(#) 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
47static char sccsid[] = "@(#)lock.c	8.1 (Berkeley) 6/6/93";
48#endif
49__RCSID("$NetBSD: lock.c,v 1.19 2000/07/29 08:40:38 martin 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 <signal.h>
64
65#include <ctype.h>
66#include <err.h>
67#include <pwd.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <termios.h>
72#include <time.h>
73#include <unistd.h>
74#ifdef SKEY
75#include <skey.h>
76#endif
77
78#define	TIMEOUT	15
79
80void	bye __P((int));
81void	hi __P((int));
82int	main __P((int, char **));
83void	quit __P((int));
84#ifdef SKEY
85int	skey_auth __P((const char *));
86#endif
87
88struct timeval	timeout;
89struct timeval	zerotime;
90struct termios	tty, ntty;
91int	notimeout;			/* no timeout at all */
92long	nexttime;			/* keep the timeout time */
93
94int
95main(argc, argv)
96	int argc;
97	char **argv;
98{
99	struct passwd *pw;
100	struct timeval timval;
101	struct itimerval ntimer, otimer;
102	struct tm *timp;
103	time_t curtime;
104	int ch, sectimeout, usemine;
105	char *ap, *mypw, *ttynam;
106	const char *tzn;
107	char hostname[MAXHOSTNAMELEN + 1], s[BUFSIZ], s1[BUFSIZ];
108
109	if (!(pw = getpwuid(getuid())))
110		errx(1, "unknown uid %ld.", (u_long) getuid());
111
112	setuid(getuid());		/* discard privs */
113
114	notimeout = 0;
115	sectimeout = TIMEOUT;
116	mypw = NULL;
117	usemine = 0;
118
119	while ((ch = getopt(argc, argv, "npt:")) != -1)
120		switch ((char)ch) {
121		case 'n':
122			notimeout = 1;
123			break;
124		case 't':
125			if ((sectimeout = atoi(optarg)) <= 0)
126				errx(1, "illegal timeout value: %s", optarg);
127			break;
128		case 'p':
129			usemine = 1;
130			mypw = strdup(pw->pw_passwd);
131			break;
132		case '?':
133		default:
134			(void)fprintf(stderr,
135			    "usage: lock [-p] [-t timeout]\n");
136			exit(1);
137		}
138	timeout.tv_sec = sectimeout * 60;
139
140	if (tcgetattr(0, &tty) < 0)	/* get information for header */
141		exit(1);
142	gethostname(hostname, sizeof(hostname));
143	hostname[sizeof(hostname) - 1] = '\0';
144	if (!(ttynam = ttyname(0)))
145		errx(1, "not a terminal?");
146	if (gettimeofday(&timval, (struct timezone *)NULL))
147		err(1, "gettimeofday");
148	curtime = timval.tv_sec;
149	nexttime = timval.tv_sec + (sectimeout * 60);
150	timp = localtime(&curtime);
151	ap = asctime(timp);
152#ifdef __SVR4
153	tzn = tzname[0];
154#else
155	tzn = timp->tm_zone;
156#endif
157
158	(void)signal(SIGINT, quit);
159	(void)signal(SIGQUIT, quit);
160	ntty = tty; ntty.c_lflag &= ~ECHO;
161	(void)tcsetattr(0, TCSADRAIN, &ntty);
162
163	if (!mypw) {
164		/* get key and check again */
165		(void)printf("Key: ");
166		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
167			quit(0);
168		(void)printf("\nAgain: ");
169		/*
170		 * Don't need EOF test here, if we get EOF, then s1 != s
171		 * and the right things will happen.
172		 */
173		(void)fgets(s1, sizeof(s1), stdin);
174		(void)putchar('\n');
175		if (strcmp(s1, s)) {
176			(void)printf("\alock: passwords didn't match.\n");
177			(void)tcsetattr(0, TCSADRAIN, &tty);
178			exit(1);
179		}
180		s[0] = '\0';
181		mypw = s1;
182	}
183
184	/* set signal handlers */
185	(void)signal(SIGINT, hi);
186	(void)signal(SIGQUIT, hi);
187	(void)signal(SIGTSTP, hi);
188
189	if (notimeout) {
190		(void)signal(SIGALRM, hi);
191	(void)printf("lock: %s on %s.  no timeout.\ntime now is %.20s%s%s",
192		    ttynam, hostname, ap, tzn, ap + 19);
193	}
194	else {
195		(void)signal(SIGALRM, bye);
196
197		ntimer.it_interval = zerotime;
198		ntimer.it_value = timeout;
199		setitimer(ITIMER_REAL, &ntimer, &otimer);
200
201		/* header info */
202(void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
203	    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
204	}
205
206	for (;;) {
207		(void)printf("Key: ");
208		if (!fgets(s, sizeof(s), stdin)) {
209			clearerr(stdin);
210			hi(0);
211			continue;
212		}
213		if (usemine) {
214			s[strlen(s) - 1] = '\0';
215#ifdef SKEY
216			if (strcasecmp(s, "s/key") == 0) {
217				if (skey_auth(pw->pw_name))
218					break;
219			}
220#endif
221			if (!strcmp(mypw, crypt(s, mypw)))
222				break;
223		}
224		else if (!strcmp(s, s1))
225			break;
226		(void)printf("\a\n");
227		if (tcsetattr(0, TCSADRAIN, &ntty) < 0)
228			exit(1);
229	}
230	quit(0);
231	/* NOTREACHED */
232	return (0);
233}
234
235#ifdef SKEY
236/*
237 * We can't use libskey's skey_authenticate() since it
238 * handles signals in a way that's inappropriate
239 * for our needs. Instead we roll our own.
240 */
241int
242skey_auth(user)
243	const char *user;
244{
245	char s[128];
246	const char *ask;
247	int ret = 0;
248
249	if (!skey_haskey(user) && (ask = skey_keyinfo(user))) {
250		printf("\n[%s]\nResponse: ", ask);
251		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
252			clearerr(stdin);
253		else {
254			s[strlen(s) - 1] = '\0';
255			if (skey_passcheck(user, s) != -1)
256				ret = 1;
257		}
258	} else
259		printf("Sorry, you have no s/key.\n");
260	return ret;
261}
262#endif
263
264void
265hi(dummy)
266	int dummy;
267{
268	struct timeval timval;
269
270	if (notimeout)
271		(void)printf("lock: type in the unlock key.\n");
272	else if (!gettimeofday(&timval, (struct timezone *)NULL))
273(void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
274	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
275}
276
277void
278quit(dummy)
279	int dummy;
280{
281	(void)putchar('\n');
282	(void)tcsetattr(0, TCSADRAIN, &tty);
283	exit(0);
284}
285
286void
287bye(dummy)
288	int dummy;
289{
290	(void)tcsetattr(0, TCSADRAIN, &tty);
291	(void)printf("lock: timeout\n");
292	exit(1);
293}
294