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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1987, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)lock.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * Lock a terminal up until the given key is entered or the given
51 * Timeout interval is by default TIMEOUT, it can be changed with
52 * an argument of the form -time where time is in minutes
55 #include <sys/param.h>
57 #include <sys/signal.h>
58 #include <sys/consio.h>
76 static void quit(int);
79 static void usage(void);
81 static struct timeval timeout;
82 static struct timeval zerotime;
83 static struct termios tty, ntty;
84 static long nexttime; /* keep the timeout time */
85 static int no_timeout; /* lock terminal forever */
86 static int vtyunlock; /* Unlock flag and code. */
90 main(int argc, char **argv)
93 struct itimerval ntimer, otimer;
96 int ch, failures, sectimeout, usemine, vtylock;
97 char *ap, *cryptpw, *mypw, *ttynam, *tzn;
98 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
100 openlog("lock", LOG_ODELAY, LOG_AUTH);
102 sectimeout = TIMEOUT;
108 while ((ch = getopt(argc, argv, "npt:v")) != -1)
111 if ((sectimeout = atoi(optarg)) <= 0)
112 errx(1, "illegal timeout value");
116 if (!(pw = getpwuid(getuid())))
117 errx(1, "unknown uid %d", getuid());
118 mypw = strdup(pw->pw_passwd);
130 timeout.tv_sec = sectimeout * 60;
133 if (setuid(getuid()) != 0)
134 errx(1, "setuid failed");
136 if (tcgetattr(0, &tty)) /* get information for header */
138 gethostname(hostname, sizeof(hostname));
139 if (!(ttynam = ttyname(0)))
140 errx(1, "not a terminal?");
141 if (strncmp(ttynam, _PATH_DEV, strlen(_PATH_DEV)) == 0)
142 ttynam += strlen(_PATH_DEV);
144 nexttime = timval + (sectimeout * 60);
145 timp = localtime(&timval);
149 (void)signal(SIGINT, quit);
150 (void)signal(SIGQUIT, quit);
151 ntty = tty; ntty.c_lflag &= ~ECHO;
152 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &ntty);
155 /* get key and check again */
156 (void)printf("Key: ");
157 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
159 (void)printf("\nAgain: ");
161 * Don't need EOF test here, if we get EOF, then s1 != s
162 * and the right things will happen.
164 (void)fgets(s1, sizeof(s1), stdin);
167 (void)printf("\07lock: passwords didn't match.\n");
168 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
175 /* set signal handlers */
176 (void)signal(SIGINT, hi);
177 (void)signal(SIGQUIT, hi);
178 (void)signal(SIGTSTP, hi);
179 (void)signal(SIGALRM, bye);
181 ntimer.it_interval = zerotime;
182 ntimer.it_value = timeout;
184 setitimer(ITIMER_REAL, &ntimer, &otimer);
187 * If this failed, we want to err out; warn isn't good
188 * enough, since we don't want the user to think that
189 * everything is nice and locked because they got a
192 if (ioctl(0, VT_LOCKSWITCH, &vtylock) == -1) {
193 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
194 err(1, "locking vty");
201 (void)printf("lock: %s using %s on %s.", pw->pw_name,
204 (void)printf("lock: %s on %s.", ttynam, hostname);
206 (void)printf(" no timeout.");
208 (void)printf(" timeout in %d minute%s.", sectimeout,
209 sectimeout != 1 ? "s" : "");
211 (void)printf(" vty locked.");
212 (void)printf("\ntime now is %.20s%s%s", ap, tzn, ap + 19);
217 (void)printf("Key: ");
218 if (!fgets(s, sizeof(s), stdin)) {
224 s[strlen(s) - 1] = '\0';
225 cryptpw = crypt(s, mypw);
226 if (cryptpw == NULL || !strcmp(mypw, cryptpw))
229 else if (!strcmp(s, s1))
231 (void)printf("\07\n");
234 syslog(LOG_NOTICE, "%d ROOT UNLOCK FAILURE%s (%s on %s)",
235 failures, failures > 1 ? "S": "", ttynam, hostname);
237 if (tcgetattr(0, &ntty) && (errno != EINTR))
239 sleep(1); /* to discourage guessing */
242 syslog(LOG_NOTICE, "ROOT UNLOCK ON hostname %s port %s",
245 return(0); /* not reached */
252 (void)fprintf(stderr, "usage: lock [-npv] [-t timeout]\n");
257 hi(int signo __unused)
262 (void)printf("lock: type in the unlock key. ");
266 (void)printf("timeout in %jd:%jd minutes\n",
267 (intmax_t)(nexttime - timval) / 60,
268 (intmax_t)(nexttime - timval) % 60);
273 quit(int signo __unused)
276 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
278 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
283 bye(int signo __unused)
286 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
288 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
289 (void)printf("lock: timeout\n");