xref: /freebsd/usr.bin/lock/lock.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*
2  * Copyright (c) 1980, 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Bob Toxen.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1980, 1987, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 static char sccsid[] = "@(#)lock.c	8.1 (Berkeley) 6/6/93";
45 #endif /* not lint */
46 
47 /*
48  * Lock a terminal up until the given key is entered, until the root
49  * password is entered, or the given interval times out.
50  *
51  * Timeout interval is by default TIMEOUT, it can be changed with
52  * an argument of the form -time where time is in minutes
53  */
54 
55 #include <sys/param.h>
56 #include <sys/stat.h>
57 #include <sys/time.h>
58 #include <sys/signal.h>
59 #include <sgtty.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #include <ctype.h>
63 #include <string.h>
64 
65 #define	TIMEOUT	15
66 
67 void quit(), bye(), hi();
68 
69 struct timeval	timeout;
70 struct timeval	zerotime;
71 struct sgttyb	tty, ntty;
72 long	nexttime;			/* keep the timeout time */
73 int            no_timeout;                     /* lock terminal forever */
74 
75 /*ARGSUSED*/
76 main(argc, argv)
77 	int argc;
78 	char **argv;
79 {
80 	extern char *optarg;
81 	extern int errno, optind;
82 	struct passwd *pw;
83 	struct timeval timval;
84 	struct itimerval ntimer, otimer;
85 	struct tm *timp;
86 	int ch, sectimeout, usemine;
87 	char *ap, *mypw, *ttynam, *tzn;
88 	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
89 	char *crypt(), *ttyname();
90 
91 	sectimeout = TIMEOUT;
92 	mypw = NULL;
93 	usemine = 0;
94        no_timeout = 0;
95        while ((ch = getopt(argc, argv, "npt:")) != EOF)
96 		switch((char)ch) {
97 		case 't':
98 			if ((sectimeout = atoi(optarg)) <= 0) {
99 				(void)fprintf(stderr,
100 				    "lock: illegal timeout value.\n");
101 				exit(1);
102 			}
103 			break;
104 		case 'p':
105 			usemine = 1;
106 			if (!(pw = getpwuid(getuid()))) {
107 				(void)fprintf(stderr,
108 				    "lock: unknown uid %d.\n", getuid());
109 				exit(1);
110 			}
111 			mypw = strdup(pw->pw_passwd);
112 			break;
113                case 'n':
114                        no_timeout = 1;
115                        break;
116 		case '?':
117 		default:
118 			(void)fprintf(stderr,
119                            "usage: lock [-n] [-p] [-t timeout]\n");
120 			exit(1);
121 	}
122 	timeout.tv_sec = sectimeout * 60;
123 
124 	setuid(getuid());		/* discard privs */
125 
126 	if (ioctl(0, TIOCGETP, &tty))	/* get information for header */
127 		exit(1);
128 	gethostname(hostname, sizeof(hostname));
129 	if (!(ttynam = ttyname(0))) {
130 		(void)printf("lock: not a terminal?\n");
131 		exit(1);
132 	}
133 	if (gettimeofday(&timval, (struct timezone *)NULL)) {
134 		(void)fprintf(stderr,
135 		    "lock: gettimeofday: %s\n", strerror(errno));
136 		exit(1);
137 	}
138 	nexttime = timval.tv_sec + (sectimeout * 60);
139 	timp = localtime(&timval.tv_sec);
140 	ap = asctime(timp);
141 	tzn = timp->tm_zone;
142 
143 	(void)signal(SIGINT, quit);
144 	(void)signal(SIGQUIT, quit);
145 	ntty = tty; ntty.sg_flags &= ~ECHO;
146 	(void)ioctl(0, TIOCSETP, &ntty);
147 
148 	if (!mypw) {
149 		/* get key and check again */
150 		(void)printf("Key: ");
151 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
152 			quit();
153 		(void)printf("\nAgain: ");
154 		/*
155 		 * Don't need EOF test here, if we get EOF, then s1 != s
156 		 * and the right things will happen.
157 		 */
158 		(void)fgets(s1, sizeof(s1), stdin);
159 		(void)putchar('\n');
160 		if (strcmp(s1, s)) {
161 			(void)printf("\07lock: passwords didn't match.\n");
162 			ioctl(0, TIOCSETP, &tty);
163 			exit(1);
164 		}
165 		s[0] = NULL;
166 		mypw = s1;
167 	}
168 
169 	/* set signal handlers */
170 	(void)signal(SIGINT, hi);
171 	(void)signal(SIGQUIT, hi);
172 	(void)signal(SIGTSTP, hi);
173 	(void)signal(SIGALRM, bye);
174 
175 	ntimer.it_interval = zerotime;
176 	ntimer.it_value = timeout;
177        if (!no_timeout)
178                setitimer(ITIMER_REAL, &ntimer, &otimer);
179 
180 	/* header info */
181        if (no_timeout) {
182 (void)printf("lock: %s on %s. no timeout\ntime now is %.20s%s%s",
183            ttynam, hostname, ap, tzn, ap + 19);
184        } else {
185 (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
186 	    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
187        }
188 
189 	for (;;) {
190 		(void)printf("Key: ");
191 		if (!fgets(s, sizeof(s), stdin)) {
192 			clearerr(stdin);
193 			hi();
194 			continue;
195 		}
196 		if (usemine) {
197 			s[strlen(s) - 1] = '\0';
198 			if (!strcmp(mypw, crypt(s, mypw)))
199 				break;
200 		}
201 		else if (!strcmp(s, s1))
202 			break;
203 		(void)printf("\07\n");
204 		if (ioctl(0, TIOCGETP, &ntty))
205 			exit(1);
206 	}
207 	quit();
208 }
209 
210 void
211 hi()
212 {
213 	struct timeval timval;
214 
215        if (!gettimeofday(&timval, (struct timezone *)NULL)) {
216                (void)printf("lock: type in the unlock key. ");
217                if (no_timeout) {
218                        (void)putchar('\n');
219                } else {
220                        (void)printf("timeout in %ld:%ld minutes\n",
221                                (nexttime - timval.tv_sec) / 60,
222                                (nexttime - timval.tv_sec) % 60);
223                }
224        }
225 }
226 
227 void
228 quit()
229 {
230 	(void)putchar('\n');
231 	(void)ioctl(0, TIOCSETP, &tty);
232 	exit(0);
233 }
234 
235 void
236 bye()
237 {
238        if (!no_timeout) {
239                (void)ioctl(0, TIOCSETP, &tty);
240                (void)printf("lock: timeout\n");
241                exit(1);
242        }
243 }
244