Annotation of src/usr.bin/mandoc/cgi.c, Revision 1.30
1.30 ! schwarze 1: /* $Id: cgi.c,v 1.29 2014/07/25 19:36:02 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: struct mdoc *mdoc;
851: struct man *man;
852: void *vp;
1.30 ! schwarze 853: char *opts;
1.1 schwarze 854: enum mandoclevel rc;
1.30 ! schwarze 855: int fd;
! 856: int usepath;
1.1 schwarze 857:
858: if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.10 schwarze 859: puts("<P>You specified an invalid manual file.</P>");
1.1 schwarze 860: return;
861: }
862:
863: mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL,
864: req->q.manpath);
865: rc = mparse_readfd(mp, fd, file);
866: close(fd);
867:
868: if (rc >= MANDOCLEVEL_FATAL) {
869: fprintf(stderr, "fatal mandoc error: %s/%s\n",
870: req->q.manpath, file);
1.12 schwarze 871: pg_error_internal();
1.1 schwarze 872: return;
873: }
874:
1.30 ! schwarze 875: usepath = strcmp(req->q.manpath, req->p[0]);
! 876: mandoc_asprintf(&opts,
! 877: "fragment,man=%s?query=%%N&sec=%%S%s%s%s%s",
! 878: scriptname,
! 879: req->q.arch ? "&arch=" : "",
! 880: req->q.arch ? req->q.arch : "",
! 881: usepath ? "&manpath=" : "",
! 882: usepath ? req->q.manpath : "");
1.1 schwarze 883:
884: mparse_result(mp, &mdoc, &man, NULL);
885: if (NULL == man && NULL == mdoc) {
886: fprintf(stderr, "fatal mandoc error: %s/%s\n",
887: req->q.manpath, file);
1.12 schwarze 888: pg_error_internal();
1.1 schwarze 889: mparse_free(mp);
890: return;
891: }
892:
893: vp = html_alloc(opts);
894:
895: if (NULL != mdoc)
896: html_mdoc(vp, mdoc);
897: else
898: html_man(vp, man);
899:
900: html_free(vp);
901: mparse_free(mp);
1.30 ! schwarze 902: free(opts);
1.1 schwarze 903: }
904:
905: static void
1.10 schwarze 906: resp_show(const struct req *req, const char *file)
907: {
1.16 schwarze 908:
909: if ('.' == file[0] && '/' == file[1])
1.11 schwarze 910: file += 2;
1.10 schwarze 911:
912: if ('c' == *file)
913: catman(req, file);
914: else
915: format(req, file);
916: }
917:
918: static void
1.24 schwarze 919: pg_show(struct req *req, const char *fullpath)
1.1 schwarze 920: {
1.24 schwarze 921: char *manpath;
922: const char *file;
1.1 schwarze 923:
1.24 schwarze 924: if ((file = strchr(fullpath, '/')) == NULL) {
1.12 schwarze 925: pg_error_badrequest(
1.1 schwarze 926: "You did not specify a page to show.");
927: return;
928: }
1.24 schwarze 929: manpath = mandoc_strndup(fullpath, file - fullpath);
930: file++;
1.1 schwarze 931:
1.24 schwarze 932: if ( ! validate_manpath(req, manpath)) {
1.17 schwarze 933: pg_error_badrequest(
934: "You specified an invalid manpath.");
1.24 schwarze 935: free(manpath);
1.17 schwarze 936: return;
937: }
938:
1.1 schwarze 939: /*
940: * Begin by chdir()ing into the manpath.
941: * This way we can pick up the database files, which are
942: * relative to the manpath root.
943: */
944:
1.24 schwarze 945: if (chdir(manpath) == -1) {
1.17 schwarze 946: fprintf(stderr, "chdir %s: %s\n",
1.24 schwarze 947: manpath, strerror(errno));
1.17 schwarze 948: pg_error_internal();
1.24 schwarze 949: free(manpath);
1.16 schwarze 950: return;
951: }
952:
1.24 schwarze 953: if (strcmp(manpath, "mandoc")) {
954: free(req->q.manpath);
955: req->q.manpath = manpath;
956: } else
957: free(manpath);
958:
959: if ( ! validate_filename(file)) {
1.16 schwarze 960: pg_error_badrequest(
961: "You specified an invalid manual file.");
1.1 schwarze 962: return;
963: }
1.19 schwarze 964:
1.10 schwarze 965: resp_begin_html(200, NULL);
966: resp_searchform(req);
1.24 schwarze 967: resp_show(req, file);
1.10 schwarze 968: resp_end_html();
1.1 schwarze 969: }
970:
971: static void
1.6 schwarze 972: pg_search(const struct req *req)
1.1 schwarze 973: {
974: struct mansearch search;
975: struct manpaths paths;
976: struct manpage *res;
977: char **cp;
978: const char *ep, *start;
979: size_t ressz;
980: int i, sz;
981:
982: /*
983: * Begin by chdir()ing into the root of the manpath.
984: * This way we can pick up the database files, which are
985: * relative to the manpath root.
986: */
987:
988: if (-1 == (chdir(req->q.manpath))) {
1.17 schwarze 989: fprintf(stderr, "chdir %s: %s\n",
990: req->q.manpath, strerror(errno));
991: pg_error_internal();
1.1 schwarze 992: return;
993: }
994:
995: search.arch = req->q.arch;
996: search.sec = req->q.sec;
1.5 schwarze 997: search.deftype = req->q.equal ? TYPE_Nm : (TYPE_Nm | TYPE_Nd);
998: search.flags = req->q.equal ? MANSEARCH_MAN : 0;
1.1 schwarze 999:
1000: paths.sz = 1;
1001: paths.paths = mandoc_malloc(sizeof(char *));
1002: paths.paths[0] = mandoc_strdup(".");
1003:
1004: /*
1005: * Poor man's tokenisation: just break apart by spaces.
1006: * Yes, this is half-ass. But it works for now.
1007: */
1008:
1.25 schwarze 1009: ep = req->q.query;
1.1 schwarze 1010: while (ep && isspace((unsigned char)*ep))
1011: ep++;
1012:
1013: sz = 0;
1014: cp = NULL;
1015: while (ep && '\0' != *ep) {
1016: cp = mandoc_reallocarray(cp, sz + 1, sizeof(char *));
1017: start = ep;
1018: while ('\0' != *ep && ! isspace((unsigned char)*ep))
1019: ep++;
1020: cp[sz] = mandoc_malloc((ep - start) + 1);
1021: memcpy(cp[sz], start, ep - start);
1022: cp[sz++][ep - start] = '\0';
1023: while (isspace((unsigned char)*ep))
1024: ep++;
1025: }
1026:
1027: if (0 == mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))
1.12 schwarze 1028: pg_noresult(req, "You entered an invalid query.");
1.1 schwarze 1029: else if (0 == ressz)
1.12 schwarze 1030: pg_noresult(req, "No results found.");
1.1 schwarze 1031: else
1.12 schwarze 1032: pg_searchres(req, res, ressz);
1.1 schwarze 1033:
1034: for (i = 0; i < sz; i++)
1035: free(cp[i]);
1036: free(cp);
1037:
1038: for (i = 0; i < (int)ressz; i++) {
1039: free(res[i].file);
1040: free(res[i].names);
1041: free(res[i].output);
1042: }
1043: free(res);
1044:
1045: free(paths.paths[0]);
1046: free(paths.paths);
1047: }
1048:
1049: int
1050: main(void)
1051: {
1.6 schwarze 1052: struct req req;
1053: const char *path;
1.23 schwarze 1054: const char *querystring;
1.1 schwarze 1055: int i;
1056:
1057: /* Scan our run-time environment. */
1058:
1059: if (NULL == (scriptname = getenv("SCRIPT_NAME")))
1060: scriptname = "";
1061:
1.20 schwarze 1062: if ( ! validate_urifrag(scriptname)) {
1063: fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n",
1064: scriptname);
1065: pg_error_internal();
1066: return(EXIT_FAILURE);
1067: }
1068:
1.1 schwarze 1069: /*
1.7 schwarze 1070: * First we change directory into the MAN_DIR so that
1.1 schwarze 1071: * subsequent scanning for manpath directories is rooted
1072: * relative to the same position.
1073: */
1074:
1.7 schwarze 1075: if (-1 == chdir(MAN_DIR)) {
1.1 schwarze 1076: fprintf(stderr, "MAN_DIR: %s: %s\n",
1.7 schwarze 1077: MAN_DIR, strerror(errno));
1.12 schwarze 1078: pg_error_internal();
1.1 schwarze 1079: return(EXIT_FAILURE);
1080: }
1081:
1082: memset(&req, 0, sizeof(struct req));
1083: pathgen(&req);
1084:
1085: /* Next parse out the query string. */
1086:
1087: if (NULL != (querystring = getenv("QUERY_STRING")))
1088: http_parse(&req, querystring);
1.17 schwarze 1089:
1.29 schwarze 1090: if ( ! (NULL == req.q.manpath ||
1091: validate_manpath(&req, req.q.manpath))) {
1.17 schwarze 1092: pg_error_badrequest(
1093: "You specified an invalid manpath.");
1094: return(EXIT_FAILURE);
1095: }
1.1 schwarze 1096:
1.20 schwarze 1097: if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
1098: pg_error_badrequest(
1099: "You specified an invalid architecture.");
1100: return(EXIT_FAILURE);
1101: }
1102:
1.6 schwarze 1103: /* Dispatch to the three different pages. */
1.1 schwarze 1104:
1.6 schwarze 1105: path = getenv("PATH_INFO");
1106: if (NULL == path)
1107: path = "";
1108: else if ('/' == *path)
1109: path++;
1110:
1111: if ('\0' != *path)
1112: pg_show(&req, path);
1.25 schwarze 1113: else if (NULL != req.q.query)
1.6 schwarze 1114: pg_search(&req);
1115: else
1.12 schwarze 1116: pg_index(&req);
1.1 schwarze 1117:
1.23 schwarze 1118: free(req.q.manpath);
1119: free(req.q.arch);
1120: free(req.q.sec);
1.25 schwarze 1121: free(req.q.query);
1.1 schwarze 1122: for (i = 0; i < (int)req.psz; i++)
1123: free(req.p[i]);
1124: free(req.p);
1125: return(EXIT_SUCCESS);
1126: }
1127:
1128: /*
1129: * Scan for indexable paths.
1130: */
1131: static void
1132: pathgen(struct req *req)
1133: {
1134: FILE *fp;
1135: char *dp;
1136: size_t dpsz;
1137:
1.14 schwarze 1138: if (NULL == (fp = fopen("manpath.conf", "r"))) {
1139: fprintf(stderr, "%s/manpath.conf: %s\n",
1140: MAN_DIR, strerror(errno));
1141: pg_error_internal();
1142: exit(EXIT_FAILURE);
1143: }
1.1 schwarze 1144:
1145: while (NULL != (dp = fgetln(fp, &dpsz))) {
1146: if ('\n' == dp[dpsz - 1])
1147: dpsz--;
1148: req->p = mandoc_realloc(req->p,
1149: (req->psz + 1) * sizeof(char *));
1.20 schwarze 1150: dp = mandoc_strndup(dp, dpsz);
1151: if ( ! validate_urifrag(dp)) {
1152: fprintf(stderr, "%s/manpath.conf contains "
1153: "unsafe path \"%s\"\n", MAN_DIR, dp);
1154: pg_error_internal();
1155: exit(EXIT_FAILURE);
1156: }
1157: if (NULL != strchr(dp, '/')) {
1158: fprintf(stderr, "%s/manpath.conf contains "
1159: "path with slash \"%s\"\n", MAN_DIR, dp);
1160: pg_error_internal();
1161: exit(EXIT_FAILURE);
1162: }
1163: req->p[req->psz++] = dp;
1.14 schwarze 1164: }
1165:
1166: if ( req->p == NULL ) {
1167: fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR);
1168: pg_error_internal();
1169: exit(EXIT_FAILURE);
1.1 schwarze 1170: }
1171: }