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