[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.71

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