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

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