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