1: /*
   2:  * Copyright (c) 1980, 1987 Regents of the University of California.
   3:  * All rights reserved.
   4:  *
   5:  * Redistribution and use in source and binary forms are permitted
   6:  * provided that the above copyright notice and this paragraph are
   7:  * duplicated in all such forms and that any documentation,
   8:  * advertising materials, and other materials related to such
   9:  * distribution and use acknowledge that the software was developed
  10:  * by the University of California, Berkeley.  The name of the
  11:  * University may not be used to endorse or promote products derived
  12:  * from this software without specific prior written permission.
  13:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16:  */
  17: 
  18: #if defined(DOSCCS) && !defined(lint)
  19: char copyright[] =
  20: "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
  21:  All rights reserved.\n";
  22: 
  23: static char sccsid[] = "@(#)lock.c	5.10.1 (2.11BSD GTE) 12/31/93";
  24: #endif
  25: 
  26: /*
  27:  * Lock a terminal up until the given key is entered, until the root
  28:  * password is entered, or the given interval times out.
  29:  *
  30:  * Timeout interval is by default TIMEOUT, it can be changed with
  31:  * an argument of the form -time where time is in minutes
  32:  */
  33: 
  34: #include <sys/param.h>
  35: #include <sys/stat.h>
  36: #include <sys/time.h>
  37: #include <sys/signal.h>
  38: #include <sgtty.h>
  39: #include <pwd.h>
  40: #include <stdio.h>
  41: #include <ctype.h>
  42: #include <strings.h>
  43: 
  44: #define TIMEOUT 15
  45: 
  46: int quit(), bye(), hi();
  47: 
  48: struct timeval  timeout;
  49: struct timeval  zerotime;
  50: struct sgttyb   tty, ntty;
  51: long    nexttime;           /* keep the timeout time */
  52: 
  53: /*ARGSUSED*/
  54: main(argc, argv)
  55:     int argc;
  56:     char **argv;
  57: {
  58:     extern char *optarg;
  59:     extern int errno, optind;
  60:     struct passwd *pw;
  61:     struct timeval timval;
  62:     struct itimerval ntimer, otimer;
  63:     struct tm *timp;
  64:     int ch, sectimeout, usemine;
  65:     char *ap, *mypw, *ttynam, *tzn;
  66:     char hostname[MAXHOSTNAMELEN], s[256], s1[256];
  67:     char *crypt(), *ttyname();
  68: 
  69:     sectimeout = TIMEOUT;
  70:     mypw = NULL;
  71:     usemine = 0;
  72:     while ((ch = getopt(argc, argv, "pt:")) != EOF)
  73:         switch((char)ch) {
  74:         case 't':
  75:             if ((sectimeout = atoi(optarg)) <= 0)
  76:                 exit(1);
  77:             break;
  78:         case 'p':
  79:             usemine = 1;
  80:             if (!(pw = getpwuid(getuid()))) {
  81:                 fprintf(stderr, "lock: unknown uid %d.\n",
  82:                     getuid());
  83:                 exit(1);
  84:             }
  85:             mypw = strdup(pw->pw_passwd);
  86:             break;
  87:         case '?':
  88:         default:
  89:             fprintf(stderr, "usage: lock [-p] [-t timeout]\n");
  90:             exit(1);
  91:     }
  92:     timeout.tv_sec = sectimeout * 60;
  93: 
  94:     setuid(getuid());       /* discard privs */
  95: 
  96:     if (ioctl(0, TIOCGETP, &tty))   /* get information for header */
  97:         exit(1);
  98:     gethostname(hostname, sizeof(hostname));
  99:     if (!(ttynam = ttyname(0))) {
 100:         printf("lock: not a terminal?\n");
 101:         exit(1);
 102:     }
 103:     if (gettimeofday(&timval, (struct timezone *)NULL)) {
 104:         fprintf(stderr, "lock: gettimeofday: %s\n", strerror(errno));
 105:         exit(1);
 106:     }
 107:     nexttime = timval.tv_sec + (sectimeout * 60);
 108:     timp = localtime(&timval.tv_sec);
 109:     ap = asctime(timp);
 110:     tzn = timp->tm_zone;
 111: 
 112:     (void)signal(SIGINT, quit);
 113:     (void)signal(SIGQUIT, quit);
 114:     ntty = tty; ntty.sg_flags &= ~ECHO;
 115:     (void)ioctl(0, TIOCSETP, &ntty);
 116: 
 117:     if (!mypw) {
 118:         /* get key and check again */
 119:         printf("Key: ");
 120:         if (!fgets(s, sizeof(s), stdin) || *s == '\n')
 121:             quit();
 122:         printf("\nAgain: ");
 123:         /*
 124: 		 * Don't need EOF test here, if we get EOF, then s1 != s
 125: 		 * and the right things will happen.
 126: 		 */
 127:         (void)fgets(s1, sizeof(s1), stdin);
 128:         putchar('\n');
 129:         if (strcmp(s1, s)) {
 130:             printf("\07lock: passwords didn't match.\n");
 131:             ioctl(0, TIOCSETP, &tty);
 132:             exit(1);
 133:         }
 134:         s[0] = NULL;
 135:         mypw = s1;
 136:     }
 137: 
 138:     /* set signal handlers */
 139:     (void)signal(SIGINT, hi);
 140:     (void)signal(SIGQUIT, hi);
 141:     (void)signal(SIGTSTP, hi);
 142:     (void)signal(SIGALRM, bye);
 143: 
 144:     ntimer.it_interval = zerotime;
 145:     ntimer.it_value = timeout;
 146:     setitimer(ITIMER_REAL, &ntimer, &otimer);
 147: 
 148:     /* header info */
 149:     printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
 150:         ttynam, hostname, sectimeout, ap, tzn, ap + 19);
 151: 
 152:     for (;;) {
 153:         printf("Key: ");
 154:         if (!fgets(s, sizeof(s), stdin)) {
 155:             clearerr(stdin);
 156:             hi();
 157:             continue;
 158:         }
 159:         if (usemine) {
 160:             s[strlen(s) - 1] = '\0';
 161:             if (!strcmp(mypw, crypt(s, mypw)))
 162:                 break;
 163:         }
 164:         else if (!strcmp(s, s1))
 165:             break;
 166:         printf("\07\n");
 167:         if (ioctl(0, TIOCGETP, &ntty))
 168:             exit(1);
 169:     }
 170:     quit();
 171: }
 172: 
 173: static
 174: hi()
 175: {
 176:     struct timeval timval;
 177: 
 178:     if (!gettimeofday(&timval, (struct timezone *)NULL))
 179:         printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
 180:         (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
 181: }
 182: 
 183: static
 184: quit()
 185: {
 186:     putchar('\n');
 187:     (void)ioctl(0, TIOCSETP, &tty);
 188:     exit(0);
 189: }
 190: 
 191: static
 192: bye()
 193: {
 194:     (void)ioctl(0, TIOCSETP, &tty);
 195:     printf("lock: timeout\n");
 196:     exit(1);
 197: }

Defined functions

bye defined in line 191; used 2 times
hi defined in line 173; used 5 times
main defined in line 54; never used
quit defined in line 183; used 5 times

Defined variables

copyright defined in line 19; never used
nexttime defined in line 51; used 3 times
ntty defined in line 50; used 4 times
sccsid defined in line 23; never used
timeout defined in line 48; used 2 times
tty defined in line 50; used 5 times
zerotime defined in line 49; used 1 times

Defined macros

TIMEOUT defined in line 44; used 1 times
  • in line 69
Last modified: 1994-01-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3595
Valid CSS Valid XHTML 1.0 Strict