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