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

Annotation of mandoc/cgi.c, Revision 1.135

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

CVSweb