1 /* $Id: cgi.c,v 1.45 2013/06/05 02:00:26 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 #include "apropos_db.h"
64 * A query as passed to the search function.
67 const char *arch; /* architecture */
68 const char *sec; /* manual section */
69 const char *expr; /* unparsed expression string */
70 int manroot; /* manroot index (or -1)*/
71 int legacy; /* whether legacy mode */
81 static int atou(const char *, unsigned *);
82 static void catman(const struct req *, const char *);
83 static int cmp(const void *, const void *);
84 static void format(const struct req *, const char *);
85 static void html_print(const char *);
86 static void html_printquery(const struct req *);
87 static void html_putchar(char);
88 static int http_decode(char *);
89 static void http_parse(struct req *, char *);
90 static void http_print(const char *);
91 static void http_putchar(char);
92 static void http_printquery(const struct req *);
93 static int pathstop(DIR *);
94 static void pathgen(DIR *, char *, struct req *);
95 static void pg_index(const struct req *, char *);
96 static void pg_search(const struct req *, char *);
97 static void pg_show(const struct req *, char *);
98 static void resp_bad(void);
99 static void resp_baddb(void);
100 static void resp_error400(void);
101 static void resp_error404(const char *);
102 static void resp_begin_html(int, const char *);
103 static void resp_begin_http(int, const char *);
104 static void resp_end_html(void);
105 static void resp_index(const struct req *);
106 static void resp_search(struct res *, size_t, void *);
107 static void resp_searchform(const struct req *);
109 static const char *progname; /* cgi script name */
110 static const char *cache; /* cache directory */
111 static const char *css; /* css directory */
112 static const char *host; /* hostname */
114 static const char * const pages[PAGE__MAX] = {
115 "index", /* PAGE_INDEX */
116 "search", /* PAGE_SEARCH */
117 "show", /* PAGE_SHOW */
121 * This is just OpenBSD's strtol(3) suggestion.
122 * I use it instead of strtonum(3) for portability's sake.
125 atou(const char *buf, unsigned *v)
131 lval = strtol(buf, &ep, 10);
132 if (buf[0] == '\0' || *ep != '\0')
134 if ((errno == ERANGE && (lval == LONG_MAX ||
135 lval == LONG_MIN)) ||
136 (lval > INT_MAX || lval < 0))
139 *v = (unsigned int)lval;
144 * Print a character, escaping HTML along the way.
145 * This will pass non-ASCII straight to output: be warned!
165 putchar((unsigned char)c);
170 http_printquery(const struct req *req)
174 http_print(req->q.expr ? req->q.expr : "");
176 http_print(req->q.sec ? req->q.sec : "");
178 http_print(req->q.arch ? req->q.arch : "");
183 html_printquery(const struct req *req)
186 printf("&expr=");
187 html_print(req->q.expr ? req->q.expr : "");
189 html_print(req->q.sec ? req->q.sec : "");
190 printf("&arch=");
191 html_print(req->q.arch ? req->q.arch : "");
195 http_print(const char *p)
205 * Call through to html_putchar().
206 * Accepts NULL strings.
209 html_print(const char *p)
219 * Parse out key-value pairs from an HTTP request variable.
220 * This can be either a cookie or a POST/GET string, although man.cgi
221 * uses only GET for simplicity.
224 http_parse(struct req *req, char *p)
226 char *key, *val, *manroot;
229 memset(&req->q, 0, sizeof(struct query));
238 p += (int)strcspn(p, ";&");
241 if (NULL != (val = strchr(key, '=')))
244 if ('\0' == *key || NULL == val || '\0' == *val)
247 /* Just abort handling. */
249 if ( ! http_decode(key))
251 if (NULL != val && ! http_decode(val))
254 if (0 == strcmp(key, "expr"))
256 else if (0 == strcmp(key, "query"))
258 else if (0 == strcmp(key, "sec"))
260 else if (0 == strcmp(key, "sektion"))
262 else if (0 == strcmp(key, "arch"))
264 else if (0 == strcmp(key, "manpath"))
266 else if (0 == strcmp(key, "apropos"))
267 legacy = 0 == strcmp(val, "0");
270 /* Test for old man.cgi compatibility mode. */
272 req->q.legacy = legacy > 0;
275 * Section "0" means no section when in legacy mode.
276 * For some man.cgi scripts, "default" arch is none.
279 if (req->q.legacy && NULL != req->q.sec)
280 if (0 == strcmp(req->q.sec, "0"))
282 if (req->q.legacy && NULL != req->q.arch)
283 if (0 == strcmp(req->q.arch, "default"))
286 /* Default to first manroot. */
288 if (NULL != manroot) {
289 for (i = 0; i < (int)req->psz; i++)
290 if (0 == strcmp(req->p[i].name, manroot))
292 req->q.manroot = i < (int)req->psz ? i : -1;
300 if (isalnum((unsigned char)c)) {
301 putchar((unsigned char)c);
303 } else if (' ' == c) {
311 * HTTP-decode a string. The standard explanation is that this turns
312 * "%4e+foo" into "n foo" in the regular way. This is done in-place
313 * over the allocated string.
323 for ( ; '\0' != *p; p++) {
325 if ('\0' == (hex[0] = *(p + 1)))
327 if ('\0' == (hex[1] = *(p + 2)))
329 if (1 != sscanf(hex, "%x", &c))
335 memmove(p + 1, p + 3, strlen(p + 3) + 1);
337 *p = '+' == *p ? ' ' : *p;
345 resp_begin_http(int code, const char *msg)
349 printf("Status: %d %s\n", code, msg);
351 puts("Content-Type: text/html; charset=utf-8\n"
352 "Cache-Control: no-cache\n"
360 resp_begin_html(int code, const char *msg)
363 resp_begin_http(code, msg);
365 printf("<!DOCTYPE HTML PUBLIC "
366 " \"-//W3C//DTD HTML 4.01//EN\""
367 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
370 "<META HTTP-EQUIV=\"Content-Type\""
371 " CONTENT=\"text/html; charset=utf-8\">\n"
372 "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
373 " TYPE=\"text/css\" media=\"all\">\n"
374 "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
375 " TYPE=\"text/css\" media=\"all\">\n"
376 "<TITLE>System Manpage Reference</TITLE>\n"
379 "<!-- Begin page content. //-->\n", css, css);
391 resp_searchform(const struct req *req)
395 puts("<!-- Begin search form. //-->");
396 printf("<DIV ID=\"mancgi\">\n"
397 "<FORM ACTION=\"%s/search.html\" METHOD=\"get\">\n"
399 "<LEGEND>Search Parameters</LEGEND>\n"
400 "<INPUT TYPE=\"submit\" "
401 " VALUE=\"Search\"> for manuals satisfying \n"
402 "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
404 html_print(req->q.expr ? req->q.expr : "");
405 printf("\">, section "
406 "<INPUT TYPE=\"text\""
407 " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
408 html_print(req->q.sec ? req->q.sec : "");
410 "<INPUT TYPE=\"text\""
411 " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
412 html_print(req->q.arch ? req->q.arch : "");
415 puts(", <SELECT NAME=\"manpath\">");
416 for (i = 0; i < (int)req->psz; i++) {
417 printf("<OPTION %s VALUE=\"",
418 (i == req->q.manroot) ||
419 (0 == i && -1 == req->q.manroot) ?
420 "SELECTED=\"selected\"" : "");
421 html_print(req->p[i].name);
423 html_print(req->p[i].name);
429 "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
433 puts("<!-- End search form. //-->");
437 resp_index(const struct req *req)
440 resp_begin_html(200, NULL);
441 resp_searchform(req);
449 resp_begin_html(400, "Query Malformed");
450 printf("<H1>Malformed Query</H1>\n"
452 "The query your entered was malformed.\n"
453 "Try again from the\n"
454 "<A HREF=\"%s/index.html\">main page</A>.\n"
460 resp_error404(const char *page)
463 resp_begin_html(404, "Not Found");
464 puts("<H1>Page Not Found</H1>\n"
466 "The page you're looking for, ");
470 "could not be found.\n"
471 "Try searching from the\n"
472 "<A HREF=\"%s/index.html\">main page</A>.\n"
480 resp_begin_html(500, "Internal Server Error");
481 puts("<P>Generic badness happened.</P>");
489 resp_begin_html(500, "Internal Server Error");
490 puts("<P>Your database is broken.</P>");
495 resp_search(struct res *r, size_t sz, void *arg)
498 const struct req *req;
500 req = (const struct req *)arg;
503 assert(req->q.manroot >= 0);
505 for (matched = i = 0; i < sz; i++)
510 for (i = 0; i < sz; i++)
514 * If we have just one result, then jump there now
517 puts("Status: 303 See Other");
518 printf("Location: http://%s%s/show/%d/%u/%u.html?",
519 host, progname, req->q.manroot,
520 r[i].volume, r[i].rec);
521 http_printquery(req);
523 "Content-Type: text/html; charset=utf-8\n");
527 resp_begin_html(200, NULL);
528 resp_searchform(req);
530 puts("<DIV CLASS=\"results\">");
534 "No results found.\n"
541 qsort(r, sz, sizeof(struct res), cmp);
545 for (i = 0; i < sz; i++) {
549 "<TD CLASS=\"title\">\n"
550 "<A HREF=\"%s/show/%d/%u/%u.html?",
551 progname, req->q.manroot,
552 r[i].volume, r[i].rec);
553 html_printquery(req);
555 html_print(r[i].title);
557 html_print(r[i].cat);
558 if (r[i].arch && '\0' != *r[i].arch) {
560 html_print(r[i].arch);
564 "<TD CLASS=\"desc\">");
565 html_print(r[i].desc);
577 pg_index(const struct req *req, char *path)
584 catman(const struct req *req, const char *file)
592 if (NULL == (f = fopen(file, "r"))) {
597 resp_begin_html(200, NULL);
598 resp_searchform(req);
599 puts("<DIV CLASS=\"catman\">\n"
602 while (NULL != (p = fgetln(f, &len))) {
604 for (i = 0; i < (int)len - 1; i++) {
606 * This means that the catpage is out of state.
607 * Ignore it and keep going (although the
611 if ('\b' == p[i] || '\n' == p[i])
615 * Print a regular character.
616 * Close out any bold/italic scopes.
617 * If we're in back-space mode, make sure we'll
618 * have something to enter when we backspace.
621 if ('\b' != p[i + 1]) {
629 } else if (i + 2 >= (int)len)
647 * Handle funny behaviour troff-isms.
648 * These grok'd from the original man2html.c.
651 if (('+' == p[i] && 'o' == p[i + 2]) ||
652 ('o' == p[i] && '+' == p[i + 2]) ||
653 ('|' == p[i] && '=' == p[i + 2]) ||
654 ('=' == p[i] && '|' == p[i + 2]) ||
655 ('*' == p[i] && '=' == p[i + 2]) ||
656 ('=' == p[i] && '*' == p[i + 2]) ||
657 ('*' == p[i] && '|' == p[i + 2]) ||
658 ('|' == p[i] && '*' == p[i + 2])) {
667 } else if (('|' == p[i] && '-' == p[i + 2]) ||
668 ('-' == p[i] && '|' == p[i + 1]) ||
669 ('+' == p[i] && '-' == p[i + 1]) ||
670 ('-' == p[i] && '+' == p[i + 1]) ||
671 ('+' == p[i] && '|' == p[i + 1]) ||
672 ('|' == p[i] && '+' == p[i + 1])) {
696 * Clean up the last character.
697 * We can get to a newline; don't print that.
705 if (i == (int)len - 1 && '\n' != p[i])
720 format(const struct req *req, const char *file)
728 char opts[PATH_MAX + 128];
730 if (-1 == (fd = open(file, O_RDONLY, 0))) {
735 mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL, NULL);
736 rc = mparse_readfd(mp, fd, file);
739 if (rc >= MANDOCLEVEL_FATAL) {
744 snprintf(opts, sizeof(opts), "fragment,"
745 "man=%s/search.html?sec=%%S&expr=Nm~^%%N$,"
746 /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
749 mparse_result(mp, &mdoc, &man);
750 if (NULL == man && NULL == mdoc) {
756 resp_begin_html(200, NULL);
757 resp_searchform(req);
759 vp = html_alloc(opts);
774 pg_show(const struct req *req, char *path)
782 unsigned int vol, rec, mr;
788 /* Parse out mroot, volume, and record from the path. */
790 if (NULL == path || NULL == (sub = strchr(path, '/'))) {
795 if ( ! atou(path, &mr)) {
800 if (NULL == (sub = strchr(path, '/'))) {
805 if ( ! atou(path, &vol) || ! atou(sub, &rec)) {
808 } else if (mr >= (unsigned int)req->psz) {
814 * Begin by chdir()ing into the manroot.
815 * This way we can pick up the database files, which are
816 * relative to the manpath root.
819 if (-1 == chdir(req->p[(int)mr].path)) {
820 perror(req->p[(int)mr].path);
825 memset(&ps, 0, sizeof(struct manpaths));
826 manpath_manconf(&ps, "etc/catman.conf");
828 if (vol >= (unsigned int)ps.sz) {
833 sz = strlcpy(file, ps.paths[vol], PATH_MAX);
834 assert(sz < PATH_MAX);
835 strlcat(file, "/", PATH_MAX);
836 strlcat(file, MANDOC_IDX, PATH_MAX);
838 /* Open the index recno(3) database. */
840 idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
850 if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
851 rc < 0 ? resp_baddb() : resp_error400();
853 } else if (0 == val.size) {
858 cp = (char *)val.data;
861 if (NULL == memchr(cp, '\0', val.size - 1))
864 file[(int)sz] = '\0';
865 strlcat(file, "/", PATH_MAX);
866 strlcat(file, cp, PATH_MAX);
879 pg_search(const struct req *req, char *path)
884 const char *ep, *start;
890 if (req->q.manroot < 0 || 0 == req->psz) {
891 resp_search(NULL, 0, (void *)req);
895 memset(&opt, 0, sizeof(struct opts));
898 opt.arch = req->q.arch;
899 opt.cat = req->q.sec;
907 * Begin by chdir()ing into the root of the manpath.
908 * This way we can pick up the database files, which are
909 * relative to the manpath root.
912 assert(req->q.manroot < (int)req->psz);
913 if (-1 == (chdir(req->p[req->q.manroot].path))) {
914 perror(req->p[req->q.manroot].path);
915 resp_search(NULL, 0, (void *)req);
919 memset(&ps, 0, sizeof(struct manpaths));
920 manpath_manconf(&ps, "etc/catman.conf");
923 * Poor man's tokenisation: just break apart by spaces.
924 * Yes, this is half-ass. But it works for now.
927 while (ep && isspace((unsigned char)*ep))
930 while (ep && '\0' != *ep) {
931 cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *));
933 while ('\0' != *ep && ! isspace((unsigned char)*ep))
935 cp[sz] = mandoc_malloc((ep - start) + 1);
936 memcpy(cp[sz], start, ep - start);
937 cp[sz++][ep - start] = '\0';
938 while (isspace((unsigned char)*ep))
943 * Pump down into apropos backend.
944 * The resp_search() function is called with the results.
947 expr = req->q.legacy ?
948 termcomp(sz, cp, &tt) : exprcomp(sz, cp, &tt);
952 (ps.sz, ps.paths, &opt, expr, tt,
953 (void *)req, &ressz, &res, resp_search);
955 /* ...unless errors occured. */
960 resp_search(NULL, 0, NULL);
962 for (i = 0; i < sz; i++)
978 char *p, *path, *subpath;
980 /* Scan our run-time environment. */
982 if (NULL == (cache = getenv("CACHE_DIR")))
983 cache = "/cache/man.cgi";
985 if (NULL == (progname = getenv("SCRIPT_NAME")))
988 if (NULL == (css = getenv("CSS_DIR")))
991 if (NULL == (host = getenv("HTTP_HOST")))
995 * First we change directory into the cache directory so that
996 * subsequent scanning for manpath directories is rooted
997 * relative to the same position.
1000 if (-1 == chdir(cache)) {
1003 return(EXIT_FAILURE);
1004 } else if (NULL == (cwd = opendir(cache))) {
1007 return(EXIT_FAILURE);
1010 memset(&req, 0, sizeof(struct req));
1012 strlcpy(buf, ".", PATH_MAX);
1013 pathgen(cwd, buf, &req);
1016 /* Next parse out the query string. */
1018 if (NULL != (p = getenv("QUERY_STRING")))
1019 http_parse(&req, p);
1022 * Now juggle paths to extract information.
1023 * We want to extract our filetype (the file suffix), the
1024 * initial path component, then the trailing component(s).
1025 * Start with leading subpath component.
1028 subpath = path = NULL;
1029 req.page = PAGE__MAX;
1031 if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
1032 req.page = PAGE_INDEX;
1034 if (NULL != path && '/' == *path && '\0' == *++path)
1035 req.page = PAGE_INDEX;
1037 /* Strip file suffix. */
1039 if (NULL != path && NULL != (p = strrchr(path, '.')))
1040 if (NULL != p && NULL == strchr(p, '/'))
1043 /* Resolve subpath component. */
1045 if (NULL != path && NULL != (subpath = strchr(path, '/')))
1048 /* Map path into one we recognise. */
1050 if (NULL != path && '\0' != *path)
1051 for (i = 0; i < (int)PAGE__MAX; i++)
1052 if (0 == strcmp(pages[i], path)) {
1053 req.page = (enum page)i;
1061 pg_index(&req, subpath);
1064 pg_search(&req, subpath);
1067 pg_show(&req, subpath);
1070 resp_error404(path);
1074 for (i = 0; i < (int)req.psz; i++) {
1075 free(req.p[i].path);
1076 free(req.p[i].name);
1080 return(EXIT_SUCCESS);
1084 cmp(const void *p1, const void *p2)
1087 return(strcasecmp(((const struct res *)p1)->title,
1088 ((const struct res *)p2)->title));
1092 * Check to see if an "etc" path consists of a catman.conf file. If it
1093 * does, that means that the path contains a tree created by catman(8)
1094 * and should be used for indexing.
1101 while (NULL != (d = readdir(dir)))
1102 if (DT_REG == d->d_type)
1103 if (0 == strcmp(d->d_name, "catman.conf"))
1110 * Scan for indexable paths.
1111 * This adds all paths with "etc/catman.conf" to the buffer.
1114 pathgen(DIR *dir, char *path, struct req *req)
1122 sz = strlcat(path, "/", PATH_MAX);
1123 if (sz >= PATH_MAX) {
1124 fprintf(stderr, "%s: Path too long", path);
1129 * First, scan for the "etc" directory.
1130 * If it's found, then see if it should cause us to stop. This
1131 * happens when a catman.conf is found in the directory.
1135 while (0 == rc && NULL != (d = readdir(dir))) {
1136 if (DT_DIR != d->d_type || strcmp(d->d_name, "etc"))
1139 path[(int)sz] = '\0';
1140 ssz = strlcat(path, d->d_name, PATH_MAX);
1142 if (ssz >= PATH_MAX) {
1143 fprintf(stderr, "%s: Path too long", path);
1145 } else if (NULL == (cd = opendir(path))) {
1155 /* This also strips the trailing slash. */
1156 path[(int)--sz] = '\0';
1157 req->p = mandoc_realloc
1159 (req->psz + 1) * sizeof(struct paths));
1161 * Strip out the leading "./" unless we're just a ".",
1162 * in which case use an empty string as our name.
1164 req->p[(int)req->psz].path = mandoc_strdup(path);
1165 req->p[(int)req->psz].name =
1166 cp = mandoc_strdup(path + (1 == sz ? 1 : 2));
1169 * The name is just the path with all the slashes taken
1170 * out of it. Simple but effective.
1172 for ( ; '\0' != *cp; cp++)
1179 * If no etc/catman.conf was found, recursively enter child
1180 * directory and continue scanning.
1184 while (NULL != (d = readdir(dir))) {
1185 if (DT_DIR != d->d_type || '.' == d->d_name[0])
1188 path[(int)sz] = '\0';
1189 ssz = strlcat(path, d->d_name, PATH_MAX);
1191 if (ssz >= PATH_MAX) {
1192 fprintf(stderr, "%s: Path too long", path);
1194 } else if (NULL == (cd = opendir(path))) {
1199 pathgen(cd, path, req);