Home | History | Annotate | Line # | Download | only in lock
lock.c revision 1.9
      1  1.9       pk /*	$NetBSD: lock.c,v 1.9 1997/05/17 19:49:02 pk Exp $	*/
      2  1.4      jtc 
      3  1.1      cgd /*
      4  1.4      jtc  * Copyright (c) 1980, 1987, 1993
      5  1.4      jtc  *	The Regents of the University of California.  All rights reserved.
      6  1.4      jtc  *
      7  1.4      jtc  * This code is derived from software contributed to Berkeley by
      8  1.4      jtc  * Bob Toxen.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  */
     38  1.1      cgd 
     39  1.1      cgd #ifndef lint
     40  1.4      jtc static char copyright[] =
     41  1.4      jtc "@(#) Copyright (c) 1980, 1987, 1993\n\
     42  1.4      jtc 	The Regents of the University of California.  All rights reserved.\n";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.1      cgd #ifndef lint
     46  1.4      jtc #if 0
     47  1.4      jtc static char sccsid[] = "@(#)lock.c	8.1 (Berkeley) 6/6/93";
     48  1.4      jtc #endif
     49  1.9       pk static char rcsid[] = "$NetBSD: lock.c,v 1.9 1997/05/17 19:49:02 pk Exp $";
     50  1.1      cgd #endif /* not lint */
     51  1.1      cgd 
     52  1.1      cgd /*
     53  1.1      cgd  * Lock a terminal up until the given key is entered, until the root
     54  1.1      cgd  * password is entered, or the given interval times out.
     55  1.1      cgd  *
     56  1.1      cgd  * Timeout interval is by default TIMEOUT, it can be changed with
     57  1.1      cgd  * an argument of the form -time where time is in minutes
     58  1.1      cgd  */
     59  1.1      cgd 
     60  1.1      cgd #include <sys/param.h>
     61  1.1      cgd #include <sys/stat.h>
     62  1.1      cgd #include <sys/time.h>
     63  1.7      jtc #include <signal.h>
     64  1.5  mycroft 
     65  1.5  mycroft #include <ctype.h>
     66  1.5  mycroft #include <err.h>
     67  1.1      cgd #include <pwd.h>
     68  1.1      cgd #include <stdio.h>
     69  1.8      jtc #include <stdlib.h>
     70  1.1      cgd #include <string.h>
     71  1.5  mycroft #include <termios.h>
     72  1.8      jtc #include <unistd.h>
     73  1.1      cgd 
     74  1.1      cgd #define	TIMEOUT	15
     75  1.1      cgd 
     76  1.1      cgd void quit(), bye(), hi();
     77  1.1      cgd 
     78  1.1      cgd struct timeval	timeout;
     79  1.1      cgd struct timeval	zerotime;
     80  1.5  mycroft struct termios	tty, ntty;
     81  1.1      cgd long	nexttime;			/* keep the timeout time */
     82  1.1      cgd 
     83  1.1      cgd /*ARGSUSED*/
     84  1.1      cgd main(argc, argv)
     85  1.1      cgd 	int argc;
     86  1.1      cgd 	char **argv;
     87  1.1      cgd {
     88  1.1      cgd 	extern char *optarg;
     89  1.1      cgd 	struct passwd *pw;
     90  1.1      cgd 	struct timeval timval;
     91  1.1      cgd 	struct itimerval ntimer, otimer;
     92  1.1      cgd 	struct tm *timp;
     93  1.6      cgd 	time_t curtime;
     94  1.1      cgd 	int ch, sectimeout, usemine;
     95  1.1      cgd 	char *ap, *mypw, *ttynam, *tzn;
     96  1.1      cgd 	char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
     97  1.8      jtc 	char *crypt();
     98  1.1      cgd 
     99  1.1      cgd 	sectimeout = TIMEOUT;
    100  1.1      cgd 	mypw = NULL;
    101  1.1      cgd 	usemine = 0;
    102  1.3  deraadt 
    103  1.5  mycroft 	if (!(pw = getpwuid(getuid())))
    104  1.5  mycroft 		errx(1, "unknown uid %d.", getuid());
    105  1.9       pk 
    106  1.1      cgd 	while ((ch = getopt(argc, argv, "pt:")) != EOF)
    107  1.1      cgd 		switch((char)ch) {
    108  1.1      cgd 		case 't':
    109  1.5  mycroft 			if ((sectimeout = atoi(optarg)) <= 0)
    110  1.5  mycroft 				errx(1, "illegal timeout value: %s", optarg);
    111  1.1      cgd 			break;
    112  1.1      cgd 		case 'p':
    113  1.1      cgd 			usemine = 1;
    114  1.1      cgd 			mypw = strdup(pw->pw_passwd);
    115  1.1      cgd 			break;
    116  1.1      cgd 		case '?':
    117  1.1      cgd 		default:
    118  1.1      cgd 			(void)fprintf(stderr,
    119  1.1      cgd 			    "usage: lock [-p] [-t timeout]\n");
    120  1.1      cgd 			exit(1);
    121  1.1      cgd 	}
    122  1.1      cgd 	timeout.tv_sec = sectimeout * 60;
    123  1.1      cgd 
    124  1.1      cgd 	setuid(getuid());		/* discard privs */
    125  1.1      cgd 
    126  1.5  mycroft 	if (tcgetattr(0, &tty) < 0)	/* get information for header */
    127  1.1      cgd 		exit(1);
    128  1.1      cgd 	gethostname(hostname, sizeof(hostname));
    129  1.5  mycroft 	if (!(ttynam = ttyname(0)))
    130  1.5  mycroft 		errx(1, "not a terminal?");
    131  1.5  mycroft 	if (gettimeofday(&timval, (struct timezone *)NULL))
    132  1.5  mycroft 		err(1, "gettimeofday");
    133  1.6      cgd 	curtime = timval.tv_sec;
    134  1.1      cgd 	nexttime = timval.tv_sec + (sectimeout * 60);
    135  1.6      cgd 	timp = localtime(&curtime);
    136  1.1      cgd 	ap = asctime(timp);
    137  1.1      cgd 	tzn = timp->tm_zone;
    138  1.1      cgd 
    139  1.1      cgd 	(void)signal(SIGINT, quit);
    140  1.1      cgd 	(void)signal(SIGQUIT, quit);
    141  1.5  mycroft 	ntty = tty; ntty.c_lflag &= ~ECHO;
    142  1.5  mycroft 	(void)tcsetattr(0, TCSADRAIN, &ntty);
    143  1.1      cgd 
    144  1.1      cgd 	if (!mypw) {
    145  1.1      cgd 		/* get key and check again */
    146  1.1      cgd 		(void)printf("Key: ");
    147  1.1      cgd 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
    148  1.1      cgd 			quit();
    149  1.1      cgd 		(void)printf("\nAgain: ");
    150  1.1      cgd 		/*
    151  1.1      cgd 		 * Don't need EOF test here, if we get EOF, then s1 != s
    152  1.1      cgd 		 * and the right things will happen.
    153  1.1      cgd 		 */
    154  1.1      cgd 		(void)fgets(s1, sizeof(s1), stdin);
    155  1.1      cgd 		(void)putchar('\n');
    156  1.1      cgd 		if (strcmp(s1, s)) {
    157  1.5  mycroft 			(void)printf("\alock: passwords didn't match.\n");
    158  1.5  mycroft 			(void)tcsetattr(0, TCSADRAIN, &tty);
    159  1.1      cgd 			exit(1);
    160  1.1      cgd 		}
    161  1.9       pk 		s[0] = '\0';
    162  1.1      cgd 		mypw = s1;
    163  1.1      cgd 	}
    164  1.1      cgd 
    165  1.1      cgd 	/* set signal handlers */
    166  1.1      cgd 	(void)signal(SIGINT, hi);
    167  1.1      cgd 	(void)signal(SIGQUIT, hi);
    168  1.1      cgd 	(void)signal(SIGTSTP, hi);
    169  1.1      cgd 	(void)signal(SIGALRM, bye);
    170  1.1      cgd 
    171  1.1      cgd 	ntimer.it_interval = zerotime;
    172  1.1      cgd 	ntimer.it_value = timeout;
    173  1.1      cgd 	setitimer(ITIMER_REAL, &ntimer, &otimer);
    174  1.1      cgd 
    175  1.1      cgd 	/* header info */
    176  1.1      cgd (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
    177  1.1      cgd 	    ttynam, hostname, sectimeout, ap, tzn, ap + 19);
    178  1.1      cgd 
    179  1.1      cgd 	for (;;) {
    180  1.1      cgd 		(void)printf("Key: ");
    181  1.1      cgd 		if (!fgets(s, sizeof(s), stdin)) {
    182  1.1      cgd 			clearerr(stdin);
    183  1.1      cgd 			hi();
    184  1.1      cgd 			continue;
    185  1.1      cgd 		}
    186  1.1      cgd 		if (usemine) {
    187  1.1      cgd 			s[strlen(s) - 1] = '\0';
    188  1.3  deraadt #ifdef SKEY
    189  1.3  deraadt 			if (strcasecmp(s, "s/key") == 0) {
    190  1.3  deraadt 				if (skey_auth(pw->pw_name))
    191  1.3  deraadt 					break;
    192  1.3  deraadt 			}
    193  1.3  deraadt #endif
    194  1.1      cgd 			if (!strcmp(mypw, crypt(s, mypw)))
    195  1.1      cgd 				break;
    196  1.1      cgd 		}
    197  1.1      cgd 		else if (!strcmp(s, s1))
    198  1.1      cgd 			break;
    199  1.5  mycroft 		(void)printf("\a\n");
    200  1.5  mycroft 		if (tcsetattr(0, TCSADRAIN, &ntty) < 0)
    201  1.1      cgd 			exit(1);
    202  1.1      cgd 	}
    203  1.1      cgd 	quit();
    204  1.1      cgd }
    205  1.3  deraadt 
    206  1.3  deraadt #ifdef SKEY
    207  1.3  deraadt /*
    208  1.3  deraadt  * We can't use libskey's skey_authenticate() since it
    209  1.3  deraadt  * handles signals in a way that's inappropriate
    210  1.3  deraadt  * for our needs. Instead we roll our own.
    211  1.3  deraadt  */
    212  1.3  deraadt int
    213  1.3  deraadt skey_auth(char *user)
    214  1.3  deraadt {
    215  1.3  deraadt 	char s[128], *ask, *skey_keyinfo __P((char *name));
    216  1.3  deraadt 	int ret = 0;
    217  1.3  deraadt 
    218  1.3  deraadt 	if (!skey_haskey(user) && (ask = skey_keyinfo(user))) {
    219  1.9       pk 		printf("\n[%s]\nResponse: ", ask);
    220  1.3  deraadt 		if (!fgets(s, sizeof(s), stdin) || *s == '\n')
    221  1.3  deraadt 			clearerr(stdin);
    222  1.3  deraadt 		else {
    223  1.3  deraadt 			s[strlen(s) - 1] = '\0';
    224  1.3  deraadt 			if (skey_passcheck(user, s) != -1)
    225  1.3  deraadt 				ret = 1;
    226  1.3  deraadt 		}
    227  1.3  deraadt 	} else
    228  1.3  deraadt 		printf("Sorry, you have no s/key.\n");
    229  1.3  deraadt 	return ret;
    230  1.3  deraadt }
    231  1.3  deraadt #endif
    232  1.1      cgd 
    233  1.1      cgd void
    234  1.1      cgd hi()
    235  1.1      cgd {
    236  1.1      cgd 	struct timeval timval;
    237  1.1      cgd 
    238  1.1      cgd 	if (!gettimeofday(&timval, (struct timezone *)NULL))
    239  1.1      cgd (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
    240  1.1      cgd 	    (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
    241  1.1      cgd }
    242  1.1      cgd 
    243  1.1      cgd void
    244  1.1      cgd quit()
    245  1.1      cgd {
    246  1.1      cgd 	(void)putchar('\n');
    247  1.5  mycroft 	(void)tcsetattr(0, TCSADRAIN, &tty);
    248  1.1      cgd 	exit(0);
    249  1.1      cgd }
    250  1.1      cgd 
    251  1.1      cgd void
    252  1.1      cgd bye()
    253  1.1      cgd {
    254  1.5  mycroft 	(void)tcsetattr(0, TCSADRAIN, &tty);
    255  1.1      cgd 	(void)printf("lock: timeout\n");
    256  1.1      cgd 	exit(1);
    257  1.1      cgd }
    258