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

Annotation of mandoc/cgi.c, Revision 1.167

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

CVSweb