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

1.90    ! schwarze    1: /*     $OpenBSD: cgi.c,v 1.89 2017/03/15 13:49:26 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) {
                    139:        case ('"'):
1.80      bentley   140:                printf("&quot;");
1.1       schwarze  141:                break;
                    142:        case ('&'):
                    143:                printf("&amp;");
                    144:                break;
                    145:        case ('>'):
                    146:                printf("&gt;");
                    147:                break;
                    148:        case ('<'):
                    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"
                    355:               "  <link rel=\"stylesheet\" href=\"%s/mandoc.css\""
1.65      schwarze  356:               " type=\"text/css\" media=\"all\">\n"
1.88      schwarze  357:               "  <title>",
                    358:               CSS_DIR);
                    359:        if (file != NULL) {
                    360:                if ((cp = strrchr(file, '/')) != NULL)
                    361:                        file = cp + 1;
                    362:                if ((cp = strrchr(file, '.')) != NULL) {
                    363:                        printf("%.*s(%s) - ", (int)(cp - file), file, cp + 1);
                    364:                } else
                    365:                        printf("%s - ", file);
                    366:        }
                    367:        printf("%s</title>\n"
1.65      schwarze  368:               "</head>\n"
1.81      schwarze  369:               "<body>\n",
1.88      schwarze  370:               CUSTOMIZE_TITLE);
1.53      schwarze  371:
                    372:        resp_copy(MAN_DIR "/header.html");
1.1       schwarze  373: }
                    374:
                    375: static void
                    376: resp_end_html(void)
                    377: {
                    378:
1.53      schwarze  379:        resp_copy(MAN_DIR "/footer.html");
                    380:
1.65      schwarze  381:        puts("</body>\n"
                    382:             "</html>");
1.1       schwarze  383: }
                    384:
                    385: static void
1.70      schwarze  386: resp_searchform(const struct req *req, enum focus focus)
1.1       schwarze  387: {
                    388:        int              i;
                    389:
1.81      schwarze  390:        printf("<form action=\"/%s\" method=\"get\">\n"
1.82      schwarze  391:               "  <fieldset>\n"
                    392:               "    <legend>Manual Page Search Parameters</legend>\n",
1.1       schwarze  393:               scriptname);
1.8       schwarze  394:
                    395:        /* Write query input box. */
                    396:
1.82      schwarze  397:        printf("    <input type=\"text\" name=\"query\" value=\"");
1.70      schwarze  398:        if (req->q.query != NULL)
1.25      schwarze  399:                html_print(req->q.query);
1.70      schwarze  400:        printf( "\" size=\"40\"");
                    401:        if (focus == FOCUS_QUERY)
                    402:                printf(" autofocus");
                    403:        puts(">");
1.8       schwarze  404:
1.71      schwarze  405:        /* Write submission buttons. */
1.8       schwarze  406:
1.82      schwarze  407:        printf( "    <button type=\"submit\" name=\"apropos\" value=\"0\">"
1.71      schwarze  408:                "man</button>\n"
1.82      schwarze  409:                "    <button type=\"submit\" name=\"apropos\" value=\"1\">"
                    410:                "apropos</button>\n"
                    411:                "    <br/>\n");
1.8       schwarze  412:
                    413:        /* Write section selector. */
                    414:
1.82      schwarze  415:        puts("    <select name=\"sec\">");
1.8       schwarze  416:        for (i = 0; i < sec_MAX; i++) {
1.82      schwarze  417:                printf("      <option value=\"%s\"", sec_numbers[i]);
1.8       schwarze  418:                if (NULL != req->q.sec &&
                    419:                    0 == strcmp(sec_numbers[i], req->q.sec))
1.65      schwarze  420:                        printf(" selected=\"selected\"");
                    421:                printf(">%s</option>\n", sec_names[i]);
1.8       schwarze  422:        }
1.82      schwarze  423:        puts("    </select>");
1.8       schwarze  424:
                    425:        /* Write architecture selector. */
                    426:
1.82      schwarze  427:        printf( "    <select name=\"arch\">\n"
                    428:                "      <option value=\"default\"");
1.21      schwarze  429:        if (NULL == req->q.arch)
1.65      schwarze  430:                printf(" selected=\"selected\"");
                    431:        puts(">All Architectures</option>");
1.8       schwarze  432:        for (i = 0; i < arch_MAX; i++) {
1.82      schwarze  433:                printf("      <option value=\"%s\"", arch_names[i]);
1.8       schwarze  434:                if (NULL != req->q.arch &&
                    435:                    0 == strcmp(arch_names[i], req->q.arch))
1.65      schwarze  436:                        printf(" selected=\"selected\"");
                    437:                printf(">%s</option>\n", arch_names[i]);
1.8       schwarze  438:        }
1.82      schwarze  439:        puts("    </select>");
1.8       schwarze  440:
                    441:        /* Write manpath selector. */
                    442:
1.1       schwarze  443:        if (req->psz > 1) {
1.82      schwarze  444:                puts("    <select name=\"manpath\">");
1.1       schwarze  445:                for (i = 0; i < (int)req->psz; i++) {
1.82      schwarze  446:                        printf("      <option ");
1.41      schwarze  447:                        if (strcmp(req->q.manpath, req->p[i]) == 0)
1.65      schwarze  448:                                printf("selected=\"selected\" ");
                    449:                        printf("value=\"");
1.1       schwarze  450:                        html_print(req->p[i]);
                    451:                        printf("\">");
                    452:                        html_print(req->p[i]);
1.65      schwarze  453:                        puts("</option>");
1.1       schwarze  454:                }
1.82      schwarze  455:                puts("    </select>");
1.1       schwarze  456:        }
1.8       schwarze  457:
1.82      schwarze  458:        puts("  </fieldset>\n"
1.81      schwarze  459:             "</form>");
1.1       schwarze  460: }
                    461:
1.16      schwarze  462: static int
1.20      schwarze  463: validate_urifrag(const char *frag)
                    464: {
                    465:
                    466:        while ('\0' != *frag) {
                    467:                if ( ! (isalnum((unsigned char)*frag) ||
                    468:                    '-' == *frag || '.' == *frag ||
                    469:                    '/' == *frag || '_' == *frag))
1.48      schwarze  470:                        return 0;
1.20      schwarze  471:                frag++;
                    472:        }
1.48      schwarze  473:        return 1;
1.20      schwarze  474: }
                    475:
                    476: static int
1.17      schwarze  477: validate_manpath(const struct req *req, const char* manpath)
                    478: {
                    479:        size_t   i;
                    480:
                    481:        for (i = 0; i < req->psz; i++)
                    482:                if ( ! strcmp(manpath, req->p[i]))
1.48      schwarze  483:                        return 1;
1.17      schwarze  484:
1.48      schwarze  485:        return 0;
1.17      schwarze  486: }
                    487:
                    488: static int
1.16      schwarze  489: validate_filename(const char *file)
                    490: {
                    491:
                    492:        if ('.' == file[0] && '/' == file[1])
                    493:                file += 2;
                    494:
1.48      schwarze  495:        return ! (strstr(file, "../") || strstr(file, "/..") ||
                    496:            (strncmp(file, "man", 3) && strncmp(file, "cat", 3)));
1.16      schwarze  497: }
                    498:
1.1       schwarze  499: static void
1.12      schwarze  500: pg_index(const struct req *req)
1.1       schwarze  501: {
                    502:
1.88      schwarze  503:        resp_begin_html(200, NULL, NULL);
1.70      schwarze  504:        resp_searchform(req, FOCUS_QUERY);
1.65      schwarze  505:        printf("<p>\n"
1.26      schwarze  506:               "This web interface is documented in the\n"
1.83      schwarze  507:               "<a class=\"Xr\" href=\"/%s%sman.cgi.8\">man.cgi(8)</a>\n"
1.26      schwarze  508:               "manual, and the\n"
1.83      schwarze  509:               "<a class=\"Xr\" href=\"/%s%sapropos.1\">apropos(1)</a>\n"
1.9       schwarze  510:               "manual explains the query syntax.\n"
1.65      schwarze  511:               "</p>\n",
1.58      schwarze  512:               scriptname, *scriptname == '\0' ? "" : "/",
                    513:               scriptname, *scriptname == '\0' ? "" : "/");
1.1       schwarze  514:        resp_end_html();
                    515: }
                    516:
                    517: static void
1.12      schwarze  518: pg_noresult(const struct req *req, const char *msg)
1.1       schwarze  519: {
1.88      schwarze  520:        resp_begin_html(200, NULL, NULL);
1.70      schwarze  521:        resp_searchform(req, FOCUS_QUERY);
1.65      schwarze  522:        puts("<p>");
1.1       schwarze  523:        puts(msg);
1.65      schwarze  524:        puts("</p>");
1.1       schwarze  525:        resp_end_html();
                    526: }
                    527:
                    528: static void
1.12      schwarze  529: pg_error_badrequest(const char *msg)
1.1       schwarze  530: {
                    531:
1.88      schwarze  532:        resp_begin_html(400, "Bad Request", NULL);
1.65      schwarze  533:        puts("<h1>Bad Request</h1>\n"
                    534:             "<p>\n");
1.1       schwarze  535:        puts(msg);
                    536:        printf("Try again from the\n"
1.65      schwarze  537:               "<a href=\"/%s\">main page</a>.\n"
                    538:               "</p>", scriptname);
1.1       schwarze  539:        resp_end_html();
                    540: }
                    541:
                    542: static void
1.12      schwarze  543: pg_error_internal(void)
1.1       schwarze  544: {
1.88      schwarze  545:        resp_begin_html(500, "Internal Server Error", NULL);
1.65      schwarze  546:        puts("<p>Internal Server Error</p>");
1.1       schwarze  547:        resp_end_html();
                    548: }
                    549:
                    550: static void
1.87      schwarze  551: pg_redirect(const struct req *req, const char *name)
                    552: {
                    553:        printf("Status: 303 See Other\r\n");
                    554:        printf("Location: http://%s/", HTTP_HOST);
                    555:        if (*scriptname != '\0')
                    556:                printf("%s/", scriptname);
                    557:        if (strcmp(req->q.manpath, req->p[0]))
                    558:                printf("%s/", req->q.manpath);
                    559:        if (req->q.arch != NULL)
                    560:                printf("%s/", req->q.arch);
                    561:        printf("%s", name);
                    562:        if (req->q.sec != NULL)
                    563:                printf(".%s", req->q.sec);
                    564:        printf("\r\nContent-Type: text/html; charset=utf-8\r\n\r\n");
                    565: }
                    566:
                    567: static void
1.12      schwarze  568: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1       schwarze  569: {
1.21      schwarze  570:        char            *arch, *archend;
1.59      schwarze  571:        const char      *sec;
                    572:        size_t           i, iuse;
1.21      schwarze  573:        int              archprio, archpriouse;
1.10      schwarze  574:        int              prio, priouse;
1.1       schwarze  575:
1.16      schwarze  576:        for (i = 0; i < sz; i++) {
                    577:                if (validate_filename(r[i].file))
                    578:                        continue;
1.67      schwarze  579:                warnx("invalid filename %s in %s database",
1.16      schwarze  580:                    r[i].file, req->q.manpath);
                    581:                pg_error_internal();
                    582:                return;
                    583:        }
                    584:
1.60      schwarze  585:        if (req->isquery && sz == 1) {
1.1       schwarze  586:                /*
                    587:                 * If we have just one result, then jump there now
                    588:                 * without any delay.
                    589:                 */
1.2       tedu      590:                printf("Status: 303 See Other\r\n");
1.58      schwarze  591:                printf("Location: http://%s/%s%s%s/%s",
                    592:                    HTTP_HOST, scriptname,
                    593:                    *scriptname == '\0' ? "" : "/",
                    594:                    req->q.manpath, r[0].file);
1.2       tedu      595:                printf("\r\n"
                    596:                     "Content-Type: text/html; charset=utf-8\r\n"
                    597:                     "\r\n");
1.1       schwarze  598:                return;
                    599:        }
                    600:
1.10      schwarze  601:        /*
                    602:         * In man(1) mode, show one of the pages
                    603:         * even if more than one is found.
                    604:         */
                    605:
1.88      schwarze  606:        iuse = 0;
1.62      schwarze  607:        if (req->q.equal || sz == 1) {
1.59      schwarze  608:                priouse = 20;
1.21      schwarze  609:                archpriouse = 3;
1.10      schwarze  610:                for (i = 0; i < sz; i++) {
1.59      schwarze  611:                        sec = r[i].file;
                    612:                        sec += strcspn(sec, "123456789");
                    613:                        if (sec[0] == '\0')
1.10      schwarze  614:                                continue;
1.59      schwarze  615:                        prio = sec_prios[sec[0] - '1'];
                    616:                        if (sec[1] != '/')
                    617:                                prio += 10;
                    618:                        if (req->q.arch == NULL) {
1.21      schwarze  619:                                archprio =
1.59      schwarze  620:                                    ((arch = strchr(sec + 1, '/'))
                    621:                                        == NULL) ? 3 :
                    622:                                    ((archend = strchr(arch + 1, '/'))
                    623:                                        == NULL) ? 0 :
1.21      schwarze  624:                                    strncmp(arch, "amd64/",
                    625:                                        archend - arch) ? 2 : 1;
                    626:                                if (archprio < archpriouse) {
                    627:                                        archpriouse = archprio;
                    628:                                        priouse = prio;
                    629:                                        iuse = i;
                    630:                                        continue;
                    631:                                }
                    632:                                if (archprio > archpriouse)
                    633:                                        continue;
                    634:                        }
1.10      schwarze  635:                        if (prio >= priouse)
                    636:                                continue;
                    637:                        priouse = prio;
                    638:                        iuse = i;
                    639:                }
1.88      schwarze  640:                resp_begin_html(200, NULL, r[iuse].file);
                    641:        } else
                    642:                resp_begin_html(200, NULL, NULL);
                    643:
                    644:        resp_searchform(req,
                    645:            req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
                    646:
                    647:        if (sz > 1) {
                    648:                puts("<table class=\"results\">");
                    649:                for (i = 0; i < sz; i++) {
                    650:                        printf("  <tr>\n"
                    651:                               "    <td>"
1.89      schwarze  652:                               "<a class=\"Xr\" href=\"/");
                    653:                        if (*scriptname != '\0')
                    654:                                printf("%s/", scriptname);
                    655:                        if (strcmp(req->q.manpath, req->p[0]))
                    656:                                printf("%s/", req->q.manpath);
                    657:                        printf("%s\">", r[i].file);
1.88      schwarze  658:                        html_print(r[i].names);
                    659:                        printf("</a></td>\n"
                    660:                               "    <td><span class=\"Nd\">");
                    661:                        html_print(r[i].output);
                    662:                        puts("</span></td>\n"
                    663:                             "  </tr>");
                    664:                }
                    665:                puts("</table>");
                    666:        }
                    667:
                    668:        if (req->q.equal || sz == 1) {
                    669:                puts("<hr>");
1.10      schwarze  670:                resp_show(req, r[iuse].file);
                    671:        }
                    672:
1.1       schwarze  673:        resp_end_html();
                    674: }
                    675:
                    676: static void
1.68      schwarze  677: resp_catman(const struct req *req, const char *file)
1.1       schwarze  678: {
                    679:        FILE            *f;
1.54      schwarze  680:        char            *p;
                    681:        size_t           sz;
                    682:        ssize_t          len;
1.1       schwarze  683:        int              i;
                    684:        int              italic, bold;
                    685:
1.54      schwarze  686:        if ((f = fopen(file, "r")) == NULL) {
1.65      schwarze  687:                puts("<p>You specified an invalid manual file.</p>");
1.1       schwarze  688:                return;
                    689:        }
                    690:
1.65      schwarze  691:        puts("<div class=\"catman\">\n"
                    692:             "<pre>");
1.1       schwarze  693:
1.54      schwarze  694:        p = NULL;
                    695:        sz = 0;
                    696:
                    697:        while ((len = getline(&p, &sz, f)) != -1) {
1.1       schwarze  698:                bold = italic = 0;
1.54      schwarze  699:                for (i = 0; i < len - 1; i++) {
1.43      schwarze  700:                        /*
1.1       schwarze  701:                         * This means that the catpage is out of state.
                    702:                         * Ignore it and keep going (although the
                    703:                         * catpage is bogus).
                    704:                         */
                    705:
                    706:                        if ('\b' == p[i] || '\n' == p[i])
                    707:                                continue;
                    708:
                    709:                        /*
                    710:                         * Print a regular character.
                    711:                         * Close out any bold/italic scopes.
                    712:                         * If we're in back-space mode, make sure we'll
                    713:                         * have something to enter when we backspace.
                    714:                         */
                    715:
                    716:                        if ('\b' != p[i + 1]) {
                    717:                                if (italic)
1.65      schwarze  718:                                        printf("</i>");
1.1       schwarze  719:                                if (bold)
1.65      schwarze  720:                                        printf("</b>");
1.1       schwarze  721:                                italic = bold = 0;
                    722:                                html_putchar(p[i]);
                    723:                                continue;
1.54      schwarze  724:                        } else if (i + 2 >= len)
1.1       schwarze  725:                                continue;
                    726:
                    727:                        /* Italic mode. */
                    728:
                    729:                        if ('_' == p[i]) {
                    730:                                if (bold)
1.65      schwarze  731:                                        printf("</b>");
1.1       schwarze  732:                                if ( ! italic)
1.65      schwarze  733:                                        printf("<i>");
1.1       schwarze  734:                                bold = 0;
                    735:                                italic = 1;
                    736:                                i += 2;
                    737:                                html_putchar(p[i]);
                    738:                                continue;
                    739:                        }
                    740:
1.43      schwarze  741:                        /*
1.1       schwarze  742:                         * Handle funny behaviour troff-isms.
                    743:                         * These grok'd from the original man2html.c.
                    744:                         */
                    745:
                    746:                        if (('+' == p[i] && 'o' == p[i + 2]) ||
                    747:                                        ('o' == p[i] && '+' == p[i + 2]) ||
                    748:                                        ('|' == p[i] && '=' == p[i + 2]) ||
                    749:                                        ('=' == p[i] && '|' == p[i + 2]) ||
                    750:                                        ('*' == p[i] && '=' == p[i + 2]) ||
                    751:                                        ('=' == p[i] && '*' == p[i + 2]) ||
                    752:                                        ('*' == p[i] && '|' == p[i + 2]) ||
                    753:                                        ('|' == p[i] && '*' == p[i + 2]))  {
                    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:                                putchar('*');
                    760:                                i += 2;
                    761:                                continue;
                    762:                        } else if (('|' == p[i] && '-' == p[i + 2]) ||
                    763:                                        ('-' == p[i] && '|' == p[i + 1]) ||
                    764:                                        ('+' == p[i] && '-' == p[i + 1]) ||
                    765:                                        ('-' == p[i] && '+' == p[i + 1]) ||
                    766:                                        ('+' == p[i] && '|' == p[i + 1]) ||
                    767:                                        ('|' == p[i] && '+' == p[i + 1]))  {
                    768:                                if (italic)
1.65      schwarze  769:                                        printf("</i>");
1.1       schwarze  770:                                if (bold)
1.65      schwarze  771:                                        printf("</b>");
1.1       schwarze  772:                                italic = bold = 0;
                    773:                                putchar('+');
                    774:                                i += 2;
                    775:                                continue;
                    776:                        }
                    777:
                    778:                        /* Bold mode. */
1.43      schwarze  779:
1.1       schwarze  780:                        if (italic)
1.65      schwarze  781:                                printf("</i>");
1.1       schwarze  782:                        if ( ! bold)
1.65      schwarze  783:                                printf("<b>");
1.1       schwarze  784:                        bold = 1;
                    785:                        italic = 0;
                    786:                        i += 2;
                    787:                        html_putchar(p[i]);
                    788:                }
                    789:
1.43      schwarze  790:                /*
1.1       schwarze  791:                 * Clean up the last character.
1.43      schwarze  792:                 * We can get to a newline; don't print that.
1.1       schwarze  793:                 */
                    794:
                    795:                if (italic)
1.65      schwarze  796:                        printf("</i>");
1.1       schwarze  797:                if (bold)
1.65      schwarze  798:                        printf("</b>");
1.1       schwarze  799:
1.54      schwarze  800:                if (i == len - 1 && p[i] != '\n')
1.1       schwarze  801:                        html_putchar(p[i]);
                    802:
                    803:                putchar('\n');
                    804:        }
1.54      schwarze  805:        free(p);
1.1       schwarze  806:
1.65      schwarze  807:        puts("</pre>\n"
                    808:             "</div>");
1.1       schwarze  809:
                    810:        fclose(f);
                    811: }
                    812:
                    813: static void
1.68      schwarze  814: resp_format(const struct req *req, const char *file)
1.1       schwarze  815: {
1.45      schwarze  816:        struct manoutput conf;
1.1       schwarze  817:        struct mparse   *mp;
1.46      schwarze  818:        struct roff_man *man;
1.1       schwarze  819:        void            *vp;
1.30      schwarze  820:        int              fd;
                    821:        int              usepath;
1.1       schwarze  822:
                    823:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.65      schwarze  824:                puts("<p>You specified an invalid manual file.</p>");
1.1       schwarze  825:                return;
                    826:        }
                    827:
1.49      schwarze  828:        mchars_alloc();
1.75      schwarze  829:        mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1,
                    830:            MANDOCLEVEL_BADARG, NULL, req->q.manpath);
1.42      schwarze  831:        mparse_readfd(mp, fd, file);
1.1       schwarze  832:        close(fd);
                    833:
1.45      schwarze  834:        memset(&conf, 0, sizeof(conf));
                    835:        conf.fragment = 1;
1.84      schwarze  836:        conf.style = mandoc_strdup(CSS_DIR "/mandoc.css");
1.30      schwarze  837:        usepath = strcmp(req->q.manpath, req->p[0]);
1.90    ! schwarze  838:        mandoc_asprintf(&conf.man, "/%s%s%s%s%%N.%%S",
        !           839:            scriptname, *scriptname == '\0' ? "" : "/",
1.61      schwarze  840:            usepath ? req->q.manpath : "", usepath ? "/" : "");
1.1       schwarze  841:
1.47      schwarze  842:        mparse_result(mp, &man, NULL);
                    843:        if (man == NULL) {
1.67      schwarze  844:                warnx("fatal mandoc error: %s/%s", req->q.manpath, file);
1.12      schwarze  845:                pg_error_internal();
1.1       schwarze  846:                mparse_free(mp);
1.49      schwarze  847:                mchars_free();
1.1       schwarze  848:                return;
                    849:        }
                    850:
1.49      schwarze  851:        vp = html_alloc(&conf);
1.1       schwarze  852:
1.50      schwarze  853:        if (man->macroset == MACROSET_MDOC) {
                    854:                mdoc_validate(man);
1.47      schwarze  855:                html_mdoc(vp, man);
1.51      schwarze  856:        } else {
                    857:                man_validate(man);
1.1       schwarze  858:                html_man(vp, man);
1.51      schwarze  859:        }
1.1       schwarze  860:
                    861:        html_free(vp);
                    862:        mparse_free(mp);
1.49      schwarze  863:        mchars_free();
1.45      schwarze  864:        free(conf.man);
1.84      schwarze  865:        free(conf.style);
1.1       schwarze  866: }
                    867:
                    868: static void
1.10      schwarze  869: resp_show(const struct req *req, const char *file)
                    870: {
1.16      schwarze  871:
                    872:        if ('.' == file[0] && '/' == file[1])
1.11      schwarze  873:                file += 2;
1.10      schwarze  874:
                    875:        if ('c' == *file)
1.68      schwarze  876:                resp_catman(req, file);
1.10      schwarze  877:        else
1.68      schwarze  878:                resp_format(req, file);
1.10      schwarze  879: }
                    880:
                    881: static void
1.24      schwarze  882: pg_show(struct req *req, const char *fullpath)
1.1       schwarze  883: {
1.24      schwarze  884:        char            *manpath;
                    885:        const char      *file;
1.1       schwarze  886:
1.24      schwarze  887:        if ((file = strchr(fullpath, '/')) == NULL) {
1.12      schwarze  888:                pg_error_badrequest(
1.1       schwarze  889:                    "You did not specify a page to show.");
                    890:                return;
1.43      schwarze  891:        }
1.24      schwarze  892:        manpath = mandoc_strndup(fullpath, file - fullpath);
                    893:        file++;
1.1       schwarze  894:
1.24      schwarze  895:        if ( ! validate_manpath(req, manpath)) {
1.17      schwarze  896:                pg_error_badrequest(
                    897:                    "You specified an invalid manpath.");
1.24      schwarze  898:                free(manpath);
1.17      schwarze  899:                return;
                    900:        }
                    901:
1.1       schwarze  902:        /*
                    903:         * Begin by chdir()ing into the manpath.
                    904:         * This way we can pick up the database files, which are
                    905:         * relative to the manpath root.
                    906:         */
                    907:
1.24      schwarze  908:        if (chdir(manpath) == -1) {
1.67      schwarze  909:                warn("chdir %s", manpath);
1.17      schwarze  910:                pg_error_internal();
1.24      schwarze  911:                free(manpath);
1.16      schwarze  912:                return;
                    913:        }
1.73      schwarze  914:        free(manpath);
1.24      schwarze  915:
                    916:        if ( ! validate_filename(file)) {
1.16      schwarze  917:                pg_error_badrequest(
                    918:                    "You specified an invalid manual file.");
1.1       schwarze  919:                return;
                    920:        }
1.19      schwarze  921:
1.88      schwarze  922:        resp_begin_html(200, NULL, file);
1.70      schwarze  923:        resp_searchform(req, FOCUS_NONE);
1.24      schwarze  924:        resp_show(req, file);
1.10      schwarze  925:        resp_end_html();
1.1       schwarze  926: }
                    927:
                    928: static void
1.6       schwarze  929: pg_search(const struct req *req)
1.1       schwarze  930: {
                    931:        struct mansearch          search;
                    932:        struct manpaths           paths;
                    933:        struct manpage           *res;
1.36      schwarze  934:        char                    **argv;
                    935:        char                     *query, *rp, *wp;
1.1       schwarze  936:        size_t                    ressz;
1.36      schwarze  937:        int                       argc;
1.1       schwarze  938:
                    939:        /*
                    940:         * Begin by chdir()ing into the root of the manpath.
                    941:         * This way we can pick up the database files, which are
                    942:         * relative to the manpath root.
                    943:         */
                    944:
1.67      schwarze  945:        if (chdir(req->q.manpath) == -1) {
                    946:                warn("chdir %s", req->q.manpath);
1.17      schwarze  947:                pg_error_internal();
1.1       schwarze  948:                return;
                    949:        }
                    950:
                    951:        search.arch = req->q.arch;
                    952:        search.sec = req->q.sec;
1.35      schwarze  953:        search.outkey = "Nd";
                    954:        search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.40      schwarze  955:        search.firstmatch = 1;
1.1       schwarze  956:
                    957:        paths.sz = 1;
                    958:        paths.paths = mandoc_malloc(sizeof(char *));
                    959:        paths.paths[0] = mandoc_strdup(".");
                    960:
                    961:        /*
1.36      schwarze  962:         * Break apart at spaces with backslash-escaping.
1.1       schwarze  963:         */
                    964:
1.36      schwarze  965:        argc = 0;
                    966:        argv = NULL;
                    967:        rp = query = mandoc_strdup(req->q.query);
                    968:        for (;;) {
                    969:                while (isspace((unsigned char)*rp))
                    970:                        rp++;
                    971:                if (*rp == '\0')
                    972:                        break;
                    973:                argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
                    974:                argv[argc++] = wp = rp;
                    975:                for (;;) {
                    976:                        if (isspace((unsigned char)*rp)) {
                    977:                                *wp = '\0';
                    978:                                rp++;
                    979:                                break;
                    980:                        }
                    981:                        if (rp[0] == '\\' && rp[1] != '\0')
                    982:                                rp++;
                    983:                        if (wp != rp)
                    984:                                *wp = *rp;
                    985:                        if (*rp == '\0')
                    986:                                break;
                    987:                        wp++;
                    988:                        rp++;
                    989:                }
1.1       schwarze  990:        }
                    991:
1.87      schwarze  992:        res = NULL;
                    993:        ressz = 0;
                    994:        if (req->isquery && req->q.equal && argc == 1)
                    995:                pg_redirect(req, argv[0]);
                    996:        else if (mansearch(&search, &paths, argc, argv, &res, &ressz) == 0)
1.12      schwarze  997:                pg_noresult(req, "You entered an invalid query.");
1.87      schwarze  998:        else if (ressz == 0)
1.12      schwarze  999:                pg_noresult(req, "No results found.");
1.1       schwarze 1000:        else
1.12      schwarze 1001:                pg_searchres(req, res, ressz);
1.1       schwarze 1002:
1.36      schwarze 1003:        free(query);
                   1004:        mansearch_free(res, ressz);
1.1       schwarze 1005:        free(paths.paths[0]);
                   1006:        free(paths.paths);
                   1007: }
                   1008:
                   1009: int
                   1010: main(void)
                   1011: {
1.6       schwarze 1012:        struct req       req;
1.33      schwarze 1013:        struct itimerval itimer;
1.6       schwarze 1014:        const char      *path;
1.23      schwarze 1015:        const char      *querystring;
1.1       schwarze 1016:        int              i;
1.86      schwarze 1017:
                   1018:        /*
                   1019:         * The "rpath" pledge could be revoked after mparse_readfd()
                   1020:         * if the file desciptor to "/footer.html" would be opened
                   1021:         * up front, but it's probably not worth the complication
                   1022:         * of the code it would cause: it would require scattering
                   1023:         * pledge() calls in multiple low-level resp_*() functions.
                   1024:         */
                   1025:
                   1026:        if (pledge("stdio rpath", NULL) == -1) {
                   1027:                warn("pledge");
                   1028:                pg_error_internal();
                   1029:                return EXIT_FAILURE;
                   1030:        }
1.33      schwarze 1031:
                   1032:        /* Poor man's ReDoS mitigation. */
                   1033:
1.38      schwarze 1034:        itimer.it_value.tv_sec = 2;
1.33      schwarze 1035:        itimer.it_value.tv_usec = 0;
1.38      schwarze 1036:        itimer.it_interval.tv_sec = 2;
1.33      schwarze 1037:        itimer.it_interval.tv_usec = 0;
                   1038:        if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1.67      schwarze 1039:                warn("setitimer");
1.20      schwarze 1040:                pg_error_internal();
1.48      schwarze 1041:                return EXIT_FAILURE;
1.20      schwarze 1042:        }
                   1043:
1.1       schwarze 1044:        /*
1.7       schwarze 1045:         * First we change directory into the MAN_DIR so that
1.1       schwarze 1046:         * subsequent scanning for manpath directories is rooted
                   1047:         * relative to the same position.
                   1048:         */
                   1049:
1.67      schwarze 1050:        if (chdir(MAN_DIR) == -1) {
                   1051:                warn("MAN_DIR: %s", MAN_DIR);
1.12      schwarze 1052:                pg_error_internal();
1.48      schwarze 1053:                return EXIT_FAILURE;
1.43      schwarze 1054:        }
1.1       schwarze 1055:
                   1056:        memset(&req, 0, sizeof(struct req));
1.57      schwarze 1057:        req.q.equal = 1;
1.68      schwarze 1058:        parse_manpath_conf(&req);
1.1       schwarze 1059:
1.56      schwarze 1060:        /* Parse the path info and the query string. */
1.1       schwarze 1061:
1.56      schwarze 1062:        if ((path = getenv("PATH_INFO")) == NULL)
                   1063:                path = "";
                   1064:        else if (*path == '/')
                   1065:                path++;
                   1066:
1.63      schwarze 1067:        if (*path != '\0') {
1.68      schwarze 1068:                parse_path_info(&req, path);
1.72      schwarze 1069:                if (req.q.manpath == NULL || access(path, F_OK) == -1)
1.63      schwarze 1070:                        path = "";
1.56      schwarze 1071:        } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.68      schwarze 1072:                parse_query_string(&req, querystring);
1.17      schwarze 1073:
1.56      schwarze 1074:        /* Validate parsed data and add defaults. */
                   1075:
1.41      schwarze 1076:        if (req.q.manpath == NULL)
                   1077:                req.q.manpath = mandoc_strdup(req.p[0]);
                   1078:        else if ( ! validate_manpath(&req, req.q.manpath)) {
1.17      schwarze 1079:                pg_error_badrequest(
                   1080:                    "You specified an invalid manpath.");
1.48      schwarze 1081:                return EXIT_FAILURE;
1.17      schwarze 1082:        }
1.1       schwarze 1083:
1.20      schwarze 1084:        if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
                   1085:                pg_error_badrequest(
                   1086:                    "You specified an invalid architecture.");
1.48      schwarze 1087:                return EXIT_FAILURE;
1.20      schwarze 1088:        }
                   1089:
1.6       schwarze 1090:        /* Dispatch to the three different pages. */
1.1       schwarze 1091:
1.6       schwarze 1092:        if ('\0' != *path)
                   1093:                pg_show(&req, path);
1.25      schwarze 1094:        else if (NULL != req.q.query)
1.6       schwarze 1095:                pg_search(&req);
                   1096:        else
1.12      schwarze 1097:                pg_index(&req);
1.1       schwarze 1098:
1.23      schwarze 1099:        free(req.q.manpath);
                   1100:        free(req.q.arch);
                   1101:        free(req.q.sec);
1.25      schwarze 1102:        free(req.q.query);
1.1       schwarze 1103:        for (i = 0; i < (int)req.psz; i++)
                   1104:                free(req.p[i]);
                   1105:        free(req.p);
1.48      schwarze 1106:        return EXIT_SUCCESS;
1.56      schwarze 1107: }
                   1108:
                   1109: /*
                   1110:  * If PATH_INFO is not a file name, translate it to a query.
                   1111:  */
                   1112: static void
1.68      schwarze 1113: parse_path_info(struct req *req, const char *path)
1.56      schwarze 1114: {
1.74      schwarze 1115:        char    *dir[4];
                   1116:        int      i;
1.56      schwarze 1117:
1.60      schwarze 1118:        req->isquery = 0;
1.56      schwarze 1119:        req->q.equal = 1;
                   1120:        req->q.manpath = mandoc_strdup(path);
1.74      schwarze 1121:        req->q.arch = NULL;
1.56      schwarze 1122:
                   1123:        /* Mandatory manual page name. */
                   1124:        if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) {
                   1125:                req->q.query = req->q.manpath;
                   1126:                req->q.manpath = NULL;
                   1127:        } else
                   1128:                *req->q.query++ = '\0';
                   1129:
                   1130:        /* Optional trailing section. */
                   1131:        if ((req->q.sec = strrchr(req->q.query, '.')) != NULL) {
                   1132:                if(isdigit((unsigned char)req->q.sec[1])) {
                   1133:                        *req->q.sec++ = '\0';
                   1134:                        req->q.sec = mandoc_strdup(req->q.sec);
                   1135:                } else
                   1136:                        req->q.sec = NULL;
                   1137:        }
                   1138:
                   1139:        /* Handle the case of name[.section] only. */
1.74      schwarze 1140:        if (req->q.manpath == NULL)
1.56      schwarze 1141:                return;
1.74      schwarze 1142:        req->q.query = mandoc_strdup(req->q.query);
                   1143:
                   1144:        /* Split directory components. */
                   1145:        dir[i = 0] = req->q.manpath;
                   1146:        while ((dir[i + 1] = strchr(dir[i], '/')) != NULL) {
                   1147:                if (++i == 3) {
                   1148:                        pg_error_badrequest(
                   1149:                            "You specified too many directory components.");
                   1150:                        exit(EXIT_FAILURE);
                   1151:                }
                   1152:                *dir[i]++ = '\0';
1.56      schwarze 1153:        }
                   1154:
1.74      schwarze 1155:        /* Optional manpath. */
                   1156:        if ((i = validate_manpath(req, req->q.manpath)) == 0)
                   1157:                req->q.manpath = NULL;
                   1158:        else if (dir[1] == NULL)
                   1159:                return;
1.56      schwarze 1160:
1.74      schwarze 1161:        /* Optional section. */
                   1162:        if (strncmp(dir[i], "man", 3) == 0) {
1.66      schwarze 1163:                free(req->q.sec);
1.74      schwarze 1164:                req->q.sec = mandoc_strdup(dir[i++] + 3);
1.56      schwarze 1165:        }
1.74      schwarze 1166:        if (dir[i] == NULL) {
                   1167:                if (req->q.manpath == NULL)
                   1168:                        free(dir[0]);
                   1169:                return;
                   1170:        }
                   1171:        if (dir[i + 1] != NULL) {
                   1172:                pg_error_badrequest(
                   1173:                    "You specified an invalid directory component.");
                   1174:                exit(EXIT_FAILURE);
                   1175:        }
                   1176:
                   1177:        /* Optional architecture. */
                   1178:        if (i) {
                   1179:                req->q.arch = mandoc_strdup(dir[i]);
                   1180:                if (req->q.manpath == NULL)
                   1181:                        free(dir[0]);
                   1182:        } else
                   1183:                req->q.arch = dir[0];
1.1       schwarze 1184: }
                   1185:
                   1186: /*
                   1187:  * Scan for indexable paths.
                   1188:  */
                   1189: static void
1.68      schwarze 1190: parse_manpath_conf(struct req *req)
1.1       schwarze 1191: {
                   1192:        FILE    *fp;
                   1193:        char    *dp;
                   1194:        size_t   dpsz;
1.54      schwarze 1195:        ssize_t  len;
1.1       schwarze 1196:
1.67      schwarze 1197:        if ((fp = fopen("manpath.conf", "r")) == NULL) {
                   1198:                warn("%s/manpath.conf", MAN_DIR);
1.14      schwarze 1199:                pg_error_internal();
                   1200:                exit(EXIT_FAILURE);
                   1201:        }
1.1       schwarze 1202:
1.54      schwarze 1203:        dp = NULL;
                   1204:        dpsz = 0;
                   1205:
                   1206:        while ((len = getline(&dp, &dpsz, fp)) != -1) {
                   1207:                if (dp[len - 1] == '\n')
                   1208:                        dp[--len] = '\0';
1.1       schwarze 1209:                req->p = mandoc_realloc(req->p,
                   1210:                    (req->psz + 1) * sizeof(char *));
1.20      schwarze 1211:                if ( ! validate_urifrag(dp)) {
1.67      schwarze 1212:                        warnx("%s/manpath.conf contains "
                   1213:                            "unsafe path \"%s\"", MAN_DIR, dp);
1.20      schwarze 1214:                        pg_error_internal();
                   1215:                        exit(EXIT_FAILURE);
                   1216:                }
1.67      schwarze 1217:                if (strchr(dp, '/') != NULL) {
                   1218:                        warnx("%s/manpath.conf contains "
                   1219:                            "path with slash \"%s\"", MAN_DIR, dp);
1.20      schwarze 1220:                        pg_error_internal();
                   1221:                        exit(EXIT_FAILURE);
                   1222:                }
                   1223:                req->p[req->psz++] = dp;
1.54      schwarze 1224:                dp = NULL;
                   1225:                dpsz = 0;
1.14      schwarze 1226:        }
1.54      schwarze 1227:        free(dp);
1.14      schwarze 1228:
1.67      schwarze 1229:        if (req->p == NULL) {
                   1230:                warnx("%s/manpath.conf is empty", MAN_DIR);
1.14      schwarze 1231:                pg_error_internal();
                   1232:                exit(EXIT_FAILURE);
1.1       schwarze 1233:        }
                   1234: }