121565Sdist /* 221565Sdist * Copyright (c) 1980 Regents of the University of California. 321565Sdist * All rights reserved. The Berkeley software License Agreement 421565Sdist * specifies the terms and conditions for redistribution. 521565Sdist */ 621565Sdist 712989Ssam #ifndef lint 821565Sdist char copyright[] = 921565Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1021565Sdist All rights reserved.\n"; 1121565Sdist #endif not lint 1212989Ssam 1321565Sdist #ifndef lint 14*30652Sbostic static char sccsid[] = "@(#)lock.c 5.2 (Berkeley) 03/18/87"; 1521565Sdist #endif not lint 1621565Sdist 1712989Ssam /* 1816239Sralph * Lock a terminal up until the given key is entered, 1916239Sralph * or until the root password is entered, 2016239Sralph * or the given interval times out. 2116239Sralph * 2216239Sralph * Timeout interval is by default TIMEOUT, it can be changed with 2316239Sralph * an argument of the form -time where time is in minutes 2412989Ssam */ 2516239Sralph 26*30652Sbostic #include <sys/param.h> 275748Sroot #include <sys/stat.h> 2816239Sralph #include <sys/time.h> 29*30652Sbostic #include <sys/signal.h> 30*30652Sbostic #include <pwd.h> 311041Sbill #include <sgtty.h> 32*30652Sbostic #include <stdio.h> 331041Sbill 34*30652Sbostic #define TIMEOUT 15 351041Sbill 36*30652Sbostic int quit(), bye(), hi(); 3716239Sralph 38*30652Sbostic struct timeval timeout; 39*30652Sbostic struct timeval zerotime; 4016239Sralph struct sgttyb tty, ntty; 4116239Sralph long nexttime; /* keep the timeout time */ 4216239Sralph 43*30652Sbostic main(argc,argv) 44*30652Sbostic int argc; 45*30652Sbostic char **argv; 461041Sbill { 47*30652Sbostic struct passwd *pwd; 4816239Sralph struct timeval timval; 4916239Sralph struct itimerval ntimer, otimer; 5016239Sralph struct tm *timp; 51*30652Sbostic int sectimeout = TIMEOUT; 52*30652Sbostic char *ttynam, *ap, *tzn, 53*30652Sbostic hostname[MAXHOSTNAMELEN], 54*30652Sbostic s[BUFSIZ], s1[BUFSIZ], 55*30652Sbostic *crypt(), *index(), *ttyname(); 561041Sbill 57*30652Sbostic /* process argument */ 5816239Sralph 59*30652Sbostic if (argc > 1 && (argv[1][0] != '-' || 60*30652Sbostic (sectimeout = atoi(argv[1] + 1)) <= 0)) { 61*30652Sbostic puts("Usage: lock [-timeout]"); 62*30652Sbostic exit (1); 6316239Sralph } 6416239Sralph timeout.tv_sec = sectimeout * 60; 6516239Sralph 6616239Sralph /* get information for header */ 6716239Sralph 6812989Ssam if (ioctl(0, TIOCGETP, &tty)) 691041Sbill exit(1); 7016239Sralph gethostname(hostname, sizeof(hostname)); 71*30652Sbostic if (!(ttynam = ttyname(0))) { 72*30652Sbostic puts("lock: not a terminal?"); 7316239Sralph exit (1); 7416239Sralph } 75*30652Sbostic if (gettimeofday(&timval, (struct timezone *)NULL)) { 76*30652Sbostic perror("gettimeofday"); 77*30652Sbostic exit (1); 78*30652Sbostic } 7916239Sralph nexttime = timval.tv_sec + (sectimeout * 60); 8016239Sralph timp = localtime(&timval.tv_sec); 8116239Sralph ap = asctime(timp); 82*30652Sbostic tzn = timp->tm_zone; 8316239Sralph 8416239Sralph /* get key and check again */ 8516239Sralph 86*30652Sbostic (void)signal(SIGINT, quit); 87*30652Sbostic (void)signal(SIGQUIT, quit); 881041Sbill ntty = tty; ntty.sg_flags &= ~ECHO; 89*30652Sbostic (void)ioctl(0, TIOCSETP, &ntty); 90*30652Sbostic 91*30652Sbostic fputs("Key: ",stdout); 92*30652Sbostic if (!gets(s,sizeof(s))) 9316599Skre quit(); 94*30652Sbostic fputs("\nAgain: ",stdout); 95*30652Sbostic 9616599Skre /* 9716599Skre * Don't need EOF test here, if we get EOF, then s1 != s 9816599Skre * and the right things will happen. 9916599Skre */ 100*30652Sbostic (void)gets(s1,sizeof(s1)); 1011041Sbill putchar('\n'); 1021041Sbill if (strcmp(s1, s)) { 1031041Sbill putchar(07); 104*30652Sbostic ioctl(0, TIOCSETP, &tty); 1051041Sbill exit(1); 1061041Sbill } 107*30652Sbostic s[0] = NULL; 10816239Sralph 10916239Sralph /* Set signal handlers */ 11016239Sralph 111*30652Sbostic (void)signal(SIGINT, hi); 112*30652Sbostic (void)signal(SIGQUIT, hi); 113*30652Sbostic (void)signal(SIGTSTP, hi); 114*30652Sbostic (void)signal(SIGALRM, bye); 115*30652Sbostic 11616239Sralph ntimer.it_interval = zerotime; 11716239Sralph ntimer.it_value = timeout; 11816239Sralph setitimer(ITIMER_REAL, &ntimer, &otimer); 11916239Sralph 12016239Sralph /* Header info */ 12116239Sralph 122*30652Sbostic printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s", 123*30652Sbostic ttynam,hostname,sectimeout,ap,tzn,ap+19); 12416239Sralph 12516239Sralph /* wait */ 12616239Sralph 127*30652Sbostic for (pwd = getpwuid(0);;) { 128*30652Sbostic fputs("Key: ",stdout); 129*30652Sbostic if (!gets(s,sizeof(s))) { 13016599Skre clearerr(stdin); 13116599Skre hi(); 13216599Skre continue; 13316599Skre } 134*30652Sbostic if (!strcmp(s1,s) || !pwd || !*pwd->pw_passwd || 135*30652Sbostic !strcmp(pwd->pw_passwd,crypt(s,pwd->pw_passwd))) 1361041Sbill break; 137*30652Sbostic puts("\07"); 13812989Ssam if (ioctl(0, TIOCGETP, &ntty)) 1391041Sbill exit(1); 1401041Sbill } 14116239Sralph putchar('\n'); 142*30652Sbostic quit(); 1431041Sbill } 14416239Sralph 14516239Sralph /* 146*30652Sbostic * tell the user we are waiting 147*30652Sbostic */ 148*30652Sbostic 149*30652Sbostic static 150*30652Sbostic hi() 151*30652Sbostic { 152*30652Sbostic struct timeval timval; 153*30652Sbostic 154*30652Sbostic if (!gettimeofday(&timval, (struct timezone *)NULL)) 155*30652Sbostic printf("lock: type in the unlock key. timeout in %d minutes\n", 156*30652Sbostic (nexttime - timval.tv_sec) / 60); 157*30652Sbostic } 158*30652Sbostic 159*30652Sbostic /* 16016239Sralph * get out of here 16116239Sralph */ 162*30652Sbostic static 16316239Sralph quit() 16416239Sralph { 165*30652Sbostic (void)ioctl(0, TIOCSETP, &tty); 16616239Sralph exit (0); 16716239Sralph } 16816239Sralph 169*30652Sbostic static 17016239Sralph bye() 17116239Sralph { 172*30652Sbostic (void)ioctl(0, TIOCSETP, &tty); 173*30652Sbostic puts("lock: timeout"); 17416239Sralph exit (1); 17516239Sralph } 176