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