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

Annotation of mandoc/cgi.c, Revision 1.118

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

CVSweb