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