[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/cgi.c, Revision 1.115

1.115   ! deraadt     1: /* $OpenBSD: cgi.c,v 1.114 2021/08/19 15:21:32 schwarze Exp $ */
1.1       schwarze    2: /*
1.114     schwarze    3:  * Copyright (c) 2014-2019, 2021 Ingo Schwarze <schwarze@usta.de>
1.1       schwarze    4:  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.44      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.44      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.110     schwarze   17:  *
                     18:  * Implementation of the man.cgi(8) program.
1.1       schwarze   19:  */
1.33      schwarze   20: #include <sys/types.h>
                     21: #include <sys/time.h>
                     22:
1.1       schwarze   23: #include <ctype.h>
1.67      schwarze   24: #include <err.h>
1.1       schwarze   25: #include <errno.h>
                     26: #include <fcntl.h>
                     27: #include <limits.h>
1.32      schwarze   28: #include <stdint.h>
1.1       schwarze   29: #include <stdio.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <unistd.h>
                     33:
1.47      schwarze   34: #include "mandoc_aux.h"
1.1       schwarze   35: #include "mandoc.h"
1.47      schwarze   36: #include "roff.h"
1.50      schwarze   37: #include "mdoc.h"
1.51      schwarze   38: #include "man.h"
1.100     schwarze   39: #include "mandoc_parse.h"
1.1       schwarze   40: #include "main.h"
1.44      schwarze   41: #include "manconf.h"
1.1       schwarze   42: #include "mansearch.h"
1.7       schwarze   43: #include "cgi.h"
1.1       schwarze   44:
                     45: /*
                     46:  * A query as passed to the search function.
                     47:  */
                     48: struct query {
1.23      schwarze   49:        char            *manpath; /* desired manual directory */
                     50:        char            *arch; /* architecture */
                     51:        char            *sec; /* manual section */
1.25      schwarze   52:        char            *query; /* unparsed query expression */
1.5       schwarze   53:        int              equal; /* match whole names, not substrings */
1.1       schwarze   54: };
                     55:
                     56: struct req {
                     57:        struct query      q;
                     58:        char            **p; /* array of available manpaths */
                     59:        size_t            psz; /* number of available manpaths */
1.60      schwarze   60:        int               isquery; /* QUERY_STRING used, not PATH_INFO */
1.1       schwarze   61: };
                     62:
1.70      schwarze   63: enum   focus {
                     64:        FOCUS_NONE = 0,
                     65:        FOCUS_QUERY
                     66: };
                     67:
1.1       schwarze   68: static void             html_print(const char *);
                     69: static void             html_putchar(char);
1.43      schwarze   70: static int              http_decode(char *);
1.110     schwarze   71: static void             http_encode(const char *);
1.68      schwarze   72: static void             parse_manpath_conf(struct req *);
1.110     schwarze   73: static void             parse_path_info(struct req *, const char *);
1.68      schwarze   74: static void             parse_query_string(struct req *, const char *);
1.12      schwarze   75: static void             pg_error_badrequest(const char *);
                     76: static void             pg_error_internal(void);
                     77: static void             pg_index(const struct req *);
1.106     schwarze   78: static void             pg_noresult(const struct req *, int, const char *,
                     79:                                const char *);
1.87      schwarze   80: static void             pg_redirect(const struct req *, const char *);
1.6       schwarze   81: static void             pg_search(const struct req *);
1.12      schwarze   82: static void             pg_searchres(const struct req *,
                     83:                                struct manpage *, size_t);
1.19      schwarze   84: static void             pg_show(struct req *, const char *);
1.88      schwarze   85: static void             resp_begin_html(int, const char *, const char *);
1.1       schwarze   86: static void             resp_begin_http(int, const char *);
1.68      schwarze   87: static void             resp_catman(const struct req *, const char *);
1.53      schwarze   88: static void             resp_copy(const char *);
1.1       schwarze   89: static void             resp_end_html(void);
1.68      schwarze   90: static void             resp_format(const struct req *, const char *);
1.70      schwarze   91: static void             resp_searchform(const struct req *, enum focus);
1.10      schwarze   92: static void             resp_show(const struct req *, const char *);
1.25      schwarze   93: static void             set_query_attr(char **, char **);
1.97      schwarze   94: static int              validate_arch(const char *);
1.25      schwarze   95: static int              validate_filename(const char *);
                     96: static int              validate_manpath(const struct req *, const char *);
                     97: static int              validate_urifrag(const char *);
1.1       schwarze   98:
1.58      schwarze   99: static const char       *scriptname = SCRIPT_NAME;
1.1       schwarze  100:
1.10      schwarze  101: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.8       schwarze  102: static const char *const sec_numbers[] = {
                    103:     "0", "1", "2", "3", "3p", "4", "5", "6", "7", "8", "9"
                    104: };
                    105: static const char *const sec_names[] = {
                    106:     "All Sections",
                    107:     "1 - General Commands",
                    108:     "2 - System Calls",
1.34      schwarze  109:     "3 - Library Functions",
                    110:     "3p - Perl Library",
                    111:     "4 - Device Drivers",
1.8       schwarze  112:     "5 - File Formats",
                    113:     "6 - Games",
1.34      schwarze  114:     "7 - Miscellaneous Information",
                    115:     "8 - System Manager\'s Manual",
                    116:     "9 - Kernel Developer\'s Manual"
1.8       schwarze  117: };
                    118: static const int sec_MAX = sizeof(sec_names) / sizeof(char *);
                    119:
                    120: static const char *const arch_names[] = {
1.111     schwarze  121:     "amd64",       "alpha",       "armv7",       "arm64",
                    122:     "hppa",        "i386",        "landisk",     "loongson",
                    123:     "luna88k",     "macppc",      "mips64",      "octeon",
1.113     visa      124:     "powerpc64",   "riscv64",     "sparc64",
1.111     schwarze  125:
1.76      schwarze  126:     "amiga",       "arc",         "armish",      "arm32",
                    127:     "atari",       "aviion",      "beagle",      "cats",
                    128:     "hppa64",      "hp300",
1.64      schwarze  129:     "ia64",        "mac68k",      "mvme68k",     "mvme88k",
                    130:     "mvmeppc",     "palm",        "pc532",       "pegasos",
1.113     visa      131:     "pmax",        "powerpc",     "sgi",         "socppc",
                    132:     "solbourne",   "sparc",
1.79      schwarze  133:     "sun3",        "vax",         "wgrisc",      "x68k",
                    134:     "zaurus"
1.8       schwarze  135: };
                    136: static const int arch_MAX = sizeof(arch_names) / sizeof(char *);
                    137:
1.1       schwarze  138: /*
                    139:  * Print a character, escaping HTML along the way.
                    140:  * This will pass non-ASCII straight to output: be warned!
                    141:  */
                    142: static void
                    143: html_putchar(char c)
                    144: {
                    145:
                    146:        switch (c) {
1.93      schwarze  147:        case '"':
1.80      bentley   148:                printf("&quot;");
1.1       schwarze  149:                break;
1.93      schwarze  150:        case '&':
1.1       schwarze  151:                printf("&amp;");
                    152:                break;
1.93      schwarze  153:        case '>':
1.1       schwarze  154:                printf("&gt;");
                    155:                break;
1.93      schwarze  156:        case '<':
1.1       schwarze  157:                printf("&lt;");
                    158:                break;
                    159:        default:
                    160:                putchar((unsigned char)c);
                    161:                break;
                    162:        }
                    163: }
                    164:
                    165: /*
                    166:  * Call through to html_putchar().
                    167:  * Accepts NULL strings.
                    168:  */
                    169: static void
                    170: html_print(const char *p)
                    171: {
1.43      schwarze  172:
1.1       schwarze  173:        if (NULL == p)
                    174:                return;
                    175:        while ('\0' != *p)
                    176:                html_putchar(*p++);
                    177: }
                    178:
                    179: /*
1.23      schwarze  180:  * Transfer the responsibility for the allocated string *val
                    181:  * to the query structure.
1.1       schwarze  182:  */
                    183: static void
1.23      schwarze  184: set_query_attr(char **attr, char **val)
1.1       schwarze  185: {
                    186:
1.23      schwarze  187:        free(*attr);
                    188:        if (**val == '\0') {
                    189:                *attr = NULL;
                    190:                free(*val);
                    191:        } else
                    192:                *attr = *val;
                    193:        *val = NULL;
                    194: }
                    195:
                    196: /*
                    197:  * Parse the QUERY_STRING for key-value pairs
                    198:  * and store the values into the query structure.
                    199:  */
                    200: static void
1.68      schwarze  201: parse_query_string(struct req *req, const char *qs)
1.23      schwarze  202: {
                    203:        char            *key, *val;
                    204:        size_t           keysz, valsz;
                    205:
1.60      schwarze  206:        req->isquery    = 1;
1.23      schwarze  207:        req->q.manpath  = NULL;
                    208:        req->q.arch     = NULL;
                    209:        req->q.sec      = NULL;
1.25      schwarze  210:        req->q.query    = NULL;
1.23      schwarze  211:        req->q.equal    = 1;
                    212:
                    213:        key = val = NULL;
                    214:        while (*qs != '\0') {
1.1       schwarze  215:
1.23      schwarze  216:                /* Parse one key. */
                    217:
                    218:                keysz = strcspn(qs, "=;&");
                    219:                key = mandoc_strndup(qs, keysz);
                    220:                qs += keysz;
                    221:                if (*qs != '=')
                    222:                        goto next;
                    223:
                    224:                /* Parse one value. */
                    225:
                    226:                valsz = strcspn(++qs, ";&");
                    227:                val = mandoc_strndup(qs, valsz);
                    228:                qs += valsz;
                    229:
                    230:                /* Decode and catch encoding errors. */
1.1       schwarze  231:
1.23      schwarze  232:                if ( ! (http_decode(key) && http_decode(val)))
                    233:                        goto next;
1.1       schwarze  234:
1.23      schwarze  235:                /* Handle key-value pairs. */
1.1       schwarze  236:
1.23      schwarze  237:                if ( ! strcmp(key, "query"))
1.25      schwarze  238:                        set_query_attr(&req->q.query, &val);
1.1       schwarze  239:
1.23      schwarze  240:                else if ( ! strcmp(key, "apropos"))
                    241:                        req->q.equal = !strcmp(val, "0");
                    242:
                    243:                else if ( ! strcmp(key, "manpath")) {
1.13      schwarze  244: #ifdef COMPAT_OLDURI
1.23      schwarze  245:                        if ( ! strncmp(val, "OpenBSD ", 8)) {
1.13      schwarze  246:                                val[7] = '-';
                    247:                                if ('C' == val[8])
                    248:                                        val[8] = 'c';
                    249:                        }
                    250: #endif
1.23      schwarze  251:                        set_query_attr(&req->q.manpath, &val);
                    252:                }
                    253:
                    254:                else if ( ! (strcmp(key, "sec")
1.13      schwarze  255: #ifdef COMPAT_OLDURI
1.23      schwarze  256:                    && strcmp(key, "sektion")
1.13      schwarze  257: #endif
1.23      schwarze  258:                    )) {
                    259:                        if ( ! strcmp(val, "0"))
                    260:                                *val = '\0';
                    261:                        set_query_attr(&req->q.sec, &val);
1.5       schwarze  262:                }
1.23      schwarze  263:
                    264:                else if ( ! strcmp(key, "arch")) {
                    265:                        if ( ! strcmp(val, "default"))
                    266:                                *val = '\0';
                    267:                        set_query_attr(&req->q.arch, &val);
                    268:                }
                    269:
                    270:                /*
                    271:                 * The key must be freed in any case.
                    272:                 * The val may have been handed over to the query
                    273:                 * structure, in which case it is now NULL.
                    274:                 */
                    275: next:
                    276:                free(key);
                    277:                key = NULL;
                    278:                free(val);
                    279:                val = NULL;
                    280:
                    281:                if (*qs != '\0')
                    282:                        qs++;
1.1       schwarze  283:        }
                    284: }
                    285:
                    286: /*
                    287:  * HTTP-decode a string.  The standard explanation is that this turns
                    288:  * "%4e+foo" into "n foo" in the regular way.  This is done in-place
                    289:  * over the allocated string.
                    290:  */
                    291: static int
                    292: http_decode(char *p)
                    293: {
                    294:        char             hex[3];
1.3       tedu      295:        char            *q;
1.1       schwarze  296:        int              c;
                    297:
                    298:        hex[2] = '\0';
                    299:
1.3       tedu      300:        q = p;
                    301:        for ( ; '\0' != *p; p++, q++) {
1.1       schwarze  302:                if ('%' == *p) {
                    303:                        if ('\0' == (hex[0] = *(p + 1)))
1.48      schwarze  304:                                return 0;
1.1       schwarze  305:                        if ('\0' == (hex[1] = *(p + 2)))
1.48      schwarze  306:                                return 0;
1.1       schwarze  307:                        if (1 != sscanf(hex, "%x", &c))
1.48      schwarze  308:                                return 0;
1.1       schwarze  309:                        if ('\0' == c)
1.48      schwarze  310:                                return 0;
1.1       schwarze  311:
1.3       tedu      312:                        *q = (char)c;
                    313:                        p += 2;
1.1       schwarze  314:                } else
1.3       tedu      315:                        *q = '+' == *p ? ' ' : *p;
1.1       schwarze  316:        }
                    317:
1.3       tedu      318:        *q = '\0';
1.48      schwarze  319:        return 1;
1.1       schwarze  320: }
                    321:
                    322: static void
1.97      schwarze  323: http_encode(const char *p)
                    324: {
                    325:        for (; *p != '\0'; p++) {
                    326:                if (isalnum((unsigned char)*p) == 0 &&
                    327:                    strchr("-._~", *p) == NULL)
1.104     schwarze  328:                        printf("%%%2.2X", (unsigned char)*p);
1.97      schwarze  329:                else
                    330:                        putchar(*p);
                    331:        }
                    332: }
                    333:
                    334: static void
1.1       schwarze  335: resp_begin_http(int code, const char *msg)
                    336: {
                    337:
                    338:        if (200 != code)
1.2       tedu      339:                printf("Status: %d %s\r\n", code, msg);
1.1       schwarze  340:
1.2       tedu      341:        printf("Content-Type: text/html; charset=utf-8\r\n"
                    342:             "Cache-Control: no-cache\r\n"
1.107     bentley   343:             "Content-Security-Policy: default-src 'none'; "
                    344:             "style-src 'self' 'unsafe-inline'\r\n"
1.2       tedu      345:             "Pragma: no-cache\r\n"
                    346:             "\r\n");
1.1       schwarze  347:
                    348:        fflush(stdout);
                    349: }
                    350:
                    351: static void
1.53      schwarze  352: resp_copy(const char *filename)
                    353: {
                    354:        char     buf[4096];
                    355:        ssize_t  sz;
                    356:        int      fd;
                    357:
                    358:        if ((fd = open(filename, O_RDONLY)) != -1) {
                    359:                fflush(stdout);
                    360:                while ((sz = read(fd, buf, sizeof(buf))) > 0)
                    361:                        write(STDOUT_FILENO, buf, sz);
1.77      jsg       362:                close(fd);
1.53      schwarze  363:        }
                    364: }
                    365:
                    366: static void
1.88      schwarze  367: resp_begin_html(int code, const char *msg, const char *file)
1.1       schwarze  368: {
1.114     schwarze  369:        const char      *name, *sec, *cp;
                    370:        int              namesz, secsz;
1.1       schwarze  371:
                    372:        resp_begin_http(code, msg);
                    373:
1.37      schwarze  374:        printf("<!DOCTYPE html>\n"
1.65      schwarze  375:               "<html>\n"
                    376:               "<head>\n"
1.82      schwarze  377:               "  <meta charset=\"UTF-8\"/>\n"
1.95      schwarze  378:               "  <meta name=\"viewport\""
                    379:                      " content=\"width=device-width, initial-scale=1.0\">\n"
1.82      schwarze  380:               "  <link rel=\"stylesheet\" href=\"%s/mandoc.css\""
1.65      schwarze  381:               " type=\"text/css\" media=\"all\">\n"
1.88      schwarze  382:               "  <title>",
                    383:               CSS_DIR);
                    384:        if (file != NULL) {
1.114     schwarze  385:                cp = strrchr(file, '/');
                    386:                name = cp == NULL ? file : cp + 1;
                    387:                cp = strrchr(name, '.');
                    388:                namesz = cp == NULL ? strlen(name) : cp - name;
                    389:                sec = NULL;
                    390:                if (cp != NULL && cp[1] != '0') {
                    391:                        sec = cp + 1;
                    392:                        secsz = strlen(sec);
                    393:                } else if (name - file > 1) {
                    394:                        for (cp = name - 2; cp >= file; cp--) {
                    395:                                if (*cp < '1' || *cp > '9')
                    396:                                        continue;
                    397:                                sec = cp;
                    398:                                secsz = name - cp - 1;
                    399:                                break;
                    400:                        }
                    401:                }
                    402:                printf("%.*s", namesz, name);
                    403:                if (sec != NULL)
                    404:                        printf("(%.*s)", secsz, sec);
                    405:                fputs(" - ", stdout);
1.88      schwarze  406:        }
                    407:        printf("%s</title>\n"
1.65      schwarze  408:               "</head>\n"
1.81      schwarze  409:               "<body>\n",
1.88      schwarze  410:               CUSTOMIZE_TITLE);
1.53      schwarze  411:
                    412:        resp_copy(MAN_DIR "/header.html");
1.1       schwarze  413: }
                    414:
                    415: static void
                    416: resp_end_html(void)
                    417: {
                    418:
1.53      schwarze  419:        resp_copy(MAN_DIR "/footer.html");
                    420:
1.65      schwarze  421:        puts("</body>\n"
                    422:             "</html>");
1.1       schwarze  423: }
                    424:
                    425: static void
1.70      schwarze  426: resp_searchform(const struct req *req, enum focus focus)
1.1       schwarze  427: {
                    428:        int              i;
                    429:
1.109     schwarze  430:        printf("<form action=\"/%s\" method=\"get\" "
                    431:               "autocomplete=\"off\" autocapitalize=\"none\">\n"
1.82      schwarze  432:               "  <fieldset>\n"
                    433:               "    <legend>Manual Page Search Parameters</legend>\n",
1.1       schwarze  434:               scriptname);
1.8       schwarze  435:
                    436:        /* Write query input box. */
                    437:
1.96      schwarze  438:        printf("    <input type=\"search\" name=\"query\" value=\"");
1.70      schwarze  439:        if (req->q.query != NULL)
1.25      schwarze  440:                html_print(req->q.query);
1.70      schwarze  441:        printf( "\" size=\"40\"");
                    442:        if (focus == FOCUS_QUERY)
                    443:                printf(" autofocus");
                    444:        puts(">");
1.8       schwarze  445:
1.71      schwarze  446:        /* Write submission buttons. */
1.8       schwarze  447:
1.82      schwarze  448:        printf( "    <button type=\"submit\" name=\"apropos\" value=\"0\">"
1.71      schwarze  449:                "man</button>\n"
1.82      schwarze  450:                "    <button type=\"submit\" name=\"apropos\" value=\"1\">"
                    451:                "apropos</button>\n"
                    452:                "    <br/>\n");
1.8       schwarze  453:
                    454:        /* Write section selector. */
                    455:
1.82      schwarze  456:        puts("    <select name=\"sec\">");
1.8       schwarze  457:        for (i = 0; i < sec_MAX; i++) {
1.82      schwarze  458:                printf("      <option value=\"%s\"", sec_numbers[i]);
1.8       schwarze  459:                if (NULL != req->q.sec &&
                    460:                    0 == strcmp(sec_numbers[i], req->q.sec))
1.65      schwarze  461:                        printf(" selected=\"selected\"");
                    462:                printf(">%s</option>\n", sec_names[i]);
1.8       schwarze  463:        }
1.82      schwarze  464:        puts("    </select>");
1.8       schwarze  465:
                    466:        /* Write architecture selector. */
                    467:
1.82      schwarze  468:        printf( "    <select name=\"arch\">\n"
                    469:                "      <option value=\"default\"");
1.21      schwarze  470:        if (NULL == req->q.arch)
1.65      schwarze  471:                printf(" selected=\"selected\"");
                    472:        puts(">All Architectures</option>");
1.8       schwarze  473:        for (i = 0; i < arch_MAX; i++) {
1.96      schwarze  474:                printf("      <option");
1.8       schwarze  475:                if (NULL != req->q.arch &&
                    476:                    0 == strcmp(arch_names[i], req->q.arch))
1.65      schwarze  477:                        printf(" selected=\"selected\"");
                    478:                printf(">%s</option>\n", arch_names[i]);
1.8       schwarze  479:        }
1.82      schwarze  480:        puts("    </select>");
1.8       schwarze  481:
                    482:        /* Write manpath selector. */
                    483:
1.1       schwarze  484:        if (req->psz > 1) {
1.82      schwarze  485:                puts("    <select name=\"manpath\">");
1.1       schwarze  486:                for (i = 0; i < (int)req->psz; i++) {
1.96      schwarze  487:                        printf("      <option");
1.41      schwarze  488:                        if (strcmp(req->q.manpath, req->p[i]) == 0)
1.96      schwarze  489:                                printf(" selected=\"selected\"");
                    490:                        printf(">");
1.1       schwarze  491:                        html_print(req->p[i]);
1.65      schwarze  492:                        puts("</option>");
1.1       schwarze  493:                }
1.82      schwarze  494:                puts("    </select>");
1.1       schwarze  495:        }
1.8       schwarze  496:
1.82      schwarze  497:        puts("  </fieldset>\n"
1.81      schwarze  498:             "</form>");
1.1       schwarze  499: }
                    500:
1.16      schwarze  501: static int
1.20      schwarze  502: validate_urifrag(const char *frag)
                    503: {
                    504:
                    505:        while ('\0' != *frag) {
                    506:                if ( ! (isalnum((unsigned char)*frag) ||
                    507:                    '-' == *frag || '.' == *frag ||
                    508:                    '/' == *frag || '_' == *frag))
1.48      schwarze  509:                        return 0;
1.20      schwarze  510:                frag++;
                    511:        }
1.48      schwarze  512:        return 1;
1.20      schwarze  513: }
                    514:
                    515: static int
1.17      schwarze  516: validate_manpath(const struct req *req, const char* manpath)
                    517: {
                    518:        size_t   i;
                    519:
                    520:        for (i = 0; i < req->psz; i++)
                    521:                if ( ! strcmp(manpath, req->p[i]))
1.48      schwarze  522:                        return 1;
1.17      schwarze  523:
1.48      schwarze  524:        return 0;
1.17      schwarze  525: }
                    526:
                    527: static int
1.97      schwarze  528: validate_arch(const char *arch)
                    529: {
                    530:        int      i;
                    531:
                    532:        for (i = 0; i < arch_MAX; i++)
                    533:                if (strcmp(arch, arch_names[i]) == 0)
                    534:                        return 1;
                    535:
                    536:        return 0;
                    537: }
                    538:
                    539: static int
1.16      schwarze  540: validate_filename(const char *file)
                    541: {
                    542:
                    543:        if ('.' == file[0] && '/' == file[1])
                    544:                file += 2;
                    545:
1.48      schwarze  546:        return ! (strstr(file, "../") || strstr(file, "/..") ||
                    547:            (strncmp(file, "man", 3) && strncmp(file, "cat", 3)));
1.16      schwarze  548: }
                    549:
1.1       schwarze  550: static void
1.12      schwarze  551: pg_index(const struct req *req)
1.1       schwarze  552: {
                    553:
1.88      schwarze  554:        resp_begin_html(200, NULL, NULL);
1.70      schwarze  555:        resp_searchform(req, FOCUS_QUERY);
1.65      schwarze  556:        printf("<p>\n"
1.26      schwarze  557:               "This web interface is documented in the\n"
1.83      schwarze  558:               "<a class=\"Xr\" href=\"/%s%sman.cgi.8\">man.cgi(8)</a>\n"
1.26      schwarze  559:               "manual, and the\n"
1.83      schwarze  560:               "<a class=\"Xr\" href=\"/%s%sapropos.1\">apropos(1)</a>\n"
1.9       schwarze  561:               "manual explains the query syntax.\n"
1.65      schwarze  562:               "</p>\n",
1.58      schwarze  563:               scriptname, *scriptname == '\0' ? "" : "/",
                    564:               scriptname, *scriptname == '\0' ? "" : "/");
1.1       schwarze  565:        resp_end_html();
                    566: }
                    567:
                    568: static void
1.106     schwarze  569: pg_noresult(const struct req *req, int code, const char *http_msg,
                    570:     const char *user_msg)
1.1       schwarze  571: {
1.106     schwarze  572:        resp_begin_html(code, http_msg, NULL);
1.70      schwarze  573:        resp_searchform(req, FOCUS_QUERY);
1.65      schwarze  574:        puts("<p>");
1.106     schwarze  575:        puts(user_msg);
1.65      schwarze  576:        puts("</p>");
1.1       schwarze  577:        resp_end_html();
                    578: }
                    579:
                    580: static void
1.12      schwarze  581: pg_error_badrequest(const char *msg)
1.1       schwarze  582: {
                    583:
1.88      schwarze  584:        resp_begin_html(400, "Bad Request", NULL);
1.65      schwarze  585:        puts("<h1>Bad Request</h1>\n"
                    586:             "<p>\n");
1.1       schwarze  587:        puts(msg);
                    588:        printf("Try again from the\n"
1.65      schwarze  589:               "<a href=\"/%s\">main page</a>.\n"
                    590:               "</p>", scriptname);
1.1       schwarze  591:        resp_end_html();
                    592: }
                    593:
                    594: static void
1.12      schwarze  595: pg_error_internal(void)
1.1       schwarze  596: {
1.88      schwarze  597:        resp_begin_html(500, "Internal Server Error", NULL);
1.65      schwarze  598:        puts("<p>Internal Server Error</p>");
1.1       schwarze  599:        resp_end_html();
                    600: }
                    601:
                    602: static void
1.87      schwarze  603: pg_redirect(const struct req *req, const char *name)
                    604: {
1.91      schwarze  605:        printf("Status: 303 See Other\r\n"
                    606:            "Location: /");
1.87      schwarze  607:        if (*scriptname != '\0')
                    608:                printf("%s/", scriptname);
                    609:        if (strcmp(req->q.manpath, req->p[0]))
                    610:                printf("%s/", req->q.manpath);
                    611:        if (req->q.arch != NULL)
                    612:                printf("%s/", req->q.arch);
1.97      schwarze  613:        http_encode(name);
                    614:        if (req->q.sec != NULL) {
                    615:                putchar('.');
                    616:                http_encode(req->q.sec);
                    617:        }
1.87      schwarze  618:        printf("\r\nContent-Type: text/html; charset=utf-8\r\n\r\n");
                    619: }
                    620:
                    621: static void
1.12      schwarze  622: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1       schwarze  623: {
1.21      schwarze  624:        char            *arch, *archend;
1.59      schwarze  625:        const char      *sec;
                    626:        size_t           i, iuse;
1.21      schwarze  627:        int              archprio, archpriouse;
1.10      schwarze  628:        int              prio, priouse;
1.1       schwarze  629:
1.16      schwarze  630:        for (i = 0; i < sz; i++) {
                    631:                if (validate_filename(r[i].file))
                    632:                        continue;
1.67      schwarze  633:                warnx("invalid filename %s in %s database",
1.16      schwarze  634:                    r[i].file, req->q.manpath);
                    635:                pg_error_internal();
                    636:                return;
                    637:        }
                    638:
1.60      schwarze  639:        if (req->isquery && sz == 1) {
1.1       schwarze  640:                /*
                    641:                 * If we have just one result, then jump there now
                    642:                 * without any delay.
                    643:                 */
1.91      schwarze  644:                printf("Status: 303 See Other\r\n"
                    645:                    "Location: /");
                    646:                if (*scriptname != '\0')
                    647:                        printf("%s/", scriptname);
                    648:                if (strcmp(req->q.manpath, req->p[0]))
                    649:                        printf("%s/", req->q.manpath);
                    650:                printf("%s\r\n"
                    651:                    "Content-Type: text/html; charset=utf-8\r\n\r\n",
                    652:                    r[0].file);
1.1       schwarze  653:                return;
                    654:        }
                    655:
1.10      schwarze  656:        /*
                    657:         * In man(1) mode, show one of the pages
                    658:         * even if more than one is found.
                    659:         */
                    660:
1.88      schwarze  661:        iuse = 0;
1.62      schwarze  662:        if (req->q.equal || sz == 1) {
1.59      schwarze  663:                priouse = 20;
1.21      schwarze  664:                archpriouse = 3;
1.10      schwarze  665:                for (i = 0; i < sz; i++) {
1.59      schwarze  666:                        sec = r[i].file;
                    667:                        sec += strcspn(sec, "123456789");
                    668:                        if (sec[0] == '\0')
1.10      schwarze  669:                                continue;
1.59      schwarze  670:                        prio = sec_prios[sec[0] - '1'];
                    671:                        if (sec[1] != '/')
                    672:                                prio += 10;
                    673:                        if (req->q.arch == NULL) {
1.21      schwarze  674:                                archprio =
1.59      schwarze  675:                                    ((arch = strchr(sec + 1, '/'))
                    676:                                        == NULL) ? 3 :
                    677:                                    ((archend = strchr(arch + 1, '/'))
                    678:                                        == NULL) ? 0 :
1.21      schwarze  679:                                    strncmp(arch, "amd64/",
                    680:                                        archend - arch) ? 2 : 1;
                    681:                                if (archprio < archpriouse) {
                    682:                                        archpriouse = archprio;
                    683:                                        priouse = prio;
                    684:                                        iuse = i;
                    685:                                        continue;
                    686:                                }
                    687:                                if (archprio > archpriouse)
                    688:                                        continue;
                    689:                        }
1.10      schwarze  690:                        if (prio >= priouse)
                    691:                                continue;
                    692:                        priouse = prio;
                    693:                        iuse = i;
                    694:                }
1.88      schwarze  695:                resp_begin_html(200, NULL, r[iuse].file);
                    696:        } else
                    697:                resp_begin_html(200, NULL, NULL);
                    698:
                    699:        resp_searchform(req,
                    700:            req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
                    701:
                    702:        if (sz > 1) {
                    703:                puts("<table class=\"results\">");
                    704:                for (i = 0; i < sz; i++) {
                    705:                        printf("  <tr>\n"
                    706:                               "    <td>"
1.89      schwarze  707:                               "<a class=\"Xr\" href=\"/");
                    708:                        if (*scriptname != '\0')
                    709:                                printf("%s/", scriptname);
                    710:                        if (strcmp(req->q.manpath, req->p[0]))
                    711:                                printf("%s/", req->q.manpath);
                    712:                        printf("%s\">", r[i].file);
1.88      schwarze  713:                        html_print(r[i].names);
                    714:                        printf("</a></td>\n"
                    715:                               "    <td><span class=\"Nd\">");
                    716:                        html_print(r[i].output);
                    717:                        puts("</span></td>\n"
                    718:                             "  </tr>");
                    719:                }
                    720:                puts("</table>");
                    721:        }
                    722:
                    723:        if (req->q.equal || sz == 1) {
                    724:                puts("<hr>");
1.10      schwarze  725:                resp_show(req, r[iuse].file);
                    726:        }
                    727:
1.1       schwarze  728:        resp_end_html();
                    729: }
                    730:
                    731: static void
1.68      schwarze  732: resp_catman(const struct req *req, const char *file)
1.1       schwarze  733: {
                    734:        FILE            *f;
1.54      schwarze  735:        char            *p;
                    736:        size_t           sz;
                    737:        ssize_t          len;
1.1       schwarze  738:        int              i;
                    739:        int              italic, bold;
                    740:
1.54      schwarze  741:        if ((f = fopen(file, "r")) == NULL) {
1.65      schwarze  742:                puts("<p>You specified an invalid manual file.</p>");
1.1       schwarze  743:                return;
                    744:        }
                    745:
1.65      schwarze  746:        puts("<div class=\"catman\">\n"
                    747:             "<pre>");
1.1       schwarze  748:
1.54      schwarze  749:        p = NULL;
                    750:        sz = 0;
                    751:
                    752:        while ((len = getline(&p, &sz, f)) != -1) {
1.1       schwarze  753:                bold = italic = 0;
1.54      schwarze  754:                for (i = 0; i < len - 1; i++) {
1.43      schwarze  755:                        /*
1.1       schwarze  756:                         * This means that the catpage is out of state.
                    757:                         * Ignore it and keep going (although the
                    758:                         * catpage is bogus).
                    759:                         */
                    760:
                    761:                        if ('\b' == p[i] || '\n' == p[i])
                    762:                                continue;
                    763:
                    764:                        /*
                    765:                         * Print a regular character.
                    766:                         * Close out any bold/italic scopes.
                    767:                         * If we're in back-space mode, make sure we'll
                    768:                         * have something to enter when we backspace.
                    769:                         */
                    770:
                    771:                        if ('\b' != p[i + 1]) {
                    772:                                if (italic)
1.65      schwarze  773:                                        printf("</i>");
1.1       schwarze  774:                                if (bold)
1.65      schwarze  775:                                        printf("</b>");
1.1       schwarze  776:                                italic = bold = 0;
                    777:                                html_putchar(p[i]);
                    778:                                continue;
1.54      schwarze  779:                        } else if (i + 2 >= len)
1.1       schwarze  780:                                continue;
                    781:
                    782:                        /* Italic mode. */
                    783:
                    784:                        if ('_' == p[i]) {
                    785:                                if (bold)
1.65      schwarze  786:                                        printf("</b>");
1.1       schwarze  787:                                if ( ! italic)
1.65      schwarze  788:                                        printf("<i>");
1.1       schwarze  789:                                bold = 0;
                    790:                                italic = 1;
                    791:                                i += 2;
                    792:                                html_putchar(p[i]);
                    793:                                continue;
                    794:                        }
                    795:
1.43      schwarze  796:                        /*
1.1       schwarze  797:                         * Handle funny behaviour troff-isms.
                    798:                         * These grok'd from the original man2html.c.
                    799:                         */
                    800:
                    801:                        if (('+' == p[i] && 'o' == p[i + 2]) ||
                    802:                                        ('o' == p[i] && '+' == p[i + 2]) ||
                    803:                                        ('|' == p[i] && '=' == p[i + 2]) ||
                    804:                                        ('=' == p[i] && '|' == p[i + 2]) ||
                    805:                                        ('*' == p[i] && '=' == p[i + 2]) ||
                    806:                                        ('=' == p[i] && '*' == p[i + 2]) ||
                    807:                                        ('*' == p[i] && '|' == p[i + 2]) ||
                    808:                                        ('|' == p[i] && '*' == p[i + 2]))  {
                    809:                                if (italic)
1.65      schwarze  810:                                        printf("</i>");
1.1       schwarze  811:                                if (bold)
1.65      schwarze  812:                                        printf("</b>");
1.1       schwarze  813:                                italic = bold = 0;
                    814:                                putchar('*');
                    815:                                i += 2;
                    816:                                continue;
                    817:                        } else if (('|' == p[i] && '-' == p[i + 2]) ||
                    818:                                        ('-' == p[i] && '|' == p[i + 1]) ||
                    819:                                        ('+' == p[i] && '-' == p[i + 1]) ||
                    820:                                        ('-' == p[i] && '+' == p[i + 1]) ||
                    821:                                        ('+' == p[i] && '|' == p[i + 1]) ||
                    822:                                        ('|' == p[i] && '+' == p[i + 1]))  {
                    823:                                if (italic)
1.65      schwarze  824:                                        printf("</i>");
1.1       schwarze  825:                                if (bold)
1.65      schwarze  826:                                        printf("</b>");
1.1       schwarze  827:                                italic = bold = 0;
                    828:                                putchar('+');
                    829:                                i += 2;
                    830:                                continue;
                    831:                        }
                    832:
                    833:                        /* Bold mode. */
1.43      schwarze  834:
1.1       schwarze  835:                        if (italic)
1.65      schwarze  836:                                printf("</i>");
1.1       schwarze  837:                        if ( ! bold)
1.65      schwarze  838:                                printf("<b>");
1.1       schwarze  839:                        bold = 1;
                    840:                        italic = 0;
                    841:                        i += 2;
                    842:                        html_putchar(p[i]);
                    843:                }
                    844:
1.43      schwarze  845:                /*
1.1       schwarze  846:                 * Clean up the last character.
1.43      schwarze  847:                 * We can get to a newline; don't print that.
1.1       schwarze  848:                 */
                    849:
                    850:                if (italic)
1.65      schwarze  851:                        printf("</i>");
1.1       schwarze  852:                if (bold)
1.65      schwarze  853:                        printf("</b>");
1.1       schwarze  854:
1.54      schwarze  855:                if (i == len - 1 && p[i] != '\n')
1.1       schwarze  856:                        html_putchar(p[i]);
                    857:
                    858:                putchar('\n');
                    859:        }
1.54      schwarze  860:        free(p);
1.1       schwarze  861:
1.65      schwarze  862:        puts("</pre>\n"
                    863:             "</div>");
1.1       schwarze  864:
                    865:        fclose(f);
                    866: }
                    867:
                    868: static void
1.68      schwarze  869: resp_format(const struct req *req, const char *file)
1.1       schwarze  870: {
1.45      schwarze  871:        struct manoutput conf;
1.1       schwarze  872:        struct mparse   *mp;
1.102     schwarze  873:        struct roff_meta *meta;
1.1       schwarze  874:        void            *vp;
1.30      schwarze  875:        int              fd;
                    876:        int              usepath;
1.1       schwarze  877:
1.115   ! deraadt   878:        if (-1 == (fd = open(file, O_RDONLY))) {
1.65      schwarze  879:                puts("<p>You specified an invalid manual file.</p>");
1.1       schwarze  880:                return;
                    881:        }
                    882:
1.49      schwarze  883:        mchars_alloc();
1.102     schwarze  884:        mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1 |
                    885:            MPARSE_VALIDATE, MANDOC_OS_OTHER, req->q.manpath);
1.42      schwarze  886:        mparse_readfd(mp, fd, file);
1.1       schwarze  887:        close(fd);
1.102     schwarze  888:        meta = mparse_result(mp);
1.1       schwarze  889:
1.45      schwarze  890:        memset(&conf, 0, sizeof(conf));
                    891:        conf.fragment = 1;
1.84      schwarze  892:        conf.style = mandoc_strdup(CSS_DIR "/mandoc.css");
1.30      schwarze  893:        usepath = strcmp(req->q.manpath, req->p[0]);
1.90      schwarze  894:        mandoc_asprintf(&conf.man, "/%s%s%s%s%%N.%%S",
                    895:            scriptname, *scriptname == '\0' ? "" : "/",
1.61      schwarze  896:            usepath ? req->q.manpath : "", usepath ? "/" : "");
1.1       schwarze  897:
1.49      schwarze  898:        vp = html_alloc(&conf);
1.102     schwarze  899:        if (meta->macroset == MACROSET_MDOC)
                    900:                html_mdoc(vp, meta);
                    901:        else
                    902:                html_man(vp, meta);
1.1       schwarze  903:
                    904:        html_free(vp);
                    905:        mparse_free(mp);
1.49      schwarze  906:        mchars_free();
1.45      schwarze  907:        free(conf.man);
1.84      schwarze  908:        free(conf.style);
1.1       schwarze  909: }
                    910:
                    911: static void
1.10      schwarze  912: resp_show(const struct req *req, const char *file)
                    913: {
1.16      schwarze  914:
                    915:        if ('.' == file[0] && '/' == file[1])
1.11      schwarze  916:                file += 2;
1.10      schwarze  917:
                    918:        if ('c' == *file)
1.68      schwarze  919:                resp_catman(req, file);
1.10      schwarze  920:        else
1.68      schwarze  921:                resp_format(req, file);
1.10      schwarze  922: }
                    923:
                    924: static void
1.24      schwarze  925: pg_show(struct req *req, const char *fullpath)
1.1       schwarze  926: {
1.24      schwarze  927:        char            *manpath;
                    928:        const char      *file;
1.1       schwarze  929:
1.24      schwarze  930:        if ((file = strchr(fullpath, '/')) == NULL) {
1.12      schwarze  931:                pg_error_badrequest(
1.1       schwarze  932:                    "You did not specify a page to show.");
                    933:                return;
1.43      schwarze  934:        }
1.24      schwarze  935:        manpath = mandoc_strndup(fullpath, file - fullpath);
                    936:        file++;
1.1       schwarze  937:
1.24      schwarze  938:        if ( ! validate_manpath(req, manpath)) {
1.17      schwarze  939:                pg_error_badrequest(
                    940:                    "You specified an invalid manpath.");
1.24      schwarze  941:                free(manpath);
1.17      schwarze  942:                return;
                    943:        }
                    944:
1.1       schwarze  945:        /*
                    946:         * Begin by chdir()ing into the manpath.
                    947:         * This way we can pick up the database files, which are
                    948:         * relative to the manpath root.
                    949:         */
                    950:
1.24      schwarze  951:        if (chdir(manpath) == -1) {
1.67      schwarze  952:                warn("chdir %s", manpath);
1.17      schwarze  953:                pg_error_internal();
1.24      schwarze  954:                free(manpath);
1.16      schwarze  955:                return;
                    956:        }
1.73      schwarze  957:        free(manpath);
1.24      schwarze  958:
                    959:        if ( ! validate_filename(file)) {
1.16      schwarze  960:                pg_error_badrequest(
                    961:                    "You specified an invalid manual file.");
1.1       schwarze  962:                return;
                    963:        }
1.19      schwarze  964:
1.88      schwarze  965:        resp_begin_html(200, NULL, file);
1.70      schwarze  966:        resp_searchform(req, FOCUS_NONE);
1.24      schwarze  967:        resp_show(req, file);
1.10      schwarze  968:        resp_end_html();
1.1       schwarze  969: }
                    970:
                    971: static void
1.6       schwarze  972: pg_search(const struct req *req)
1.1       schwarze  973: {
                    974:        struct mansearch          search;
                    975:        struct manpaths           paths;
                    976:        struct manpage           *res;
1.36      schwarze  977:        char                    **argv;
                    978:        char                     *query, *rp, *wp;
1.1       schwarze  979:        size_t                    ressz;
1.36      schwarze  980:        int                       argc;
1.1       schwarze  981:
                    982:        /*
                    983:         * Begin by chdir()ing into the root of the manpath.
                    984:         * This way we can pick up the database files, which are
                    985:         * relative to the manpath root.
                    986:         */
                    987:
1.67      schwarze  988:        if (chdir(req->q.manpath) == -1) {
                    989:                warn("chdir %s", req->q.manpath);
1.17      schwarze  990:                pg_error_internal();
1.1       schwarze  991:                return;
                    992:        }
                    993:
                    994:        search.arch = req->q.arch;
                    995:        search.sec = req->q.sec;
1.35      schwarze  996:        search.outkey = "Nd";
                    997:        search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.40      schwarze  998:        search.firstmatch = 1;
1.1       schwarze  999:
                   1000:        paths.sz = 1;
                   1001:        paths.paths = mandoc_malloc(sizeof(char *));
                   1002:        paths.paths[0] = mandoc_strdup(".");
                   1003:
                   1004:        /*
1.36      schwarze 1005:         * Break apart at spaces with backslash-escaping.
1.1       schwarze 1006:         */
                   1007:
1.36      schwarze 1008:        argc = 0;
                   1009:        argv = NULL;
                   1010:        rp = query = mandoc_strdup(req->q.query);
                   1011:        for (;;) {
                   1012:                while (isspace((unsigned char)*rp))
                   1013:                        rp++;
                   1014:                if (*rp == '\0')
                   1015:                        break;
                   1016:                argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
                   1017:                argv[argc++] = wp = rp;
                   1018:                for (;;) {
                   1019:                        if (isspace((unsigned char)*rp)) {
                   1020:                                *wp = '\0';
                   1021:                                rp++;
                   1022:                                break;
                   1023:                        }
                   1024:                        if (rp[0] == '\\' && rp[1] != '\0')
                   1025:                                rp++;
                   1026:                        if (wp != rp)
                   1027:                                *wp = *rp;
                   1028:                        if (*rp == '\0')
                   1029:                                break;
                   1030:                        wp++;
                   1031:                        rp++;
                   1032:                }
1.1       schwarze 1033:        }
                   1034:
1.87      schwarze 1035:        res = NULL;
                   1036:        ressz = 0;
                   1037:        if (req->isquery && req->q.equal && argc == 1)
                   1038:                pg_redirect(req, argv[0]);
                   1039:        else if (mansearch(&search, &paths, argc, argv, &res, &ressz) == 0)
1.106     schwarze 1040:                pg_noresult(req, 400, "Bad Request",
                   1041:                    "You entered an invalid query.");
1.87      schwarze 1042:        else if (ressz == 0)
1.106     schwarze 1043:                pg_noresult(req, 404, "Not Found", "No results found.");
1.1       schwarze 1044:        else
1.12      schwarze 1045:                pg_searchres(req, res, ressz);
1.1       schwarze 1046:
1.36      schwarze 1047:        free(query);
                   1048:        mansearch_free(res, ressz);
1.1       schwarze 1049:        free(paths.paths[0]);
                   1050:        free(paths.paths);
                   1051: }
                   1052:
                   1053: int
                   1054: main(void)
                   1055: {
1.6       schwarze 1056:        struct req       req;
1.33      schwarze 1057:        struct itimerval itimer;
1.6       schwarze 1058:        const char      *path;
1.23      schwarze 1059:        const char      *querystring;
1.1       schwarze 1060:        int              i;
1.86      schwarze 1061:
                   1062:        /*
                   1063:         * The "rpath" pledge could be revoked after mparse_readfd()
                   1064:         * if the file desciptor to "/footer.html" would be opened
                   1065:         * up front, but it's probably not worth the complication
                   1066:         * of the code it would cause: it would require scattering
                   1067:         * pledge() calls in multiple low-level resp_*() functions.
                   1068:         */
                   1069:
                   1070:        if (pledge("stdio rpath", NULL) == -1) {
                   1071:                warn("pledge");
                   1072:                pg_error_internal();
                   1073:                return EXIT_FAILURE;
                   1074:        }
1.33      schwarze 1075:
                   1076:        /* Poor man's ReDoS mitigation. */
                   1077:
1.38      schwarze 1078:        itimer.it_value.tv_sec = 2;
1.33      schwarze 1079:        itimer.it_value.tv_usec = 0;
1.38      schwarze 1080:        itimer.it_interval.tv_sec = 2;
1.33      schwarze 1081:        itimer.it_interval.tv_usec = 0;
                   1082:        if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1.67      schwarze 1083:                warn("setitimer");
1.20      schwarze 1084:                pg_error_internal();
1.48      schwarze 1085:                return EXIT_FAILURE;
1.20      schwarze 1086:        }
                   1087:
1.1       schwarze 1088:        /*
1.7       schwarze 1089:         * First we change directory into the MAN_DIR so that
1.1       schwarze 1090:         * subsequent scanning for manpath directories is rooted
                   1091:         * relative to the same position.
                   1092:         */
                   1093:
1.67      schwarze 1094:        if (chdir(MAN_DIR) == -1) {
                   1095:                warn("MAN_DIR: %s", MAN_DIR);
1.12      schwarze 1096:                pg_error_internal();
1.48      schwarze 1097:                return EXIT_FAILURE;
1.43      schwarze 1098:        }
1.1       schwarze 1099:
                   1100:        memset(&req, 0, sizeof(struct req));
1.57      schwarze 1101:        req.q.equal = 1;
1.68      schwarze 1102:        parse_manpath_conf(&req);
1.1       schwarze 1103:
1.56      schwarze 1104:        /* Parse the path info and the query string. */
1.1       schwarze 1105:
1.56      schwarze 1106:        if ((path = getenv("PATH_INFO")) == NULL)
                   1107:                path = "";
                   1108:        else if (*path == '/')
                   1109:                path++;
                   1110:
1.63      schwarze 1111:        if (*path != '\0') {
1.68      schwarze 1112:                parse_path_info(&req, path);
1.92      schwarze 1113:                if (req.q.manpath == NULL || req.q.sec == NULL ||
                   1114:                    *req.q.query == '\0' || access(path, F_OK) == -1)
1.63      schwarze 1115:                        path = "";
1.56      schwarze 1116:        } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.68      schwarze 1117:                parse_query_string(&req, querystring);
1.17      schwarze 1118:
1.56      schwarze 1119:        /* Validate parsed data and add defaults. */
                   1120:
1.41      schwarze 1121:        if (req.q.manpath == NULL)
                   1122:                req.q.manpath = mandoc_strdup(req.p[0]);
                   1123:        else if ( ! validate_manpath(&req, req.q.manpath)) {
1.17      schwarze 1124:                pg_error_badrequest(
                   1125:                    "You specified an invalid manpath.");
1.48      schwarze 1126:                return EXIT_FAILURE;
1.17      schwarze 1127:        }
1.1       schwarze 1128:
1.97      schwarze 1129:        if (req.q.arch != NULL && validate_arch(req.q.arch) == 0) {
1.20      schwarze 1130:                pg_error_badrequest(
                   1131:                    "You specified an invalid architecture.");
1.48      schwarze 1132:                return EXIT_FAILURE;
1.20      schwarze 1133:        }
                   1134:
1.6       schwarze 1135:        /* Dispatch to the three different pages. */
1.1       schwarze 1136:
1.6       schwarze 1137:        if ('\0' != *path)
                   1138:                pg_show(&req, path);
1.25      schwarze 1139:        else if (NULL != req.q.query)
1.6       schwarze 1140:                pg_search(&req);
                   1141:        else
1.12      schwarze 1142:                pg_index(&req);
1.1       schwarze 1143:
1.23      schwarze 1144:        free(req.q.manpath);
                   1145:        free(req.q.arch);
                   1146:        free(req.q.sec);
1.25      schwarze 1147:        free(req.q.query);
1.1       schwarze 1148:        for (i = 0; i < (int)req.psz; i++)
                   1149:                free(req.p[i]);
                   1150:        free(req.p);
1.48      schwarze 1151:        return EXIT_SUCCESS;
1.56      schwarze 1152: }
                   1153:
                   1154: /*
1.99      schwarze 1155:  * Translate PATH_INFO to a query.
1.56      schwarze 1156:  */
                   1157: static void
1.68      schwarze 1158: parse_path_info(struct req *req, const char *path)
1.56      schwarze 1159: {
1.99      schwarze 1160:        const char      *name, *sec, *end;
1.56      schwarze 1161:
1.60      schwarze 1162:        req->isquery = 0;
1.56      schwarze 1163:        req->q.equal = 1;
1.99      schwarze 1164:        req->q.manpath = NULL;
1.74      schwarze 1165:        req->q.arch = NULL;
1.56      schwarze 1166:
                   1167:        /* Mandatory manual page name. */
1.99      schwarze 1168:        if ((name = strrchr(path, '/')) == NULL)
                   1169:                name = path;
                   1170:        else
                   1171:                name++;
1.56      schwarze 1172:
                   1173:        /* Optional trailing section. */
1.99      schwarze 1174:        sec = strrchr(name, '.');
                   1175:        if (sec != NULL && isdigit((unsigned char)*++sec)) {
                   1176:                req->q.query = mandoc_strndup(name, sec - name - 1);
                   1177:                req->q.sec = mandoc_strdup(sec);
                   1178:        } else {
                   1179:                req->q.query = mandoc_strdup(name);
                   1180:                req->q.sec = NULL;
1.56      schwarze 1181:        }
                   1182:
                   1183:        /* Handle the case of name[.section] only. */
1.99      schwarze 1184:        if (name == path)
1.56      schwarze 1185:                return;
                   1186:
1.74      schwarze 1187:        /* Optional manpath. */
1.99      schwarze 1188:        end = strchr(path, '/');
                   1189:        req->q.manpath = mandoc_strndup(path, end - path);
                   1190:        if (validate_manpath(req, req->q.manpath)) {
                   1191:                path = end + 1;
                   1192:                if (name == path)
                   1193:                        return;
                   1194:        } else {
                   1195:                free(req->q.manpath);
1.74      schwarze 1196:                req->q.manpath = NULL;
1.99      schwarze 1197:        }
1.56      schwarze 1198:
1.74      schwarze 1199:        /* Optional section. */
1.103     schwarze 1200:        if (strncmp(path, "man", 3) == 0 || strncmp(path, "cat", 3) == 0) {
1.99      schwarze 1201:                path += 3;
                   1202:                end = strchr(path, '/');
1.66      schwarze 1203:                free(req->q.sec);
1.99      schwarze 1204:                req->q.sec = mandoc_strndup(path, end - path);
                   1205:                path = end + 1;
                   1206:                if (name == path)
                   1207:                        return;
1.56      schwarze 1208:        }
1.99      schwarze 1209:
                   1210:        /* Optional architecture. */
                   1211:        end = strchr(path, '/');
                   1212:        if (end + 1 != name) {
                   1213:                pg_error_badrequest(
                   1214:                    "You specified too many directory components.");
                   1215:                exit(EXIT_FAILURE);
1.74      schwarze 1216:        }
1.99      schwarze 1217:        req->q.arch = mandoc_strndup(path, end - path);
                   1218:        if (validate_arch(req->q.arch) == 0) {
1.74      schwarze 1219:                pg_error_badrequest(
                   1220:                    "You specified an invalid directory component.");
                   1221:                exit(EXIT_FAILURE);
                   1222:        }
1.1       schwarze 1223: }
                   1224:
                   1225: /*
                   1226:  * Scan for indexable paths.
                   1227:  */
                   1228: static void
1.68      schwarze 1229: parse_manpath_conf(struct req *req)
1.1       schwarze 1230: {
                   1231:        FILE    *fp;
                   1232:        char    *dp;
                   1233:        size_t   dpsz;
1.54      schwarze 1234:        ssize_t  len;
1.1       schwarze 1235:
1.67      schwarze 1236:        if ((fp = fopen("manpath.conf", "r")) == NULL) {
                   1237:                warn("%s/manpath.conf", MAN_DIR);
1.14      schwarze 1238:                pg_error_internal();
                   1239:                exit(EXIT_FAILURE);
                   1240:        }
1.1       schwarze 1241:
1.54      schwarze 1242:        dp = NULL;
                   1243:        dpsz = 0;
                   1244:
                   1245:        while ((len = getline(&dp, &dpsz, fp)) != -1) {
                   1246:                if (dp[len - 1] == '\n')
                   1247:                        dp[--len] = '\0';
1.1       schwarze 1248:                req->p = mandoc_realloc(req->p,
                   1249:                    (req->psz + 1) * sizeof(char *));
1.20      schwarze 1250:                if ( ! validate_urifrag(dp)) {
1.67      schwarze 1251:                        warnx("%s/manpath.conf contains "
                   1252:                            "unsafe path \"%s\"", MAN_DIR, dp);
1.20      schwarze 1253:                        pg_error_internal();
                   1254:                        exit(EXIT_FAILURE);
                   1255:                }
1.67      schwarze 1256:                if (strchr(dp, '/') != NULL) {
                   1257:                        warnx("%s/manpath.conf contains "
                   1258:                            "path with slash \"%s\"", MAN_DIR, dp);
1.20      schwarze 1259:                        pg_error_internal();
                   1260:                        exit(EXIT_FAILURE);
                   1261:                }
                   1262:                req->p[req->psz++] = dp;
1.54      schwarze 1263:                dp = NULL;
                   1264:                dpsz = 0;
1.14      schwarze 1265:        }
1.54      schwarze 1266:        free(dp);
1.14      schwarze 1267:
1.67      schwarze 1268:        if (req->p == NULL) {
                   1269:                warnx("%s/manpath.conf is empty", MAN_DIR);
1.14      schwarze 1270:                pg_error_internal();
                   1271:                exit(EXIT_FAILURE);
1.1       schwarze 1272:        }
                   1273: }