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

Annotation of src/usr.bin/skeyaudit/skeyaudit.c, Revision 1.10

1.10    ! pjanzen     1: /*     $OpenBSD: skeyaudit.c,v 1.9 2000/08/20 18:42:40 millert Exp $   */
1.2       millert     2:
                      3: /*
1.9       millert     4:  * Copyright (c) 1997, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
1.2       millert     5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.8       millert    15:  * 3. The name of the author may not be used to endorse or promote products
1.2       millert    16:  *    derived from this software without specific prior written permission.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     19:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     20:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
                     21:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     22:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     23:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     24:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     25:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     26:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     27:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     28:  */
                     29:
1.9       millert    30: #include <sys/param.h>
                     31: #include <sys/wait.h>
                     32:
1.1       millert    33: #include <err.h>
                     34: #include <errno.h>
                     35: #include <limits.h>
1.9       millert    36: #include <login_cap.h>
1.1       millert    37: #include <paths.h>
                     38: #include <pwd.h>
                     39: #include <stdio.h>
                     40: #include <stdlib.h>
                     41: #include <string.h>
                     42: #include <unistd.h>
                     43: #include <skey.h>
                     44:
                     45: extern char *__progname;
                     46:
1.9       millert    47: void notify __P((struct passwd *, int, int));
                     48: FILE *runsendmail __P((struct passwd *, int *));
1.1       millert    49: void usage __P((void));
                     50:
                     51: int
                     52: main(argc, argv)
                     53:        int argc;
                     54:        char **argv;
                     55: {
                     56:        struct passwd *pw;
                     57:        struct skey key;
1.6       millert    58:        int ch, errs = 0, left = 0, aflag = 0, iflag = 0, limit = 12;
                     59:        char *name;
                     60:
                     61:        if (geteuid() != 0)
                     62:                errx(1, "must be setuid root");
1.1       millert    63:
1.6       millert    64:        while ((ch = getopt(argc, argv, "ail:")) != -1)
1.1       millert    65:                switch(ch) {
1.6       millert    66:                case 'a':
                     67:                        aflag = 1;
                     68:                        if (getuid() != 0)
                     69:                                errx(1, "only root may use the -a flag");
                     70:                        break;
1.1       millert    71:                case 'i':
                     72:                        iflag = 1;
                     73:                        break;
                     74:                case 'l':
                     75:                        errno = 0;
                     76:                        if ((limit = (int)strtol(optarg, NULL, 10)) == 0)
                     77:                                errno = ERANGE;
                     78:                        if (errno) {
                     79:                                warn("key limit");
                     80:                                usage();
                     81:                        }
                     82:                        break;
                     83:                default:
                     84:                        usage();
                     85:        }
                     86:
                     87:        if (argc - optind > 0)
                     88:                usage();
                     89:
1.6       millert    90:        /* Need key.keyfile zero'd at the very least */
                     91:        (void)memset(&key, 0, sizeof(key));
                     92:
                     93:        if (aflag) {
                     94:                while ((ch = skeygetnext(&key)) == 0) {
1.1       millert    95:                        left = key.n - 1;
1.6       millert    96:                        if ((pw = getpwnam(key.logname)) == NULL)
                     97:                                continue;
                     98:                        if (left >= limit)
                     99:                                continue;
1.9       millert   100:                        notify(pw, left, iflag);
1.6       millert   101:                }
                    102:                if (ch == -1)
                    103:                        errx(-1, "cannot open %s", _PATH_SKEYKEYS);
                    104:                else
                    105:                        (void)fclose(key.keyfile);
                    106:        } else {
                    107:                if ((pw = getpwuid(getuid())) == NULL)
                    108:                        errx(1, "no passwd entry for uid %u", getuid());
                    109:                if ((name = strdup(pw->pw_name)) == NULL)
                    110:                        err(1, "cannot allocate memory");
                    111:                sevenbit(name);
                    112:
                    113:                errs = skeylookup(&key, name);
                    114:                switch (errs) {
                    115:                        case 0:         /* Success! */
                    116:                                left = key.n - 1;
                    117:                                break;
                    118:                        case -1:        /* File error */
                    119:                                errx(errs, "cannot open %s", _PATH_SKEYKEYS);
                    120:                                break;
                    121:                        case 1:         /* Unknown user */
                    122:                                warnx("%s is not listed in %s", name,
                    123:                                    _PATH_SKEYKEYS);
                    124:                }
                    125:                (void)fclose(key.keyfile);
                    126:
                    127:                if (!errs && left < limit)
1.9       millert   128:                        notify(pw, left, iflag);
1.1       millert   129:        }
1.6       millert   130:
                    131:        exit(errs);
                    132: }
1.1       millert   133:
1.6       millert   134: void
1.9       millert   135: notify(pw, seq, interactive)
                    136:        struct passwd *pw;
1.6       millert   137:        int seq;
                    138:        int interactive;
                    139: {
                    140:        static char hostname[MAXHOSTNAMELEN];
                    141:        int pid;
                    142:        FILE *out;
1.1       millert   143:
1.6       millert   144:        /* Only set this once */
                    145:        if (hostname[0] == '\0' && gethostname(hostname, sizeof(hostname)) == -1)
1.1       millert   146:                strcpy(hostname, "unknown");
                    147:
1.6       millert   148:        if (interactive)
1.1       millert   149:                out = stdout;
1.6       millert   150:        else
1.9       millert   151:                out = runsendmail(pw, &pid);
1.1       millert   152:
1.6       millert   153:        if (!interactive)
1.1       millert   154:                (void)fprintf(out,
1.9       millert   155:                   "To: %s\nSubject: IMPORTANT action required\n", pw->pw_name);
1.1       millert   156:
1.10    ! pjanzen   157:        if (seq)
        !           158:                (void)fprintf(out,
1.1       millert   159: "\nYou are nearing the end of your current S/Key sequence for account\n\
                    160: %s on system %s.\n\n\
1.10    ! pjanzen   161: Your S/Key sequence number is now %d.  When it reaches zero\n\
        !           162: you will no longer be able to use S/Key to log into the system.\n\n",
1.9       millert   163: pw->pw_name, hostname, seq);
1.10    ! pjanzen   164:        else
        !           165:                (void)fprintf(out,
        !           166: "\nYou are at the end of your current S/Key sequence for account\n\
        !           167: %s on system %s.\n\n\
        !           168: At this point you can no longer use S/Key to log into the system.\n\n",
        !           169: pw->pw_name, hostname);
        !           170:        (void)fprintf(out,
        !           171: "Type \"skeyinit -s\" to reinitialize your sequence number.\n\n");
1.1       millert   172:
1.6       millert   173:        (void)fclose(out);
                    174:        if (!interactive)
                    175:                (void)waitpid(pid, NULL, 0);
1.1       millert   176: }
                    177:
1.6       millert   178: FILE *
1.9       millert   179: runsendmail(pw, pidp)
                    180:        struct passwd *pw;
1.6       millert   181:        int *pidp;
                    182: {
                    183:        FILE *fp;
                    184:        int pfd[2], pid;
                    185:
                    186:        if (pipe(pfd) < 0)
                    187:                return(NULL);
                    188:
                    189:        switch (pid = fork()) {
                    190:        case -1:                        /* fork(2) failed */
                    191:                (void)close(pfd[0]);
                    192:                (void)close(pfd[1]);
                    193:                return(NULL);
                    194:        case 0:                         /* In child */
                    195:                (void)close(pfd[1]);
                    196:                (void)dup2(pfd[0], STDIN_FILENO);
                    197:                (void)close(pfd[0]);
                    198:
                    199:                /* Run sendmail as target user not root */
1.9       millert   200:                if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
                    201:                        warn("cannot set user context");
                    202:                        _exit(127);
                    203:                }
1.6       millert   204:
                    205:                execl(_PATH_SENDMAIL, "sendmail", "-t", NULL);
                    206:                warn("cannot run \"%s -t\"", _PATH_SENDMAIL);
                    207:                _exit(127);
                    208:        }
                    209:
                    210:        /* In parent */
                    211:        *pidp = pid;
                    212:        fp = fdopen(pfd[1], "w");
                    213:        (void)close(pfd[0]);
                    214:
                    215:        return(fp);
                    216: }
1.1       millert   217: void
                    218: usage()
                    219: {
                    220:        (void)fprintf(stderr, "Usage: %s [-i] [-l limit]\n",
                    221:            __progname);
                    222:        exit(1);
                    223: }