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

Annotation of mandoc/cgi.c, Revision 1.101

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

CVSweb