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

1.16    ! millert     1: /*     $OpenBSD: skeyaudit.c,v 1.15 2003/03/14 04:29:04 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:                if ((pw = getpwuid(getuid())) == NULL)
                    101:                        errx(1, "no passwd entry for uid %u", getuid());
                    102:                if ((name = strdup(pw->pw_name)) == NULL)
                    103:                        err(1, "cannot allocate memory");
                    104:                sevenbit(name);
                    105:
1.14      millert   106:                switch (skeylookup(&key, name)) {
1.6       millert   107:                        case 0:         /* Success! */
                    108:                                left = key.n - 1;
                    109:                                break;
                    110:                        case -1:        /* File error */
1.14      millert   111:                                errx(1, "cannot open %s/%s", _PATH_SKEYDIR,
                    112:                                    name);
1.6       millert   113:                                break;
                    114:                        case 1:         /* Unknown user */
1.14      millert   115:                                errx(1, "user %s is not listed in %s", name,
1.13      millert   116:                                    _PATH_SKEYDIR);
1.6       millert   117:                }
                    118:                (void)fclose(key.keyfile);
                    119:
1.14      millert   120:                if (left < limit)
1.9       millert   121:                        notify(pw, left, iflag);
1.1       millert   122:        }
1.6       millert   123:
1.14      millert   124:        exit(0);
1.6       millert   125: }
1.1       millert   126:
1.6       millert   127: void
1.14      millert   128: notify(struct passwd *pw, int seq, int interactive)
1.6       millert   129: {
                    130:        static char hostname[MAXHOSTNAMELEN];
1.14      millert   131:        pid_t pid;
1.6       millert   132:        FILE *out;
1.1       millert   133:
1.6       millert   134:        /* Only set this once */
                    135:        if (hostname[0] == '\0' && gethostname(hostname, sizeof(hostname)) == -1)
1.15      millert   136:                strlcpy(hostname, "unknown", sizeof(hostname));
1.1       millert   137:
1.6       millert   138:        if (interactive)
1.1       millert   139:                out = stdout;
1.6       millert   140:        else
1.9       millert   141:                out = runsendmail(pw, &pid);
1.1       millert   142:
1.6       millert   143:        if (!interactive)
1.1       millert   144:                (void)fprintf(out,
1.9       millert   145:                   "To: %s\nSubject: IMPORTANT action required\n", pw->pw_name);
1.1       millert   146:
1.10      pjanzen   147:        if (seq)
                    148:                (void)fprintf(out,
1.1       millert   149: "\nYou are nearing the end of your current S/Key sequence for account\n\
                    150: %s on system %s.\n\n\
1.10      pjanzen   151: Your S/Key sequence number is now %d.  When it reaches zero\n\
                    152: you will no longer be able to use S/Key to log into the system.\n\n",
1.9       millert   153: pw->pw_name, hostname, seq);
1.10      pjanzen   154:        else
                    155:                (void)fprintf(out,
                    156: "\nYou are at the end of your current S/Key sequence for account\n\
                    157: %s on system %s.\n\n\
                    158: At this point you can no longer use S/Key to log into the system.\n\n",
                    159: pw->pw_name, hostname);
                    160:        (void)fprintf(out,
                    161: "Type \"skeyinit -s\" to reinitialize your sequence number.\n\n");
1.1       millert   162:
1.6       millert   163:        (void)fclose(out);
                    164:        if (!interactive)
                    165:                (void)waitpid(pid, NULL, 0);
1.1       millert   166: }
                    167:
1.6       millert   168: FILE *
1.14      millert   169: runsendmail(struct passwd *pw, pid_t *pidp)
1.6       millert   170: {
                    171:        FILE *fp;
1.14      millert   172:        int pfd[2];
                    173:        pid_t pid;
1.6       millert   174:
                    175:        if (pipe(pfd) < 0)
                    176:                return(NULL);
                    177:
                    178:        switch (pid = fork()) {
                    179:        case -1:                        /* fork(2) failed */
                    180:                (void)close(pfd[0]);
                    181:                (void)close(pfd[1]);
                    182:                return(NULL);
                    183:        case 0:                         /* In child */
                    184:                (void)close(pfd[1]);
                    185:                (void)dup2(pfd[0], STDIN_FILENO);
                    186:                (void)close(pfd[0]);
                    187:
                    188:                /* Run sendmail as target user not root */
1.9       millert   189:                if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
                    190:                        warn("cannot set user context");
                    191:                        _exit(127);
                    192:                }
1.6       millert   193:
1.11      deraadt   194:                execl(_PATH_SENDMAIL, "sendmail", "-t", (char *)NULL);
1.6       millert   195:                warn("cannot run \"%s -t\"", _PATH_SENDMAIL);
                    196:                _exit(127);
                    197:        }
                    198:
                    199:        /* In parent */
                    200:        *pidp = pid;
                    201:        fp = fdopen(pfd[1], "w");
                    202:        (void)close(pfd[0]);
                    203:
                    204:        return(fp);
                    205: }
1.14      millert   206:
                    207: __dead void
                    208: usage(void)
1.1       millert   209: {
1.14      millert   210:        extern char *__progname;
                    211:
1.1       millert   212:        (void)fprintf(stderr, "Usage: %s [-i] [-l limit]\n",
                    213:            __progname);
                    214:        exit(1);
                    215: }