Annotation of mandoc/cgi.c, Revision 1.142
1.142 ! schwarze 1: /* $Id: cgi.c,v 1.141 2016/09/12 00:06:20 schwarze Exp $ */
1.6 kristaps 2: /*
1.42 kristaps 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.142 ! schwarze 4: * Copyright (c) 2014, 2015, 2016, 2017 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",
1.140 schwarze 119: "octeon", "sgi", "socppc", "sparc64",
1.137 schwarze 120: "amiga", "arc", "armish", "arm32",
121: "atari", "aviion", "beagle", "cats",
122: "hppa64", "hp300",
1.125 schwarze 123: "ia64", "mac68k", "mvme68k", "mvme88k",
124: "mvmeppc", "palm", "pc532", "pegasos",
1.139 schwarze 125: "pmax", "powerpc", "solbourne", "sparc",
1.140 schwarze 126: "sun3", "vax", "wgrisc", "x68k",
127: "zaurus"
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 ('"'):
1.141 schwarze 141: printf(""");
1.10 kristaps 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);
1.138 schwarze 341: close(fd);
1.114 schwarze 342: }
343: }
344:
345: static void
1.6 kristaps 346: resp_begin_html(int code, const char *msg)
347: {
348:
349: resp_begin_http(code, msg);
350:
1.98 kristaps 351: printf("<!DOCTYPE html>\n"
1.126 schwarze 352: "<html>\n"
353: "<head>\n"
354: "<meta charset=\"UTF-8\"/>\n"
355: "<link rel=\"stylesheet\" href=\"%s/mandoc.css\""
356: " type=\"text/css\" media=\"all\">\n"
357: "<title>%s</title>\n"
358: "</head>\n"
1.142 ! schwarze 359: "<body>\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.142 ! schwarze 380: printf("<form action=\"/%s\" method=\"get\">\n"
1.126 schwarze 381: "<fieldset>\n"
382: "<legend>Manual Page Search Parameters</legend>\n",
1.58 schwarze 383: scriptname);
1.68 schwarze 384:
385: /* Write query input box. */
386:
1.132 schwarze 387: printf("<input type=\"text\" name=\"query\" value=\"");
1.131 schwarze 388: if (req->q.query != NULL)
1.85 schwarze 389: html_print(req->q.query);
1.131 schwarze 390: printf( "\" size=\"40\"");
391: if (focus == FOCUS_QUERY)
392: printf(" autofocus");
393: puts(">");
1.68 schwarze 394:
1.132 schwarze 395: /* Write submission buttons. */
1.68 schwarze 396:
1.132 schwarze 397: printf( "<button type=\"submit\" name=\"apropos\" value=\"0\">"
398: "man</button>\n"
399: "<button type=\"submit\" name=\"apropos\" value=\"1\">"
400: "apropos</button>\n<br/>\n");
1.68 schwarze 401:
402: /* Write section selector. */
403:
1.132 schwarze 404: puts("<select name=\"sec\">");
1.68 schwarze 405: for (i = 0; i < sec_MAX; i++) {
1.126 schwarze 406: printf("<option value=\"%s\"", sec_numbers[i]);
1.68 schwarze 407: if (NULL != req->q.sec &&
408: 0 == strcmp(sec_numbers[i], req->q.sec))
1.126 schwarze 409: printf(" selected=\"selected\"");
410: printf(">%s</option>\n", sec_names[i]);
1.68 schwarze 411: }
1.126 schwarze 412: puts("</select>");
1.68 schwarze 413:
414: /* Write architecture selector. */
415:
1.126 schwarze 416: printf( "<select name=\"arch\">\n"
417: "<option value=\"default\"");
1.81 schwarze 418: if (NULL == req->q.arch)
1.126 schwarze 419: printf(" selected=\"selected\"");
420: puts(">All Architectures</option>");
1.68 schwarze 421: for (i = 0; i < arch_MAX; i++) {
1.126 schwarze 422: printf("<option value=\"%s\"", arch_names[i]);
1.68 schwarze 423: if (NULL != req->q.arch &&
424: 0 == strcmp(arch_names[i], req->q.arch))
1.126 schwarze 425: printf(" selected=\"selected\"");
426: printf(">%s</option>\n", arch_names[i]);
1.68 schwarze 427: }
1.126 schwarze 428: puts("</select>");
1.68 schwarze 429:
430: /* Write manpath selector. */
431:
1.27 kristaps 432: if (req->psz > 1) {
1.126 schwarze 433: puts("<select name=\"manpath\">");
1.27 kristaps 434: for (i = 0; i < (int)req->psz; i++) {
1.126 schwarze 435: printf("<option ");
1.102 schwarze 436: if (strcmp(req->q.manpath, req->p[i]) == 0)
1.126 schwarze 437: printf("selected=\"selected\" ");
438: printf("value=\"");
1.52 schwarze 439: html_print(req->p[i]);
1.27 kristaps 440: printf("\">");
1.52 schwarze 441: html_print(req->p[i]);
1.126 schwarze 442: puts("</option>");
1.27 kristaps 443: }
1.126 schwarze 444: puts("</select>");
1.27 kristaps 445: }
1.68 schwarze 446:
1.132 schwarze 447: puts("</fieldset>\n"
1.142 ! schwarze 448: "</form>");
1.6 kristaps 449: }
450:
1.76 schwarze 451: static int
1.80 schwarze 452: validate_urifrag(const char *frag)
453: {
454:
455: while ('\0' != *frag) {
456: if ( ! (isalnum((unsigned char)*frag) ||
457: '-' == *frag || '.' == *frag ||
458: '/' == *frag || '_' == *frag))
1.109 schwarze 459: return 0;
1.80 schwarze 460: frag++;
461: }
1.109 schwarze 462: return 1;
1.80 schwarze 463: }
464:
465: static int
1.77 schwarze 466: validate_manpath(const struct req *req, const char* manpath)
467: {
468: size_t i;
469:
470: for (i = 0; i < req->psz; i++)
471: if ( ! strcmp(manpath, req->p[i]))
1.109 schwarze 472: return 1;
1.77 schwarze 473:
1.109 schwarze 474: return 0;
1.77 schwarze 475: }
476:
477: static int
1.76 schwarze 478: validate_filename(const char *file)
479: {
480:
481: if ('.' == file[0] && '/' == file[1])
482: file += 2;
483:
1.109 schwarze 484: return ! (strstr(file, "../") || strstr(file, "/..") ||
485: (strncmp(file, "man", 3) && strncmp(file, "cat", 3)));
1.76 schwarze 486: }
487:
1.6 kristaps 488: static void
1.72 schwarze 489: pg_index(const struct req *req)
1.6 kristaps 490: {
491:
492: resp_begin_html(200, NULL);
1.131 schwarze 493: resp_searchform(req, FOCUS_QUERY);
1.126 schwarze 494: printf("<p>\n"
1.86 schwarze 495: "This web interface is documented in the\n"
1.134 schwarze 496: "<a href=\"/%s%sman.cgi.8\">man.cgi(8)</a>\n"
1.86 schwarze 497: "manual, and the\n"
1.134 schwarze 498: "<a href=\"/%s%sapropos.1\">apropos(1)</a>\n"
1.69 schwarze 499: "manual explains the query syntax.\n"
1.126 schwarze 500: "</p>\n",
1.119 schwarze 501: scriptname, *scriptname == '\0' ? "" : "/",
502: scriptname, *scriptname == '\0' ? "" : "/");
1.6 kristaps 503: resp_end_html();
504: }
505:
506: static void
1.72 schwarze 507: pg_noresult(const struct req *req, const char *msg)
1.59 schwarze 508: {
509: resp_begin_html(200, NULL);
1.131 schwarze 510: resp_searchform(req, FOCUS_QUERY);
1.126 schwarze 511: puts("<p>");
1.59 schwarze 512: puts(msg);
1.126 schwarze 513: puts("</p>");
1.59 schwarze 514: resp_end_html();
515: }
516:
517: static void
1.72 schwarze 518: pg_error_badrequest(const char *msg)
1.9 kristaps 519: {
520:
1.59 schwarze 521: resp_begin_html(400, "Bad Request");
1.126 schwarze 522: puts("<h1>Bad Request</h1>\n"
523: "<p>\n");
1.59 schwarze 524: puts(msg);
525: printf("Try again from the\n"
1.126 schwarze 526: "<a href=\"/%s\">main page</a>.\n"
527: "</p>", scriptname);
1.9 kristaps 528: resp_end_html();
529: }
530:
531: static void
1.72 schwarze 532: pg_error_internal(void)
1.7 kristaps 533: {
534: resp_begin_html(500, "Internal Server Error");
1.126 schwarze 535: puts("<p>Internal Server Error</p>");
1.7 kristaps 536: resp_end_html();
537: }
538:
539: static void
1.72 schwarze 540: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1 kristaps 541: {
1.81 schwarze 542: char *arch, *archend;
1.120 schwarze 543: const char *sec;
544: size_t i, iuse;
1.81 schwarze 545: int archprio, archpriouse;
1.70 schwarze 546: int prio, priouse;
1.19 kristaps 547:
1.76 schwarze 548: for (i = 0; i < sz; i++) {
549: if (validate_filename(r[i].file))
550: continue;
1.128 schwarze 551: warnx("invalid filename %s in %s database",
1.76 schwarze 552: r[i].file, req->q.manpath);
553: pg_error_internal();
554: return;
555: }
556:
1.121 schwarze 557: if (req->isquery && sz == 1) {
1.6 kristaps 558: /*
559: * If we have just one result, then jump there now
560: * without any delay.
561: */
1.62 schwarze 562: printf("Status: 303 See Other\r\n");
1.119 schwarze 563: printf("Location: http://%s/%s%s%s/%s",
564: HTTP_HOST, scriptname,
565: *scriptname == '\0' ? "" : "/",
566: req->q.manpath, r[0].file);
1.62 schwarze 567: printf("\r\n"
568: "Content-Type: text/html; charset=utf-8\r\n"
569: "\r\n");
1.6 kristaps 570: return;
571: }
572:
1.12 kristaps 573: resp_begin_html(200, NULL);
1.131 schwarze 574: resp_searchform(req,
575: req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
1.1 kristaps 576:
1.123 schwarze 577: if (sz > 1) {
1.126 schwarze 578: puts("<div class=\"results\">");
579: puts("<table>");
1.123 schwarze 580:
581: for (i = 0; i < sz; i++) {
1.126 schwarze 582: printf("<tr>\n"
583: "<td class=\"title\">\n"
584: "<a href=\"/%s%s%s/%s",
1.123 schwarze 585: scriptname, *scriptname == '\0' ? "" : "/",
586: req->q.manpath, r[i].file);
587: printf("\">");
588: html_print(r[i].names);
1.126 schwarze 589: printf("</a>\n"
590: "</td>\n"
591: "<td class=\"desc\">");
1.123 schwarze 592: html_print(r[i].output);
1.126 schwarze 593: puts("</td>\n"
594: "</tr>");
1.123 schwarze 595: }
596:
1.126 schwarze 597: puts("</table>\n"
598: "</div>");
1.1 kristaps 599: }
1.16 kristaps 600:
1.70 schwarze 601: /*
602: * In man(1) mode, show one of the pages
603: * even if more than one is found.
604: */
605:
1.123 schwarze 606: if (req->q.equal || sz == 1) {
1.126 schwarze 607: puts("<hr>");
1.70 schwarze 608: iuse = 0;
1.120 schwarze 609: priouse = 20;
1.81 schwarze 610: archpriouse = 3;
1.70 schwarze 611: for (i = 0; i < sz; i++) {
1.120 schwarze 612: sec = r[i].file;
613: sec += strcspn(sec, "123456789");
614: if (sec[0] == '\0')
1.70 schwarze 615: continue;
1.120 schwarze 616: prio = sec_prios[sec[0] - '1'];
617: if (sec[1] != '/')
618: prio += 10;
619: if (req->q.arch == NULL) {
1.81 schwarze 620: archprio =
1.120 schwarze 621: ((arch = strchr(sec + 1, '/'))
622: == NULL) ? 3 :
623: ((archend = strchr(arch + 1, '/'))
624: == NULL) ? 0 :
1.81 schwarze 625: strncmp(arch, "amd64/",
626: archend - arch) ? 2 : 1;
627: if (archprio < archpriouse) {
628: archpriouse = archprio;
629: priouse = prio;
630: iuse = i;
631: continue;
632: }
633: if (archprio > archpriouse)
634: continue;
635: }
1.70 schwarze 636: if (prio >= priouse)
637: continue;
638: priouse = prio;
639: iuse = i;
640: }
641: resp_show(req, r[iuse].file);
642: }
643:
1.6 kristaps 644: resp_end_html();
645: }
646:
1.1 kristaps 647: static void
1.129 schwarze 648: resp_catman(const struct req *req, const char *file)
1.9 kristaps 649: {
1.10 kristaps 650: FILE *f;
1.115 schwarze 651: char *p;
652: size_t sz;
653: ssize_t len;
1.10 kristaps 654: int i;
655: int italic, bold;
1.9 kristaps 656:
1.115 schwarze 657: if ((f = fopen(file, "r")) == NULL) {
1.126 schwarze 658: puts("<p>You specified an invalid manual file.</p>");
1.9 kristaps 659: return;
660: }
661:
1.126 schwarze 662: puts("<div class=\"catman\">\n"
663: "<pre>");
1.10 kristaps 664:
1.115 schwarze 665: p = NULL;
666: sz = 0;
667:
668: while ((len = getline(&p, &sz, f)) != -1) {
1.10 kristaps 669: bold = italic = 0;
1.115 schwarze 670: for (i = 0; i < len - 1; i++) {
1.104 schwarze 671: /*
1.10 kristaps 672: * This means that the catpage is out of state.
673: * Ignore it and keep going (although the
674: * catpage is bogus).
675: */
676:
677: if ('\b' == p[i] || '\n' == p[i])
678: continue;
679:
680: /*
681: * Print a regular character.
682: * Close out any bold/italic scopes.
683: * If we're in back-space mode, make sure we'll
684: * have something to enter when we backspace.
685: */
686:
687: if ('\b' != p[i + 1]) {
688: if (italic)
1.126 schwarze 689: printf("</i>");
1.10 kristaps 690: if (bold)
1.126 schwarze 691: printf("</b>");
1.10 kristaps 692: italic = bold = 0;
693: html_putchar(p[i]);
694: continue;
1.115 schwarze 695: } else if (i + 2 >= len)
1.10 kristaps 696: continue;
697:
698: /* Italic mode. */
699:
700: if ('_' == p[i]) {
701: if (bold)
1.126 schwarze 702: printf("</b>");
1.10 kristaps 703: if ( ! italic)
1.126 schwarze 704: printf("<i>");
1.10 kristaps 705: bold = 0;
706: italic = 1;
707: i += 2;
708: html_putchar(p[i]);
709: continue;
710: }
711:
1.104 schwarze 712: /*
1.10 kristaps 713: * Handle funny behaviour troff-isms.
714: * These grok'd from the original man2html.c.
715: */
716:
717: if (('+' == p[i] && 'o' == p[i + 2]) ||
718: ('o' == p[i] && '+' == p[i + 2]) ||
719: ('|' == p[i] && '=' == p[i + 2]) ||
720: ('=' == p[i] && '|' == p[i + 2]) ||
721: ('*' == p[i] && '=' == p[i + 2]) ||
722: ('=' == p[i] && '*' == p[i + 2]) ||
723: ('*' == p[i] && '|' == p[i + 2]) ||
724: ('|' == p[i] && '*' == p[i + 2])) {
725: if (italic)
1.126 schwarze 726: printf("</i>");
1.10 kristaps 727: if (bold)
1.126 schwarze 728: printf("</b>");
1.10 kristaps 729: italic = bold = 0;
730: putchar('*');
731: i += 2;
732: continue;
733: } else if (('|' == p[i] && '-' == p[i + 2]) ||
734: ('-' == p[i] && '|' == p[i + 1]) ||
735: ('+' == p[i] && '-' == p[i + 1]) ||
736: ('-' == p[i] && '+' == p[i + 1]) ||
737: ('+' == p[i] && '|' == p[i + 1]) ||
738: ('|' == p[i] && '+' == p[i + 1])) {
739: if (italic)
1.126 schwarze 740: printf("</i>");
1.10 kristaps 741: if (bold)
1.126 schwarze 742: printf("</b>");
1.10 kristaps 743: italic = bold = 0;
744: putchar('+');
745: i += 2;
746: continue;
747: }
748:
749: /* Bold mode. */
1.104 schwarze 750:
1.10 kristaps 751: if (italic)
1.126 schwarze 752: printf("</i>");
1.10 kristaps 753: if ( ! bold)
1.126 schwarze 754: printf("<b>");
1.10 kristaps 755: bold = 1;
756: italic = 0;
757: i += 2;
758: html_putchar(p[i]);
759: }
760:
1.104 schwarze 761: /*
1.10 kristaps 762: * Clean up the last character.
1.104 schwarze 763: * We can get to a newline; don't print that.
1.10 kristaps 764: */
1.9 kristaps 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.9 kristaps 770:
1.115 schwarze 771: if (i == len - 1 && p[i] != '\n')
1.10 kristaps 772: html_putchar(p[i]);
1.9 kristaps 773:
1.10 kristaps 774: putchar('\n');
775: }
1.115 schwarze 776: free(p);
1.10 kristaps 777:
1.126 schwarze 778: puts("</pre>\n"
779: "</div>");
1.10 kristaps 780:
781: fclose(f);
1.9 kristaps 782: }
783:
784: static void
1.129 schwarze 785: resp_format(const struct req *req, const char *file)
1.7 kristaps 786: {
1.106 schwarze 787: struct manoutput conf;
1.8 kristaps 788: struct mparse *mp;
1.107 schwarze 789: struct roff_man *man;
1.8 kristaps 790: void *vp;
1.90 schwarze 791: int fd;
792: int usepath;
1.7 kristaps 793:
1.8 kristaps 794: if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.126 schwarze 795: puts("<p>You specified an invalid manual file.</p>");
1.7 kristaps 796: return;
797: }
798:
1.110 schwarze 799: mchars_alloc();
1.136 schwarze 800: mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1,
801: MANDOCLEVEL_BADARG, NULL, req->q.manpath);
1.103 schwarze 802: mparse_readfd(mp, fd, file);
1.8 kristaps 803: close(fd);
1.7 kristaps 804:
1.106 schwarze 805: memset(&conf, 0, sizeof(conf));
806: conf.fragment = 1;
1.90 schwarze 807: usepath = strcmp(req->q.manpath, req->p[0]);
1.122 schwarze 808: mandoc_asprintf(&conf.man, "/%s%s%%N.%%S",
809: usepath ? req->q.manpath : "", usepath ? "/" : "");
1.10 kristaps 810:
1.108 schwarze 811: mparse_result(mp, &man, NULL);
812: if (man == NULL) {
1.128 schwarze 813: warnx("fatal mandoc error: %s/%s", req->q.manpath, file);
1.72 schwarze 814: pg_error_internal();
1.32 kristaps 815: mparse_free(mp);
1.110 schwarze 816: mchars_free();
1.32 kristaps 817: return;
818: }
819:
1.110 schwarze 820: vp = html_alloc(&conf);
1.7 kristaps 821:
1.111 schwarze 822: if (man->macroset == MACROSET_MDOC) {
823: mdoc_validate(man);
1.108 schwarze 824: html_mdoc(vp, man);
1.112 schwarze 825: } else {
826: man_validate(man);
1.8 kristaps 827: html_man(vp, man);
1.112 schwarze 828: }
1.32 kristaps 829:
1.8 kristaps 830: html_free(vp);
831: mparse_free(mp);
1.110 schwarze 832: mchars_free();
1.106 schwarze 833: free(conf.man);
1.7 kristaps 834: }
835:
836: static void
1.70 schwarze 837: resp_show(const struct req *req, const char *file)
838: {
1.76 schwarze 839:
840: if ('.' == file[0] && '/' == file[1])
1.71 schwarze 841: file += 2;
1.70 schwarze 842:
843: if ('c' == *file)
1.129 schwarze 844: resp_catman(req, file);
1.70 schwarze 845: else
1.129 schwarze 846: resp_format(req, file);
1.70 schwarze 847: }
848:
849: static void
1.84 schwarze 850: pg_show(struct req *req, const char *fullpath)
1.1 kristaps 851: {
1.84 schwarze 852: char *manpath;
853: const char *file;
1.25 kristaps 854:
1.84 schwarze 855: if ((file = strchr(fullpath, '/')) == NULL) {
1.72 schwarze 856: pg_error_badrequest(
1.59 schwarze 857: "You did not specify a page to show.");
1.25 kristaps 858: return;
1.104 schwarze 859: }
1.84 schwarze 860: manpath = mandoc_strndup(fullpath, file - fullpath);
861: file++;
1.6 kristaps 862:
1.84 schwarze 863: if ( ! validate_manpath(req, manpath)) {
1.77 schwarze 864: pg_error_badrequest(
865: "You specified an invalid manpath.");
1.84 schwarze 866: free(manpath);
1.77 schwarze 867: return;
868: }
869:
1.24 kristaps 870: /*
1.58 schwarze 871: * Begin by chdir()ing into the manpath.
1.24 kristaps 872: * This way we can pick up the database files, which are
873: * relative to the manpath root.
874: */
875:
1.84 schwarze 876: if (chdir(manpath) == -1) {
1.128 schwarze 877: warn("chdir %s", manpath);
1.77 schwarze 878: pg_error_internal();
1.84 schwarze 879: free(manpath);
1.76 schwarze 880: return;
881: }
1.134 schwarze 882: free(manpath);
1.84 schwarze 883:
884: if ( ! validate_filename(file)) {
1.76 schwarze 885: pg_error_badrequest(
886: "You specified an invalid manual file.");
1.24 kristaps 887: return;
888: }
1.79 schwarze 889:
1.70 schwarze 890: resp_begin_html(200, NULL);
1.131 schwarze 891: resp_searchform(req, FOCUS_NONE);
1.84 schwarze 892: resp_show(req, file);
1.70 schwarze 893: resp_end_html();
1.6 kristaps 894: }
895:
896: static void
1.66 schwarze 897: pg_search(const struct req *req)
1.6 kristaps 898: {
1.52 schwarze 899: struct mansearch search;
900: struct manpaths paths;
901: struct manpage *res;
1.97 schwarze 902: char **argv;
903: char *query, *rp, *wp;
1.52 schwarze 904: size_t ressz;
1.97 schwarze 905: int argc;
1.6 kristaps 906:
907: /*
1.24 kristaps 908: * Begin by chdir()ing into the root of the manpath.
909: * This way we can pick up the database files, which are
910: * relative to the manpath root.
911: */
912:
1.128 schwarze 913: if (chdir(req->q.manpath) == -1) {
914: warn("chdir %s", req->q.manpath);
1.77 schwarze 915: pg_error_internal();
1.24 kristaps 916: return;
917: }
918:
1.52 schwarze 919: search.arch = req->q.arch;
920: search.sec = req->q.sec;
1.94 schwarze 921: search.outkey = "Nd";
922: search.argmode = req->q.equal ? ARG_NAME : ARG_EXPR;
1.101 schwarze 923: search.firstmatch = 1;
1.52 schwarze 924:
925: paths.sz = 1;
926: paths.paths = mandoc_malloc(sizeof(char *));
927: paths.paths[0] = mandoc_strdup(".");
1.24 kristaps 928:
929: /*
1.97 schwarze 930: * Break apart at spaces with backslash-escaping.
1.6 kristaps 931: */
932:
1.97 schwarze 933: argc = 0;
934: argv = NULL;
935: rp = query = mandoc_strdup(req->q.query);
936: for (;;) {
937: while (isspace((unsigned char)*rp))
938: rp++;
939: if (*rp == '\0')
940: break;
941: argv = mandoc_reallocarray(argv, argc + 1, sizeof(char *));
942: argv[argc++] = wp = rp;
943: for (;;) {
944: if (isspace((unsigned char)*rp)) {
945: *wp = '\0';
946: rp++;
947: break;
948: }
949: if (rp[0] == '\\' && rp[1] != '\0')
950: rp++;
951: if (wp != rp)
952: *wp = *rp;
953: if (*rp == '\0')
954: break;
955: wp++;
956: rp++;
957: }
1.6 kristaps 958: }
959:
1.97 schwarze 960: if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz))
1.72 schwarze 961: pg_noresult(req, "You entered an invalid query.");
1.59 schwarze 962: else if (0 == ressz)
1.72 schwarze 963: pg_noresult(req, "No results found.");
1.59 schwarze 964: else
1.72 schwarze 965: pg_searchres(req, res, ressz);
1.6 kristaps 966:
1.97 schwarze 967: free(query);
968: mansearch_free(res, ressz);
1.52 schwarze 969: free(paths.paths[0]);
970: free(paths.paths);
1.1 kristaps 971: }
972:
973: int
974: main(void)
975: {
1.66 schwarze 976: struct req req;
1.95 schwarze 977: struct itimerval itimer;
1.66 schwarze 978: const char *path;
1.83 schwarze 979: const char *querystring;
1.1 kristaps 980: int i;
1.95 schwarze 981:
982: /* Poor man's ReDoS mitigation. */
983:
1.99 schwarze 984: itimer.it_value.tv_sec = 2;
1.95 schwarze 985: itimer.it_value.tv_usec = 0;
1.99 schwarze 986: itimer.it_interval.tv_sec = 2;
1.95 schwarze 987: itimer.it_interval.tv_usec = 0;
988: if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) {
1.128 schwarze 989: warn("setitimer");
1.80 schwarze 990: pg_error_internal();
1.109 schwarze 991: return EXIT_FAILURE;
1.80 schwarze 992: }
993:
1.24 kristaps 994: /*
1.67 schwarze 995: * First we change directory into the MAN_DIR so that
1.24 kristaps 996: * subsequent scanning for manpath directories is rooted
997: * relative to the same position.
998: */
999:
1.128 schwarze 1000: if (chdir(MAN_DIR) == -1) {
1001: warn("MAN_DIR: %s", MAN_DIR);
1.72 schwarze 1002: pg_error_internal();
1.109 schwarze 1003: return EXIT_FAILURE;
1.104 schwarze 1004: }
1.24 kristaps 1005:
1006: memset(&req, 0, sizeof(struct req));
1.118 schwarze 1007: req.q.equal = 1;
1.129 schwarze 1008: parse_manpath_conf(&req);
1.1 kristaps 1009:
1.117 schwarze 1010: /* Parse the path info and the query string. */
1.1 kristaps 1011:
1.117 schwarze 1012: if ((path = getenv("PATH_INFO")) == NULL)
1013: path = "";
1014: else if (*path == '/')
1015: path++;
1016:
1.124 schwarze 1017: if (*path != '\0') {
1.129 schwarze 1018: parse_path_info(&req, path);
1.133 schwarze 1019: if (req.q.manpath == NULL || access(path, F_OK) == -1)
1.124 schwarze 1020: path = "";
1.117 schwarze 1021: } else if ((querystring = getenv("QUERY_STRING")) != NULL)
1.129 schwarze 1022: parse_query_string(&req, querystring);
1.77 schwarze 1023:
1.117 schwarze 1024: /* Validate parsed data and add defaults. */
1025:
1.102 schwarze 1026: if (req.q.manpath == NULL)
1027: req.q.manpath = mandoc_strdup(req.p[0]);
1028: else if ( ! validate_manpath(&req, req.q.manpath)) {
1.77 schwarze 1029: pg_error_badrequest(
1030: "You specified an invalid manpath.");
1.109 schwarze 1031: return EXIT_FAILURE;
1.77 schwarze 1032: }
1.1 kristaps 1033:
1.80 schwarze 1034: if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) {
1035: pg_error_badrequest(
1036: "You specified an invalid architecture.");
1.109 schwarze 1037: return EXIT_FAILURE;
1.80 schwarze 1038: }
1039:
1.66 schwarze 1040: /* Dispatch to the three different pages. */
1.1 kristaps 1041:
1.66 schwarze 1042: if ('\0' != *path)
1043: pg_show(&req, path);
1.85 schwarze 1044: else if (NULL != req.q.query)
1.66 schwarze 1045: pg_search(&req);
1046: else
1.72 schwarze 1047: pg_index(&req);
1.1 kristaps 1048:
1.83 schwarze 1049: free(req.q.manpath);
1050: free(req.q.arch);
1051: free(req.q.sec);
1.85 schwarze 1052: free(req.q.query);
1.52 schwarze 1053: for (i = 0; i < (int)req.psz; i++)
1054: free(req.p[i]);
1.24 kristaps 1055: free(req.p);
1.109 schwarze 1056: return EXIT_SUCCESS;
1.117 schwarze 1057: }
1058:
1059: /*
1060: * If PATH_INFO is not a file name, translate it to a query.
1061: */
1062: static void
1.129 schwarze 1063: parse_path_info(struct req *req, const char *path)
1.117 schwarze 1064: {
1.135 schwarze 1065: char *dir[4];
1066: int i;
1.117 schwarze 1067:
1.121 schwarze 1068: req->isquery = 0;
1.117 schwarze 1069: req->q.equal = 1;
1070: req->q.manpath = mandoc_strdup(path);
1.135 schwarze 1071: req->q.arch = NULL;
1.117 schwarze 1072:
1073: /* Mandatory manual page name. */
1074: if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) {
1075: req->q.query = req->q.manpath;
1076: req->q.manpath = NULL;
1077: } else
1078: *req->q.query++ = '\0';
1079:
1080: /* Optional trailing section. */
1081: if ((req->q.sec = strrchr(req->q.query, '.')) != NULL) {
1082: if(isdigit((unsigned char)req->q.sec[1])) {
1083: *req->q.sec++ = '\0';
1084: req->q.sec = mandoc_strdup(req->q.sec);
1085: } else
1086: req->q.sec = NULL;
1087: }
1088:
1089: /* Handle the case of name[.section] only. */
1.135 schwarze 1090: if (req->q.manpath == NULL)
1.117 schwarze 1091: return;
1.135 schwarze 1092: req->q.query = mandoc_strdup(req->q.query);
1093:
1094: /* Split directory components. */
1095: dir[i = 0] = req->q.manpath;
1096: while ((dir[i + 1] = strchr(dir[i], '/')) != NULL) {
1097: if (++i == 3) {
1098: pg_error_badrequest(
1099: "You specified too many directory components.");
1100: exit(EXIT_FAILURE);
1101: }
1102: *dir[i]++ = '\0';
1.117 schwarze 1103: }
1104:
1.135 schwarze 1105: /* Optional manpath. */
1106: if ((i = validate_manpath(req, req->q.manpath)) == 0)
1107: req->q.manpath = NULL;
1108: else if (dir[1] == NULL)
1109: return;
1.117 schwarze 1110:
1.135 schwarze 1111: /* Optional section. */
1112: if (strncmp(dir[i], "man", 3) == 0) {
1.127 schwarze 1113: free(req->q.sec);
1.135 schwarze 1114: req->q.sec = mandoc_strdup(dir[i++] + 3);
1.117 schwarze 1115: }
1.135 schwarze 1116: if (dir[i] == NULL) {
1117: if (req->q.manpath == NULL)
1118: free(dir[0]);
1119: return;
1120: }
1121: if (dir[i + 1] != NULL) {
1122: pg_error_badrequest(
1123: "You specified an invalid directory component.");
1124: exit(EXIT_FAILURE);
1125: }
1126:
1127: /* Optional architecture. */
1128: if (i) {
1129: req->q.arch = mandoc_strdup(dir[i]);
1130: if (req->q.manpath == NULL)
1131: free(dir[0]);
1132: } else
1133: req->q.arch = dir[0];
1.24 kristaps 1134: }
1135:
1136: /*
1137: * Scan for indexable paths.
1138: */
1139: static void
1.129 schwarze 1140: parse_manpath_conf(struct req *req)
1.24 kristaps 1141: {
1.54 schwarze 1142: FILE *fp;
1143: char *dp;
1144: size_t dpsz;
1.115 schwarze 1145: ssize_t len;
1.54 schwarze 1146:
1.128 schwarze 1147: if ((fp = fopen("manpath.conf", "r")) == NULL) {
1148: warn("%s/manpath.conf", MAN_DIR);
1.74 schwarze 1149: pg_error_internal();
1150: exit(EXIT_FAILURE);
1151: }
1.24 kristaps 1152:
1.115 schwarze 1153: dp = NULL;
1154: dpsz = 0;
1155:
1156: while ((len = getline(&dp, &dpsz, fp)) != -1) {
1157: if (dp[len - 1] == '\n')
1158: dp[--len] = '\0';
1.54 schwarze 1159: req->p = mandoc_realloc(req->p,
1160: (req->psz + 1) * sizeof(char *));
1.80 schwarze 1161: if ( ! validate_urifrag(dp)) {
1.128 schwarze 1162: warnx("%s/manpath.conf contains "
1163: "unsafe path \"%s\"", MAN_DIR, dp);
1.80 schwarze 1164: pg_error_internal();
1165: exit(EXIT_FAILURE);
1166: }
1.128 schwarze 1167: if (strchr(dp, '/') != NULL) {
1168: warnx("%s/manpath.conf contains "
1169: "path with slash \"%s\"", MAN_DIR, dp);
1.80 schwarze 1170: pg_error_internal();
1171: exit(EXIT_FAILURE);
1172: }
1173: req->p[req->psz++] = dp;
1.115 schwarze 1174: dp = NULL;
1175: dpsz = 0;
1.74 schwarze 1176: }
1.115 schwarze 1177: free(dp);
1.74 schwarze 1178:
1.128 schwarze 1179: if (req->p == NULL) {
1180: warnx("%s/manpath.conf is empty", MAN_DIR);
1.74 schwarze 1181: pg_error_internal();
1182: exit(EXIT_FAILURE);
1.24 kristaps 1183: }
1184: }
CVSweb