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

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