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