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

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