121565Sdist /* 233161Sbostic * Copyright (c) 1980, 1987 Regents of the University of California. 333161Sbostic * All rights reserved. 433161Sbostic * 533161Sbostic * Redistribution and use in source and binary forms are permitted 6*34911Sbostic * provided that the above copyright notice and this paragraph are 7*34911Sbostic * duplicated in all such forms and that any documentation, 8*34911Sbostic * advertising materials, and other materials related to such 9*34911Sbostic * distribution and use acknowledge that the software was developed 10*34911Sbostic * by the University of California, Berkeley. The name of the 11*34911Sbostic * University may not be used to endorse or promote products derived 12*34911Sbostic * from this software without specific prior written permission. 13*34911Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34911Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34911Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621565Sdist */ 1721565Sdist 1812989Ssam #ifndef lint 1921565Sdist char copyright[] = 2033161Sbostic "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\ 2121565Sdist All rights reserved.\n"; 2233161Sbostic #endif /* not lint */ 2312989Ssam 2421565Sdist #ifndef lint 25*34911Sbostic static char sccsid[] = "@(#)lock.c 5.6 (Berkeley) 06/29/88"; 2633161Sbostic #endif /* not lint */ 2721565Sdist 2812989Ssam /* 2930956Sbostic * Lock a terminal up until the given key is entered, until the root 3030956Sbostic * password is entered, or the given interval times out. 3116239Sralph * 3216239Sralph * Timeout interval is by default TIMEOUT, it can be changed with 3316239Sralph * an argument of the form -time where time is in minutes 3412989Ssam */ 3516239Sralph 3630652Sbostic #include <sys/param.h> 375748Sroot #include <sys/stat.h> 3816239Sralph #include <sys/time.h> 3930652Sbostic #include <sys/signal.h> 4030652Sbostic #include <pwd.h> 411041Sbill #include <sgtty.h> 4230652Sbostic #include <stdio.h> 4332615Sbostic #include <ctype.h> 441041Sbill 4530956Sbostic #define TIMEOUT 15 4630956Sbostic #define YES 1 4730956Sbostic #define NO 0 481041Sbill 4930652Sbostic int quit(), bye(), hi(); 5016239Sralph 5130652Sbostic struct timeval timeout; 5230652Sbostic struct timeval zerotime; 5316239Sralph struct sgttyb tty, ntty; 5430956Sbostic long nexttime; /* keep the timeout time */ 5516239Sralph 5630956Sbostic /*ARGSUSED*/ 5730956Sbostic main(argc, argv) 5830652Sbostic int argc; 5930652Sbostic char **argv; 601041Sbill { 6130956Sbostic struct passwd *root_pwd, *my_pwd; 6216239Sralph struct timeval timval; 6316239Sralph struct itimerval ntimer, otimer; 6416239Sralph struct tm *timp; 6530956Sbostic int sectimeout = TIMEOUT, 6630956Sbostic use_mine; 6730652Sbostic char *ttynam, *ap, *tzn, 6830956Sbostic hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ], 6930652Sbostic *crypt(), *index(), *ttyname(); 701041Sbill 7130956Sbostic use_mine = NO; 7230956Sbostic for (++argv; *argv; ++argv) { 7330956Sbostic if (argv[0][0] != '-') 7430956Sbostic usage(); 7530956Sbostic if (argv[0][1] == 'p') 7630956Sbostic use_mine = YES; 7732615Sbostic else if (!isdigit(argv[0][1])) { 7832615Sbostic fprintf(stderr, "lock: illegal option -- %c\n", argv[0][1]); 7932615Sbostic usage(); 8032615Sbostic } 8130956Sbostic else if ((sectimeout = atoi(*argv + 1)) <= 0) 8230956Sbostic usage(); 8316239Sralph } 8416239Sralph timeout.tv_sec = sectimeout * 60; 8516239Sralph 8616239Sralph /* get information for header */ 8712989Ssam if (ioctl(0, TIOCGETP, &tty)) 881041Sbill exit(1); 8916239Sralph gethostname(hostname, sizeof(hostname)); 9030652Sbostic if (!(ttynam = ttyname(0))) { 9130652Sbostic puts("lock: not a terminal?"); 9230956Sbostic exit(1); 9316239Sralph } 9430652Sbostic if (gettimeofday(&timval, (struct timezone *)NULL)) { 9530652Sbostic perror("gettimeofday"); 9630956Sbostic exit(1); 9730652Sbostic } 9816239Sralph nexttime = timval.tv_sec + (sectimeout * 60); 9916239Sralph timp = localtime(&timval.tv_sec); 10016239Sralph ap = asctime(timp); 10130652Sbostic tzn = timp->tm_zone; 10216239Sralph 10330652Sbostic (void)signal(SIGINT, quit); 10430652Sbostic (void)signal(SIGQUIT, quit); 1051041Sbill ntty = tty; ntty.sg_flags &= ~ECHO; 10630652Sbostic (void)ioctl(0, TIOCSETP, &ntty); 10730652Sbostic 10830956Sbostic if (!use_mine) { 10930956Sbostic /* get key and check again */ 11030956Sbostic fputs("Key: ", stdout); 11130956Sbostic if (!gets(s, sizeof(s))) 11230956Sbostic quit(); 11330956Sbostic fputs("\nAgain: ", stdout); 11430956Sbostic /* 11530956Sbostic * Don't need EOF test here, if we get EOF, then s1 != s 11630956Sbostic * and the right things will happen. 11730956Sbostic */ 11830956Sbostic (void)gets(s1, sizeof(s1)); 11930956Sbostic putchar('\n'); 12030956Sbostic if (strcmp(s1, s)) { 12130956Sbostic puts("\07lock: passwords didn't match."); 12230956Sbostic ioctl(0, TIOCSETP, &tty); 12330956Sbostic exit(1); 12430956Sbostic } 12530956Sbostic s[0] = NULL; 1261041Sbill } 12716239Sralph 12830956Sbostic /* set signal handlers */ 12930652Sbostic (void)signal(SIGINT, hi); 13030652Sbostic (void)signal(SIGQUIT, hi); 13130652Sbostic (void)signal(SIGTSTP, hi); 13230652Sbostic (void)signal(SIGALRM, bye); 13330652Sbostic 13416239Sralph ntimer.it_interval = zerotime; 13516239Sralph ntimer.it_value = timeout; 13616239Sralph setitimer(ITIMER_REAL, &ntimer, &otimer); 13716239Sralph 13830956Sbostic /* header info */ 13930652Sbostic printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s", 14030956Sbostic ttynam, hostname, sectimeout, ap, tzn, ap + 19); 14116239Sralph 14216239Sralph /* wait */ 14330956Sbostic root_pwd = getpwuid(0); 14430956Sbostic if (use_mine) 14530956Sbostic my_pwd = getpwuid(getuid()); 14630956Sbostic for (;;) { 14730956Sbostic fputs("Key: ", stdout); 14830956Sbostic if (!gets(s, sizeof(s))) { 14916599Skre clearerr(stdin); 15016599Skre hi(); 15116599Skre continue; 15216599Skre } 15330956Sbostic if (use_mine) { 15430956Sbostic if (!my_pwd || !*my_pwd->pw_passwd || !strcmp(my_pwd->pw_passwd, crypt(s, my_pwd->pw_passwd))) 15530956Sbostic break; 15630956Sbostic } 15730956Sbostic else if (!strcmp(s1, s)) 1581041Sbill break; 15930956Sbostic if (!root_pwd || !*root_pwd->pw_passwd || !strcmp(root_pwd->pw_passwd, crypt(s, root_pwd->pw_passwd))) 16030956Sbostic break; 16130652Sbostic puts("\07"); 16212989Ssam if (ioctl(0, TIOCGETP, &ntty)) 1631041Sbill exit(1); 1641041Sbill } 16516239Sralph putchar('\n'); 16630652Sbostic quit(); 1671041Sbill } 16816239Sralph 16930652Sbostic static 17030652Sbostic hi() 17130652Sbostic { 17230652Sbostic struct timeval timval; 17330652Sbostic 17430652Sbostic if (!gettimeofday(&timval, (struct timezone *)NULL)) 17530956Sbostic printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n", 17630956Sbostic (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60); 17730652Sbostic } 17830652Sbostic 17930652Sbostic static 18016239Sralph quit() 18116239Sralph { 18230652Sbostic (void)ioctl(0, TIOCSETP, &tty); 18330956Sbostic exit(0); 18416239Sralph } 18516239Sralph 18630652Sbostic static 18716239Sralph bye() 18816239Sralph { 18930652Sbostic (void)ioctl(0, TIOCSETP, &tty); 19030652Sbostic puts("lock: timeout"); 19130956Sbostic exit(1); 19216239Sralph } 19330956Sbostic 19430956Sbostic static 19530956Sbostic usage() 19630956Sbostic { 19732615Sbostic fputs("usage: lock [-p] [-timeout]\n", stderr); 19830956Sbostic exit(1); 19930956Sbostic } 200