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