Annotation of mandoc/cgi.c, Revision 1.115
1.115 ! schwarze 1: /* $Id: cgi.c,v 1.114 2015/11/05 20:55:41 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;
1.115 ! schwarze 707: char *p;
! 708: size_t sz;
! 709: ssize_t len;
1.10 kristaps 710: int i;
711: int italic, bold;
1.9 kristaps 712:
1.115 ! schwarze 713: if ((f = fopen(file, "r")) == NULL) {
1.70 schwarze 714: puts("<P>You specified an invalid manual file.</P>");
1.9 kristaps 715: return;
716: }
717:
1.32 kristaps 718: puts("<DIV CLASS=\"catman\">\n"
719: "<PRE>");
1.10 kristaps 720:
1.115 ! schwarze 721: p = NULL;
! 722: sz = 0;
! 723:
! 724: while ((len = getline(&p, &sz, f)) != -1) {
1.10 kristaps 725: bold = italic = 0;
1.115 ! schwarze 726: for (i = 0; i < len - 1; i++) {
1.104 schwarze 727: /*
1.10 kristaps 728: * This means that the catpage is out of state.
729: * Ignore it and keep going (although the
730: * catpage is bogus).
731: */
732:
733: if ('\b' == p[i] || '\n' == p[i])
734: continue;
735:
736: /*
737: * Print a regular character.
738: * Close out any bold/italic scopes.
739: * If we're in back-space mode, make sure we'll
740: * have something to enter when we backspace.
741: */
742:
743: if ('\b' != p[i + 1]) {
744: if (italic)
745: printf("</I>");
746: if (bold)
747: printf("</B>");
748: italic = bold = 0;
749: html_putchar(p[i]);
750: continue;
1.115 ! schwarze 751: } else if (i + 2 >= len)
1.10 kristaps 752: continue;
753:
754: /* Italic mode. */
755:
756: if ('_' == p[i]) {
757: if (bold)
758: printf("</B>");
759: if ( ! italic)
760: printf("<I>");
761: bold = 0;
762: italic = 1;
763: i += 2;
764: html_putchar(p[i]);
765: continue;
766: }
767:
1.104 schwarze 768: /*
1.10 kristaps 769: * Handle funny behaviour troff-isms.
770: * These grok'd from the original man2html.c.
771: */
772:
773: if (('+' == p[i] && 'o' == p[i + 2]) ||
774: ('o' == p[i] && '+' == p[i + 2]) ||
775: ('|' == p[i] && '=' == p[i + 2]) ||
776: ('=' == p[i] && '|' == p[i + 2]) ||
777: ('*' == p[i] && '=' == p[i + 2]) ||
778: ('=' == p[i] && '*' == p[i + 2]) ||
779: ('*' == p[i] && '|' == p[i + 2]) ||
780: ('|' == p[i] && '*' == p[i + 2])) {
781: if (italic)
782: printf("</I>");
783: if (bold)
784: printf("</B>");
785: italic = bold = 0;
786: putchar('*');
787: i += 2;
788: continue;
789: } else if (('|' == p[i] && '-' == p[i + 2]) ||
790: ('-' == p[i] && '|' == p[i + 1]) ||
791: ('+' == p[i] && '-' == p[i + 1]) ||
792: ('-' == p[i] && '+' == p[i + 1]) ||
793: ('+' == p[i] && '|' == p[i + 1]) ||
794: ('|' == p[i] && '+' == p[i + 1])) {
795: if (italic)
796: printf("</I>");
797: if (bold)
798: printf("</B>");
799: italic = bold = 0;
800: putchar('+');
801: i += 2;
802: continue;
803: }
804:
805: /* Bold mode. */
1.104 schwarze 806:
1.10 kristaps 807: if (italic)
808: printf("</I>");
809: if ( ! bold)
810: printf("<B>");
811: bold = 1;
812: italic = 0;
813: i += 2;
814: html_putchar(p[i]);
815: }
816:
1.104 schwarze 817: /*
1.10 kristaps 818: * Clean up the last character.
1.104 schwarze 819: * We can get to a newline; don't print that.
1.10 kristaps 820: */
1.9 kristaps 821:
1.10 kristaps 822: if (italic)
823: printf("</I>");
824: if (bold)
825: printf("</B>");
1.9 kristaps 826:
1.115 ! schwarze 827: if (i == len - 1 && p[i] != '\n')
1.10 kristaps 828: html_putchar(p[i]);
1.9 kristaps 829:
1.10 kristaps 830: putchar('\n');
831: }
1.115 ! schwarze 832: free(p);
1.10 kristaps 833:
834: puts("</PRE>\n"
1.70 schwarze 835: "</DIV>");
1.10 kristaps 836:
837: fclose(f);
1.9 kristaps 838: }
839:
840: static void
1.32 kristaps 841: format(const struct req *req, const char *file)
1.7 kristaps 842: {
1.106 schwarze 843: struct manoutput conf;
1.8 kristaps 844: struct mparse *mp;
1.107 schwarze 845: struct roff_man *man;
1.8 kristaps 846: void *vp;
1.90 schwarze 847: int fd;
848: int usepath;
1.7 kristaps 849:
1.8 kristaps 850: if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.70 schwarze 851: puts("<P>You specified an invalid manual file.</P>");
1.7 kristaps 852: return;
853: }
854:
1.110 schwarze 855: mchars_alloc();
856: mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, req->q.manpath);
1.103 schwarze 857: mparse_readfd(mp, fd, file);
1.8 kristaps 858: close(fd);
1.7 kristaps 859:
1.106 schwarze 860: memset(&conf, 0, sizeof(conf));
861: conf.fragment = 1;
1.90 schwarze 862: usepath = strcmp(req->q.manpath, req->p[0]);
1.106 schwarze 863: mandoc_asprintf(&conf.man, "%s?query=%%N&sec=%%S%s%s%s%s",
1.90 schwarze 864: scriptname,
865: req->q.arch ? "&arch=" : "",
866: req->q.arch ? req->q.arch : "",
867: usepath ? "&manpath=" : "",
868: usepath ? req->q.manpath : "");
1.10 kristaps 869:
1.108 schwarze 870: mparse_result(mp, &man, NULL);
871: if (man == NULL) {
1.59 schwarze 872: fprintf(stderr, "fatal mandoc error: %s/%s\n",
873: req->q.manpath, file);
1.72 schwarze 874: pg_error_internal();
1.32 kristaps 875: mparse_free(mp);
1.110 schwarze 876: mchars_free();
1.32 kristaps 877: return;
878: }
879:
1.110 schwarze 880: vp = html_alloc(&conf);
1.7 kristaps 881:
1.111 schwarze 882: if (man->macroset == MACROSET_MDOC) {
883: mdoc_validate(man);
1.108 schwarze 884: html_mdoc(vp, man);
1.112 schwarze 885: } else {
886: man_validate(man);
1.8 kristaps 887: html_man(vp, man);
1.112 schwarze 888: }
1.32 kristaps 889:
1.8 kristaps 890: html_free(vp);
891: mparse_free(mp);
1.110 schwarze 892: mchars_free();
1.106 schwarze 893: free(conf.man);
1.7 kristaps 894: }
895:
896: static void
1.70 schwarze 897: resp_show(const struct req *req, const char *file)
898: {
1.76 schwarze 899:
900: if ('.' == file[0] && '/' == file[1])
1.71 schwarze 901: file += 2;
1.70 schwarze 902:
903: if ('c' == *file)
904: catman(req, file);
905: else
906: format(req, file);
907: }
908:
909: static void
1.84 schwarze 910: pg_show(struct req *req, const char *fullpath)
1.1 kristaps 911: {
1.84 schwarze 912: char *manpath;
913: const char *file;
1.25 kristaps 914:
1.84 schwarze 915: if ((file = strchr(fullpath, '/')) == NULL) {
1.72 schwarze 916: pg_error_badrequest(
1.59 schwarze 917: "You did not specify a page to show.");
1.25 kristaps 918: return;
1.104 schwarze 919: }
1.84 schwarze 920: manpath = mandoc_strndup(fullpath, file - fullpath);
921: file++;
1.6 kristaps 922:
1.84 schwarze 923: if ( ! validate_manpath(req, manpath)) {
1.77 schwarze 924: pg_error_badrequest(
925: "You specified an invalid manpath.");
1.84 schwarze 926: free(manpath);
1.77 schwarze 927: return;
928: }
929:
1.24 kristaps 930: /*
1.58 schwarze 931: * Begin by chdir()ing into the manpath.
1.24 kristaps 932: * This way we can pick up the database files, which are
933: * relative to the manpath root.
934: */
935:
1.84 schwarze 936: if (chdir(manpath) == -1) {
1.77 schwarze 937: fprintf(stderr, "chdir %s: %s\n",
1.84 schwarze 938: manpath, strerror(errno));
1.77 schwarze 939: pg_error_internal();
1.84 schwarze 940: free(manpath);
1.76 schwarze 941: return;
942: }
943:
1.84 schwarze 944: if (strcmp(manpath, "mandoc")) {
945: free(req->q.manpath);
946: req->q.manpath = manpath;
947: } else
948: free(manpath);
949:
950: if ( ! validate_filename(file)) {
1.76 schwarze 951: pg_error_badrequest(
952: "You specified an invalid manual file.");
1.24 kristaps 953: return;
954: }
1.79 schwarze 955:
1.70 schwarze 956: resp_begin_html(200, NULL);
957: resp_searchform(req);
1.84 schwarze 958: resp_show(req, file);
1.70 schwarze 959: resp_end_html();
1.6 kristaps 960: }
961:
962: static void
1.66 schwarze 963: pg_search(const struct req *req)
1.6 kristaps 964: {
1.52 schwarze 965: struct mansearch search;
966: struct manpaths paths;
967: struct manpage *res;
1.97 schwarze 968: char **argv;
969: char *query, *rp, *wp;
1.52 schwarze 970: size_t ressz;
1.97 schwarze 971: int argc;
1.6 kristaps 972:
973: /*
1.24 kristaps 974: * Begin by chdir()ing into the root of the manpath.
975: * This way we can pick up the database files, which are
976: * relative to the manpath root.
977: */
978:
1.58 schwarze 979: if (-1 == (chdir(req->q.manpath))) {
1.77 schwarze 980: fprintf(stderr, "chdir %s: %s\n",
981: req->q.manpath, strerror(errno));
982: pg_error_internal();
1.24 kristaps 983: return;
984: }
985:
1.52 schwarze 986: search.arch = req->q.arch;
987: search.sec = req->q.sec;
1.94 schwarze 988: search.outkey = "Nd";
989: search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.101 schwarze 990: search.firstmatch = 1;
1.52 schwarze 991:
992: paths.sz = 1;
993: paths.paths = mandoc_malloc(sizeof(char *));
994: paths.paths[0] = mandoc_strdup(".");
1.24 kristaps 995:
996: /*
1.97 schwarze 997: * Break apart at spaces with backslash-escaping.
1.6 kristaps 998: */
999:
1.97 schwarze 1000: argc = 0;
1001: argv = NULL;
1002: rp = query = mandoc_strdup(req->q.query);
1003: for (;;) {
1004: while (isspace((unsigned char)*rp))
1005: rp++;
1006: if (*rp == '\0')
1007: break;
1008: argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
1009: argv[argc++] = wp = rp;
1010: for (;;) {
1011: if (isspace((unsigned char)*rp)) {
1012: *wp = '\0';
1013: rp++;
1014: break;
1015: }
1016: if (rp[0] == '\\' && rp[1] != '\0')
1017: rp++;
1018: if (wp != rp)
1019: *wp = *rp;
1020: if (*rp == '\0')
1021: break;
1022: wp++;
1023: rp++;
1024: }
1.6 kristaps 1025: }
1026:
1.97 schwarze 1027: if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz))
1.72 schwarze 1028: pg_noresult(req, "You entered an invalid query.");
1.59 schwarze 1029: else if (0 == ressz)
1.72 schwarze 1030: pg_noresult(req, "No results found.");
1.59 schwarze 1031: else
1.72 schwarze 1032: pg_searchres(req, res, ressz);
1.6 kristaps 1033:
1.97 schwarze 1034: free(query);
1035: mansearch_free(res, ressz);
1.52 schwarze 1036: free(paths.paths[0]);
1037: free(paths.paths);
1.1 kristaps 1038: }
1039:
1040: int
1041: main(void)
1042: {
1.66 schwarze 1043: struct req req;
1.95 schwarze 1044: struct itimerval itimer;
1.66 schwarze 1045: const char *path;
1.83 schwarze 1046: const char *querystring;
1.1 kristaps 1047: int i;
1.95 schwarze 1048:
1049: /* Poor man's ReDoS mitigation. */
1050:
1.99 schwarze 1051: itimer.it_value.tv_sec = 2;
1.95 schwarze 1052: itimer.it_value.tv_usec = 0;
1.99 schwarze 1053: itimer.it_interval.tv_sec = 2;
1.95 schwarze 1054: itimer.it_interval.tv_usec = 0;
1055: if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1056: fprintf(stderr, "setitimer: %s\n", strerror(errno));
1057: pg_error_internal();
1.109 schwarze 1058: return EXIT_FAILURE;
1.95 schwarze 1059: }
1.6 kristaps 1060:
1.24 kristaps 1061: /* Scan our run-time environment. */
1.6 kristaps 1062:
1.58 schwarze 1063: if (NULL == (scriptname = getenv("SCRIPT_NAME")))
1064: scriptname = "";
1.24 kristaps 1065:
1.80 schwarze 1066: if ( ! validate_urifrag(scriptname)) {
1067: fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n",
1068: scriptname);
1069: pg_error_internal();
1.109 schwarze 1070: return EXIT_FAILURE;
1.80 schwarze 1071: }
1072:
1.24 kristaps 1073: /*
1.67 schwarze 1074: * First we change directory into the MAN_DIR so that
1.24 kristaps 1075: * subsequent scanning for manpath directories is rooted
1076: * relative to the same position.
1077: */
1078:
1.67 schwarze 1079: if (-1 == chdir(MAN_DIR)) {
1.58 schwarze 1080: fprintf(stderr, "MAN_DIR: %s: %s\n",
1.67 schwarze 1081: MAN_DIR, strerror(errno));
1.72 schwarze 1082: pg_error_internal();
1.109 schwarze 1083: return EXIT_FAILURE;
1.104 schwarze 1084: }
1.24 kristaps 1085:
1086: memset(&req, 0, sizeof(struct req));
1.54 schwarze 1087: pathgen(&req);
1.1 kristaps 1088:
1.24 kristaps 1089: /* Next parse out the query string. */
1.1 kristaps 1090:
1.58 schwarze 1091: if (NULL != (querystring = getenv("QUERY_STRING")))
1092: http_parse(&req, querystring);
1.77 schwarze 1093:
1.102 schwarze 1094: if (req.q.manpath == NULL)
1095: req.q.manpath = mandoc_strdup(req.p[0]);
1096: else if ( ! validate_manpath(&req, req.q.manpath)) {
1.77 schwarze 1097: pg_error_badrequest(
1098: "You specified an invalid manpath.");
1.109 schwarze 1099: return EXIT_FAILURE;
1.77 schwarze 1100: }
1.1 kristaps 1101:
1.80 schwarze 1102: if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
1103: pg_error_badrequest(
1104: "You specified an invalid architecture.");
1.109 schwarze 1105: return EXIT_FAILURE;
1.80 schwarze 1106: }
1107:
1.66 schwarze 1108: /* Dispatch to the three different pages. */
1.1 kristaps 1109:
1.66 schwarze 1110: path = getenv("PATH_INFO");
1111: if (NULL == path)
1112: path = "";
1113: else if ('/' == *path)
1114: path++;
1115:
1116: if ('\0' != *path)
1117: pg_show(&req, path);
1.85 schwarze 1118: else if (NULL != req.q.query)
1.66 schwarze 1119: pg_search(&req);
1120: else
1.72 schwarze 1121: pg_index(&req);
1.1 kristaps 1122:
1.83 schwarze 1123: free(req.q.manpath);
1124: free(req.q.arch);
1125: free(req.q.sec);
1.85 schwarze 1126: free(req.q.query);
1.52 schwarze 1127: for (i = 0; i < (int)req.psz; i++)
1128: free(req.p[i]);
1.24 kristaps 1129: free(req.p);
1.109 schwarze 1130: return EXIT_SUCCESS;
1.24 kristaps 1131: }
1132:
1133: /*
1134: * Scan for indexable paths.
1135: */
1136: static void
1.54 schwarze 1137: pathgen(struct req *req)
1.24 kristaps 1138: {
1.54 schwarze 1139: FILE *fp;
1140: char *dp;
1141: size_t dpsz;
1.115 ! schwarze 1142: ssize_t len;
1.54 schwarze 1143:
1.74 schwarze 1144: if (NULL == (fp = fopen("manpath.conf", "r"))) {
1145: fprintf(stderr, "%s/manpath.conf: %s\n",
1146: MAN_DIR, strerror(errno));
1147: pg_error_internal();
1148: exit(EXIT_FAILURE);
1149: }
1.24 kristaps 1150:
1.115 ! schwarze 1151: dp = NULL;
! 1152: dpsz = 0;
! 1153:
! 1154: while ((len = getline(&dp, &dpsz, fp)) != -1) {
! 1155: if (dp[len - 1] == '\n')
! 1156: dp[--len] = '\0';
1.54 schwarze 1157: req->p = mandoc_realloc(req->p,
1158: (req->psz + 1) * sizeof(char *));
1.80 schwarze 1159: if ( ! validate_urifrag(dp)) {
1160: fprintf(stderr, "%s/manpath.conf contains "
1161: "unsafe path \"%s\"\n", MAN_DIR, dp);
1162: pg_error_internal();
1163: exit(EXIT_FAILURE);
1164: }
1165: if (NULL != strchr(dp, '/')) {
1166: fprintf(stderr, "%s/manpath.conf contains "
1167: "path with slash \"%s\"\n", MAN_DIR, dp);
1168: pg_error_internal();
1169: exit(EXIT_FAILURE);
1170: }
1171: req->p[req->psz++] = dp;
1.115 ! schwarze 1172: dp = NULL;
! 1173: dpsz = 0;
1.74 schwarze 1174: }
1.115 ! schwarze 1175: free(dp);
1.74 schwarze 1176:
1177: if ( req->p == NULL ) {
1178: fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR);
1179: pg_error_internal();
1180: exit(EXIT_FAILURE);
1.24 kristaps 1181: }
1182: }
CVSweb