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