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

Annotation of mandoc/cgi.c, Revision 1.123

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

CVSweb