Annotation of src/usr.bin/skeyaudit/skeyaudit.c, Revision 1.25
1.25 ! deraadt 1: /* $OpenBSD: skeyaudit.c,v 1.24 2008/11/12 16:13:46 sobrado Exp $ */
1.2 millert 2:
3: /*
1.17 millert 4: * Copyright (c) 1997, 2000, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.2 millert 5: *
1.20 millert 6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.2 millert 9: *
1.20 millert 10: * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
13: * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15: * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.17 millert 17: *
18: * Sponsored in part by the Defense Advanced Research Projects
19: * Agency (DARPA) and Air Force Research Laboratory, Air Force
20: * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1.2 millert 21: */
22:
1.9 millert 23: #include <sys/wait.h>
24:
1.1 millert 25: #include <err.h>
26: #include <errno.h>
1.17 millert 27: #include <fcntl.h>
1.1 millert 28: #include <limits.h>
1.9 millert 29: #include <login_cap.h>
1.1 millert 30: #include <paths.h>
31: #include <pwd.h>
32: #include <stdio.h>
33: #include <stdlib.h>
34: #include <string.h>
35: #include <unistd.h>
36: #include <skey.h>
37:
1.12 millert 38: void notify(struct passwd *, int, int);
39: FILE *runsendmail(struct passwd *, int *);
1.14 millert 40: __dead void usage(void);
1.1 millert 41:
42: int
1.14 millert 43: main(int argc, char **argv)
1.1 millert 44: {
45: struct passwd *pw;
46: struct skey key;
1.6 millert 47: char *name;
1.14 millert 48: int ch, left, aflag, iflag, limit;
1.6 millert 49:
1.17 millert 50: aflag = iflag = 0;
1.14 millert 51: limit = 12;
1.6 millert 52: while ((ch = getopt(argc, argv, "ail:")) != -1)
1.1 millert 53: switch(ch) {
1.6 millert 54: case 'a':
55: if (getuid() != 0)
56: errx(1, "only root may use the -a flag");
1.14 millert 57: aflag = 1;
1.6 millert 58: break;
1.1 millert 59: case 'i':
60: iflag = 1;
61: break;
62: case 'l':
63: errno = 0;
64: if ((limit = (int)strtol(optarg, NULL, 10)) == 0)
65: errno = ERANGE;
66: if (errno) {
67: warn("key limit");
68: usage();
69: }
70: break;
71: default:
72: usage();
73: }
74:
1.17 millert 75: /*
76: * Make sure STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are open.
77: * If not, open /dev/null in their place or bail.
78: * If we are in interactive mode, STDOUT_FILENO *must* be open.
79: */
80: for (ch = STDIN_FILENO; ch <= STDERR_FILENO; ch++) {
1.22 millert 81: if (fcntl(ch, F_GETFL, 0) == -1 && errno == EBADF) {
1.17 millert 82: if (ch == STDOUT_FILENO && iflag)
83: exit(1); /* need stdout for -i */
84: if (open(_PATH_DEVNULL, O_RDWR, 0644) == -1)
85: exit(1); /* just bail */
86: }
87: }
88:
1.1 millert 89: if (argc - optind > 0)
90: usage();
91:
1.6 millert 92: /* Need key.keyfile zero'd at the very least */
93: (void)memset(&key, 0, sizeof(key));
94:
1.17 millert 95: left = 0;
1.6 millert 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;
1.17 millert 103: (void)fclose(key.keyfile);
104: key.keyfile = NULL;
1.9 millert 105: notify(pw, left, iflag);
1.6 millert 106: }
107: if (ch == -1)
1.13 millert 108: errx(-1, "cannot open %s", _PATH_SKEYDIR);
1.6 millert 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:
1.14 millert 116: switch (skeylookup(&key, name)) {
1.6 millert 117: case 0: /* Success! */
118: left = key.n - 1;
119: break;
120: case -1: /* File error */
1.14 millert 121: errx(1, "cannot open %s/%s", _PATH_SKEYDIR,
122: name);
1.6 millert 123: break;
124: case 1: /* Unknown user */
1.14 millert 125: errx(1, "user %s is not listed in %s", name,
1.13 millert 126: _PATH_SKEYDIR);
1.6 millert 127: }
128: (void)fclose(key.keyfile);
129:
1.14 millert 130: if (left < limit)
1.9 millert 131: notify(pw, left, iflag);
1.1 millert 132: }
1.6 millert 133:
1.14 millert 134: exit(0);
1.6 millert 135: }
1.1 millert 136:
1.6 millert 137: void
1.14 millert 138: notify(struct passwd *pw, int seq, int interactive)
1.6 millert 139: {
1.25 ! deraadt 140: static char hostname[HOST_NAME_MAX+1];
1.14 millert 141: pid_t pid;
1.6 millert 142: FILE *out;
1.1 millert 143:
1.6 millert 144: /* Only set this once */
145: if (hostname[0] == '\0' && gethostname(hostname, sizeof(hostname)) == -1)
1.15 millert 146: strlcpy(hostname, "unknown", sizeof(hostname));
1.1 millert 147:
1.6 millert 148: if (interactive)
1.1 millert 149: out = stdout;
1.6 millert 150: else
1.9 millert 151: out = runsendmail(pw, &pid);
1.1 millert 152:
1.6 millert 153: if (!interactive)
1.1 millert 154: (void)fprintf(out,
1.23 deraadt 155: "Auto-Submitted: auto-generated\n"
1.9 millert 156: "To: %s\nSubject: IMPORTANT action required\n", pw->pw_name);
1.1 millert 157:
1.10 pjanzen 158: if (seq)
159: (void)fprintf(out,
1.1 millert 160: "\nYou are nearing the end of your current S/Key sequence for account\n\
161: %s on system %s.\n\n\
1.10 pjanzen 162: Your S/Key sequence number is now %d. When it reaches zero\n\
163: you will no longer be able to use S/Key to log into the system.\n\n",
1.9 millert 164: pw->pw_name, hostname, seq);
1.10 pjanzen 165: else
166: (void)fprintf(out,
167: "\nYou are at the end of your current S/Key sequence for account\n\
168: %s on system %s.\n\n\
169: At this point you can no longer use S/Key to log into the system.\n\n",
170: pw->pw_name, hostname);
171: (void)fprintf(out,
172: "Type \"skeyinit -s\" to reinitialize your sequence number.\n\n");
1.1 millert 173:
1.17 millert 174: if (!interactive) {
175: (void)fclose(out);
1.6 millert 176: (void)waitpid(pid, NULL, 0);
1.17 millert 177: }
1.1 millert 178: }
179:
1.6 millert 180: FILE *
1.14 millert 181: runsendmail(struct passwd *pw, pid_t *pidp)
1.6 millert 182: {
183: FILE *fp;
1.14 millert 184: int pfd[2];
185: pid_t pid;
1.6 millert 186:
187: if (pipe(pfd) < 0)
188: return(NULL);
189:
190: switch (pid = fork()) {
191: case -1: /* fork(2) failed */
192: (void)close(pfd[0]);
193: (void)close(pfd[1]);
194: return(NULL);
195: case 0: /* In child */
196: (void)close(pfd[1]);
197: (void)dup2(pfd[0], STDIN_FILENO);
198: (void)close(pfd[0]);
199:
200: /* Run sendmail as target user not root */
1.19 millert 201: if (getuid() == 0 &&
202: setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
1.9 millert 203: warn("cannot set user context");
204: _exit(127);
205: }
1.6 millert 206:
1.11 deraadt 207: execl(_PATH_SENDMAIL, "sendmail", "-t", (char *)NULL);
1.6 millert 208: warn("cannot run \"%s -t\"", _PATH_SENDMAIL);
209: _exit(127);
210: }
211:
212: /* In parent */
213: *pidp = pid;
214: fp = fdopen(pfd[1], "w");
215: (void)close(pfd[0]);
216:
217: return(fp);
218: }
1.14 millert 219:
220: __dead void
221: usage(void)
1.1 millert 222: {
1.14 millert 223: extern char *__progname;
224:
1.24 sobrado 225: (void)fprintf(stderr, "usage: %s [-ai] [-l limit]\n",
1.1 millert 226: __progname);
227: exit(1);
228: }