[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/cgi.c, Revision 1.110

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