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

Annotation of mandoc/cgi.c, Revision 1.119

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

CVSweb