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

1.76    ! schwarze    1: /*     $OpenBSD: cgi.c,v 1.75 2016/07/31 23:35:26 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.57      schwarze    4:  * Copyright (c) 2014, 2015, 2016 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",
                    117:     "octeon",      "sgi",         "socppc",      "sparc",
                    118:     "sparc64",     "zaurus",
1.76    ! schwarze  119:     "amiga",       "arc",         "armish",      "arm32",
        !           120:     "atari",       "aviion",      "beagle",      "cats",
        !           121:     "hppa64",      "hp300",
1.64      schwarze  122:     "ia64",        "mac68k",      "mvme68k",     "mvme88k",
                    123:     "mvmeppc",     "palm",        "pc532",       "pegasos",
                    124:     "pmax",        "powerpc",     "solbourne",   "sun3",
                    125:     "vax",         "wgrisc",      "x68k"
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 ('"'):
                    139:                printf("&quote;");
                    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);
                    339:        }
                    340: }
                    341:
                    342: static void
1.1       schwarze  343: resp_begin_html(int code, const char *msg)
                    344: {
                    345:
                    346:        resp_begin_http(code, msg);
                    347:
1.37      schwarze  348:        printf("<!DOCTYPE html>\n"
1.65      schwarze  349:               "<html>\n"
                    350:               "<head>\n"
                    351:               "<meta charset=\"UTF-8\"/>\n"
                    352:               "<link rel=\"stylesheet\" href=\"%s/mandoc.css\""
                    353:               " type=\"text/css\" media=\"all\">\n"
                    354:               "<title>%s</title>\n"
                    355:               "</head>\n"
                    356:               "<body>\n"
1.1       schwarze  357:               "<!-- Begin page content. //-->\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:
                    378:        puts("<!-- Begin search form. //-->");
1.65      schwarze  379:        printf("<div id=\"mancgi\">\n"
                    380:               "<form action=\"/%s\" method=\"get\">\n"
                    381:               "<fieldset>\n"
                    382:               "<legend>Manual Page Search Parameters</legend>\n",
1.1       schwarze  383:               scriptname);
1.8       schwarze  384:
                    385:        /* Write query input box. */
                    386:
1.71      schwarze  387:        printf("<input type=\"text\" name=\"query\" value=\"");
1.70      schwarze  388:        if (req->q.query != NULL)
1.25      schwarze  389:                html_print(req->q.query);
1.70      schwarze  390:        printf( "\" size=\"40\"");
                    391:        if (focus == FOCUS_QUERY)
                    392:                printf(" autofocus");
                    393:        puts(">");
1.8       schwarze  394:
1.71      schwarze  395:        /* Write submission buttons. */
1.8       schwarze  396:
1.71      schwarze  397:        printf( "<button type=\"submit\" name=\"apropos\" value=\"0\">"
                    398:                "man</button>\n"
                    399:                "<button type=\"submit\" name=\"apropos\" value=\"1\">"
                    400:                "apropos</button>\n<br/>\n");
1.8       schwarze  401:
                    402:        /* Write section selector. */
                    403:
1.71      schwarze  404:        puts("<select name=\"sec\">");
1.8       schwarze  405:        for (i = 0; i < sec_MAX; i++) {
1.65      schwarze  406:                printf("<option value=\"%s\"", sec_numbers[i]);
1.8       schwarze  407:                if (NULL != req->q.sec &&
                    408:                    0 == strcmp(sec_numbers[i], req->q.sec))
1.65      schwarze  409:                        printf(" selected=\"selected\"");
                    410:                printf(">%s</option>\n", sec_names[i]);
1.8       schwarze  411:        }
1.65      schwarze  412:        puts("</select>");
1.8       schwarze  413:
                    414:        /* Write architecture selector. */
                    415:
1.65      schwarze  416:        printf( "<select name=\"arch\">\n"
                    417:                "<option value=\"default\"");
1.21      schwarze  418:        if (NULL == req->q.arch)
1.65      schwarze  419:                printf(" selected=\"selected\"");
                    420:        puts(">All Architectures</option>");
1.8       schwarze  421:        for (i = 0; i < arch_MAX; i++) {
1.65      schwarze  422:                printf("<option value=\"%s\"", arch_names[i]);
1.8       schwarze  423:                if (NULL != req->q.arch &&
                    424:                    0 == strcmp(arch_names[i], req->q.arch))
1.65      schwarze  425:                        printf(" selected=\"selected\"");
                    426:                printf(">%s</option>\n", arch_names[i]);
1.8       schwarze  427:        }
1.65      schwarze  428:        puts("</select>");
1.8       schwarze  429:
                    430:        /* Write manpath selector. */
                    431:
1.1       schwarze  432:        if (req->psz > 1) {
1.65      schwarze  433:                puts("<select name=\"manpath\">");
1.1       schwarze  434:                for (i = 0; i < (int)req->psz; i++) {
1.65      schwarze  435:                        printf("<option ");
1.41      schwarze  436:                        if (strcmp(req->q.manpath, req->p[i]) == 0)
1.65      schwarze  437:                                printf("selected=\"selected\" ");
                    438:                        printf("value=\"");
1.1       schwarze  439:                        html_print(req->p[i]);
                    440:                        printf("\">");
                    441:                        html_print(req->p[i]);
1.65      schwarze  442:                        puts("</option>");
1.1       schwarze  443:                }
1.65      schwarze  444:                puts("</select>");
1.1       schwarze  445:        }
1.8       schwarze  446:
1.71      schwarze  447:        puts("</fieldset>\n"
1.65      schwarze  448:             "</form>\n"
                    449:             "</div>");
1.1       schwarze  450:        puts("<!-- End search form. //-->");
                    451: }
                    452:
1.16      schwarze  453: static int
1.20      schwarze  454: validate_urifrag(const char *frag)
                    455: {
                    456:
                    457:        while ('\0' != *frag) {
                    458:                if ( ! (isalnum((unsigned char)*frag) ||
                    459:                    '-' == *frag || '.' == *frag ||
                    460:                    '/' == *frag || '_' == *frag))
1.48      schwarze  461:                        return 0;
1.20      schwarze  462:                frag++;
                    463:        }
1.48      schwarze  464:        return 1;
1.20      schwarze  465: }
                    466:
                    467: static int
1.17      schwarze  468: validate_manpath(const struct req *req, const char* manpath)
                    469: {
                    470:        size_t   i;
                    471:
                    472:        for (i = 0; i < req->psz; i++)
                    473:                if ( ! strcmp(manpath, req->p[i]))
1.48      schwarze  474:                        return 1;
1.17      schwarze  475:
1.48      schwarze  476:        return 0;
1.17      schwarze  477: }
                    478:
                    479: static int
1.16      schwarze  480: validate_filename(const char *file)
                    481: {
                    482:
                    483:        if ('.' == file[0] && '/' == file[1])
                    484:                file += 2;
                    485:
1.48      schwarze  486:        return ! (strstr(file, "../") || strstr(file, "/..") ||
                    487:            (strncmp(file, "man", 3) && strncmp(file, "cat", 3)));
1.16      schwarze  488: }
                    489:
1.1       schwarze  490: static void
1.12      schwarze  491: pg_index(const struct req *req)
1.1       schwarze  492: {
                    493:
                    494:        resp_begin_html(200, NULL);
1.70      schwarze  495:        resp_searchform(req, FOCUS_QUERY);
1.65      schwarze  496:        printf("<p>\n"
1.26      schwarze  497:               "This web interface is documented in the\n"
1.73      schwarze  498:               "<a href=\"/%s%sman.cgi.8\">man.cgi(8)</a>\n"
1.26      schwarze  499:               "manual, and the\n"
1.73      schwarze  500:               "<a href=\"/%s%sapropos.1\">apropos(1)</a>\n"
1.9       schwarze  501:               "manual explains the query syntax.\n"
1.65      schwarze  502:               "</p>\n",
1.58      schwarze  503:               scriptname, *scriptname == '\0' ? "" : "/",
                    504:               scriptname, *scriptname == '\0' ? "" : "/");
1.1       schwarze  505:        resp_end_html();
                    506: }
                    507:
                    508: static void
1.12      schwarze  509: pg_noresult(const struct req *req, const char *msg)
1.1       schwarze  510: {
                    511:        resp_begin_html(200, NULL);
1.70      schwarze  512:        resp_searchform(req, FOCUS_QUERY);
1.65      schwarze  513:        puts("<p>");
1.1       schwarze  514:        puts(msg);
1.65      schwarze  515:        puts("</p>");
1.1       schwarze  516:        resp_end_html();
                    517: }
                    518:
                    519: static void
1.12      schwarze  520: pg_error_badrequest(const char *msg)
1.1       schwarze  521: {
                    522:
                    523:        resp_begin_html(400, "Bad Request");
1.65      schwarze  524:        puts("<h1>Bad Request</h1>\n"
                    525:             "<p>\n");
1.1       schwarze  526:        puts(msg);
                    527:        printf("Try again from the\n"
1.65      schwarze  528:               "<a href=\"/%s\">main page</a>.\n"
                    529:               "</p>", scriptname);
1.1       schwarze  530:        resp_end_html();
                    531: }
                    532:
                    533: static void
1.12      schwarze  534: pg_error_internal(void)
1.1       schwarze  535: {
                    536:        resp_begin_html(500, "Internal Server Error");
1.65      schwarze  537:        puts("<p>Internal Server Error</p>");
1.1       schwarze  538:        resp_end_html();
                    539: }
                    540:
                    541: static void
1.12      schwarze  542: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1       schwarze  543: {
1.21      schwarze  544:        char            *arch, *archend;
1.59      schwarze  545:        const char      *sec;
                    546:        size_t           i, iuse;
1.21      schwarze  547:        int              archprio, archpriouse;
1.10      schwarze  548:        int              prio, priouse;
1.1       schwarze  549:
1.16      schwarze  550:        for (i = 0; i < sz; i++) {
                    551:                if (validate_filename(r[i].file))
                    552:                        continue;
1.67      schwarze  553:                warnx("invalid filename %s in %s database",
1.16      schwarze  554:                    r[i].file, req->q.manpath);
                    555:                pg_error_internal();
                    556:                return;
                    557:        }
                    558:
1.60      schwarze  559:        if (req->isquery && sz == 1) {
1.1       schwarze  560:                /*
                    561:                 * If we have just one result, then jump there now
                    562:                 * without any delay.
                    563:                 */
1.2       tedu      564:                printf("Status: 303 See Other\r\n");
1.58      schwarze  565:                printf("Location: http://%s/%s%s%s/%s",
                    566:                    HTTP_HOST, scriptname,
                    567:                    *scriptname == '\0' ? "" : "/",
                    568:                    req->q.manpath, r[0].file);
1.2       tedu      569:                printf("\r\n"
                    570:                     "Content-Type: text/html; charset=utf-8\r\n"
                    571:                     "\r\n");
1.1       schwarze  572:                return;
                    573:        }
                    574:
                    575:        resp_begin_html(200, NULL);
1.70      schwarze  576:        resp_searchform(req,
                    577:            req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
1.1       schwarze  578:
1.62      schwarze  579:        if (sz > 1) {
1.65      schwarze  580:                puts("<div class=\"results\">");
                    581:                puts("<table>");
1.62      schwarze  582:
                    583:                for (i = 0; i < sz; i++) {
1.65      schwarze  584:                        printf("<tr>\n"
                    585:                               "<td class=\"title\">\n"
                    586:                               "<a href=\"/%s%s%s/%s",
1.62      schwarze  587:                            scriptname, *scriptname == '\0' ? "" : "/",
                    588:                            req->q.manpath, r[i].file);
                    589:                        printf("\">");
                    590:                        html_print(r[i].names);
1.65      schwarze  591:                        printf("</a>\n"
                    592:                               "</td>\n"
                    593:                               "<td class=\"desc\">");
1.62      schwarze  594:                        html_print(r[i].output);
1.65      schwarze  595:                        puts("</td>\n"
                    596:                             "</tr>");
1.62      schwarze  597:                }
                    598:
1.65      schwarze  599:                puts("</table>\n"
                    600:                     "</div>");
1.1       schwarze  601:        }
                    602:
1.10      schwarze  603:        /*
                    604:         * In man(1) mode, show one of the pages
                    605:         * even if more than one is found.
                    606:         */
                    607:
1.62      schwarze  608:        if (req->q.equal || sz == 1) {
1.65      schwarze  609:                puts("<hr>");
1.10      schwarze  610:                iuse = 0;
1.59      schwarze  611:                priouse = 20;
1.21      schwarze  612:                archpriouse = 3;
1.10      schwarze  613:                for (i = 0; i < sz; i++) {
1.59      schwarze  614:                        sec = r[i].file;
                    615:                        sec += strcspn(sec, "123456789");
                    616:                        if (sec[0] == '\0')
1.10      schwarze  617:                                continue;
1.59      schwarze  618:                        prio = sec_prios[sec[0] - '1'];
                    619:                        if (sec[1] != '/')
                    620:                                prio += 10;
                    621:                        if (req->q.arch == NULL) {
1.21      schwarze  622:                                archprio =
1.59      schwarze  623:                                    ((arch = strchr(sec + 1, '/'))
                    624:                                        == NULL) ? 3 :
                    625:                                    ((archend = strchr(arch + 1, '/'))
                    626:                                        == NULL) ? 0 :
1.21      schwarze  627:                                    strncmp(arch, "amd64/",
                    628:                                        archend - arch) ? 2 : 1;
                    629:                                if (archprio < archpriouse) {
                    630:                                        archpriouse = archprio;
                    631:                                        priouse = prio;
                    632:                                        iuse = i;
                    633:                                        continue;
                    634:                                }
                    635:                                if (archprio > archpriouse)
                    636:                                        continue;
                    637:                        }
1.10      schwarze  638:                        if (prio >= priouse)
                    639:                                continue;
                    640:                        priouse = prio;
                    641:                        iuse = i;
                    642:                }
                    643:                resp_show(req, r[iuse].file);
                    644:        }
                    645:
1.1       schwarze  646:        resp_end_html();
                    647: }
                    648:
                    649: static void
1.68      schwarze  650: resp_catman(const struct req *req, const char *file)
1.1       schwarze  651: {
                    652:        FILE            *f;
1.54      schwarze  653:        char            *p;
                    654:        size_t           sz;
                    655:        ssize_t          len;
1.1       schwarze  656:        int              i;
                    657:        int              italic, bold;
                    658:
1.54      schwarze  659:        if ((f = fopen(file, "r")) == NULL) {
1.65      schwarze  660:                puts("<p>You specified an invalid manual file.</p>");
1.1       schwarze  661:                return;
                    662:        }
                    663:
1.65      schwarze  664:        puts("<div class=\"catman\">\n"
                    665:             "<pre>");
1.1       schwarze  666:
1.54      schwarze  667:        p = NULL;
                    668:        sz = 0;
                    669:
                    670:        while ((len = getline(&p, &sz, f)) != -1) {
1.1       schwarze  671:                bold = italic = 0;
1.54      schwarze  672:                for (i = 0; i < len - 1; i++) {
1.43      schwarze  673:                        /*
1.1       schwarze  674:                         * This means that the catpage is out of state.
                    675:                         * Ignore it and keep going (although the
                    676:                         * catpage is bogus).
                    677:                         */
                    678:
                    679:                        if ('\b' == p[i] || '\n' == p[i])
                    680:                                continue;
                    681:
                    682:                        /*
                    683:                         * Print a regular character.
                    684:                         * Close out any bold/italic scopes.
                    685:                         * If we're in back-space mode, make sure we'll
                    686:                         * have something to enter when we backspace.
                    687:                         */
                    688:
                    689:                        if ('\b' != p[i + 1]) {
                    690:                                if (italic)
1.65      schwarze  691:                                        printf("</i>");
1.1       schwarze  692:                                if (bold)
1.65      schwarze  693:                                        printf("</b>");
1.1       schwarze  694:                                italic = bold = 0;
                    695:                                html_putchar(p[i]);
                    696:                                continue;
1.54      schwarze  697:                        } else if (i + 2 >= len)
1.1       schwarze  698:                                continue;
                    699:
                    700:                        /* Italic mode. */
                    701:
                    702:                        if ('_' == p[i]) {
                    703:                                if (bold)
1.65      schwarze  704:                                        printf("</b>");
1.1       schwarze  705:                                if ( ! italic)
1.65      schwarze  706:                                        printf("<i>");
1.1       schwarze  707:                                bold = 0;
                    708:                                italic = 1;
                    709:                                i += 2;
                    710:                                html_putchar(p[i]);
                    711:                                continue;
                    712:                        }
                    713:
1.43      schwarze  714:                        /*
1.1       schwarze  715:                         * Handle funny behaviour troff-isms.
                    716:                         * These grok'd from the original man2html.c.
                    717:                         */
                    718:
                    719:                        if (('+' == p[i] && 'o' == p[i + 2]) ||
                    720:                                        ('o' == p[i] && '+' == p[i + 2]) ||
                    721:                                        ('|' == p[i] && '=' == p[i + 2]) ||
                    722:                                        ('=' == p[i] && '|' == p[i + 2]) ||
                    723:                                        ('*' == p[i] && '=' == p[i + 2]) ||
                    724:                                        ('=' == p[i] && '*' == p[i + 2]) ||
                    725:                                        ('*' == p[i] && '|' == p[i + 2]) ||
                    726:                                        ('|' == p[i] && '*' == p[i + 2]))  {
                    727:                                if (italic)
1.65      schwarze  728:                                        printf("</i>");
1.1       schwarze  729:                                if (bold)
1.65      schwarze  730:                                        printf("</b>");
1.1       schwarze  731:                                italic = bold = 0;
                    732:                                putchar('*');
                    733:                                i += 2;
                    734:                                continue;
                    735:                        } else if (('|' == p[i] && '-' == p[i + 2]) ||
                    736:                                        ('-' == p[i] && '|' == p[i + 1]) ||
                    737:                                        ('+' == p[i] && '-' == p[i + 1]) ||
                    738:                                        ('-' == p[i] && '+' == p[i + 1]) ||
                    739:                                        ('+' == p[i] && '|' == p[i + 1]) ||
                    740:                                        ('|' == p[i] && '+' == p[i + 1]))  {
                    741:                                if (italic)
1.65      schwarze  742:                                        printf("</i>");
1.1       schwarze  743:                                if (bold)
1.65      schwarze  744:                                        printf("</b>");
1.1       schwarze  745:                                italic = bold = 0;
                    746:                                putchar('+');
                    747:                                i += 2;
                    748:                                continue;
                    749:                        }
                    750:
                    751:                        /* Bold mode. */
1.43      schwarze  752:
1.1       schwarze  753:                        if (italic)
1.65      schwarze  754:                                printf("</i>");
1.1       schwarze  755:                        if ( ! bold)
1.65      schwarze  756:                                printf("<b>");
1.1       schwarze  757:                        bold = 1;
                    758:                        italic = 0;
                    759:                        i += 2;
                    760:                        html_putchar(p[i]);
                    761:                }
                    762:
1.43      schwarze  763:                /*
1.1       schwarze  764:                 * Clean up the last character.
1.43      schwarze  765:                 * We can get to a newline; don't print that.
1.1       schwarze  766:                 */
                    767:
                    768:                if (italic)
1.65      schwarze  769:                        printf("</i>");
1.1       schwarze  770:                if (bold)
1.65      schwarze  771:                        printf("</b>");
1.1       schwarze  772:
1.54      schwarze  773:                if (i == len - 1 && p[i] != '\n')
1.1       schwarze  774:                        html_putchar(p[i]);
                    775:
                    776:                putchar('\n');
                    777:        }
1.54      schwarze  778:        free(p);
1.1       schwarze  779:
1.65      schwarze  780:        puts("</pre>\n"
                    781:             "</div>");
1.1       schwarze  782:
                    783:        fclose(f);
                    784: }
                    785:
                    786: static void
1.68      schwarze  787: resp_format(const struct req *req, const char *file)
1.1       schwarze  788: {
1.45      schwarze  789:        struct manoutput conf;
1.1       schwarze  790:        struct mparse   *mp;
1.46      schwarze  791:        struct roff_man *man;
1.1       schwarze  792:        void            *vp;
1.30      schwarze  793:        int              fd;
                    794:        int              usepath;
1.1       schwarze  795:
                    796:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.65      schwarze  797:                puts("<p>You specified an invalid manual file.</p>");
1.1       schwarze  798:                return;
                    799:        }
                    800:
1.49      schwarze  801:        mchars_alloc();
1.75      schwarze  802:        mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1,
                    803:            MANDOCLEVEL_BADARG, NULL, req->q.manpath);
1.42      schwarze  804:        mparse_readfd(mp, fd, file);
1.1       schwarze  805:        close(fd);
                    806:
1.45      schwarze  807:        memset(&conf, 0, sizeof(conf));
                    808:        conf.fragment = 1;
1.30      schwarze  809:        usepath = strcmp(req->q.manpath, req->p[0]);
1.61      schwarze  810:        mandoc_asprintf(&conf.man, "/%s%s%%N.%%S",
                    811:            usepath ? req->q.manpath : "", usepath ? "/" : "");
1.1       schwarze  812:
1.47      schwarze  813:        mparse_result(mp, &man, NULL);
                    814:        if (man == NULL) {
1.67      schwarze  815:                warnx("fatal mandoc error: %s/%s", req->q.manpath, file);
1.12      schwarze  816:                pg_error_internal();
1.1       schwarze  817:                mparse_free(mp);
1.49      schwarze  818:                mchars_free();
1.1       schwarze  819:                return;
                    820:        }
                    821:
1.49      schwarze  822:        vp = html_alloc(&conf);
1.1       schwarze  823:
1.50      schwarze  824:        if (man->macroset == MACROSET_MDOC) {
                    825:                mdoc_validate(man);
1.47      schwarze  826:                html_mdoc(vp, man);
1.51      schwarze  827:        } else {
                    828:                man_validate(man);
1.1       schwarze  829:                html_man(vp, man);
1.51      schwarze  830:        }
1.1       schwarze  831:
                    832:        html_free(vp);
                    833:        mparse_free(mp);
1.49      schwarze  834:        mchars_free();
1.45      schwarze  835:        free(conf.man);
1.1       schwarze  836: }
                    837:
                    838: static void
1.10      schwarze  839: resp_show(const struct req *req, const char *file)
                    840: {
1.16      schwarze  841:
                    842:        if ('.' == file[0] && '/' == file[1])
1.11      schwarze  843:                file += 2;
1.10      schwarze  844:
                    845:        if ('c' == *file)
1.68      schwarze  846:                resp_catman(req, file);
1.10      schwarze  847:        else
1.68      schwarze  848:                resp_format(req, file);
1.10      schwarze  849: }
                    850:
                    851: static void
1.24      schwarze  852: pg_show(struct req *req, const char *fullpath)
1.1       schwarze  853: {
1.24      schwarze  854:        char            *manpath;
                    855:        const char      *file;
1.1       schwarze  856:
1.24      schwarze  857:        if ((file = strchr(fullpath, '/')) == NULL) {
1.12      schwarze  858:                pg_error_badrequest(
1.1       schwarze  859:                    "You did not specify a page to show.");
                    860:                return;
1.43      schwarze  861:        }
1.24      schwarze  862:        manpath = mandoc_strndup(fullpath, file - fullpath);
                    863:        file++;
1.1       schwarze  864:
1.24      schwarze  865:        if ( ! validate_manpath(req, manpath)) {
1.17      schwarze  866:                pg_error_badrequest(
                    867:                    "You specified an invalid manpath.");
1.24      schwarze  868:                free(manpath);
1.17      schwarze  869:                return;
                    870:        }
                    871:
1.1       schwarze  872:        /*
                    873:         * Begin by chdir()ing into the manpath.
                    874:         * This way we can pick up the database files, which are
                    875:         * relative to the manpath root.
                    876:         */
                    877:
1.24      schwarze  878:        if (chdir(manpath) == -1) {
1.67      schwarze  879:                warn("chdir %s", manpath);
1.17      schwarze  880:                pg_error_internal();
1.24      schwarze  881:                free(manpath);
1.16      schwarze  882:                return;
                    883:        }
1.73      schwarze  884:        free(manpath);
1.24      schwarze  885:
                    886:        if ( ! validate_filename(file)) {
1.16      schwarze  887:                pg_error_badrequest(
                    888:                    "You specified an invalid manual file.");
1.1       schwarze  889:                return;
                    890:        }
1.19      schwarze  891:
1.10      schwarze  892:        resp_begin_html(200, NULL);
1.70      schwarze  893:        resp_searchform(req, FOCUS_NONE);
1.24      schwarze  894:        resp_show(req, file);
1.10      schwarze  895:        resp_end_html();
1.1       schwarze  896: }
                    897:
                    898: static void
1.6       schwarze  899: pg_search(const struct req *req)
1.1       schwarze  900: {
                    901:        struct mansearch          search;
                    902:        struct manpaths           paths;
                    903:        struct manpage           *res;
1.36      schwarze  904:        char                    **argv;
                    905:        char                     *query, *rp, *wp;
1.1       schwarze  906:        size_t                    ressz;
1.36      schwarze  907:        int                       argc;
1.1       schwarze  908:
                    909:        /*
                    910:         * Begin by chdir()ing into the root of the manpath.
                    911:         * This way we can pick up the database files, which are
                    912:         * relative to the manpath root.
                    913:         */
                    914:
1.67      schwarze  915:        if (chdir(req->q.manpath) == -1) {
                    916:                warn("chdir %s", req->q.manpath);
1.17      schwarze  917:                pg_error_internal();
1.1       schwarze  918:                return;
                    919:        }
                    920:
                    921:        search.arch = req->q.arch;
                    922:        search.sec = req->q.sec;
1.35      schwarze  923:        search.outkey = "Nd";
                    924:        search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.40      schwarze  925:        search.firstmatch = 1;
1.1       schwarze  926:
                    927:        paths.sz = 1;
                    928:        paths.paths = mandoc_malloc(sizeof(char *));
                    929:        paths.paths[0] = mandoc_strdup(".");
                    930:
                    931:        /*
1.36      schwarze  932:         * Break apart at spaces with backslash-escaping.
1.1       schwarze  933:         */
                    934:
1.36      schwarze  935:        argc = 0;
                    936:        argv = NULL;
                    937:        rp = query = mandoc_strdup(req->q.query);
                    938:        for (;;) {
                    939:                while (isspace((unsigned char)*rp))
                    940:                        rp++;
                    941:                if (*rp == '\0')
                    942:                        break;
                    943:                argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
                    944:                argv[argc++] = wp = rp;
                    945:                for (;;) {
                    946:                        if (isspace((unsigned char)*rp)) {
                    947:                                *wp = '\0';
                    948:                                rp++;
                    949:                                break;
                    950:                        }
                    951:                        if (rp[0] == '\\' && rp[1] != '\0')
                    952:                                rp++;
                    953:                        if (wp != rp)
                    954:                                *wp = *rp;
                    955:                        if (*rp == '\0')
                    956:                                break;
                    957:                        wp++;
                    958:                        rp++;
                    959:                }
1.1       schwarze  960:        }
                    961:
1.36      schwarze  962:        if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz))
1.12      schwarze  963:                pg_noresult(req, "You entered an invalid query.");
1.1       schwarze  964:        else if (0 == ressz)
1.12      schwarze  965:                pg_noresult(req, "No results found.");
1.1       schwarze  966:        else
1.12      schwarze  967:                pg_searchres(req, res, ressz);
1.1       schwarze  968:
1.36      schwarze  969:        free(query);
                    970:        mansearch_free(res, ressz);
1.1       schwarze  971:        free(paths.paths[0]);
                    972:        free(paths.paths);
                    973: }
                    974:
                    975: int
                    976: main(void)
                    977: {
1.6       schwarze  978:        struct req       req;
1.33      schwarze  979:        struct itimerval itimer;
1.6       schwarze  980:        const char      *path;
1.23      schwarze  981:        const char      *querystring;
1.1       schwarze  982:        int              i;
1.33      schwarze  983:
                    984:        /* Poor man's ReDoS mitigation. */
                    985:
1.38      schwarze  986:        itimer.it_value.tv_sec = 2;
1.33      schwarze  987:        itimer.it_value.tv_usec = 0;
1.38      schwarze  988:        itimer.it_interval.tv_sec = 2;
1.33      schwarze  989:        itimer.it_interval.tv_usec = 0;
                    990:        if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1.67      schwarze  991:                warn("setitimer");
1.20      schwarze  992:                pg_error_internal();
1.48      schwarze  993:                return EXIT_FAILURE;
1.20      schwarze  994:        }
                    995:
1.1       schwarze  996:        /*
1.7       schwarze  997:         * First we change directory into the MAN_DIR so that
1.1       schwarze  998:         * subsequent scanning for manpath directories is rooted
                    999:         * relative to the same position.
                   1000:         */
                   1001:
1.67      schwarze 1002:        if (chdir(MAN_DIR) == -1) {
                   1003:                warn("MAN_DIR: %s", MAN_DIR);
1.12      schwarze 1004:                pg_error_internal();
1.48      schwarze 1005:                return EXIT_FAILURE;
1.43      schwarze 1006:        }
1.1       schwarze 1007:
                   1008:        memset(&req, 0, sizeof(struct req));
1.57      schwarze 1009:        req.q.equal = 1;
1.68      schwarze 1010:        parse_manpath_conf(&req);
1.1       schwarze 1011:
1.56      schwarze 1012:        /* Parse the path info and the query string. */
1.1       schwarze 1013:
1.56      schwarze 1014:        if ((path = getenv("PATH_INFO")) == NULL)
                   1015:                path = "";
                   1016:        else if (*path == '/')
                   1017:                path++;
                   1018:
1.63      schwarze 1019:        if (*path != '\0') {
1.68      schwarze 1020:                parse_path_info(&req, path);
1.72      schwarze 1021:                if (req.q.manpath == NULL || access(path, F_OK) == -1)
1.63      schwarze 1022:                        path = "";
1.56      schwarze 1023:        } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.68      schwarze 1024:                parse_query_string(&req, querystring);
1.17      schwarze 1025:
1.56      schwarze 1026:        /* Validate parsed data and add defaults. */
                   1027:
1.41      schwarze 1028:        if (req.q.manpath == NULL)
                   1029:                req.q.manpath = mandoc_strdup(req.p[0]);
                   1030:        else if ( ! validate_manpath(&req, req.q.manpath)) {
1.17      schwarze 1031:                pg_error_badrequest(
                   1032:                    "You specified an invalid manpath.");
1.48      schwarze 1033:                return EXIT_FAILURE;
1.17      schwarze 1034:        }
1.1       schwarze 1035:
1.20      schwarze 1036:        if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
                   1037:                pg_error_badrequest(
                   1038:                    "You specified an invalid architecture.");
1.48      schwarze 1039:                return EXIT_FAILURE;
1.20      schwarze 1040:        }
                   1041:
1.6       schwarze 1042:        /* Dispatch to the three different pages. */
1.1       schwarze 1043:
1.6       schwarze 1044:        if ('\0' != *path)
                   1045:                pg_show(&req, path);
1.25      schwarze 1046:        else if (NULL != req.q.query)
1.6       schwarze 1047:                pg_search(&req);
                   1048:        else
1.12      schwarze 1049:                pg_index(&req);
1.1       schwarze 1050:
1.23      schwarze 1051:        free(req.q.manpath);
                   1052:        free(req.q.arch);
                   1053:        free(req.q.sec);
1.25      schwarze 1054:        free(req.q.query);
1.1       schwarze 1055:        for (i = 0; i < (int)req.psz; i++)
                   1056:                free(req.p[i]);
                   1057:        free(req.p);
1.48      schwarze 1058:        return EXIT_SUCCESS;
1.56      schwarze 1059: }
                   1060:
                   1061: /*
                   1062:  * If PATH_INFO is not a file name, translate it to a query.
                   1063:  */
                   1064: static void
1.68      schwarze 1065: parse_path_info(struct req *req, const char *path)
1.56      schwarze 1066: {
1.74      schwarze 1067:        char    *dir[4];
                   1068:        int      i;
1.56      schwarze 1069:
1.60      schwarze 1070:        req->isquery = 0;
1.56      schwarze 1071:        req->q.equal = 1;
                   1072:        req->q.manpath = mandoc_strdup(path);
1.74      schwarze 1073:        req->q.arch = NULL;
1.56      schwarze 1074:
                   1075:        /* Mandatory manual page name. */
                   1076:        if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) {
                   1077:                req->q.query = req->q.manpath;
                   1078:                req->q.manpath = NULL;
                   1079:        } else
                   1080:                *req->q.query++ = '\0';
                   1081:
                   1082:        /* Optional trailing section. */
                   1083:        if ((req->q.sec = strrchr(req->q.query, '.')) != NULL) {
                   1084:                if(isdigit((unsigned char)req->q.sec[1])) {
                   1085:                        *req->q.sec++ = '\0';
                   1086:                        req->q.sec = mandoc_strdup(req->q.sec);
                   1087:                } else
                   1088:                        req->q.sec = NULL;
                   1089:        }
                   1090:
                   1091:        /* Handle the case of name[.section] only. */
1.74      schwarze 1092:        if (req->q.manpath == NULL)
1.56      schwarze 1093:                return;
1.74      schwarze 1094:        req->q.query = mandoc_strdup(req->q.query);
                   1095:
                   1096:        /* Split directory components. */
                   1097:        dir[i = 0] = req->q.manpath;
                   1098:        while ((dir[i + 1] = strchr(dir[i], '/')) != NULL) {
                   1099:                if (++i == 3) {
                   1100:                        pg_error_badrequest(
                   1101:                            "You specified too many directory components.");
                   1102:                        exit(EXIT_FAILURE);
                   1103:                }
                   1104:                *dir[i]++ = '\0';
1.56      schwarze 1105:        }
                   1106:
1.74      schwarze 1107:        /* Optional manpath. */
                   1108:        if ((i = validate_manpath(req, req->q.manpath)) == 0)
                   1109:                req->q.manpath = NULL;
                   1110:        else if (dir[1] == NULL)
                   1111:                return;
1.56      schwarze 1112:
1.74      schwarze 1113:        /* Optional section. */
                   1114:        if (strncmp(dir[i], "man", 3) == 0) {
1.66      schwarze 1115:                free(req->q.sec);
1.74      schwarze 1116:                req->q.sec = mandoc_strdup(dir[i++] + 3);
1.56      schwarze 1117:        }
1.74      schwarze 1118:        if (dir[i] == NULL) {
                   1119:                if (req->q.manpath == NULL)
                   1120:                        free(dir[0]);
                   1121:                return;
                   1122:        }
                   1123:        if (dir[i + 1] != NULL) {
                   1124:                pg_error_badrequest(
                   1125:                    "You specified an invalid directory component.");
                   1126:                exit(EXIT_FAILURE);
                   1127:        }
                   1128:
                   1129:        /* Optional architecture. */
                   1130:        if (i) {
                   1131:                req->q.arch = mandoc_strdup(dir[i]);
                   1132:                if (req->q.manpath == NULL)
                   1133:                        free(dir[0]);
                   1134:        } else
                   1135:                req->q.arch = dir[0];
1.1       schwarze 1136: }
                   1137:
                   1138: /*
                   1139:  * Scan for indexable paths.
                   1140:  */
                   1141: static void
1.68      schwarze 1142: parse_manpath_conf(struct req *req)
1.1       schwarze 1143: {
                   1144:        FILE    *fp;
                   1145:        char    *dp;
                   1146:        size_t   dpsz;
1.54      schwarze 1147:        ssize_t  len;
1.1       schwarze 1148:
1.67      schwarze 1149:        if ((fp = fopen("manpath.conf", "r")) == NULL) {
                   1150:                warn("%s/manpath.conf", MAN_DIR);
1.14      schwarze 1151:                pg_error_internal();
                   1152:                exit(EXIT_FAILURE);
                   1153:        }
1.1       schwarze 1154:
1.54      schwarze 1155:        dp = NULL;
                   1156:        dpsz = 0;
                   1157:
                   1158:        while ((len = getline(&dp, &dpsz, fp)) != -1) {
                   1159:                if (dp[len - 1] == '\n')
                   1160:                        dp[--len] = '\0';
1.1       schwarze 1161:                req->p = mandoc_realloc(req->p,
                   1162:                    (req->psz + 1) * sizeof(char *));
1.20      schwarze 1163:                if ( ! validate_urifrag(dp)) {
1.67      schwarze 1164:                        warnx("%s/manpath.conf contains "
                   1165:                            "unsafe path \"%s\"", MAN_DIR, dp);
1.20      schwarze 1166:                        pg_error_internal();
                   1167:                        exit(EXIT_FAILURE);
                   1168:                }
1.67      schwarze 1169:                if (strchr(dp, '/') != NULL) {
                   1170:                        warnx("%s/manpath.conf contains "
                   1171:                            "path with slash \"%s\"", MAN_DIR, dp);
1.20      schwarze 1172:                        pg_error_internal();
                   1173:                        exit(EXIT_FAILURE);
                   1174:                }
                   1175:                req->p[req->psz++] = dp;
1.54      schwarze 1176:                dp = NULL;
                   1177:                dpsz = 0;
1.14      schwarze 1178:        }
1.54      schwarze 1179:        free(dp);
1.14      schwarze 1180:
1.67      schwarze 1181:        if (req->p == NULL) {
                   1182:                warnx("%s/manpath.conf is empty", MAN_DIR);
1.14      schwarze 1183:                pg_error_internal();
                   1184:                exit(EXIT_FAILURE);
1.1       schwarze 1185:        }
                   1186: }