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

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