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

1.24    ! sobrado     1: /*     $OpenBSD: skeyaudit.c,v 1.23 2006/12/11 20:50:54 deraadt Exp $  */
1.2       millert     2:
                      3: /*
1.17      millert     4:  * Copyright (c) 1997, 2000, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.2       millert     5:  *
1.20      millert     6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.2       millert     9:  *
1.20      millert    10:  * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
                     12:  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
                     13:  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
                     15:  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
                     16:  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.17      millert    17:  *
                     18:  * Sponsored in part by the Defense Advanced Research Projects
                     19:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
                     20:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1.2       millert    21:  */
                     22:
1.9       millert    23: #include <sys/param.h>
                     24: #include <sys/wait.h>
                     25:
1.1       millert    26: #include <err.h>
                     27: #include <errno.h>
1.17      millert    28: #include <fcntl.h>
1.1       millert    29: #include <limits.h>
1.9       millert    30: #include <login_cap.h>
1.1       millert    31: #include <paths.h>
                     32: #include <pwd.h>
                     33: #include <stdio.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
                     36: #include <unistd.h>
                     37: #include <skey.h>
                     38:
1.12      millert    39: void notify(struct passwd *, int, int);
                     40: FILE *runsendmail(struct passwd *, int *);
1.14      millert    41: __dead void usage(void);
1.1       millert    42:
                     43: int
1.14      millert    44: main(int argc, char **argv)
1.1       millert    45: {
                     46:        struct passwd *pw;
                     47:        struct skey key;
1.6       millert    48:        char *name;
1.14      millert    49:        int ch, left, aflag, iflag, limit;
1.6       millert    50:
1.17      millert    51:        aflag = iflag = 0;
1.14      millert    52:        limit = 12;
1.6       millert    53:        while ((ch = getopt(argc, argv, "ail:")) != -1)
1.1       millert    54:                switch(ch) {
1.6       millert    55:                case 'a':
                     56:                        if (getuid() != 0)
                     57:                                errx(1, "only root may use the -a flag");
1.14      millert    58:                        aflag = 1;
1.6       millert    59:                        break;
1.1       millert    60:                case 'i':
                     61:                        iflag = 1;
                     62:                        break;
                     63:                case 'l':
                     64:                        errno = 0;
                     65:                        if ((limit = (int)strtol(optarg, NULL, 10)) == 0)
                     66:                                errno = ERANGE;
                     67:                        if (errno) {
                     68:                                warn("key limit");
                     69:                                usage();
                     70:                        }
                     71:                        break;
                     72:                default:
                     73:                        usage();
                     74:        }
                     75:
1.17      millert    76:        /*
                     77:         * Make sure STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are open.
                     78:         * If not, open /dev/null in their place or bail.
                     79:         * If we are in interactive mode, STDOUT_FILENO *must* be open.
                     80:         */
                     81:        for (ch = STDIN_FILENO; ch <= STDERR_FILENO; ch++) {
1.22      millert    82:                if (fcntl(ch, F_GETFL, 0) == -1 && errno == EBADF) {
1.17      millert    83:                        if (ch == STDOUT_FILENO && iflag)
                     84:                                exit(1);        /* need stdout for -i */
                     85:                        if (open(_PATH_DEVNULL, O_RDWR, 0644) == -1)
                     86:                                exit(1);        /* just bail */
                     87:                }
                     88:        }
                     89:
1.1       millert    90:        if (argc - optind > 0)
                     91:                usage();
                     92:
1.6       millert    93:        /* Need key.keyfile zero'd at the very least */
                     94:        (void)memset(&key, 0, sizeof(key));
                     95:
1.17      millert    96:        left = 0;
1.6       millert    97:        if (aflag) {
                     98:                while ((ch = skeygetnext(&key)) == 0) {
1.1       millert    99:                        left = key.n - 1;
1.6       millert   100:                        if ((pw = getpwnam(key.logname)) == NULL)
                    101:                                continue;
                    102:                        if (left >= limit)
                    103:                                continue;
1.17      millert   104:                        (void)fclose(key.keyfile);
                    105:                        key.keyfile = NULL;
1.9       millert   106:                        notify(pw, left, iflag);
1.6       millert   107:                }
                    108:                if (ch == -1)
1.13      millert   109:                        errx(-1, "cannot open %s", _PATH_SKEYDIR);
1.6       millert   110:        } else {
                    111:                if ((pw = getpwuid(getuid())) == NULL)
                    112:                        errx(1, "no passwd entry for uid %u", getuid());
                    113:                if ((name = strdup(pw->pw_name)) == NULL)
                    114:                        err(1, "cannot allocate memory");
                    115:                sevenbit(name);
                    116:
1.14      millert   117:                switch (skeylookup(&key, name)) {
1.6       millert   118:                        case 0:         /* Success! */
                    119:                                left = key.n - 1;
                    120:                                break;
                    121:                        case -1:        /* File error */
1.14      millert   122:                                errx(1, "cannot open %s/%s", _PATH_SKEYDIR,
                    123:                                    name);
1.6       millert   124:                                break;
                    125:                        case 1:         /* Unknown user */
1.14      millert   126:                                errx(1, "user %s is not listed in %s", name,
1.13      millert   127:                                    _PATH_SKEYDIR);
1.6       millert   128:                }
                    129:                (void)fclose(key.keyfile);
                    130:
1.14      millert   131:                if (left < limit)
1.9       millert   132:                        notify(pw, left, iflag);
1.1       millert   133:        }
1.6       millert   134:
1.14      millert   135:        exit(0);
1.6       millert   136: }
1.1       millert   137:
1.6       millert   138: void
1.14      millert   139: notify(struct passwd *pw, int seq, int interactive)
1.6       millert   140: {
                    141:        static char hostname[MAXHOSTNAMELEN];
1.14      millert   142:        pid_t pid;
1.6       millert   143:        FILE *out;
1.1       millert   144:
1.6       millert   145:        /* Only set this once */
                    146:        if (hostname[0] == '\0' && gethostname(hostname, sizeof(hostname)) == -1)
1.15      millert   147:                strlcpy(hostname, "unknown", sizeof(hostname));
1.1       millert   148:
1.6       millert   149:        if (interactive)
1.1       millert   150:                out = stdout;
1.6       millert   151:        else
1.9       millert   152:                out = runsendmail(pw, &pid);
1.1       millert   153:
1.6       millert   154:        if (!interactive)
1.1       millert   155:                (void)fprintf(out,
1.23      deraadt   156:                   "Auto-Submitted: auto-generated\n"
1.9       millert   157:                   "To: %s\nSubject: IMPORTANT action required\n", pw->pw_name);
1.1       millert   158:
1.10      pjanzen   159:        if (seq)
                    160:                (void)fprintf(out,
1.1       millert   161: "\nYou are nearing the end of your current S/Key sequence for account\n\
                    162: %s on system %s.\n\n\
1.10      pjanzen   163: Your S/Key sequence number is now %d.  When it reaches zero\n\
                    164: you will no longer be able to use S/Key to log into the system.\n\n",
1.9       millert   165: pw->pw_name, hostname, seq);
1.10      pjanzen   166:        else
                    167:                (void)fprintf(out,
                    168: "\nYou are at the end of your current S/Key sequence for account\n\
                    169: %s on system %s.\n\n\
                    170: At this point you can no longer use S/Key to log into the system.\n\n",
                    171: pw->pw_name, hostname);
                    172:        (void)fprintf(out,
                    173: "Type \"skeyinit -s\" to reinitialize your sequence number.\n\n");
1.1       millert   174:
1.17      millert   175:        if (!interactive) {
                    176:                (void)fclose(out);
1.6       millert   177:                (void)waitpid(pid, NULL, 0);
1.17      millert   178:        }
1.1       millert   179: }
                    180:
1.6       millert   181: FILE *
1.14      millert   182: runsendmail(struct passwd *pw, pid_t *pidp)
1.6       millert   183: {
                    184:        FILE *fp;
1.14      millert   185:        int pfd[2];
                    186:        pid_t pid;
1.6       millert   187:
                    188:        if (pipe(pfd) < 0)
                    189:                return(NULL);
                    190:
                    191:        switch (pid = fork()) {
                    192:        case -1:                        /* fork(2) failed */
                    193:                (void)close(pfd[0]);
                    194:                (void)close(pfd[1]);
                    195:                return(NULL);
                    196:        case 0:                         /* In child */
                    197:                (void)close(pfd[1]);
                    198:                (void)dup2(pfd[0], STDIN_FILENO);
                    199:                (void)close(pfd[0]);
                    200:
                    201:                /* Run sendmail as target user not root */
1.19      millert   202:                if (getuid() == 0 &&
                    203:                    setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
1.9       millert   204:                        warn("cannot set user context");
                    205:                        _exit(127);
                    206:                }
1.6       millert   207:
1.11      deraadt   208:                execl(_PATH_SENDMAIL, "sendmail", "-t", (char *)NULL);
1.6       millert   209:                warn("cannot run \"%s -t\"", _PATH_SENDMAIL);
                    210:                _exit(127);
                    211:        }
                    212:
                    213:        /* In parent */
                    214:        *pidp = pid;
                    215:        fp = fdopen(pfd[1], "w");
                    216:        (void)close(pfd[0]);
                    217:
                    218:        return(fp);
                    219: }
1.14      millert   220:
                    221: __dead void
                    222: usage(void)
1.1       millert   223: {
1.14      millert   224:        extern char *__progname;
                    225:
1.24    ! sobrado   226:        (void)fprintf(stderr, "usage: %s [-ai] [-l limit]\n",
1.1       millert   227:            __progname);
                    228:        exit(1);
                    229: }