Annotation of src/usr.bin/sendbug/sendbug.c, Revision 1.56
1.56 ! chl 1: /* $OpenBSD: sendbug.c,v 1.55 2008/04/19 09:22:31 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.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:
42: const char *categories = "system user library documentation ports kernel "
43: "alpha amd64 arm i386 m68k m88k mips ppc sgi 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.47 ray 61: int Dflag, 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.12 tedu 100: init();
1.3 deraadt 101: template(stdout);
102: exit(0);
1.5 deraadt 103: case 'V':
104: printf("%s\n", version);
105: exit(0);
1.3 deraadt 106: default:
107: usage();
1.7 deraadt 108: }
1.35 ray 109: argc -= optind;
110: argv += optind;
1.7 deraadt 111:
1.37 ray 112: if (argc > 0)
1.3 deraadt 113: usage();
1.1 ray 114:
115: if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
116: tmpdir = _PATH_TMP;
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:
203: fputs("\n"
1.53 ray 204: "SENDBUG: dmesg is attached.\n"
205: "SENDBUG: Feel free to delete or use the -D flag if it contains "
206: "sensitive information.\n", fp);
1.51 ray 207: /* Find last dmesg. */
1.36 ray 208: for (;;) {
209: off_t o;
210:
211: o = ftello(dfp);
212: if (fgets(buf, sizeof(buf), dfp) == NULL)
213: break;
1.51 ray 214: if (!strncmp(DMESG_START, buf, sizeof(DMESG_START) - 1))
1.36 ray 215: offset = o;
216: }
217: if (offset != -1) {
218: size_t len;
219:
220: clearerr(dfp);
221: fseeko(dfp, offset, SEEK_SET);
222: while (offset != -1 && !feof(dfp)) {
223: len = fread(buf, 1, sizeof buf, dfp);
224: if (len == 0)
225: break;
226: if (fwrite(buf, 1, len, fp) != len)
227: break;
228: }
229: }
230: fclose(dfp);
1.1 ray 231: }
232:
1.48 ray 233: /*
234: * Execute an editor on the specified pathname, which is interpreted
235: * from the shell. This means flags may be included.
236: *
237: * Returns -1 on error, or the exit value on success.
238: */
1.15 deraadt 239: int
1.42 ray 240: editit(const char *pathname)
1.15 deraadt 241: {
1.19 deraadt 242: char *argp[] = {"sh", "-c", NULL, NULL}, *ed, *p;
1.52 deraadt 243: sig_t sighup, sigint, sigquit, sigchld;
1.42 ray 244: pid_t pid;
1.52 deraadt 245: int saved_errno, st, ret = -1;
1.15 deraadt 246:
1.26 ray 247: ed = getenv("VISUAL");
248: if (ed == NULL || ed[0] == '\0')
249: ed = getenv("EDITOR");
250: if (ed == NULL || ed[0] == '\0')
1.15 deraadt 251: ed = _PATH_VI;
1.38 ray 252: if (asprintf(&p, "%s %s", ed, pathname) == -1)
1.18 ray 253: return (-1);
1.15 deraadt 254: argp[2] = p;
255:
1.25 ray 256: sighup = signal(SIGHUP, SIG_IGN);
257: sigint = signal(SIGINT, SIG_IGN);
258: sigquit = signal(SIGQUIT, SIG_IGN);
1.52 deraadt 259: sigchld = signal(SIGCHLD, SIG_DFL);
1.49 ray 260: if ((pid = fork()) == -1)
261: goto fail;
1.15 deraadt 262: if (pid == 0) {
263: execv(_PATH_BSHELL, argp);
264: _exit(127);
265: }
1.46 ray 266: while (waitpid(pid, &st, 0) == -1)
267: if (errno != EINTR)
268: goto fail;
1.52 deraadt 269: if (!WIFEXITED(st))
1.48 ray 270: errno = EINTR;
1.52 deraadt 271: else
272: ret = WEXITSTATUS(st);
1.46 ray 273:
274: fail:
275: saved_errno = errno;
276: (void)signal(SIGHUP, sighup);
277: (void)signal(SIGINT, sigint);
278: (void)signal(SIGQUIT, sigquit);
1.52 deraadt 279: (void)signal(SIGCHLD, sigchld);
1.46 ray 280: free(p);
281: errno = saved_errno;
1.52 deraadt 282: return (ret);
1.15 deraadt 283: }
1.12 tedu 284:
1.1 ray 285: int
286: prompt(void)
287: {
288: int c, ret;
289:
290: fpurge(stdin);
291: fprintf(stderr, "a)bort, e)dit, or s)end: ");
292: fflush(stderr);
293: ret = getchar();
294: if (ret == EOF || ret == '\n')
295: return (ret);
296: do {
297: c = getchar();
298: } while (c != EOF && c != '\n');
299: return (ret);
300: }
301:
302: int
1.38 ray 303: sendmail(const char *pathname)
1.1 ray 304: {
305: int filedes[2];
306:
307: if (pipe(filedes) == -1) {
1.38 ray 308: warn("pipe: unsent report in %s", pathname);
1.1 ray 309: return (-1);
310: }
311: switch (fork()) {
312: case -1:
313: warn("fork error: unsent report in %s",
1.38 ray 314: pathname);
1.1 ray 315: return (-1);
316: case 0:
317: close(filedes[1]);
318: if (dup2(filedes[0], STDIN_FILENO) == -1) {
319: warn("dup2 error: unsent report in %s",
1.38 ray 320: pathname);
1.1 ray 321: return (-1);
322: }
323: close(filedes[0]);
1.56 ! chl 324: execl(_PATH_SENDMAIL, "sendmail",
1.2 deraadt 325: "-oi", "-t", (void *)NULL);
1.1 ray 326: warn("sendmail error: unsent report in %s",
1.38 ray 327: pathname);
1.1 ray 328: return (-1);
329: default:
330: close(filedes[0]);
331: /* Pipe into sendmail. */
1.38 ray 332: if (send_file(pathname, filedes[1]) == -1) {
1.1 ray 333: warn("send_file error: unsent report in %s",
1.38 ray 334: pathname);
1.1 ray 335: return (-1);
336: }
337: close(filedes[1]);
338: wait(NULL);
339: break;
340: }
341: return (0);
342: }
343:
1.12 tedu 344: void
1.1 ray 345: init(void)
346: {
1.33 ray 347: size_t amp, len, gecoslen, namelen;
1.1 ray 348: int sysname[2];
1.33 ray 349: char ch, *cp;
1.1 ray 350:
1.19 deraadt 351: if ((pw = getpwuid(getuid())) == NULL)
1.12 tedu 352: err(1, "getpwuid");
1.13 ray 353: namelen = strlen(pw->pw_name);
1.1 ray 354:
1.31 moritz 355: /* Count number of '&'. */
1.33 ray 356: for (amp = 0, cp = pw->pw_gecos; *cp && *cp != ','; ++cp)
357: if (*cp == '&')
1.31 moritz 358: ++amp;
1.33 ray 359:
360: /* Truncate gecos to full name. */
361: gecoslen = cp - pw->pw_gecos;
362: pw->pw_gecos[gecoslen] = '\0';
363:
1.31 moritz 364: /* Expanded str = orig str - '&' chars + concatenated logins. */
1.33 ray 365: len = gecoslen - amp + (amp * namelen) + 1;
366: if ((fullname = malloc(len)) == NULL)
1.12 tedu 367: err(1, "malloc");
1.19 deraadt 368:
1.33 ray 369: /* Upper case first char of login. */
370: ch = pw->pw_name[0];
371: pw->pw_name[0] = toupper((unsigned char)pw->pw_name[0]);
372:
373: cp = pw->pw_gecos;
374: fullname[0] = '\0';
375: while (cp != NULL) {
376: char *token;
377:
378: token = strsep(&cp, "&");
379: if (token != pw->pw_gecos &&
380: strlcat(fullname, pw->pw_name, len) >= len)
381: errx(1, "truncated string");
382: if (strlcat(fullname, token, len) >= len)
383: errx(1, "truncated string");
1.13 ray 384: }
1.33 ray 385: /* Restore case of first char of login. */
386: pw->pw_name[0] = ch;
1.1 ray 387:
388: sysname[0] = CTL_KERN;
389: sysname[1] = KERN_OSTYPE;
390: len = sizeof(os) - 1;
1.19 deraadt 391: if (sysctl(sysname, 2, &os, &len, NULL, 0) == -1)
1.12 tedu 392: err(1, "sysctl");
1.1 ray 393:
394: sysname[0] = CTL_KERN;
395: sysname[1] = KERN_OSRELEASE;
396: len = sizeof(rel) - 1;
1.19 deraadt 397: if (sysctl(sysname, 2, &rel, &len, NULL, 0) == -1)
1.12 tedu 398: err(1, "sysctl");
1.1 ray 399:
1.14 deraadt 400: sysname[0] = CTL_KERN;
401: sysname[1] = KERN_VERSION;
402: len = sizeof(details) - 1;
1.19 deraadt 403: if (sysctl(sysname, 2, &details, &len, NULL, 0) == -1)
1.14 deraadt 404: err(1, "sysctl");
405:
406: cp = strchr(details, '\n');
407: if (cp) {
408: cp++;
409: if (*cp)
410: *cp++ = '\t';
411: if (*cp)
412: *cp++ = '\t';
413: if (*cp)
414: *cp++ = '\t';
415: }
416:
1.1 ray 417: sysname[0] = CTL_HW;
418: sysname[1] = HW_MACHINE;
419: len = sizeof(mach) - 1;
1.19 deraadt 420: if (sysctl(sysname, 2, &mach, &len, NULL, 0) == -1)
1.12 tedu 421: err(1, "sysctl");
1.1 ray 422: }
423:
424: int
425: send_file(const char *file, int dst)
426: {
1.2 deraadt 427: size_t len;
1.54 ray 428: char *buf, *lbuf;
1.1 ray 429: FILE *fp;
1.54 ray 430: int rval = -1, saved_errno;
1.1 ray 431:
432: if ((fp = fopen(file, "r")) == NULL)
433: return (-1);
1.54 ray 434: lbuf = NULL;
1.1 ray 435: while ((buf = fgetln(fp, &len))) {
1.55 ray 436: if (buf[len - 1] == '\n') {
1.54 ray 437: buf[len - 1] = '\0';
1.55 ray 438: --len;
439: } else {
1.54 ray 440: /* EOF without EOL, copy and add the NUL */
441: if ((lbuf = malloc(len + 1)) == NULL)
442: goto end;
443: memcpy(lbuf, buf, len);
444: lbuf[len] = '\0';
445: buf = lbuf;
446: }
447:
1.1 ray 448: /* Skip lines starting with "SENDBUG". */
1.54 ray 449: if (strncmp(buf, "SENDBUG", sizeof("SENDBUG") - 1) == 0)
1.1 ray 450: continue;
451: while (len) {
1.14 deraadt 452: char *sp = NULL, *ep = NULL;
1.1 ray 453: size_t copylen;
454:
1.54 ray 455: if ((sp = strchr(buf, '<')) != NULL) {
456: size_t i;
457:
458: for (i = 0; i < sizeof(comment) / sizeof(*comment); ++i) {
459: size_t commentlen = strlen(comment[i]);
460:
461: if (strncmp(sp, comment[i], commentlen) == 0) {
462: ep = sp + commentlen - 1;
463: break;
464: }
465: }
466: }
1.1 ray 467: /* Length of string before comment. */
1.4 ray 468: if (ep)
469: copylen = sp - buf;
470: else
471: copylen = len;
1.55 ray 472: if (atomicio(vwrite, dst, buf, copylen) != copylen)
1.54 ray 473: goto end;
1.1 ray 474: if (!ep)
475: break;
476: /* Skip comment. */
477: len -= ep - buf + 1;
478: buf = ep + 1;
479: }
1.55 ray 480: if (atomicio(vwrite, dst, "\n", 1) != 1)
481: goto end;
1.1 ray 482: }
1.54 ray 483: rval = 0;
484: end:
485: saved_errno = errno;
486: free(lbuf);
1.1 ray 487: fclose(fp);
1.54 ray 488: errno = saved_errno;
489: return (rval);
1.39 ray 490: }
491:
492: /*
493: * Does line start with `s' and end with non-comment and non-whitespace?
1.40 ray 494: * Note: Does not treat `line' as a C string.
1.39 ray 495: */
496: int
1.40 ray 497: matchline(const char *s, const char *line, size_t linelen)
1.39 ray 498: {
499: size_t slen;
1.53 ray 500: int iscomment;
1.39 ray 501:
502: slen = strlen(s);
503: /* Is line shorter than string? */
504: if (linelen <= slen)
505: return (0);
506: /* Does line start with string? */
507: if (memcmp(line, s, slen) != 0)
508: return (0);
509: /* Does line contain anything but comments and whitespace? */
510: line += slen;
511: linelen -= slen;
1.53 ray 512: iscomment = 0;
1.39 ray 513: while (linelen) {
1.53 ray 514: if (iscomment) {
1.39 ray 515: if (*line == '>')
1.53 ray 516: iscomment = 0;
1.39 ray 517: } else if (*line == '<')
1.53 ray 518: iscomment = 1;
1.40 ray 519: else if (!isspace((unsigned char)*line))
1.39 ray 520: return (1);
521: ++line;
522: --linelen;
523: }
524: return (0);
525: }
526:
527: /*
528: * Are all required fields filled out?
529: */
530: int
531: checkfile(const char *pathname)
532: {
533: FILE *fp;
534: size_t len;
535: int category, class, priority, release, severity, synopsis;
536: char *buf;
537:
538: if ((fp = fopen(pathname, "r")) == NULL) {
539: warn("%s", pathname);
540: return (0);
541: }
542: category = class = priority = release = severity = synopsis = 0;
543: while ((buf = fgetln(fp, &len))) {
544: if (matchline(">Category:", buf, len))
545: category = 1;
546: else if (matchline(">Class:", buf, len))
547: class = 1;
548: else if (matchline(">Priority:", buf, len))
549: priority = 1;
550: else if (matchline(">Release:", buf, len))
551: release = 1;
552: else if (matchline(">Severity:", buf, len))
553: severity = 1;
554: else if (matchline(">Synopsis:", buf, len))
555: synopsis = 1;
556: }
557: fclose(fp);
558: return (category && class && priority && release && severity &&
559: synopsis);
1.1 ray 560: }
561:
562: void
563: template(FILE *fp)
564: {
565: fprintf(fp, "SENDBUG: -*- sendbug -*-\n");
1.19 deraadt 566: fprintf(fp, "SENDBUG: Lines starting with `SENDBUG' will"
1.54 ray 567: " be removed automatically.\n");
1.1 ray 568: fprintf(fp, "SENDBUG:\n");
569: fprintf(fp, "SENDBUG: Choose from the following categories:\n");
570: fprintf(fp, "SENDBUG:\n");
571: fprintf(fp, "SENDBUG: %s\n", categories);
572: fprintf(fp, "SENDBUG:\n");
573: fprintf(fp, "SENDBUG:\n");
574: fprintf(fp, "To: %s\n", "gnats@openbsd.org");
575: fprintf(fp, "Subject: \n");
576: fprintf(fp, "From: %s\n", pw->pw_name);
1.34 ray 577: fprintf(fp, "Cc: %s\n", pw->pw_name);
1.1 ray 578: fprintf(fp, "Reply-To: %s\n", pw->pw_name);
1.5 deraadt 579: fprintf(fp, "X-sendbug-version: %s\n", version);
1.1 ray 580: fprintf(fp, "\n");
581: fprintf(fp, "\n");
582: fprintf(fp, ">Submitter-Id:\tnet\n");
583: fprintf(fp, ">Originator:\t%s\n", fullname);
584: fprintf(fp, ">Organization:\n");
585: fprintf(fp, "net\n");
1.53 ray 586: fprintf(fp, ">Synopsis:\t%s\n", comment[0]);
587: fprintf(fp, ">Severity:\t%s\n", comment[1]);
588: fprintf(fp, ">Priority:\t%s\n", comment[2]);
589: fprintf(fp, ">Category:\t%s\n", comment[3]);
590: fprintf(fp, ">Class:\t\t%s\n", comment[4]);
591: fprintf(fp, ">Release:\t%s\n", comment[5]);
1.1 ray 592: fprintf(fp, ">Environment:\n");
1.53 ray 593: fprintf(fp, "\t%s\n", comment[6]);
1.1 ray 594: fprintf(fp, "\tSystem : %s %s\n", os, rel);
1.14 deraadt 595: fprintf(fp, "\tDetails : %s\n", details);
1.1 ray 596: fprintf(fp, "\tArchitecture: %s.%s\n", os, mach);
597: fprintf(fp, "\tMachine : %s\n", mach);
598: fprintf(fp, ">Description:\n");
1.53 ray 599: fprintf(fp, "\t%s\n", comment[7]);
1.1 ray 600: fprintf(fp, ">How-To-Repeat:\n");
1.53 ray 601: fprintf(fp, "\t%s\n", comment[8]);
1.1 ray 602: fprintf(fp, ">Fix:\n");
1.53 ray 603: fprintf(fp, "\t%s\n", comment[9]);
1.47 ray 604:
605: if (!Dflag)
606: dmesg(fp);
1.1 ray 607: }