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