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