Annotation of src/usr.bin/mandoc/cgi.c, Revision 1.55
1.55 ! bentley 1: /* $OpenBSD: cgi.c,v 1.54 2015/11/07 17:58:52 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.45 schwarze 4: * Copyright (c) 2014, 2015 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 */
56: };
57:
58: static void catman(const struct req *, const char *);
59: static void format(const struct req *, const char *);
60: static void html_print(const char *);
61: static void html_putchar(char);
1.43 schwarze 62: static int http_decode(char *);
1.23 schwarze 63: static void http_parse(struct req *, const char *);
1.1 schwarze 64: static void pathgen(struct req *);
1.12 schwarze 65: static void pg_error_badrequest(const char *);
66: static void pg_error_internal(void);
67: static void pg_index(const struct req *);
68: static void pg_noresult(const struct req *, const char *);
1.6 schwarze 69: static void pg_search(const struct req *);
1.12 schwarze 70: static void pg_searchres(const struct req *,
71: struct manpage *, size_t);
1.19 schwarze 72: static void pg_show(struct req *, const char *);
1.1 schwarze 73: static void resp_begin_html(int, const char *);
74: static void resp_begin_http(int, const char *);
1.53 schwarze 75: static void resp_copy(const char *);
1.1 schwarze 76: static void resp_end_html(void);
77: static void resp_searchform(const struct req *);
1.10 schwarze 78: static void resp_show(const struct req *, const char *);
1.25 schwarze 79: static void set_query_attr(char **, char **);
80: static int validate_filename(const char *);
81: static int validate_manpath(const struct req *, const char *);
82: static int validate_urifrag(const char *);
1.1 schwarze 83:
84: static const char *scriptname; /* CGI script name */
85:
1.10 schwarze 86: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.8 schwarze 87: static const char *const sec_numbers[] = {
88: "0", "1", "2", "3", "3p", "4", "5", "6", "7", "8", "9"
89: };
90: static const char *const sec_names[] = {
91: "All Sections",
92: "1 - General Commands",
93: "2 - System Calls",
1.34 schwarze 94: "3 - Library Functions",
95: "3p - Perl Library",
96: "4 - Device Drivers",
1.8 schwarze 97: "5 - File Formats",
98: "6 - Games",
1.34 schwarze 99: "7 - Miscellaneous Information",
100: "8 - System Manager\'s Manual",
101: "9 - Kernel Developer\'s Manual"
1.8 schwarze 102: };
103: static const int sec_MAX = sizeof(sec_names) / sizeof(char *);
104:
105: static const char *const arch_names[] = {
106: "amd64", "alpha", "armish", "armv7",
107: "aviion", "hppa", "hppa64", "i386",
108: "ia64", "landisk", "loongson", "luna88k",
109: "macppc", "mips64", "octeon", "sgi",
110: "socppc", "solbourne", "sparc", "sparc64",
111: "vax", "zaurus",
112: "amiga", "arc", "arm32", "atari",
113: "beagle", "cats", "hp300", "mac68k",
114: "mvme68k", "mvme88k", "mvmeppc", "palm",
115: "pc532", "pegasos", "pmax", "powerpc",
116: "sun3", "wgrisc", "x68k"
117: };
118: static const int arch_MAX = sizeof(arch_names) / sizeof(char *);
119:
1.1 schwarze 120: /*
121: * Print a character, escaping HTML along the way.
122: * This will pass non-ASCII straight to output: be warned!
123: */
124: static void
125: html_putchar(char c)
126: {
127:
128: switch (c) {
129: case ('"'):
130: printf(""e;");
131: break;
132: case ('&'):
133: printf("&");
134: break;
135: case ('>'):
136: printf(">");
137: break;
138: case ('<'):
139: printf("<");
140: break;
141: default:
142: putchar((unsigned char)c);
143: break;
144: }
145: }
146:
147: /*
148: * Call through to html_putchar().
149: * Accepts NULL strings.
150: */
151: static void
152: html_print(const char *p)
153: {
1.43 schwarze 154:
1.1 schwarze 155: if (NULL == p)
156: return;
157: while ('\0' != *p)
158: html_putchar(*p++);
159: }
160:
161: /*
1.23 schwarze 162: * Transfer the responsibility for the allocated string *val
163: * to the query structure.
1.1 schwarze 164: */
165: static void
1.23 schwarze 166: set_query_attr(char **attr, char **val)
1.1 schwarze 167: {
168:
1.23 schwarze 169: free(*attr);
170: if (**val == '\0') {
171: *attr = NULL;
172: free(*val);
173: } else
174: *attr = *val;
175: *val = NULL;
176: }
177:
178: /*
179: * Parse the QUERY_STRING for key-value pairs
180: * and store the values into the query structure.
181: */
182: static void
183: http_parse(struct req *req, const char *qs)
184: {
185: char *key, *val;
186: size_t keysz, valsz;
187:
188: req->q.manpath = NULL;
189: req->q.arch = NULL;
190: req->q.sec = NULL;
1.25 schwarze 191: req->q.query = NULL;
1.23 schwarze 192: req->q.equal = 1;
193:
194: key = val = NULL;
195: while (*qs != '\0') {
1.1 schwarze 196:
1.23 schwarze 197: /* Parse one key. */
198:
199: keysz = strcspn(qs, "=;&");
200: key = mandoc_strndup(qs, keysz);
201: qs += keysz;
202: if (*qs != '=')
203: goto next;
204:
205: /* Parse one value. */
206:
207: valsz = strcspn(++qs, ";&");
208: val = mandoc_strndup(qs, valsz);
209: qs += valsz;
210:
211: /* Decode and catch encoding errors. */
1.1 schwarze 212:
1.23 schwarze 213: if ( ! (http_decode(key) && http_decode(val)))
214: goto next;
1.1 schwarze 215:
1.23 schwarze 216: /* Handle key-value pairs. */
1.1 schwarze 217:
1.23 schwarze 218: if ( ! strcmp(key, "query"))
1.25 schwarze 219: set_query_attr(&req->q.query, &val);
1.1 schwarze 220:
1.23 schwarze 221: else if ( ! strcmp(key, "apropos"))
222: req->q.equal = !strcmp(val, "0");
223:
224: else if ( ! strcmp(key, "manpath")) {
1.13 schwarze 225: #ifdef COMPAT_OLDURI
1.23 schwarze 226: if ( ! strncmp(val, "OpenBSD ", 8)) {
1.13 schwarze 227: val[7] = '-';
228: if ('C' == val[8])
229: val[8] = 'c';
230: }
231: #endif
1.23 schwarze 232: set_query_attr(&req->q.manpath, &val);
233: }
234:
235: else if ( ! (strcmp(key, "sec")
1.13 schwarze 236: #ifdef COMPAT_OLDURI
1.23 schwarze 237: && strcmp(key, "sektion")
1.13 schwarze 238: #endif
1.23 schwarze 239: )) {
240: if ( ! strcmp(val, "0"))
241: *val = '\0';
242: set_query_attr(&req->q.sec, &val);
1.5 schwarze 243: }
1.23 schwarze 244:
245: else if ( ! strcmp(key, "arch")) {
246: if ( ! strcmp(val, "default"))
247: *val = '\0';
248: set_query_attr(&req->q.arch, &val);
249: }
250:
251: /*
252: * The key must be freed in any case.
253: * The val may have been handed over to the query
254: * structure, in which case it is now NULL.
255: */
256: next:
257: free(key);
258: key = NULL;
259: free(val);
260: val = NULL;
261:
262: if (*qs != '\0')
263: qs++;
1.1 schwarze 264: }
265: }
266:
267: /*
268: * HTTP-decode a string. The standard explanation is that this turns
269: * "%4e+foo" into "n foo" in the regular way. This is done in-place
270: * over the allocated string.
271: */
272: static int
273: http_decode(char *p)
274: {
275: char hex[3];
1.3 tedu 276: char *q;
1.1 schwarze 277: int c;
278:
279: hex[2] = '\0';
280:
1.3 tedu 281: q = p;
282: for ( ; '\0' != *p; p++, q++) {
1.1 schwarze 283: if ('%' == *p) {
284: if ('\0' == (hex[0] = *(p + 1)))
1.48 schwarze 285: return 0;
1.1 schwarze 286: if ('\0' == (hex[1] = *(p + 2)))
1.48 schwarze 287: return 0;
1.1 schwarze 288: if (1 != sscanf(hex, "%x", &c))
1.48 schwarze 289: return 0;
1.1 schwarze 290: if ('\0' == c)
1.48 schwarze 291: return 0;
1.1 schwarze 292:
1.3 tedu 293: *q = (char)c;
294: p += 2;
1.1 schwarze 295: } else
1.3 tedu 296: *q = '+' == *p ? ' ' : *p;
1.1 schwarze 297: }
298:
1.3 tedu 299: *q = '\0';
1.48 schwarze 300: return 1;
1.1 schwarze 301: }
302:
303: static void
304: resp_begin_http(int code, const char *msg)
305: {
306:
307: if (200 != code)
1.2 tedu 308: printf("Status: %d %s\r\n", code, msg);
1.1 schwarze 309:
1.2 tedu 310: printf("Content-Type: text/html; charset=utf-8\r\n"
311: "Cache-Control: no-cache\r\n"
312: "Pragma: no-cache\r\n"
313: "\r\n");
1.1 schwarze 314:
315: fflush(stdout);
316: }
317:
318: static void
1.53 schwarze 319: resp_copy(const char *filename)
320: {
321: char buf[4096];
322: ssize_t sz;
323: int fd;
324:
325: if ((fd = open(filename, O_RDONLY)) != -1) {
326: fflush(stdout);
327: while ((sz = read(fd, buf, sizeof(buf))) > 0)
328: write(STDOUT_FILENO, buf, sz);
329: }
330: }
331:
332: static void
1.1 schwarze 333: resp_begin_html(int code, const char *msg)
334: {
335:
336: resp_begin_http(code, msg);
337:
1.37 schwarze 338: printf("<!DOCTYPE html>\n"
1.1 schwarze 339: "<HTML>\n"
340: "<HEAD>\n"
1.37 schwarze 341: "<META CHARSET=\"UTF-8\" />\n"
1.52 schwarze 342: "<LINK REL=\"stylesheet\" HREF=\"%s/mandoc.css\""
1.1 schwarze 343: " TYPE=\"text/css\" media=\"all\">\n"
1.7 schwarze 344: "<TITLE>%s</TITLE>\n"
1.1 schwarze 345: "</HEAD>\n"
346: "<BODY>\n"
347: "<!-- Begin page content. //-->\n",
1.52 schwarze 348: CSS_DIR, CUSTOMIZE_TITLE);
1.53 schwarze 349:
350: resp_copy(MAN_DIR "/header.html");
1.1 schwarze 351: }
352:
353: static void
354: resp_end_html(void)
355: {
356:
1.53 schwarze 357: resp_copy(MAN_DIR "/footer.html");
358:
1.1 schwarze 359: puts("</BODY>\n"
360: "</HTML>");
361: }
362:
363: static void
364: resp_searchform(const struct req *req)
365: {
366: int i;
367:
368: puts("<!-- Begin search form. //-->");
369: printf("<DIV ID=\"mancgi\">\n"
1.6 schwarze 370: "<FORM ACTION=\"%s\" METHOD=\"get\">\n"
1.1 schwarze 371: "<FIELDSET>\n"
1.8 schwarze 372: "<LEGEND>Manual Page Search Parameters</LEGEND>\n",
1.1 schwarze 373: scriptname);
1.8 schwarze 374:
375: /* Write query input box. */
376:
377: printf( "<TABLE><TR><TD>\n"
378: "<INPUT TYPE=\"text\" NAME=\"query\" VALUE=\"");
1.25 schwarze 379: if (NULL != req->q.query)
380: html_print(req->q.query);
1.8 schwarze 381: puts("\" SIZE=\"40\">");
382:
383: /* Write submission and reset buttons. */
384:
385: printf( "<INPUT TYPE=\"submit\" VALUE=\"Submit\">\n"
386: "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n");
387:
388: /* Write show radio button */
389:
390: printf( "</TD><TD>\n"
391: "<INPUT TYPE=\"radio\" ");
1.5 schwarze 392: if (req->q.equal)
1.26 schwarze 393: printf("CHECKED=\"checked\" ");
1.8 schwarze 394: printf( "NAME=\"apropos\" ID=\"show\" VALUE=\"0\">\n"
395: "<LABEL FOR=\"show\">Show named manual page</LABEL>\n");
396:
397: /* Write section selector. */
398:
1.26 schwarze 399: puts( "</TD></TR><TR><TD>\n"
1.8 schwarze 400: "<SELECT NAME=\"sec\">");
401: for (i = 0; i < sec_MAX; i++) {
402: printf("<OPTION VALUE=\"%s\"", sec_numbers[i]);
403: if (NULL != req->q.sec &&
404: 0 == strcmp(sec_numbers[i], req->q.sec))
1.26 schwarze 405: printf(" SELECTED=\"selected\"");
1.8 schwarze 406: printf(">%s</OPTION>\n", sec_names[i]);
407: }
408: puts("</SELECT>");
409:
410: /* Write architecture selector. */
411:
1.21 schwarze 412: printf( "<SELECT NAME=\"arch\">\n"
413: "<OPTION VALUE=\"default\"");
414: if (NULL == req->q.arch)
1.26 schwarze 415: printf(" SELECTED=\"selected\"");
1.21 schwarze 416: puts(">All Architectures</OPTION>");
1.8 schwarze 417: for (i = 0; i < arch_MAX; i++) {
418: printf("<OPTION VALUE=\"%s\"", arch_names[i]);
419: if (NULL != req->q.arch &&
420: 0 == strcmp(arch_names[i], req->q.arch))
1.26 schwarze 421: printf(" SELECTED=\"selected\"");
1.8 schwarze 422: printf(">%s</OPTION>\n", arch_names[i]);
423: }
424: puts("</SELECT>");
425:
426: /* Write manpath selector. */
427:
1.1 schwarze 428: if (req->psz > 1) {
1.8 schwarze 429: puts("<SELECT NAME=\"manpath\">");
1.1 schwarze 430: for (i = 0; i < (int)req->psz; i++) {
431: printf("<OPTION ");
1.41 schwarze 432: if (strcmp(req->q.manpath, req->p[i]) == 0)
1.26 schwarze 433: printf("SELECTED=\"selected\" ");
1.1 schwarze 434: printf("VALUE=\"");
435: html_print(req->p[i]);
436: printf("\">");
437: html_print(req->p[i]);
438: puts("</OPTION>");
439: }
440: puts("</SELECT>");
441: }
1.8 schwarze 442:
443: /* Write search radio button */
444:
445: printf( "</TD><TD>\n"
446: "<INPUT TYPE=\"radio\" ");
447: if (0 == req->q.equal)
1.26 schwarze 448: printf("CHECKED=\"checked\" ");
1.8 schwarze 449: printf( "NAME=\"apropos\" ID=\"search\" VALUE=\"1\">\n"
450: "<LABEL FOR=\"search\">Search with apropos query</LABEL>\n");
451:
452: puts("</TD></TR></TABLE>\n"
1.1 schwarze 453: "</FIELDSET>\n"
454: "</FORM>\n"
455: "</DIV>");
456: puts("<!-- End search form. //-->");
457: }
458:
1.16 schwarze 459: static int
1.20 schwarze 460: validate_urifrag(const char *frag)
461: {
462:
463: while ('\0' != *frag) {
464: if ( ! (isalnum((unsigned char)*frag) ||
465: '-' == *frag || '.' == *frag ||
466: '/' == *frag || '_' == *frag))
1.48 schwarze 467: return 0;
1.20 schwarze 468: frag++;
469: }
1.48 schwarze 470: return 1;
1.20 schwarze 471: }
472:
473: static int
1.17 schwarze 474: validate_manpath(const struct req *req, const char* manpath)
475: {
476: size_t i;
477:
478: if ( ! strcmp(manpath, "mandoc"))
1.48 schwarze 479: return 1;
1.17 schwarze 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:
503: resp_begin_html(200, NULL);
504: resp_searchform(req);
1.4 schwarze 505: printf("<P>\n"
1.26 schwarze 506: "This web interface is documented in the\n"
507: "<A HREF=\"%s/mandoc/man8/man.cgi.8\">man.cgi</A>\n"
508: "manual, and the\n"
509: "<A HREF=\"%s/mandoc/man1/apropos.1\">apropos</A>\n"
1.9 schwarze 510: "manual explains the query syntax.\n"
1.4 schwarze 511: "</P>\n",
512: scriptname, scriptname);
1.1 schwarze 513: resp_end_html();
514: }
515:
516: static void
1.12 schwarze 517: pg_noresult(const struct req *req, const char *msg)
1.1 schwarze 518: {
519: resp_begin_html(200, NULL);
520: resp_searchform(req);
521: puts("<P>");
522: puts(msg);
523: puts("</P>");
524: resp_end_html();
525: }
526:
527: static void
1.12 schwarze 528: pg_error_badrequest(const char *msg)
1.1 schwarze 529: {
530:
531: resp_begin_html(400, "Bad Request");
532: puts("<H1>Bad Request</H1>\n"
533: "<P>\n");
534: puts(msg);
535: printf("Try again from the\n"
536: "<A HREF=\"%s\">main page</A>.\n"
537: "</P>", scriptname);
538: resp_end_html();
539: }
540:
541: static void
1.12 schwarze 542: pg_error_internal(void)
1.1 schwarze 543: {
544: resp_begin_html(500, "Internal Server Error");
545: puts("<P>Internal Server Error</P>");
546: resp_end_html();
547: }
548:
549: static void
1.12 schwarze 550: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1 schwarze 551: {
1.21 schwarze 552: char *arch, *archend;
1.10 schwarze 553: size_t i, iuse, isec;
1.21 schwarze 554: int archprio, archpriouse;
1.10 schwarze 555: int prio, priouse;
556: char sec;
1.1 schwarze 557:
1.16 schwarze 558: for (i = 0; i < sz; i++) {
559: if (validate_filename(r[i].file))
560: continue;
561: fprintf(stderr, "invalid filename %s in %s database\n",
562: r[i].file, req->q.manpath);
563: pg_error_internal();
564: return;
565: }
566:
1.1 schwarze 567: if (1 == sz) {
568: /*
569: * If we have just one result, then jump there now
570: * without any delay.
571: */
1.2 tedu 572: printf("Status: 303 See Other\r\n");
1.55 ! bentley 573: printf("Location: http://%s%s/%s/%s",
1.18 schwarze 574: HTTP_HOST, scriptname, req->q.manpath, r[0].file);
1.2 tedu 575: printf("\r\n"
576: "Content-Type: text/html; charset=utf-8\r\n"
577: "\r\n");
1.1 schwarze 578: return;
579: }
580:
581: resp_begin_html(200, NULL);
582: resp_searchform(req);
583: puts("<DIV CLASS=\"results\">");
584: puts("<TABLE>");
585:
586: for (i = 0; i < sz; i++) {
587: printf("<TR>\n"
588: "<TD CLASS=\"title\">\n"
1.55 ! bentley 589: "<A HREF=\"%s/%s/%s",
1.1 schwarze 590: scriptname, req->q.manpath, r[i].file);
591: printf("\">");
592: html_print(r[i].names);
593: printf("</A>\n"
594: "</TD>\n"
595: "<TD CLASS=\"desc\">");
596: html_print(r[i].output);
597: puts("</TD>\n"
598: "</TR>");
599: }
600:
601: puts("</TABLE>\n"
602: "</DIV>");
1.10 schwarze 603:
604: /*
605: * In man(1) mode, show one of the pages
606: * even if more than one is found.
607: */
608:
609: if (req->q.equal) {
610: puts("<HR>");
611: iuse = 0;
612: priouse = 10;
1.21 schwarze 613: archpriouse = 3;
1.10 schwarze 614: for (i = 0; i < sz; i++) {
615: isec = strcspn(r[i].file, "123456789");
616: sec = r[i].file[isec];
617: if ('\0' == sec)
618: continue;
619: prio = sec_prios[sec - '1'];
1.21 schwarze 620: if (NULL == req->q.arch) {
621: archprio =
622: (NULL == (arch = strchr(
623: r[i].file + isec, '/'))) ? 3 :
624: (NULL == (archend = strchr(
625: arch + 1, '/'))) ? 0 :
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
649: catman(const struct req *req, const char *file)
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.10 schwarze 659: puts("<P>You specified an invalid manual file.</P>");
1.1 schwarze 660: return;
661: }
662:
663: puts("<DIV CLASS=\"catman\">\n"
664: "<PRE>");
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)
690: printf("</I>");
691: if (bold)
692: printf("</B>");
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)
703: printf("</B>");
704: if ( ! italic)
705: printf("<I>");
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)
727: printf("</I>");
728: if (bold)
729: printf("</B>");
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)
741: printf("</I>");
742: if (bold)
743: printf("</B>");
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)
753: printf("</I>");
754: if ( ! bold)
755: printf("<B>");
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)
768: printf("</I>");
769: if (bold)
770: printf("</B>");
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:
779: puts("</PRE>\n"
1.10 schwarze 780: "</DIV>");
1.1 schwarze 781:
782: fclose(f);
783: }
784:
785: static void
786: format(const struct req *req, const char *file)
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.10 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.45 schwarze 808: mandoc_asprintf(&conf.man, "%s?query=%%N&sec=%%S%s%s%s%s",
1.30 schwarze 809: scriptname,
810: req->q.arch ? "&arch=" : "",
811: req->q.arch ? req->q.arch : "",
812: usepath ? "&manpath=" : "",
813: usepath ? req->q.manpath : "");
1.1 schwarze 814:
1.47 schwarze 815: mparse_result(mp, &man, NULL);
816: if (man == NULL) {
1.1 schwarze 817: fprintf(stderr, "fatal mandoc error: %s/%s\n",
818: req->q.manpath, file);
1.12 schwarze 819: pg_error_internal();
1.1 schwarze 820: mparse_free(mp);
1.49 schwarze 821: mchars_free();
1.1 schwarze 822: return;
823: }
824:
1.49 schwarze 825: vp = html_alloc(&conf);
1.1 schwarze 826:
1.50 schwarze 827: if (man->macroset == MACROSET_MDOC) {
828: mdoc_validate(man);
1.47 schwarze 829: html_mdoc(vp, man);
1.51 schwarze 830: } else {
831: man_validate(man);
1.1 schwarze 832: html_man(vp, man);
1.51 schwarze 833: }
1.1 schwarze 834:
835: html_free(vp);
836: mparse_free(mp);
1.49 schwarze 837: mchars_free();
1.45 schwarze 838: free(conf.man);
1.1 schwarze 839: }
840:
841: static void
1.10 schwarze 842: resp_show(const struct req *req, const char *file)
843: {
1.16 schwarze 844:
845: if ('.' == file[0] && '/' == file[1])
1.11 schwarze 846: file += 2;
1.10 schwarze 847:
848: if ('c' == *file)
849: catman(req, file);
850: else
851: format(req, file);
852: }
853:
854: static void
1.24 schwarze 855: pg_show(struct req *req, const char *fullpath)
1.1 schwarze 856: {
1.24 schwarze 857: char *manpath;
858: const char *file;
1.1 schwarze 859:
1.24 schwarze 860: if ((file = strchr(fullpath, '/')) == NULL) {
1.12 schwarze 861: pg_error_badrequest(
1.1 schwarze 862: "You did not specify a page to show.");
863: return;
1.43 schwarze 864: }
1.24 schwarze 865: manpath = mandoc_strndup(fullpath, file - fullpath);
866: file++;
1.1 schwarze 867:
1.24 schwarze 868: if ( ! validate_manpath(req, manpath)) {
1.17 schwarze 869: pg_error_badrequest(
870: "You specified an invalid manpath.");
1.24 schwarze 871: free(manpath);
1.17 schwarze 872: return;
873: }
874:
1.1 schwarze 875: /*
876: * Begin by chdir()ing into the manpath.
877: * This way we can pick up the database files, which are
878: * relative to the manpath root.
879: */
880:
1.24 schwarze 881: if (chdir(manpath) == -1) {
1.17 schwarze 882: fprintf(stderr, "chdir %s: %s\n",
1.24 schwarze 883: manpath, strerror(errno));
1.17 schwarze 884: pg_error_internal();
1.24 schwarze 885: free(manpath);
1.16 schwarze 886: return;
887: }
888:
1.24 schwarze 889: if (strcmp(manpath, "mandoc")) {
890: free(req->q.manpath);
891: req->q.manpath = manpath;
892: } else
893: free(manpath);
894:
895: if ( ! validate_filename(file)) {
1.16 schwarze 896: pg_error_badrequest(
897: "You specified an invalid manual file.");
1.1 schwarze 898: return;
899: }
1.19 schwarze 900:
1.10 schwarze 901: resp_begin_html(200, NULL);
902: resp_searchform(req);
1.24 schwarze 903: resp_show(req, file);
1.10 schwarze 904: resp_end_html();
1.1 schwarze 905: }
906:
907: static void
1.6 schwarze 908: pg_search(const struct req *req)
1.1 schwarze 909: {
910: struct mansearch search;
911: struct manpaths paths;
912: struct manpage *res;
1.36 schwarze 913: char **argv;
914: char *query, *rp, *wp;
1.1 schwarze 915: size_t ressz;
1.36 schwarze 916: int argc;
1.1 schwarze 917:
918: /*
919: * Begin by chdir()ing into the root of the manpath.
920: * This way we can pick up the database files, which are
921: * relative to the manpath root.
922: */
923:
924: if (-1 == (chdir(req->q.manpath))) {
1.17 schwarze 925: fprintf(stderr, "chdir %s: %s\n",
926: req->q.manpath, strerror(errno));
927: pg_error_internal();
1.1 schwarze 928: return;
929: }
930:
931: search.arch = req->q.arch;
932: search.sec = req->q.sec;
1.35 schwarze 933: search.outkey = "Nd";
934: search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.40 schwarze 935: search.firstmatch = 1;
1.1 schwarze 936:
937: paths.sz = 1;
938: paths.paths = mandoc_malloc(sizeof(char *));
939: paths.paths[0] = mandoc_strdup(".");
940:
941: /*
1.36 schwarze 942: * Break apart at spaces with backslash-escaping.
1.1 schwarze 943: */
944:
1.36 schwarze 945: argc = 0;
946: argv = NULL;
947: rp = query = mandoc_strdup(req->q.query);
948: for (;;) {
949: while (isspace((unsigned char)*rp))
950: rp++;
951: if (*rp == '\0')
952: break;
953: argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
954: argv[argc++] = wp = rp;
955: for (;;) {
956: if (isspace((unsigned char)*rp)) {
957: *wp = '\0';
958: rp++;
959: break;
960: }
961: if (rp[0] == '\\' && rp[1] != '\0')
962: rp++;
963: if (wp != rp)
964: *wp = *rp;
965: if (*rp == '\0')
966: break;
967: wp++;
968: rp++;
969: }
1.1 schwarze 970: }
971:
1.36 schwarze 972: if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz))
1.12 schwarze 973: pg_noresult(req, "You entered an invalid query.");
1.1 schwarze 974: else if (0 == ressz)
1.12 schwarze 975: pg_noresult(req, "No results found.");
1.1 schwarze 976: else
1.12 schwarze 977: pg_searchres(req, res, ressz);
1.1 schwarze 978:
1.36 schwarze 979: free(query);
980: mansearch_free(res, ressz);
1.1 schwarze 981: free(paths.paths[0]);
982: free(paths.paths);
983: }
984:
985: int
986: main(void)
987: {
1.6 schwarze 988: struct req req;
1.33 schwarze 989: struct itimerval itimer;
1.6 schwarze 990: const char *path;
1.23 schwarze 991: const char *querystring;
1.1 schwarze 992: int i;
1.33 schwarze 993:
994: /* Poor man's ReDoS mitigation. */
995:
1.38 schwarze 996: itimer.it_value.tv_sec = 2;
1.33 schwarze 997: itimer.it_value.tv_usec = 0;
1.38 schwarze 998: itimer.it_interval.tv_sec = 2;
1.33 schwarze 999: itimer.it_interval.tv_usec = 0;
1000: if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1001: fprintf(stderr, "setitimer: %s\n", strerror(errno));
1002: pg_error_internal();
1.48 schwarze 1003: return EXIT_FAILURE;
1.33 schwarze 1004: }
1.1 schwarze 1005:
1006: /* Scan our run-time environment. */
1007:
1008: if (NULL == (scriptname = getenv("SCRIPT_NAME")))
1009: scriptname = "";
1010:
1.20 schwarze 1011: if ( ! validate_urifrag(scriptname)) {
1012: fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n",
1013: scriptname);
1014: pg_error_internal();
1.48 schwarze 1015: return EXIT_FAILURE;
1.20 schwarze 1016: }
1017:
1.1 schwarze 1018: /*
1.7 schwarze 1019: * First we change directory into the MAN_DIR so that
1.1 schwarze 1020: * subsequent scanning for manpath directories is rooted
1021: * relative to the same position.
1022: */
1023:
1.7 schwarze 1024: if (-1 == chdir(MAN_DIR)) {
1.1 schwarze 1025: fprintf(stderr, "MAN_DIR: %s: %s\n",
1.7 schwarze 1026: MAN_DIR, strerror(errno));
1.12 schwarze 1027: pg_error_internal();
1.48 schwarze 1028: return EXIT_FAILURE;
1.43 schwarze 1029: }
1.1 schwarze 1030:
1031: memset(&req, 0, sizeof(struct req));
1032: pathgen(&req);
1033:
1034: /* Next parse out the query string. */
1035:
1036: if (NULL != (querystring = getenv("QUERY_STRING")))
1037: http_parse(&req, querystring);
1.17 schwarze 1038:
1.41 schwarze 1039: if (req.q.manpath == NULL)
1040: req.q.manpath = mandoc_strdup(req.p[0]);
1041: else if ( ! validate_manpath(&req, req.q.manpath)) {
1.17 schwarze 1042: pg_error_badrequest(
1043: "You specified an invalid manpath.");
1.48 schwarze 1044: return EXIT_FAILURE;
1.17 schwarze 1045: }
1.1 schwarze 1046:
1.20 schwarze 1047: if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
1048: pg_error_badrequest(
1049: "You specified an invalid architecture.");
1.48 schwarze 1050: return EXIT_FAILURE;
1.20 schwarze 1051: }
1052:
1.6 schwarze 1053: /* Dispatch to the three different pages. */
1.1 schwarze 1054:
1.6 schwarze 1055: path = getenv("PATH_INFO");
1056: if (NULL == path)
1057: path = "";
1058: else if ('/' == *path)
1059: path++;
1060:
1061: if ('\0' != *path)
1062: pg_show(&req, path);
1.25 schwarze 1063: else if (NULL != req.q.query)
1.6 schwarze 1064: pg_search(&req);
1065: else
1.12 schwarze 1066: pg_index(&req);
1.1 schwarze 1067:
1.23 schwarze 1068: free(req.q.manpath);
1069: free(req.q.arch);
1070: free(req.q.sec);
1.25 schwarze 1071: free(req.q.query);
1.1 schwarze 1072: for (i = 0; i < (int)req.psz; i++)
1073: free(req.p[i]);
1074: free(req.p);
1.48 schwarze 1075: return EXIT_SUCCESS;
1.1 schwarze 1076: }
1077:
1078: /*
1079: * Scan for indexable paths.
1080: */
1081: static void
1082: pathgen(struct req *req)
1083: {
1084: FILE *fp;
1085: char *dp;
1086: size_t dpsz;
1.54 schwarze 1087: ssize_t len;
1.1 schwarze 1088:
1.14 schwarze 1089: if (NULL == (fp = fopen("manpath.conf", "r"))) {
1090: fprintf(stderr, "%s/manpath.conf: %s\n",
1091: MAN_DIR, strerror(errno));
1092: pg_error_internal();
1093: exit(EXIT_FAILURE);
1094: }
1.1 schwarze 1095:
1.54 schwarze 1096: dp = NULL;
1097: dpsz = 0;
1098:
1099: while ((len = getline(&dp, &dpsz, fp)) != -1) {
1100: if (dp[len - 1] == '\n')
1101: dp[--len] = '\0';
1.1 schwarze 1102: req->p = mandoc_realloc(req->p,
1103: (req->psz + 1) * sizeof(char *));
1.20 schwarze 1104: if ( ! validate_urifrag(dp)) {
1105: fprintf(stderr, "%s/manpath.conf contains "
1106: "unsafe path \"%s\"\n", MAN_DIR, dp);
1107: pg_error_internal();
1108: exit(EXIT_FAILURE);
1109: }
1110: if (NULL != strchr(dp, '/')) {
1111: fprintf(stderr, "%s/manpath.conf contains "
1112: "path with slash \"%s\"\n", MAN_DIR, dp);
1113: pg_error_internal();
1114: exit(EXIT_FAILURE);
1115: }
1116: req->p[req->psz++] = dp;
1.54 schwarze 1117: dp = NULL;
1118: dpsz = 0;
1.14 schwarze 1119: }
1.54 schwarze 1120: free(dp);
1.14 schwarze 1121:
1122: if ( req->p == NULL ) {
1123: fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR);
1124: pg_error_internal();
1125: exit(EXIT_FAILURE);
1.1 schwarze 1126: }
1127: }