xref: /csrg-svn/usr.bin/lock/lock.c (revision 36867)
121565Sdist /*
233161Sbostic  * Copyright (c) 1980, 1987 Regents of the University of California.
333161Sbostic  * All rights reserved.
433161Sbostic  *
533161Sbostic  * Redistribution and use in source and binary forms are permitted
634911Sbostic  * provided that the above copyright notice and this paragraph are
734911Sbostic  * duplicated in all such forms and that any documentation,
834911Sbostic  * advertising materials, and other materials related to such
934911Sbostic  * distribution and use acknowledge that the software was developed
1034911Sbostic  * by the University of California, Berkeley.  The name of the
1134911Sbostic  * University may not be used to endorse or promote products derived
1234911Sbostic  * from this software without specific prior written permission.
1334911Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434911Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534911Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621565Sdist  */
1721565Sdist 
1812989Ssam #ifndef lint
1921565Sdist char copyright[] =
2033161Sbostic "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
2121565Sdist  All rights reserved.\n";
2233161Sbostic #endif /* not lint */
2312989Ssam 
2421565Sdist #ifndef lint
25*36867Sbostic static char sccsid[] = "@(#)lock.c	5.8 (Berkeley) 02/22/89";
2633161Sbostic #endif /* not lint */
2721565Sdist 
2812989Ssam /*
2930956Sbostic  * Lock a terminal up until the given key is entered, until the root
3030956Sbostic  * password is entered, or the given interval times out.
3116239Sralph  *
3216239Sralph  * Timeout interval is by default TIMEOUT, it can be changed with
3316239Sralph  * an argument of the form -time where time is in minutes
3412989Ssam  */
3516239Sralph 
3630652Sbostic #include <sys/param.h>
375748Sroot #include <sys/stat.h>
3816239Sralph #include <sys/time.h>
3930652Sbostic #include <sys/signal.h>
40*36867Sbostic #include <sgtty.h>
4130652Sbostic #include <pwd.h>
4230652Sbostic #include <stdio.h>
4332615Sbostic #include <ctype.h>
44*36867Sbostic #include <strings.h>
451041Sbill 
4630956Sbostic #define	TIMEOUT	15
471041Sbill 
4830652Sbostic int	quit(), bye(), hi();
4916239Sralph 
5030652Sbostic struct timeval	timeout;
5130652Sbostic struct timeval	zerotime;
5216239Sralph struct sgttyb	tty, ntty;
5330956Sbostic long	nexttime;			/* keep the timeout time */
5416239Sralph 
5530956Sbostic /*ARGSUSED*/
5630956Sbostic main(argc, argv)
5736292Sbostic 	int argc;
5836292Sbostic 	char **argv;
591041Sbill {
6036292Sbostic 	extern char *optarg;
61*36867Sbostic 	extern int errno, optind;
62*36867Sbostic 	struct passwd *pw;
6336292Sbostic 	struct timeval timval;
6436292Sbostic 	struct itimerval ntimer, otimer;
6536292Sbostic 	struct tm *timp;
66*36867Sbostic 	int ch, sectimeout, usemine;
67*36867Sbostic 	char *ap, *mypw, *rootpw, *ttynam, *tzn;
6836292Sbostic 	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
69*36867Sbostic 	char *crypt(), *ttyname();
701041Sbill 
7136292Sbostic 	sectimeout = TIMEOUT;
72*36867Sbostic 	mypw = NULL;
73*36867Sbostic 	usemine = 0;
7436292Sbostic 	while ((ch = getopt(argc, argv, "pt:")) != EOF)
7536292Sbostic 		switch((char)ch) {
7636292Sbostic 		case 't':
7736292Sbostic 			if ((sectimeout = atoi(optarg)) <= 0)
78*36867Sbostic 				exit(1);
7936292Sbostic 			break;
8036292Sbostic 		case 'p':
81*36867Sbostic 			usemine = 1;
82*36867Sbostic 			if (!(pw = getpwuid(getuid()))) {
83*36867Sbostic 				fprintf(stderr, "lock: unknown uid %d.\n",
84*36867Sbostic 				    getuid());
85*36867Sbostic 				exit(1);
86*36867Sbostic 			}
87*36867Sbostic 			mypw = strdup(pw->pw_passwd);
8836292Sbostic 			break;
8936292Sbostic 		case '?':
9036292Sbostic 		default:
91*36867Sbostic 			fprintf(stderr, "usage: lock [-p] [-t timeout]\n");
9236292Sbostic 			exit(1);
9316239Sralph 	}
9416239Sralph 	timeout.tv_sec = sectimeout * 60;
9516239Sralph 
96*36867Sbostic 	rootpw = (pw = getpwnam("root")) ? strdup(pw->pw_passwd) : NULL;
97*36867Sbostic 
98*36867Sbostic 	setuid(getuid());		/* discard privs */
99*36867Sbostic 
100*36867Sbostic 	if (ioctl(0, TIOCGETP, &tty))	/* get information for header */
1011041Sbill 		exit(1);
10216239Sralph 	gethostname(hostname, sizeof(hostname));
10330652Sbostic 	if (!(ttynam = ttyname(0))) {
104*36867Sbostic 		printf("lock: not a terminal?\n");
10530956Sbostic 		exit(1);
10616239Sralph 	}
10730652Sbostic 	if (gettimeofday(&timval, (struct timezone *)NULL)) {
108*36867Sbostic 		fprintf(stderr, "lock: gettimeofday: %s\n", strerror(errno));
10930956Sbostic 		exit(1);
11030652Sbostic 	}
11116239Sralph 	nexttime = timval.tv_sec + (sectimeout * 60);
11216239Sralph 	timp = localtime(&timval.tv_sec);
11316239Sralph 	ap = asctime(timp);
11430652Sbostic 	tzn = timp->tm_zone;
11516239Sralph 
11630652Sbostic 	(void)signal(SIGINT, quit);
11730652Sbostic 	(void)signal(SIGQUIT, quit);
1181041Sbill 	ntty = tty; ntty.sg_flags &= ~ECHO;
11930652Sbostic 	(void)ioctl(0, TIOCSETP, &ntty);
12030652Sbostic 
121*36867Sbostic 	if (!mypw) {
12230956Sbostic 		/* get key and check again */
123*36867Sbostic 		printf("Key: ");
124*36867Sbostic 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
12530956Sbostic 			quit();
126*36867Sbostic 		printf("\nAgain: ");
12730956Sbostic 		/*
12830956Sbostic 		 * Don't need EOF test here, if we get EOF, then s1 != s
12930956Sbostic 		 * and the right things will happen.
13030956Sbostic 		 */
131*36867Sbostic 		(void)fgets(s1, sizeof(s1), stdin);
13230956Sbostic 		putchar('\n');
13330956Sbostic 		if (strcmp(s1, s)) {
134*36867Sbostic 			printf("\07lock: passwords didn't match.\n");
13530956Sbostic 			ioctl(0, TIOCSETP, &tty);
13630956Sbostic 			exit(1);
13730956Sbostic 		}
13830956Sbostic 		s[0] = NULL;
139*36867Sbostic 		mypw = s1;
1401041Sbill 	}
14116239Sralph 
14230956Sbostic 	/* set signal handlers */
14330652Sbostic 	(void)signal(SIGINT, hi);
14430652Sbostic 	(void)signal(SIGQUIT, hi);
14530652Sbostic 	(void)signal(SIGTSTP, hi);
14630652Sbostic 	(void)signal(SIGALRM, bye);
14730652Sbostic 
14816239Sralph 	ntimer.it_interval = zerotime;
14916239Sralph 	ntimer.it_value = timeout;
15016239Sralph 	setitimer(ITIMER_REAL, &ntimer, &otimer);
15116239Sralph 
15230956Sbostic 	/* header info */
15330652Sbostic 	printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
15430956Sbostic 		ttynam, hostname, sectimeout, ap, tzn, ap + 19);
15516239Sralph 
15630956Sbostic 	for (;;) {
157*36867Sbostic 		printf("Key: ");
158*36867Sbostic 		if (!fgets(s, sizeof(s), stdin)) {
15916599Skre 			clearerr(stdin);
16016599Skre 			hi();
16116599Skre 			continue;
16216599Skre 		}
163*36867Sbostic 		if (usemine) {
164*36867Sbostic 			if (!strcmp(mypw, crypt(s, mypw)))
16530956Sbostic 				break;
16630956Sbostic 		}
167*36867Sbostic 		else if (!strcmp(s, s1))
1681041Sbill 			break;
169*36867Sbostic 		if (rootpw && !strcmp(rootpw, crypt(s, rootpw)))
17030956Sbostic 			break;
171*36867Sbostic 		printf("\07\n");
17212989Ssam 		if (ioctl(0, TIOCGETP, &ntty))
1731041Sbill 			exit(1);
1741041Sbill 	}
17530652Sbostic 	quit();
1761041Sbill }
17716239Sralph 
17830652Sbostic static
17930652Sbostic hi()
18030652Sbostic {
18136292Sbostic 	struct timeval timval;
18230652Sbostic 
18330652Sbostic 	if (!gettimeofday(&timval, (struct timezone *)NULL))
18430956Sbostic 	    printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
18530956Sbostic 	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
18630652Sbostic }
18730652Sbostic 
18830652Sbostic static
18916239Sralph quit()
19016239Sralph {
191*36867Sbostic 	putchar('\n');
19230652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
19330956Sbostic 	exit(0);
19416239Sralph }
19516239Sralph 
19630652Sbostic static
19716239Sralph bye()
19816239Sralph {
19930652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
200*36867Sbostic 	printf("lock: timeout\n");
20130956Sbostic 	exit(1);
20216239Sralph }
203