xref: /csrg-svn/usr.bin/lock/lock.c (revision 62068)
121565Sdist /*
2*62068Sbostic  * Copyright (c) 1980, 1987, 1993
3*62068Sbostic  *	The Regents of the University of California.  All rights reserved.
433161Sbostic  *
558201Sbostic  * This code is derived from software contributed to Berkeley by
658201Sbostic  * Bob Toxen.
758201Sbostic  *
842738Sbostic  * %sccs.include.redist.c%
921565Sdist  */
1021565Sdist 
1112989Ssam #ifndef lint
12*62068Sbostic static char copyright[] =
13*62068Sbostic "@(#) Copyright (c) 1980, 1987, 1993\n\
14*62068Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1533161Sbostic #endif /* not lint */
1612989Ssam 
1721565Sdist #ifndef lint
18*62068Sbostic static char sccsid[] = "@(#)lock.c	8.1 (Berkeley) 06/06/93";
1933161Sbostic #endif /* not lint */
2021565Sdist 
2112989Ssam /*
2230956Sbostic  * Lock a terminal up until the given key is entered, until the root
2330956Sbostic  * password is entered, or the given interval times out.
2416239Sralph  *
2516239Sralph  * Timeout interval is by default TIMEOUT, it can be changed with
2616239Sralph  * an argument of the form -time where time is in minutes
2712989Ssam  */
2816239Sralph 
2930652Sbostic #include <sys/param.h>
305748Sroot #include <sys/stat.h>
3116239Sralph #include <sys/time.h>
3230652Sbostic #include <sys/signal.h>
3336867Sbostic #include <sgtty.h>
3430652Sbostic #include <pwd.h>
3530652Sbostic #include <stdio.h>
3632615Sbostic #include <ctype.h>
3742062Sbostic #include <string.h>
381041Sbill 
3930956Sbostic #define	TIMEOUT	15
401041Sbill 
4139228Sbostic void quit(), bye(), hi();
4216239Sralph 
4330652Sbostic struct timeval	timeout;
4430652Sbostic struct timeval	zerotime;
4516239Sralph struct sgttyb	tty, ntty;
4630956Sbostic long	nexttime;			/* keep the timeout time */
4716239Sralph 
4830956Sbostic /*ARGSUSED*/
main(argc,argv)4930956Sbostic main(argc, argv)
5036292Sbostic 	int argc;
5136292Sbostic 	char **argv;
521041Sbill {
5336292Sbostic 	extern char *optarg;
5436867Sbostic 	extern int errno, optind;
5536867Sbostic 	struct passwd *pw;
5636292Sbostic 	struct timeval timval;
5736292Sbostic 	struct itimerval ntimer, otimer;
5836292Sbostic 	struct tm *timp;
5936867Sbostic 	int ch, sectimeout, usemine;
6036869Sbostic 	char *ap, *mypw, *ttynam, *tzn;
6136292Sbostic 	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
6236867Sbostic 	char *crypt(), *ttyname();
631041Sbill 
6436292Sbostic 	sectimeout = TIMEOUT;
6536867Sbostic 	mypw = NULL;
6636867Sbostic 	usemine = 0;
6736292Sbostic 	while ((ch = getopt(argc, argv, "pt:")) != EOF)
6836292Sbostic 		switch((char)ch) {
6936292Sbostic 		case 't':
7039228Sbostic 			if ((sectimeout = atoi(optarg)) <= 0) {
7139228Sbostic 				(void)fprintf(stderr,
7239228Sbostic 				    "lock: illegal timeout value.\n");
7336867Sbostic 				exit(1);
7439228Sbostic 			}
7536292Sbostic 			break;
7636292Sbostic 		case 'p':
7736867Sbostic 			usemine = 1;
7836867Sbostic 			if (!(pw = getpwuid(getuid()))) {
7939228Sbostic 				(void)fprintf(stderr,
8039228Sbostic 				    "lock: unknown uid %d.\n", getuid());
8136867Sbostic 				exit(1);
8236867Sbostic 			}
8336867Sbostic 			mypw = strdup(pw->pw_passwd);
8436292Sbostic 			break;
8536292Sbostic 		case '?':
8636292Sbostic 		default:
8739228Sbostic 			(void)fprintf(stderr,
8839228Sbostic 			    "usage: lock [-p] [-t timeout]\n");
8936292Sbostic 			exit(1);
9016239Sralph 	}
9116239Sralph 	timeout.tv_sec = sectimeout * 60;
9216239Sralph 
9336867Sbostic 	setuid(getuid());		/* discard privs */
9436867Sbostic 
9536867Sbostic 	if (ioctl(0, TIOCGETP, &tty))	/* get information for header */
961041Sbill 		exit(1);
9716239Sralph 	gethostname(hostname, sizeof(hostname));
9830652Sbostic 	if (!(ttynam = ttyname(0))) {
9939228Sbostic 		(void)printf("lock: not a terminal?\n");
10030956Sbostic 		exit(1);
10116239Sralph 	}
10230652Sbostic 	if (gettimeofday(&timval, (struct timezone *)NULL)) {
10339228Sbostic 		(void)fprintf(stderr,
10439228Sbostic 		    "lock: gettimeofday: %s\n", strerror(errno));
10530956Sbostic 		exit(1);
10630652Sbostic 	}
10716239Sralph 	nexttime = timval.tv_sec + (sectimeout * 60);
10816239Sralph 	timp = localtime(&timval.tv_sec);
10916239Sralph 	ap = asctime(timp);
11030652Sbostic 	tzn = timp->tm_zone;
11116239Sralph 
11230652Sbostic 	(void)signal(SIGINT, quit);
11330652Sbostic 	(void)signal(SIGQUIT, quit);
1141041Sbill 	ntty = tty; ntty.sg_flags &= ~ECHO;
11530652Sbostic 	(void)ioctl(0, TIOCSETP, &ntty);
11630652Sbostic 
11736867Sbostic 	if (!mypw) {
11830956Sbostic 		/* get key and check again */
11939228Sbostic 		(void)printf("Key: ");
12036867Sbostic 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
12130956Sbostic 			quit();
12239228Sbostic 		(void)printf("\nAgain: ");
12330956Sbostic 		/*
12430956Sbostic 		 * Don't need EOF test here, if we get EOF, then s1 != s
12530956Sbostic 		 * and the right things will happen.
12630956Sbostic 		 */
12736867Sbostic 		(void)fgets(s1, sizeof(s1), stdin);
12839228Sbostic 		(void)putchar('\n');
12930956Sbostic 		if (strcmp(s1, s)) {
13039228Sbostic 			(void)printf("\07lock: passwords didn't match.\n");
13130956Sbostic 			ioctl(0, TIOCSETP, &tty);
13230956Sbostic 			exit(1);
13330956Sbostic 		}
13430956Sbostic 		s[0] = NULL;
13536867Sbostic 		mypw = s1;
1361041Sbill 	}
13716239Sralph 
13830956Sbostic 	/* set signal handlers */
13930652Sbostic 	(void)signal(SIGINT, hi);
14030652Sbostic 	(void)signal(SIGQUIT, hi);
14130652Sbostic 	(void)signal(SIGTSTP, hi);
14230652Sbostic 	(void)signal(SIGALRM, bye);
14330652Sbostic 
14416239Sralph 	ntimer.it_interval = zerotime;
14516239Sralph 	ntimer.it_value = timeout;
14616239Sralph 	setitimer(ITIMER_REAL, &ntimer, &otimer);
14716239Sralph 
14830956Sbostic 	/* header info */
14939228Sbostic (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
15039228Sbostic 	    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
15116239Sralph 
15230956Sbostic 	for (;;) {
15339228Sbostic 		(void)printf("Key: ");
15436867Sbostic 		if (!fgets(s, sizeof(s), stdin)) {
15516599Skre 			clearerr(stdin);
15616599Skre 			hi();
15716599Skre 			continue;
15816599Skre 		}
15936867Sbostic 		if (usemine) {
16037235Sbostic 			s[strlen(s) - 1] = '\0';
16136867Sbostic 			if (!strcmp(mypw, crypt(s, mypw)))
16230956Sbostic 				break;
16330956Sbostic 		}
16436867Sbostic 		else if (!strcmp(s, s1))
1651041Sbill 			break;
16639228Sbostic 		(void)printf("\07\n");
16712989Ssam 		if (ioctl(0, TIOCGETP, &ntty))
1681041Sbill 			exit(1);
1691041Sbill 	}
17030652Sbostic 	quit();
1711041Sbill }
17216239Sralph 
17339228Sbostic void
hi()17430652Sbostic hi()
17530652Sbostic {
17636292Sbostic 	struct timeval timval;
17730652Sbostic 
17830652Sbostic 	if (!gettimeofday(&timval, (struct timezone *)NULL))
17939228Sbostic (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
18030956Sbostic 	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
18130652Sbostic }
18230652Sbostic 
18339228Sbostic void
quit()18416239Sralph quit()
18516239Sralph {
18639228Sbostic 	(void)putchar('\n');
18730652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
18830956Sbostic 	exit(0);
18916239Sralph }
19016239Sralph 
19139228Sbostic void
bye()19216239Sralph bye()
19316239Sralph {
19430652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
19539228Sbostic 	(void)printf("lock: timeout\n");
19630956Sbostic 	exit(1);
19716239Sralph }
198