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