xref: /csrg-svn/usr.bin/lock/lock.c (revision 36869)
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*36869Sbostic static char sccsid[] = "@(#)lock.c	5.9 (Berkeley) 02/23/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>
4036867Sbostic #include <sgtty.h>
4130652Sbostic #include <pwd.h>
4230652Sbostic #include <stdio.h>
4332615Sbostic #include <ctype.h>
4436867Sbostic #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;
6136867Sbostic 	extern int errno, optind;
6236867Sbostic 	struct passwd *pw;
6336292Sbostic 	struct timeval timval;
6436292Sbostic 	struct itimerval ntimer, otimer;
6536292Sbostic 	struct tm *timp;
6636867Sbostic 	int ch, sectimeout, usemine;
67*36869Sbostic 	char *ap, *mypw, *ttynam, *tzn;
6836292Sbostic 	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
6936867Sbostic 	char *crypt(), *ttyname();
701041Sbill 
7136292Sbostic 	sectimeout = TIMEOUT;
7236867Sbostic 	mypw = NULL;
7336867Sbostic 	usemine = 0;
7436292Sbostic 	while ((ch = getopt(argc, argv, "pt:")) != EOF)
7536292Sbostic 		switch((char)ch) {
7636292Sbostic 		case 't':
7736292Sbostic 			if ((sectimeout = atoi(optarg)) <= 0)
7836867Sbostic 				exit(1);
7936292Sbostic 			break;
8036292Sbostic 		case 'p':
8136867Sbostic 			usemine = 1;
8236867Sbostic 			if (!(pw = getpwuid(getuid()))) {
8336867Sbostic 				fprintf(stderr, "lock: unknown uid %d.\n",
8436867Sbostic 				    getuid());
8536867Sbostic 				exit(1);
8636867Sbostic 			}
8736867Sbostic 			mypw = strdup(pw->pw_passwd);
8836292Sbostic 			break;
8936292Sbostic 		case '?':
9036292Sbostic 		default:
9136867Sbostic 			fprintf(stderr, "usage: lock [-p] [-t timeout]\n");
9236292Sbostic 			exit(1);
9316239Sralph 	}
9416239Sralph 	timeout.tv_sec = sectimeout * 60;
9516239Sralph 
9636867Sbostic 	setuid(getuid());		/* discard privs */
9736867Sbostic 
9836867Sbostic 	if (ioctl(0, TIOCGETP, &tty))	/* get information for header */
991041Sbill 		exit(1);
10016239Sralph 	gethostname(hostname, sizeof(hostname));
10130652Sbostic 	if (!(ttynam = ttyname(0))) {
10236867Sbostic 		printf("lock: not a terminal?\n");
10330956Sbostic 		exit(1);
10416239Sralph 	}
10530652Sbostic 	if (gettimeofday(&timval, (struct timezone *)NULL)) {
10636867Sbostic 		fprintf(stderr, "lock: gettimeofday: %s\n", strerror(errno));
10730956Sbostic 		exit(1);
10830652Sbostic 	}
10916239Sralph 	nexttime = timval.tv_sec + (sectimeout * 60);
11016239Sralph 	timp = localtime(&timval.tv_sec);
11116239Sralph 	ap = asctime(timp);
11230652Sbostic 	tzn = timp->tm_zone;
11316239Sralph 
11430652Sbostic 	(void)signal(SIGINT, quit);
11530652Sbostic 	(void)signal(SIGQUIT, quit);
1161041Sbill 	ntty = tty; ntty.sg_flags &= ~ECHO;
11730652Sbostic 	(void)ioctl(0, TIOCSETP, &ntty);
11830652Sbostic 
11936867Sbostic 	if (!mypw) {
12030956Sbostic 		/* get key and check again */
12136867Sbostic 		printf("Key: ");
12236867Sbostic 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
12330956Sbostic 			quit();
12436867Sbostic 		printf("\nAgain: ");
12530956Sbostic 		/*
12630956Sbostic 		 * Don't need EOF test here, if we get EOF, then s1 != s
12730956Sbostic 		 * and the right things will happen.
12830956Sbostic 		 */
12936867Sbostic 		(void)fgets(s1, sizeof(s1), stdin);
13030956Sbostic 		putchar('\n');
13130956Sbostic 		if (strcmp(s1, s)) {
13236867Sbostic 			printf("\07lock: passwords didn't match.\n");
13330956Sbostic 			ioctl(0, TIOCSETP, &tty);
13430956Sbostic 			exit(1);
13530956Sbostic 		}
13630956Sbostic 		s[0] = NULL;
13736867Sbostic 		mypw = s1;
1381041Sbill 	}
13916239Sralph 
14030956Sbostic 	/* set signal handlers */
14130652Sbostic 	(void)signal(SIGINT, hi);
14230652Sbostic 	(void)signal(SIGQUIT, hi);
14330652Sbostic 	(void)signal(SIGTSTP, hi);
14430652Sbostic 	(void)signal(SIGALRM, bye);
14530652Sbostic 
14616239Sralph 	ntimer.it_interval = zerotime;
14716239Sralph 	ntimer.it_value = timeout;
14816239Sralph 	setitimer(ITIMER_REAL, &ntimer, &otimer);
14916239Sralph 
15030956Sbostic 	/* header info */
15130652Sbostic 	printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
15230956Sbostic 		ttynam, hostname, sectimeout, ap, tzn, ap + 19);
15316239Sralph 
15430956Sbostic 	for (;;) {
15536867Sbostic 		printf("Key: ");
15636867Sbostic 		if (!fgets(s, sizeof(s), stdin)) {
15716599Skre 			clearerr(stdin);
15816599Skre 			hi();
15916599Skre 			continue;
16016599Skre 		}
16136867Sbostic 		if (usemine) {
16236867Sbostic 			if (!strcmp(mypw, crypt(s, mypw)))
16330956Sbostic 				break;
16430956Sbostic 		}
16536867Sbostic 		else if (!strcmp(s, s1))
1661041Sbill 			break;
16736867Sbostic 		printf("\07\n");
16812989Ssam 		if (ioctl(0, TIOCGETP, &ntty))
1691041Sbill 			exit(1);
1701041Sbill 	}
17130652Sbostic 	quit();
1721041Sbill }
17316239Sralph 
17430652Sbostic static
17530652Sbostic hi()
17630652Sbostic {
17736292Sbostic 	struct timeval timval;
17830652Sbostic 
17930652Sbostic 	if (!gettimeofday(&timval, (struct timezone *)NULL))
18030956Sbostic 	    printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
18130956Sbostic 	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
18230652Sbostic }
18330652Sbostic 
18430652Sbostic static
18516239Sralph quit()
18616239Sralph {
18736867Sbostic 	putchar('\n');
18830652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
18930956Sbostic 	exit(0);
19016239Sralph }
19116239Sralph 
19230652Sbostic static
19316239Sralph bye()
19416239Sralph {
19530652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
19636867Sbostic 	printf("lock: timeout\n");
19730956Sbostic 	exit(1);
19816239Sralph }
199