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