Annotation of 43BSDTahoe/ucb/lock.c, revision 1.1

1.1     ! root        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: #ifndef lint
        !            19: char copyright[] =
        !            20: "@(#) Copyright (c) 1980, 1987 Regents of the University of California.\n\
        !            21:  All rights reserved.\n";
        !            22: #endif /* not lint */
        !            23: 
        !            24: #ifndef lint
        !            25: static char sccsid[] = "@(#)lock.c     5.6 (Berkeley) 6/29/88";
        !            26: #endif /* not lint */
        !            27: 
        !            28: /*
        !            29:  * Lock a terminal up until the given key is entered, until the root
        !            30:  * password is entered, or the given interval times out.
        !            31:  *
        !            32:  * Timeout interval is by default TIMEOUT, it can be changed with
        !            33:  * an argument of the form -time where time is in minutes
        !            34:  */
        !            35: 
        !            36: #include <sys/param.h>
        !            37: #include <sys/stat.h>
        !            38: #include <sys/time.h>
        !            39: #include <sys/signal.h>
        !            40: #include <pwd.h>
        !            41: #include <sgtty.h>
        !            42: #include <stdio.h>
        !            43: #include <ctype.h>
        !            44: 
        !            45: #define        TIMEOUT 15
        !            46: #define        YES     1
        !            47: #define        NO      0
        !            48: 
        !            49: int    quit(), bye(), hi();
        !            50: 
        !            51: struct timeval timeout;
        !            52: struct timeval zerotime;
        !            53: struct sgttyb  tty, ntty;
        !            54: long   nexttime;                       /* keep the timeout time */
        !            55: 
        !            56: /*ARGSUSED*/
        !            57: main(argc, argv)
        !            58:        int     argc;
        !            59:        char    **argv;
        !            60: {
        !            61:        struct passwd   *root_pwd, *my_pwd;
        !            62:        struct timeval  timval;
        !            63:        struct itimerval        ntimer, otimer;
        !            64:        struct tm       *timp;
        !            65:        int     sectimeout = TIMEOUT,
        !            66:                use_mine;
        !            67:        char    *ttynam, *ap, *tzn,
        !            68:                hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ],
        !            69:                *crypt(), *index(), *ttyname();
        !            70: 
        !            71:        use_mine = NO;
        !            72:        for (++argv; *argv; ++argv) {
        !            73:                if (argv[0][0] != '-')
        !            74:                        usage();
        !            75:                if (argv[0][1] == 'p')
        !            76:                        use_mine = YES;
        !            77:                else if (!isdigit(argv[0][1])) {
        !            78:                        fprintf(stderr, "lock: illegal option -- %c\n", argv[0][1]);
        !            79:                        usage();
        !            80:                }
        !            81:                else if ((sectimeout = atoi(*argv + 1)) <= 0)
        !            82:                        usage();
        !            83:        }
        !            84:        timeout.tv_sec = sectimeout * 60;
        !            85: 
        !            86:        /* get information for header */
        !            87:        if (ioctl(0, TIOCGETP, &tty))
        !            88:                exit(1);
        !            89:        gethostname(hostname, sizeof(hostname));
        !            90:        if (!(ttynam = ttyname(0))) {
        !            91:                puts("lock: not a terminal?");
        !            92:                exit(1);
        !            93:        }
        !            94:        if (gettimeofday(&timval, (struct timezone *)NULL)) {
        !            95:                perror("gettimeofday");
        !            96:                exit(1);
        !            97:        }
        !            98:        nexttime = timval.tv_sec + (sectimeout * 60);
        !            99:        timp = localtime(&timval.tv_sec);
        !           100:        ap = asctime(timp);
        !           101:        tzn = timp->tm_zone;
        !           102: 
        !           103:        (void)signal(SIGINT, quit);
        !           104:        (void)signal(SIGQUIT, quit);
        !           105:        ntty = tty; ntty.sg_flags &= ~ECHO;
        !           106:        (void)ioctl(0, TIOCSETP, &ntty);
        !           107: 
        !           108:        if (!use_mine) {
        !           109:                /* get key and check again */
        !           110:                fputs("Key: ", stdout);
        !           111:                if (!gets(s, sizeof(s)))
        !           112:                        quit();
        !           113:                fputs("\nAgain: ", stdout);
        !           114:                /*
        !           115:                 * Don't need EOF test here, if we get EOF, then s1 != s
        !           116:                 * and the right things will happen.
        !           117:                 */
        !           118:                (void)gets(s1, sizeof(s1));
        !           119:                putchar('\n');
        !           120:                if (strcmp(s1, s)) {
        !           121:                        puts("\07lock: passwords didn't match.");
        !           122:                        ioctl(0, TIOCSETP, &tty);
        !           123:                        exit(1);
        !           124:                }
        !           125:                s[0] = NULL;
        !           126:        }
        !           127: 
        !           128:        /* set signal handlers */
        !           129:        (void)signal(SIGINT, hi);
        !           130:        (void)signal(SIGQUIT, hi);
        !           131:        (void)signal(SIGTSTP, hi);
        !           132:        (void)signal(SIGALRM, bye);
        !           133: 
        !           134:        ntimer.it_interval = zerotime;
        !           135:        ntimer.it_value = timeout;
        !           136:        setitimer(ITIMER_REAL, &ntimer, &otimer);
        !           137: 
        !           138:        /* header info */
        !           139:        printf ("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
        !           140:                ttynam, hostname, sectimeout, ap, tzn, ap + 19);
        !           141: 
        !           142:        /* wait */
        !           143:        root_pwd = getpwuid(0);
        !           144:        if (use_mine)
        !           145:                my_pwd = getpwuid(getuid());
        !           146:        for (;;) {
        !           147:                fputs("Key: ", stdout);
        !           148:                if (!gets(s, sizeof(s))) {
        !           149:                        clearerr(stdin);
        !           150:                        hi();
        !           151:                        continue;
        !           152:                }
        !           153:                if (use_mine) {
        !           154:                        if (!my_pwd || !*my_pwd->pw_passwd || !strcmp(my_pwd->pw_passwd, crypt(s, my_pwd->pw_passwd)))
        !           155:                                break;
        !           156:                }
        !           157:                else if (!strcmp(s1, s))
        !           158:                        break;
        !           159:                if (!root_pwd || !*root_pwd->pw_passwd || !strcmp(root_pwd->pw_passwd, crypt(s, root_pwd->pw_passwd)))
        !           160:                        break;
        !           161:                puts("\07");
        !           162:                if (ioctl(0, TIOCGETP, &ntty))
        !           163:                        exit(1);
        !           164:        }
        !           165:        putchar('\n');
        !           166:        quit();
        !           167: }
        !           168: 
        !           169: static
        !           170: hi()
        !           171: {
        !           172:        struct timeval  timval;
        !           173: 
        !           174:        if (!gettimeofday(&timval, (struct timezone *)NULL))
        !           175:            printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
        !           176:            (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
        !           177: }
        !           178: 
        !           179: static
        !           180: quit()
        !           181: {
        !           182:        (void)ioctl(0, TIOCSETP, &tty);
        !           183:        exit(0);
        !           184: }
        !           185: 
        !           186: static
        !           187: bye()
        !           188: {
        !           189:        (void)ioctl(0, TIOCSETP, &tty);
        !           190:        puts("lock: timeout");
        !           191:        exit(1);
        !           192: }
        !           193: 
        !           194: static
        !           195: usage()
        !           196: {
        !           197:        fputs("usage: lock [-p] [-timeout]\n", stderr);
        !           198:        exit(1);
        !           199: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.