xref: /openbsd-src/usr.bin/sendbug/sendbug.c (revision c8a426ad09be751d07dfe2930b6f58b9e6a09e78)
1 /*	$OpenBSD: sendbug.c,v 1.26 2007/03/26 06:22:12 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 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 	sig_t sighup, sigint, sigquit;
170 	pid_t pid, xpid;
171 	int st;
172 
173 	ed = getenv("VISUAL");
174 	if (ed == NULL || ed[0] == '\0')
175 		ed = getenv("EDITOR");
176 	if (ed == NULL || ed[0] == '\0')
177 		ed = _PATH_VI;
178 	if (asprintf(&p, "%s %s", ed, tmpfile) == -1)
179 		return (-1);
180 	argp[2] = p;
181 
182  top:
183 	sighup = signal(SIGHUP, SIG_IGN);
184 	sigint = signal(SIGINT, SIG_IGN);
185 	sigquit = signal(SIGQUIT, SIG_IGN);
186 	if ((pid = fork()) == -1) {
187 		int saved_errno = errno;
188 
189 		(void)signal(SIGHUP, sighup);
190 		(void)signal(SIGINT, sigint);
191 		(void)signal(SIGQUIT, sigquit);
192 		if (saved_errno == EAGAIN) {
193 			sleep(1);
194 			goto top;
195 		}
196 		errno = saved_errno;
197 		perror("fork");
198 		free(p);
199 		return (-1);
200 	}
201 	if (pid == 0) {
202 		execv(_PATH_BSHELL, argp);
203 		_exit(127);
204 	}
205 	free(p);
206 	for (;;) {
207 		xpid = waitpid(pid, (int *)&st, WUNTRACED);
208 		if (xpid == -1) {
209 			if (errno != EINTR) {
210 				warn("waidpid");
211 				return (-1);
212 			}
213 		} else if (WIFSTOPPED(st))
214 			raise(WSTOPSIG(st));
215 		else if (WIFEXITED(st))
216 			break;
217 	}
218 	(void)signal(SIGHUP, sighup);
219 	(void)signal(SIGINT, sigint);
220 	(void)signal(SIGQUIT, sigquit);
221 	if (!WIFEXITED(st) || WEXITSTATUS(st) != 0)
222 		return (-1);
223 	return (0);
224 }
225 
226 int
227 prompt(void)
228 {
229 	int c, ret;
230 
231 	fpurge(stdin);
232 	fprintf(stderr, "a)bort, e)dit, or s)end: ");
233 	fflush(stderr);
234 	ret = getchar();
235 	if (ret == EOF || ret == '\n')
236 		return (ret);
237 	do {
238 		c = getchar();
239 	} while (c != EOF && c != '\n');
240 	return (ret);
241 }
242 
243 int
244 sendmail(const char *tmppath)
245 {
246 	int filedes[2];
247 
248 	if (pipe(filedes) == -1) {
249 		warn("pipe: unsent report in %s", tmppath);
250 		return (-1);
251 	}
252 	switch (fork()) {
253 	case -1:
254 		warn("fork error: unsent report in %s",
255 		    tmppath);
256 		return (-1);
257 	case 0:
258 		close(filedes[1]);
259 		if (dup2(filedes[0], STDIN_FILENO) == -1) {
260 			warn("dup2 error: unsent report in %s",
261 			    tmppath);
262 			return (-1);
263 		}
264 		close(filedes[0]);
265 		execl("/usr/sbin/sendmail", "sendmail",
266 		    "-oi", "-t", (void *)NULL);
267 		warn("sendmail error: unsent report in %s",
268 		    tmppath);
269 		return (-1);
270 	default:
271 		close(filedes[0]);
272 		/* Pipe into sendmail. */
273 		if (send_file(tmppath, filedes[1]) == -1) {
274 			warn("send_file error: unsent report in %s",
275 			    tmppath);
276 			return (-1);
277 		}
278 		close(filedes[1]);
279 		wait(NULL);
280 		break;
281 	}
282 	return (0);
283 }
284 
285 void
286 init(void)
287 {
288 	size_t len = 0, namelen;
289 	const char *src;
290 	int sysname[2];
291 	char *dst, *cp;
292 
293 	if ((pw = getpwuid(getuid())) == NULL)
294 		err(1, "getpwuid");
295 	namelen = strlen(pw->pw_name);
296 
297 	/* Add length of expanded '&', minus existing '&'. */
298 	src = pw->pw_gecos;
299 	src += strcspn(src, ",&");
300 	while (*src == '&') {
301 		len += namelen - 1;
302 		/* Look for next '&', skipping the one we just found. */
303 		src += 1 + strcspn(src, ",&");
304 	}
305 	/* Add full name length, including all those '&' we skipped. */
306 	len += src - pw->pw_gecos;
307 	if ((fullname = malloc(len + 1)) == NULL)
308 		err(1, "malloc");
309 
310 	dst = fullname;
311 	src = pw->pw_gecos;
312 	while (*src != ',' && *src != '\0') {
313 		/* Copy text up to ',' or '&' and skip. */
314 		len = strcspn(src, ",&");
315 		memcpy(dst, src, len);
316 		dst += len;
317 		src += len;
318 		/* Replace '&' with login. */
319 		if (*src == '&') {
320 			memcpy(dst, pw->pw_name, namelen);
321 			*dst = toupper((unsigned char)*dst);
322 			dst += namelen;
323 			++src;
324 		}
325 	}
326 	*dst = '\0';
327 
328 	sysname[0] = CTL_KERN;
329 	sysname[1] = KERN_OSTYPE;
330 	len = sizeof(os) - 1;
331 	if (sysctl(sysname, 2, &os, &len, NULL, 0) == -1)
332 		err(1, "sysctl");
333 
334 	sysname[0] = CTL_KERN;
335 	sysname[1] = KERN_OSRELEASE;
336 	len = sizeof(rel) - 1;
337 	if (sysctl(sysname, 2, &rel, &len, NULL, 0) == -1)
338 		err(1, "sysctl");
339 
340 	sysname[0] = CTL_KERN;
341 	sysname[1] = KERN_VERSION;
342 	len = sizeof(details) - 1;
343 	if (sysctl(sysname, 2, &details, &len, NULL, 0) == -1)
344 		err(1, "sysctl");
345 
346 	cp = strchr(details, '\n');
347 	if (cp) {
348 		cp++;
349 		if (*cp)
350 			*cp++ = '\t';
351 		if (*cp)
352 			*cp++ = '\t';
353 		if (*cp)
354 			*cp++ = '\t';
355 	}
356 
357 	sysname[0] = CTL_HW;
358 	sysname[1] = HW_MACHINE;
359 	len = sizeof(mach) - 1;
360 	if (sysctl(sysname, 2, &mach, &len, NULL, 0) == -1)
361 		err(1, "sysctl");
362 }
363 
364 int
365 send_file(const char *file, int dst)
366 {
367 	int blank = 0;
368 	size_t len;
369 	char *buf;
370 	FILE *fp;
371 
372 	if ((fp = fopen(file, "r")) == NULL)
373 		return (-1);
374 	while ((buf = fgetln(fp, &len))) {
375 		/* Skip lines starting with "SENDBUG". */
376 		if (len >= sizeof("SENDBUG") - 1 &&
377 		    memcmp(buf, "SENDBUG", sizeof("SENDBUG") - 1) == 0)
378 			continue;
379 		if (len == 1 && buf[0] == '\n')
380 			blank = 1;
381 		/* Skip comments, but only if we encountered a blank line. */
382 		while (len) {
383 			char *sp = NULL, *ep = NULL;
384 			size_t copylen;
385 
386 			if (blank && (sp = memchr(buf, '<', len)) != NULL)
387 				ep = memchr(sp, '>', len - (sp - buf + 1));
388 			/* Length of string before comment. */
389 			if (ep)
390 				copylen = sp - buf;
391 			else
392 				copylen = len;
393 			if (atomicio(vwrite, dst, buf, copylen) != copylen) {
394 				int saved_errno = errno;
395 
396 				fclose(fp);
397 				errno = saved_errno;
398 				return (-1);
399 			}
400 			if (!ep)
401 				break;
402 			/* Skip comment. */
403 			len -= ep - buf + 1;
404 			buf = ep + 1;
405 		}
406 	}
407 	fclose(fp);
408 	return (0);
409 }
410 
411 void
412 template(FILE *fp)
413 {
414 	fprintf(fp, "SENDBUG: -*- sendbug -*-\n");
415 	fprintf(fp, "SENDBUG: Lines starting with `SENDBUG' will"
416 	    " be removed automatically, as\n");
417 	fprintf(fp, "SENDBUG: will all comments (text enclosed in `<' and `>').\n");
418 	fprintf(fp, "SENDBUG:\n");
419 	fprintf(fp, "SENDBUG: Choose from the following categories:\n");
420 	fprintf(fp, "SENDBUG:\n");
421 	fprintf(fp, "SENDBUG: %s\n", categories);
422 	fprintf(fp, "SENDBUG:\n");
423 	fprintf(fp, "SENDBUG:\n");
424 	fprintf(fp, "To: %s\n", "gnats@openbsd.org");
425 	fprintf(fp, "Subject: \n");
426 	fprintf(fp, "From: %s\n", pw->pw_name);
427 	fprintf(fp, "Cc: \n");
428 	fprintf(fp, "Reply-To: %s\n", pw->pw_name);
429 	fprintf(fp, "X-sendbug-version: %s\n", version);
430 	fprintf(fp, "\n");
431 	fprintf(fp, "\n");
432 	fprintf(fp, ">Submitter-Id:\tnet\n");
433 	fprintf(fp, ">Originator:\t%s\n", fullname);
434 	fprintf(fp, ">Organization:\n");
435 	fprintf(fp, "net\n");
436 	fprintf(fp, ">Synopsis:\t<synopsis of the problem (one line)>\n");
437 	fprintf(fp, ">Severity:\t"
438 	    "<[ non-critical | serious | critical ] (one line)>\n");
439 	fprintf(fp, ">Priority:\t<[ low | medium | high ] (one line)>\n");
440 	fprintf(fp, ">Category:\t<PR category (one line)>\n");
441 	fprintf(fp, ">Class:\t\t"
442 	    "<[ sw-bug | doc-bug | change-request | support ] (one line)>\n");
443 	fprintf(fp, ">Release:\t<release number or tag (one line)>\n");
444 	fprintf(fp, ">Environment:\n");
445 	fprintf(fp, "\t<machine, os, target, libraries (multiple lines)>\n");
446 	fprintf(fp, "\tSystem      : %s %s\n", os, rel);
447 	fprintf(fp, "\tDetails     : %s\n", details);
448 	fprintf(fp, "\tArchitecture: %s.%s\n", os, mach);
449 	fprintf(fp, "\tMachine     : %s\n", mach);
450 	fprintf(fp, ">Description:\n");
451 	fprintf(fp, "\t<precise description of the problem (multiple lines)>\n");
452 	fprintf(fp, ">How-To-Repeat:\n");
453 	fprintf(fp, "\t<code/input/activities to reproduce the problem"
454 	    " (multiple lines)>\n");
455 	fprintf(fp, ">Fix:\n");
456 	fprintf(fp, "\t<how to correct or work around the problem,"
457 	    " if known (multiple lines)>\n");
458 }
459