2 * Copyright (c) 1980, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1980, 1987, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)lock.c 8.1 (Berkeley) 6/6/93";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
52 * Lock a terminal up until the given key is entered or the given
55 * Timeout interval is by default TIMEOUT, it can be changed with
56 * an argument of the form -time where time is in minutes
59 #include <sys/param.h>
62 #include <sys/signal.h>
63 #include <sys/consio.h>
81 static void usage(void);
83 struct timeval timeout;
84 struct timeval zerotime;
85 struct termios tty, ntty;
86 long nexttime; /* keep the timeout time */
87 int no_timeout; /* lock terminal forever */
88 int vtyunlock; /* Unlock flag and code. */
92 main(int argc, char **argv)
95 struct timeval timval;
97 struct itimerval ntimer, otimer;
99 int ch, failures, sectimeout, usemine, vtylock;
100 char *ap, *mypw, *ttynam, *tzn;
101 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
103 openlog("lock", LOG_ODELAY, LOG_AUTH);
105 sectimeout = TIMEOUT;
111 while ((ch = getopt(argc, argv, "npt:v")) != -1)
114 if ((sectimeout = atoi(optarg)) <= 0)
115 errx(1, "illegal timeout value");
119 if (!(pw = getpwuid(getuid())))
120 errx(1, "unknown uid %d", getuid());
121 mypw = strdup(pw->pw_passwd);
133 timeout.tv_sec = sectimeout * 60;
136 if (setuid(getuid()) != 0)
137 errx(1, "setuid failed");
139 if (tcgetattr(0, &tty)) /* get information for header */
141 gethostname(hostname, sizeof(hostname));
142 if (!(ttynam = ttyname(0)))
143 errx(1, "not a terminal?");
144 if (strncmp(ttynam, _PATH_DEV, strlen(_PATH_DEV)) == 0)
145 ttynam += strlen(_PATH_DEV);
146 if (gettimeofday(&timval, (struct timezone *)NULL))
147 err(1, "gettimeofday");
148 nexttime = timval.tv_sec + (sectimeout * 60);
149 timval_sec = timval.tv_sec;
150 timp = localtime(&timval_sec);
154 (void)signal(SIGINT, quit);
155 (void)signal(SIGQUIT, quit);
156 ntty = tty; ntty.c_lflag &= ~ECHO;
157 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &ntty);
160 /* get key and check again */
161 (void)printf("Key: ");
162 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
164 (void)printf("\nAgain: ");
166 * Don't need EOF test here, if we get EOF, then s1 != s
167 * and the right things will happen.
169 (void)fgets(s1, sizeof(s1), stdin);
172 (void)printf("\07lock: passwords didn't match.\n");
173 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
180 /* set signal handlers */
181 (void)signal(SIGINT, hi);
182 (void)signal(SIGQUIT, hi);
183 (void)signal(SIGTSTP, hi);
184 (void)signal(SIGALRM, bye);
186 ntimer.it_interval = zerotime;
187 ntimer.it_value = timeout;
189 setitimer(ITIMER_REAL, &ntimer, &otimer);
192 * If this failed, we want to err out; warn isn't good
193 * enough, since we don't want the user to think that
194 * everything is nice and locked because they got a
197 if (ioctl(0, VT_LOCKSWITCH, &vtylock) == -1) {
198 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
199 err(1, "locking vty");
206 (void)printf("lock: %s using %s on %s.", pw->pw_name,
209 (void)printf("lock: %s on %s.", ttynam, hostname);
211 (void)printf(" no timeout.");
213 (void)printf(" timeout in %d minute%s.", sectimeout,
214 sectimeout != 1 ? "s" : "");
216 (void)printf(" vty locked.");
217 (void)printf("\ntime now is %.20s%s%s", ap, tzn, ap + 19);
222 (void)printf("Key: ");
223 if (!fgets(s, sizeof(s), stdin)) {
229 s[strlen(s) - 1] = '\0';
230 if (!strcmp(mypw, crypt(s, mypw)))
233 else if (!strcmp(s, s1))
235 (void)printf("\07\n");
238 syslog(LOG_NOTICE, "%d ROOT UNLOCK FAILURE%s (%s on %s)",
239 failures, failures > 1 ? "S": "", ttynam, hostname);
241 if (tcgetattr(0, &ntty) && (errno != EINTR))
243 sleep(1); /* to discourage guessing */
246 syslog(LOG_NOTICE, "ROOT UNLOCK ON hostname %s port %s",
249 return(0); /* not reached */
256 (void)fprintf(stderr, "usage: lock [-npv] [-t timeout]\n");
261 hi(int signo __unused)
263 struct timeval timval;
265 if (!gettimeofday(&timval, (struct timezone *)NULL)) {
266 (void)printf("lock: type in the unlock key. ");
270 (void)printf("timeout in %ld:%ld minutes\n",
271 (nexttime - timval.tv_sec) / 60,
272 (nexttime - timval.tv_sec) % 60);
278 quit(int signo __unused)
281 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
283 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
288 bye(int signo __unused)
291 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
293 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
294 (void)printf("lock: timeout\n");