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