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

1.59    ! schwarze    1: /*     $OpenBSD: cgi.c,v 1.58 2016/03/18 13:21:07 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:
1.58      schwarze   85: static const char       *scriptname = SCRIPT_NAME;
1.1       schwarze   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.58      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"
1.58      schwarze  508:               "<A HREF=\"/%s%smandoc/man8/man.cgi.8\">man.cgi</A>\n"
1.26      schwarze  509:               "manual, and the\n"
1.58      schwarze  510:               "<A HREF=\"/%s%smandoc/man1/apropos.1\">apropos</A>\n"
1.9       schwarze  511:               "manual explains the query syntax.\n"
1.4       schwarze  512:               "</P>\n",
1.58      schwarze  513:               scriptname, *scriptname == '\0' ? "" : "/",
                    514:               scriptname, *scriptname == '\0' ? "" : "/");
1.1       schwarze  515:        resp_end_html();
                    516: }
                    517:
                    518: static void
1.12      schwarze  519: pg_noresult(const struct req *req, const char *msg)
1.1       schwarze  520: {
                    521:        resp_begin_html(200, NULL);
                    522:        resp_searchform(req);
                    523:        puts("<P>");
                    524:        puts(msg);
                    525:        puts("</P>");
                    526:        resp_end_html();
                    527: }
                    528:
                    529: static void
1.12      schwarze  530: pg_error_badrequest(const char *msg)
1.1       schwarze  531: {
                    532:
                    533:        resp_begin_html(400, "Bad Request");
                    534:        puts("<H1>Bad Request</H1>\n"
                    535:             "<P>\n");
                    536:        puts(msg);
                    537:        printf("Try again from the\n"
1.58      schwarze  538:               "<A HREF=\"/%s\">main page</A>.\n"
1.1       schwarze  539:               "</P>", scriptname);
                    540:        resp_end_html();
                    541: }
                    542:
                    543: static void
1.12      schwarze  544: pg_error_internal(void)
1.1       schwarze  545: {
                    546:        resp_begin_html(500, "Internal Server Error");
                    547:        puts("<P>Internal Server Error</P>");
                    548:        resp_end_html();
                    549: }
                    550:
                    551: static void
1.12      schwarze  552: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1       schwarze  553: {
1.21      schwarze  554:        char            *arch, *archend;
1.59    ! schwarze  555:        const char      *sec;
        !           556:        size_t           i, iuse;
1.21      schwarze  557:        int              archprio, archpriouse;
1.10      schwarze  558:        int              prio, priouse;
1.1       schwarze  559:
1.16      schwarze  560:        for (i = 0; i < sz; i++) {
                    561:                if (validate_filename(r[i].file))
                    562:                        continue;
                    563:                fprintf(stderr, "invalid filename %s in %s database\n",
                    564:                    r[i].file, req->q.manpath);
                    565:                pg_error_internal();
                    566:                return;
                    567:        }
                    568:
1.1       schwarze  569:        if (1 == sz) {
                    570:                /*
                    571:                 * If we have just one result, then jump there now
                    572:                 * without any delay.
                    573:                 */
1.2       tedu      574:                printf("Status: 303 See Other\r\n");
1.58      schwarze  575:                printf("Location: http://%s/%s%s%s/%s",
                    576:                    HTTP_HOST, scriptname,
                    577:                    *scriptname == '\0' ? "" : "/",
                    578:                    req->q.manpath, r[0].file);
1.2       tedu      579:                printf("\r\n"
                    580:                     "Content-Type: text/html; charset=utf-8\r\n"
                    581:                     "\r\n");
1.1       schwarze  582:                return;
                    583:        }
                    584:
                    585:        resp_begin_html(200, NULL);
                    586:        resp_searchform(req);
                    587:        puts("<DIV CLASS=\"results\">");
                    588:        puts("<TABLE>");
                    589:
                    590:        for (i = 0; i < sz; i++) {
                    591:                printf("<TR>\n"
                    592:                       "<TD CLASS=\"title\">\n"
1.58      schwarze  593:                       "<A HREF=\"/%s%s%s/%s",
                    594:                    scriptname, *scriptname == '\0' ? "" : "/",
                    595:                    req->q.manpath, r[i].file);
1.1       schwarze  596:                printf("\">");
                    597:                html_print(r[i].names);
                    598:                printf("</A>\n"
                    599:                       "</TD>\n"
                    600:                       "<TD CLASS=\"desc\">");
                    601:                html_print(r[i].output);
                    602:                puts("</TD>\n"
                    603:                     "</TR>");
                    604:        }
                    605:
                    606:        puts("</TABLE>\n"
                    607:             "</DIV>");
1.10      schwarze  608:
                    609:        /*
                    610:         * In man(1) mode, show one of the pages
                    611:         * even if more than one is found.
                    612:         */
                    613:
                    614:        if (req->q.equal) {
                    615:                puts("<HR>");
                    616:                iuse = 0;
1.59    ! schwarze  617:                priouse = 20;
1.21      schwarze  618:                archpriouse = 3;
1.10      schwarze  619:                for (i = 0; i < sz; i++) {
1.59    ! schwarze  620:                        sec = r[i].file;
        !           621:                        sec += strcspn(sec, "123456789");
        !           622:                        if (sec[0] == '\0')
1.10      schwarze  623:                                continue;
1.59    ! schwarze  624:                        prio = sec_prios[sec[0] - '1'];
        !           625:                        if (sec[1] != '/')
        !           626:                                prio += 10;
        !           627:                        if (req->q.arch == NULL) {
1.21      schwarze  628:                                archprio =
1.59    ! schwarze  629:                                    ((arch = strchr(sec + 1, '/'))
        !           630:                                        == NULL) ? 3 :
        !           631:                                    ((archend = strchr(arch + 1, '/'))
        !           632:                                        == NULL) ? 0 :
1.21      schwarze  633:                                    strncmp(arch, "amd64/",
                    634:                                        archend - arch) ? 2 : 1;
                    635:                                if (archprio < archpriouse) {
                    636:                                        archpriouse = archprio;
                    637:                                        priouse = prio;
                    638:                                        iuse = i;
                    639:                                        continue;
                    640:                                }
                    641:                                if (archprio > archpriouse)
                    642:                                        continue;
                    643:                        }
1.10      schwarze  644:                        if (prio >= priouse)
                    645:                                continue;
                    646:                        priouse = prio;
                    647:                        iuse = i;
                    648:                }
                    649:                resp_show(req, r[iuse].file);
                    650:        }
                    651:
1.1       schwarze  652:        resp_end_html();
                    653: }
                    654:
                    655: static void
                    656: catman(const struct req *req, const char *file)
                    657: {
                    658:        FILE            *f;
1.54      schwarze  659:        char            *p;
                    660:        size_t           sz;
                    661:        ssize_t          len;
1.1       schwarze  662:        int              i;
                    663:        int              italic, bold;
                    664:
1.54      schwarze  665:        if ((f = fopen(file, "r")) == NULL) {
1.10      schwarze  666:                puts("<P>You specified an invalid manual file.</P>");
1.1       schwarze  667:                return;
                    668:        }
                    669:
                    670:        puts("<DIV CLASS=\"catman\">\n"
                    671:             "<PRE>");
                    672:
1.54      schwarze  673:        p = NULL;
                    674:        sz = 0;
                    675:
                    676:        while ((len = getline(&p, &sz, f)) != -1) {
1.1       schwarze  677:                bold = italic = 0;
1.54      schwarze  678:                for (i = 0; i < len - 1; i++) {
1.43      schwarze  679:                        /*
1.1       schwarze  680:                         * This means that the catpage is out of state.
                    681:                         * Ignore it and keep going (although the
                    682:                         * catpage is bogus).
                    683:                         */
                    684:
                    685:                        if ('\b' == p[i] || '\n' == p[i])
                    686:                                continue;
                    687:
                    688:                        /*
                    689:                         * Print a regular character.
                    690:                         * Close out any bold/italic scopes.
                    691:                         * If we're in back-space mode, make sure we'll
                    692:                         * have something to enter when we backspace.
                    693:                         */
                    694:
                    695:                        if ('\b' != p[i + 1]) {
                    696:                                if (italic)
                    697:                                        printf("</I>");
                    698:                                if (bold)
                    699:                                        printf("</B>");
                    700:                                italic = bold = 0;
                    701:                                html_putchar(p[i]);
                    702:                                continue;
1.54      schwarze  703:                        } else if (i + 2 >= len)
1.1       schwarze  704:                                continue;
                    705:
                    706:                        /* Italic mode. */
                    707:
                    708:                        if ('_' == p[i]) {
                    709:                                if (bold)
                    710:                                        printf("</B>");
                    711:                                if ( ! italic)
                    712:                                        printf("<I>");
                    713:                                bold = 0;
                    714:                                italic = 1;
                    715:                                i += 2;
                    716:                                html_putchar(p[i]);
                    717:                                continue;
                    718:                        }
                    719:
1.43      schwarze  720:                        /*
1.1       schwarze  721:                         * Handle funny behaviour troff-isms.
                    722:                         * These grok'd from the original man2html.c.
                    723:                         */
                    724:
                    725:                        if (('+' == p[i] && 'o' == p[i + 2]) ||
                    726:                                        ('o' == p[i] && '+' == p[i + 2]) ||
                    727:                                        ('|' == p[i] && '=' == p[i + 2]) ||
                    728:                                        ('=' == p[i] && '|' == p[i + 2]) ||
                    729:                                        ('*' == p[i] && '=' == p[i + 2]) ||
                    730:                                        ('=' == p[i] && '*' == p[i + 2]) ||
                    731:                                        ('*' == p[i] && '|' == p[i + 2]) ||
                    732:                                        ('|' == p[i] && '*' == p[i + 2]))  {
                    733:                                if (italic)
                    734:                                        printf("</I>");
                    735:                                if (bold)
                    736:                                        printf("</B>");
                    737:                                italic = bold = 0;
                    738:                                putchar('*');
                    739:                                i += 2;
                    740:                                continue;
                    741:                        } else if (('|' == p[i] && '-' == p[i + 2]) ||
                    742:                                        ('-' == p[i] && '|' == p[i + 1]) ||
                    743:                                        ('+' == p[i] && '-' == p[i + 1]) ||
                    744:                                        ('-' == p[i] && '+' == p[i + 1]) ||
                    745:                                        ('+' == p[i] && '|' == p[i + 1]) ||
                    746:                                        ('|' == p[i] && '+' == p[i + 1]))  {
                    747:                                if (italic)
                    748:                                        printf("</I>");
                    749:                                if (bold)
                    750:                                        printf("</B>");
                    751:                                italic = bold = 0;
                    752:                                putchar('+');
                    753:                                i += 2;
                    754:                                continue;
                    755:                        }
                    756:
                    757:                        /* Bold mode. */
1.43      schwarze  758:
1.1       schwarze  759:                        if (italic)
                    760:                                printf("</I>");
                    761:                        if ( ! bold)
                    762:                                printf("<B>");
                    763:                        bold = 1;
                    764:                        italic = 0;
                    765:                        i += 2;
                    766:                        html_putchar(p[i]);
                    767:                }
                    768:
1.43      schwarze  769:                /*
1.1       schwarze  770:                 * Clean up the last character.
1.43      schwarze  771:                 * We can get to a newline; don't print that.
1.1       schwarze  772:                 */
                    773:
                    774:                if (italic)
                    775:                        printf("</I>");
                    776:                if (bold)
                    777:                        printf("</B>");
                    778:
1.54      schwarze  779:                if (i == len - 1 && p[i] != '\n')
1.1       schwarze  780:                        html_putchar(p[i]);
                    781:
                    782:                putchar('\n');
                    783:        }
1.54      schwarze  784:        free(p);
1.1       schwarze  785:
                    786:        puts("</PRE>\n"
1.10      schwarze  787:             "</DIV>");
1.1       schwarze  788:
                    789:        fclose(f);
                    790: }
                    791:
                    792: static void
                    793: format(const struct req *req, const char *file)
                    794: {
1.45      schwarze  795:        struct manoutput conf;
1.1       schwarze  796:        struct mparse   *mp;
1.46      schwarze  797:        struct roff_man *man;
1.1       schwarze  798:        void            *vp;
1.30      schwarze  799:        int              fd;
                    800:        int              usepath;
1.1       schwarze  801:
                    802:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.10      schwarze  803:                puts("<P>You specified an invalid manual file.</P>");
1.1       schwarze  804:                return;
                    805:        }
                    806:
1.49      schwarze  807:        mchars_alloc();
                    808:        mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, req->q.manpath);
1.42      schwarze  809:        mparse_readfd(mp, fd, file);
1.1       schwarze  810:        close(fd);
                    811:
1.45      schwarze  812:        memset(&conf, 0, sizeof(conf));
                    813:        conf.fragment = 1;
1.30      schwarze  814:        usepath = strcmp(req->q.manpath, req->p[0]);
1.58      schwarze  815:        mandoc_asprintf(&conf.man, "/%s?query=%%N&sec=%%S%s%s%s%s",
1.30      schwarze  816:            scriptname,
                    817:            req->q.arch ? "&arch="       : "",
                    818:            req->q.arch ? req->q.arch    : "",
                    819:            usepath     ? "&manpath="    : "",
                    820:            usepath     ? req->q.manpath : "");
1.1       schwarze  821:
1.47      schwarze  822:        mparse_result(mp, &man, NULL);
                    823:        if (man == NULL) {
1.1       schwarze  824:                fprintf(stderr, "fatal mandoc error: %s/%s\n",
                    825:                    req->q.manpath, file);
1.12      schwarze  826:                pg_error_internal();
1.1       schwarze  827:                mparse_free(mp);
1.49      schwarze  828:                mchars_free();
1.1       schwarze  829:                return;
                    830:        }
                    831:
1.49      schwarze  832:        vp = html_alloc(&conf);
1.1       schwarze  833:
1.50      schwarze  834:        if (man->macroset == MACROSET_MDOC) {
                    835:                mdoc_validate(man);
1.47      schwarze  836:                html_mdoc(vp, man);
1.51      schwarze  837:        } else {
                    838:                man_validate(man);
1.1       schwarze  839:                html_man(vp, man);
1.51      schwarze  840:        }
1.1       schwarze  841:
                    842:        html_free(vp);
                    843:        mparse_free(mp);
1.49      schwarze  844:        mchars_free();
1.45      schwarze  845:        free(conf.man);
1.1       schwarze  846: }
                    847:
                    848: static void
1.10      schwarze  849: resp_show(const struct req *req, const char *file)
                    850: {
1.16      schwarze  851:
                    852:        if ('.' == file[0] && '/' == file[1])
1.11      schwarze  853:                file += 2;
1.10      schwarze  854:
                    855:        if ('c' == *file)
                    856:                catman(req, file);
                    857:        else
                    858:                format(req, file);
                    859: }
                    860:
                    861: static void
1.24      schwarze  862: pg_show(struct req *req, const char *fullpath)
1.1       schwarze  863: {
1.24      schwarze  864:        char            *manpath;
                    865:        const char      *file;
1.1       schwarze  866:
1.24      schwarze  867:        if ((file = strchr(fullpath, '/')) == NULL) {
1.12      schwarze  868:                pg_error_badrequest(
1.1       schwarze  869:                    "You did not specify a page to show.");
                    870:                return;
1.43      schwarze  871:        }
1.24      schwarze  872:        manpath = mandoc_strndup(fullpath, file - fullpath);
                    873:        file++;
1.1       schwarze  874:
1.24      schwarze  875:        if ( ! validate_manpath(req, manpath)) {
1.17      schwarze  876:                pg_error_badrequest(
                    877:                    "You specified an invalid manpath.");
1.24      schwarze  878:                free(manpath);
1.17      schwarze  879:                return;
                    880:        }
                    881:
1.1       schwarze  882:        /*
                    883:         * Begin by chdir()ing into the manpath.
                    884:         * This way we can pick up the database files, which are
                    885:         * relative to the manpath root.
                    886:         */
                    887:
1.24      schwarze  888:        if (chdir(manpath) == -1) {
1.17      schwarze  889:                fprintf(stderr, "chdir %s: %s\n",
1.24      schwarze  890:                    manpath, strerror(errno));
1.17      schwarze  891:                pg_error_internal();
1.24      schwarze  892:                free(manpath);
1.16      schwarze  893:                return;
                    894:        }
                    895:
1.24      schwarze  896:        if (strcmp(manpath, "mandoc")) {
                    897:                free(req->q.manpath);
                    898:                req->q.manpath = manpath;
                    899:        } else
                    900:                free(manpath);
                    901:
                    902:        if ( ! validate_filename(file)) {
1.16      schwarze  903:                pg_error_badrequest(
                    904:                    "You specified an invalid manual file.");
1.1       schwarze  905:                return;
                    906:        }
1.19      schwarze  907:
1.10      schwarze  908:        resp_begin_html(200, NULL);
                    909:        resp_searchform(req);
1.24      schwarze  910:        resp_show(req, file);
1.10      schwarze  911:        resp_end_html();
1.1       schwarze  912: }
                    913:
                    914: static void
1.6       schwarze  915: pg_search(const struct req *req)
1.1       schwarze  916: {
                    917:        struct mansearch          search;
                    918:        struct manpaths           paths;
                    919:        struct manpage           *res;
1.36      schwarze  920:        char                    **argv;
                    921:        char                     *query, *rp, *wp;
1.1       schwarze  922:        size_t                    ressz;
1.36      schwarze  923:        int                       argc;
1.1       schwarze  924:
                    925:        /*
                    926:         * Begin by chdir()ing into the root of the manpath.
                    927:         * This way we can pick up the database files, which are
                    928:         * relative to the manpath root.
                    929:         */
                    930:
                    931:        if (-1 == (chdir(req->q.manpath))) {
1.17      schwarze  932:                fprintf(stderr, "chdir %s: %s\n",
                    933:                    req->q.manpath, strerror(errno));
                    934:                pg_error_internal();
1.1       schwarze  935:                return;
                    936:        }
                    937:
                    938:        search.arch = req->q.arch;
                    939:        search.sec = req->q.sec;
1.35      schwarze  940:        search.outkey = "Nd";
                    941:        search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.40      schwarze  942:        search.firstmatch = 1;
1.1       schwarze  943:
                    944:        paths.sz = 1;
                    945:        paths.paths = mandoc_malloc(sizeof(char *));
                    946:        paths.paths[0] = mandoc_strdup(".");
                    947:
                    948:        /*
1.36      schwarze  949:         * Break apart at spaces with backslash-escaping.
1.1       schwarze  950:         */
                    951:
1.36      schwarze  952:        argc = 0;
                    953:        argv = NULL;
                    954:        rp = query = mandoc_strdup(req->q.query);
                    955:        for (;;) {
                    956:                while (isspace((unsigned char)*rp))
                    957:                        rp++;
                    958:                if (*rp == '\0')
                    959:                        break;
                    960:                argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
                    961:                argv[argc++] = wp = rp;
                    962:                for (;;) {
                    963:                        if (isspace((unsigned char)*rp)) {
                    964:                                *wp = '\0';
                    965:                                rp++;
                    966:                                break;
                    967:                        }
                    968:                        if (rp[0] == '\\' && rp[1] != '\0')
                    969:                                rp++;
                    970:                        if (wp != rp)
                    971:                                *wp = *rp;
                    972:                        if (*rp == '\0')
                    973:                                break;
                    974:                        wp++;
                    975:                        rp++;
                    976:                }
1.1       schwarze  977:        }
                    978:
1.36      schwarze  979:        if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz))
1.12      schwarze  980:                pg_noresult(req, "You entered an invalid query.");
1.1       schwarze  981:        else if (0 == ressz)
1.12      schwarze  982:                pg_noresult(req, "No results found.");
1.1       schwarze  983:        else
1.12      schwarze  984:                pg_searchres(req, res, ressz);
1.1       schwarze  985:
1.36      schwarze  986:        free(query);
                    987:        mansearch_free(res, ressz);
1.1       schwarze  988:        free(paths.paths[0]);
                    989:        free(paths.paths);
                    990: }
                    991:
                    992: int
                    993: main(void)
                    994: {
1.6       schwarze  995:        struct req       req;
1.33      schwarze  996:        struct itimerval itimer;
1.6       schwarze  997:        const char      *path;
1.23      schwarze  998:        const char      *querystring;
1.1       schwarze  999:        int              i;
1.33      schwarze 1000:
                   1001:        /* Poor man's ReDoS mitigation. */
                   1002:
1.38      schwarze 1003:        itimer.it_value.tv_sec = 2;
1.33      schwarze 1004:        itimer.it_value.tv_usec = 0;
1.38      schwarze 1005:        itimer.it_interval.tv_sec = 2;
1.33      schwarze 1006:        itimer.it_interval.tv_usec = 0;
                   1007:        if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
                   1008:                fprintf(stderr, "setitimer: %s\n", strerror(errno));
1.20      schwarze 1009:                pg_error_internal();
1.48      schwarze 1010:                return EXIT_FAILURE;
1.20      schwarze 1011:        }
                   1012:
1.1       schwarze 1013:        /*
1.7       schwarze 1014:         * First we change directory into the MAN_DIR so that
1.1       schwarze 1015:         * subsequent scanning for manpath directories is rooted
                   1016:         * relative to the same position.
                   1017:         */
                   1018:
1.7       schwarze 1019:        if (-1 == chdir(MAN_DIR)) {
1.1       schwarze 1020:                fprintf(stderr, "MAN_DIR: %s: %s\n",
1.7       schwarze 1021:                    MAN_DIR, strerror(errno));
1.12      schwarze 1022:                pg_error_internal();
1.48      schwarze 1023:                return EXIT_FAILURE;
1.43      schwarze 1024:        }
1.1       schwarze 1025:
                   1026:        memset(&req, 0, sizeof(struct req));
1.57      schwarze 1027:        req.q.equal = 1;
1.1       schwarze 1028:        pathgen(&req);
                   1029:
1.56      schwarze 1030:        /* Parse the path info and the query string. */
1.1       schwarze 1031:
1.56      schwarze 1032:        if ((path = getenv("PATH_INFO")) == NULL)
                   1033:                path = "";
                   1034:        else if (*path == '/')
                   1035:                path++;
                   1036:
                   1037:        if (*path != '\0' && access(path, F_OK) == -1) {
                   1038:                path_parse(&req, path);
                   1039:                path = "";
                   1040:        } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.1       schwarze 1041:                http_parse(&req, querystring);
1.17      schwarze 1042:
1.56      schwarze 1043:        /* Validate parsed data and add defaults. */
                   1044:
1.41      schwarze 1045:        if (req.q.manpath == NULL)
                   1046:                req.q.manpath = mandoc_strdup(req.p[0]);
                   1047:        else if ( ! validate_manpath(&req, req.q.manpath)) {
1.17      schwarze 1048:                pg_error_badrequest(
                   1049:                    "You specified an invalid manpath.");
1.48      schwarze 1050:                return EXIT_FAILURE;
1.17      schwarze 1051:        }
1.1       schwarze 1052:
1.20      schwarze 1053:        if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
                   1054:                pg_error_badrequest(
                   1055:                    "You specified an invalid architecture.");
1.48      schwarze 1056:                return EXIT_FAILURE;
1.20      schwarze 1057:        }
                   1058:
1.6       schwarze 1059:        /* Dispatch to the three different pages. */
1.1       schwarze 1060:
1.6       schwarze 1061:        if ('\0' != *path)
                   1062:                pg_show(&req, path);
1.25      schwarze 1063:        else if (NULL != req.q.query)
1.6       schwarze 1064:                pg_search(&req);
                   1065:        else
1.12      schwarze 1066:                pg_index(&req);
1.1       schwarze 1067:
1.23      schwarze 1068:        free(req.q.manpath);
                   1069:        free(req.q.arch);
                   1070:        free(req.q.sec);
1.25      schwarze 1071:        free(req.q.query);
1.1       schwarze 1072:        for (i = 0; i < (int)req.psz; i++)
                   1073:                free(req.p[i]);
                   1074:        free(req.p);
1.48      schwarze 1075:        return EXIT_SUCCESS;
1.56      schwarze 1076: }
                   1077:
                   1078: /*
                   1079:  * If PATH_INFO is not a file name, translate it to a query.
                   1080:  */
                   1081: static void
                   1082: path_parse(struct req *req, const char *path)
                   1083: {
                   1084:        int      dir_done;
                   1085:
                   1086:        req->q.equal = 1;
                   1087:        req->q.manpath = mandoc_strdup(path);
                   1088:
                   1089:        /* Mandatory manual page name. */
                   1090:        if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) {
                   1091:                req->q.query = req->q.manpath;
                   1092:                req->q.manpath = NULL;
                   1093:        } else
                   1094:                *req->q.query++ = '\0';
                   1095:
                   1096:        /* Optional trailing section. */
                   1097:        if ((req->q.sec = strrchr(req->q.query, '.')) != NULL) {
                   1098:                if(isdigit((unsigned char)req->q.sec[1])) {
                   1099:                        *req->q.sec++ = '\0';
                   1100:                        req->q.sec = mandoc_strdup(req->q.sec);
                   1101:                } else
                   1102:                        req->q.sec = NULL;
                   1103:        }
                   1104:
                   1105:        /* Handle the case of name[.section] only. */
                   1106:        if (req->q.manpath == NULL) {
                   1107:                req->q.arch = NULL;
                   1108:                return;
                   1109:        }
                   1110:        req->q.query = mandoc_strdup(req->q.query);
                   1111:
                   1112:        /* Optional architecture. */
                   1113:        dir_done = 0;
                   1114:        for (;;) {
                   1115:                if ((req->q.arch = strrchr(req->q.manpath, '/')) == NULL)
                   1116:                        break;
                   1117:                *req->q.arch++ = '\0';
                   1118:                if (dir_done || strncmp(req->q.arch, "man", 3)) {
                   1119:                        req->q.arch = mandoc_strdup(req->q.arch);
                   1120:                        break;
                   1121:                }
                   1122:
                   1123:                /* Optional directory name. */
                   1124:                req->q.arch += 3;
                   1125:                if (*req->q.arch != '\0') {
                   1126:                        free(req->q.sec);
                   1127:                        req->q.sec = mandoc_strdup(req->q.arch);
                   1128:                }
                   1129:                dir_done = 1;
                   1130:        }
1.1       schwarze 1131: }
                   1132:
                   1133: /*
                   1134:  * Scan for indexable paths.
                   1135:  */
                   1136: static void
                   1137: pathgen(struct req *req)
                   1138: {
                   1139:        FILE    *fp;
                   1140:        char    *dp;
                   1141:        size_t   dpsz;
1.54      schwarze 1142:        ssize_t  len;
1.1       schwarze 1143:
1.14      schwarze 1144:        if (NULL == (fp = fopen("manpath.conf", "r"))) {
                   1145:                fprintf(stderr, "%s/manpath.conf: %s\n",
                   1146:                        MAN_DIR, strerror(errno));
                   1147:                pg_error_internal();
                   1148:                exit(EXIT_FAILURE);
                   1149:        }
1.1       schwarze 1150:
1.54      schwarze 1151:        dp = NULL;
                   1152:        dpsz = 0;
                   1153:
                   1154:        while ((len = getline(&dp, &dpsz, fp)) != -1) {
                   1155:                if (dp[len - 1] == '\n')
                   1156:                        dp[--len] = '\0';
1.1       schwarze 1157:                req->p = mandoc_realloc(req->p,
                   1158:                    (req->psz + 1) * sizeof(char *));
1.20      schwarze 1159:                if ( ! validate_urifrag(dp)) {
                   1160:                        fprintf(stderr, "%s/manpath.conf contains "
                   1161:                            "unsafe path \"%s\"\n", MAN_DIR, dp);
                   1162:                        pg_error_internal();
                   1163:                        exit(EXIT_FAILURE);
                   1164:                }
                   1165:                if (NULL != strchr(dp, '/')) {
                   1166:                        fprintf(stderr, "%s/manpath.conf contains "
                   1167:                            "path with slash \"%s\"\n", MAN_DIR, dp);
                   1168:                        pg_error_internal();
                   1169:                        exit(EXIT_FAILURE);
                   1170:                }
                   1171:                req->p[req->psz++] = dp;
1.54      schwarze 1172:                dp = NULL;
                   1173:                dpsz = 0;
1.14      schwarze 1174:        }
1.54      schwarze 1175:        free(dp);
1.14      schwarze 1176:
                   1177:        if ( req->p == NULL ) {
                   1178:                fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR);
                   1179:                pg_error_internal();
                   1180:                exit(EXIT_FAILURE);
1.1       schwarze 1181:        }
                   1182: }