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

Annotation of mandoc/cgi.c, Revision 1.148

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

CVSweb