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

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