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