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

1.15    ! deraadt     1: /*     $OpenBSD: sendbug.c,v 1.14 2007/03/23 15:46:40 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/mman.h>
                     10: #include <sys/param.h>
                     11: #include <sys/stat.h>
                     12: #include <sys/sysctl.h>
                     13: #include <sys/wait.h>
                     14:
1.13      ray        15: #include <ctype.h>
1.1       ray        16: #include <err.h>
                     17: #include <errno.h>
                     18: #include <fcntl.h>
                     19: #include <limits.h>
                     20: #include <paths.h>
                     21: #include <pwd.h>
1.15    ! deraadt    22: #include <signal.h>
1.1       ray        23: #include <stdio.h>
                     24: #include <stdlib.h>
                     25: #include <string.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "atomicio.h"
                     29:
1.15    ! deraadt    30: int editit(char *);
1.12      tedu       31: void init(void);
1.1       ray        32: int prompt(void);
                     33: int send_file(const char *, int dst);
                     34: int sendmail(const char *);
                     35: void template(FILE *);
                     36:
                     37: const char *categories = "system user library documentation ports kernel "
                     38:     "alpha amd64 arm i386 m68k m88k mips ppc sgi sparc sparc64 vax";
1.11      deraadt    39: char *version = "4.2";
                     40:
                     41: struct passwd *pw;
1.14      deraadt    42: char os[BUFSIZ], rel[BUFSIZ], mach[BUFSIZ], details[BUFSIZ];
1.1       ray        43: char *fullname;
1.12      tedu       44: char *tmppath;
                     45: int wantcleanup;
1.11      deraadt    46:
1.12      tedu       47: __dead void
1.3       deraadt    48: usage(void)
                     49: {
1.6       deraadt    50:        fprintf(stderr, "usage: sendbug [-LPV]\n");
1.12      tedu       51:        exit(1);
                     52: }
                     53:
                     54: void
                     55: cleanup()
                     56: {
                     57:        if (wantcleanup && tmppath && unlink(tmppath) == -1)
                     58:                warn("unlink");
1.3       deraadt    59: }
                     60:
1.12      tedu       61:
1.1       ray        62: int
                     63: main(int argc, char *argv[])
                     64: {
1.15    ! deraadt    65:        const char *tmpdir;
        !            66:        char *pr_form;
1.3       deraadt    67:        int ch, c, fd, ret = 1;
1.2       deraadt    68:        struct stat sb;
1.1       ray        69:        time_t mtime;
1.2       deraadt    70:        FILE *fp;
1.3       deraadt    71:
1.5       deraadt    72:        while ((ch = getopt(argc, argv, "LPV")) != -1)
1.3       deraadt    73:                switch (ch) {
                     74:                case 'L':
                     75:                        printf("Known categories:\n");
                     76:                        printf("%s\n\n", categories);
                     77:                        exit(0);
                     78:                case 'P':
1.12      tedu       79:                        init();
1.3       deraadt    80:                        template(stdout);
                     81:                        exit(0);
1.5       deraadt    82:                case 'V':
                     83:                        printf("%s\n", version);
                     84:                        exit(0);
1.3       deraadt    85:                default:
                     86:                        usage();
1.7       deraadt    87:                }
                     88:
1.3       deraadt    89:        if (argc > 1) {
                     90:                usage();
                     91:        }
1.1       ray        92:
                     93:        if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0')
                     94:                tmpdir = _PATH_TMP;
1.9       ray        95:        if (asprintf(&tmppath, "%s%sp.XXXXXXXXXX", tmpdir,
                     96:            tmpdir[strlen(tmpdir) - 1] == '/' ? "" : "/") == -1) {
1.12      tedu       97:                err(1, "asprintf");
1.1       ray        98:        }
                     99:        if ((fd = mkstemp(tmppath)) == -1)
                    100:                err(1, "mkstemp");
1.12      tedu      101:        wantcleanup = 1;
                    102:        atexit(cleanup);
1.1       ray       103:        if ((fp = fdopen(fd, "w+")) == NULL) {
1.12      tedu      104:                err(1, "fdopen");
1.1       ray       105:        }
                    106:
1.12      tedu      107:        init();
1.1       ray       108:
1.10      deraadt   109:        pr_form = getenv("PR_FORM");
                    110:        if (pr_form) {
                    111:                char buf[BUFSIZ];
                    112:                size_t len;
                    113:                FILE *frfp;
                    114:
                    115:                frfp = fopen(pr_form, "r");
                    116:                if (frfp == NULL) {
                    117:                        fprintf(stderr, "sendbug: can't seem to read your"
                    118:                            " template file (`%s'), ignoring PR_FORM\n",
                    119:                            pr_form);
                    120:                        template(fp);
                    121:                } else {
                    122:                        while (!feof(frfp)) {
                    123:                                len = fread(buf, 1, sizeof buf, frfp);
                    124:                                if (len == 0)
                    125:                                        break;
                    126:                                if (fwrite(buf, 1, len, fp) != len)
                    127:                                        break;
                    128:                        }
                    129:                        fclose(frfp);
                    130:                }
                    131:        } else
                    132:                template(fp);
1.1       ray       133:
                    134:        if (fflush(fp) == EOF || fstat(fd, &sb) == -1 || fclose(fp) == EOF) {
1.12      tedu      135:                err(1, "error creating template");
1.1       ray       136:        }
                    137:        mtime = sb.st_mtime;
                    138:
                    139:  edit:
1.15    ! deraadt   140:        editit(tmppath);
1.1       ray       141:
                    142:        if (stat(tmppath, &sb) == -1) {
1.12      tedu      143:                err(1, "stat");
1.1       ray       144:        }
                    145:        if (mtime == sb.st_mtime) {
1.12      tedu      146:                errx(1, "report unchanged, nothing sent");
1.1       ray       147:        }
                    148:
                    149:  prompt:
                    150:        c = prompt();
                    151:        switch (c) {
1.5       deraadt   152:        case 'a':
                    153:        case EOF:
1.12      tedu      154:                wantcleanup = 0;
                    155:                errx(1, "unsent report in %s", tmppath);
1.1       ray       156:        case 'e':
                    157:                goto edit;
                    158:        case 's':
                    159:                if (sendmail(tmppath) == -1)
                    160:                        goto quit;
                    161:                break;
                    162:        default:
                    163:                goto prompt;
                    164:        }
                    165:
                    166:        ret = 0;
1.12      tedu      167: quit:
1.1       ray       168:        return (ret);
                    169: }
                    170:
1.15    ! deraadt   171: int
        !           172: editit(char *tmpfile)
        !           173: {
        !           174:        pid_t pid, xpid;
        !           175:        char *argp[] = {"sh", "-c", NULL, NULL};
        !           176:        char *ed, *p;
        !           177:        int stat;
        !           178:
        !           179:        if ((ed = getenv("EDITOR")) == (char *)0)
        !           180:                ed = _PATH_VI;
        !           181:        if (asprintf(&p, "%s %s", ed, tmpfile) == -1)
        !           182:                return (0);
        !           183:        argp[2] = p;
        !           184:
        !           185:  top:
        !           186:        (void)signal(SIGHUP, SIG_IGN);
        !           187:        (void)signal(SIGINT, SIG_IGN);
        !           188:        (void)signal(SIGQUIT, SIG_IGN);
        !           189:        if ((pid = fork()) < 0) {
        !           190:                (void)signal(SIGHUP, SIG_DFL);
        !           191:                (void)signal(SIGINT, SIG_DFL);
        !           192:                (void)signal(SIGQUIT, SIG_DFL);
        !           193:                if (errno == EPROCLIM) {
        !           194:                        warnx("you have too many processes");
        !           195:                        free(p);
        !           196:                        return(0);
        !           197:                }
        !           198:                if (errno == EAGAIN) {
        !           199:                        sleep(1);
        !           200:                        goto top;
        !           201:                }
        !           202:                perror("fork");
        !           203:                free(p);
        !           204:                return(0);
        !           205:        }
        !           206:        if (pid == 0) {
        !           207:                (void)signal(SIGHUP, SIG_DFL);
        !           208:                (void)signal(SIGINT, SIG_DFL);
        !           209:                (void)signal(SIGQUIT, SIG_DFL);
        !           210:                execv(_PATH_BSHELL, argp);
        !           211:                _exit(127);
        !           212:        }
        !           213:        free(p);
        !           214:        for (;;) {
        !           215:                xpid = waitpid(pid, (int *)&stat, WUNTRACED);
        !           216:                if (WIFSTOPPED(stat))
        !           217:                        raise(WSTOPSIG(stat));
        !           218:                else if (WIFEXITED(stat))
        !           219:                        break;
        !           220:        }
        !           221:        (void)signal(SIGHUP, SIG_DFL);
        !           222:        (void)signal(SIGINT, SIG_DFL);
        !           223:        (void)signal(SIGQUIT, SIG_DFL);
        !           224:        if (!WIFEXITED(stat) || WEXITSTATUS(stat) != 0)
        !           225:                return(0);
        !           226:        return(1);
        !           227: }
1.12      tedu      228:
1.1       ray       229: int
                    230: prompt(void)
                    231: {
                    232:        int c, ret;
                    233:
                    234:        fpurge(stdin);
                    235:        fprintf(stderr, "a)bort, e)dit, or s)end: ");
                    236:        fflush(stderr);
                    237:        ret = getchar();
                    238:        if (ret == EOF || ret == '\n')
                    239:                return (ret);
                    240:        do {
                    241:                c = getchar();
                    242:        } while (c != EOF && c != '\n');
                    243:        return (ret);
                    244: }
                    245:
                    246: int
                    247: sendmail(const char *tmppath)
                    248: {
                    249:        int filedes[2];
                    250:
                    251:        if (pipe(filedes) == -1) {
                    252:                warn("pipe: unsent report in %s", tmppath);
                    253:                return (-1);
                    254:        }
                    255:        switch (fork()) {
                    256:        case -1:
                    257:                warn("fork error: unsent report in %s",
                    258:                    tmppath);
                    259:                return (-1);
                    260:        case 0:
                    261:                close(filedes[1]);
                    262:                if (dup2(filedes[0], STDIN_FILENO) == -1) {
                    263:                        warn("dup2 error: unsent report in %s",
                    264:                            tmppath);
                    265:                        return (-1);
                    266:                }
                    267:                close(filedes[0]);
                    268:                execl("/usr/sbin/sendmail", "sendmail",
1.2       deraadt   269:                    "-oi", "-t", (void *)NULL);
1.1       ray       270:                warn("sendmail error: unsent report in %s",
                    271:                    tmppath);
                    272:                return (-1);
                    273:        default:
                    274:                close(filedes[0]);
                    275:                /* Pipe into sendmail. */
                    276:                if (send_file(tmppath, filedes[1]) == -1) {
                    277:                        warn("send_file error: unsent report in %s",
                    278:                            tmppath);
                    279:                        return (-1);
                    280:                }
                    281:                close(filedes[1]);
                    282:                wait(NULL);
                    283:                break;
                    284:        }
                    285:        return (0);
                    286: }
                    287:
1.12      tedu      288: void
1.1       ray       289: init(void)
                    290: {
1.13      ray       291:        size_t len = 0, namelen;
1.1       ray       292:        int sysname[2];
1.13      ray       293:        const char *src;
1.14      deraadt   294:        char *dst, *cp;
1.1       ray       295:
                    296:        if ((pw = getpwuid(getuid())) == NULL) {
1.12      tedu      297:                err(1, "getpwuid");
1.1       ray       298:        }
1.13      ray       299:        namelen = strlen(pw->pw_name);
1.1       ray       300:
1.13      ray       301:        /* Add length of expanded '&', minus existing '&'. */
                    302:        src = pw->pw_gecos;
                    303:        src += strcspn(src, ",&");
                    304:        while (*src == '&') {
                    305:                len += namelen - 1;
                    306:                /* Look for next '&', skipping the one we just found. */
                    307:                src += 1 + strcspn(src, ",&");
                    308:        }
                    309:        /* Add full name length, including all those '&' we skipped. */
                    310:        len += src - pw->pw_gecos;
1.1       ray       311:        if ((fullname = malloc(len + 1)) == NULL) {
1.12      tedu      312:                err(1, "malloc");
1.1       ray       313:        }
1.13      ray       314:        dst = fullname;
                    315:        src = pw->pw_gecos;
                    316:        while (*src != ',' && *src != '\0') {
                    317:                /* Copy text up to ',' or '&' and skip. */
                    318:                len = strcspn(src, ",&");
                    319:                memcpy(dst, src, len);
                    320:                dst += len;
                    321:                src += len;
                    322:                /* Replace '&' with login. */
                    323:                if (*src == '&') {
                    324:                        memcpy(dst, pw->pw_name, namelen);
                    325:                        *dst = toupper((unsigned char)*dst);
                    326:                        dst += namelen;
                    327:                        ++src;
                    328:                }
                    329:        }
                    330:        *dst = '\0';
1.1       ray       331:
                    332:        sysname[0] = CTL_KERN;
                    333:        sysname[1] = KERN_OSTYPE;
                    334:        len = sizeof(os) - 1;
                    335:        if (sysctl(sysname, 2, &os, &len, NULL, 0) == -1) {
1.12      tedu      336:                err(1, "sysctl");
1.1       ray       337:        }
                    338:
                    339:        sysname[0] = CTL_KERN;
                    340:        sysname[1] = KERN_OSRELEASE;
                    341:        len = sizeof(rel) - 1;
                    342:        if (sysctl(sysname, 2, &rel, &len, NULL, 0) == -1) {
1.12      tedu      343:                err(1, "sysctl");
1.1       ray       344:        }
                    345:
1.14      deraadt   346:        sysname[0] = CTL_KERN;
                    347:        sysname[1] = KERN_VERSION;
                    348:        len = sizeof(details) - 1;
                    349:        if (sysctl(sysname, 2, &details, &len, NULL, 0) == -1) {
                    350:                err(1, "sysctl");
                    351:        }
                    352:
                    353:        cp = strchr(details, '\n');
                    354:        if (cp) {
                    355:                cp++;
                    356:                if (*cp)
                    357:                        *cp++ = '\t';
                    358:                if (*cp)
                    359:                        *cp++ = '\t';
                    360:                if (*cp)
                    361:                        *cp++ = '\t';
                    362:        }
                    363:
1.1       ray       364:        sysname[0] = CTL_HW;
                    365:        sysname[1] = HW_MACHINE;
                    366:        len = sizeof(mach) - 1;
                    367:        if (sysctl(sysname, 2, &mach, &len, NULL, 0) == -1) {
1.12      tedu      368:                err(1, "sysctl");
1.1       ray       369:        }
                    370:
                    371: }
                    372:
                    373: int
                    374: send_file(const char *file, int dst)
                    375: {
1.2       deraadt   376:        int blank = 0;
                    377:        size_t len;
                    378:        char *buf;
1.1       ray       379:        FILE *fp;
                    380:
                    381:        if ((fp = fopen(file, "r")) == NULL)
                    382:                return (-1);
                    383:        while ((buf = fgetln(fp, &len))) {
                    384:                /* Skip lines starting with "SENDBUG". */
                    385:                if (len >= sizeof("SENDBUG") - 1 &&
                    386:                    memcmp(buf, "SENDBUG", sizeof("SENDBUG") - 1) == 0)
                    387:                        continue;
                    388:                if (len == 1 && buf[0] == '\n')
                    389:                        blank = 1;
                    390:                /* Skip comments, but only if we encountered a blank line. */
                    391:                while (len) {
1.14      deraadt   392:                        char *sp = NULL, *ep = NULL;
1.1       ray       393:                        size_t copylen;
                    394:
                    395:                        if (blank && (sp = memchr(buf, '<', len)) != NULL)
                    396:                                ep = memchr(sp, '>', len - (sp - buf + 1));
                    397:                        /* Length of string before comment. */
1.4       ray       398:                        if (ep)
                    399:                                copylen = sp - buf;
                    400:                        else
                    401:                                copylen = len;
1.1       ray       402:                        if (atomicio(vwrite, dst, buf, copylen) != copylen) {
                    403:                                int saved_errno = errno;
                    404:
                    405:                                fclose(fp);
                    406:                                errno = saved_errno;
                    407:                                return (-1);
                    408:                        }
                    409:                        if (!ep)
                    410:                                break;
                    411:                        /* Skip comment. */
                    412:                        len -= ep - buf + 1;
                    413:                        buf = ep + 1;
                    414:                }
                    415:        }
                    416:        fclose(fp);
                    417:        return (0);
                    418: }
                    419:
                    420: void
                    421: template(FILE *fp)
                    422: {
                    423:        fprintf(fp, "SENDBUG: -*- sendbug -*-\n");
                    424:        fprintf(fp, "SENDBUG: Lines starting with `SENDBUG' will be removed automatically, as\n");
                    425:        fprintf(fp, "SENDBUG: will all comments (text enclosed in `<' and `>').              \n");
                    426:        fprintf(fp, "SENDBUG:\n");
                    427:        fprintf(fp, "SENDBUG: Choose from the following categories:\n");
                    428:        fprintf(fp, "SENDBUG:\n");
                    429:        fprintf(fp, "SENDBUG: %s\n", categories);
                    430:        fprintf(fp, "SENDBUG:\n");
                    431:        fprintf(fp, "SENDBUG:\n");
                    432:        fprintf(fp, "To: %s\n", "gnats@openbsd.org");
                    433:        fprintf(fp, "Subject: \n");
                    434:        fprintf(fp, "From: %s\n", pw->pw_name);
                    435:        fprintf(fp, "Cc: \n");
                    436:        fprintf(fp, "Reply-To: %s\n", pw->pw_name);
1.5       deraadt   437:        fprintf(fp, "X-sendbug-version: %s\n", version);
1.1       ray       438:        fprintf(fp, "\n");
                    439:        fprintf(fp, "\n");
                    440:        fprintf(fp, ">Submitter-Id:\tnet\n");
                    441:        fprintf(fp, ">Originator:\t%s\n", fullname);
                    442:        fprintf(fp, ">Organization:\n");
                    443:        fprintf(fp, "net\n");
                    444:        fprintf(fp, ">Synopsis:\t<synopsis of the problem (one line)>\n");
                    445:        fprintf(fp, ">Severity:\t<[ non-critical | serious | critical ] (one line)>\n");
                    446:        fprintf(fp, ">Priority:\t<[ low | medium | high ] (one line)>\n");
                    447:        fprintf(fp, ">Category:\t<PR category (one line)>\n");
                    448:        fprintf(fp, ">Class:\t\t<[ sw-bug | doc-bug | change-request | support ] (one line)>\n");
                    449:        fprintf(fp, ">Release:\t<release number or tag (one line)>\n");
                    450:        fprintf(fp, ">Environment:\n");
                    451:        fprintf(fp, "\t<machine, os, target, libraries (multiple lines)>\n");
                    452:        fprintf(fp, "\tSystem      : %s %s\n", os, rel);
1.14      deraadt   453:        fprintf(fp, "\tDetails     : %s\n", details);
1.1       ray       454:        fprintf(fp, "\tArchitecture: %s.%s\n", os, mach);
                    455:        fprintf(fp, "\tMachine     : %s\n", mach);
                    456:        fprintf(fp, ">Description:\n");
                    457:        fprintf(fp, "\t<precise description of the problem (multiple lines)>\n");
                    458:        fprintf(fp, ">How-To-Repeat:\n");
                    459:        fprintf(fp, "\t<code/input/activities to reproduce the problem (multiple lines)>\n");
                    460:        fprintf(fp, ">Fix:\n");
                    461:        fprintf(fp, "\t<how to correct or work around the problem, if known (multiple lines)>\n");
                    462: }