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

Annotation of mandoc/cgi.c, Revision 1.131

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

CVSweb