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