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