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

Annotation of mandoc/cgi.c, Revision 1.172

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

CVSweb