xref: /csrg-svn/usr.bin/lock/lock.c (revision 30956)
121565Sdist /*
221565Sdist  * Copyright (c) 1980 Regents of the University of California.
321565Sdist  * All rights reserved.  The Berkeley software License Agreement
421565Sdist  * specifies the terms and conditions for redistribution.
521565Sdist  */
621565Sdist 
712989Ssam #ifndef lint
821565Sdist char copyright[] =
921565Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1021565Sdist  All rights reserved.\n";
1121565Sdist #endif not lint
1212989Ssam 
1321565Sdist #ifndef lint
14*30956Sbostic static char sccsid[] = "@(#)lock.c	5.3 (Berkeley) 04/26/87";
1521565Sdist #endif not lint
1621565Sdist 
1712989Ssam /*
18*30956Sbostic  * Lock a terminal up until the given key is entered, until the root
19*30956Sbostic  * password is entered, or the given interval times out.
2016239Sralph  *
2116239Sralph  * Timeout interval is by default TIMEOUT, it can be changed with
2216239Sralph  * an argument of the form -time where time is in minutes
2312989Ssam  */
2416239Sralph 
2530652Sbostic #include <sys/param.h>
265748Sroot #include <sys/stat.h>
2716239Sralph #include <sys/time.h>
2830652Sbostic #include <sys/signal.h>
2930652Sbostic #include <pwd.h>
301041Sbill #include <sgtty.h>
3130652Sbostic #include <stdio.h>
321041Sbill 
33*30956Sbostic #define	TIMEOUT	15
34*30956Sbostic #define	YES	1
35*30956Sbostic #define	NO	0
361041Sbill 
3730652Sbostic int	quit(), bye(), hi();
3816239Sralph 
3930652Sbostic struct timeval	timeout;
4030652Sbostic struct timeval	zerotime;
4116239Sralph struct sgttyb	tty, ntty;
42*30956Sbostic long	nexttime;			/* keep the timeout time */
4316239Sralph 
44*30956Sbostic /*ARGSUSED*/
45*30956Sbostic main(argc, argv)
4630652Sbostic 	int	argc;
4730652Sbostic 	char	**argv;
481041Sbill {
49*30956Sbostic 	struct passwd	*root_pwd, *my_pwd;
5016239Sralph 	struct timeval	timval;
5116239Sralph 	struct itimerval	ntimer, otimer;
5216239Sralph 	struct tm	*timp;
53*30956Sbostic 	int	sectimeout = TIMEOUT,
54*30956Sbostic 		use_mine;
5530652Sbostic 	char	*ttynam, *ap, *tzn,
56*30956Sbostic 		hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ],
5730652Sbostic 		*crypt(), *index(), *ttyname();
581041Sbill 
59*30956Sbostic 	use_mine = NO;
60*30956Sbostic 	for (++argv; *argv; ++argv) {
61*30956Sbostic 		if (argv[0][0] != '-')
62*30956Sbostic 			usage();
63*30956Sbostic 		if (argv[0][1] == 'p')
64*30956Sbostic 			use_mine = YES;
65*30956Sbostic 		else if ((sectimeout = atoi(*argv + 1)) <= 0)
66*30956Sbostic 			usage();
6716239Sralph 	}
6816239Sralph 	timeout.tv_sec = sectimeout * 60;
6916239Sralph 
7016239Sralph 	/* get information for header */
7112989Ssam 	if (ioctl(0, TIOCGETP, &tty))
721041Sbill 		exit(1);
7316239Sralph 	gethostname(hostname, sizeof(hostname));
7430652Sbostic 	if (!(ttynam = ttyname(0))) {
7530652Sbostic 		puts("lock: not a terminal?");
76*30956Sbostic 		exit(1);
7716239Sralph 	}
7830652Sbostic 	if (gettimeofday(&timval, (struct timezone *)NULL)) {
7930652Sbostic 		perror("gettimeofday");
80*30956Sbostic 		exit(1);
8130652Sbostic 	}
8216239Sralph 	nexttime = timval.tv_sec + (sectimeout * 60);
8316239Sralph 	timp = localtime(&timval.tv_sec);
8416239Sralph 	ap = asctime(timp);
8530652Sbostic 	tzn = timp->tm_zone;
8616239Sralph 
8730652Sbostic 	(void)signal(SIGINT, quit);
8830652Sbostic 	(void)signal(SIGQUIT, quit);
891041Sbill 	ntty = tty; ntty.sg_flags &= ~ECHO;
9030652Sbostic 	(void)ioctl(0, TIOCSETP, &ntty);
9130652Sbostic 
92*30956Sbostic 	if (!use_mine) {
93*30956Sbostic 		/* get key and check again */
94*30956Sbostic 		fputs("Key: ", stdout);
95*30956Sbostic 		if (!gets(s, sizeof(s)))
96*30956Sbostic 			quit();
97*30956Sbostic 		fputs("\nAgain: ", stdout);
98*30956Sbostic 		/*
99*30956Sbostic 		 * Don't need EOF test here, if we get EOF, then s1 != s
100*30956Sbostic 		 * and the right things will happen.
101*30956Sbostic 		 */
102*30956Sbostic 		(void)gets(s1, sizeof(s1));
103*30956Sbostic 		putchar('\n');
104*30956Sbostic 		if (strcmp(s1, s)) {
105*30956Sbostic 			puts("\07lock: passwords didn't match.");
106*30956Sbostic 			ioctl(0, TIOCSETP, &tty);
107*30956Sbostic 			exit(1);
108*30956Sbostic 		}
109*30956Sbostic 		s[0] = NULL;
1101041Sbill 	}
11116239Sralph 
112*30956Sbostic 	/* set signal handlers */
11330652Sbostic 	(void)signal(SIGINT, hi);
11430652Sbostic 	(void)signal(SIGQUIT, hi);
11530652Sbostic 	(void)signal(SIGTSTP, hi);
11630652Sbostic 	(void)signal(SIGALRM, bye);
11730652Sbostic 
11816239Sralph 	ntimer.it_interval = zerotime;
11916239Sralph 	ntimer.it_value = timeout;
12016239Sralph 	setitimer(ITIMER_REAL, &ntimer, &otimer);
12116239Sralph 
122*30956Sbostic 	/* header info */
12330652Sbostic 	printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
124*30956Sbostic 		ttynam, hostname, sectimeout, ap, tzn, ap + 19);
12516239Sralph 
12616239Sralph 	/* wait */
127*30956Sbostic 	root_pwd = getpwuid(0);
128*30956Sbostic 	if (use_mine)
129*30956Sbostic 		my_pwd = getpwuid(getuid());
130*30956Sbostic 	for (;;) {
131*30956Sbostic 		fputs("Key: ", stdout);
132*30956Sbostic 		if (!gets(s, sizeof(s))) {
13316599Skre 			clearerr(stdin);
13416599Skre 			hi();
13516599Skre 			continue;
13616599Skre 		}
137*30956Sbostic 		if (use_mine) {
138*30956Sbostic 			if (!my_pwd || !*my_pwd->pw_passwd || !strcmp(my_pwd->pw_passwd, crypt(s, my_pwd->pw_passwd)))
139*30956Sbostic 				break;
140*30956Sbostic 		}
141*30956Sbostic 		else if (!strcmp(s1, s))
1421041Sbill 			break;
143*30956Sbostic 		if (!root_pwd || !*root_pwd->pw_passwd || !strcmp(root_pwd->pw_passwd, crypt(s, root_pwd->pw_passwd)))
144*30956Sbostic 			break;
14530652Sbostic 		puts("\07");
14612989Ssam 		if (ioctl(0, TIOCGETP, &ntty))
1471041Sbill 			exit(1);
1481041Sbill 	}
14916239Sralph 	putchar('\n');
15030652Sbostic 	quit();
1511041Sbill }
15216239Sralph 
15330652Sbostic static
15430652Sbostic hi()
15530652Sbostic {
15630652Sbostic 	struct timeval	timval;
15730652Sbostic 
15830652Sbostic 	if (!gettimeofday(&timval, (struct timezone *)NULL))
159*30956Sbostic 	    printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
160*30956Sbostic 	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
16130652Sbostic }
16230652Sbostic 
16330652Sbostic static
16416239Sralph quit()
16516239Sralph {
16630652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
167*30956Sbostic 	exit(0);
16816239Sralph }
16916239Sralph 
17030652Sbostic static
17116239Sralph bye()
17216239Sralph {
17330652Sbostic 	(void)ioctl(0, TIOCSETP, &tty);
17430652Sbostic 	puts("lock: timeout");
175*30956Sbostic 	exit(1);
17616239Sralph }
177*30956Sbostic 
178*30956Sbostic static
179*30956Sbostic usage()
180*30956Sbostic {
181*30956Sbostic 	puts("Usage: lock [-p] [-timeout]");
182*30956Sbostic 	exit(1);
183*30956Sbostic }
184