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