xref: /csrg-svn/usr.bin/lock/lock.c (revision 21565)
1*21565Sdist /*
2*21565Sdist  * Copyright (c) 1980 Regents of the University of California.
3*21565Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21565Sdist  * specifies the terms and conditions for redistribution.
5*21565Sdist  */
6*21565Sdist 
712989Ssam #ifndef lint
8*21565Sdist char copyright[] =
9*21565Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21565Sdist  All rights reserved.\n";
11*21565Sdist #endif not lint
1212989Ssam 
13*21565Sdist #ifndef lint
14*21565Sdist static char sccsid[] = "@(#)lock.c	5.1 (Berkeley) 05/31/85";
15*21565Sdist #endif not lint
16*21565Sdist 
1712989Ssam /*
1816239Sralph  * Lock a terminal up until the given key is entered,
1916239Sralph  * or until the root password is entered,
2016239Sralph  * 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 
2616239Sralph #include <pwd.h>
271041Sbill #include <stdio.h>
281041Sbill #include <sys/types.h>
295748Sroot #include <sys/stat.h>
3016239Sralph #include <sys/time.h>
311041Sbill #include <signal.h>
321041Sbill #include <sgtty.h>
331041Sbill 
3416239Sralph #define TIMEOUT 15
351041Sbill 
3616239Sralph struct	passwd *pwd;
3716239Sralph char	*crypt();
3816239Sralph char	*getpass();
3916239Sralph char	*index();
4016239Sralph char	*ttyname();
4116239Sralph char	*timezone();
4216239Sralph char	*asctime();
4316239Sralph struct	tm *localtime();
4416239Sralph 
4516239Sralph int	quit();
4616239Sralph int	bye();
4716239Sralph int	hi();
4816239Sralph 
4916239Sralph struct timeval	timeout	= {0, 0};
5016239Sralph struct timeval	zerotime = {0, 0};
5116239Sralph struct sgttyb	tty, ntty;
5216239Sralph long	nexttime;		/* keep the timeout time */
5316239Sralph 
541041Sbill main(argc, argv)
5516239Sralph 	int argc;
561041Sbill 	char **argv;
571041Sbill {
581041Sbill 	register int t;
5916239Sralph 	char	*ttynam;
6016239Sralph 	char	*ap;
6116239Sralph 	int	sectimeout = TIMEOUT;
6216239Sralph 	char	s[BUFSIZ], s1[BUFSIZ];
6316239Sralph 	char	hostname[32];
6416239Sralph 	char	*tzn;
6516239Sralph 	struct timeval	timval;
6616239Sralph 	struct itimerval	ntimer, otimer;
6716239Sralph 	struct timezone	timzone;
6816239Sralph 	struct tm	*timp;
6916239Sralph 	struct stat	statb;
701041Sbill 
7116239Sralph 	/* process arguments */
7216239Sralph 
7316239Sralph 	if (argc > 1){
7416239Sralph 		if (argv[1][0] != '-')
7516239Sralph 			goto usage;
7616239Sralph 		if (sscanf(&(argv[1][1]), "%d", &sectimeout) != 1)
7716239Sralph 			goto usage;
7816239Sralph 	}
7916239Sralph 	timeout.tv_sec = sectimeout * 60;
8016239Sralph 
8116239Sralph 	/* get information for header */
8216239Sralph 
8312989Ssam 	if (ioctl(0, TIOCGETP, &tty))
841041Sbill 		exit(1);
8516239Sralph 	pwd = getpwuid(0);
8616239Sralph 	gethostname(hostname, sizeof(hostname));
8716239Sralph 	if (!(ttynam = ttyname(0))){
8816239Sralph 		printf("lock: not a terminal?");
8916239Sralph 		exit (1);
9016239Sralph 	}
9116239Sralph 	gettimeofday(&timval, &timzone);
9216239Sralph 	nexttime = timval.tv_sec + (sectimeout * 60);
9316239Sralph 	timp = localtime(&timval.tv_sec);
9416239Sralph 	ap = asctime(timp);
9516239Sralph 	tzn = timezone(timzone.tz_minuteswest, timp->tm_isdst);
9616239Sralph 
9716239Sralph 	/* get key and check again */
9816239Sralph 
9916239Sralph 	signal(SIGINT, quit);
10016239Sralph 	signal(SIGQUIT, quit);
1011041Sbill 	ntty = tty; ntty.sg_flags &= ~ECHO;
10212989Ssam 	ioctl(0, TIOCSETN, &ntty);
1031041Sbill 	printf("Key: ");
10416599Skre 	if (fgets(s, sizeof s, stdin) == NULL) {
10516599Skre 		putchar('\n');
10616599Skre 		quit();
10716599Skre 	}
1081041Sbill 	printf("\nAgain: ");
10916599Skre 	/*
11016599Skre 	 * Don't need EOF test here, if we get EOF, then s1 != s
11116599Skre 	 * and the right things will happen.
11216599Skre 	 */
11316599Skre 	(void) fgets(s1, sizeof s1, stdin);
1141041Sbill 	putchar('\n');
1151041Sbill 	if (strcmp(s1, s)) {
1161041Sbill 		putchar(07);
1171041Sbill 		stty(0, &tty);
1181041Sbill 		exit(1);
1191041Sbill 	}
1201041Sbill 	s[0] = 0;
12116239Sralph 
12216239Sralph 	/* Set signal handlers */
12316239Sralph 
12416239Sralph 	signal(SIGINT, hi);
12516239Sralph 	signal(SIGQUIT, hi);
12616239Sralph 	signal(SIGTSTP, hi);
12716239Sralph 	signal(SIGALRM, bye);
12816239Sralph 	ntimer.it_interval = zerotime;
12916239Sralph 	ntimer.it_value = timeout;
13016239Sralph 	setitimer(ITIMER_REAL, &ntimer, &otimer);
13116239Sralph 
13216239Sralph 	/* Header info */
13316239Sralph 
13416239Sralph 	printf ("lock: %s on %s. timeout in %d minutes\n",
13516239Sralph 		ttynam, hostname, sectimeout);
13616239Sralph 	printf("time now is %.20s", ap);
13716239Sralph 	if (tzn)
13816239Sralph 		printf("%s", tzn);
13916239Sralph 	printf("%s", ap+19);
14016239Sralph 
14116239Sralph 	/* wait */
14216239Sralph 
1431041Sbill 	for (;;) {
14416239Sralph 		printf("Key: ");
14516599Skre 		if (fgets(s, sizeof s, stdin) == NULL) {
14616599Skre 			clearerr(stdin);
14716599Skre 			hi();
14816599Skre 			continue;
14916599Skre 		}
1501041Sbill 		if (strcmp(s1, s) == 0)
1511041Sbill 			break;
15216239Sralph 		if (pwd == (struct passwd *) 0 || pwd->pw_passwd[0] == '\0')
1531041Sbill 			break;
15416239Sralph 		ap = index(s, '\n');
15516239Sralph 		if (ap != NULL)
15616239Sralph 			*ap = '\0';
15716239Sralph 		if (strcmp(pwd->pw_passwd, crypt(s, pwd->pw_passwd)) == 0)
15816239Sralph 			break;
15916239Sralph 		printf("\07\n");
16012989Ssam 		if (ioctl(0, TIOCGETP, &ntty))
1611041Sbill 			exit(1);
1621041Sbill 	}
16312989Ssam 	ioctl(0, TIOCSETN, &tty);
16416239Sralph 	putchar('\n');
16516239Sralph 	exit (0);
16616239Sralph usage:
16716239Sralph 	printf("Usage: lock [-timeout]\n");
16816239Sralph 	exit (1);
1691041Sbill }
17016239Sralph 
17116239Sralph /*
17216239Sralph  *	get out of here
17316239Sralph  */
17416239Sralph 
17516239Sralph quit()
17616239Sralph {
17716239Sralph 	ioctl(0, TIOCSETN, &tty);
17816239Sralph 	exit (0);
17916239Sralph }
18016239Sralph 
18116239Sralph bye()
18216239Sralph {
18316239Sralph 	ioctl(0, TIOCSETN, &tty);
18416239Sralph 	printf("lock: timeout\n");
18516239Sralph 	exit (1);
18616239Sralph }
18716239Sralph 
18816239Sralph /*
18916239Sralph  *	tell the user we are waiting
19016239Sralph  */
19116239Sralph 
19216239Sralph hi()
19316239Sralph {
19416239Sralph 	long	curtime;
19516239Sralph 	struct timeval	timval;
19616239Sralph 	struct timezone	timzone;
19716239Sralph 
19816239Sralph 	gettimeofday(&timval, &timzone);
19916239Sralph 	curtime = timval.tv_sec;
20016239Sralph 	printf("lock: type in the unlock key. timeout in %d minutes\n",
20116239Sralph 		(nexttime-curtime)/60);
20216239Sralph }
203