xref: /openbsd-src/usr.bin/sendbug/sendbug.c (revision 534084640310a3b73784adb5e68a3695b1c4a21c)
1 /*	$OpenBSD: sendbug.c,v 1.74 2016/03/17 19:40:43 krw Exp $	*/
2 
3 /*
4  * Written by Ray Lai <ray@cyth.net>.
5  * Public domain.
6  */
7 
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/sysctl.h>
11 #include <sys/wait.h>
12 
13 #include <ctype.h>
14 #include <err.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <limits.h>
18 #include <paths.h>
19 #include <pwd.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "atomicio.h"
27 
28 #define _PATH_DMESG "/var/run/dmesg.boot"
29 #define DMESG_START "OpenBSD "
30 #define BEGIN64 "begin-base64 "
31 #define END64 "===="
32 
33 int	checkfile(const char *);
34 void	debase(void);
35 void	dmesg(FILE *);
36 int	editit(const char *);
37 void	hwdump(FILE *);
38 void	init(void);
39 int	matchline(const char *, const char *, size_t);
40 int	prompt(void);
41 int	send_file(const char *, int);
42 int	sendmail(const char *);
43 void	template(FILE *);
44 void	usbdevs(FILE *);
45 
46 const char *categories = "system user library documentation kernel "
47     "alpha amd64 arm hppa i386 m88k mips64 powerpc sh sparc sparc64 vax";
48 const char *comment[] = {
49 	"<synopsis of the problem (one line)>",
50 	"<PR category (one line)>",
51 	"<precise description of the problem (multiple lines)>",
52 	"<code/input/activities to reproduce the problem (multiple lines)>",
53 	"<how to correct or work around the problem, if known (multiple lines)>"
54 };
55 
56 struct passwd *pw;
57 char os[BUFSIZ], rel[BUFSIZ], mach[BUFSIZ], details[BUFSIZ];
58 const char *tmpdir = _PATH_TMP;
59 char *tmppath;
60 int Dflag, Pflag, wantcleanup;
61 
62 __dead void
63 usage(void)
64 {
65 	extern char *__progname;
66 
67 	fprintf(stderr, "usage: %s [-DEP]\n", __progname);
68 	exit(1);
69 }
70 
71 void
72 cleanup()
73 {
74 	if (wantcleanup && tmppath && unlink(tmppath) == -1)
75 		warn("unlink");
76 }
77 
78 
79 int
80 main(int argc, char *argv[])
81 {
82 	int ch, c, fd, ret = 1;
83 	struct stat sb;
84 	char *pr_form;
85 	time_t mtime;
86 	FILE *fp;
87 
88 	if (pledge("stdio rpath wpath cpath tmppath getpw proc exec", NULL) == -1)
89 		err(1, "pledge");
90 
91 	while ((ch = getopt(argc, argv, "DEP")) != -1)
92 		switch (ch) {
93 		case 'D':
94 			Dflag = 1;
95 			break;
96 		case 'E':
97 			debase();
98 			exit(0);
99 		case 'P':
100 			Pflag = 1;
101 			break;
102 		default:
103 			usage();
104 		}
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (argc > 0)
109 		usage();
110 
111 	if (Pflag) {
112 		init();
113 		template(stdout);
114 		exit(0);
115 	}
116 
117 	if (asprintf(&tmppath, "%s%sp.XXXXXXXXXX", tmpdir,
118 	    tmpdir[strlen(tmpdir) - 1] == '/' ? "" : "/") == -1)
119 		err(1, "asprintf");
120 	if ((fd = mkstemp(tmppath)) == -1)
121 		err(1, "mkstemp");
122 	wantcleanup = 1;
123 	atexit(cleanup);
124 	if ((fp = fdopen(fd, "w+")) == NULL)
125 		err(1, "fdopen");
126 
127 	init();
128 
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) {
137 			warn("can't seem to read your template file "
138 			    "(`%s'), ignoring PR_FORM", pr_form);
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 		}
150 	} else
151 		template(fp);
152 
153 	if (fflush(fp) == EOF || fstat(fd, &sb) == -1 || fclose(fp) == EOF)
154 		err(1, "error creating template");
155 	mtime = sb.st_mtime;
156 
157  edit:
158 	if (editit(tmppath) == -1)
159 		err(1, "error running editor");
160 
161 	if (stat(tmppath, &sb) == -1)
162 		err(1, "stat");
163 	if (mtime == sb.st_mtime)
164 		errx(1, "report unchanged, nothing sent");
165 
166  prompt:
167 	if (!checkfile(tmppath))
168 		fprintf(stderr, "fields are blank, must be filled in\n");
169 	c = prompt();
170 	switch (c) {
171 	case 'a':
172 	case EOF:
173 		wantcleanup = 0;
174 		errx(1, "unsent report in %s", tmppath);
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;
186 quit:
187 	return (ret);
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 	/* Find last dmesg. */
204 	for (;;) {
205 		off_t o;
206 
207 		o = ftello(dfp);
208 		if (fgets(buf, sizeof(buf), dfp) == NULL)
209 			break;
210 		if (!strncmp(DMESG_START, buf, sizeof(DMESG_START) - 1))
211 			offset = o;
212 	}
213 	if (offset != -1) {
214 		size_t len;
215 
216 		clearerr(dfp);
217 		fseeko(dfp, offset, SEEK_SET);
218 		while (offset != -1 && !feof(dfp)) {
219 			len = fread(buf, 1, sizeof buf, dfp);
220 			if (len == 0)
221 				break;
222 			if (fwrite(buf, 1, len, fp) != len)
223 				break;
224 		}
225 	}
226 	fclose(dfp);
227 }
228 
229 void
230 usbdevs(FILE *ofp)
231 {
232 	char buf[BUFSIZ];
233 	FILE *ifp;
234 	size_t len;
235 
236 	if ((ifp = popen("usbdevs -v", "r")) != NULL) {
237 		while (!feof(ifp)) {
238 			len = fread(buf, 1, sizeof buf, ifp);
239 			if (len == 0)
240 				break;
241 			if (fwrite(buf, 1, len, ofp) != len)
242 				break;
243 		}
244 		pclose(ifp);
245 	}
246 }
247 
248 /*
249  * Execute an editor on the specified pathname, which is interpreted
250  * from the shell.  This means flags may be included.
251  *
252  * Returns -1 on error, or the exit value on success.
253  */
254 int
255 editit(const char *pathname)
256 {
257 	char *argp[] = {"sh", "-c", NULL, NULL}, *ed, *p;
258 	sig_t sighup, sigint, sigquit, sigchld;
259 	pid_t pid;
260 	int saved_errno, st, ret = -1;
261 
262 	ed = getenv("VISUAL");
263 	if (ed == NULL || ed[0] == '\0')
264 		ed = getenv("EDITOR");
265 	if (ed == NULL || ed[0] == '\0')
266 		ed = _PATH_VI;
267 	if (asprintf(&p, "%s %s", ed, pathname) == -1)
268 		return (-1);
269 	argp[2] = p;
270 
271 	sighup = signal(SIGHUP, SIG_IGN);
272 	sigint = signal(SIGINT, SIG_IGN);
273 	sigquit = signal(SIGQUIT, SIG_IGN);
274 	sigchld = signal(SIGCHLD, SIG_DFL);
275 	if ((pid = fork()) == -1)
276 		goto fail;
277 	if (pid == 0) {
278 		execv(_PATH_BSHELL, argp);
279 		_exit(127);
280 	}
281 	while (waitpid(pid, &st, 0) == -1)
282 		if (errno != EINTR)
283 			goto fail;
284 	if (!WIFEXITED(st))
285 		errno = EINTR;
286 	else
287 		ret = WEXITSTATUS(st);
288 
289  fail:
290 	saved_errno = errno;
291 	(void)signal(SIGHUP, sighup);
292 	(void)signal(SIGINT, sigint);
293 	(void)signal(SIGQUIT, sigquit);
294 	(void)signal(SIGCHLD, sigchld);
295 	free(p);
296 	errno = saved_errno;
297 	return (ret);
298 }
299 
300 int
301 prompt(void)
302 {
303 	int c, ret;
304 
305 	fpurge(stdin);
306 	fprintf(stderr, "a)bort, e)dit, or s)end: ");
307 	fflush(stderr);
308 	ret = getchar();
309 	if (ret == EOF || ret == '\n')
310 		return (ret);
311 	do {
312 		c = getchar();
313 	} while (c != EOF && c != '\n');
314 	return (ret);
315 }
316 
317 int
318 sendmail(const char *pathname)
319 {
320 	int filedes[2];
321 
322 	if (pipe(filedes) == -1) {
323 		warn("pipe: unsent report in %s", pathname);
324 		return (-1);
325 	}
326 	switch (fork()) {
327 	case -1:
328 		warn("fork error: unsent report in %s",
329 		    pathname);
330 		return (-1);
331 	case 0:
332 		close(filedes[1]);
333 		if (dup2(filedes[0], STDIN_FILENO) == -1) {
334 			warn("dup2 error: unsent report in %s",
335 			    pathname);
336 			return (-1);
337 		}
338 		close(filedes[0]);
339 		execl(_PATH_SENDMAIL, "sendmail",
340 		    "-oi", "-t", (char *)NULL);
341 		warn("sendmail error: unsent report in %s",
342 		    pathname);
343 		return (-1);
344 	default:
345 		close(filedes[0]);
346 		/* Pipe into sendmail. */
347 		if (send_file(pathname, filedes[1]) == -1) {
348 			warn("send_file error: unsent report in %s",
349 			    pathname);
350 			return (-1);
351 		}
352 		close(filedes[1]);
353 		wait(NULL);
354 		break;
355 	}
356 	return (0);
357 }
358 
359 void
360 init(void)
361 {
362 	size_t len;
363 	int sysname[2];
364 	char *cp;
365 
366 	if ((pw = getpwuid(getuid())) == NULL)
367 		err(1, "getpwuid");
368 
369 	sysname[0] = CTL_KERN;
370 	sysname[1] = KERN_OSTYPE;
371 	len = sizeof(os) - 1;
372 	if (sysctl(sysname, 2, &os, &len, NULL, 0) == -1)
373 		err(1, "sysctl");
374 
375 	sysname[0] = CTL_KERN;
376 	sysname[1] = KERN_OSRELEASE;
377 	len = sizeof(rel) - 1;
378 	if (sysctl(sysname, 2, &rel, &len, NULL, 0) == -1)
379 		err(1, "sysctl");
380 
381 	sysname[0] = CTL_KERN;
382 	sysname[1] = KERN_VERSION;
383 	len = sizeof(details) - 1;
384 	if (sysctl(sysname, 2, &details, &len, NULL, 0) == -1)
385 		err(1, "sysctl");
386 
387 	cp = strchr(details, '\n');
388 	if (cp) {
389 		cp++;
390 		if (*cp)
391 			*cp++ = '\t';
392 		if (*cp)
393 			*cp++ = '\t';
394 		if (*cp)
395 			*cp++ = '\t';
396 	}
397 
398 	sysname[0] = CTL_HW;
399 	sysname[1] = HW_MACHINE;
400 	len = sizeof(mach) - 1;
401 	if (sysctl(sysname, 2, &mach, &len, NULL, 0) == -1)
402 		err(1, "sysctl");
403 }
404 
405 int
406 send_file(const char *file, int dst)
407 {
408 	size_t len;
409 	char *buf, *lbuf;
410 	FILE *fp;
411 	int rval = -1, saved_errno;
412 
413 	if ((fp = fopen(file, "r")) == NULL)
414 		return (-1);
415 	lbuf = NULL;
416 	while ((buf = fgetln(fp, &len))) {
417 		if (buf[len - 1] == '\n') {
418 			buf[len - 1] = '\0';
419 			--len;
420 		} else {
421 			/* EOF without EOL, copy and add the NUL */
422 			if ((lbuf = malloc(len + 1)) == NULL)
423 				goto end;
424 			memcpy(lbuf, buf, len);
425 			lbuf[len] = '\0';
426 			buf = lbuf;
427 		}
428 
429 		/* Skip lines starting with "SENDBUG". */
430 		if (strncmp(buf, "SENDBUG", sizeof("SENDBUG") - 1) == 0)
431 			continue;
432 		while (len) {
433 			char *sp = NULL, *ep = NULL;
434 			size_t copylen;
435 
436 			if ((sp = strchr(buf, '<')) != NULL) {
437 				size_t i;
438 
439 				for (i = 0; i < sizeof(comment) / sizeof(*comment); ++i) {
440 					size_t commentlen = strlen(comment[i]);
441 
442 					if (strncmp(sp, comment[i], commentlen) == 0) {
443 						ep = sp + commentlen - 1;
444 						break;
445 					}
446 				}
447 			}
448 			/* Length of string before comment. */
449 			if (ep)
450 				copylen = sp - buf;
451 			else
452 				copylen = len;
453 			if (atomicio(vwrite, dst, buf, copylen) != copylen)
454 				goto end;
455 			if (!ep)
456 				break;
457 			/* Skip comment. */
458 			len -= ep - buf + 1;
459 			buf = ep + 1;
460 		}
461 		if (atomicio(vwrite, dst, "\n", 1) != 1)
462 			goto end;
463 	}
464 	rval = 0;
465  end:
466 	saved_errno = errno;
467 	free(lbuf);
468 	fclose(fp);
469 	errno = saved_errno;
470 	return (rval);
471 }
472 
473 /*
474  * Does line start with `s' and end with non-comment and non-whitespace?
475  * Note: Does not treat `line' as a C string.
476  */
477 int
478 matchline(const char *s, const char *line, size_t linelen)
479 {
480 	size_t slen;
481 	int iscomment;
482 
483 	slen = strlen(s);
484 	/* Is line shorter than string? */
485 	if (linelen <= slen)
486 		return (0);
487 	/* Does line start with string? */
488 	if (memcmp(line, s, slen) != 0)
489 		return (0);
490 	/* Does line contain anything but comments and whitespace? */
491 	line += slen;
492 	linelen -= slen;
493 	iscomment = 0;
494 	while (linelen) {
495 		if (iscomment) {
496 			if (*line == '>')
497 				iscomment = 0;
498 		} else if (*line == '<')
499 			iscomment = 1;
500 		else if (!isspace((unsigned char)*line))
501 			return (1);
502 		++line;
503 		--linelen;
504 	}
505 	return (0);
506 }
507 
508 /*
509  * Are all required fields filled out?
510  */
511 int
512 checkfile(const char *pathname)
513 {
514 	FILE *fp;
515 	size_t len;
516 	int category = 0, synopsis = 0;
517 	char *buf;
518 
519 	if ((fp = fopen(pathname, "r")) == NULL) {
520 		warn("%s", pathname);
521 		return (0);
522 	}
523 	while ((buf = fgetln(fp, &len))) {
524 		if (matchline(">Category:", buf, len))
525 			category = 1;
526 		else if (matchline(">Synopsis:", buf, len))
527 			synopsis = 1;
528 	}
529 	fclose(fp);
530 	return (category && synopsis);
531 }
532 
533 void
534 template(FILE *fp)
535 {
536 	fprintf(fp, "SENDBUG: -*- sendbug -*-\n");
537 	fprintf(fp, "SENDBUG: Lines starting with `SENDBUG' will"
538 	    " be removed automatically.\n");
539 	fprintf(fp, "SENDBUG:\n");
540 	fprintf(fp, "SENDBUG: Choose from the following categories:\n");
541 	fprintf(fp, "SENDBUG:\n");
542 	fprintf(fp, "SENDBUG: %s\n", categories);
543 	fprintf(fp, "SENDBUG:\n");
544 	fprintf(fp, "SENDBUG:\n");
545 	fprintf(fp, "To: %s\n", "bugs@openbsd.org");
546 	fprintf(fp, "Subject: \n");
547 	fprintf(fp, "From: %s\n", pw->pw_name);
548 	fprintf(fp, "Cc: %s\n", pw->pw_name);
549 	fprintf(fp, "Reply-To: %s\n", pw->pw_name);
550 	fprintf(fp, "\n");
551 	fprintf(fp, ">Synopsis:\t%s\n", comment[0]);
552 	fprintf(fp, ">Category:\t%s\n", comment[1]);
553 	fprintf(fp, ">Environment:\n");
554 	fprintf(fp, "\tSystem      : %s %s\n", os, rel);
555 	fprintf(fp, "\tDetails     : %s\n", details);
556 	fprintf(fp, "\tArchitecture: %s.%s\n", os, mach);
557 	fprintf(fp, "\tMachine     : %s\n", mach);
558 	fprintf(fp, ">Description:\n");
559 	fprintf(fp, "\t%s\n", comment[2]);
560 	fprintf(fp, ">How-To-Repeat:\n");
561 	fprintf(fp, "\t%s\n", comment[3]);
562 	fprintf(fp, ">Fix:\n");
563 	fprintf(fp, "\t%s\n", comment[4]);
564 
565 	if (!Dflag) {
566 		int root;
567 
568 		fprintf(fp, "\n");
569 		root = !geteuid();
570 		if (!root)
571 			fprintf(fp, "SENDBUG: Run sendbug as root "
572 			    "if this is an ACPI report!\n");
573 		fprintf(fp, "SENDBUG: dmesg%s and usbdevs are attached.\n"
574 		    "SENDBUG: Feel free to delete or use the -D flag if they "
575 		    "contain sensitive information.\n",
576 		    root ? ", pcidump, acpidump" : "");
577 		fputs("\ndmesg:\n", fp);
578 		dmesg(fp);
579 		fputs("\nusbdevs:\n", fp);
580 		usbdevs(fp);
581 		if (root)
582 			hwdump(fp);
583 	}
584 }
585 
586 void
587 hwdump(FILE *ofp)
588 {
589 	char buf[BUFSIZ];
590 	FILE *ifp;
591 	char *cmd, *acpidir;
592 	size_t len;
593 
594 	if (gethostname(buf, sizeof(buf)) == -1)
595 		err(1, "gethostname");
596 	buf[strcspn(buf, ".")] = '\0';
597 
598 	if (asprintf(&acpidir, "%s%sp.XXXXXXXXXX", tmpdir,
599 	    tmpdir[strlen(tmpdir) - 1] == '/' ? "" : "/") == -1)
600 		err(1, "asprintf");
601 	if (mkdtemp(acpidir) == NULL)
602 		err(1, "mkdtemp");
603 
604 	if (asprintf(&cmd, "echo \"\\npcidump:\"; pcidump -xxv; "
605 	    "echo \"\\nacpidump:\"; cd %s && acpidump -o %s; "
606 	    "for i in *; do b64encode $i $i; done; rm -rf %s",
607 	    acpidir, buf, acpidir) == -1)
608 		err(1, "asprintf");
609 
610 	if ((ifp = popen(cmd, "r")) != NULL) {
611 		while (!feof(ifp)) {
612 			len = fread(buf, 1, sizeof buf, ifp);
613 			if (len == 0)
614 				break;
615 			if (fwrite(buf, 1, len, ofp) != len)
616 				break;
617 		}
618 		pclose(ifp);
619 	}
620 	free(cmd);
621 	free(acpidir);
622 }
623 
624 void
625 debase(void)
626 {
627 	char buf[BUFSIZ];
628 	FILE *fp = NULL;
629 	size_t len;
630 
631 	while (fgets(buf, sizeof(buf), stdin) != NULL) {
632 		len = strlen(buf);
633 		if (!strncmp(buf, BEGIN64, sizeof(BEGIN64) - 1)) {
634 			if (fp)
635 				errx(1, "double begin");
636 			fp = popen("b64decode", "w");
637 			if (!fp)
638 				errx(1, "popen b64decode");
639 		}
640 		if (fp && fwrite(buf, 1, len, fp) != len)
641 			errx(1, "pipe error");
642 		if (!strncmp(buf, END64, sizeof(END64) - 1)) {
643 			if (pclose(fp) == -1)
644 				errx(1, "pclose b64decode");
645 			fp = NULL;
646 		}
647 	}
648 }
649