xref: /csrg-svn/usr.bin/lock/lock.c (revision 42738)
121565Sdist /*
233161Sbostic  * Copyright (c) 1980, 1987 Regents of the University of California.
333161Sbostic  * All rights reserved.
433161Sbostic  *
5*42738Sbostic  * %sccs.include.redist.c%
621565Sdist  */
721565Sdist 
812989Ssam #ifndef lint
921565Sdist char copyright[] =
1033161Sbostic "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
1121565Sdist  All rights reserved.\n";
1233161Sbostic #endif /* not lint */
1312989Ssam 
1421565Sdist #ifndef lint
15*42738Sbostic static char sccsid[] = "@(#)lock.c	5.13 (Berkeley) 06/01/90";
1633161Sbostic #endif /* not lint */
1721565Sdist 
1812989Ssam /*
1930956Sbostic  * Lock a terminal up until the given key is entered, until the root
2030956Sbostic  * password is entered, or the given interval times out.
2116239Sralph  *
2216239Sralph  * Timeout interval is by default TIMEOUT, it can be changed with
2316239Sralph  * an argument of the form -time where time is in minutes
2412989Ssam  */
2516239Sralph 
2630652Sbostic #include <sys/param.h>
275748Sroot #include <sys/stat.h>
2816239Sralph #include <sys/time.h>
2930652Sbostic #include <sys/signal.h>
3036867Sbostic #include <sgtty.h>
3130652Sbostic #include <pwd.h>
3230652Sbostic #include <stdio.h>
3332615Sbostic #include <ctype.h>
3442062Sbostic #include <string.h>
351041Sbill 
3630956Sbostic #define	TIMEOUT	15
371041Sbill 
3839228Sbostic void quit(), bye(), hi();
3916239Sralph 
4030652Sbostic struct timeval	timeout;
4130652Sbostic struct timeval	zerotime;
4216239Sralph struct sgttyb	tty, ntty;
4330956Sbostic long	nexttime;			/* keep the timeout time */
4416239Sralph 
4530956Sbostic /*ARGSUSED*/
4630956Sbostic main(argc, argv)
4736292Sbostic 	int argc;
4836292Sbostic 	char **argv;
491041Sbill {
5036292Sbostic 	extern char *optarg;
5136867Sbostic 	extern int errno, optind;
5236867Sbostic 	struct passwd *pw;
5336292Sbostic 	struct timeval timval;
5436292Sbostic 	struct itimerval ntimer, otimer;
5536292Sbostic 	struct tm *timp;
5636867Sbostic 	int ch, sectimeout, usemine;
5736869Sbostic 	char *ap, *mypw, *ttynam, *tzn;
5836292Sbostic 	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
5936867Sbostic 	char *crypt(), *ttyname();
601041Sbill 
6136292Sbostic 	sectimeout = TIMEOUT;
6236867Sbostic 	mypw = NULL;
6336867Sbostic 	usemine = 0;
6436292Sbostic 	while ((ch = getopt(argc, argv, "pt:")) != EOF)
6536292Sbostic 		switch((char)ch) {
6636292Sbostic 		case 't':
6739228Sbostic 			if ((sectimeout = atoi(optarg)) <= 0) {
6839228Sbostic 				(void)fprintf(stderr,
6939228Sbostic 				    "lock: illegal timeout value.\n");
7036867Sbostic 				exit(1);
7139228Sbostic 			}
7236292Sbostic 			break;
7336292Sbostic 		case 'p':
7436867Sbostic 			usemine = 1;
7536867Sbostic 			if (!(pw = getpwuid(getuid()))) {
7639228Sbostic 				(void)fprintf(stderr,
7739228Sbostic 				    "lock: unknown uid %d.\n", getuid());
7836867Sbostic 				exit(1);
7936867Sbostic 			}
8036867Sbostic 			mypw = strdup(pw->pw_passwd);
8136292Sbostic 			break;
8236292Sbostic 		case '?':
8336292Sbostic 		default:
8439228Sbostic 			(void)fprintf(stderr,
8539228Sbostic 			    "usage: lock [-p] [-t timeout]\n");
8636292Sbostic 			exit(1);
8716239Sralph 	}
8816239Sralph 	timeout.tv_sec = sectimeout * 60;
8916239Sralph 
9036867Sbostic 	setuid(getuid());		/* discard privs */
9136867Sbostic 
9236867Sbostic 	if (ioctl(0, TIOCGETP, &tty))	/* get information for header */
931041Sbill 		exit(1);
9416239Sralph 	gethostname(hostname, sizeof(hostname));
9530652Sbostic 	if (!(ttynam = ttyname(0))) {
9639228Sbostic 		(void)printf("lock: not a terminal?\n");
9730956Sbostic 		exit(1);
9816239Sralph 	}
9930652Sbostic 	if (gettimeofday(&timval, (struct timezone *)NULL)) {
10039228Sbostic 		(void)fprintf(stderr,
10139228Sbostic 		    "lock: gettimeofday: %s\n", strerror(errno));
10230956Sbostic 		exit(1);
10330652Sbostic 	}
10416239Sralph 	nexttime = timval.tv_sec + (sectimeout * 60);
10516239Sralph 	timp = localtime(&timval.tv_sec);
10616239Sralph 	ap = asctime(timp);
10730652Sbostic 	tzn = timp->tm_zone;
10816239Sralph 
10930652Sbostic 	(void)signal(SIGINT, quit);
11030652Sbostic 	(void)signal(SIGQUIT, quit);
1111041Sbill 	ntty = tty; ntty.sg_flags &= ~ECHO;
11230652Sbostic 	(void)ioctl(0, TIOCSETP, &ntty);
11330652Sbostic 
11436867Sbostic 	if (!mypw) {
11530956Sbostic 		/* get key and check again */
11639228Sbostic 		(void)printf("Key: ");
11736867Sbostic 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
11830956Sbostic 			quit();
11939228Sbostic 		(void)printf("\nAgain: ");
12030956Sbostic 		/*
12130956Sbostic 		 * Don't need EOF test here, if we get EOF, then s1 != s
12230956Sbostic 		 * and the right things will happen.
12330956Sbostic 		 */
12436867Sbostic 		(void)fgets(s1, sizeof(s1), stdin);
12539228Sbostic 		(void)putchar('\n');
12630956Sbostic 		if (strcmp(s1, s)) {
12739228Sbostic 			(void)printf("\07lock: passwords didn't match.\n");
12830956Sbostic 			ioctl(0, TIOCSETP, &tty);
12930956Sbostic 			exit(1);
13030956Sbostic 		}
13130956Sbostic 		s[0] = NULL;
13236867Sbostic 		mypw = s1;
1331041Sbill 	}
13416239Sralph 
13530956Sbostic 	/* set signal handlers */
13630652Sbostic 	(void)signal(SIGINT, hi);
13730652Sbostic 	(void)signal(SIGQUIT, hi);
13830652Sbostic 	(void)signal(SIGTSTP, hi);
13930652Sbostic 	(void)signal(SIGALRM, bye);
14030652Sbostic 
14116239Sralph 	ntimer.it_interval = zerotime;
14216239Sralph 	ntimer.it_value = timeout;
14316239Sralph 	setitimer(ITIMER_REAL, &ntimer, &otimer);
14416239Sralph 
14530956Sbostic 	/* header info */
14639228Sbostic (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
14739228Sbostic 	    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
14816239Sralph 
14930956Sbostic 	for (;;) {
15039228Sbostic 		(void)printf("Key: ");
15136867Sbostic 		if (!fgets(s, sizeof(s), stdin)) {
15216599Skre 			clearerr(stdin);
15316599Skre 			hi();
15416599Skre 			continue;
15516599Skre 		}
15636867Sbostic 		if (usemine) {
15737235Sbostic 			s[strlen(s) - 1] = '\0';
15836867Sbostic 			if (!strcmp(mypw, crypt(s, mypw)))
15930956Sbostic 				break;
16030956Sbostic 		}
16136867Sbostic 		else if (!strcmp(s, s1))
1621041Sbill 			break;
16339228Sbostic 		(void)printf("\07\n");
16412989Ssam 		if (ioctl(0, TIOCGETP, &ntty))
1651041Sbill 			exit(1);
1661041Sbill 	}
16730652Sbostic 	quit();
1681041Sbill }
16916239Sralph 
17039228Sbostic void
17130652Sbostic hi()
17230652Sbostic {
17336292Sbostic 	struct timeval timval;
17430652Sbostic 
17530652Sbostic 	if (!gettimeofday(&timval, (struct timezone *)NULL))
17639228Sbostic (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
17730956Sbostic 	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
17830652Sbostic }
17930652Sbostic 
18039228Sbostic void
18116239Sralph quit()
18216239Sralph {
18339228Sbostic 	(void)putchar('\n');
18430652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
18530956Sbostic 	exit(0);
18616239Sralph }
18716239Sralph 
18839228Sbostic void
18916239Sralph bye()
19016239Sralph {
19130652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
19239228Sbostic 	(void)printf("lock: timeout\n");
19330956Sbostic 	exit(1);
19416239Sralph }
195