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