[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.95

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