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