[BACK]Return to sendbug.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sendbug

Annotation of src/usr.bin/sendbug/sendbug.c, Revision 1.72

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