[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/cgi.c, Revision 1.144

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

CVSweb