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