112989Ssam #ifndef lint 2*16599Skre static char *sccsid = "@(#)lock.c 4.5 (Berkeley) 06/22/84"; 312989Ssam #endif 412989Ssam 512989Ssam /* 616239Sralph * Lock a terminal up until the given key is entered, 716239Sralph * or until the root password is entered, 816239Sralph * or the given interval times out. 916239Sralph * 1016239Sralph * Timeout interval is by default TIMEOUT, it can be changed with 1116239Sralph * an argument of the form -time where time is in minutes 1212989Ssam */ 1316239Sralph 1416239Sralph #include <pwd.h> 151041Sbill #include <stdio.h> 161041Sbill #include <sys/types.h> 175748Sroot #include <sys/stat.h> 1816239Sralph #include <sys/time.h> 191041Sbill #include <signal.h> 201041Sbill #include <sgtty.h> 211041Sbill 2216239Sralph #define TIMEOUT 15 231041Sbill 2416239Sralph struct passwd *pwd; 2516239Sralph char *crypt(); 2616239Sralph char *getpass(); 2716239Sralph char *index(); 2816239Sralph char *ttyname(); 2916239Sralph char *timezone(); 3016239Sralph char *asctime(); 3116239Sralph struct tm *localtime(); 3216239Sralph 3316239Sralph int quit(); 3416239Sralph int bye(); 3516239Sralph int hi(); 3616239Sralph 3716239Sralph struct timeval timeout = {0, 0}; 3816239Sralph struct timeval zerotime = {0, 0}; 3916239Sralph struct sgttyb tty, ntty; 4016239Sralph long nexttime; /* keep the timeout time */ 4116239Sralph 421041Sbill main(argc, argv) 4316239Sralph int argc; 441041Sbill char **argv; 451041Sbill { 461041Sbill register int t; 4716239Sralph char *ttynam; 4816239Sralph char *ap; 4916239Sralph int sectimeout = TIMEOUT; 5016239Sralph char s[BUFSIZ], s1[BUFSIZ]; 5116239Sralph char hostname[32]; 5216239Sralph char *tzn; 5316239Sralph struct timeval timval; 5416239Sralph struct itimerval ntimer, otimer; 5516239Sralph struct timezone timzone; 5616239Sralph struct tm *timp; 5716239Sralph struct stat statb; 581041Sbill 5916239Sralph /* process arguments */ 6016239Sralph 6116239Sralph if (argc > 1){ 6216239Sralph if (argv[1][0] != '-') 6316239Sralph goto usage; 6416239Sralph if (sscanf(&(argv[1][1]), "%d", §imeout) != 1) 6516239Sralph goto usage; 6616239Sralph } 6716239Sralph timeout.tv_sec = sectimeout * 60; 6816239Sralph 6916239Sralph /* get information for header */ 7016239Sralph 7112989Ssam if (ioctl(0, TIOCGETP, &tty)) 721041Sbill exit(1); 7316239Sralph pwd = getpwuid(0); 7416239Sralph gethostname(hostname, sizeof(hostname)); 7516239Sralph if (!(ttynam = ttyname(0))){ 7616239Sralph printf("lock: not a terminal?"); 7716239Sralph exit (1); 7816239Sralph } 7916239Sralph gettimeofday(&timval, &timzone); 8016239Sralph nexttime = timval.tv_sec + (sectimeout * 60); 8116239Sralph timp = localtime(&timval.tv_sec); 8216239Sralph ap = asctime(timp); 8316239Sralph tzn = timezone(timzone.tz_minuteswest, timp->tm_isdst); 8416239Sralph 8516239Sralph /* get key and check again */ 8616239Sralph 8716239Sralph signal(SIGINT, quit); 8816239Sralph signal(SIGQUIT, quit); 891041Sbill ntty = tty; ntty.sg_flags &= ~ECHO; 9012989Ssam ioctl(0, TIOCSETN, &ntty); 911041Sbill printf("Key: "); 92*16599Skre if (fgets(s, sizeof s, stdin) == NULL) { 93*16599Skre putchar('\n'); 94*16599Skre quit(); 95*16599Skre } 961041Sbill printf("\nAgain: "); 97*16599Skre /* 98*16599Skre * Don't need EOF test here, if we get EOF, then s1 != s 99*16599Skre * and the right things will happen. 100*16599Skre */ 101*16599Skre (void) fgets(s1, sizeof s1, stdin); 1021041Sbill putchar('\n'); 1031041Sbill if (strcmp(s1, s)) { 1041041Sbill putchar(07); 1051041Sbill stty(0, &tty); 1061041Sbill exit(1); 1071041Sbill } 1081041Sbill s[0] = 0; 10916239Sralph 11016239Sralph /* Set signal handlers */ 11116239Sralph 11216239Sralph signal(SIGINT, hi); 11316239Sralph signal(SIGQUIT, hi); 11416239Sralph signal(SIGTSTP, hi); 11516239Sralph signal(SIGALRM, bye); 11616239Sralph ntimer.it_interval = zerotime; 11716239Sralph ntimer.it_value = timeout; 11816239Sralph setitimer(ITIMER_REAL, &ntimer, &otimer); 11916239Sralph 12016239Sralph /* Header info */ 12116239Sralph 12216239Sralph printf ("lock: %s on %s. timeout in %d minutes\n", 12316239Sralph ttynam, hostname, sectimeout); 12416239Sralph printf("time now is %.20s", ap); 12516239Sralph if (tzn) 12616239Sralph printf("%s", tzn); 12716239Sralph printf("%s", ap+19); 12816239Sralph 12916239Sralph /* wait */ 13016239Sralph 1311041Sbill for (;;) { 13216239Sralph printf("Key: "); 133*16599Skre if (fgets(s, sizeof s, stdin) == NULL) { 134*16599Skre clearerr(stdin); 135*16599Skre hi(); 136*16599Skre continue; 137*16599Skre } 1381041Sbill if (strcmp(s1, s) == 0) 1391041Sbill break; 14016239Sralph if (pwd == (struct passwd *) 0 || pwd->pw_passwd[0] == '\0') 1411041Sbill break; 14216239Sralph ap = index(s, '\n'); 14316239Sralph if (ap != NULL) 14416239Sralph *ap = '\0'; 14516239Sralph if (strcmp(pwd->pw_passwd, crypt(s, pwd->pw_passwd)) == 0) 14616239Sralph break; 14716239Sralph printf("\07\n"); 14812989Ssam if (ioctl(0, TIOCGETP, &ntty)) 1491041Sbill exit(1); 1501041Sbill } 15112989Ssam ioctl(0, TIOCSETN, &tty); 15216239Sralph putchar('\n'); 15316239Sralph exit (0); 15416239Sralph usage: 15516239Sralph printf("Usage: lock [-timeout]\n"); 15616239Sralph exit (1); 1571041Sbill } 15816239Sralph 15916239Sralph /* 16016239Sralph * get out of here 16116239Sralph */ 16216239Sralph 16316239Sralph quit() 16416239Sralph { 16516239Sralph ioctl(0, TIOCSETN, &tty); 16616239Sralph exit (0); 16716239Sralph } 16816239Sralph 16916239Sralph bye() 17016239Sralph { 17116239Sralph ioctl(0, TIOCSETN, &tty); 17216239Sralph printf("lock: timeout\n"); 17316239Sralph exit (1); 17416239Sralph } 17516239Sralph 17616239Sralph /* 17716239Sralph * tell the user we are waiting 17816239Sralph */ 17916239Sralph 18016239Sralph hi() 18116239Sralph { 18216239Sralph long curtime; 18316239Sralph struct timeval timval; 18416239Sralph struct timezone timzone; 18516239Sralph 18616239Sralph gettimeofday(&timval, &timzone); 18716239Sralph curtime = timval.tv_sec; 18816239Sralph printf("lock: type in the unlock key. timeout in %d minutes\n", 18916239Sralph (nexttime-curtime)/60); 19016239Sralph } 191