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

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