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

Annotation of mandoc/cgi.c, Revision 1.159

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

CVSweb