Annotation of src/usr.bin/sendbug/sendbug.c, Revision 1.73
1.73 ! deraadt 1: /* $OpenBSD: sendbug.c,v 1.72 2015/10/11 21:23:15 deraadt 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/stat.h>
10: #include <sys/sysctl.h>
11: #include <sys/wait.h>
12:
1.13 ray 13: #include <ctype.h>
1.1 ray 14: #include <err.h>
15: #include <errno.h>
16: #include <fcntl.h>
17: #include <limits.h>
18: #include <paths.h>
19: #include <pwd.h>
1.15 deraadt 20: #include <signal.h>
1.1 ray 21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
24: #include <unistd.h>
25:
26: #include "atomicio.h"
27:
1.35 ray 28: #define _PATH_DMESG "/var/run/dmesg.boot"
1.51 ray 29: #define DMESG_START "OpenBSD "
1.61 ray 30: #define BEGIN64 "begin-base64 "
31: #define END64 "===="
1.35 ray 32:
1.39 ray 33: int checkfile(const char *);
1.61 ray 34: void debase(void);
1.36 ray 35: void dmesg(FILE *);
1.42 ray 36: int editit(const char *);
1.61 ray 37: void hwdump(FILE *);
1.21 deraadt 38: void init(void);
1.40 ray 39: int matchline(const char *, const char *, size_t);
1.21 deraadt 40: int prompt(void);
1.32 ray 41: int send_file(const char *, int);
1.21 deraadt 42: int sendmail(const char *);
43: void template(FILE *);
1.65 sthen 44: void usbdevs(FILE *);
1.1 ray 45:
1.60 ray 46: const char *categories = "system user library documentation kernel "
1.68 miod 47: "alpha amd64 arm hppa i386 m88k mips64 powerpc sh sparc sparc64 vax";
1.53 ray 48: const char *comment[] = {
49: "<synopsis of the problem (one line)>",
1.60 ray 50: "<PR category (one line)>",
1.53 ray 51: "<precise description of the problem (multiple lines)>",
52: "<code/input/activities to reproduce the problem (multiple lines)>",
53: "<how to correct or work around the problem, if known (multiple lines)>"
54: };
1.11 deraadt 55:
56: struct passwd *pw;
1.14 deraadt 57: char os[BUFSIZ], rel[BUFSIZ], mach[BUFSIZ], details[BUFSIZ];
1.73 ! deraadt 58: const char *tmpdir = _PATH_TMP;
1.62 ray 59: char *tmppath;
1.58 deraadt 60: int Dflag, Pflag, wantcleanup;
1.11 deraadt 61:
1.12 tedu 62: __dead void
1.3 deraadt 63: usage(void)
64: {
1.41 ray 65: extern char *__progname;
66:
1.72 deraadt 67: fprintf(stderr, "usage: %s [-DEP]\n", __progname);
1.12 tedu 68: exit(1);
69: }
70:
71: void
72: cleanup()
73: {
74: if (wantcleanup && tmppath && unlink(tmppath) == -1)
75: warn("unlink");
1.3 deraadt 76: }
77:
1.12 tedu 78:
1.1 ray 79: int
80: main(int argc, char *argv[])
81: {
1.47 ray 82: int ch, c, fd, ret = 1;
1.19 deraadt 83: struct stat sb;
1.15 deraadt 84: char *pr_form;
1.1 ray 85: time_t mtime;
1.2 deraadt 86: FILE *fp;
1.70 deraadt 87:
1.71 deraadt 88: if (pledge("stdio rpath wpath cpath tmppath getpw proc exec", NULL) == -1)
1.70 deraadt 89: err(1, "pledge");
1.3 deraadt 90:
1.72 deraadt 91: while ((ch = getopt(argc, argv, "DEP")) != -1)
1.3 deraadt 92: switch (ch) {
1.35 ray 93: case 'D':
94: Dflag = 1;
95: break;
1.61 ray 96: case 'E':
97: debase();
98: exit(0);
1.3 deraadt 99: case 'P':
1.58 deraadt 100: Pflag = 1;
101: break;
1.3 deraadt 102: default:
103: usage();
1.7 deraadt 104: }
1.35 ray 105: argc -= optind;
106: argv += optind;
1.7 deraadt 107:
1.37 ray 108: if (argc > 0)
1.3 deraadt 109: usage();
1.63 deraadt 110:
1.58 deraadt 111: if (Pflag) {
112: init();
113: template(stdout);
114: exit(0);
115: }
1.1 ray 116:
1.9 ray 117: if (asprintf(&tmppath, "%s%sp.XXXXXXXXXX", tmpdir,
1.19 deraadt 118: tmpdir[strlen(tmpdir) - 1] == '/' ? "" : "/") == -1)
1.12 tedu 119: err(1, "asprintf");
1.1 ray 120: if ((fd = mkstemp(tmppath)) == -1)
121: err(1, "mkstemp");
1.12 tedu 122: wantcleanup = 1;
123: atexit(cleanup);
1.19 deraadt 124: if ((fp = fdopen(fd, "w+")) == NULL)
1.12 tedu 125: err(1, "fdopen");
1.1 ray 126:
1.12 tedu 127: init();
1.1 ray 128:
1.10 deraadt 129: pr_form = getenv("PR_FORM");
130: if (pr_form) {
131: char buf[BUFSIZ];
132: size_t len;
133: FILE *frfp;
134:
135: frfp = fopen(pr_form, "r");
136: if (frfp == NULL) {
1.41 ray 137: warn("can't seem to read your template file "
138: "(`%s'), ignoring PR_FORM", pr_form);
1.10 deraadt 139: template(fp);
140: } else {
141: while (!feof(frfp)) {
142: len = fread(buf, 1, sizeof buf, frfp);
143: if (len == 0)
144: break;
145: if (fwrite(buf, 1, len, fp) != len)
146: break;
147: }
148: fclose(frfp);
149: }
1.47 ray 150: } else
1.10 deraadt 151: template(fp);
1.1 ray 152:
1.19 deraadt 153: if (fflush(fp) == EOF || fstat(fd, &sb) == -1 || fclose(fp) == EOF)
1.12 tedu 154: err(1, "error creating template");
1.1 ray 155: mtime = sb.st_mtime;
156:
157: edit:
1.48 ray 158: if (editit(tmppath) == -1)
1.28 ray 159: err(1, "error running editor");
1.1 ray 160:
1.19 deraadt 161: if (stat(tmppath, &sb) == -1)
1.12 tedu 162: err(1, "stat");
1.19 deraadt 163: if (mtime == sb.st_mtime)
1.12 tedu 164: errx(1, "report unchanged, nothing sent");
1.1 ray 165:
166: prompt:
1.39 ray 167: if (!checkfile(tmppath))
168: fprintf(stderr, "fields are blank, must be filled in\n");
1.1 ray 169: c = prompt();
170: switch (c) {
1.5 deraadt 171: case 'a':
172: case EOF:
1.12 tedu 173: wantcleanup = 0;
174: errx(1, "unsent report in %s", tmppath);
1.1 ray 175: case 'e':
176: goto edit;
177: case 's':
178: if (sendmail(tmppath) == -1)
179: goto quit;
180: break;
181: default:
182: goto prompt;
183: }
184:
185: ret = 0;
1.12 tedu 186: quit:
1.1 ray 187: return (ret);
1.36 ray 188: }
189:
190: void
191: dmesg(FILE *fp)
192: {
193: char buf[BUFSIZ];
194: FILE *dfp;
195: off_t offset = -1;
196:
197: dfp = fopen(_PATH_DMESG, "r");
198: if (dfp == NULL) {
199: warn("can't read dmesg");
200: return;
201: }
202:
1.51 ray 203: /* Find last dmesg. */
1.36 ray 204: for (;;) {
205: off_t o;
206:
207: o = ftello(dfp);
208: if (fgets(buf, sizeof(buf), dfp) == NULL)
209: break;
1.51 ray 210: if (!strncmp(DMESG_START, buf, sizeof(DMESG_START) - 1))
1.36 ray 211: offset = o;
212: }
213: if (offset != -1) {
214: size_t len;
215:
216: clearerr(dfp);
217: fseeko(dfp, offset, SEEK_SET);
218: while (offset != -1 && !feof(dfp)) {
219: len = fread(buf, 1, sizeof buf, dfp);
220: if (len == 0)
221: break;
222: if (fwrite(buf, 1, len, fp) != len)
223: break;
224: }
225: }
226: fclose(dfp);
1.1 ray 227: }
228:
1.65 sthen 229: void
230: usbdevs(FILE *ofp)
231: {
232: char buf[BUFSIZ];
233: FILE *ifp;
234: size_t len;
235:
236: if ((ifp = popen("usbdevs -v", "r")) != NULL) {
237: while (!feof(ifp)) {
238: len = fread(buf, 1, sizeof buf, ifp);
239: if (len == 0)
240: break;
241: if (fwrite(buf, 1, len, ofp) != len)
242: break;
243: }
1.66 sthen 244: pclose(ifp);
1.65 sthen 245: }
246: }
247:
1.48 ray 248: /*
249: * Execute an editor on the specified pathname, which is interpreted
250: * from the shell. This means flags may be included.
251: *
252: * Returns -1 on error, or the exit value on success.
253: */
1.15 deraadt 254: int
1.42 ray 255: editit(const char *pathname)
1.15 deraadt 256: {
1.19 deraadt 257: char *argp[] = {"sh", "-c", NULL, NULL}, *ed, *p;
1.52 deraadt 258: sig_t sighup, sigint, sigquit, sigchld;
1.42 ray 259: pid_t pid;
1.52 deraadt 260: int saved_errno, st, ret = -1;
1.15 deraadt 261:
1.26 ray 262: ed = getenv("VISUAL");
263: if (ed == NULL || ed[0] == '\0')
264: ed = getenv("EDITOR");
265: if (ed == NULL || ed[0] == '\0')
1.15 deraadt 266: ed = _PATH_VI;
1.38 ray 267: if (asprintf(&p, "%s %s", ed, pathname) == -1)
1.18 ray 268: return (-1);
1.15 deraadt 269: argp[2] = p;
270:
1.25 ray 271: sighup = signal(SIGHUP, SIG_IGN);
272: sigint = signal(SIGINT, SIG_IGN);
273: sigquit = signal(SIGQUIT, SIG_IGN);
1.52 deraadt 274: sigchld = signal(SIGCHLD, SIG_DFL);
1.49 ray 275: if ((pid = fork()) == -1)
276: goto fail;
1.15 deraadt 277: if (pid == 0) {
278: execv(_PATH_BSHELL, argp);
279: _exit(127);
280: }
1.46 ray 281: while (waitpid(pid, &st, 0) == -1)
282: if (errno != EINTR)
283: goto fail;
1.52 deraadt 284: if (!WIFEXITED(st))
1.48 ray 285: errno = EINTR;
1.52 deraadt 286: else
287: ret = WEXITSTATUS(st);
1.46 ray 288:
289: fail:
290: saved_errno = errno;
291: (void)signal(SIGHUP, sighup);
292: (void)signal(SIGINT, sigint);
293: (void)signal(SIGQUIT, sigquit);
1.52 deraadt 294: (void)signal(SIGCHLD, sigchld);
1.46 ray 295: free(p);
296: errno = saved_errno;
1.52 deraadt 297: return (ret);
1.15 deraadt 298: }
1.12 tedu 299:
1.1 ray 300: int
301: prompt(void)
302: {
303: int c, ret;
304:
305: fpurge(stdin);
306: fprintf(stderr, "a)bort, e)dit, or s)end: ");
307: fflush(stderr);
308: ret = getchar();
309: if (ret == EOF || ret == '\n')
310: return (ret);
311: do {
312: c = getchar();
313: } while (c != EOF && c != '\n');
314: return (ret);
315: }
316:
317: int
1.38 ray 318: sendmail(const char *pathname)
1.1 ray 319: {
320: int filedes[2];
321:
322: if (pipe(filedes) == -1) {
1.38 ray 323: warn("pipe: unsent report in %s", pathname);
1.1 ray 324: return (-1);
325: }
326: switch (fork()) {
327: case -1:
328: warn("fork error: unsent report in %s",
1.38 ray 329: pathname);
1.1 ray 330: return (-1);
331: case 0:
332: close(filedes[1]);
333: if (dup2(filedes[0], STDIN_FILENO) == -1) {
334: warn("dup2 error: unsent report in %s",
1.38 ray 335: pathname);
1.1 ray 336: return (-1);
337: }
338: close(filedes[0]);
1.56 chl 339: execl(_PATH_SENDMAIL, "sendmail",
1.2 deraadt 340: "-oi", "-t", (void *)NULL);
1.1 ray 341: warn("sendmail error: unsent report in %s",
1.38 ray 342: pathname);
1.1 ray 343: return (-1);
344: default:
345: close(filedes[0]);
346: /* Pipe into sendmail. */
1.38 ray 347: if (send_file(pathname, filedes[1]) == -1) {
1.1 ray 348: warn("send_file error: unsent report in %s",
1.38 ray 349: pathname);
1.1 ray 350: return (-1);
351: }
352: close(filedes[1]);
353: wait(NULL);
354: break;
355: }
356: return (0);
357: }
358:
1.12 tedu 359: void
1.1 ray 360: init(void)
361: {
1.62 ray 362: size_t len;
1.1 ray 363: int sysname[2];
1.62 ray 364: char *cp;
1.1 ray 365:
1.19 deraadt 366: if ((pw = getpwuid(getuid())) == NULL)
1.12 tedu 367: err(1, "getpwuid");
1.1 ray 368:
369: sysname[0] = CTL_KERN;
370: sysname[1] = KERN_OSTYPE;
371: len = sizeof(os) - 1;
1.19 deraadt 372: if (sysctl(sysname, 2, &os, &len, NULL, 0) == -1)
1.12 tedu 373: err(1, "sysctl");
1.1 ray 374:
375: sysname[0] = CTL_KERN;
376: sysname[1] = KERN_OSRELEASE;
377: len = sizeof(rel) - 1;
1.19 deraadt 378: if (sysctl(sysname, 2, &rel, &len, NULL, 0) == -1)
1.12 tedu 379: err(1, "sysctl");
1.1 ray 380:
1.14 deraadt 381: sysname[0] = CTL_KERN;
382: sysname[1] = KERN_VERSION;
383: len = sizeof(details) - 1;
1.19 deraadt 384: if (sysctl(sysname, 2, &details, &len, NULL, 0) == -1)
1.14 deraadt 385: err(1, "sysctl");
386:
387: cp = strchr(details, '\n');
388: if (cp) {
389: cp++;
390: if (*cp)
391: *cp++ = '\t';
392: if (*cp)
393: *cp++ = '\t';
394: if (*cp)
395: *cp++ = '\t';
396: }
397:
1.1 ray 398: sysname[0] = CTL_HW;
399: sysname[1] = HW_MACHINE;
400: len = sizeof(mach) - 1;
1.19 deraadt 401: if (sysctl(sysname, 2, &mach, &len, NULL, 0) == -1)
1.12 tedu 402: err(1, "sysctl");
1.1 ray 403: }
404:
405: int
406: send_file(const char *file, int dst)
407: {
1.2 deraadt 408: size_t len;
1.54 ray 409: char *buf, *lbuf;
1.1 ray 410: FILE *fp;
1.54 ray 411: int rval = -1, saved_errno;
1.1 ray 412:
413: if ((fp = fopen(file, "r")) == NULL)
414: return (-1);
1.54 ray 415: lbuf = NULL;
1.1 ray 416: while ((buf = fgetln(fp, &len))) {
1.55 ray 417: if (buf[len - 1] == '\n') {
1.54 ray 418: buf[len - 1] = '\0';
1.55 ray 419: --len;
420: } else {
1.54 ray 421: /* EOF without EOL, copy and add the NUL */
422: if ((lbuf = malloc(len + 1)) == NULL)
423: goto end;
424: memcpy(lbuf, buf, len);
425: lbuf[len] = '\0';
426: buf = lbuf;
427: }
428:
1.1 ray 429: /* Skip lines starting with "SENDBUG". */
1.54 ray 430: if (strncmp(buf, "SENDBUG", sizeof("SENDBUG") - 1) == 0)
1.1 ray 431: continue;
432: while (len) {
1.14 deraadt 433: char *sp = NULL, *ep = NULL;
1.1 ray 434: size_t copylen;
435:
1.54 ray 436: if ((sp = strchr(buf, '<')) != NULL) {
437: size_t i;
438:
439: for (i = 0; i < sizeof(comment) / sizeof(*comment); ++i) {
440: size_t commentlen = strlen(comment[i]);
441:
442: if (strncmp(sp, comment[i], commentlen) == 0) {
443: ep = sp + commentlen - 1;
444: break;
445: }
446: }
447: }
1.1 ray 448: /* Length of string before comment. */
1.4 ray 449: if (ep)
450: copylen = sp - buf;
451: else
452: copylen = len;
1.55 ray 453: if (atomicio(vwrite, dst, buf, copylen) != copylen)
1.54 ray 454: goto end;
1.1 ray 455: if (!ep)
456: break;
457: /* Skip comment. */
458: len -= ep - buf + 1;
459: buf = ep + 1;
460: }
1.55 ray 461: if (atomicio(vwrite, dst, "\n", 1) != 1)
462: goto end;
1.1 ray 463: }
1.54 ray 464: rval = 0;
465: end:
466: saved_errno = errno;
467: free(lbuf);
1.1 ray 468: fclose(fp);
1.54 ray 469: errno = saved_errno;
470: return (rval);
1.39 ray 471: }
472:
473: /*
474: * Does line start with `s' and end with non-comment and non-whitespace?
1.40 ray 475: * Note: Does not treat `line' as a C string.
1.39 ray 476: */
477: int
1.40 ray 478: matchline(const char *s, const char *line, size_t linelen)
1.39 ray 479: {
480: size_t slen;
1.53 ray 481: int iscomment;
1.39 ray 482:
483: slen = strlen(s);
484: /* Is line shorter than string? */
485: if (linelen <= slen)
486: return (0);
487: /* Does line start with string? */
488: if (memcmp(line, s, slen) != 0)
489: return (0);
490: /* Does line contain anything but comments and whitespace? */
491: line += slen;
492: linelen -= slen;
1.53 ray 493: iscomment = 0;
1.39 ray 494: while (linelen) {
1.53 ray 495: if (iscomment) {
1.39 ray 496: if (*line == '>')
1.53 ray 497: iscomment = 0;
1.39 ray 498: } else if (*line == '<')
1.53 ray 499: iscomment = 1;
1.40 ray 500: else if (!isspace((unsigned char)*line))
1.39 ray 501: return (1);
502: ++line;
503: --linelen;
504: }
505: return (0);
506: }
507:
508: /*
509: * Are all required fields filled out?
510: */
511: int
512: checkfile(const char *pathname)
513: {
514: FILE *fp;
515: size_t len;
1.60 ray 516: int category = 0, synopsis = 0;
1.39 ray 517: char *buf;
518:
519: if ((fp = fopen(pathname, "r")) == NULL) {
520: warn("%s", pathname);
521: return (0);
522: }
523: while ((buf = fgetln(fp, &len))) {
1.60 ray 524: if (matchline(">Category:", buf, len))
525: category = 1;
526: else if (matchline(">Synopsis:", buf, len))
527: synopsis = 1;
1.39 ray 528: }
529: fclose(fp);
1.60 ray 530: return (category && synopsis);
1.1 ray 531: }
532:
533: void
534: template(FILE *fp)
535: {
536: fprintf(fp, "SENDBUG: -*- sendbug -*-\n");
1.19 deraadt 537: fprintf(fp, "SENDBUG: Lines starting with `SENDBUG' will"
1.54 ray 538: " be removed automatically.\n");
1.1 ray 539: fprintf(fp, "SENDBUG:\n");
1.60 ray 540: fprintf(fp, "SENDBUG: Choose from the following categories:\n");
541: fprintf(fp, "SENDBUG:\n");
542: fprintf(fp, "SENDBUG: %s\n", categories);
543: fprintf(fp, "SENDBUG:\n");
544: fprintf(fp, "SENDBUG:\n");
1.67 phessler 545: fprintf(fp, "To: %s\n", "bugs@openbsd.org");
1.1 ray 546: fprintf(fp, "Subject: \n");
547: fprintf(fp, "From: %s\n", pw->pw_name);
1.34 ray 548: fprintf(fp, "Cc: %s\n", pw->pw_name);
1.1 ray 549: fprintf(fp, "Reply-To: %s\n", pw->pw_name);
550: fprintf(fp, "\n");
1.53 ray 551: fprintf(fp, ">Synopsis:\t%s\n", comment[0]);
1.60 ray 552: fprintf(fp, ">Category:\t%s\n", comment[1]);
1.1 ray 553: fprintf(fp, ">Environment:\n");
554: fprintf(fp, "\tSystem : %s %s\n", os, rel);
1.14 deraadt 555: fprintf(fp, "\tDetails : %s\n", details);
1.1 ray 556: fprintf(fp, "\tArchitecture: %s.%s\n", os, mach);
557: fprintf(fp, "\tMachine : %s\n", mach);
558: fprintf(fp, ">Description:\n");
1.60 ray 559: fprintf(fp, "\t%s\n", comment[2]);
1.1 ray 560: fprintf(fp, ">How-To-Repeat:\n");
1.60 ray 561: fprintf(fp, "\t%s\n", comment[3]);
1.1 ray 562: fprintf(fp, ">Fix:\n");
1.60 ray 563: fprintf(fp, "\t%s\n", comment[4]);
1.47 ray 564:
1.61 ray 565: if (!Dflag) {
566: int root;
567:
568: fprintf(fp, "\n");
569: root = !geteuid();
570: if (!root)
571: fprintf(fp, "SENDBUG: Run sendbug as root "
572: "if this is an ACPI report!\n");
1.65 sthen 573: fprintf(fp, "SENDBUG: dmesg%s and usbdevs are attached.\n"
574: "SENDBUG: Feel free to delete or use the -D flag if they "
575: "contain sensitive information.\n",
576: root ? ", pcidump, acpidump" : "");
1.61 ray 577: fputs("\ndmesg:\n", fp);
1.47 ray 578: dmesg(fp);
1.65 sthen 579: fputs("\nusbdevs:\n", fp);
580: usbdevs(fp);
1.61 ray 581: if (root)
582: hwdump(fp);
583: }
584: }
585:
586: void
587: hwdump(FILE *ofp)
588: {
589: char buf[BUFSIZ];
590: FILE *ifp;
591: char *cmd, *acpidir;
592: size_t len;
593:
594: if (gethostname(buf, sizeof(buf)) == -1)
595: err(1, "gethostname");
596: buf[strcspn(buf, ".")] = '\0';
597:
598: if (asprintf(&acpidir, "%s%sp.XXXXXXXXXX", tmpdir,
599: tmpdir[strlen(tmpdir) - 1] == '/' ? "" : "/") == -1)
600: err(1, "asprintf");
601: if (mkdtemp(acpidir) == NULL)
602: err(1, "mkdtemp");
603:
604: if (asprintf(&cmd, "echo \"\\npcidump:\"; pcidump -xxv; "
605: "echo \"\\nacpidump:\"; cd %s && acpidump -o %s; "
606: "for i in *; do b64encode $i $i; done; rm -rf %s",
607: acpidir, buf, acpidir) == -1)
608: err(1, "asprintf");
609:
610: if ((ifp = popen(cmd, "r")) != NULL) {
611: while (!feof(ifp)) {
612: len = fread(buf, 1, sizeof buf, ifp);
613: if (len == 0)
614: break;
615: if (fwrite(buf, 1, len, ofp) != len)
616: break;
617: }
1.66 sthen 618: pclose(ifp);
1.61 ray 619: }
620: free(cmd);
1.64 deraadt 621: free(acpidir);
1.61 ray 622: }
623:
624: void
625: debase(void)
626: {
627: char buf[BUFSIZ];
628: FILE *fp = NULL;
629: size_t len;
630:
631: while (fgets(buf, sizeof(buf), stdin) != NULL) {
632: len = strlen(buf);
633: if (!strncmp(buf, BEGIN64, sizeof(BEGIN64) - 1)) {
634: if (fp)
635: errx(1, "double begin");
636: fp = popen("b64decode", "w");
637: if (!fp)
638: errx(1, "popen b64decode");
639: }
640: if (fp && fwrite(buf, 1, len, fp) != len)
641: errx(1, "pipe error");
642: if (!strncmp(buf, END64, sizeof(END64) - 1)) {
643: if (pclose(fp) == -1)
644: errx(1, "pclose b64decode");
645: fp = NULL;
646: }
647: }
1.1 ray 648: }