Annotation of mandoc/cgi.c, Revision 1.163
1.163 ! schwarze 1: /* $Id: cgi.c,v 1.162 2018/12/13 11:55:46 schwarze Exp $ */
1.6 kristaps 2: /*
1.42 kristaps 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.160 schwarze 4: * Copyright (c) 2014, 2015, 2016, 2017, 2018 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.147 schwarze 24: #if HAVE_ERR
1.128 schwarze 25: #include <err.h>
1.147 schwarze 26: #endif
1.58 schwarze 27: #include <errno.h>
1.1 kristaps 28: #include <fcntl.h>
1.6 kristaps 29: #include <limits.h>
1.92 schwarze 30: #include <stdint.h>
1.1 kristaps 31: #include <stdio.h>
32: #include <stdlib.h>
33: #include <string.h>
1.6 kristaps 34: #include <unistd.h>
1.1 kristaps 35:
1.108 schwarze 36: #include "mandoc_aux.h"
1.4 schwarze 37: #include "mandoc.h"
1.108 schwarze 38: #include "roff.h"
1.111 schwarze 39: #include "mdoc.h"
1.112 schwarze 40: #include "man.h"
1.162 schwarze 41: #include "mandoc_parse.h"
1.8 kristaps 42: #include "main.h"
1.105 schwarze 43: #include "manconf.h"
1.52 schwarze 44: #include "mansearch.h"
1.67 schwarze 45: #include "cgi.h"
1.1 kristaps 46:
1.20 kristaps 47: /*
48: * A query as passed to the search function.
49: */
50: struct query {
1.83 schwarze 51: char *manpath; /* desired manual directory */
52: char *arch; /* architecture */
53: char *sec; /* manual section */
1.85 schwarze 54: char *query; /* unparsed query expression */
1.65 schwarze 55: int equal; /* match whole names, not substrings */
1.20 kristaps 56: };
57:
1.1 kristaps 58: struct req {
1.58 schwarze 59: struct query q;
60: char **p; /* array of available manpaths */
61: size_t psz; /* number of available manpaths */
1.121 schwarze 62: int isquery; /* QUERY_STRING used, not PATH_INFO */
1.1 kristaps 63: };
64:
1.131 schwarze 65: enum focus {
66: FOCUS_NONE = 0,
67: FOCUS_QUERY
68: };
69:
1.6 kristaps 70: static void html_print(const char *);
1.10 kristaps 71: static void html_putchar(char);
1.104 schwarze 72: static int http_decode(char *);
1.159 schwarze 73: static void http_encode(const char *p);
1.129 schwarze 74: static void parse_manpath_conf(struct req *);
75: static void parse_path_info(struct req *req, const char *path);
76: static void parse_query_string(struct req *, const char *);
1.72 schwarze 77: static void pg_error_badrequest(const char *);
78: static void pg_error_internal(void);
79: static void pg_index(const struct req *);
80: static void pg_noresult(const struct req *, const char *);
1.149 schwarze 81: static void pg_redirect(const struct req *, const char *);
1.66 schwarze 82: static void pg_search(const struct req *);
1.72 schwarze 83: static void pg_searchres(const struct req *,
84: struct manpage *, size_t);
1.79 schwarze 85: static void pg_show(struct req *, const char *);
1.150 schwarze 86: static void resp_begin_html(int, const char *, const char *);
1.6 kristaps 87: static void resp_begin_http(int, const char *);
1.129 schwarze 88: static void resp_catman(const struct req *, const char *);
1.114 schwarze 89: static void resp_copy(const char *);
1.6 kristaps 90: static void resp_end_html(void);
1.129 schwarze 91: static void resp_format(const struct req *, const char *);
1.131 schwarze 92: static void resp_searchform(const struct req *, enum focus);
1.70 schwarze 93: static void resp_show(const struct req *, const char *);
1.85 schwarze 94: static void set_query_attr(char **, char **);
1.159 schwarze 95: static int validate_arch(const char *);
1.85 schwarze 96: static int validate_filename(const char *);
97: static int validate_manpath(const struct req *, const char *);
98: static int validate_urifrag(const char *);
1.6 kristaps 99:
1.119 schwarze 100: static const char *scriptname = SCRIPT_NAME;
1.1 kristaps 101:
1.70 schwarze 102: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.68 schwarze 103: static const char *const sec_numbers[] = {
104: "0", "1", "2", "3", "3p", "4", "5", "6", "7", "8", "9"
105: };
106: static const char *const sec_names[] = {
107: "All Sections",
108: "1 - General Commands",
109: "2 - System Calls",
1.96 schwarze 110: "3 - Library Functions",
111: "3p - Perl Library",
112: "4 - Device Drivers",
1.68 schwarze 113: "5 - File Formats",
114: "6 - Games",
1.96 schwarze 115: "7 - Miscellaneous Information",
116: "8 - System Manager\'s Manual",
117: "9 - Kernel Developer\'s Manual"
1.68 schwarze 118: };
119: static const int sec_MAX = sizeof(sec_names) / sizeof(char *);
120:
121: static const char *const arch_names[] = {
1.146 schwarze 122: "amd64", "alpha", "armv7", "arm64",
1.137 schwarze 123: "hppa", "i386", "landisk",
1.125 schwarze 124: "loongson", "luna88k", "macppc", "mips64",
1.140 schwarze 125: "octeon", "sgi", "socppc", "sparc64",
1.137 schwarze 126: "amiga", "arc", "armish", "arm32",
127: "atari", "aviion", "beagle", "cats",
128: "hppa64", "hp300",
1.125 schwarze 129: "ia64", "mac68k", "mvme68k", "mvme88k",
130: "mvmeppc", "palm", "pc532", "pegasos",
1.139 schwarze 131: "pmax", "powerpc", "solbourne", "sparc",
1.140 schwarze 132: "sun3", "vax", "wgrisc", "x68k",
133: "zaurus"
1.68 schwarze 134: };
135: static const int arch_MAX = sizeof(arch_names) / sizeof(char *);
136:
1.6 kristaps 137: /*
1.20 kristaps 138: * Print a character, escaping HTML along the way.
139: * This will pass non-ASCII straight to output: be warned!
140: */
1.10 kristaps 141: static void
142: html_putchar(char c)
143: {
144:
145: switch (c) {
1.155 schwarze 146: case '"':
1.141 schwarze 147: printf(""");
1.10 kristaps 148: break;
1.155 schwarze 149: case '&':
1.10 kristaps 150: printf("&");
151: break;
1.155 schwarze 152: case '>':
1.10 kristaps 153: printf(">");
154: break;
1.155 schwarze 155: case '<':
1.10 kristaps 156: printf("<");
157: break;
158: default:
159: putchar((unsigned char)c);
160: break;
161: }
162: }
1.57 schwarze 163:
1.6 kristaps 164: /*
1.20 kristaps 165: * Call through to html_putchar().
166: * Accepts NULL strings.
1.6 kristaps 167: */
1.1 kristaps 168: static void
1.6 kristaps 169: html_print(const char *p)
1.1 kristaps 170: {
1.104 schwarze 171:
1.6 kristaps 172: if (NULL == p)
173: return;
1.1 kristaps 174: while ('\0' != *p)
1.10 kristaps 175: html_putchar(*p++);
1.1 kristaps 176: }
177:
178: /*
1.83 schwarze 179: * Transfer the responsibility for the allocated string *val
180: * to the query structure.
1.1 kristaps 181: */
182: static void
1.83 schwarze 183: set_query_attr(char **attr, char **val)
1.1 kristaps 184: {
185:
1.83 schwarze 186: free(*attr);
187: if (**val == '\0') {
188: *attr = NULL;
189: free(*val);
190: } else
191: *attr = *val;
192: *val = NULL;
193: }
194:
195: /*
196: * Parse the QUERY_STRING for key-value pairs
197: * and store the values into the query structure.
198: */
199: static void
1.129 schwarze 200: parse_query_string(struct req *req, const char *qs)
1.83 schwarze 201: {
202: char *key, *val;
203: size_t keysz, valsz;
204:
1.121 schwarze 205: req->isquery = 1;
1.83 schwarze 206: req->q.manpath = NULL;
207: req->q.arch = NULL;
208: req->q.sec = NULL;
1.85 schwarze 209: req->q.query = NULL;
1.83 schwarze 210: req->q.equal = 1;
211:
212: key = val = NULL;
213: while (*qs != '\0') {
1.24 kristaps 214:
1.83 schwarze 215: /* Parse one key. */
216:
217: keysz = strcspn(qs, "=;&");
218: key = mandoc_strndup(qs, keysz);
219: qs += keysz;
220: if (*qs != '=')
221: goto next;
222:
223: /* Parse one value. */
224:
225: valsz = strcspn(++qs, ";&");
226: val = mandoc_strndup(qs, valsz);
227: qs += valsz;
228:
229: /* Decode and catch encoding errors. */
1.1 kristaps 230:
1.83 schwarze 231: if ( ! (http_decode(key) && http_decode(val)))
232: goto next;
1.1 kristaps 233:
1.83 schwarze 234: /* Handle key-value pairs. */
1.1 kristaps 235:
1.83 schwarze 236: if ( ! strcmp(key, "query"))
1.85 schwarze 237: set_query_attr(&req->q.query, &val);
1.1 kristaps 238:
1.83 schwarze 239: else if ( ! strcmp(key, "apropos"))
240: req->q.equal = !strcmp(val, "0");
241:
242: else if ( ! strcmp(key, "manpath")) {
1.73 schwarze 243: #ifdef COMPAT_OLDURI
1.83 schwarze 244: if ( ! strncmp(val, "OpenBSD ", 8)) {
1.73 schwarze 245: val[7] = '-';
246: if ('C' == val[8])
247: val[8] = 'c';
248: }
249: #endif
1.83 schwarze 250: set_query_attr(&req->q.manpath, &val);
251: }
252:
253: else if ( ! (strcmp(key, "sec")
1.73 schwarze 254: #ifdef COMPAT_OLDURI
1.83 schwarze 255: && strcmp(key, "sektion")
1.73 schwarze 256: #endif
1.83 schwarze 257: )) {
258: if ( ! strcmp(val, "0"))
259: *val = '\0';
260: set_query_attr(&req->q.sec, &val);
1.65 schwarze 261: }
1.83 schwarze 262:
263: else if ( ! strcmp(key, "arch")) {
264: if ( ! strcmp(val, "default"))
265: *val = '\0';
266: set_query_attr(&req->q.arch, &val);
267: }
268:
269: /*
270: * The key must be freed in any case.
271: * The val may have been handed over to the query
272: * structure, in which case it is now NULL.
273: */
274: next:
275: free(key);
276: key = NULL;
277: free(val);
278: val = NULL;
279:
280: if (*qs != '\0')
281: qs++;
1.24 kristaps 282: }
1.1 kristaps 283: }
284:
285: /*
1.6 kristaps 286: * HTTP-decode a string. The standard explanation is that this turns
287: * "%4e+foo" into "n foo" in the regular way. This is done in-place
288: * over the allocated string.
1.1 kristaps 289: */
290: static int
1.24 kristaps 291: http_decode(char *p)
1.1 kristaps 292: {
293: char hex[3];
1.63 schwarze 294: char *q;
1.1 kristaps 295: int c;
296:
297: hex[2] = '\0';
298:
1.63 schwarze 299: q = p;
300: for ( ; '\0' != *p; p++, q++) {
1.1 kristaps 301: if ('%' == *p) {
302: if ('\0' == (hex[0] = *(p + 1)))
1.109 schwarze 303: return 0;
1.1 kristaps 304: if ('\0' == (hex[1] = *(p + 2)))
1.109 schwarze 305: return 0;
1.1 kristaps 306: if (1 != sscanf(hex, "%x", &c))
1.109 schwarze 307: return 0;
1.1 kristaps 308: if ('\0' == c)
1.109 schwarze 309: return 0;
1.1 kristaps 310:
1.63 schwarze 311: *q = (char)c;
312: p += 2;
1.1 kristaps 313: } else
1.63 schwarze 314: *q = '+' == *p ? ' ' : *p;
1.1 kristaps 315: }
316:
1.63 schwarze 317: *q = '\0';
1.109 schwarze 318: return 1;
1.1 kristaps 319: }
320:
1.6 kristaps 321: static void
1.159 schwarze 322: http_encode(const char *p)
323: {
324: for (; *p != '\0'; p++) {
325: if (isalnum((unsigned char)*p) == 0 &&
326: strchr("-._~", *p) == NULL)
327: printf("%%%02.2X", (unsigned char)*p);
328: else
329: putchar(*p);
330: }
331: }
332:
333: static void
1.6 kristaps 334: resp_begin_http(int code, const char *msg)
335: {
336:
337: if (200 != code)
1.62 schwarze 338: printf("Status: %d %s\r\n", code, msg);
1.6 kristaps 339:
1.62 schwarze 340: printf("Content-Type: text/html; charset=utf-8\r\n"
341: "Cache-Control: no-cache\r\n"
342: "Pragma: no-cache\r\n"
343: "\r\n");
1.6 kristaps 344:
345: fflush(stdout);
346: }
347:
348: static void
1.114 schwarze 349: resp_copy(const char *filename)
350: {
351: char buf[4096];
352: ssize_t sz;
353: int fd;
354:
355: if ((fd = open(filename, O_RDONLY)) != -1) {
356: fflush(stdout);
357: while ((sz = read(fd, buf, sizeof(buf))) > 0)
358: write(STDOUT_FILENO, buf, sz);
1.138 schwarze 359: close(fd);
1.114 schwarze 360: }
361: }
362:
363: static void
1.150 schwarze 364: resp_begin_html(int code, const char *msg, const char *file)
1.6 kristaps 365: {
1.150 schwarze 366: char *cp;
1.6 kristaps 367:
368: resp_begin_http(code, msg);
369:
1.98 kristaps 370: printf("<!DOCTYPE html>\n"
1.126 schwarze 371: "<html>\n"
372: "<head>\n"
1.143 schwarze 373: " <meta charset=\"UTF-8\"/>\n"
1.157 schwarze 374: " <meta name=\"viewport\""
375: " content=\"width=device-width, initial-scale=1.0\">\n"
1.143 schwarze 376: " <link rel=\"stylesheet\" href=\"%s/mandoc.css\""
1.126 schwarze 377: " type=\"text/css\" media=\"all\">\n"
1.150 schwarze 378: " <title>",
379: CSS_DIR);
380: if (file != NULL) {
381: if ((cp = strrchr(file, '/')) != NULL)
382: file = cp + 1;
383: if ((cp = strrchr(file, '.')) != NULL) {
384: printf("%.*s(%s) - ", (int)(cp - file), file, cp + 1);
385: } else
386: printf("%s - ", file);
387: }
388: printf("%s</title>\n"
1.126 schwarze 389: "</head>\n"
1.142 schwarze 390: "<body>\n",
1.150 schwarze 391: CUSTOMIZE_TITLE);
1.114 schwarze 392:
393: resp_copy(MAN_DIR "/header.html");
1.6 kristaps 394: }
395:
396: static void
397: resp_end_html(void)
398: {
399:
1.114 schwarze 400: resp_copy(MAN_DIR "/footer.html");
401:
1.126 schwarze 402: puts("</body>\n"
403: "</html>");
1.6 kristaps 404: }
405:
406: static void
1.131 schwarze 407: resp_searchform(const struct req *req, enum focus focus)
1.6 kristaps 408: {
1.27 kristaps 409: int i;
1.13 kristaps 410:
1.142 schwarze 411: printf("<form action=\"/%s\" method=\"get\">\n"
1.143 schwarze 412: " <fieldset>\n"
413: " <legend>Manual Page Search Parameters</legend>\n",
1.58 schwarze 414: scriptname);
1.68 schwarze 415:
416: /* Write query input box. */
417:
1.158 schwarze 418: printf(" <input type=\"search\" name=\"query\" value=\"");
1.131 schwarze 419: if (req->q.query != NULL)
1.85 schwarze 420: html_print(req->q.query);
1.131 schwarze 421: printf( "\" size=\"40\"");
422: if (focus == FOCUS_QUERY)
423: printf(" autofocus");
424: puts(">");
1.68 schwarze 425:
1.132 schwarze 426: /* Write submission buttons. */
1.68 schwarze 427:
1.143 schwarze 428: printf( " <button type=\"submit\" name=\"apropos\" value=\"0\">"
1.132 schwarze 429: "man</button>\n"
1.143 schwarze 430: " <button type=\"submit\" name=\"apropos\" value=\"1\">"
431: "apropos</button>\n"
432: " <br/>\n");
1.68 schwarze 433:
434: /* Write section selector. */
435:
1.143 schwarze 436: puts(" <select name=\"sec\">");
1.68 schwarze 437: for (i = 0; i < sec_MAX; i++) {
1.143 schwarze 438: printf(" <option value=\"%s\"", sec_numbers[i]);
1.68 schwarze 439: if (NULL != req->q.sec &&
440: 0 == strcmp(sec_numbers[i], req->q.sec))
1.126 schwarze 441: printf(" selected=\"selected\"");
442: printf(">%s</option>\n", sec_names[i]);
1.68 schwarze 443: }
1.143 schwarze 444: puts(" </select>");
1.68 schwarze 445:
446: /* Write architecture selector. */
447:
1.143 schwarze 448: printf( " <select name=\"arch\">\n"
449: " <option value=\"default\"");
1.81 schwarze 450: if (NULL == req->q.arch)
1.126 schwarze 451: printf(" selected=\"selected\"");
452: puts(">All Architectures</option>");
1.68 schwarze 453: for (i = 0; i < arch_MAX; i++) {
1.158 schwarze 454: printf(" <option");
1.68 schwarze 455: if (NULL != req->q.arch &&
456: 0 == strcmp(arch_names[i], req->q.arch))
1.126 schwarze 457: printf(" selected=\"selected\"");
458: printf(">%s</option>\n", arch_names[i]);
1.68 schwarze 459: }
1.143 schwarze 460: puts(" </select>");
1.68 schwarze 461:
462: /* Write manpath selector. */
463:
1.27 kristaps 464: if (req->psz > 1) {
1.143 schwarze 465: puts(" <select name=\"manpath\">");
1.27 kristaps 466: for (i = 0; i < (int)req->psz; i++) {
1.158 schwarze 467: printf(" <option");
1.102 schwarze 468: if (strcmp(req->q.manpath, req->p[i]) == 0)
1.158 schwarze 469: printf(" selected=\"selected\"");
470: printf(">");
1.52 schwarze 471: html_print(req->p[i]);
1.126 schwarze 472: puts("</option>");
1.27 kristaps 473: }
1.143 schwarze 474: puts(" </select>");
1.27 kristaps 475: }
1.68 schwarze 476:
1.143 schwarze 477: puts(" </fieldset>\n"
1.142 schwarze 478: "</form>");
1.6 kristaps 479: }
480:
1.76 schwarze 481: static int
1.80 schwarze 482: validate_urifrag(const char *frag)
483: {
484:
485: while ('\0' != *frag) {
486: if ( ! (isalnum((unsigned char)*frag) ||
487: '-' == *frag || '.' == *frag ||
488: '/' == *frag || '_' == *frag))
1.109 schwarze 489: return 0;
1.80 schwarze 490: frag++;
491: }
1.109 schwarze 492: return 1;
1.80 schwarze 493: }
494:
495: static int
1.77 schwarze 496: validate_manpath(const struct req *req, const char* manpath)
497: {
498: size_t i;
499:
500: for (i = 0; i < req->psz; i++)
501: if ( ! strcmp(manpath, req->p[i]))
1.109 schwarze 502: return 1;
1.77 schwarze 503:
1.109 schwarze 504: return 0;
1.77 schwarze 505: }
506:
507: static int
1.159 schwarze 508: validate_arch(const char *arch)
509: {
510: int i;
511:
512: for (i = 0; i < arch_MAX; i++)
513: if (strcmp(arch, arch_names[i]) == 0)
514: return 1;
515:
516: return 0;
517: }
518:
519: static int
1.76 schwarze 520: validate_filename(const char *file)
521: {
522:
523: if ('.' == file[0] && '/' == file[1])
524: file += 2;
525:
1.109 schwarze 526: return ! (strstr(file, "../") || strstr(file, "/..") ||
527: (strncmp(file, "man", 3) && strncmp(file, "cat", 3)));
1.76 schwarze 528: }
529:
1.6 kristaps 530: static void
1.72 schwarze 531: pg_index(const struct req *req)
1.6 kristaps 532: {
533:
1.150 schwarze 534: resp_begin_html(200, NULL, NULL);
1.131 schwarze 535: resp_searchform(req, FOCUS_QUERY);
1.126 schwarze 536: printf("<p>\n"
1.86 schwarze 537: "This web interface is documented in the\n"
1.144 schwarze 538: "<a class=\"Xr\" href=\"/%s%sman.cgi.8\">man.cgi(8)</a>\n"
1.86 schwarze 539: "manual, and the\n"
1.144 schwarze 540: "<a class=\"Xr\" href=\"/%s%sapropos.1\">apropos(1)</a>\n"
1.69 schwarze 541: "manual explains the query syntax.\n"
1.126 schwarze 542: "</p>\n",
1.119 schwarze 543: scriptname, *scriptname == '\0' ? "" : "/",
544: scriptname, *scriptname == '\0' ? "" : "/");
1.6 kristaps 545: resp_end_html();
546: }
547:
548: static void
1.72 schwarze 549: pg_noresult(const struct req *req, const char *msg)
1.59 schwarze 550: {
1.150 schwarze 551: resp_begin_html(200, NULL, NULL);
1.131 schwarze 552: resp_searchform(req, FOCUS_QUERY);
1.126 schwarze 553: puts("<p>");
1.59 schwarze 554: puts(msg);
1.126 schwarze 555: puts("</p>");
1.59 schwarze 556: resp_end_html();
557: }
558:
559: static void
1.72 schwarze 560: pg_error_badrequest(const char *msg)
1.9 kristaps 561: {
562:
1.150 schwarze 563: resp_begin_html(400, "Bad Request", NULL);
1.126 schwarze 564: puts("<h1>Bad Request</h1>\n"
565: "<p>\n");
1.59 schwarze 566: puts(msg);
567: printf("Try again from the\n"
1.126 schwarze 568: "<a href=\"/%s\">main page</a>.\n"
569: "</p>", scriptname);
1.9 kristaps 570: resp_end_html();
571: }
572:
573: static void
1.72 schwarze 574: pg_error_internal(void)
1.7 kristaps 575: {
1.150 schwarze 576: resp_begin_html(500, "Internal Server Error", NULL);
1.126 schwarze 577: puts("<p>Internal Server Error</p>");
1.7 kristaps 578: resp_end_html();
579: }
580:
581: static void
1.149 schwarze 582: pg_redirect(const struct req *req, const char *name)
583: {
1.153 schwarze 584: printf("Status: 303 See Other\r\n"
585: "Location: /");
1.149 schwarze 586: if (*scriptname != '\0')
587: printf("%s/", scriptname);
588: if (strcmp(req->q.manpath, req->p[0]))
589: printf("%s/", req->q.manpath);
590: if (req->q.arch != NULL)
591: printf("%s/", req->q.arch);
1.159 schwarze 592: http_encode(name);
593: if (req->q.sec != NULL) {
594: putchar('.');
595: http_encode(req->q.sec);
596: }
1.149 schwarze 597: printf("\r\nContent-Type: text/html; charset=utf-8\r\n\r\n");
598: }
599:
600: static void
1.72 schwarze 601: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1 kristaps 602: {
1.81 schwarze 603: char *arch, *archend;
1.120 schwarze 604: const char *sec;
605: size_t i, iuse;
1.81 schwarze 606: int archprio, archpriouse;
1.70 schwarze 607: int prio, priouse;
1.19 kristaps 608:
1.76 schwarze 609: for (i = 0; i < sz; i++) {
610: if (validate_filename(r[i].file))
611: continue;
1.128 schwarze 612: warnx("invalid filename %s in %s database",
1.76 schwarze 613: r[i].file, req->q.manpath);
614: pg_error_internal();
615: return;
616: }
617:
1.121 schwarze 618: if (req->isquery && sz == 1) {
1.6 kristaps 619: /*
620: * If we have just one result, then jump there now
621: * without any delay.
622: */
1.153 schwarze 623: printf("Status: 303 See Other\r\n"
624: "Location: /");
625: if (*scriptname != '\0')
626: printf("%s/", scriptname);
627: if (strcmp(req->q.manpath, req->p[0]))
628: printf("%s/", req->q.manpath);
629: printf("%s\r\n"
630: "Content-Type: text/html; charset=utf-8\r\n\r\n",
631: r[0].file);
1.6 kristaps 632: return;
633: }
634:
1.70 schwarze 635: /*
636: * In man(1) mode, show one of the pages
637: * even if more than one is found.
638: */
639:
1.150 schwarze 640: iuse = 0;
1.123 schwarze 641: if (req->q.equal || sz == 1) {
1.120 schwarze 642: priouse = 20;
1.81 schwarze 643: archpriouse = 3;
1.70 schwarze 644: for (i = 0; i < sz; i++) {
1.120 schwarze 645: sec = r[i].file;
646: sec += strcspn(sec, "123456789");
647: if (sec[0] == '\0')
1.70 schwarze 648: continue;
1.120 schwarze 649: prio = sec_prios[sec[0] - '1'];
650: if (sec[1] != '/')
651: prio += 10;
652: if (req->q.arch == NULL) {
1.81 schwarze 653: archprio =
1.120 schwarze 654: ((arch = strchr(sec + 1, '/'))
655: == NULL) ? 3 :
656: ((archend = strchr(arch + 1, '/'))
657: == NULL) ? 0 :
1.81 schwarze 658: strncmp(arch, "amd64/",
659: archend - arch) ? 2 : 1;
660: if (archprio < archpriouse) {
661: archpriouse = archprio;
662: priouse = prio;
663: iuse = i;
664: continue;
665: }
666: if (archprio > archpriouse)
667: continue;
668: }
1.70 schwarze 669: if (prio >= priouse)
670: continue;
671: priouse = prio;
672: iuse = i;
673: }
1.150 schwarze 674: resp_begin_html(200, NULL, r[iuse].file);
675: } else
676: resp_begin_html(200, NULL, NULL);
677:
678: resp_searchform(req,
679: req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
680:
681: if (sz > 1) {
682: puts("<table class=\"results\">");
683: for (i = 0; i < sz; i++) {
684: printf(" <tr>\n"
685: " <td>"
1.151 schwarze 686: "<a class=\"Xr\" href=\"/");
687: if (*scriptname != '\0')
688: printf("%s/", scriptname);
689: if (strcmp(req->q.manpath, req->p[0]))
690: printf("%s/", req->q.manpath);
691: printf("%s\">", r[i].file);
1.150 schwarze 692: html_print(r[i].names);
693: printf("</a></td>\n"
694: " <td><span class=\"Nd\">");
695: html_print(r[i].output);
696: puts("</span></td>\n"
697: " </tr>");
698: }
699: puts("</table>");
700: }
701:
702: if (req->q.equal || sz == 1) {
703: puts("<hr>");
1.70 schwarze 704: resp_show(req, r[iuse].file);
705: }
706:
1.6 kristaps 707: resp_end_html();
708: }
709:
1.1 kristaps 710: static void
1.129 schwarze 711: resp_catman(const struct req *req, const char *file)
1.9 kristaps 712: {
1.10 kristaps 713: FILE *f;
1.115 schwarze 714: char *p;
715: size_t sz;
716: ssize_t len;
1.10 kristaps 717: int i;
718: int italic, bold;
1.9 kristaps 719:
1.115 schwarze 720: if ((f = fopen(file, "r")) == NULL) {
1.126 schwarze 721: puts("<p>You specified an invalid manual file.</p>");
1.9 kristaps 722: return;
723: }
724:
1.126 schwarze 725: puts("<div class=\"catman\">\n"
726: "<pre>");
1.10 kristaps 727:
1.115 schwarze 728: p = NULL;
729: sz = 0;
730:
731: while ((len = getline(&p, &sz, f)) != -1) {
1.10 kristaps 732: bold = italic = 0;
1.115 schwarze 733: for (i = 0; i < len - 1; i++) {
1.104 schwarze 734: /*
1.10 kristaps 735: * This means that the catpage is out of state.
736: * Ignore it and keep going (although the
737: * catpage is bogus).
738: */
739:
740: if ('\b' == p[i] || '\n' == p[i])
741: continue;
742:
743: /*
744: * Print a regular character.
745: * Close out any bold/italic scopes.
746: * If we're in back-space mode, make sure we'll
747: * have something to enter when we backspace.
748: */
749:
750: if ('\b' != p[i + 1]) {
751: if (italic)
1.126 schwarze 752: printf("</i>");
1.10 kristaps 753: if (bold)
1.126 schwarze 754: printf("</b>");
1.10 kristaps 755: italic = bold = 0;
756: html_putchar(p[i]);
757: continue;
1.115 schwarze 758: } else if (i + 2 >= len)
1.10 kristaps 759: continue;
760:
761: /* Italic mode. */
762:
763: if ('_' == p[i]) {
764: if (bold)
1.126 schwarze 765: printf("</b>");
1.10 kristaps 766: if ( ! italic)
1.126 schwarze 767: printf("<i>");
1.10 kristaps 768: bold = 0;
769: italic = 1;
770: i += 2;
771: html_putchar(p[i]);
772: continue;
773: }
774:
1.104 schwarze 775: /*
1.10 kristaps 776: * Handle funny behaviour troff-isms.
777: * These grok'd from the original man2html.c.
778: */
779:
780: if (('+' == p[i] && 'o' == p[i + 2]) ||
781: ('o' == p[i] && '+' == p[i + 2]) ||
782: ('|' == p[i] && '=' == p[i + 2]) ||
783: ('=' == p[i] && '|' == p[i + 2]) ||
784: ('*' == p[i] && '=' == p[i + 2]) ||
785: ('=' == p[i] && '*' == p[i + 2]) ||
786: ('*' == p[i] && '|' == p[i + 2]) ||
787: ('|' == p[i] && '*' == p[i + 2])) {
788: if (italic)
1.126 schwarze 789: printf("</i>");
1.10 kristaps 790: if (bold)
1.126 schwarze 791: printf("</b>");
1.10 kristaps 792: italic = bold = 0;
793: putchar('*');
794: i += 2;
795: continue;
796: } else if (('|' == p[i] && '-' == p[i + 2]) ||
797: ('-' == p[i] && '|' == p[i + 1]) ||
798: ('+' == p[i] && '-' == p[i + 1]) ||
799: ('-' == p[i] && '+' == p[i + 1]) ||
800: ('+' == p[i] && '|' == p[i + 1]) ||
801: ('|' == p[i] && '+' == p[i + 1])) {
802: if (italic)
1.126 schwarze 803: printf("</i>");
1.10 kristaps 804: if (bold)
1.126 schwarze 805: printf("</b>");
1.10 kristaps 806: italic = bold = 0;
807: putchar('+');
808: i += 2;
809: continue;
810: }
811:
812: /* Bold mode. */
1.104 schwarze 813:
1.10 kristaps 814: if (italic)
1.126 schwarze 815: printf("</i>");
1.10 kristaps 816: if ( ! bold)
1.126 schwarze 817: printf("<b>");
1.10 kristaps 818: bold = 1;
819: italic = 0;
820: i += 2;
821: html_putchar(p[i]);
822: }
823:
1.104 schwarze 824: /*
1.10 kristaps 825: * Clean up the last character.
1.104 schwarze 826: * We can get to a newline; don't print that.
1.10 kristaps 827: */
1.9 kristaps 828:
1.10 kristaps 829: if (italic)
1.126 schwarze 830: printf("</i>");
1.10 kristaps 831: if (bold)
1.126 schwarze 832: printf("</b>");
1.9 kristaps 833:
1.115 schwarze 834: if (i == len - 1 && p[i] != '\n')
1.10 kristaps 835: html_putchar(p[i]);
1.9 kristaps 836:
1.10 kristaps 837: putchar('\n');
838: }
1.115 schwarze 839: free(p);
1.10 kristaps 840:
1.126 schwarze 841: puts("</pre>\n"
842: "</div>");
1.10 kristaps 843:
844: fclose(f);
1.9 kristaps 845: }
846:
847: static void
1.129 schwarze 848: resp_format(const struct req *req, const char *file)
1.7 kristaps 849: {
1.106 schwarze 850: struct manoutput conf;
1.8 kristaps 851: struct mparse *mp;
1.107 schwarze 852: struct roff_man *man;
1.8 kristaps 853: void *vp;
1.90 schwarze 854: int fd;
855: int usepath;
1.7 kristaps 856:
1.8 kristaps 857: if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.126 schwarze 858: puts("<p>You specified an invalid manual file.</p>");
1.7 kristaps 859: return;
860: }
861:
1.110 schwarze 862: mchars_alloc();
1.136 schwarze 863: mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1,
1.163 ! schwarze 864: MANDOC_OS_OTHER, req->q.manpath);
1.103 schwarze 865: mparse_readfd(mp, fd, file);
1.8 kristaps 866: close(fd);
1.7 kristaps 867:
1.106 schwarze 868: memset(&conf, 0, sizeof(conf));
869: conf.fragment = 1;
1.145 schwarze 870: conf.style = mandoc_strdup(CSS_DIR "/mandoc.css");
1.160 schwarze 871: conf.toc = 1;
1.90 schwarze 872: usepath = strcmp(req->q.manpath, req->p[0]);
1.152 schwarze 873: mandoc_asprintf(&conf.man, "/%s%s%s%s%%N.%%S",
874: scriptname, *scriptname == '\0' ? "" : "/",
1.122 schwarze 875: usepath ? req->q.manpath : "", usepath ? "/" : "");
1.10 kristaps 876:
1.108 schwarze 877: mparse_result(mp, &man, NULL);
878: if (man == NULL) {
1.128 schwarze 879: warnx("fatal mandoc error: %s/%s", req->q.manpath, file);
1.72 schwarze 880: pg_error_internal();
1.32 kristaps 881: mparse_free(mp);
1.110 schwarze 882: mchars_free();
1.32 kristaps 883: return;
884: }
885:
1.110 schwarze 886: vp = html_alloc(&conf);
1.7 kristaps 887:
1.111 schwarze 888: if (man->macroset == MACROSET_MDOC) {
889: mdoc_validate(man);
1.108 schwarze 890: html_mdoc(vp, man);
1.112 schwarze 891: } else {
892: man_validate(man);
1.8 kristaps 893: html_man(vp, man);
1.112 schwarze 894: }
1.32 kristaps 895:
1.8 kristaps 896: html_free(vp);
897: mparse_free(mp);
1.110 schwarze 898: mchars_free();
1.106 schwarze 899: free(conf.man);
1.145 schwarze 900: free(conf.style);
1.7 kristaps 901: }
902:
903: static void
1.70 schwarze 904: resp_show(const struct req *req, const char *file)
905: {
1.76 schwarze 906:
907: if ('.' == file[0] && '/' == file[1])
1.71 schwarze 908: file += 2;
1.70 schwarze 909:
910: if ('c' == *file)
1.129 schwarze 911: resp_catman(req, file);
1.70 schwarze 912: else
1.129 schwarze 913: resp_format(req, file);
1.70 schwarze 914: }
915:
916: static void
1.84 schwarze 917: pg_show(struct req *req, const char *fullpath)
1.1 kristaps 918: {
1.84 schwarze 919: char *manpath;
920: const char *file;
1.25 kristaps 921:
1.84 schwarze 922: if ((file = strchr(fullpath, '/')) == NULL) {
1.72 schwarze 923: pg_error_badrequest(
1.59 schwarze 924: "You did not specify a page to show.");
1.25 kristaps 925: return;
1.104 schwarze 926: }
1.84 schwarze 927: manpath = mandoc_strndup(fullpath, file - fullpath);
928: file++;
1.6 kristaps 929:
1.84 schwarze 930: if ( ! validate_manpath(req, manpath)) {
1.77 schwarze 931: pg_error_badrequest(
932: "You specified an invalid manpath.");
1.84 schwarze 933: free(manpath);
1.77 schwarze 934: return;
935: }
936:
1.24 kristaps 937: /*
1.58 schwarze 938: * Begin by chdir()ing into the manpath.
1.24 kristaps 939: * This way we can pick up the database files, which are
940: * relative to the manpath root.
941: */
942:
1.84 schwarze 943: if (chdir(manpath) == -1) {
1.128 schwarze 944: warn("chdir %s", manpath);
1.77 schwarze 945: pg_error_internal();
1.84 schwarze 946: free(manpath);
1.76 schwarze 947: return;
948: }
1.134 schwarze 949: free(manpath);
1.84 schwarze 950:
951: if ( ! validate_filename(file)) {
1.76 schwarze 952: pg_error_badrequest(
953: "You specified an invalid manual file.");
1.24 kristaps 954: return;
955: }
1.79 schwarze 956:
1.150 schwarze 957: resp_begin_html(200, NULL, file);
1.131 schwarze 958: resp_searchform(req, FOCUS_NONE);
1.84 schwarze 959: resp_show(req, file);
1.70 schwarze 960: resp_end_html();
1.6 kristaps 961: }
962:
963: static void
1.66 schwarze 964: pg_search(const struct req *req)
1.6 kristaps 965: {
1.52 schwarze 966: struct mansearch search;
967: struct manpaths paths;
968: struct manpage *res;
1.97 schwarze 969: char **argv;
970: char *query, *rp, *wp;
1.52 schwarze 971: size_t ressz;
1.97 schwarze 972: int argc;
1.6 kristaps 973:
974: /*
1.24 kristaps 975: * Begin by chdir()ing into the root of the manpath.
976: * This way we can pick up the database files, which are
977: * relative to the manpath root.
978: */
979:
1.128 schwarze 980: if (chdir(req->q.manpath) == -1) {
981: warn("chdir %s", req->q.manpath);
1.77 schwarze 982: pg_error_internal();
1.24 kristaps 983: return;
984: }
985:
1.52 schwarze 986: search.arch = req->q.arch;
987: search.sec = req->q.sec;
1.94 schwarze 988: search.outkey = "Nd";
989: search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.101 schwarze 990: search.firstmatch = 1;
1.52 schwarze 991:
992: paths.sz = 1;
993: paths.paths = mandoc_malloc(sizeof(char *));
994: paths.paths[0] = mandoc_strdup(".");
1.24 kristaps 995:
996: /*
1.97 schwarze 997: * Break apart at spaces with backslash-escaping.
1.6 kristaps 998: */
999:
1.97 schwarze 1000: argc = 0;
1001: argv = NULL;
1002: rp = query = mandoc_strdup(req->q.query);
1003: for (;;) {
1004: while (isspace((unsigned char)*rp))
1005: rp++;
1006: if (*rp == '\0')
1007: break;
1008: argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
1009: argv[argc++] = wp = rp;
1010: for (;;) {
1011: if (isspace((unsigned char)*rp)) {
1012: *wp = '\0';
1013: rp++;
1014: break;
1015: }
1016: if (rp[0] == '\\' && rp[1] != '\0')
1017: rp++;
1018: if (wp != rp)
1019: *wp = *rp;
1020: if (*rp == '\0')
1021: break;
1022: wp++;
1023: rp++;
1024: }
1.6 kristaps 1025: }
1026:
1.149 schwarze 1027: res = NULL;
1028: ressz = 0;
1029: if (req->isquery && req->q.equal && argc == 1)
1030: pg_redirect(req, argv[0]);
1031: else if (mansearch(&search, &paths, argc, argv, &res, &ressz) == 0)
1.72 schwarze 1032: pg_noresult(req, "You entered an invalid query.");
1.149 schwarze 1033: else if (ressz == 0)
1.72 schwarze 1034: pg_noresult(req, "No results found.");
1.59 schwarze 1035: else
1.72 schwarze 1036: pg_searchres(req, res, ressz);
1.6 kristaps 1037:
1.97 schwarze 1038: free(query);
1039: mansearch_free(res, ressz);
1.52 schwarze 1040: free(paths.paths[0]);
1041: free(paths.paths);
1.1 kristaps 1042: }
1043:
1044: int
1045: main(void)
1046: {
1.66 schwarze 1047: struct req req;
1.95 schwarze 1048: struct itimerval itimer;
1.66 schwarze 1049: const char *path;
1.83 schwarze 1050: const char *querystring;
1.1 kristaps 1051: int i;
1.148 schwarze 1052:
1053: #if HAVE_PLEDGE
1054: /*
1055: * The "rpath" pledge could be revoked after mparse_readfd()
1056: * if the file desciptor to "/footer.html" would be opened
1057: * up front, but it's probably not worth the complication
1058: * of the code it would cause: it would require scattering
1059: * pledge() calls in multiple low-level resp_*() functions.
1060: */
1061:
1062: if (pledge("stdio rpath", NULL) == -1) {
1063: warn("pledge");
1064: pg_error_internal();
1065: return EXIT_FAILURE;
1066: }
1067: #endif
1.95 schwarze 1068:
1069: /* Poor man's ReDoS mitigation. */
1070:
1.99 schwarze 1071: itimer.it_value.tv_sec = 2;
1.95 schwarze 1072: itimer.it_value.tv_usec = 0;
1.99 schwarze 1073: itimer.it_interval.tv_sec = 2;
1.95 schwarze 1074: itimer.it_interval.tv_usec = 0;
1075: if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1.128 schwarze 1076: warn("setitimer");
1.80 schwarze 1077: pg_error_internal();
1.109 schwarze 1078: return EXIT_FAILURE;
1.80 schwarze 1079: }
1080:
1.24 kristaps 1081: /*
1.67 schwarze 1082: * First we change directory into the MAN_DIR so that
1.24 kristaps 1083: * subsequent scanning for manpath directories is rooted
1084: * relative to the same position.
1085: */
1086:
1.128 schwarze 1087: if (chdir(MAN_DIR) == -1) {
1088: warn("MAN_DIR: %s", MAN_DIR);
1.72 schwarze 1089: pg_error_internal();
1.109 schwarze 1090: return EXIT_FAILURE;
1.104 schwarze 1091: }
1.24 kristaps 1092:
1093: memset(&req, 0, sizeof(struct req));
1.118 schwarze 1094: req.q.equal = 1;
1.129 schwarze 1095: parse_manpath_conf(&req);
1.1 kristaps 1096:
1.117 schwarze 1097: /* Parse the path info and the query string. */
1.1 kristaps 1098:
1.117 schwarze 1099: if ((path = getenv("PATH_INFO")) == NULL)
1100: path = "";
1101: else if (*path == '/')
1102: path++;
1103:
1.124 schwarze 1104: if (*path != '\0') {
1.129 schwarze 1105: parse_path_info(&req, path);
1.154 schwarze 1106: if (req.q.manpath == NULL || req.q.sec == NULL ||
1107: *req.q.query == '\0' || access(path, F_OK) == -1)
1.124 schwarze 1108: path = "";
1.117 schwarze 1109: } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.129 schwarze 1110: parse_query_string(&req, querystring);
1.77 schwarze 1111:
1.117 schwarze 1112: /* Validate parsed data and add defaults. */
1113:
1.102 schwarze 1114: if (req.q.manpath == NULL)
1115: req.q.manpath = mandoc_strdup(req.p[0]);
1116: else if ( ! validate_manpath(&req, req.q.manpath)) {
1.77 schwarze 1117: pg_error_badrequest(
1118: "You specified an invalid manpath.");
1.109 schwarze 1119: return EXIT_FAILURE;
1.77 schwarze 1120: }
1.1 kristaps 1121:
1.159 schwarze 1122: if (req.q.arch != NULL && validate_arch(req.q.arch) == 0) {
1.80 schwarze 1123: pg_error_badrequest(
1124: "You specified an invalid architecture.");
1.109 schwarze 1125: return EXIT_FAILURE;
1.80 schwarze 1126: }
1127:
1.66 schwarze 1128: /* Dispatch to the three different pages. */
1.1 kristaps 1129:
1.66 schwarze 1130: if ('\0' != *path)
1131: pg_show(&req, path);
1.85 schwarze 1132: else if (NULL != req.q.query)
1.66 schwarze 1133: pg_search(&req);
1134: else
1.72 schwarze 1135: pg_index(&req);
1.1 kristaps 1136:
1.83 schwarze 1137: free(req.q.manpath);
1138: free(req.q.arch);
1139: free(req.q.sec);
1.85 schwarze 1140: free(req.q.query);
1.52 schwarze 1141: for (i = 0; i < (int)req.psz; i++)
1142: free(req.p[i]);
1.24 kristaps 1143: free(req.p);
1.109 schwarze 1144: return EXIT_SUCCESS;
1.117 schwarze 1145: }
1146:
1147: /*
1.161 schwarze 1148: * Translate PATH_INFO to a query.
1.117 schwarze 1149: */
1150: static void
1.129 schwarze 1151: parse_path_info(struct req *req, const char *path)
1.117 schwarze 1152: {
1.161 schwarze 1153: const char *name, *sec, *end;
1.117 schwarze 1154:
1.121 schwarze 1155: req->isquery = 0;
1.117 schwarze 1156: req->q.equal = 1;
1.161 schwarze 1157: req->q.manpath = NULL;
1.135 schwarze 1158: req->q.arch = NULL;
1.117 schwarze 1159:
1160: /* Mandatory manual page name. */
1.161 schwarze 1161: if ((name = strrchr(path, '/')) == NULL)
1162: name = path;
1163: else
1164: name++;
1.117 schwarze 1165:
1166: /* Optional trailing section. */
1.161 schwarze 1167: sec = strrchr(name, '.');
1168: if (sec != NULL && isdigit((unsigned char)*++sec)) {
1169: req->q.query = mandoc_strndup(name, sec - name - 1);
1170: req->q.sec = mandoc_strdup(sec);
1171: } else {
1172: req->q.query = mandoc_strdup(name);
1173: req->q.sec = NULL;
1.117 schwarze 1174: }
1175:
1176: /* Handle the case of name[.section] only. */
1.161 schwarze 1177: if (name == path)
1.117 schwarze 1178: return;
1179:
1.135 schwarze 1180: /* Optional manpath. */
1.161 schwarze 1181: end = strchr(path, '/');
1182: req->q.manpath = mandoc_strndup(path, end - path);
1183: if (validate_manpath(req, req->q.manpath)) {
1184: path = end + 1;
1185: if (name == path)
1186: return;
1187: } else {
1188: free(req->q.manpath);
1.135 schwarze 1189: req->q.manpath = NULL;
1.161 schwarze 1190: }
1.117 schwarze 1191:
1.135 schwarze 1192: /* Optional section. */
1.161 schwarze 1193: if (strncmp(path, "man", 3) == 0) {
1194: path += 3;
1195: end = strchr(path, '/');
1.127 schwarze 1196: free(req->q.sec);
1.161 schwarze 1197: req->q.sec = mandoc_strndup(path, end - path);
1198: path = end + 1;
1199: if (name == path)
1200: return;
1.117 schwarze 1201: }
1.161 schwarze 1202:
1203: /* Optional architecture. */
1204: end = strchr(path, '/');
1205: if (end + 1 != name) {
1206: pg_error_badrequest(
1207: "You specified too many directory components.");
1208: exit(EXIT_FAILURE);
1.135 schwarze 1209: }
1.161 schwarze 1210: req->q.arch = mandoc_strndup(path, end - path);
1211: if (validate_arch(req->q.arch) == 0) {
1.135 schwarze 1212: pg_error_badrequest(
1213: "You specified an invalid directory component.");
1214: exit(EXIT_FAILURE);
1215: }
1.24 kristaps 1216: }
1217:
1218: /*
1219: * Scan for indexable paths.
1220: */
1221: static void
1.129 schwarze 1222: parse_manpath_conf(struct req *req)
1.24 kristaps 1223: {
1.54 schwarze 1224: FILE *fp;
1225: char *dp;
1226: size_t dpsz;
1.115 schwarze 1227: ssize_t len;
1.54 schwarze 1228:
1.128 schwarze 1229: if ((fp = fopen("manpath.conf", "r")) == NULL) {
1230: warn("%s/manpath.conf", MAN_DIR);
1.74 schwarze 1231: pg_error_internal();
1232: exit(EXIT_FAILURE);
1233: }
1.24 kristaps 1234:
1.115 schwarze 1235: dp = NULL;
1236: dpsz = 0;
1237:
1238: while ((len = getline(&dp, &dpsz, fp)) != -1) {
1239: if (dp[len - 1] == '\n')
1240: dp[--len] = '\0';
1.54 schwarze 1241: req->p = mandoc_realloc(req->p,
1242: (req->psz + 1) * sizeof(char *));
1.80 schwarze 1243: if ( ! validate_urifrag(dp)) {
1.128 schwarze 1244: warnx("%s/manpath.conf contains "
1245: "unsafe path \"%s\"", MAN_DIR, dp);
1.80 schwarze 1246: pg_error_internal();
1247: exit(EXIT_FAILURE);
1248: }
1.128 schwarze 1249: if (strchr(dp, '/') != NULL) {
1250: warnx("%s/manpath.conf contains "
1251: "path with slash \"%s\"", MAN_DIR, dp);
1.80 schwarze 1252: pg_error_internal();
1253: exit(EXIT_FAILURE);
1254: }
1255: req->p[req->psz++] = dp;
1.115 schwarze 1256: dp = NULL;
1257: dpsz = 0;
1.74 schwarze 1258: }
1.115 schwarze 1259: free(dp);
1.74 schwarze 1260:
1.128 schwarze 1261: if (req->p == NULL) {
1262: warnx("%s/manpath.conf is empty", MAN_DIR);
1.74 schwarze 1263: pg_error_internal();
1264: exit(EXIT_FAILURE);
1.24 kristaps 1265: }
1266: }
CVSweb