2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1987, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1980, 1987, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)lock.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 * Lock a terminal up until the given key is entered or the given
53 * Timeout interval is by default TIMEOUT, it can be changed with
54 * an argument of the form -time where time is in minutes
57 #include <sys/param.h>
59 #include <sys/signal.h>
60 #include <sys/consio.h>
76 #include <security/pam_appl.h>
77 #include <security/openpam.h> /* for openpam_ttyconv() */
81 static void quit(int);
84 static void usage(void);
86 static struct timeval timeout;
87 static struct timeval zerotime;
88 static struct termios tty, ntty;
89 static long nexttime; /* keep the timeout time */
90 static int no_timeout; /* lock terminal forever */
91 static int vtyunlock; /* Unlock flag and code. */
95 main(int argc, char **argv)
97 static const struct pam_conv pamc = { &openpam_ttyconv, NULL };
100 struct itimerval ntimer, otimer;
103 int ch, failures, pam_err, sectimeout, usemine, vtylock;
104 char *ap, *ttynam, *tzn;
105 char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
107 openlog("lock", 0, LOG_AUTH);
109 pam_err = PAM_SYSTEM_ERR; /* pacify GCC */
111 sectimeout = TIMEOUT;
117 while ((ch = getopt(argc, argv, "npt:v")) != -1)
120 if ((sectimeout = atoi(optarg)) <= 0)
121 errx(1, "illegal timeout value");
125 if (!(pw = getpwuid(getuid())))
126 errx(1, "unknown uid %d", getuid());
138 timeout.tv_sec = sectimeout * 60;
140 if (!usemine) { /* -p with PAM or S/key needs privs */
142 if (setuid(getuid()) != 0)
143 errx(1, "setuid failed");
146 if (tcgetattr(0, &tty)) /* get information for header */
148 gethostname(hostname, sizeof(hostname));
149 if (!(ttynam = ttyname(0)))
150 errx(1, "not a terminal?");
151 if (strncmp(ttynam, _PATH_DEV, strlen(_PATH_DEV)) == 0)
152 ttynam += strlen(_PATH_DEV);
154 nexttime = timval + (sectimeout * 60);
155 timp = localtime(&timval);
159 (void)signal(SIGINT, quit);
160 (void)signal(SIGQUIT, quit);
161 ntty = tty; ntty.c_lflag &= ~ECHO;
162 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &ntty);
165 pam_err = pam_start("lock", pw->pw_name, &pamc, &pamh);
166 if (pam_err != PAM_SUCCESS)
167 err(1, "pam_start: %s", pam_strerror(NULL, pam_err));
169 /* get key and check again */
170 (void)printf("Key: ");
171 if (!fgets(s, sizeof(s), stdin) || *s == '\n')
173 (void)printf("\nAgain: ");
175 * Don't need EOF test here, if we get EOF, then s1 != s
176 * and the right things will happen.
178 (void)fgets(s1, sizeof(s1), stdin);
181 (void)printf("\07lock: passwords didn't match.\n");
182 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
188 /* set signal handlers */
189 (void)signal(SIGINT, hi);
190 (void)signal(SIGQUIT, hi);
191 (void)signal(SIGTSTP, hi);
192 (void)signal(SIGALRM, bye);
194 ntimer.it_interval = zerotime;
195 ntimer.it_value = timeout;
197 setitimer(ITIMER_REAL, &ntimer, &otimer);
200 * If this failed, we want to err out; warn isn't good
201 * enough, since we don't want the user to think that
202 * everything is nice and locked because they got a
205 if (ioctl(0, VT_LOCKSWITCH, &vtylock) == -1) {
206 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
207 err(1, "locking vty");
214 (void)printf("lock: %s using %s on %s.", pw->pw_name,
217 (void)printf("lock: %s on %s.", ttynam, hostname);
219 (void)printf(" no timeout.");
221 (void)printf(" timeout in %d minute%s.", sectimeout,
222 sectimeout != 1 ? "s" : "");
224 (void)printf(" vty locked.");
225 (void)printf("\ntime now is %.20s%s%s", ap, tzn, ap + 19);
231 pam_err = pam_authenticate(pamh, 0);
232 if (pam_err == PAM_SUCCESS)
235 if (pam_err != PAM_AUTH_ERR &&
236 pam_err != PAM_USER_UNKNOWN &&
237 pam_err != PAM_MAXTRIES) {
238 syslog(LOG_ERR, "pam_authenticate: %s",
239 pam_strerror(pamh, pam_err));
244 (void)printf("Key: ");
245 if (!fgets(s, sizeof(s), stdin)) {
252 (void)printf("\07\n");
255 syslog(LOG_NOTICE, "%d ROOT UNLOCK FAILURE%s (%s on %s)",
256 failures, failures > 1 ? "S": "", ttynam, hostname);
258 if (tcgetattr(0, &ntty) && (errno != EINTR))
260 sleep(1); /* to discourage guessing */
263 syslog(LOG_NOTICE, "ROOT UNLOCK ON hostname %s port %s",
266 (void)pam_end(pamh, pam_err);
268 return(0); /* not reached */
275 (void)fprintf(stderr, "usage: lock [-npv] [-t timeout]\n");
280 hi(int signo __unused)
285 (void)printf("lock: type in the unlock key. ");
289 (void)printf("timeout in %jd:%jd minutes\n",
290 (intmax_t)(nexttime - timval) / 60,
291 (intmax_t)(nexttime - timval) % 60);
296 quit(int signo __unused)
299 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
301 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
306 bye(int signo __unused)
309 (void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
311 (void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
312 (void)printf("lock: timeout\n");