Annotation of src/usr.bin/skeyaudit/skeyaudit.c, Revision 1.2
1.2 ! millert 1: /* $OpenBSD$ */
! 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:
102: setuid(getuid()); /* Run sendmail as user not root */
103:
104: if (errs || left >= limit)
105: exit(errs);
106:
107: if (gethostname(hostname, sizeof(hostname)) == -1)
108: strcpy(hostname, "unknown");
109:
110: if (iflag) {
111: out = stdout;
112: } else {
113: char cmd[sizeof(_PATH_SENDMAIL) + 3];
114:
115: sprintf(cmd, "%s -t", _PATH_SENDMAIL);
116: out = popen(cmd, "w");
117: }
118:
119: if (!iflag)
120: (void)fprintf(out,
121: "To: %s\nSubject: IMPORTANT action required\n", name);
122:
123: (void)fprintf(out,
124: "\nYou are nearing the end of your current S/Key sequence for account\n\
125: %s on system %s.\n\n\
126: Your S/key sequence number is now %d. When it reaches zero\n\
127: you will no longer be able to use S/Key to login into the system.\n\n\
128: Type \"skeyinit -s\" to reinitialize your sequence number.\n\n",
129: name, hostname, left - 1);
130:
131: if (iflag)
132: (void)fclose(out);
133: else
134: (void)pclose(out);
135:
136: exit(0);
137: }
138:
139: void
140: usage()
141: {
142: (void)fprintf(stderr, "Usage: %s [-i] [-l limit]\n",
143: __progname);
144: exit(1);
145: }