Annotation of src/usr.bin/sendbug/sendbug.c, Revision 1.19
1.19 ! deraadt 1: /* $OpenBSD: sendbug.c,v 1.18 2007/03/25 23:23:29 ray Exp $ */
1.1 ray 2:
3: /*
4: * Written by Ray Lai <ray@cyth.net>.
5: * Public domain.
6: */
7:
8: #include <sys/types.h>
9: #include <sys/mman.h>
10: #include <sys/param.h>
11: #include <sys/stat.h>
12: #include <sys/sysctl.h>
13: #include <sys/wait.h>
14:
1.13 ray 15: #include <ctype.h>
1.1 ray 16: #include <err.h>
17: #include <errno.h>
18: #include <fcntl.h>
19: #include <limits.h>
20: #include <paths.h>
21: #include <pwd.h>
1.15 deraadt 22: #include <signal.h>
1.1 ray 23: #include <stdio.h>
24: #include <stdlib.h>
25: #include <string.h>
26: #include <unistd.h>
27:
28: #include "atomicio.h"
29:
1.15 deraadt 30: int editit(char *);
1.12 tedu 31: void init(void);
1.1 ray 32: int prompt(void);
33: int send_file(const char *, int dst);
34: int sendmail(const char *);
35: void template(FILE *);
36:
37: const char *categories = "system user library documentation ports kernel "
38: "alpha amd64 arm i386 m68k m88k mips ppc sgi sparc sparc64 vax";
1.11 deraadt 39: char *version = "4.2";
40:
41: struct passwd *pw;
1.14 deraadt 42: char os[BUFSIZ], rel[BUFSIZ], mach[BUFSIZ], details[BUFSIZ];
1.1 ray 43: char *fullname;
1.12 tedu 44: char *tmppath;
45: int wantcleanup;
1.11 deraadt 46:
1.12 tedu 47: __dead void
1.3 deraadt 48: usage(void)
49: {
1.6 deraadt 50: fprintf(stderr, "usage: sendbug [-LPV]\n");
1.12 tedu 51: exit(1);
52: }
53:
54: void
55: cleanup()
56: {
57: if (wantcleanup && tmppath && unlink(tmppath) == -1)
58: warn("unlink");
1.3 deraadt 59: }
60:
1.12 tedu 61:
1.1 ray 62: int
63: main(int argc, char *argv[])
64: {
1.19 ! deraadt 65: int ch, c, fd, ret = 1;
1.15 deraadt 66: const char *tmpdir;
1.19 ! deraadt 67: struct stat sb;
1.15 deraadt 68: char *pr_form;
1.1 ray 69: time_t mtime;
1.2 deraadt 70: FILE *fp;
1.3 deraadt 71:
1.5 deraadt 72: while ((ch = getopt(argc, argv, "LPV")) != -1)
1.3 deraadt 73: switch (ch) {
74: case 'L':
75: printf("Known categories:\n");
76: printf("%s\n\n", categories);
77: exit(0);
78: case 'P':
1.12 tedu 79: init();
1.3 deraadt 80: template(stdout);
81: exit(0);
1.5 deraadt 82: case 'V':
83: printf("%s\n", version);
84: exit(0);
1.3 deraadt 85: default:
86: usage();
1.7 deraadt 87: }
88:
1.3 deraadt 89: if (argc > 1) {
90: usage();
91: }
1.1 ray 92:
93: if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
94: tmpdir = _PATH_TMP;
1.9 ray 95: if (asprintf(&tmppath, "%s%sp.XXXXXXXXXX", tmpdir,
1.19 ! deraadt 96: tmpdir[strlen(tmpdir) - 1] == '/' ? "" : "/") == -1)
1.12 tedu 97: err(1, "asprintf");
1.1 ray 98: if ((fd = mkstemp(tmppath)) == -1)
99: err(1, "mkstemp");
1.12 tedu 100: wantcleanup = 1;
101: atexit(cleanup);
1.19 ! deraadt 102: if ((fp = fdopen(fd, "w+")) == NULL)
1.12 tedu 103: err(1, "fdopen");
1.1 ray 104:
1.12 tedu 105: init();
1.1 ray 106:
1.10 deraadt 107: pr_form = getenv("PR_FORM");
108: if (pr_form) {
109: char buf[BUFSIZ];
110: size_t len;
111: FILE *frfp;
112:
113: frfp = fopen(pr_form, "r");
114: if (frfp == NULL) {
115: fprintf(stderr, "sendbug: can't seem to read your"
116: " template file (`%s'), ignoring PR_FORM\n",
117: pr_form);
118: template(fp);
119: } else {
120: while (!feof(frfp)) {
121: len = fread(buf, 1, sizeof buf, frfp);
122: if (len == 0)
123: break;
124: if (fwrite(buf, 1, len, fp) != len)
125: break;
126: }
127: fclose(frfp);
128: }
129: } else
130: template(fp);
1.1 ray 131:
1.19 ! deraadt 132: if (fflush(fp) == EOF || fstat(fd, &sb) == -1 || fclose(fp) == EOF)
1.12 tedu 133: err(1, "error creating template");
1.1 ray 134: mtime = sb.st_mtime;
135:
136: edit:
1.15 deraadt 137: editit(tmppath);
1.1 ray 138:
1.19 ! deraadt 139: if (stat(tmppath, &sb) == -1)
1.12 tedu 140: err(1, "stat");
1.19 ! deraadt 141: if (mtime == sb.st_mtime)
1.12 tedu 142: errx(1, "report unchanged, nothing sent");
1.1 ray 143:
144: prompt:
145: c = prompt();
146: switch (c) {
1.5 deraadt 147: case 'a':
148: case EOF:
1.12 tedu 149: wantcleanup = 0;
150: errx(1, "unsent report in %s", tmppath);
1.1 ray 151: case 'e':
152: goto edit;
153: case 's':
154: if (sendmail(tmppath) == -1)
155: goto quit;
156: break;
157: default:
158: goto prompt;
159: }
160:
161: ret = 0;
1.12 tedu 162: quit:
1.1 ray 163: return (ret);
164: }
165:
1.15 deraadt 166: int
167: editit(char *tmpfile)
168: {
1.19 ! deraadt 169: char *argp[] = {"sh", "-c", NULL, NULL}, *ed, *p;
1.15 deraadt 170: pid_t pid, xpid;
171: int stat;
172:
173: if ((ed = getenv("EDITOR")) == (char *)0)
174: ed = _PATH_VI;
175: if (asprintf(&p, "%s %s", ed, tmpfile) == -1)
1.18 ray 176: return (-1);
1.15 deraadt 177: argp[2] = p;
178:
179: top:
180: (void)signal(SIGHUP, SIG_IGN);
181: (void)signal(SIGINT, SIG_IGN);
182: (void)signal(SIGQUIT, SIG_IGN);
183: if ((pid = fork()) < 0) {
1.16 ray 184: int saved_errno = errno;
185:
1.15 deraadt 186: (void)signal(SIGHUP, SIG_DFL);
187: (void)signal(SIGINT, SIG_DFL);
188: (void)signal(SIGQUIT, SIG_DFL);
1.16 ray 189: if (saved_errno == EPROCLIM) {
1.15 deraadt 190: warnx("you have too many processes");
191: free(p);
1.18 ray 192: return (-1);
1.15 deraadt 193: }
1.16 ray 194: if (saved_errno == EAGAIN) {
1.15 deraadt 195: sleep(1);
196: goto top;
197: }
198: perror("fork");
199: free(p);
1.18 ray 200: return (-1);
1.15 deraadt 201: }
202: if (pid == 0) {
203: (void)signal(SIGHUP, SIG_DFL);
204: (void)signal(SIGINT, SIG_DFL);
205: (void)signal(SIGQUIT, SIG_DFL);
206: execv(_PATH_BSHELL, argp);
207: _exit(127);
208: }
209: free(p);
210: for (;;) {
211: xpid = waitpid(pid, (int *)&stat, WUNTRACED);
212: if (WIFSTOPPED(stat))
213: raise(WSTOPSIG(stat));
214: else if (WIFEXITED(stat))
215: break;
216: }
217: (void)signal(SIGHUP, SIG_DFL);
218: (void)signal(SIGINT, SIG_DFL);
219: (void)signal(SIGQUIT, SIG_DFL);
220: if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
1.18 ray 221: return (-1);
222: return (0);
1.15 deraadt 223: }
1.12 tedu 224:
1.1 ray 225: int
226: prompt(void)
227: {
228: int c, ret;
229:
230: fpurge(stdin);
231: fprintf(stderr, "a)bort, e)dit, or s)end: ");
232: fflush(stderr);
233: ret = getchar();
234: if (ret == EOF || ret == '\n')
235: return (ret);
236: do {
237: c = getchar();
238: } while (c != EOF && c != '\n');
239: return (ret);
240: }
241:
242: int
243: sendmail(const char *tmppath)
244: {
245: int filedes[2];
246:
247: if (pipe(filedes) == -1) {
248: warn("pipe: unsent report in %s", tmppath);
249: return (-1);
250: }
251: switch (fork()) {
252: case -1:
253: warn("fork error: unsent report in %s",
254: tmppath);
255: return (-1);
256: case 0:
257: close(filedes[1]);
258: if (dup2(filedes[0], STDIN_FILENO) == -1) {
259: warn("dup2 error: unsent report in %s",
260: tmppath);
261: return (-1);
262: }
263: close(filedes[0]);
264: execl("/usr/sbin/sendmail", "sendmail",
1.2 deraadt 265: "-oi", "-t", (void *)NULL);
1.1 ray 266: warn("sendmail error: unsent report in %s",
267: tmppath);
268: return (-1);
269: default:
270: close(filedes[0]);
271: /* Pipe into sendmail. */
272: if (send_file(tmppath, filedes[1]) == -1) {
273: warn("send_file error: unsent report in %s",
274: tmppath);
275: return (-1);
276: }
277: close(filedes[1]);
278: wait(NULL);
279: break;
280: }
281: return (0);
282: }
283:
1.12 tedu 284: void
1.1 ray 285: init(void)
286: {
1.13 ray 287: size_t len = 0, namelen;
1.19 ! deraadt 288: const char *src;
1.1 ray 289: int sysname[2];
1.14 deraadt 290: char *dst, *cp;
1.1 ray 291:
1.19 ! deraadt 292: if ((pw = getpwuid(getuid())) == NULL)
1.12 tedu 293: err(1, "getpwuid");
1.13 ray 294: namelen = strlen(pw->pw_name);
1.1 ray 295:
1.13 ray 296: /* Add length of expanded '&', minus existing '&'. */
297: src = pw->pw_gecos;
298: src += strcspn(src, ",&");
299: while (*src == '&') {
300: len += namelen - 1;
301: /* Look for next '&', skipping the one we just found. */
302: src += 1 + strcspn(src, ",&");
303: }
304: /* Add full name length, including all those '&' we skipped. */
305: len += src - pw->pw_gecos;
1.19 ! deraadt 306: if ((fullname = malloc(len + 1)) == NULL)
1.12 tedu 307: err(1, "malloc");
1.19 ! deraadt 308:
1.13 ray 309: dst = fullname;
310: src = pw->pw_gecos;
311: while (*src != ',' && *src != '\0') {
312: /* Copy text up to ',' or '&' and skip. */
313: len = strcspn(src, ",&");
314: memcpy(dst, src, len);
315: dst += len;
316: src += len;
317: /* Replace '&' with login. */
318: if (*src == '&') {
319: memcpy(dst, pw->pw_name, namelen);
320: *dst = toupper((unsigned char)*dst);
321: dst += namelen;
322: ++src;
323: }
324: }
325: *dst = '\0';
1.1 ray 326:
327: sysname[0] = CTL_KERN;
328: sysname[1] = KERN_OSTYPE;
329: len = sizeof(os) - 1;
1.19 ! deraadt 330: if (sysctl(sysname, 2, &os, &len, NULL, 0) == -1)
1.12 tedu 331: err(1, "sysctl");
1.1 ray 332:
333: sysname[0] = CTL_KERN;
334: sysname[1] = KERN_OSRELEASE;
335: len = sizeof(rel) - 1;
1.19 ! deraadt 336: if (sysctl(sysname, 2, &rel, &len, NULL, 0) == -1)
1.12 tedu 337: err(1, "sysctl");
1.1 ray 338:
1.14 deraadt 339: sysname[0] = CTL_KERN;
340: sysname[1] = KERN_VERSION;
341: len = sizeof(details) - 1;
1.19 ! deraadt 342: if (sysctl(sysname, 2, &details, &len, NULL, 0) == -1)
1.14 deraadt 343: err(1, "sysctl");
344:
345: cp = strchr(details, '\n');
346: if (cp) {
347: cp++;
348: if (*cp)
349: *cp++ = '\t';
350: if (*cp)
351: *cp++ = '\t';
352: if (*cp)
353: *cp++ = '\t';
354: }
355:
1.1 ray 356: sysname[0] = CTL_HW;
357: sysname[1] = HW_MACHINE;
358: len = sizeof(mach) - 1;
1.19 ! deraadt 359: if (sysctl(sysname, 2, &mach, &len, NULL, 0) == -1)
1.12 tedu 360: err(1, "sysctl");
1.1 ray 361: }
362:
363: int
364: send_file(const char *file, int dst)
365: {
1.2 deraadt 366: int blank = 0;
367: size_t len;
368: char *buf;
1.1 ray 369: FILE *fp;
370:
371: if ((fp = fopen(file, "r")) == NULL)
372: return (-1);
373: while ((buf = fgetln(fp, &len))) {
374: /* Skip lines starting with "SENDBUG". */
375: if (len >= sizeof("SENDBUG") - 1 &&
376: memcmp(buf, "SENDBUG", sizeof("SENDBUG") - 1) == 0)
377: continue;
378: if (len == 1 && buf[0] == '\n')
379: blank = 1;
380: /* Skip comments, but only if we encountered a blank line. */
381: while (len) {
1.14 deraadt 382: char *sp = NULL, *ep = NULL;
1.1 ray 383: size_t copylen;
384:
385: if (blank && (sp = memchr(buf, '<', len)) != NULL)
386: ep = memchr(sp, '>', len - (sp - buf + 1));
387: /* Length of string before comment. */
1.4 ray 388: if (ep)
389: copylen = sp - buf;
390: else
391: copylen = len;
1.1 ray 392: if (atomicio(vwrite, dst, buf, copylen) != copylen) {
393: int saved_errno = errno;
394:
395: fclose(fp);
396: errno = saved_errno;
397: return (-1);
398: }
399: if (!ep)
400: break;
401: /* Skip comment. */
402: len -= ep - buf + 1;
403: buf = ep + 1;
404: }
405: }
406: fclose(fp);
407: return (0);
408: }
409:
410: void
411: template(FILE *fp)
412: {
413: fprintf(fp, "SENDBUG: -*- sendbug -*-\n");
1.19 ! deraadt 414: fprintf(fp, "SENDBUG: Lines starting with `SENDBUG' will"
! 415: " be removed automatically, as\n");
! 416: fprintf(fp, "SENDBUG: will all comments (text enclosed in `<' and `>').\n");
1.1 ray 417: fprintf(fp, "SENDBUG:\n");
418: fprintf(fp, "SENDBUG: Choose from the following categories:\n");
419: fprintf(fp, "SENDBUG:\n");
420: fprintf(fp, "SENDBUG: %s\n", categories);
421: fprintf(fp, "SENDBUG:\n");
422: fprintf(fp, "SENDBUG:\n");
423: fprintf(fp, "To: %s\n", "gnats@openbsd.org");
424: fprintf(fp, "Subject: \n");
425: fprintf(fp, "From: %s\n", pw->pw_name);
426: fprintf(fp, "Cc: \n");
427: fprintf(fp, "Reply-To: %s\n", pw->pw_name);
1.5 deraadt 428: fprintf(fp, "X-sendbug-version: %s\n", version);
1.1 ray 429: fprintf(fp, "\n");
430: fprintf(fp, "\n");
431: fprintf(fp, ">Submitter-Id:\tnet\n");
432: fprintf(fp, ">Originator:\t%s\n", fullname);
433: fprintf(fp, ">Organization:\n");
434: fprintf(fp, "net\n");
435: fprintf(fp, ">Synopsis:\t<synopsis of the problem (one line)>\n");
1.19 ! deraadt 436: fprintf(fp, ">Severity:\t"
! 437: "<[ non-critical | serious | critical ] (one line)>\n");
1.1 ray 438: fprintf(fp, ">Priority:\t<[ low | medium | high ] (one line)>\n");
439: fprintf(fp, ">Category:\t<PR category (one line)>\n");
1.19 ! deraadt 440: fprintf(fp, ">Class:\t\t"
! 441: "<[ sw-bug | doc-bug | change-request | support ] (one line)>\n");
1.1 ray 442: fprintf(fp, ">Release:\t<release number or tag (one line)>\n");
443: fprintf(fp, ">Environment:\n");
444: fprintf(fp, "\t<machine, os, target, libraries (multiple lines)>\n");
445: fprintf(fp, "\tSystem : %s %s\n", os, rel);
1.14 deraadt 446: fprintf(fp, "\tDetails : %s\n", details);
1.1 ray 447: fprintf(fp, "\tArchitecture: %s.%s\n", os, mach);
448: fprintf(fp, "\tMachine : %s\n", mach);
449: fprintf(fp, ">Description:\n");
450: fprintf(fp, "\t<precise description of the problem (multiple lines)>\n");
451: fprintf(fp, ">How-To-Repeat:\n");
1.19 ! deraadt 452: fprintf(fp, "\t<code/input/activities to reproduce the problem"
! 453: " (multiple lines)>\n");
1.1 ray 454: fprintf(fp, ">Fix:\n");
1.19 ! deraadt 455: fprintf(fp, "\t<how to correct or work around the problem,"
! 456: " if known (multiple lines)>\n");
1.1 ray 457: }