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

1.14    ! millert     1: /*     $OpenBSD: skeyaudit.c,v 1.13 2002/05/16 03:50:42 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:
1.12      millert    45: void notify(struct passwd *, int, int);
                     46: FILE *runsendmail(struct passwd *, int *);
1.14    ! millert    47: __dead void usage(void);
1.1       millert    48:
                     49: int
1.14    ! millert    50: main(int argc, char **argv)
1.1       millert    51: {
                     52:        struct passwd *pw;
                     53:        struct skey key;
1.6       millert    54:        char *name;
1.14    ! millert    55:        int ch, left, aflag, iflag, limit;
1.6       millert    56:
1.14    ! millert    57:        left = aflag = iflag = 0;
        !            58:        limit = 12;
1.6       millert    59:        while ((ch = getopt(argc, argv, "ail:")) != -1)
1.1       millert    60:                switch(ch) {
1.6       millert    61:                case 'a':
                     62:                        if (getuid() != 0)
                     63:                                errx(1, "only root may use the -a flag");
1.14    ! millert    64:                        aflag = 1;
1.6       millert    65:                        break;
1.1       millert    66:                case 'i':
                     67:                        iflag = 1;
                     68:                        break;
                     69:                case 'l':
                     70:                        errno = 0;
                     71:                        if ((limit = (int)strtol(optarg, NULL, 10)) == 0)
                     72:                                errno = ERANGE;
                     73:                        if (errno) {
                     74:                                warn("key limit");
                     75:                                usage();
                     76:                        }
                     77:                        break;
                     78:                default:
                     79:                        usage();
                     80:        }
                     81:
                     82:        if (argc - optind > 0)
                     83:                usage();
                     84:
1.6       millert    85:        /* Need key.keyfile zero'd at the very least */
                     86:        (void)memset(&key, 0, sizeof(key));
                     87:
                     88:        if (aflag) {
                     89:                while ((ch = skeygetnext(&key)) == 0) {
1.1       millert    90:                        left = key.n - 1;
1.6       millert    91:                        if ((pw = getpwnam(key.logname)) == NULL)
                     92:                                continue;
                     93:                        if (left >= limit)
                     94:                                continue;
1.9       millert    95:                        notify(pw, left, iflag);
1.6       millert    96:                }
                     97:                if (ch == -1)
1.13      millert    98:                        errx(-1, "cannot open %s", _PATH_SKEYDIR);
1.6       millert    99:                else
                    100:                        (void)fclose(key.keyfile);
                    101:        } else {
                    102:                if ((pw = getpwuid(getuid())) == NULL)
                    103:                        errx(1, "no passwd entry for uid %u", getuid());
                    104:                if ((name = strdup(pw->pw_name)) == NULL)
                    105:                        err(1, "cannot allocate memory");
                    106:                sevenbit(name);
                    107:
1.14    ! millert   108:                switch (skeylookup(&key, name)) {
1.6       millert   109:                        case 0:         /* Success! */
                    110:                                left = key.n - 1;
                    111:                                break;
                    112:                        case -1:        /* File error */
1.14    ! millert   113:                                errx(1, "cannot open %s/%s", _PATH_SKEYDIR,
        !           114:                                    name);
1.6       millert   115:                                break;
                    116:                        case 1:         /* Unknown user */
1.14    ! millert   117:                                errx(1, "user %s is not listed in %s", name,
1.13      millert   118:                                    _PATH_SKEYDIR);
1.6       millert   119:                }
                    120:                (void)fclose(key.keyfile);
                    121:
1.14    ! millert   122:                if (left < limit)
1.9       millert   123:                        notify(pw, left, iflag);
1.1       millert   124:        }
1.6       millert   125:
1.14    ! millert   126:        exit(0);
1.6       millert   127: }
1.1       millert   128:
1.6       millert   129: void
1.14    ! millert   130: notify(struct passwd *pw, int seq, int interactive)
1.6       millert   131: {
                    132:        static char hostname[MAXHOSTNAMELEN];
1.14    ! millert   133:        pid_t pid;
1.6       millert   134:        FILE *out;
1.1       millert   135:
1.6       millert   136:        /* Only set this once */
                    137:        if (hostname[0] == '\0' && gethostname(hostname, sizeof(hostname)) == -1)
1.1       millert   138:                strcpy(hostname, "unknown");
                    139:
1.6       millert   140:        if (interactive)
1.1       millert   141:                out = stdout;
1.6       millert   142:        else
1.9       millert   143:                out = runsendmail(pw, &pid);
1.1       millert   144:
1.6       millert   145:        if (!interactive)
1.1       millert   146:                (void)fprintf(out,
1.9       millert   147:                   "To: %s\nSubject: IMPORTANT action required\n", pw->pw_name);
1.1       millert   148:
1.10      pjanzen   149:        if (seq)
                    150:                (void)fprintf(out,
1.1       millert   151: "\nYou are nearing the end of your current S/Key sequence for account\n\
                    152: %s on system %s.\n\n\
1.10      pjanzen   153: Your S/Key sequence number is now %d.  When it reaches zero\n\
                    154: you will no longer be able to use S/Key to log into the system.\n\n",
1.9       millert   155: pw->pw_name, hostname, seq);
1.10      pjanzen   156:        else
                    157:                (void)fprintf(out,
                    158: "\nYou are at the end of your current S/Key sequence for account\n\
                    159: %s on system %s.\n\n\
                    160: At this point you can no longer use S/Key to log into the system.\n\n",
                    161: pw->pw_name, hostname);
                    162:        (void)fprintf(out,
                    163: "Type \"skeyinit -s\" to reinitialize your sequence number.\n\n");
1.1       millert   164:
1.6       millert   165:        (void)fclose(out);
                    166:        if (!interactive)
                    167:                (void)waitpid(pid, NULL, 0);
1.1       millert   168: }
                    169:
1.6       millert   170: FILE *
1.14    ! millert   171: runsendmail(struct passwd *pw, pid_t *pidp)
1.6       millert   172: {
                    173:        FILE *fp;
1.14    ! millert   174:        int pfd[2];
        !           175:        pid_t pid;
1.6       millert   176:
                    177:        if (pipe(pfd) < 0)
                    178:                return(NULL);
                    179:
                    180:        switch (pid = fork()) {
                    181:        case -1:                        /* fork(2) failed */
                    182:                (void)close(pfd[0]);
                    183:                (void)close(pfd[1]);
                    184:                return(NULL);
                    185:        case 0:                         /* In child */
                    186:                (void)close(pfd[1]);
                    187:                (void)dup2(pfd[0], STDIN_FILENO);
                    188:                (void)close(pfd[0]);
                    189:
                    190:                /* Run sendmail as target user not root */
1.9       millert   191:                if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
                    192:                        warn("cannot set user context");
                    193:                        _exit(127);
                    194:                }
1.6       millert   195:
1.11      deraadt   196:                execl(_PATH_SENDMAIL, "sendmail", "-t", (char *)NULL);
1.6       millert   197:                warn("cannot run \"%s -t\"", _PATH_SENDMAIL);
                    198:                _exit(127);
                    199:        }
                    200:
                    201:        /* In parent */
                    202:        *pidp = pid;
                    203:        fp = fdopen(pfd[1], "w");
                    204:        (void)close(pfd[0]);
                    205:
                    206:        return(fp);
                    207: }
1.14    ! millert   208:
        !           209: __dead void
        !           210: usage(void)
1.1       millert   211: {
1.14    ! millert   212:        extern char *__progname;
        !           213:
1.1       millert   214:        (void)fprintf(stderr, "Usage: %s [-i] [-l limit]\n",
                    215:            __progname);
                    216:        exit(1);
                    217: }