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