[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/cgi.c, Revision 1.64

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