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

Annotation of mandoc/cgi.c, Revision 1.161

1.161   ! schwarze    1: /*     $Id: cgi.c,v 1.160 2018/10/02 19:41:23 schwarze Exp $ */
1.6       kristaps    2: /*
1.42      kristaps    3:  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.160     schwarze    4:  * Copyright (c) 2014, 2015, 2016, 2017, 2018 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.160     schwarze  870:        conf.toc = 1;
1.90      schwarze  871:        usepath = strcmp(req->q.manpath, req->p[0]);
1.152     schwarze  872:        mandoc_asprintf(&conf.man, "/%s%s%s%s%%N.%%S",
                    873:            scriptname, *scriptname == '\0' ? "" : "/",
1.122     schwarze  874:            usepath ? req->q.manpath : "", usepath ? "/" : "");
1.10      kristaps  875:
1.108     schwarze  876:        mparse_result(mp, &man, NULL);
                    877:        if (man == NULL) {
1.128     schwarze  878:                warnx("fatal mandoc error: %s/%s", req->q.manpath, file);
1.72      schwarze  879:                pg_error_internal();
1.32      kristaps  880:                mparse_free(mp);
1.110     schwarze  881:                mchars_free();
1.32      kristaps  882:                return;
                    883:        }
                    884:
1.110     schwarze  885:        vp = html_alloc(&conf);
1.7       kristaps  886:
1.111     schwarze  887:        if (man->macroset == MACROSET_MDOC) {
                    888:                mdoc_validate(man);
1.108     schwarze  889:                html_mdoc(vp, man);
1.112     schwarze  890:        } else {
                    891:                man_validate(man);
1.8       kristaps  892:                html_man(vp, man);
1.112     schwarze  893:        }
1.32      kristaps  894:
1.8       kristaps  895:        html_free(vp);
                    896:        mparse_free(mp);
1.110     schwarze  897:        mchars_free();
1.106     schwarze  898:        free(conf.man);
1.145     schwarze  899:        free(conf.style);
1.7       kristaps  900: }
                    901:
                    902: static void
1.70      schwarze  903: resp_show(const struct req *req, const char *file)
                    904: {
1.76      schwarze  905:
                    906:        if ('.' == file[0] && '/' == file[1])
1.71      schwarze  907:                file += 2;
1.70      schwarze  908:
                    909:        if ('c' == *file)
1.129     schwarze  910:                resp_catman(req, file);
1.70      schwarze  911:        else
1.129     schwarze  912:                resp_format(req, file);
1.70      schwarze  913: }
                    914:
                    915: static void
1.84      schwarze  916: pg_show(struct req *req, const char *fullpath)
1.1       kristaps  917: {
1.84      schwarze  918:        char            *manpath;
                    919:        const char      *file;
1.25      kristaps  920:
1.84      schwarze  921:        if ((file = strchr(fullpath, '/')) == NULL) {
1.72      schwarze  922:                pg_error_badrequest(
1.59      schwarze  923:                    "You did not specify a page to show.");
1.25      kristaps  924:                return;
1.104     schwarze  925:        }
1.84      schwarze  926:        manpath = mandoc_strndup(fullpath, file - fullpath);
                    927:        file++;
1.6       kristaps  928:
1.84      schwarze  929:        if ( ! validate_manpath(req, manpath)) {
1.77      schwarze  930:                pg_error_badrequest(
                    931:                    "You specified an invalid manpath.");
1.84      schwarze  932:                free(manpath);
1.77      schwarze  933:                return;
                    934:        }
                    935:
1.24      kristaps  936:        /*
1.58      schwarze  937:         * Begin by chdir()ing into the manpath.
1.24      kristaps  938:         * This way we can pick up the database files, which are
                    939:         * relative to the manpath root.
                    940:         */
                    941:
1.84      schwarze  942:        if (chdir(manpath) == -1) {
1.128     schwarze  943:                warn("chdir %s", manpath);
1.77      schwarze  944:                pg_error_internal();
1.84      schwarze  945:                free(manpath);
1.76      schwarze  946:                return;
                    947:        }
1.134     schwarze  948:        free(manpath);
1.84      schwarze  949:
                    950:        if ( ! validate_filename(file)) {
1.76      schwarze  951:                pg_error_badrequest(
                    952:                    "You specified an invalid manual file.");
1.24      kristaps  953:                return;
                    954:        }
1.79      schwarze  955:
1.150     schwarze  956:        resp_begin_html(200, NULL, file);
1.131     schwarze  957:        resp_searchform(req, FOCUS_NONE);
1.84      schwarze  958:        resp_show(req, file);
1.70      schwarze  959:        resp_end_html();
1.6       kristaps  960: }
                    961:
                    962: static void
1.66      schwarze  963: pg_search(const struct req *req)
1.6       kristaps  964: {
1.52      schwarze  965:        struct mansearch          search;
                    966:        struct manpaths           paths;
                    967:        struct manpage           *res;
1.97      schwarze  968:        char                    **argv;
                    969:        char                     *query, *rp, *wp;
1.52      schwarze  970:        size_t                    ressz;
1.97      schwarze  971:        int                       argc;
1.6       kristaps  972:
                    973:        /*
1.24      kristaps  974:         * Begin by chdir()ing into the root of the manpath.
                    975:         * This way we can pick up the database files, which are
                    976:         * relative to the manpath root.
                    977:         */
                    978:
1.128     schwarze  979:        if (chdir(req->q.manpath) == -1) {
                    980:                warn("chdir %s", req->q.manpath);
1.77      schwarze  981:                pg_error_internal();
1.24      kristaps  982:                return;
                    983:        }
                    984:
1.52      schwarze  985:        search.arch = req->q.arch;
                    986:        search.sec = req->q.sec;
1.94      schwarze  987:        search.outkey = "Nd";
                    988:        search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.101     schwarze  989:        search.firstmatch = 1;
1.52      schwarze  990:
                    991:        paths.sz = 1;
                    992:        paths.paths = mandoc_malloc(sizeof(char *));
                    993:        paths.paths[0] = mandoc_strdup(".");
1.24      kristaps  994:
                    995:        /*
1.97      schwarze  996:         * Break apart at spaces with backslash-escaping.
1.6       kristaps  997:         */
                    998:
1.97      schwarze  999:        argc = 0;
                   1000:        argv = NULL;
                   1001:        rp = query = mandoc_strdup(req->q.query);
                   1002:        for (;;) {
                   1003:                while (isspace((unsigned char)*rp))
                   1004:                        rp++;
                   1005:                if (*rp == '\0')
                   1006:                        break;
                   1007:                argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
                   1008:                argv[argc++] = wp = rp;
                   1009:                for (;;) {
                   1010:                        if (isspace((unsigned char)*rp)) {
                   1011:                                *wp = '\0';
                   1012:                                rp++;
                   1013:                                break;
                   1014:                        }
                   1015:                        if (rp[0] == '\\' && rp[1] != '\0')
                   1016:                                rp++;
                   1017:                        if (wp != rp)
                   1018:                                *wp = *rp;
                   1019:                        if (*rp == '\0')
                   1020:                                break;
                   1021:                        wp++;
                   1022:                        rp++;
                   1023:                }
1.6       kristaps 1024:        }
                   1025:
1.149     schwarze 1026:        res = NULL;
                   1027:        ressz = 0;
                   1028:        if (req->isquery && req->q.equal && argc == 1)
                   1029:                pg_redirect(req, argv[0]);
                   1030:        else if (mansearch(&search, &paths, argc, argv, &res, &ressz) == 0)
1.72      schwarze 1031:                pg_noresult(req, "You entered an invalid query.");
1.149     schwarze 1032:        else if (ressz == 0)
1.72      schwarze 1033:                pg_noresult(req, "No results found.");
1.59      schwarze 1034:        else
1.72      schwarze 1035:                pg_searchres(req, res, ressz);
1.6       kristaps 1036:
1.97      schwarze 1037:        free(query);
                   1038:        mansearch_free(res, ressz);
1.52      schwarze 1039:        free(paths.paths[0]);
                   1040:        free(paths.paths);
1.1       kristaps 1041: }
                   1042:
                   1043: int
                   1044: main(void)
                   1045: {
1.66      schwarze 1046:        struct req       req;
1.95      schwarze 1047:        struct itimerval itimer;
1.66      schwarze 1048:        const char      *path;
1.83      schwarze 1049:        const char      *querystring;
1.1       kristaps 1050:        int              i;
1.148     schwarze 1051:
                   1052: #if HAVE_PLEDGE
                   1053:        /*
                   1054:         * The "rpath" pledge could be revoked after mparse_readfd()
                   1055:         * if the file desciptor to "/footer.html" would be opened
                   1056:         * up front, but it's probably not worth the complication
                   1057:         * of the code it would cause: it would require scattering
                   1058:         * pledge() calls in multiple low-level resp_*() functions.
                   1059:         */
                   1060:
                   1061:        if (pledge("stdio rpath", NULL) == -1) {
                   1062:                warn("pledge");
                   1063:                pg_error_internal();
                   1064:                return EXIT_FAILURE;
                   1065:        }
                   1066: #endif
1.95      schwarze 1067:
                   1068:        /* Poor man's ReDoS mitigation. */
                   1069:
1.99      schwarze 1070:        itimer.it_value.tv_sec = 2;
1.95      schwarze 1071:        itimer.it_value.tv_usec = 0;
1.99      schwarze 1072:        itimer.it_interval.tv_sec = 2;
1.95      schwarze 1073:        itimer.it_interval.tv_usec = 0;
                   1074:        if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1.128     schwarze 1075:                warn("setitimer");
1.80      schwarze 1076:                pg_error_internal();
1.109     schwarze 1077:                return EXIT_FAILURE;
1.80      schwarze 1078:        }
                   1079:
1.24      kristaps 1080:        /*
1.67      schwarze 1081:         * First we change directory into the MAN_DIR so that
1.24      kristaps 1082:         * subsequent scanning for manpath directories is rooted
                   1083:         * relative to the same position.
                   1084:         */
                   1085:
1.128     schwarze 1086:        if (chdir(MAN_DIR) == -1) {
                   1087:                warn("MAN_DIR: %s", MAN_DIR);
1.72      schwarze 1088:                pg_error_internal();
1.109     schwarze 1089:                return EXIT_FAILURE;
1.104     schwarze 1090:        }
1.24      kristaps 1091:
                   1092:        memset(&req, 0, sizeof(struct req));
1.118     schwarze 1093:        req.q.equal = 1;
1.129     schwarze 1094:        parse_manpath_conf(&req);
1.1       kristaps 1095:
1.117     schwarze 1096:        /* Parse the path info and the query string. */
1.1       kristaps 1097:
1.117     schwarze 1098:        if ((path = getenv("PATH_INFO")) == NULL)
                   1099:                path = "";
                   1100:        else if (*path == '/')
                   1101:                path++;
                   1102:
1.124     schwarze 1103:        if (*path != '\0') {
1.129     schwarze 1104:                parse_path_info(&req, path);
1.154     schwarze 1105:                if (req.q.manpath == NULL || req.q.sec == NULL ||
                   1106:                    *req.q.query == '\0' || access(path, F_OK) == -1)
1.124     schwarze 1107:                        path = "";
1.117     schwarze 1108:        } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.129     schwarze 1109:                parse_query_string(&req, querystring);
1.77      schwarze 1110:
1.117     schwarze 1111:        /* Validate parsed data and add defaults. */
                   1112:
1.102     schwarze 1113:        if (req.q.manpath == NULL)
                   1114:                req.q.manpath = mandoc_strdup(req.p[0]);
                   1115:        else if ( ! validate_manpath(&req, req.q.manpath)) {
1.77      schwarze 1116:                pg_error_badrequest(
                   1117:                    "You specified an invalid manpath.");
1.109     schwarze 1118:                return EXIT_FAILURE;
1.77      schwarze 1119:        }
1.1       kristaps 1120:
1.159     schwarze 1121:        if (req.q.arch != NULL && validate_arch(req.q.arch) == 0) {
1.80      schwarze 1122:                pg_error_badrequest(
                   1123:                    "You specified an invalid architecture.");
1.109     schwarze 1124:                return EXIT_FAILURE;
1.80      schwarze 1125:        }
                   1126:
1.66      schwarze 1127:        /* Dispatch to the three different pages. */
1.1       kristaps 1128:
1.66      schwarze 1129:        if ('\0' != *path)
                   1130:                pg_show(&req, path);
1.85      schwarze 1131:        else if (NULL != req.q.query)
1.66      schwarze 1132:                pg_search(&req);
                   1133:        else
1.72      schwarze 1134:                pg_index(&req);
1.1       kristaps 1135:
1.83      schwarze 1136:        free(req.q.manpath);
                   1137:        free(req.q.arch);
                   1138:        free(req.q.sec);
1.85      schwarze 1139:        free(req.q.query);
1.52      schwarze 1140:        for (i = 0; i < (int)req.psz; i++)
                   1141:                free(req.p[i]);
1.24      kristaps 1142:        free(req.p);
1.109     schwarze 1143:        return EXIT_SUCCESS;
1.117     schwarze 1144: }
                   1145:
                   1146: /*
1.161   ! schwarze 1147:  * Translate PATH_INFO to a query.
1.117     schwarze 1148:  */
                   1149: static void
1.129     schwarze 1150: parse_path_info(struct req *req, const char *path)
1.117     schwarze 1151: {
1.161   ! schwarze 1152:        const char      *name, *sec, *end;
1.117     schwarze 1153:
1.121     schwarze 1154:        req->isquery = 0;
1.117     schwarze 1155:        req->q.equal = 1;
1.161   ! schwarze 1156:        req->q.manpath = NULL;
1.135     schwarze 1157:        req->q.arch = NULL;
1.117     schwarze 1158:
                   1159:        /* Mandatory manual page name. */
1.161   ! schwarze 1160:        if ((name = strrchr(path, '/')) == NULL)
        !          1161:                name = path;
        !          1162:        else
        !          1163:                name++;
1.117     schwarze 1164:
                   1165:        /* Optional trailing section. */
1.161   ! schwarze 1166:        sec = strrchr(name, '.');
        !          1167:        if (sec != NULL && isdigit((unsigned char)*++sec)) {
        !          1168:                req->q.query = mandoc_strndup(name, sec - name - 1);
        !          1169:                req->q.sec = mandoc_strdup(sec);
        !          1170:        } else {
        !          1171:                req->q.query = mandoc_strdup(name);
        !          1172:                req->q.sec = NULL;
1.117     schwarze 1173:        }
                   1174:
                   1175:        /* Handle the case of name[.section] only. */
1.161   ! schwarze 1176:        if (name == path)
1.117     schwarze 1177:                return;
                   1178:
1.135     schwarze 1179:        /* Optional manpath. */
1.161   ! schwarze 1180:        end = strchr(path, '/');
        !          1181:        req->q.manpath = mandoc_strndup(path, end - path);
        !          1182:        if (validate_manpath(req, req->q.manpath)) {
        !          1183:                path = end + 1;
        !          1184:                if (name == path)
        !          1185:                        return;
        !          1186:        } else {
        !          1187:                free(req->q.manpath);
1.135     schwarze 1188:                req->q.manpath = NULL;
1.161   ! schwarze 1189:        }
1.117     schwarze 1190:
1.135     schwarze 1191:        /* Optional section. */
1.161   ! schwarze 1192:        if (strncmp(path, "man", 3) == 0) {
        !          1193:                path += 3;
        !          1194:                end = strchr(path, '/');
1.127     schwarze 1195:                free(req->q.sec);
1.161   ! schwarze 1196:                req->q.sec = mandoc_strndup(path, end - path);
        !          1197:                path = end + 1;
        !          1198:                if (name == path)
        !          1199:                        return;
1.117     schwarze 1200:        }
1.161   ! schwarze 1201:
        !          1202:        /* Optional architecture. */
        !          1203:        end = strchr(path, '/');
        !          1204:        if (end + 1 != name) {
        !          1205:                pg_error_badrequest(
        !          1206:                    "You specified too many directory components.");
        !          1207:                exit(EXIT_FAILURE);
1.135     schwarze 1208:        }
1.161   ! schwarze 1209:        req->q.arch = mandoc_strndup(path, end - path);
        !          1210:        if (validate_arch(req->q.arch) == 0) {
1.135     schwarze 1211:                pg_error_badrequest(
                   1212:                    "You specified an invalid directory component.");
                   1213:                exit(EXIT_FAILURE);
                   1214:        }
1.24      kristaps 1215: }
                   1216:
                   1217: /*
                   1218:  * Scan for indexable paths.
                   1219:  */
                   1220: static void
1.129     schwarze 1221: parse_manpath_conf(struct req *req)
1.24      kristaps 1222: {
1.54      schwarze 1223:        FILE    *fp;
                   1224:        char    *dp;
                   1225:        size_t   dpsz;
1.115     schwarze 1226:        ssize_t  len;
1.54      schwarze 1227:
1.128     schwarze 1228:        if ((fp = fopen("manpath.conf", "r")) == NULL) {
                   1229:                warn("%s/manpath.conf", MAN_DIR);
1.74      schwarze 1230:                pg_error_internal();
                   1231:                exit(EXIT_FAILURE);
                   1232:        }
1.24      kristaps 1233:
1.115     schwarze 1234:        dp = NULL;
                   1235:        dpsz = 0;
                   1236:
                   1237:        while ((len = getline(&dp, &dpsz, fp)) != -1) {
                   1238:                if (dp[len - 1] == '\n')
                   1239:                        dp[--len] = '\0';
1.54      schwarze 1240:                req->p = mandoc_realloc(req->p,
                   1241:                    (req->psz + 1) * sizeof(char *));
1.80      schwarze 1242:                if ( ! validate_urifrag(dp)) {
1.128     schwarze 1243:                        warnx("%s/manpath.conf contains "
                   1244:                            "unsafe path \"%s\"", MAN_DIR, dp);
1.80      schwarze 1245:                        pg_error_internal();
                   1246:                        exit(EXIT_FAILURE);
                   1247:                }
1.128     schwarze 1248:                if (strchr(dp, '/') != NULL) {
                   1249:                        warnx("%s/manpath.conf contains "
                   1250:                            "path with slash \"%s\"", MAN_DIR, dp);
1.80      schwarze 1251:                        pg_error_internal();
                   1252:                        exit(EXIT_FAILURE);
                   1253:                }
                   1254:                req->p[req->psz++] = dp;
1.115     schwarze 1255:                dp = NULL;
                   1256:                dpsz = 0;
1.74      schwarze 1257:        }
1.115     schwarze 1258:        free(dp);
1.74      schwarze 1259:
1.128     schwarze 1260:        if (req->p == NULL) {
                   1261:                warnx("%s/manpath.conf is empty", MAN_DIR);
1.74      schwarze 1262:                pg_error_internal();
                   1263:                exit(EXIT_FAILURE);
1.24      kristaps 1264:        }
                   1265: }

CVSweb