xref: /original-bsd/usr.bin/lock/lock.c (revision d15729d4)
1 /*
2  * Copyright (c) 1980, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)lock.c	5.13 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 /*
19  * Lock a terminal up until the given key is entered, until the root
20  * password is entered, or the given interval times out.
21  *
22  * Timeout interval is by default TIMEOUT, it can be changed with
23  * an argument of the form -time where time is in minutes
24  */
25 
26 #include <sys/param.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/signal.h>
30 #include <sgtty.h>
31 #include <pwd.h>
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <string.h>
35 
36 #define	TIMEOUT	15
37 
38 void quit(), bye(), hi();
39 
40 struct timeval	timeout;
41 struct timeval	zerotime;
42 struct sgttyb	tty, ntty;
43 long	nexttime;			/* keep the timeout time */
44 
45 /*ARGSUSED*/
46 main(argc, argv)
47 	int argc;
48 	char **argv;
49 {
50 	extern char *optarg;
51 	extern int errno, optind;
52 	struct passwd *pw;
53 	struct timeval timval;
54 	struct itimerval ntimer, otimer;
55 	struct tm *timp;
56 	int ch, sectimeout, usemine;
57 	char *ap, *mypw, *ttynam, *tzn;
58 	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
59 	char *crypt(), *ttyname();
60 
61 	sectimeout = TIMEOUT;
62 	mypw = NULL;
63 	usemine = 0;
64 	while ((ch = getopt(argc, argv, "pt:")) != EOF)
65 		switch((char)ch) {
66 		case 't':
67 			if ((sectimeout = atoi(optarg)) <= 0) {
68 				(void)fprintf(stderr,
69 				    "lock: illegal timeout value.\n");
70 				exit(1);
71 			}
72 			break;
73 		case 'p':
74 			usemine = 1;
75 			if (!(pw = getpwuid(getuid()))) {
76 				(void)fprintf(stderr,
77 				    "lock: unknown uid %d.\n", getuid());
78 				exit(1);
79 			}
80 			mypw = strdup(pw->pw_passwd);
81 			break;
82 		case '?':
83 		default:
84 			(void)fprintf(stderr,
85 			    "usage: lock [-p] [-t timeout]\n");
86 			exit(1);
87 	}
88 	timeout.tv_sec = sectimeout * 60;
89 
90 	setuid(getuid());		/* discard privs */
91 
92 	if (ioctl(0, TIOCGETP, &tty))	/* get information for header */
93 		exit(1);
94 	gethostname(hostname, sizeof(hostname));
95 	if (!(ttynam = ttyname(0))) {
96 		(void)printf("lock: not a terminal?\n");
97 		exit(1);
98 	}
99 	if (gettimeofday(&timval, (struct timezone *)NULL)) {
100 		(void)fprintf(stderr,
101 		    "lock: gettimeofday: %s\n", strerror(errno));
102 		exit(1);
103 	}
104 	nexttime = timval.tv_sec + (sectimeout * 60);
105 	timp = localtime(&timval.tv_sec);
106 	ap = asctime(timp);
107 	tzn = timp->tm_zone;
108 
109 	(void)signal(SIGINT, quit);
110 	(void)signal(SIGQUIT, quit);
111 	ntty = tty; ntty.sg_flags &= ~ECHO;
112 	(void)ioctl(0, TIOCSETP, &ntty);
113 
114 	if (!mypw) {
115 		/* get key and check again */
116 		(void)printf("Key: ");
117 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
118 			quit();
119 		(void)printf("\nAgain: ");
120 		/*
121 		 * Don't need EOF test here, if we get EOF, then s1 != s
122 		 * and the right things will happen.
123 		 */
124 		(void)fgets(s1, sizeof(s1), stdin);
125 		(void)putchar('\n');
126 		if (strcmp(s1, s)) {
127 			(void)printf("\07lock: passwords didn't match.\n");
128 			ioctl(0, TIOCSETP, &tty);
129 			exit(1);
130 		}
131 		s[0] = NULL;
132 		mypw = s1;
133 	}
134 
135 	/* set signal handlers */
136 	(void)signal(SIGINT, hi);
137 	(void)signal(SIGQUIT, hi);
138 	(void)signal(SIGTSTP, hi);
139 	(void)signal(SIGALRM, bye);
140 
141 	ntimer.it_interval = zerotime;
142 	ntimer.it_value = timeout;
143 	setitimer(ITIMER_REAL, &ntimer, &otimer);
144 
145 	/* header info */
146 (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
147 	    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
148 
149 	for (;;) {
150 		(void)printf("Key: ");
151 		if (!fgets(s, sizeof(s), stdin)) {
152 			clearerr(stdin);
153 			hi();
154 			continue;
155 		}
156 		if (usemine) {
157 			s[strlen(s) - 1] = '\0';
158 			if (!strcmp(mypw, crypt(s, mypw)))
159 				break;
160 		}
161 		else if (!strcmp(s, s1))
162 			break;
163 		(void)printf("\07\n");
164 		if (ioctl(0, TIOCGETP, &ntty))
165 			exit(1);
166 	}
167 	quit();
168 }
169 
170 void
171 hi()
172 {
173 	struct timeval timval;
174 
175 	if (!gettimeofday(&timval, (struct timezone *)NULL))
176 (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
177 	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
178 }
179 
180 void
181 quit()
182 {
183 	(void)putchar('\n');
184 	(void)ioctl(0, TIOCSETP, &tty);
185 	exit(0);
186 }
187 
188 void
189 bye()
190 {
191 	(void)ioctl(0, TIOCSETP, &tty);
192 	(void)printf("lock: timeout\n");
193 	exit(1);
194 }
195