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

Annotation of mandoc/cgi.c, Revision 1.168

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

CVSweb