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