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