[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.19

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