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

unix.superglobalmegacorp.com

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