xref: /csrg-svn/usr.bin/lock/lock.c (revision 16239)
112989Ssam #ifndef lint
2*16239Sralph static char *sccsid = "@(#)lock.c	4.4 (Berkeley) 03/26/84";
312989Ssam #endif
412989Ssam 
512989Ssam /*
6*16239Sralph  * Lock a terminal up until the given key is entered,
7*16239Sralph  * or until the root password is entered,
8*16239Sralph  * or the given interval times out.
9*16239Sralph  *
10*16239Sralph  * Timeout interval is by default TIMEOUT, it can be changed with
11*16239Sralph  * an argument of the form -time where time is in minutes
1212989Ssam  */
13*16239Sralph 
14*16239Sralph #include <pwd.h>
151041Sbill #include <stdio.h>
161041Sbill #include <sys/types.h>
175748Sroot #include <sys/stat.h>
18*16239Sralph #include <sys/time.h>
191041Sbill #include <signal.h>
201041Sbill #include <sgtty.h>
211041Sbill 
22*16239Sralph #define TIMEOUT 15
231041Sbill 
24*16239Sralph struct	passwd *pwd;
25*16239Sralph char	*crypt();
26*16239Sralph char	*getpass();
27*16239Sralph char	*index();
28*16239Sralph char	*ttyname();
29*16239Sralph char	*timezone();
30*16239Sralph char	*asctime();
31*16239Sralph struct	tm *localtime();
32*16239Sralph 
33*16239Sralph int	quit();
34*16239Sralph int	bye();
35*16239Sralph int	hi();
36*16239Sralph 
37*16239Sralph struct timeval	timeout	= {0, 0};
38*16239Sralph struct timeval	zerotime = {0, 0};
39*16239Sralph struct sgttyb	tty, ntty;
40*16239Sralph long	nexttime;		/* keep the timeout time */
41*16239Sralph 
421041Sbill main(argc, argv)
43*16239Sralph 	int argc;
441041Sbill 	char **argv;
451041Sbill {
461041Sbill 	register int t;
47*16239Sralph 	char	*ttynam;
48*16239Sralph 	char	*ap;
49*16239Sralph 	int	sectimeout = TIMEOUT;
50*16239Sralph 	char	s[BUFSIZ], s1[BUFSIZ];
51*16239Sralph 	char	hostname[32];
52*16239Sralph 	char	*tzn;
53*16239Sralph 	struct timeval	timval;
54*16239Sralph 	struct itimerval	ntimer, otimer;
55*16239Sralph 	struct timezone	timzone;
56*16239Sralph 	struct tm	*timp;
57*16239Sralph 	struct stat	statb;
581041Sbill 
59*16239Sralph 	/* process arguments */
60*16239Sralph 
61*16239Sralph 	if (argc > 1){
62*16239Sralph 		if (argv[1][0] != '-')
63*16239Sralph 			goto usage;
64*16239Sralph 		if (sscanf(&(argv[1][1]), "%d", &sectimeout) != 1)
65*16239Sralph 			goto usage;
66*16239Sralph 	}
67*16239Sralph 	timeout.tv_sec = sectimeout * 60;
68*16239Sralph 
69*16239Sralph 	/* get information for header */
70*16239Sralph 
7112989Ssam 	if (ioctl(0, TIOCGETP, &tty))
721041Sbill 		exit(1);
73*16239Sralph 	pwd = getpwuid(0);
74*16239Sralph 	gethostname(hostname, sizeof(hostname));
75*16239Sralph 	if (!(ttynam = ttyname(0))){
76*16239Sralph 		printf("lock: not a terminal?");
77*16239Sralph 		exit (1);
78*16239Sralph 	}
79*16239Sralph 	gettimeofday(&timval, &timzone);
80*16239Sralph 	nexttime = timval.tv_sec + (sectimeout * 60);
81*16239Sralph 	timp = localtime(&timval.tv_sec);
82*16239Sralph 	ap = asctime(timp);
83*16239Sralph 	tzn = timezone(timzone.tz_minuteswest, timp->tm_isdst);
84*16239Sralph 
85*16239Sralph 	/* get key and check again */
86*16239Sralph 
87*16239Sralph 	signal(SIGINT, quit);
88*16239Sralph 	signal(SIGQUIT, quit);
891041Sbill 	ntty = tty; ntty.sg_flags &= ~ECHO;
9012989Ssam 	ioctl(0, TIOCSETN, &ntty);
911041Sbill 	printf("Key: ");
921041Sbill 	fgets(s, sizeof s, stdin);
931041Sbill 	printf("\nAgain: ");
941041Sbill 	fgets(s1, sizeof s1, stdin);
951041Sbill 	putchar('\n');
961041Sbill 	if (strcmp(s1, s)) {
971041Sbill 		putchar(07);
981041Sbill 		stty(0, &tty);
991041Sbill 		exit(1);
1001041Sbill 	}
1011041Sbill 	s[0] = 0;
102*16239Sralph 
103*16239Sralph 	/* Set signal handlers */
104*16239Sralph 
105*16239Sralph 	signal(SIGINT, hi);
106*16239Sralph 	signal(SIGQUIT, hi);
107*16239Sralph 	signal(SIGTSTP, hi);
108*16239Sralph 	signal(SIGALRM, bye);
109*16239Sralph 	ntimer.it_interval = zerotime;
110*16239Sralph 	ntimer.it_value = timeout;
111*16239Sralph 	setitimer(ITIMER_REAL, &ntimer, &otimer);
112*16239Sralph 
113*16239Sralph 	/* Header info */
114*16239Sralph 
115*16239Sralph 	printf ("lock: %s on %s. timeout in %d minutes\n",
116*16239Sralph 		ttynam, hostname, sectimeout);
117*16239Sralph 	printf("time now is %.20s", ap);
118*16239Sralph 	if (tzn)
119*16239Sralph 		printf("%s", tzn);
120*16239Sralph 	printf("%s", ap+19);
121*16239Sralph 
122*16239Sralph 	/* wait */
123*16239Sralph 
1241041Sbill 	for (;;) {
125*16239Sralph 		printf("Key: ");
1261041Sbill 		fgets(s, sizeof s, stdin);
1271041Sbill 		if (strcmp(s1, s) == 0)
1281041Sbill 			break;
129*16239Sralph 		if (pwd == (struct passwd *) 0 || pwd->pw_passwd[0] == '\0')
1301041Sbill 			break;
131*16239Sralph 		ap = index(s, '\n');
132*16239Sralph 		if (ap != NULL)
133*16239Sralph 			*ap = '\0';
134*16239Sralph 		if (strcmp(pwd->pw_passwd, crypt(s, pwd->pw_passwd)) == 0)
135*16239Sralph 			break;
136*16239Sralph 		printf("\07\n");
13712989Ssam 		if (ioctl(0, TIOCGETP, &ntty))
1381041Sbill 			exit(1);
1391041Sbill 	}
14012989Ssam 	ioctl(0, TIOCSETN, &tty);
141*16239Sralph 	putchar('\n');
142*16239Sralph 	exit (0);
143*16239Sralph usage:
144*16239Sralph 	printf("Usage: lock [-timeout]\n");
145*16239Sralph 	exit (1);
1461041Sbill }
147*16239Sralph 
148*16239Sralph /*
149*16239Sralph  *	get out of here
150*16239Sralph  */
151*16239Sralph 
152*16239Sralph quit()
153*16239Sralph {
154*16239Sralph 	ioctl(0, TIOCSETN, &tty);
155*16239Sralph 	exit (0);
156*16239Sralph }
157*16239Sralph 
158*16239Sralph bye()
159*16239Sralph {
160*16239Sralph 	ioctl(0, TIOCSETN, &tty);
161*16239Sralph 	printf("lock: timeout\n");
162*16239Sralph 	exit (1);
163*16239Sralph }
164*16239Sralph 
165*16239Sralph /*
166*16239Sralph  *	tell the user we are waiting
167*16239Sralph  */
168*16239Sralph 
169*16239Sralph hi()
170*16239Sralph {
171*16239Sralph 	long	curtime;
172*16239Sralph 	struct timeval	timval;
173*16239Sralph 	struct timezone	timzone;
174*16239Sralph 
175*16239Sralph 	gettimeofday(&timval, &timzone);
176*16239Sralph 	curtime = timval.tv_sec;
177*16239Sralph 	printf("lock: type in the unlock key. timeout in %d minutes\n",
178*16239Sralph 		(nexttime-curtime)/60);
179*16239Sralph }
180