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