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

Annotation of mandoc/cgi.c, Revision 1.150

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

CVSweb