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

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