[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.57

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