Annotation of src/usr.bin/sendbug/sendbug.c, Revision 1.23
1.23 ! ray 1: /* $OpenBSD: sendbug.c,v 1.22 2007/03/26 05:17:53 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.21 deraadt 30: int editit(char *);
31: void init(void);
32: int prompt(void);
33: int send_file(const char *, int dst);
34: int sendmail(const char *);
35: void template(FILE *);
1.1 ray 36:
37: const char *categories = "system user library documentation ports kernel "
38: "alpha amd64 arm i386 m68k m88k mips ppc sgi sparc sparc64 vax";
1.11 deraadt 39: char *version = "4.2";
40:
41: struct passwd *pw;
1.14 deraadt 42: char os[BUFSIZ], rel[BUFSIZ], mach[BUFSIZ], details[BUFSIZ];
1.21 deraadt 43: char *fullname, *tmppath;
1.12 tedu 44: int wantcleanup;
1.11 deraadt 45:
1.12 tedu 46: __dead void
1.3 deraadt 47: usage(void)
48: {
1.6 deraadt 49: fprintf(stderr, "usage: sendbug [-LPV]\n");
1.12 tedu 50: exit(1);
51: }
52:
53: void
54: cleanup()
55: {
56: if (wantcleanup && tmppath && unlink(tmppath) == -1)
57: warn("unlink");
1.3 deraadt 58: }
59:
1.12 tedu 60:
1.1 ray 61: int
62: main(int argc, char *argv[])
63: {
1.19 deraadt 64: int ch, c, fd, ret = 1;
1.15 deraadt 65: const char *tmpdir;
1.19 deraadt 66: struct stat sb;
1.15 deraadt 67: char *pr_form;
1.1 ray 68: time_t mtime;
1.2 deraadt 69: FILE *fp;
1.3 deraadt 70:
1.5 deraadt 71: while ((ch = getopt(argc, argv, "LPV")) != -1)
1.3 deraadt 72: switch (ch) {
73: case 'L':
74: printf("Known categories:\n");
75: printf("%s\n\n", categories);
76: exit(0);
77: case 'P':
1.12 tedu 78: init();
1.3 deraadt 79: template(stdout);
80: exit(0);
1.5 deraadt 81: case 'V':
82: printf("%s\n", version);
83: exit(0);
1.3 deraadt 84: default:
85: usage();
1.7 deraadt 86: }
87:
1.3 deraadt 88: if (argc > 1) {
89: usage();
90: }
1.1 ray 91:
92: if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
93: tmpdir = _PATH_TMP;
1.9 ray 94: if (asprintf(&tmppath, "%s%sp.XXXXXXXXXX", tmpdir,
1.19 deraadt 95: tmpdir[strlen(tmpdir) - 1] == '/' ? "" : "/") == -1)
1.12 tedu 96: err(1, "asprintf");
1.1 ray 97: if ((fd = mkstemp(tmppath)) == -1)
98: err(1, "mkstemp");
1.12 tedu 99: wantcleanup = 1;
100: atexit(cleanup);
1.19 deraadt 101: if ((fp = fdopen(fd, "w+")) == NULL)
1.12 tedu 102: err(1, "fdopen");
1.1 ray 103:
1.12 tedu 104: init();
1.1 ray 105:
1.10 deraadt 106: pr_form = getenv("PR_FORM");
107: if (pr_form) {
108: char buf[BUFSIZ];
109: size_t len;
110: FILE *frfp;
111:
112: frfp = fopen(pr_form, "r");
113: if (frfp == NULL) {
114: fprintf(stderr, "sendbug: can't seem to read your"
115: " template file (`%s'), ignoring PR_FORM\n",
116: pr_form);
117: template(fp);
118: } else {
119: while (!feof(frfp)) {
120: len = fread(buf, 1, sizeof buf, frfp);
121: if (len == 0)
122: break;
123: if (fwrite(buf, 1, len, fp) != len)
124: break;
125: }
126: fclose(frfp);
127: }
128: } else
129: template(fp);
1.1 ray 130:
1.19 deraadt 131: if (fflush(fp) == EOF || fstat(fd, &sb) == -1 || fclose(fp) == EOF)
1.12 tedu 132: err(1, "error creating template");
1.1 ray 133: mtime = sb.st_mtime;
134:
135: edit:
1.15 deraadt 136: editit(tmppath);
1.1 ray 137:
1.19 deraadt 138: if (stat(tmppath, &sb) == -1)
1.12 tedu 139: err(1, "stat");
1.19 deraadt 140: if (mtime == sb.st_mtime)
1.12 tedu 141: errx(1, "report unchanged, nothing sent");
1.1 ray 142:
143: prompt:
144: c = prompt();
145: switch (c) {
1.5 deraadt 146: case 'a':
147: case EOF:
1.12 tedu 148: wantcleanup = 0;
149: errx(1, "unsent report in %s", tmppath);
1.1 ray 150: case 'e':
151: goto edit;
152: case 's':
153: if (sendmail(tmppath) == -1)
154: goto quit;
155: break;
156: default:
157: goto prompt;
158: }
159:
160: ret = 0;
1.12 tedu 161: quit:
1.1 ray 162: return (ret);
163: }
164:
1.15 deraadt 165: int
166: editit(char *tmpfile)
167: {
1.19 deraadt 168: char *argp[] = {"sh", "-c", NULL, NULL}, *ed, *p;
1.15 deraadt 169: pid_t pid, xpid;
1.21 deraadt 170: int st;
1.15 deraadt 171:
172: if ((ed = getenv("EDITOR")) == (char *)0)
173: ed = _PATH_VI;
174: if (asprintf(&p, "%s %s", ed, tmpfile) == -1)
1.18 ray 175: return (-1);
1.15 deraadt 176: argp[2] = p;
177:
178: top:
179: (void)signal(SIGHUP, SIG_IGN);
180: (void)signal(SIGINT, SIG_IGN);
181: (void)signal(SIGQUIT, SIG_IGN);
1.23 ! ray 182: if ((pid = fork()) == -1) {
1.16 ray 183: int saved_errno = errno;
184:
1.15 deraadt 185: (void)signal(SIGHUP, SIG_DFL);
186: (void)signal(SIGINT, SIG_DFL);
187: (void)signal(SIGQUIT, SIG_DFL);
1.16 ray 188: if (saved_errno == EAGAIN) {
1.15 deraadt 189: sleep(1);
190: goto top;
191: }
192: perror("fork");
193: free(p);
1.18 ray 194: return (-1);
1.15 deraadt 195: }
196: if (pid == 0) {
197: execv(_PATH_BSHELL, argp);
198: _exit(127);
199: }
200: free(p);
201: for (;;) {
1.21 deraadt 202: xpid = waitpid(pid, (int *)&st, WUNTRACED);
203: if (xpid == -1) {
204: if (errno != EINTR) {
205: warn("waidpid");
206: return (-1);
207: }
208: } else if (WIFSTOPPED(st))
209: raise(WSTOPSIG(st));
210: else if (WIFEXITED(st))
1.15 deraadt 211: break;
212: }
213: (void)signal(SIGHUP, SIG_DFL);
214: (void)signal(SIGINT, SIG_DFL);
215: (void)signal(SIGQUIT, SIG_DFL);
1.21 deraadt 216: if (!WIFEXITED(st) || WEXITSTATUS(st) != 0)
1.18 ray 217: return (-1);
218: return (0);
1.15 deraadt 219: }
1.12 tedu 220:
1.1 ray 221: int
222: prompt(void)
223: {
224: int c, ret;
225:
226: fpurge(stdin);
227: fprintf(stderr, "a)bort, e)dit, or s)end: ");
228: fflush(stderr);
229: ret = getchar();
230: if (ret == EOF || ret == '\n')
231: return (ret);
232: do {
233: c = getchar();
234: } while (c != EOF && c != '\n');
235: return (ret);
236: }
237:
238: int
239: sendmail(const char *tmppath)
240: {
241: int filedes[2];
242:
243: if (pipe(filedes) == -1) {
244: warn("pipe: unsent report in %s", tmppath);
245: return (-1);
246: }
247: switch (fork()) {
248: case -1:
249: warn("fork error: unsent report in %s",
250: tmppath);
251: return (-1);
252: case 0:
253: close(filedes[1]);
254: if (dup2(filedes[0], STDIN_FILENO) == -1) {
255: warn("dup2 error: unsent report in %s",
256: tmppath);
257: return (-1);
258: }
259: close(filedes[0]);
260: execl("/usr/sbin/sendmail", "sendmail",
1.2 deraadt 261: "-oi", "-t", (void *)NULL);
1.1 ray 262: warn("sendmail error: unsent report in %s",
263: tmppath);
264: return (-1);
265: default:
266: close(filedes[0]);
267: /* Pipe into sendmail. */
268: if (send_file(tmppath, filedes[1]) == -1) {
269: warn("send_file error: unsent report in %s",
270: tmppath);
271: return (-1);
272: }
273: close(filedes[1]);
274: wait(NULL);
275: break;
276: }
277: return (0);
278: }
279:
1.12 tedu 280: void
1.1 ray 281: init(void)
282: {
1.13 ray 283: size_t len = 0, namelen;
1.19 deraadt 284: const char *src;
1.1 ray 285: int sysname[2];
1.14 deraadt 286: char *dst, *cp;
1.1 ray 287:
1.19 deraadt 288: if ((pw = getpwuid(getuid())) == NULL)
1.12 tedu 289: err(1, "getpwuid");
1.13 ray 290: namelen = strlen(pw->pw_name);
1.1 ray 291:
1.13 ray 292: /* Add length of expanded '&', minus existing '&'. */
293: src = pw->pw_gecos;
294: src += strcspn(src, ",&");
295: while (*src == '&') {
296: len += namelen - 1;
297: /* Look for next '&', skipping the one we just found. */
298: src += 1 + strcspn(src, ",&");
299: }
300: /* Add full name length, including all those '&' we skipped. */
301: len += src - pw->pw_gecos;
1.19 deraadt 302: if ((fullname = malloc(len + 1)) == NULL)
1.12 tedu 303: err(1, "malloc");
1.19 deraadt 304:
1.13 ray 305: dst = fullname;
306: src = pw->pw_gecos;
307: while (*src != ',' && *src != '\0') {
308: /* Copy text up to ',' or '&' and skip. */
309: len = strcspn(src, ",&");
310: memcpy(dst, src, len);
311: dst += len;
312: src += len;
313: /* Replace '&' with login. */
314: if (*src == '&') {
315: memcpy(dst, pw->pw_name, namelen);
316: *dst = toupper((unsigned char)*dst);
317: dst += namelen;
318: ++src;
319: }
320: }
321: *dst = '\0';
1.1 ray 322:
323: sysname[0] = CTL_KERN;
324: sysname[1] = KERN_OSTYPE;
325: len = sizeof(os) - 1;
1.19 deraadt 326: if (sysctl(sysname, 2, &os, &len, NULL, 0) == -1)
1.12 tedu 327: err(1, "sysctl");
1.1 ray 328:
329: sysname[0] = CTL_KERN;
330: sysname[1] = KERN_OSRELEASE;
331: len = sizeof(rel) - 1;
1.19 deraadt 332: if (sysctl(sysname, 2, &rel, &len, NULL, 0) == -1)
1.12 tedu 333: err(1, "sysctl");
1.1 ray 334:
1.14 deraadt 335: sysname[0] = CTL_KERN;
336: sysname[1] = KERN_VERSION;
337: len = sizeof(details) - 1;
1.19 deraadt 338: if (sysctl(sysname, 2, &details, &len, NULL, 0) == -1)
1.14 deraadt 339: err(1, "sysctl");
340:
341: cp = strchr(details, '\n');
342: if (cp) {
343: cp++;
344: if (*cp)
345: *cp++ = '\t';
346: if (*cp)
347: *cp++ = '\t';
348: if (*cp)
349: *cp++ = '\t';
350: }
351:
1.1 ray 352: sysname[0] = CTL_HW;
353: sysname[1] = HW_MACHINE;
354: len = sizeof(mach) - 1;
1.19 deraadt 355: if (sysctl(sysname, 2, &mach, &len, NULL, 0) == -1)
1.12 tedu 356: err(1, "sysctl");
1.1 ray 357: }
358:
359: int
360: send_file(const char *file, int dst)
361: {
1.2 deraadt 362: int blank = 0;
363: size_t len;
364: char *buf;
1.1 ray 365: FILE *fp;
366:
367: if ((fp = fopen(file, "r")) == NULL)
368: return (-1);
369: while ((buf = fgetln(fp, &len))) {
370: /* Skip lines starting with "SENDBUG". */
371: if (len >= sizeof("SENDBUG") - 1 &&
372: memcmp(buf, "SENDBUG", sizeof("SENDBUG") - 1) == 0)
373: continue;
374: if (len == 1 && buf[0] == '\n')
375: blank = 1;
376: /* Skip comments, but only if we encountered a blank line. */
377: while (len) {
1.14 deraadt 378: char *sp = NULL, *ep = NULL;
1.1 ray 379: size_t copylen;
380:
381: if (blank && (sp = memchr(buf, '<', len)) != NULL)
382: ep = memchr(sp, '>', len - (sp - buf + 1));
383: /* Length of string before comment. */
1.4 ray 384: if (ep)
385: copylen = sp - buf;
386: else
387: copylen = len;
1.1 ray 388: if (atomicio(vwrite, dst, buf, copylen) != copylen) {
389: int saved_errno = errno;
390:
391: fclose(fp);
392: errno = saved_errno;
393: return (-1);
394: }
395: if (!ep)
396: break;
397: /* Skip comment. */
398: len -= ep - buf + 1;
399: buf = ep + 1;
400: }
401: }
402: fclose(fp);
403: return (0);
404: }
405:
406: void
407: template(FILE *fp)
408: {
409: fprintf(fp, "SENDBUG: -*- sendbug -*-\n");
1.19 deraadt 410: fprintf(fp, "SENDBUG: Lines starting with `SENDBUG' will"
411: " be removed automatically, as\n");
412: fprintf(fp, "SENDBUG: will all comments (text enclosed in `<' and `>').\n");
1.1 ray 413: fprintf(fp, "SENDBUG:\n");
414: fprintf(fp, "SENDBUG: Choose from the following categories:\n");
415: fprintf(fp, "SENDBUG:\n");
416: fprintf(fp, "SENDBUG: %s\n", categories);
417: fprintf(fp, "SENDBUG:\n");
418: fprintf(fp, "SENDBUG:\n");
419: fprintf(fp, "To: %s\n", "gnats@openbsd.org");
420: fprintf(fp, "Subject: \n");
421: fprintf(fp, "From: %s\n", pw->pw_name);
422: fprintf(fp, "Cc: \n");
423: fprintf(fp, "Reply-To: %s\n", pw->pw_name);
1.5 deraadt 424: fprintf(fp, "X-sendbug-version: %s\n", version);
1.1 ray 425: fprintf(fp, "\n");
426: fprintf(fp, "\n");
427: fprintf(fp, ">Submitter-Id:\tnet\n");
428: fprintf(fp, ">Originator:\t%s\n", fullname);
429: fprintf(fp, ">Organization:\n");
430: fprintf(fp, "net\n");
431: fprintf(fp, ">Synopsis:\t<synopsis of the problem (one line)>\n");
1.19 deraadt 432: fprintf(fp, ">Severity:\t"
433: "<[ non-critical | serious | critical ] (one line)>\n");
1.1 ray 434: fprintf(fp, ">Priority:\t<[ low | medium | high ] (one line)>\n");
435: fprintf(fp, ">Category:\t<PR category (one line)>\n");
1.19 deraadt 436: fprintf(fp, ">Class:\t\t"
437: "<[ sw-bug | doc-bug | change-request | support ] (one line)>\n");
1.1 ray 438: fprintf(fp, ">Release:\t<release number or tag (one line)>\n");
439: fprintf(fp, ">Environment:\n");
440: fprintf(fp, "\t<machine, os, target, libraries (multiple lines)>\n");
441: fprintf(fp, "\tSystem : %s %s\n", os, rel);
1.14 deraadt 442: fprintf(fp, "\tDetails : %s\n", details);
1.1 ray 443: fprintf(fp, "\tArchitecture: %s.%s\n", os, mach);
444: fprintf(fp, "\tMachine : %s\n", mach);
445: fprintf(fp, ">Description:\n");
446: fprintf(fp, "\t<precise description of the problem (multiple lines)>\n");
447: fprintf(fp, ">How-To-Repeat:\n");
1.19 deraadt 448: fprintf(fp, "\t<code/input/activities to reproduce the problem"
449: " (multiple lines)>\n");
1.1 ray 450: fprintf(fp, ">Fix:\n");
1.19 deraadt 451: fprintf(fp, "\t<how to correct or work around the problem,"
452: " if known (multiple lines)>\n");
1.1 ray 453: }