Annotation of src/usr.bin/mandoc/cgi.c, Revision 1.116
1.116 ! schwarze 1: /* $OpenBSD: cgi.c,v 1.115 2021/10/24 21:24:16 deraadt Exp $ */
1.1 schwarze 2: /*
1.114 schwarze 3: * Copyright (c) 2014-2019, 2021 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.88 schwarze 86: static void 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.53 schwarze 89: static void resp_copy(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:
352: static void
1.53 schwarze 353: resp_copy(const char *filename)
354: {
355: char buf[4096];
356: ssize_t sz;
357: int fd;
358:
359: if ((fd = open(filename, O_RDONLY)) != -1) {
360: fflush(stdout);
361: while ((sz = read(fd, buf, sizeof(buf))) > 0)
362: write(STDOUT_FILENO, buf, sz);
1.77 jsg 363: close(fd);
1.53 schwarze 364: }
365: }
366:
367: static void
1.88 schwarze 368: resp_begin_html(int code, const char *msg, const char *file)
1.1 schwarze 369: {
1.114 schwarze 370: const char *name, *sec, *cp;
371: int namesz, secsz;
1.1 schwarze 372:
373: resp_begin_http(code, msg);
374:
1.37 schwarze 375: printf("<!DOCTYPE html>\n"
1.65 schwarze 376: "<html>\n"
377: "<head>\n"
1.82 schwarze 378: " <meta charset=\"UTF-8\"/>\n"
1.95 schwarze 379: " <meta name=\"viewport\""
380: " content=\"width=device-width, initial-scale=1.0\">\n"
1.82 schwarze 381: " <link rel=\"stylesheet\" href=\"%s/mandoc.css\""
1.65 schwarze 382: " type=\"text/css\" media=\"all\">\n"
1.88 schwarze 383: " <title>",
384: CSS_DIR);
385: if (file != NULL) {
1.114 schwarze 386: cp = strrchr(file, '/');
387: name = cp == NULL ? file : cp + 1;
388: cp = strrchr(name, '.');
389: namesz = cp == NULL ? strlen(name) : cp - name;
390: sec = NULL;
391: if (cp != NULL && cp[1] != '0') {
392: sec = cp + 1;
393: secsz = strlen(sec);
394: } else if (name - file > 1) {
395: for (cp = name - 2; cp >= file; cp--) {
396: if (*cp < '1' || *cp > '9')
397: continue;
398: sec = cp;
399: secsz = name - cp - 1;
400: break;
401: }
402: }
403: printf("%.*s", namesz, name);
404: if (sec != NULL)
405: printf("(%.*s)", secsz, sec);
406: fputs(" - ", stdout);
1.88 schwarze 407: }
408: printf("%s</title>\n"
1.65 schwarze 409: "</head>\n"
1.81 schwarze 410: "<body>\n",
1.88 schwarze 411: CUSTOMIZE_TITLE);
1.53 schwarze 412:
413: resp_copy(MAN_DIR "/header.html");
1.1 schwarze 414: }
415:
416: static void
417: resp_end_html(void)
418: {
419:
1.53 schwarze 420: resp_copy(MAN_DIR "/footer.html");
421:
1.65 schwarze 422: puts("</body>\n"
423: "</html>");
1.1 schwarze 424: }
425:
426: static void
1.70 schwarze 427: resp_searchform(const struct req *req, enum focus focus)
1.1 schwarze 428: {
429: int i;
430:
1.116 ! schwarze 431: printf("<header>\n"
! 432: "<form role=\"search\" action=\"/%s\" method=\"get\" "
1.109 schwarze 433: "autocomplete=\"off\" autocapitalize=\"none\">\n"
1.82 schwarze 434: " <fieldset>\n"
435: " <legend>Manual Page Search Parameters</legend>\n",
1.1 schwarze 436: scriptname);
1.8 schwarze 437:
438: /* Write query input box. */
439:
1.96 schwarze 440: printf(" <input type=\"search\" name=\"query\" value=\"");
1.70 schwarze 441: if (req->q.query != NULL)
1.25 schwarze 442: html_print(req->q.query);
1.70 schwarze 443: printf( "\" size=\"40\"");
444: if (focus == FOCUS_QUERY)
445: printf(" autofocus");
446: puts(">");
1.8 schwarze 447:
1.71 schwarze 448: /* Write submission buttons. */
1.8 schwarze 449:
1.82 schwarze 450: printf( " <button type=\"submit\" name=\"apropos\" value=\"0\">"
1.71 schwarze 451: "man</button>\n"
1.82 schwarze 452: " <button type=\"submit\" name=\"apropos\" value=\"1\">"
453: "apropos</button>\n"
454: " <br/>\n");
1.8 schwarze 455:
456: /* Write section selector. */
457:
1.116 ! schwarze 458: puts(" <select name=\"sec\" aria-label=\"manual section\">");
1.8 schwarze 459: for (i = 0; i < sec_MAX; i++) {
1.82 schwarze 460: printf(" <option value=\"%s\"", sec_numbers[i]);
1.8 schwarze 461: if (NULL != req->q.sec &&
462: 0 == strcmp(sec_numbers[i], req->q.sec))
1.65 schwarze 463: printf(" selected=\"selected\"");
464: printf(">%s</option>\n", sec_names[i]);
1.8 schwarze 465: }
1.82 schwarze 466: puts(" </select>");
1.8 schwarze 467:
468: /* Write architecture selector. */
469:
1.116 ! schwarze 470: printf( " <select name=\"arch\" aria-label=\"CPU architecture\">\n"
1.82 schwarze 471: " <option value=\"default\"");
1.21 schwarze 472: if (NULL == req->q.arch)
1.65 schwarze 473: printf(" selected=\"selected\"");
474: puts(">All Architectures</option>");
1.8 schwarze 475: for (i = 0; i < arch_MAX; i++) {
1.96 schwarze 476: printf(" <option");
1.8 schwarze 477: if (NULL != req->q.arch &&
478: 0 == strcmp(arch_names[i], req->q.arch))
1.65 schwarze 479: printf(" selected=\"selected\"");
480: printf(">%s</option>\n", arch_names[i]);
1.8 schwarze 481: }
1.82 schwarze 482: puts(" </select>");
1.8 schwarze 483:
484: /* Write manpath selector. */
485:
1.1 schwarze 486: if (req->psz > 1) {
1.82 schwarze 487: puts(" <select name=\"manpath\">");
1.1 schwarze 488: for (i = 0; i < (int)req->psz; i++) {
1.96 schwarze 489: printf(" <option");
1.41 schwarze 490: if (strcmp(req->q.manpath, req->p[i]) == 0)
1.96 schwarze 491: printf(" selected=\"selected\"");
492: printf(">");
1.1 schwarze 493: html_print(req->p[i]);
1.65 schwarze 494: puts("</option>");
1.1 schwarze 495: }
1.82 schwarze 496: puts(" </select>");
1.1 schwarze 497: }
1.8 schwarze 498:
1.82 schwarze 499: puts(" </fieldset>\n"
1.116 ! schwarze 500: "</form>\n"
! 501: "</header>");
1.1 schwarze 502: }
503:
1.16 schwarze 504: static int
1.20 schwarze 505: validate_urifrag(const char *frag)
506: {
507:
508: while ('\0' != *frag) {
509: if ( ! (isalnum((unsigned char)*frag) ||
510: '-' == *frag || '.' == *frag ||
511: '/' == *frag || '_' == *frag))
1.48 schwarze 512: return 0;
1.20 schwarze 513: frag++;
514: }
1.48 schwarze 515: return 1;
1.20 schwarze 516: }
517:
518: static int
1.17 schwarze 519: validate_manpath(const struct req *req, const char* manpath)
520: {
521: size_t i;
522:
523: for (i = 0; i < req->psz; i++)
524: if ( ! strcmp(manpath, req->p[i]))
1.48 schwarze 525: return 1;
1.17 schwarze 526:
1.48 schwarze 527: return 0;
1.17 schwarze 528: }
529:
530: static int
1.97 schwarze 531: validate_arch(const char *arch)
532: {
533: int i;
534:
535: for (i = 0; i < arch_MAX; i++)
536: if (strcmp(arch, arch_names[i]) == 0)
537: return 1;
538:
539: return 0;
540: }
541:
542: static int
1.16 schwarze 543: validate_filename(const char *file)
544: {
545:
546: if ('.' == file[0] && '/' == file[1])
547: file += 2;
548:
1.48 schwarze 549: return ! (strstr(file, "../") || strstr(file, "/..") ||
550: (strncmp(file, "man", 3) && strncmp(file, "cat", 3)));
1.16 schwarze 551: }
552:
1.1 schwarze 553: static void
1.12 schwarze 554: pg_index(const struct req *req)
1.1 schwarze 555: {
556:
1.88 schwarze 557: resp_begin_html(200, NULL, NULL);
1.70 schwarze 558: resp_searchform(req, FOCUS_QUERY);
1.116 ! schwarze 559: printf("<main>\n"
! 560: "<p role=\"doc-notice\" aria-label=\"usage\">\n"
1.26 schwarze 561: "This web interface is documented in the\n"
1.116 ! schwarze 562: "<a class=\"Xr\" href=\"/%s%sman.cgi.8\""
! 563: " aria-label=\"man dot CGI, section 8\">man.cgi(8)</a>\n"
1.26 schwarze 564: "manual, and the\n"
1.116 ! schwarze 565: "<a class=\"Xr\" href=\"/%s%sapropos.1\""
! 566: " aria-label=\"apropos, section 1\">apropos(1)</a>\n"
1.9 schwarze 567: "manual explains the query syntax.\n"
1.116 ! schwarze 568: "</p>\n"
! 569: "</main>\n",
1.58 schwarze 570: scriptname, *scriptname == '\0' ? "" : "/",
571: scriptname, *scriptname == '\0' ? "" : "/");
1.1 schwarze 572: resp_end_html();
573: }
574:
575: static void
1.106 schwarze 576: pg_noresult(const struct req *req, int code, const char *http_msg,
577: const char *user_msg)
1.1 schwarze 578: {
1.106 schwarze 579: resp_begin_html(code, http_msg, NULL);
1.70 schwarze 580: resp_searchform(req, FOCUS_QUERY);
1.116 ! schwarze 581: puts("<main>");
! 582: puts("<p role=\"doc-notice\" aria-label=\"no result\">");
1.106 schwarze 583: puts(user_msg);
1.65 schwarze 584: puts("</p>");
1.116 ! schwarze 585: puts("</main>");
1.1 schwarze 586: resp_end_html();
587: }
588:
589: static void
1.12 schwarze 590: pg_error_badrequest(const char *msg)
1.1 schwarze 591: {
592:
1.88 schwarze 593: resp_begin_html(400, "Bad Request", NULL);
1.116 ! schwarze 594: puts("<main>\n"
! 595: "<h1>Bad Request</h1>\n"
! 596: "<p role=\"doc-notice\" aria-label=\"Bad Request\">");
1.1 schwarze 597: puts(msg);
598: printf("Try again from the\n"
1.65 schwarze 599: "<a href=\"/%s\">main page</a>.\n"
1.116 ! schwarze 600: "</p>\n"
! 601: "</main>", scriptname);
1.1 schwarze 602: resp_end_html();
603: }
604:
605: static void
1.12 schwarze 606: pg_error_internal(void)
1.1 schwarze 607: {
1.88 schwarze 608: resp_begin_html(500, "Internal Server Error", NULL);
1.116 ! schwarze 609: puts("<main><p role=\"doc-notice\">Internal Server Error</p></main>");
1.1 schwarze 610: resp_end_html();
611: }
612:
613: static void
1.87 schwarze 614: pg_redirect(const struct req *req, const char *name)
615: {
1.91 schwarze 616: printf("Status: 303 See Other\r\n"
617: "Location: /");
1.87 schwarze 618: if (*scriptname != '\0')
619: printf("%s/", scriptname);
620: if (strcmp(req->q.manpath, req->p[0]))
621: printf("%s/", req->q.manpath);
622: if (req->q.arch != NULL)
623: printf("%s/", req->q.arch);
1.97 schwarze 624: http_encode(name);
625: if (req->q.sec != NULL) {
626: putchar('.');
627: http_encode(req->q.sec);
628: }
1.87 schwarze 629: printf("\r\nContent-Type: text/html; charset=utf-8\r\n\r\n");
630: }
631:
632: static void
1.12 schwarze 633: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1 schwarze 634: {
1.21 schwarze 635: char *arch, *archend;
1.59 schwarze 636: const char *sec;
637: size_t i, iuse;
1.21 schwarze 638: int archprio, archpriouse;
1.10 schwarze 639: int prio, priouse;
1.1 schwarze 640:
1.16 schwarze 641: for (i = 0; i < sz; i++) {
642: if (validate_filename(r[i].file))
643: continue;
1.67 schwarze 644: warnx("invalid filename %s in %s database",
1.16 schwarze 645: r[i].file, req->q.manpath);
646: pg_error_internal();
647: return;
648: }
649:
1.60 schwarze 650: if (req->isquery && sz == 1) {
1.1 schwarze 651: /*
652: * If we have just one result, then jump there now
653: * without any delay.
654: */
1.91 schwarze 655: printf("Status: 303 See Other\r\n"
656: "Location: /");
657: if (*scriptname != '\0')
658: printf("%s/", scriptname);
659: if (strcmp(req->q.manpath, req->p[0]))
660: printf("%s/", req->q.manpath);
661: printf("%s\r\n"
662: "Content-Type: text/html; charset=utf-8\r\n\r\n",
663: r[0].file);
1.1 schwarze 664: return;
665: }
666:
1.10 schwarze 667: /*
668: * In man(1) mode, show one of the pages
669: * even if more than one is found.
670: */
671:
1.88 schwarze 672: iuse = 0;
1.62 schwarze 673: if (req->q.equal || sz == 1) {
1.59 schwarze 674: priouse = 20;
1.21 schwarze 675: archpriouse = 3;
1.10 schwarze 676: for (i = 0; i < sz; i++) {
1.59 schwarze 677: sec = r[i].file;
678: sec += strcspn(sec, "123456789");
679: if (sec[0] == '\0')
1.10 schwarze 680: continue;
1.59 schwarze 681: prio = sec_prios[sec[0] - '1'];
682: if (sec[1] != '/')
683: prio += 10;
684: if (req->q.arch == NULL) {
1.21 schwarze 685: archprio =
1.59 schwarze 686: ((arch = strchr(sec + 1, '/'))
687: == NULL) ? 3 :
688: ((archend = strchr(arch + 1, '/'))
689: == NULL) ? 0 :
1.21 schwarze 690: strncmp(arch, "amd64/",
691: archend - arch) ? 2 : 1;
692: if (archprio < archpriouse) {
693: archpriouse = archprio;
694: priouse = prio;
695: iuse = i;
696: continue;
697: }
698: if (archprio > archpriouse)
699: continue;
700: }
1.10 schwarze 701: if (prio >= priouse)
702: continue;
703: priouse = prio;
704: iuse = i;
705: }
1.88 schwarze 706: resp_begin_html(200, NULL, r[iuse].file);
707: } else
708: resp_begin_html(200, NULL, NULL);
709:
710: resp_searchform(req,
711: req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
712:
713: if (sz > 1) {
1.116 ! schwarze 714: puts("<nav>");
1.88 schwarze 715: puts("<table class=\"results\">");
716: for (i = 0; i < sz; i++) {
717: printf(" <tr>\n"
718: " <td>"
1.89 schwarze 719: "<a class=\"Xr\" href=\"/");
720: if (*scriptname != '\0')
721: printf("%s/", scriptname);
722: if (strcmp(req->q.manpath, req->p[0]))
723: printf("%s/", req->q.manpath);
724: printf("%s\">", r[i].file);
1.88 schwarze 725: html_print(r[i].names);
726: printf("</a></td>\n"
727: " <td><span class=\"Nd\">");
728: html_print(r[i].output);
729: puts("</span></td>\n"
730: " </tr>");
731: }
732: puts("</table>");
1.116 ! schwarze 733: puts("</nav>");
1.88 schwarze 734: }
735:
736: if (req->q.equal || sz == 1) {
737: puts("<hr>");
1.10 schwarze 738: resp_show(req, r[iuse].file);
739: }
740:
1.1 schwarze 741: resp_end_html();
742: }
743:
744: static void
1.68 schwarze 745: resp_catman(const struct req *req, const char *file)
1.1 schwarze 746: {
747: FILE *f;
1.54 schwarze 748: char *p;
749: size_t sz;
750: ssize_t len;
1.1 schwarze 751: int i;
752: int italic, bold;
753:
1.54 schwarze 754: if ((f = fopen(file, "r")) == NULL) {
1.116 ! schwarze 755: puts("<p role=\"doc-notice\">\n"
! 756: " You specified an invalid manual file.\n"
! 757: "</p>");
1.1 schwarze 758: return;
759: }
760:
1.65 schwarze 761: puts("<div class=\"catman\">\n"
762: "<pre>");
1.1 schwarze 763:
1.54 schwarze 764: p = NULL;
765: sz = 0;
766:
767: while ((len = getline(&p, &sz, f)) != -1) {
1.1 schwarze 768: bold = italic = 0;
1.54 schwarze 769: for (i = 0; i < len - 1; i++) {
1.43 schwarze 770: /*
1.1 schwarze 771: * This means that the catpage is out of state.
772: * Ignore it and keep going (although the
773: * catpage is bogus).
774: */
775:
776: if ('\b' == p[i] || '\n' == p[i])
777: continue;
778:
779: /*
780: * Print a regular character.
781: * Close out any bold/italic scopes.
782: * If we're in back-space mode, make sure we'll
783: * have something to enter when we backspace.
784: */
785:
786: if ('\b' != p[i + 1]) {
787: if (italic)
1.65 schwarze 788: printf("</i>");
1.1 schwarze 789: if (bold)
1.65 schwarze 790: printf("</b>");
1.1 schwarze 791: italic = bold = 0;
792: html_putchar(p[i]);
793: continue;
1.54 schwarze 794: } else if (i + 2 >= len)
1.1 schwarze 795: continue;
796:
797: /* Italic mode. */
798:
799: if ('_' == p[i]) {
800: if (bold)
1.65 schwarze 801: printf("</b>");
1.1 schwarze 802: if ( ! italic)
1.65 schwarze 803: printf("<i>");
1.1 schwarze 804: bold = 0;
805: italic = 1;
806: i += 2;
807: html_putchar(p[i]);
808: continue;
809: }
810:
1.43 schwarze 811: /*
1.1 schwarze 812: * Handle funny behaviour troff-isms.
813: * These grok'd from the original man2html.c.
814: */
815:
816: if (('+' == p[i] && 'o' == p[i + 2]) ||
817: ('o' == p[i] && '+' == p[i + 2]) ||
818: ('|' == p[i] && '=' == p[i + 2]) ||
819: ('=' == p[i] && '|' == p[i + 2]) ||
820: ('*' == p[i] && '=' == p[i + 2]) ||
821: ('=' == p[i] && '*' == p[i + 2]) ||
822: ('*' == p[i] && '|' == p[i + 2]) ||
823: ('|' == p[i] && '*' == p[i + 2])) {
824: if (italic)
1.65 schwarze 825: printf("</i>");
1.1 schwarze 826: if (bold)
1.65 schwarze 827: printf("</b>");
1.1 schwarze 828: italic = bold = 0;
829: putchar('*');
830: i += 2;
831: continue;
832: } else if (('|' == p[i] && '-' == p[i + 2]) ||
833: ('-' == p[i] && '|' == p[i + 1]) ||
834: ('+' == p[i] && '-' == p[i + 1]) ||
835: ('-' == p[i] && '+' == p[i + 1]) ||
836: ('+' == p[i] && '|' == p[i + 1]) ||
837: ('|' == p[i] && '+' == p[i + 1])) {
838: if (italic)
1.65 schwarze 839: printf("</i>");
1.1 schwarze 840: if (bold)
1.65 schwarze 841: printf("</b>");
1.1 schwarze 842: italic = bold = 0;
843: putchar('+');
844: i += 2;
845: continue;
846: }
847:
848: /* Bold mode. */
1.43 schwarze 849:
1.1 schwarze 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: bold = 1;
855: italic = 0;
856: i += 2;
857: html_putchar(p[i]);
858: }
859:
1.43 schwarze 860: /*
1.1 schwarze 861: * Clean up the last character.
1.43 schwarze 862: * We can get to a newline; don't print that.
1.1 schwarze 863: */
864:
865: if (italic)
1.65 schwarze 866: printf("</i>");
1.1 schwarze 867: if (bold)
1.65 schwarze 868: printf("</b>");
1.1 schwarze 869:
1.54 schwarze 870: if (i == len - 1 && p[i] != '\n')
1.1 schwarze 871: html_putchar(p[i]);
872:
873: putchar('\n');
874: }
1.54 schwarze 875: free(p);
1.1 schwarze 876:
1.65 schwarze 877: puts("</pre>\n"
878: "</div>");
1.1 schwarze 879:
880: fclose(f);
881: }
882:
883: static void
1.68 schwarze 884: resp_format(const struct req *req, const char *file)
1.1 schwarze 885: {
1.45 schwarze 886: struct manoutput conf;
1.1 schwarze 887: struct mparse *mp;
1.102 schwarze 888: struct roff_meta *meta;
1.1 schwarze 889: void *vp;
1.30 schwarze 890: int fd;
891: int usepath;
1.1 schwarze 892:
1.115 deraadt 893: if (-1 == (fd = open(file, O_RDONLY))) {
1.116 ! schwarze 894: puts("<p role=\"doc-notice\">\n"
! 895: " You specified an invalid manual file.\n"
! 896: "</p>");
1.1 schwarze 897: return;
898: }
899:
1.49 schwarze 900: mchars_alloc();
1.102 schwarze 901: mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1 |
902: MPARSE_VALIDATE, MANDOC_OS_OTHER, req->q.manpath);
1.42 schwarze 903: mparse_readfd(mp, fd, file);
1.1 schwarze 904: close(fd);
1.102 schwarze 905: meta = mparse_result(mp);
1.1 schwarze 906:
1.45 schwarze 907: memset(&conf, 0, sizeof(conf));
908: conf.fragment = 1;
1.84 schwarze 909: conf.style = mandoc_strdup(CSS_DIR "/mandoc.css");
1.30 schwarze 910: usepath = strcmp(req->q.manpath, req->p[0]);
1.90 schwarze 911: mandoc_asprintf(&conf.man, "/%s%s%s%s%%N.%%S",
912: scriptname, *scriptname == '\0' ? "" : "/",
1.61 schwarze 913: usepath ? req->q.manpath : "", usepath ? "/" : "");
1.1 schwarze 914:
1.49 schwarze 915: vp = html_alloc(&conf);
1.102 schwarze 916: if (meta->macroset == MACROSET_MDOC)
917: html_mdoc(vp, meta);
918: else
919: html_man(vp, meta);
1.1 schwarze 920:
921: html_free(vp);
922: mparse_free(mp);
1.49 schwarze 923: mchars_free();
1.45 schwarze 924: free(conf.man);
1.84 schwarze 925: free(conf.style);
1.1 schwarze 926: }
927:
928: static void
1.10 schwarze 929: resp_show(const struct req *req, const char *file)
930: {
1.16 schwarze 931:
932: if ('.' == file[0] && '/' == file[1])
1.11 schwarze 933: file += 2;
1.10 schwarze 934:
935: if ('c' == *file)
1.68 schwarze 936: resp_catman(req, file);
1.10 schwarze 937: else
1.68 schwarze 938: resp_format(req, file);
1.10 schwarze 939: }
940:
941: static void
1.24 schwarze 942: pg_show(struct req *req, const char *fullpath)
1.1 schwarze 943: {
1.24 schwarze 944: char *manpath;
945: const char *file;
1.1 schwarze 946:
1.24 schwarze 947: if ((file = strchr(fullpath, '/')) == NULL) {
1.12 schwarze 948: pg_error_badrequest(
1.1 schwarze 949: "You did not specify a page to show.");
950: return;
1.43 schwarze 951: }
1.24 schwarze 952: manpath = mandoc_strndup(fullpath, file - fullpath);
953: file++;
1.1 schwarze 954:
1.24 schwarze 955: if ( ! validate_manpath(req, manpath)) {
1.17 schwarze 956: pg_error_badrequest(
957: "You specified an invalid manpath.");
1.24 schwarze 958: free(manpath);
1.17 schwarze 959: return;
960: }
961:
1.1 schwarze 962: /*
963: * Begin by chdir()ing into the manpath.
964: * This way we can pick up the database files, which are
965: * relative to the manpath root.
966: */
967:
1.24 schwarze 968: if (chdir(manpath) == -1) {
1.67 schwarze 969: warn("chdir %s", manpath);
1.17 schwarze 970: pg_error_internal();
1.24 schwarze 971: free(manpath);
1.16 schwarze 972: return;
973: }
1.73 schwarze 974: free(manpath);
1.24 schwarze 975:
976: if ( ! validate_filename(file)) {
1.16 schwarze 977: pg_error_badrequest(
978: "You specified an invalid manual file.");
1.1 schwarze 979: return;
980: }
1.19 schwarze 981:
1.88 schwarze 982: resp_begin_html(200, NULL, file);
1.70 schwarze 983: resp_searchform(req, FOCUS_NONE);
1.24 schwarze 984: resp_show(req, file);
1.10 schwarze 985: resp_end_html();
1.1 schwarze 986: }
987:
988: static void
1.6 schwarze 989: pg_search(const struct req *req)
1.1 schwarze 990: {
991: struct mansearch search;
992: struct manpaths paths;
993: struct manpage *res;
1.36 schwarze 994: char **argv;
995: char *query, *rp, *wp;
1.1 schwarze 996: size_t ressz;
1.36 schwarze 997: int argc;
1.1 schwarze 998:
999: /*
1000: * Begin by chdir()ing into the root of the manpath.
1001: * This way we can pick up the database files, which are
1002: * relative to the manpath root.
1003: */
1004:
1.67 schwarze 1005: if (chdir(req->q.manpath) == -1) {
1006: warn("chdir %s", req->q.manpath);
1.17 schwarze 1007: pg_error_internal();
1.1 schwarze 1008: return;
1009: }
1010:
1011: search.arch = req->q.arch;
1012: search.sec = req->q.sec;
1.35 schwarze 1013: search.outkey = "Nd";
1014: search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.40 schwarze 1015: search.firstmatch = 1;
1.1 schwarze 1016:
1017: paths.sz = 1;
1018: paths.paths = mandoc_malloc(sizeof(char *));
1019: paths.paths[0] = mandoc_strdup(".");
1020:
1021: /*
1.36 schwarze 1022: * Break apart at spaces with backslash-escaping.
1.1 schwarze 1023: */
1024:
1.36 schwarze 1025: argc = 0;
1026: argv = NULL;
1027: rp = query = mandoc_strdup(req->q.query);
1028: for (;;) {
1029: while (isspace((unsigned char)*rp))
1030: rp++;
1031: if (*rp == '\0')
1032: break;
1033: argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
1034: argv[argc++] = wp = rp;
1035: for (;;) {
1036: if (isspace((unsigned char)*rp)) {
1037: *wp = '\0';
1038: rp++;
1039: break;
1040: }
1041: if (rp[0] == '\\' && rp[1] != '\0')
1042: rp++;
1043: if (wp != rp)
1044: *wp = *rp;
1045: if (*rp == '\0')
1046: break;
1047: wp++;
1048: rp++;
1049: }
1.1 schwarze 1050: }
1051:
1.87 schwarze 1052: res = NULL;
1053: ressz = 0;
1054: if (req->isquery && req->q.equal && argc == 1)
1055: pg_redirect(req, argv[0]);
1056: else if (mansearch(&search, &paths, argc, argv, &res, &ressz) == 0)
1.106 schwarze 1057: pg_noresult(req, 400, "Bad Request",
1058: "You entered an invalid query.");
1.87 schwarze 1059: else if (ressz == 0)
1.106 schwarze 1060: pg_noresult(req, 404, "Not Found", "No results found.");
1.1 schwarze 1061: else
1.12 schwarze 1062: pg_searchres(req, res, ressz);
1.1 schwarze 1063:
1.36 schwarze 1064: free(query);
1065: mansearch_free(res, ressz);
1.1 schwarze 1066: free(paths.paths[0]);
1067: free(paths.paths);
1068: }
1069:
1070: int
1071: main(void)
1072: {
1.6 schwarze 1073: struct req req;
1.33 schwarze 1074: struct itimerval itimer;
1.6 schwarze 1075: const char *path;
1.23 schwarze 1076: const char *querystring;
1.1 schwarze 1077: int i;
1.86 schwarze 1078:
1079: /*
1080: * The "rpath" pledge could be revoked after mparse_readfd()
1081: * if the file desciptor to "/footer.html" would be opened
1082: * up front, but it's probably not worth the complication
1083: * of the code it would cause: it would require scattering
1084: * pledge() calls in multiple low-level resp_*() functions.
1085: */
1086:
1087: if (pledge("stdio rpath", NULL) == -1) {
1088: warn("pledge");
1089: pg_error_internal();
1090: return EXIT_FAILURE;
1091: }
1.33 schwarze 1092:
1093: /* Poor man's ReDoS mitigation. */
1094:
1.38 schwarze 1095: itimer.it_value.tv_sec = 2;
1.33 schwarze 1096: itimer.it_value.tv_usec = 0;
1.38 schwarze 1097: itimer.it_interval.tv_sec = 2;
1.33 schwarze 1098: itimer.it_interval.tv_usec = 0;
1099: if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1.67 schwarze 1100: warn("setitimer");
1.20 schwarze 1101: pg_error_internal();
1.48 schwarze 1102: return EXIT_FAILURE;
1.20 schwarze 1103: }
1104:
1.1 schwarze 1105: /*
1.7 schwarze 1106: * First we change directory into the MAN_DIR so that
1.1 schwarze 1107: * subsequent scanning for manpath directories is rooted
1108: * relative to the same position.
1109: */
1110:
1.67 schwarze 1111: if (chdir(MAN_DIR) == -1) {
1112: warn("MAN_DIR: %s", MAN_DIR);
1.12 schwarze 1113: pg_error_internal();
1.48 schwarze 1114: return EXIT_FAILURE;
1.43 schwarze 1115: }
1.1 schwarze 1116:
1117: memset(&req, 0, sizeof(struct req));
1.57 schwarze 1118: req.q.equal = 1;
1.68 schwarze 1119: parse_manpath_conf(&req);
1.1 schwarze 1120:
1.56 schwarze 1121: /* Parse the path info and the query string. */
1.1 schwarze 1122:
1.56 schwarze 1123: if ((path = getenv("PATH_INFO")) == NULL)
1124: path = "";
1125: else if (*path == '/')
1126: path++;
1127:
1.63 schwarze 1128: if (*path != '\0') {
1.68 schwarze 1129: parse_path_info(&req, path);
1.92 schwarze 1130: if (req.q.manpath == NULL || req.q.sec == NULL ||
1131: *req.q.query == '\0' || access(path, F_OK) == -1)
1.63 schwarze 1132: path = "";
1.56 schwarze 1133: } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.68 schwarze 1134: parse_query_string(&req, querystring);
1.17 schwarze 1135:
1.56 schwarze 1136: /* Validate parsed data and add defaults. */
1137:
1.41 schwarze 1138: if (req.q.manpath == NULL)
1139: req.q.manpath = mandoc_strdup(req.p[0]);
1140: else if ( ! validate_manpath(&req, req.q.manpath)) {
1.17 schwarze 1141: pg_error_badrequest(
1142: "You specified an invalid manpath.");
1.48 schwarze 1143: return EXIT_FAILURE;
1.17 schwarze 1144: }
1.1 schwarze 1145:
1.97 schwarze 1146: if (req.q.arch != NULL && validate_arch(req.q.arch) == 0) {
1.20 schwarze 1147: pg_error_badrequest(
1148: "You specified an invalid architecture.");
1.48 schwarze 1149: return EXIT_FAILURE;
1.20 schwarze 1150: }
1151:
1.6 schwarze 1152: /* Dispatch to the three different pages. */
1.1 schwarze 1153:
1.6 schwarze 1154: if ('\0' != *path)
1155: pg_show(&req, path);
1.25 schwarze 1156: else if (NULL != req.q.query)
1.6 schwarze 1157: pg_search(&req);
1158: else
1.12 schwarze 1159: pg_index(&req);
1.1 schwarze 1160:
1.23 schwarze 1161: free(req.q.manpath);
1162: free(req.q.arch);
1163: free(req.q.sec);
1.25 schwarze 1164: free(req.q.query);
1.1 schwarze 1165: for (i = 0; i < (int)req.psz; i++)
1166: free(req.p[i]);
1167: free(req.p);
1.48 schwarze 1168: return EXIT_SUCCESS;
1.56 schwarze 1169: }
1170:
1171: /*
1.99 schwarze 1172: * Translate PATH_INFO to a query.
1.56 schwarze 1173: */
1174: static void
1.68 schwarze 1175: parse_path_info(struct req *req, const char *path)
1.56 schwarze 1176: {
1.99 schwarze 1177: const char *name, *sec, *end;
1.56 schwarze 1178:
1.60 schwarze 1179: req->isquery = 0;
1.56 schwarze 1180: req->q.equal = 1;
1.99 schwarze 1181: req->q.manpath = NULL;
1.74 schwarze 1182: req->q.arch = NULL;
1.56 schwarze 1183:
1184: /* Mandatory manual page name. */
1.99 schwarze 1185: if ((name = strrchr(path, '/')) == NULL)
1186: name = path;
1187: else
1188: name++;
1.56 schwarze 1189:
1190: /* Optional trailing section. */
1.99 schwarze 1191: sec = strrchr(name, '.');
1192: if (sec != NULL && isdigit((unsigned char)*++sec)) {
1193: req->q.query = mandoc_strndup(name, sec - name - 1);
1194: req->q.sec = mandoc_strdup(sec);
1195: } else {
1196: req->q.query = mandoc_strdup(name);
1197: req->q.sec = NULL;
1.56 schwarze 1198: }
1199:
1200: /* Handle the case of name[.section] only. */
1.99 schwarze 1201: if (name == path)
1.56 schwarze 1202: return;
1203:
1.74 schwarze 1204: /* Optional manpath. */
1.99 schwarze 1205: end = strchr(path, '/');
1206: req->q.manpath = mandoc_strndup(path, end - path);
1207: if (validate_manpath(req, req->q.manpath)) {
1208: path = end + 1;
1209: if (name == path)
1210: return;
1211: } else {
1212: free(req->q.manpath);
1.74 schwarze 1213: req->q.manpath = NULL;
1.99 schwarze 1214: }
1.56 schwarze 1215:
1.74 schwarze 1216: /* Optional section. */
1.103 schwarze 1217: if (strncmp(path, "man", 3) == 0 || strncmp(path, "cat", 3) == 0) {
1.99 schwarze 1218: path += 3;
1219: end = strchr(path, '/');
1.66 schwarze 1220: free(req->q.sec);
1.99 schwarze 1221: req->q.sec = mandoc_strndup(path, end - path);
1222: path = end + 1;
1223: if (name == path)
1224: return;
1.56 schwarze 1225: }
1.99 schwarze 1226:
1227: /* Optional architecture. */
1228: end = strchr(path, '/');
1229: if (end + 1 != name) {
1230: pg_error_badrequest(
1231: "You specified too many directory components.");
1232: exit(EXIT_FAILURE);
1.74 schwarze 1233: }
1.99 schwarze 1234: req->q.arch = mandoc_strndup(path, end - path);
1235: if (validate_arch(req->q.arch) == 0) {
1.74 schwarze 1236: pg_error_badrequest(
1237: "You specified an invalid directory component.");
1238: exit(EXIT_FAILURE);
1239: }
1.1 schwarze 1240: }
1241:
1242: /*
1243: * Scan for indexable paths.
1244: */
1245: static void
1.68 schwarze 1246: parse_manpath_conf(struct req *req)
1.1 schwarze 1247: {
1248: FILE *fp;
1249: char *dp;
1250: size_t dpsz;
1.54 schwarze 1251: ssize_t len;
1.1 schwarze 1252:
1.67 schwarze 1253: if ((fp = fopen("manpath.conf", "r")) == NULL) {
1254: warn("%s/manpath.conf", MAN_DIR);
1.14 schwarze 1255: pg_error_internal();
1256: exit(EXIT_FAILURE);
1257: }
1.1 schwarze 1258:
1.54 schwarze 1259: dp = NULL;
1260: dpsz = 0;
1261:
1262: while ((len = getline(&dp, &dpsz, fp)) != -1) {
1263: if (dp[len - 1] == '\n')
1264: dp[--len] = '\0';
1.1 schwarze 1265: req->p = mandoc_realloc(req->p,
1266: (req->psz + 1) * sizeof(char *));
1.20 schwarze 1267: if ( ! validate_urifrag(dp)) {
1.67 schwarze 1268: warnx("%s/manpath.conf contains "
1269: "unsafe path \"%s\"", MAN_DIR, dp);
1.20 schwarze 1270: pg_error_internal();
1271: exit(EXIT_FAILURE);
1272: }
1.67 schwarze 1273: if (strchr(dp, '/') != NULL) {
1274: warnx("%s/manpath.conf contains "
1275: "path with slash \"%s\"", MAN_DIR, dp);
1.20 schwarze 1276: pg_error_internal();
1277: exit(EXIT_FAILURE);
1278: }
1279: req->p[req->psz++] = dp;
1.54 schwarze 1280: dp = NULL;
1281: dpsz = 0;
1.14 schwarze 1282: }
1.54 schwarze 1283: free(dp);
1.14 schwarze 1284:
1.67 schwarze 1285: if (req->p == NULL) {
1286: warnx("%s/manpath.conf is empty", MAN_DIR);
1.14 schwarze 1287: pg_error_internal();
1288: exit(EXIT_FAILURE);
1.1 schwarze 1289: }
1290: }