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