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