Annotation of mandoc/cgi.c, Revision 1.127
1.127 ! schwarze 1: /* $Id: cgi.c,v 1.126 2016/04/15 01:34:51 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",
1.125 schwarze 111: "hppa", "hppa64", "i386", "landisk",
112: "loongson", "luna88k", "macppc", "mips64",
113: "octeon", "sgi", "socppc", "sparc",
114: "sparc64", "zaurus",
1.68 schwarze 115: "amiga", "arc", "arm32", "atari",
1.125 schwarze 116: "aviion", "beagle", "cats", "hp300",
117: "ia64", "mac68k", "mvme68k", "mvme88k",
118: "mvmeppc", "palm", "pc532", "pegasos",
119: "pmax", "powerpc", "solbourne", "sun3",
120: "vax", "wgrisc", "x68k"
1.68 schwarze 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.126 schwarze 344: "<html>\n"
345: "<head>\n"
346: "<meta charset=\"UTF-8\"/>\n"
347: "<link rel=\"stylesheet\" href=\"%s/mandoc.css\""
348: " type=\"text/css\" media=\"all\">\n"
349: "<title>%s</title>\n"
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.126 schwarze 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.126 schwarze 374: printf("<div id=\"mancgi\">\n"
375: "<form action=\"/%s\" method=\"get\">\n"
376: "<fieldset>\n"
377: "<legend>Manual Page Search Parameters</legend>\n",
1.58 schwarze 378: scriptname);
1.68 schwarze 379:
380: /* Write query input box. */
381:
1.126 schwarze 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.126 schwarze 386: puts("\" size=\"40\">");
1.68 schwarze 387:
388: /* Write submission and reset buttons. */
389:
1.126 schwarze 390: printf( "<input type=\"submit\" value=\"Submit\">\n"
391: "<input type=\"reset\" value=\"Reset\">\n");
1.68 schwarze 392:
393: /* Write show radio button */
394:
1.126 schwarze 395: printf( "</td><td>\n"
396: "<input type=\"radio\" ");
1.65 schwarze 397: if (req->q.equal)
1.126 schwarze 398: printf("checked=\"checked\" ");
399: printf( "name=\"apropos\" id=\"show\" value=\"0\">\n"
400: "<label for=\"show\">Show named manual page</label>\n");
1.68 schwarze 401:
402: /* Write section selector. */
403:
1.126 schwarze 404: puts( "</td></tr><tr><td>\n"
405: "<select name=\"sec\">");
1.68 schwarze 406: for (i = 0; i < sec_MAX; i++) {
1.126 schwarze 407: printf("<option value=\"%s\"", sec_numbers[i]);
1.68 schwarze 408: if (NULL != req->q.sec &&
409: 0 == strcmp(sec_numbers[i], req->q.sec))
1.126 schwarze 410: printf(" selected=\"selected\"");
411: printf(">%s</option>\n", sec_names[i]);
1.68 schwarze 412: }
1.126 schwarze 413: puts("</select>");
1.68 schwarze 414:
415: /* Write architecture selector. */
416:
1.126 schwarze 417: printf( "<select name=\"arch\">\n"
418: "<option value=\"default\"");
1.81 schwarze 419: if (NULL == req->q.arch)
1.126 schwarze 420: printf(" selected=\"selected\"");
421: puts(">All Architectures</option>");
1.68 schwarze 422: for (i = 0; i < arch_MAX; i++) {
1.126 schwarze 423: printf("<option value=\"%s\"", arch_names[i]);
1.68 schwarze 424: if (NULL != req->q.arch &&
425: 0 == strcmp(arch_names[i], req->q.arch))
1.126 schwarze 426: printf(" selected=\"selected\"");
427: printf(">%s</option>\n", arch_names[i]);
1.68 schwarze 428: }
1.126 schwarze 429: puts("</select>");
1.68 schwarze 430:
431: /* Write manpath selector. */
432:
1.27 kristaps 433: if (req->psz > 1) {
1.126 schwarze 434: puts("<select name=\"manpath\">");
1.27 kristaps 435: for (i = 0; i < (int)req->psz; i++) {
1.126 schwarze 436: printf("<option ");
1.102 schwarze 437: if (strcmp(req->q.manpath, req->p[i]) == 0)
1.126 schwarze 438: printf("selected=\"selected\" ");
439: printf("value=\"");
1.52 schwarze 440: html_print(req->p[i]);
1.27 kristaps 441: printf("\">");
1.52 schwarze 442: html_print(req->p[i]);
1.126 schwarze 443: puts("</option>");
1.27 kristaps 444: }
1.126 schwarze 445: puts("</select>");
1.27 kristaps 446: }
1.68 schwarze 447:
448: /* Write search radio button */
449:
1.126 schwarze 450: printf( "</td><td>\n"
451: "<input type=\"radio\" ");
1.68 schwarze 452: if (0 == req->q.equal)
1.126 schwarze 453: printf("checked=\"checked\" ");
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"
458: "</fieldset>\n"
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.126 schwarze 510: printf("<p>\n"
1.86 schwarze 511: "This web interface is documented in the\n"
1.126 schwarze 512: "<a href=\"/%s%smandoc/man8/man.cgi.8\">man.cgi</a>\n"
1.86 schwarze 513: "manual, and the\n"
1.126 schwarze 514: "<a href=\"/%s%smandoc/man1/apropos.1\">apropos</a>\n"
1.69 schwarze 515: "manual explains the query syntax.\n"
1.126 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);
1.126 schwarze 527: puts("<p>");
1.59 schwarze 528: puts(msg);
1.126 schwarze 529: puts("</p>");
1.59 schwarze 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");
1.126 schwarze 538: puts("<h1>Bad Request</h1>\n"
539: "<p>\n");
1.59 schwarze 540: puts(msg);
541: printf("Try again from the\n"
1.126 schwarze 542: "<a href=\"/%s\">main page</a>.\n"
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.126 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.1 kristaps 591:
1.123 schwarze 592: if (sz > 1) {
1.126 schwarze 593: puts("<div class=\"results\">");
594: puts("<table>");
1.123 schwarze 595:
596: for (i = 0; i < sz; i++) {
1.126 schwarze 597: printf("<tr>\n"
598: "<td class=\"title\">\n"
599: "<a href=\"/%s%s%s/%s",
1.123 schwarze 600: scriptname, *scriptname == '\0' ? "" : "/",
601: req->q.manpath, r[i].file);
602: printf("\">");
603: html_print(r[i].names);
1.126 schwarze 604: printf("</a>\n"
605: "</td>\n"
606: "<td class=\"desc\">");
1.123 schwarze 607: html_print(r[i].output);
1.126 schwarze 608: puts("</td>\n"
609: "</tr>");
1.123 schwarze 610: }
611:
1.126 schwarze 612: puts("</table>\n"
613: "</div>");
1.1 kristaps 614: }
1.16 kristaps 615:
1.70 schwarze 616: /*
617: * In man(1) mode, show one of the pages
618: * even if more than one is found.
619: */
620:
1.123 schwarze 621: if (req->q.equal || sz == 1) {
1.126 schwarze 622: puts("<hr>");
1.70 schwarze 623: iuse = 0;
1.120 schwarze 624: priouse = 20;
1.81 schwarze 625: archpriouse = 3;
1.70 schwarze 626: for (i = 0; i < sz; i++) {
1.120 schwarze 627: sec = r[i].file;
628: sec += strcspn(sec, "123456789");
629: if (sec[0] == '\0')
1.70 schwarze 630: continue;
1.120 schwarze 631: prio = sec_prios[sec[0] - '1'];
632: if (sec[1] != '/')
633: prio += 10;
634: if (req->q.arch == NULL) {
1.81 schwarze 635: archprio =
1.120 schwarze 636: ((arch = strchr(sec + 1, '/'))
637: == NULL) ? 3 :
638: ((archend = strchr(arch + 1, '/'))
639: == NULL) ? 0 :
1.81 schwarze 640: strncmp(arch, "amd64/",
641: archend - arch) ? 2 : 1;
642: if (archprio < archpriouse) {
643: archpriouse = archprio;
644: priouse = prio;
645: iuse = i;
646: continue;
647: }
648: if (archprio > archpriouse)
649: continue;
650: }
1.70 schwarze 651: if (prio >= priouse)
652: continue;
653: priouse = prio;
654: iuse = i;
655: }
656: resp_show(req, r[iuse].file);
657: }
658:
1.6 kristaps 659: resp_end_html();
660: }
661:
1.1 kristaps 662: static void
1.32 kristaps 663: catman(const struct req *req, const char *file)
1.9 kristaps 664: {
1.10 kristaps 665: FILE *f;
1.115 schwarze 666: char *p;
667: size_t sz;
668: ssize_t len;
1.10 kristaps 669: int i;
670: int italic, bold;
1.9 kristaps 671:
1.115 schwarze 672: if ((f = fopen(file, "r")) == NULL) {
1.126 schwarze 673: puts("<p>You specified an invalid manual file.</p>");
1.9 kristaps 674: return;
675: }
676:
1.126 schwarze 677: puts("<div class=\"catman\">\n"
678: "<pre>");
1.10 kristaps 679:
1.115 schwarze 680: p = NULL;
681: sz = 0;
682:
683: while ((len = getline(&p, &sz, f)) != -1) {
1.10 kristaps 684: bold = italic = 0;
1.115 schwarze 685: for (i = 0; i < len - 1; i++) {
1.104 schwarze 686: /*
1.10 kristaps 687: * This means that the catpage is out of state.
688: * Ignore it and keep going (although the
689: * catpage is bogus).
690: */
691:
692: if ('\b' == p[i] || '\n' == p[i])
693: continue;
694:
695: /*
696: * Print a regular character.
697: * Close out any bold/italic scopes.
698: * If we're in back-space mode, make sure we'll
699: * have something to enter when we backspace.
700: */
701:
702: if ('\b' != p[i + 1]) {
703: if (italic)
1.126 schwarze 704: printf("</i>");
1.10 kristaps 705: if (bold)
1.126 schwarze 706: printf("</b>");
1.10 kristaps 707: italic = bold = 0;
708: html_putchar(p[i]);
709: continue;
1.115 schwarze 710: } else if (i + 2 >= len)
1.10 kristaps 711: continue;
712:
713: /* Italic mode. */
714:
715: if ('_' == p[i]) {
716: if (bold)
1.126 schwarze 717: printf("</b>");
1.10 kristaps 718: if ( ! italic)
1.126 schwarze 719: printf("<i>");
1.10 kristaps 720: bold = 0;
721: italic = 1;
722: i += 2;
723: html_putchar(p[i]);
724: continue;
725: }
726:
1.104 schwarze 727: /*
1.10 kristaps 728: * Handle funny behaviour troff-isms.
729: * These grok'd from the original man2html.c.
730: */
731:
732: if (('+' == p[i] && 'o' == p[i + 2]) ||
733: ('o' == p[i] && '+' == p[i + 2]) ||
734: ('|' == p[i] && '=' == p[i + 2]) ||
735: ('=' == p[i] && '|' == p[i + 2]) ||
736: ('*' == p[i] && '=' == p[i + 2]) ||
737: ('=' == p[i] && '*' == p[i + 2]) ||
738: ('*' == p[i] && '|' == p[i + 2]) ||
739: ('|' == p[i] && '*' == p[i + 2])) {
740: if (italic)
1.126 schwarze 741: printf("</i>");
1.10 kristaps 742: if (bold)
1.126 schwarze 743: printf("</b>");
1.10 kristaps 744: italic = bold = 0;
745: putchar('*');
746: i += 2;
747: continue;
748: } else if (('|' == p[i] && '-' == p[i + 2]) ||
749: ('-' == p[i] && '|' == p[i + 1]) ||
750: ('+' == p[i] && '-' == p[i + 1]) ||
751: ('-' == p[i] && '+' == p[i + 1]) ||
752: ('+' == p[i] && '|' == p[i + 1]) ||
753: ('|' == p[i] && '+' == p[i + 1])) {
754: if (italic)
1.126 schwarze 755: printf("</i>");
1.10 kristaps 756: if (bold)
1.126 schwarze 757: printf("</b>");
1.10 kristaps 758: italic = bold = 0;
759: putchar('+');
760: i += 2;
761: continue;
762: }
763:
764: /* Bold mode. */
1.104 schwarze 765:
1.10 kristaps 766: if (italic)
1.126 schwarze 767: printf("</i>");
1.10 kristaps 768: if ( ! bold)
1.126 schwarze 769: printf("<b>");
1.10 kristaps 770: bold = 1;
771: italic = 0;
772: i += 2;
773: html_putchar(p[i]);
774: }
775:
1.104 schwarze 776: /*
1.10 kristaps 777: * Clean up the last character.
1.104 schwarze 778: * We can get to a newline; don't print that.
1.10 kristaps 779: */
1.9 kristaps 780:
1.10 kristaps 781: if (italic)
1.126 schwarze 782: printf("</i>");
1.10 kristaps 783: if (bold)
1.126 schwarze 784: printf("</b>");
1.9 kristaps 785:
1.115 schwarze 786: if (i == len - 1 && p[i] != '\n')
1.10 kristaps 787: html_putchar(p[i]);
1.9 kristaps 788:
1.10 kristaps 789: putchar('\n');
790: }
1.115 schwarze 791: free(p);
1.10 kristaps 792:
1.126 schwarze 793: puts("</pre>\n"
794: "</div>");
1.10 kristaps 795:
796: fclose(f);
1.9 kristaps 797: }
798:
799: static void
1.32 kristaps 800: format(const struct req *req, const char *file)
1.7 kristaps 801: {
1.106 schwarze 802: struct manoutput conf;
1.8 kristaps 803: struct mparse *mp;
1.107 schwarze 804: struct roff_man *man;
1.8 kristaps 805: void *vp;
1.90 schwarze 806: int fd;
807: int usepath;
1.7 kristaps 808:
1.8 kristaps 809: if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.126 schwarze 810: puts("<p>You specified an invalid manual file.</p>");
1.7 kristaps 811: return;
812: }
813:
1.110 schwarze 814: mchars_alloc();
815: mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, req->q.manpath);
1.103 schwarze 816: mparse_readfd(mp, fd, file);
1.8 kristaps 817: close(fd);
1.7 kristaps 818:
1.106 schwarze 819: memset(&conf, 0, sizeof(conf));
820: conf.fragment = 1;
1.90 schwarze 821: usepath = strcmp(req->q.manpath, req->p[0]);
1.122 schwarze 822: mandoc_asprintf(&conf.man, "/%s%s%%N.%%S",
823: usepath ? req->q.manpath : "", usepath ? "/" : "");
1.10 kristaps 824:
1.108 schwarze 825: mparse_result(mp, &man, NULL);
826: if (man == NULL) {
1.59 schwarze 827: fprintf(stderr, "fatal mandoc error: %s/%s\n",
828: req->q.manpath, file);
1.72 schwarze 829: pg_error_internal();
1.32 kristaps 830: mparse_free(mp);
1.110 schwarze 831: mchars_free();
1.32 kristaps 832: return;
833: }
834:
1.110 schwarze 835: vp = html_alloc(&conf);
1.7 kristaps 836:
1.111 schwarze 837: if (man->macroset == MACROSET_MDOC) {
838: mdoc_validate(man);
1.108 schwarze 839: html_mdoc(vp, man);
1.112 schwarze 840: } else {
841: man_validate(man);
1.8 kristaps 842: html_man(vp, man);
1.112 schwarze 843: }
1.32 kristaps 844:
1.8 kristaps 845: html_free(vp);
846: mparse_free(mp);
1.110 schwarze 847: mchars_free();
1.106 schwarze 848: free(conf.man);
1.7 kristaps 849: }
850:
851: static void
1.70 schwarze 852: resp_show(const struct req *req, const char *file)
853: {
1.76 schwarze 854:
855: if ('.' == file[0] && '/' == file[1])
1.71 schwarze 856: file += 2;
1.70 schwarze 857:
858: if ('c' == *file)
859: catman(req, file);
860: else
861: format(req, file);
862: }
863:
864: static void
1.84 schwarze 865: pg_show(struct req *req, const char *fullpath)
1.1 kristaps 866: {
1.84 schwarze 867: char *manpath;
868: const char *file;
1.25 kristaps 869:
1.84 schwarze 870: if ((file = strchr(fullpath, '/')) == NULL) {
1.72 schwarze 871: pg_error_badrequest(
1.59 schwarze 872: "You did not specify a page to show.");
1.25 kristaps 873: return;
1.104 schwarze 874: }
1.84 schwarze 875: manpath = mandoc_strndup(fullpath, file - fullpath);
876: file++;
1.6 kristaps 877:
1.84 schwarze 878: if ( ! validate_manpath(req, manpath)) {
1.77 schwarze 879: pg_error_badrequest(
880: "You specified an invalid manpath.");
1.84 schwarze 881: free(manpath);
1.77 schwarze 882: return;
883: }
884:
1.24 kristaps 885: /*
1.58 schwarze 886: * Begin by chdir()ing into the manpath.
1.24 kristaps 887: * This way we can pick up the database files, which are
888: * relative to the manpath root.
889: */
890:
1.84 schwarze 891: if (chdir(manpath) == -1) {
1.77 schwarze 892: fprintf(stderr, "chdir %s: %s\n",
1.84 schwarze 893: manpath, strerror(errno));
1.77 schwarze 894: pg_error_internal();
1.84 schwarze 895: free(manpath);
1.76 schwarze 896: return;
897: }
898:
1.84 schwarze 899: if (strcmp(manpath, "mandoc")) {
900: free(req->q.manpath);
901: req->q.manpath = manpath;
902: } else
903: free(manpath);
904:
905: if ( ! validate_filename(file)) {
1.76 schwarze 906: pg_error_badrequest(
907: "You specified an invalid manual file.");
1.24 kristaps 908: return;
909: }
1.79 schwarze 910:
1.70 schwarze 911: resp_begin_html(200, NULL);
912: resp_searchform(req);
1.84 schwarze 913: resp_show(req, file);
1.70 schwarze 914: resp_end_html();
1.6 kristaps 915: }
916:
917: static void
1.66 schwarze 918: pg_search(const struct req *req)
1.6 kristaps 919: {
1.52 schwarze 920: struct mansearch search;
921: struct manpaths paths;
922: struct manpage *res;
1.97 schwarze 923: char **argv;
924: char *query, *rp, *wp;
1.52 schwarze 925: size_t ressz;
1.97 schwarze 926: int argc;
1.6 kristaps 927:
928: /*
1.24 kristaps 929: * Begin by chdir()ing into the root of the manpath.
930: * This way we can pick up the database files, which are
931: * relative to the manpath root.
932: */
933:
1.58 schwarze 934: if (-1 == (chdir(req->q.manpath))) {
1.77 schwarze 935: fprintf(stderr, "chdir %s: %s\n",
936: req->q.manpath, strerror(errno));
937: pg_error_internal();
1.24 kristaps 938: return;
939: }
940:
1.52 schwarze 941: search.arch = req->q.arch;
942: search.sec = req->q.sec;
1.94 schwarze 943: search.outkey = "Nd";
944: search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.101 schwarze 945: search.firstmatch = 1;
1.52 schwarze 946:
947: paths.sz = 1;
948: paths.paths = mandoc_malloc(sizeof(char *));
949: paths.paths[0] = mandoc_strdup(".");
1.24 kristaps 950:
951: /*
1.97 schwarze 952: * Break apart at spaces with backslash-escaping.
1.6 kristaps 953: */
954:
1.97 schwarze 955: argc = 0;
956: argv = NULL;
957: rp = query = mandoc_strdup(req->q.query);
958: for (;;) {
959: while (isspace((unsigned char)*rp))
960: rp++;
961: if (*rp == '\0')
962: break;
963: argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
964: argv[argc++] = wp = rp;
965: for (;;) {
966: if (isspace((unsigned char)*rp)) {
967: *wp = '\0';
968: rp++;
969: break;
970: }
971: if (rp[0] == '\\' && rp[1] != '\0')
972: rp++;
973: if (wp != rp)
974: *wp = *rp;
975: if (*rp == '\0')
976: break;
977: wp++;
978: rp++;
979: }
1.6 kristaps 980: }
981:
1.97 schwarze 982: if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz))
1.72 schwarze 983: pg_noresult(req, "You entered an invalid query.");
1.59 schwarze 984: else if (0 == ressz)
1.72 schwarze 985: pg_noresult(req, "No results found.");
1.59 schwarze 986: else
1.72 schwarze 987: pg_searchres(req, res, ressz);
1.6 kristaps 988:
1.97 schwarze 989: free(query);
990: mansearch_free(res, ressz);
1.52 schwarze 991: free(paths.paths[0]);
992: free(paths.paths);
1.1 kristaps 993: }
994:
995: int
996: main(void)
997: {
1.66 schwarze 998: struct req req;
1.95 schwarze 999: struct itimerval itimer;
1.66 schwarze 1000: const char *path;
1.83 schwarze 1001: const char *querystring;
1.1 kristaps 1002: int i;
1.95 schwarze 1003:
1004: /* Poor man's ReDoS mitigation. */
1005:
1.99 schwarze 1006: itimer.it_value.tv_sec = 2;
1.95 schwarze 1007: itimer.it_value.tv_usec = 0;
1.99 schwarze 1008: itimer.it_interval.tv_sec = 2;
1.95 schwarze 1009: itimer.it_interval.tv_usec = 0;
1010: if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1011: fprintf(stderr, "setitimer: %s\n", strerror(errno));
1.80 schwarze 1012: pg_error_internal();
1.109 schwarze 1013: return EXIT_FAILURE;
1.80 schwarze 1014: }
1015:
1.24 kristaps 1016: /*
1.67 schwarze 1017: * First we change directory into the MAN_DIR so that
1.24 kristaps 1018: * subsequent scanning for manpath directories is rooted
1019: * relative to the same position.
1020: */
1021:
1.67 schwarze 1022: if (-1 == chdir(MAN_DIR)) {
1.58 schwarze 1023: fprintf(stderr, "MAN_DIR: %s: %s\n",
1.67 schwarze 1024: MAN_DIR, strerror(errno));
1.72 schwarze 1025: pg_error_internal();
1.109 schwarze 1026: return EXIT_FAILURE;
1.104 schwarze 1027: }
1.24 kristaps 1028:
1029: memset(&req, 0, sizeof(struct req));
1.118 schwarze 1030: req.q.equal = 1;
1.54 schwarze 1031: pathgen(&req);
1.1 kristaps 1032:
1.117 schwarze 1033: /* Parse the path info and the query string. */
1.1 kristaps 1034:
1.117 schwarze 1035: if ((path = getenv("PATH_INFO")) == NULL)
1036: path = "";
1037: else if (*path == '/')
1038: path++;
1039:
1.124 schwarze 1040: if (*path != '\0') {
1.117 schwarze 1041: path_parse(&req, path);
1.124 schwarze 1042: if (access(path, F_OK) == -1)
1043: path = "";
1.117 schwarze 1044: } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.58 schwarze 1045: http_parse(&req, querystring);
1.77 schwarze 1046:
1.117 schwarze 1047: /* Validate parsed data and add defaults. */
1048:
1.102 schwarze 1049: if (req.q.manpath == NULL)
1050: req.q.manpath = mandoc_strdup(req.p[0]);
1051: else if ( ! validate_manpath(&req, req.q.manpath)) {
1.77 schwarze 1052: pg_error_badrequest(
1053: "You specified an invalid manpath.");
1.109 schwarze 1054: return EXIT_FAILURE;
1.77 schwarze 1055: }
1.1 kristaps 1056:
1.80 schwarze 1057: if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
1058: pg_error_badrequest(
1059: "You specified an invalid architecture.");
1.109 schwarze 1060: return EXIT_FAILURE;
1.80 schwarze 1061: }
1062:
1.66 schwarze 1063: /* Dispatch to the three different pages. */
1.1 kristaps 1064:
1.66 schwarze 1065: if ('\0' != *path)
1066: pg_show(&req, path);
1.85 schwarze 1067: else if (NULL != req.q.query)
1.66 schwarze 1068: pg_search(&req);
1069: else
1.72 schwarze 1070: pg_index(&req);
1.1 kristaps 1071:
1.83 schwarze 1072: free(req.q.manpath);
1073: free(req.q.arch);
1074: free(req.q.sec);
1.85 schwarze 1075: free(req.q.query);
1.52 schwarze 1076: for (i = 0; i < (int)req.psz; i++)
1077: free(req.p[i]);
1.24 kristaps 1078: free(req.p);
1.109 schwarze 1079: return EXIT_SUCCESS;
1.117 schwarze 1080: }
1081:
1082: /*
1083: * If PATH_INFO is not a file name, translate it to a query.
1084: */
1085: static void
1086: path_parse(struct req *req, const char *path)
1087: {
1.127 ! schwarze 1088: char *dir;
1.117 schwarze 1089:
1.121 schwarze 1090: req->isquery = 0;
1.117 schwarze 1091: req->q.equal = 1;
1092: req->q.manpath = mandoc_strdup(path);
1093:
1094: /* Mandatory manual page name. */
1095: if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) {
1096: req->q.query = req->q.manpath;
1097: req->q.manpath = NULL;
1098: } else
1099: *req->q.query++ = '\0';
1100:
1101: /* Optional trailing section. */
1102: if ((req->q.sec = strrchr(req->q.query, '.')) != NULL) {
1103: if(isdigit((unsigned char)req->q.sec[1])) {
1104: *req->q.sec++ = '\0';
1105: req->q.sec = mandoc_strdup(req->q.sec);
1106: } else
1107: req->q.sec = NULL;
1108: }
1109:
1110: /* Handle the case of name[.section] only. */
1111: if (req->q.manpath == NULL) {
1112: req->q.arch = NULL;
1113: return;
1114: }
1115: req->q.query = mandoc_strdup(req->q.query);
1116:
1117: /* Optional architecture. */
1.127 ! schwarze 1118: dir = strrchr(req->q.manpath, '/');
! 1119: if (dir != NULL && strncmp(dir + 1, "man", 3) != 0) {
! 1120: *dir++ = '\0';
! 1121: req->q.arch = mandoc_strdup(dir);
! 1122: dir = strrchr(req->q.manpath, '/');
! 1123: } else
! 1124: req->q.arch = NULL;
1.117 schwarze 1125:
1.127 ! schwarze 1126: /* Optional directory name. */
! 1127: if (dir != NULL && strncmp(dir + 1, "man", 3) == 0) {
! 1128: *dir++ = '\0';
! 1129: free(req->q.sec);
! 1130: req->q.sec = mandoc_strdup(dir + 3);
1.117 schwarze 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