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