2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1980, 1987, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Bob Toxen.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:33:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-21 12:09:34 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1980, 1987, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-21 12:09:34 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
static char sccsid[] = "@(#)lock.c 8.1 (Berkeley) 6/6/93";
|
1997-07-21 12:09:34 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
2002-07-10 04:05:33 +00:00
|
|
|
* Lock a terminal up until the given key is entered or the given
|
|
|
|
* interval times out.
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Timeout interval is by default TIMEOUT, it can be changed with
|
|
|
|
* an argument of the form -time where time is in minutes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/signal.h>
|
2002-07-28 07:13:53 +00:00
|
|
|
#include <sys/consio.h>
|
2012-09-01 14:45:15 +00:00
|
|
|
|
1997-07-21 12:09:34 +00:00
|
|
|
#include <err.h>
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <ctype.h>
|
2004-01-22 04:24:15 +00:00
|
|
|
#include <errno.h>
|
2010-12-25 11:24:27 +00:00
|
|
|
#include <paths.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <pwd.h>
|
2010-06-28 08:30:10 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
1997-07-21 12:09:34 +00:00
|
|
|
#include <stdlib.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <string.h>
|
1999-10-12 13:33:48 +00:00
|
|
|
#include <syslog.h>
|
2003-02-10 02:08:49 +00:00
|
|
|
#include <termios.h>
|
2012-09-01 14:45:15 +00:00
|
|
|
#include <time.h>
|
1997-07-21 12:09:34 +00:00
|
|
|
#include <unistd.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2018-02-11 13:35:31 +00:00
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/openpam.h> /* for openpam_ttyconv() */
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#define TIMEOUT 15
|
|
|
|
|
2011-11-06 08:15:48 +00:00
|
|
|
static void quit(int);
|
|
|
|
static void bye(int);
|
|
|
|
static void hi(int);
|
2002-03-22 01:22:50 +00:00
|
|
|
static void usage(void);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2011-11-06 08:15:48 +00:00
|
|
|
static struct timeval timeout;
|
|
|
|
static struct timeval zerotime;
|
|
|
|
static struct termios tty, ntty;
|
|
|
|
static long nexttime; /* keep the timeout time */
|
|
|
|
static int no_timeout; /* lock terminal forever */
|
|
|
|
static int vtyunlock; /* Unlock flag and code. */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*ARGSUSED*/
|
1997-07-21 12:09:34 +00:00
|
|
|
int
|
2002-09-04 23:29:10 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2018-02-11 13:35:31 +00:00
|
|
|
static const struct pam_conv pamc = { &openpam_ttyconv, NULL };
|
|
|
|
pam_handle_t *pamh;
|
1994-05-27 12:33:43 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
struct itimerval ntimer, otimer;
|
|
|
|
struct tm *timp;
|
2012-09-01 14:45:15 +00:00
|
|
|
time_t timval;
|
2018-02-11 13:35:31 +00:00
|
|
|
int ch, failures, pam_err, sectimeout, usemine, vtylock;
|
|
|
|
char *ap, *ttynam, *tzn;
|
1994-05-27 12:33:43 +00:00
|
|
|
char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
|
|
|
|
|
2014-08-17 19:06:26 +00:00
|
|
|
openlog("lock", 0, LOG_AUTH);
|
1999-10-12 13:33:48 +00:00
|
|
|
|
2018-02-11 13:35:31 +00:00
|
|
|
pam_err = PAM_SYSTEM_ERR; /* pacify GCC */
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
sectimeout = TIMEOUT;
|
2018-02-11 13:35:31 +00:00
|
|
|
pamh = NULL;
|
2010-12-25 11:24:27 +00:00
|
|
|
pw = NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
usemine = 0;
|
2002-07-10 04:03:33 +00:00
|
|
|
no_timeout = 0;
|
2002-07-28 07:13:53 +00:00
|
|
|
vtylock = 0;
|
|
|
|
while ((ch = getopt(argc, argv, "npt:v")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch((char)ch) {
|
|
|
|
case 't':
|
1997-07-21 12:09:34 +00:00
|
|
|
if ((sectimeout = atoi(optarg)) <= 0)
|
|
|
|
errx(1, "illegal timeout value");
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
usemine = 1;
|
1997-07-21 12:09:34 +00:00
|
|
|
if (!(pw = getpwuid(getuid())))
|
|
|
|
errx(1, "unknown uid %d", getuid());
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2002-07-10 04:03:33 +00:00
|
|
|
case 'n':
|
|
|
|
no_timeout = 1;
|
|
|
|
break;
|
2002-07-28 07:13:53 +00:00
|
|
|
case 'v':
|
2014-05-22 09:28:36 +00:00
|
|
|
vtylock = 1;
|
2002-07-28 07:13:53 +00:00
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
1997-07-21 12:09:34 +00:00
|
|
|
usage();
|
2002-07-10 04:03:33 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
timeout.tv_sec = sectimeout * 60;
|
|
|
|
|
2018-02-11 13:35:31 +00:00
|
|
|
if (!usemine) { /* -p with PAM or S/key needs privs */
|
|
|
|
/* discard privs */
|
|
|
|
if (setuid(getuid()) != 0)
|
|
|
|
errx(1, "setuid failed");
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2003-02-10 02:08:49 +00:00
|
|
|
if (tcgetattr(0, &tty)) /* get information for header */
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
gethostname(hostname, sizeof(hostname));
|
1997-07-21 12:09:34 +00:00
|
|
|
if (!(ttynam = ttyname(0)))
|
|
|
|
errx(1, "not a terminal?");
|
2010-12-25 11:24:27 +00:00
|
|
|
if (strncmp(ttynam, _PATH_DEV, strlen(_PATH_DEV)) == 0)
|
|
|
|
ttynam += strlen(_PATH_DEV);
|
2012-09-01 14:45:15 +00:00
|
|
|
timval = time(NULL);
|
|
|
|
nexttime = timval + (sectimeout * 60);
|
|
|
|
timp = localtime(&timval);
|
1994-05-27 12:33:43 +00:00
|
|
|
ap = asctime(timp);
|
|
|
|
tzn = timp->tm_zone;
|
|
|
|
|
|
|
|
(void)signal(SIGINT, quit);
|
|
|
|
(void)signal(SIGQUIT, quit);
|
2003-02-10 02:08:49 +00:00
|
|
|
ntty = tty; ntty.c_lflag &= ~ECHO;
|
|
|
|
(void)tcsetattr(0, TCSADRAIN|TCSASOFT, &ntty);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2018-02-11 13:35:31 +00:00
|
|
|
if (usemine) {
|
|
|
|
pam_err = pam_start("lock", pw->pw_name, &pamc, &pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
err(1, "pam_start: %s", pam_strerror(NULL, pam_err));
|
|
|
|
} else {
|
1994-05-27 12:33:43 +00:00
|
|
|
/* get key and check again */
|
|
|
|
(void)printf("Key: ");
|
|
|
|
if (!fgets(s, sizeof(s), stdin) || *s == '\n')
|
2001-12-03 21:01:52 +00:00
|
|
|
quit(0);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)printf("\nAgain: ");
|
|
|
|
/*
|
|
|
|
* Don't need EOF test here, if we get EOF, then s1 != s
|
|
|
|
* and the right things will happen.
|
|
|
|
*/
|
|
|
|
(void)fgets(s1, sizeof(s1), stdin);
|
|
|
|
(void)putchar('\n');
|
|
|
|
if (strcmp(s1, s)) {
|
|
|
|
(void)printf("\07lock: passwords didn't match.\n");
|
2003-02-10 02:08:49 +00:00
|
|
|
(void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1997-07-21 12:09:34 +00:00
|
|
|
s[0] = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set signal handlers */
|
|
|
|
(void)signal(SIGINT, hi);
|
|
|
|
(void)signal(SIGQUIT, hi);
|
|
|
|
(void)signal(SIGTSTP, hi);
|
|
|
|
(void)signal(SIGALRM, bye);
|
|
|
|
|
|
|
|
ntimer.it_interval = zerotime;
|
|
|
|
ntimer.it_value = timeout;
|
2002-07-10 04:03:33 +00:00
|
|
|
if (!no_timeout)
|
|
|
|
setitimer(ITIMER_REAL, &ntimer, &otimer);
|
2002-07-28 07:13:53 +00:00
|
|
|
if (vtylock) {
|
|
|
|
/*
|
|
|
|
* If this failed, we want to err out; warn isn't good
|
|
|
|
* enough, since we don't want the user to think that
|
|
|
|
* everything is nice and locked because they got a
|
|
|
|
* "Key:" prompt.
|
|
|
|
*/
|
|
|
|
if (ioctl(0, VT_LOCKSWITCH, &vtylock) == -1) {
|
2003-02-10 02:08:49 +00:00
|
|
|
(void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
|
2002-07-28 07:13:53 +00:00
|
|
|
err(1, "locking vty");
|
|
|
|
}
|
2014-05-22 09:28:36 +00:00
|
|
|
vtyunlock = 0x2;
|
2002-07-28 07:13:53 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* header info */
|
2010-12-25 11:24:27 +00:00
|
|
|
if (pw != NULL)
|
2010-12-27 15:05:15 +00:00
|
|
|
(void)printf("lock: %s using %s on %s.", pw->pw_name,
|
|
|
|
ttynam, hostname);
|
2010-12-25 11:24:27 +00:00
|
|
|
else
|
|
|
|
(void)printf("lock: %s on %s.", ttynam, hostname);
|
2002-07-28 07:13:53 +00:00
|
|
|
if (no_timeout)
|
|
|
|
(void)printf(" no timeout.");
|
|
|
|
else
|
|
|
|
(void)printf(" timeout in %d minute%s.", sectimeout,
|
|
|
|
sectimeout != 1 ? "s" : "");
|
|
|
|
if (vtylock)
|
|
|
|
(void)printf(" vty locked.");
|
|
|
|
(void)printf("\ntime now is %.20s%s%s", ap, tzn, ap + 19);
|
|
|
|
|
2002-07-10 04:03:33 +00:00
|
|
|
failures = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2018-02-11 13:35:31 +00:00
|
|
|
if (usemine) {
|
|
|
|
pam_err = pam_authenticate(pamh, 0);
|
|
|
|
if (pam_err == PAM_SUCCESS)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (pam_err != PAM_AUTH_ERR &&
|
|
|
|
pam_err != PAM_USER_UNKNOWN &&
|
|
|
|
pam_err != PAM_MAXTRIES) {
|
|
|
|
syslog(LOG_ERR, "pam_authenticate: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto tryagain;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)printf("Key: ");
|
|
|
|
if (!fgets(s, sizeof(s), stdin)) {
|
|
|
|
clearerr(stdin);
|
2001-12-03 21:01:52 +00:00
|
|
|
hi(0);
|
2004-01-22 04:24:15 +00:00
|
|
|
goto tryagain;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2018-02-11 13:35:31 +00:00
|
|
|
if (!strcmp(s, s1))
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
(void)printf("\07\n");
|
1999-10-12 13:33:48 +00:00
|
|
|
failures++;
|
|
|
|
if (getuid() == 0)
|
|
|
|
syslog(LOG_NOTICE, "%d ROOT UNLOCK FAILURE%s (%s on %s)",
|
|
|
|
failures, failures > 1 ? "S": "", ttynam, hostname);
|
2004-01-22 04:24:15 +00:00
|
|
|
tryagain:
|
|
|
|
if (tcgetattr(0, &ntty) && (errno != EINTR))
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
1999-10-12 13:33:48 +00:00
|
|
|
sleep(1); /* to discourage guessing */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1999-10-12 13:33:48 +00:00
|
|
|
if (getuid() == 0)
|
2002-07-10 04:03:33 +00:00
|
|
|
syslog(LOG_NOTICE, "ROOT UNLOCK ON hostname %s port %s",
|
|
|
|
hostname, ttynam);
|
2018-02-11 13:35:31 +00:00
|
|
|
if (usemine)
|
|
|
|
(void)pam_end(pamh, pam_err);
|
2001-12-03 21:01:52 +00:00
|
|
|
quit(0);
|
1997-07-21 12:09:34 +00:00
|
|
|
return(0); /* not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
usage(void)
|
1997-07-21 12:09:34 +00:00
|
|
|
{
|
2002-07-28 07:13:53 +00:00
|
|
|
(void)fprintf(stderr, "usage: lock [-npv] [-t timeout]\n");
|
1997-07-21 12:09:34 +00:00
|
|
|
exit(1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 08:15:48 +00:00
|
|
|
static void
|
2001-12-03 21:01:52 +00:00
|
|
|
hi(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2012-09-01 14:45:15 +00:00
|
|
|
time_t timval;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2012-09-01 14:45:15 +00:00
|
|
|
timval = time(NULL);
|
|
|
|
(void)printf("lock: type in the unlock key. ");
|
|
|
|
if (no_timeout) {
|
|
|
|
(void)putchar('\n');
|
|
|
|
} else {
|
|
|
|
(void)printf("timeout in %jd:%jd minutes\n",
|
|
|
|
(intmax_t)(nexttime - timval) / 60,
|
|
|
|
(intmax_t)(nexttime - timval) % 60);
|
2002-07-10 04:03:33 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 08:15:48 +00:00
|
|
|
static void
|
2001-12-03 21:01:52 +00:00
|
|
|
quit(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
(void)putchar('\n');
|
2003-02-10 02:08:49 +00:00
|
|
|
(void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
|
2002-07-28 07:13:53 +00:00
|
|
|
if (vtyunlock)
|
|
|
|
(void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:15:48 +00:00
|
|
|
static void
|
2001-12-03 21:01:52 +00:00
|
|
|
bye(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-07-10 04:03:33 +00:00
|
|
|
if (!no_timeout) {
|
2003-02-10 02:08:49 +00:00
|
|
|
(void)tcsetattr(0, TCSADRAIN|TCSASOFT, &tty);
|
2002-07-28 07:13:53 +00:00
|
|
|
if (vtyunlock)
|
|
|
|
(void)ioctl(0, VT_LOCKSWITCH, &vtyunlock);
|
2002-07-10 04:03:33 +00:00
|
|
|
(void)printf("lock: timeout\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|