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";
47 static const char rcsid[] =
52 * Lock a terminal up until the given key is entered, until the root
53 * password is entered, or the given interval times out.
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>
76 void quit(), bye(), hi();
77 static void usage __P((void));
79 struct timeval timeout;
80 struct timeval zerotime;
81 struct sgttyb tty, ntty;
82 long nexttime; /* keep the timeout time */
83 int no_timeout; /* lock terminal forever */
92 struct timeval timval;
94 struct itimerval ntimer, otimer;
96 int ch, failures, sectimeout, usemine;
97 char *ap, *mypw, *ttynam, *tzn;
98 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
99 char *crypt(), *ttyname();
101 openlog("lock", LOG_ODELAY, LOG_AUTH);
103 sectimeout = TIMEOUT;
107 while ((ch = getopt(argc, argv, "npt:")) != -1)
110 if ((sectimeout = atoi(optarg)) <= 0)
111 errx(1, "illegal timeout value");
115 if (!(pw = getpwuid(getuid())))
116 errx(1, "unknown uid %d", getuid());
117 mypw = strdup(pw->pw_passwd);
126 timeout.tv_sec = sectimeout * 60;
128 setuid(getuid()); /* discard privs */
130 if (ioctl(0, TIOCGETP, &tty)) /* get information for header */
132 gethostname(hostname, sizeof(hostname));
133 if (!(ttynam = ttyname(0)))
134 errx(1, "not a terminal?");
135 if (gettimeofday(&timval, (struct timezone *)NULL))
136 err(1, "gettimeofday");
137 nexttime = timval.tv_sec + (sectimeout * 60);
138 timval_sec = timval.tv_sec;
139 timp = localtime(&timval_sec);
143 (void)signal(SIGINT, quit);
144 (void)signal(SIGQUIT, quit);
145 ntty = tty; ntty.sg_flags &= ~ECHO;
146 (void)ioctl(0, TIOCSETP, &ntty);
149 /* get key and check again */
150 (void)printf("Key: ");
151 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
153 (void)printf("\nAgain: ");
155 * Don't need EOF test here, if we get EOF, then s1 != s
156 * and the right things will happen.
158 (void)fgets(s1, sizeof(s1), stdin);
161 (void)printf("\07lock: passwords didn't match.\n");
162 ioctl(0, TIOCSETP, &tty);
169 /* set signal handlers */
170 (void)signal(SIGINT, hi);
171 (void)signal(SIGQUIT, hi);
172 (void)signal(SIGTSTP, hi);
173 (void)signal(SIGALRM, bye);
175 ntimer.it_interval = zerotime;
176 ntimer.it_value = timeout;
178 setitimer(ITIMER_REAL, &ntimer, &otimer);
182 (void)printf("lock: %s on %s. no timeout\ntime now is %.20s%s%s",
183 ttynam, hostname, ap, tzn, ap + 19);
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);
191 (void)printf("Key: ");
192 if (!fgets(s, sizeof(s), stdin)) {
198 s[strlen(s) - 1] = '\0';
199 if (!strcmp(mypw, crypt(s, mypw)))
202 else if (!strcmp(s, s1))
204 (void)printf("\07\n");
207 syslog(LOG_NOTICE, "%d ROOT UNLOCK FAILURE%s (%s on %s)",
208 failures, failures > 1 ? "S": "", ttynam, hostname);
209 if (ioctl(0, TIOCGETP, &ntty))
211 sleep(1); /* to discourage guessing */
214 syslog(LOG_NOTICE, "ROOT UNLOCK ON hostname %s port %s",
217 return(0); /* not reached */
224 (void)fprintf(stderr, "usage: lock [-n] [-p] [-t timeout]\n");
231 struct timeval timval;
233 if (!gettimeofday(&timval, (struct timezone *)NULL)) {
234 (void)printf("lock: type in the unlock key. ");
238 (void)printf("timeout in %ld:%ld minutes\n",
239 (nexttime - timval.tv_sec) / 60,
240 (nexttime - timval.tv_sec) % 60);
249 (void)ioctl(0, TIOCSETP, &tty);
257 (void)ioctl(0, TIOCSETP, &tty);
258 (void)printf("lock: timeout\n");