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

1.3     ! millert     1: /*     $OpenBSD: skeyaudit.c,v 1.2 1997/07/23 04:31:17 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>
                     46:
                     47: extern char *__progname;
                     48:
                     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;
                     58:        int ch, errs, left = 0, iflag = 0, limit = 12;
                     59:        char *name, hostname[MAXHOSTNAMELEN];
                     60:        FILE *out;
                     61:
                     62:        while ((ch = getopt(argc, argv, "il:")) != -1)
                     63:                switch(ch) {
                     64:                case 'i':
                     65:                        iflag = 1;
                     66:                        break;
                     67:                case 'l':
                     68:                        errno = 0;
                     69:                        if ((limit = (int)strtol(optarg, NULL, 10)) == 0)
                     70:                                errno = ERANGE;
                     71:                        if (errno) {
                     72:                                warn("key limit");
                     73:                                usage();
                     74:                        }
                     75:                        break;
                     76:                default:
                     77:                        usage();
                     78:        }
                     79:
                     80:        if (argc - optind > 0)
                     81:                usage();
                     82:
                     83:        if ((pw = getpwuid(getuid())) == NULL)
                     84:                errx(1, "no passwd entry for uid %u", getuid());
                     85:        if ((name = strdup(pw->pw_name)) == NULL)
                     86:                err(1, "cannot allocate memory");
                     87:        sevenbit(name);
                     88:
                     89:        errs = skeylookup(&key, name);
                     90:        switch (errs) {
                     91:                case 0:         /* Success! */
                     92:                        left = key.n - 1;
                     93:                        break;
                     94:                case -1:        /* File error */
                     95:                        /* XXX - _PATH_SKEYFILE should be in paths.h? */
                     96:                        warnx("cannot open /etc/skeykeys");
                     97:                        break;
                     98:                case 1:         /* Unknown user */
                     99:                        warnx("%s is not listed in /etc/skeykeys", name);
                    100:        }
                    101:
1.3     ! millert   102:        /* Run sendmail as user not root */
        !           103:        seteuid(getuid());
        !           104:        setuid(getuid());
1.1       millert   105:
                    106:        if (errs || left >= limit)
                    107:                exit(errs);
                    108:
                    109:        if (gethostname(hostname, sizeof(hostname)) == -1)
                    110:                strcpy(hostname, "unknown");
                    111:
                    112:        if (iflag) {
                    113:                out = stdout;
                    114:        } else {
                    115:                char cmd[sizeof(_PATH_SENDMAIL) + 3];
                    116:
                    117:                sprintf(cmd, "%s -t", _PATH_SENDMAIL);
                    118:                out = popen(cmd, "w");
                    119:        }
                    120:
                    121:        if (!iflag)
                    122:                (void)fprintf(out,
                    123:                    "To: %s\nSubject: IMPORTANT action required\n", name);
                    124:
                    125:        (void)fprintf(out,
                    126: "\nYou are nearing the end of your current S/Key sequence for account\n\
                    127: %s on system %s.\n\n\
                    128: Your S/key sequence number is now %d.  When it reaches zero\n\
                    129: you will no longer be able to use S/Key to login into the system.\n\n\
                    130: Type \"skeyinit -s\" to reinitialize your sequence number.\n\n",
                    131: name, hostname, left - 1);
                    132:
                    133:        if (iflag)
                    134:                (void)fclose(out);
                    135:        else
                    136:                (void)pclose(out);
                    137:
                    138:        exit(0);
                    139: }
                    140:
                    141: void
                    142: usage()
                    143: {
                    144:        (void)fprintf(stderr, "Usage: %s [-i] [-l limit]\n",
                    145:            __progname);
                    146:        exit(1);
                    147: }