[BACK]Return to lock.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / lock

Annotation of src/usr.bin/lock/lock.c, Revision 1.2

1.2     ! deraadt     1: /*     $NetBSD: lock.c,v 1.8 1996/05/07 18:32:31 jtc Exp $     */
1.1       deraadt     2:
                      3: /*
                      4:  * Copyright (c) 1980, 1987, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Bob Toxen.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: static char copyright[] =
                     41: "@(#) Copyright (c) 1980, 1987, 1993\n\
                     42:        The Regents of the University of California.  All rights reserved.\n";
                     43: #endif /* not lint */
                     44:
                     45: #ifndef lint
                     46: #if 0
                     47: static char sccsid[] = "@(#)lock.c     8.1 (Berkeley) 6/6/93";
                     48: #endif
1.2     ! deraadt    49: static char rcsid[] = "$NetBSD: lock.c,v 1.8 1996/05/07 18:32:31 jtc Exp $";
1.1       deraadt    50: #endif /* not lint */
                     51:
                     52: /*
                     53:  * Lock a terminal up until the given key is entered, until the root
                     54:  * password is entered, or the given interval times out.
                     55:  *
                     56:  * Timeout interval is by default TIMEOUT, it can be changed with
                     57:  * an argument of the form -time where time is in minutes
                     58:  */
                     59:
                     60: #include <sys/param.h>
                     61: #include <sys/stat.h>
                     62: #include <sys/time.h>
                     63: #include <signal.h>
                     64:
                     65: #include <ctype.h>
                     66: #include <err.h>
                     67: #include <pwd.h>
                     68: #include <stdio.h>
1.2     ! deraadt    69: #include <stdlib.h>
1.1       deraadt    70: #include <string.h>
                     71: #include <termios.h>
1.2     ! deraadt    72: #include <unistd.h>
1.1       deraadt    73:
                     74: #define        TIMEOUT 15
                     75:
                     76: void quit(), bye(), hi();
                     77:
                     78: struct timeval timeout;
                     79: struct timeval zerotime;
                     80: struct termios tty, ntty;
                     81: long   nexttime;                       /* keep the timeout time */
                     82:
                     83: /*ARGSUSED*/
                     84: main(argc, argv)
                     85:        int argc;
                     86:        char **argv;
                     87: {
                     88:        extern char *optarg;
                     89:        struct passwd *pw;
                     90:        struct timeval timval;
                     91:        struct itimerval ntimer, otimer;
                     92:        struct tm *timp;
                     93:        time_t curtime;
                     94:        int ch, sectimeout, usemine;
                     95:        char *ap, *mypw, *ttynam, *tzn;
                     96:        char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
1.2     ! deraadt    97:        char *crypt();
1.1       deraadt    98:
                     99:        sectimeout = TIMEOUT;
                    100:        mypw = NULL;
                    101:        usemine = 0;
                    102:
                    103:        if (!(pw = getpwuid(getuid())))
                    104:                errx(1, "unknown uid %d.", getuid());
                    105:
                    106:        while ((ch = getopt(argc, argv, "pt:")) != EOF)
                    107:                switch((char)ch) {
                    108:                case 't':
                    109:                        if ((sectimeout = atoi(optarg)) <= 0)
                    110:                                errx(1, "illegal timeout value: %s", optarg);
                    111:                        break;
                    112:                case 'p':
                    113:                        usemine = 1;
                    114:                        mypw = strdup(pw->pw_passwd);
                    115:                        break;
                    116:                case '?':
                    117:                default:
                    118:                        (void)fprintf(stderr,
                    119:                            "usage: lock [-p] [-t timeout]\n");
                    120:                        exit(1);
                    121:        }
                    122:        timeout.tv_sec = sectimeout * 60;
                    123:
                    124:        setuid(getuid());               /* discard privs */
                    125:
                    126:        if (tcgetattr(0, &tty) < 0)     /* get information for header */
                    127:                exit(1);
                    128:        gethostname(hostname, sizeof(hostname));
                    129:        if (!(ttynam = ttyname(0)))
                    130:                errx(1, "not a terminal?");
                    131:        if (gettimeofday(&timval, (struct timezone *)NULL))
                    132:                err(1, "gettimeofday");
                    133:        curtime = timval.tv_sec;
                    134:        nexttime = timval.tv_sec + (sectimeout * 60);
                    135:        timp = localtime(&curtime);
                    136:        ap = asctime(timp);
                    137:        tzn = timp->tm_zone;
                    138:
                    139:        (void)signal(SIGINT, quit);
                    140:        (void)signal(SIGQUIT, quit);
                    141:        ntty = tty; ntty.c_lflag &= ~ECHO;
                    142:        (void)tcsetattr(0, TCSADRAIN, &ntty);
                    143:
                    144:        if (!mypw) {
                    145:                /* get key and check again */
                    146:                (void)printf("Key: ");
                    147:                if (!fgets(s, sizeof(s), stdin) || *s == '\n')
                    148:                        quit();
                    149:                (void)printf("\nAgain: ");
                    150:                /*
                    151:                 * Don't need EOF test here, if we get EOF, then s1 != s
                    152:                 * and the right things will happen.
                    153:                 */
                    154:                (void)fgets(s1, sizeof(s1), stdin);
                    155:                (void)putchar('\n');
                    156:                if (strcmp(s1, s)) {
                    157:                        (void)printf("\alock: passwords didn't match.\n");
                    158:                        (void)tcsetattr(0, TCSADRAIN, &tty);
                    159:                        exit(1);
                    160:                }
                    161:                s[0] = NULL;
                    162:                mypw = s1;
                    163:        }
                    164:
                    165:        /* set signal handlers */
                    166:        (void)signal(SIGINT, hi);
                    167:        (void)signal(SIGQUIT, hi);
                    168:        (void)signal(SIGTSTP, hi);
                    169:        (void)signal(SIGALRM, bye);
                    170:
                    171:        ntimer.it_interval = zerotime;
                    172:        ntimer.it_value = timeout;
                    173:        setitimer(ITIMER_REAL, &ntimer, &otimer);
                    174:
                    175:        /* header info */
                    176: (void)printf("lock: %s on %s. timeout in %d minutes\ntime now is %.20s%s%s",
                    177:            ttynam, hostname, sectimeout, ap, tzn, ap + 19);
                    178:
                    179:        for (;;) {
                    180:                (void)printf("Key: ");
                    181:                if (!fgets(s, sizeof(s), stdin)) {
                    182:                        clearerr(stdin);
                    183:                        hi();
                    184:                        continue;
                    185:                }
                    186:                if (usemine) {
                    187:                        s[strlen(s) - 1] = '\0';
                    188: #ifdef SKEY
                    189:                        if (strcasecmp(s, "s/key") == 0) {
                    190:                                if (skey_auth(pw->pw_name))
                    191:                                        break;
                    192:                        }
                    193: #endif
                    194:                        if (!strcmp(mypw, crypt(s, mypw)))
                    195:                                break;
                    196:                }
                    197:                else if (!strcmp(s, s1))
                    198:                        break;
                    199:                (void)printf("\a\n");
                    200:                if (tcsetattr(0, TCSADRAIN, &ntty) < 0)
                    201:                        exit(1);
                    202:        }
                    203:        quit();
                    204: }
                    205:
                    206: #ifdef SKEY
                    207: /*
                    208:  * We can't use libskey's skey_authenticate() since it
                    209:  * handles signals in a way that's inappropriate
                    210:  * for our needs. Instead we roll our own.
                    211:  */
                    212: int
                    213: skey_auth(char *user)
                    214: {
                    215:        char s[128], *ask, *skey_keyinfo __P((char *name));
                    216:        int ret = 0;
                    217:
                    218:        if (!skey_haskey(user) && (ask = skey_keyinfo(user))) {
                    219:                printf("\n[%s]\nResponse: ", ask);
                    220:                if (!fgets(s, sizeof(s), stdin) || *s == '\n')
                    221:                        clearerr(stdin);
                    222:                else {
                    223:                        s[strlen(s) - 1] = '\0';
                    224:                        if (skey_passcheck(user, s) != -1)
                    225:                                ret = 1;
                    226:                }
                    227:        } else
                    228:                printf("Sorry, you have no s/key.\n");
                    229:        return ret;
                    230: }
                    231: #endif
                    232:
                    233: void
                    234: hi()
                    235: {
                    236:        struct timeval timval;
                    237:
                    238:        if (!gettimeofday(&timval, (struct timezone *)NULL))
                    239: (void)printf("lock: type in the unlock key. timeout in %ld:%ld minutes\n",
                    240:            (nexttime - timval.tv_sec) / 60, (nexttime - timval.tv_sec) % 60);
                    241: }
                    242:
                    243: void
                    244: quit()
                    245: {
                    246:        (void)putchar('\n');
                    247:        (void)tcsetattr(0, TCSADRAIN, &tty);
                    248:        exit(0);
                    249: }
                    250:
                    251: void
                    252: bye()
                    253: {
                    254:        (void)tcsetattr(0, TCSADRAIN, &tty);
                    255:        (void)printf("lock: timeout\n");
                    256:        exit(1);
                    257: }