1 /* $Id: cgi.c,v 1.46 2013/10/11 00:06:48 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.
40 #include <sys/types.h>
44 #include "apropos_db.h"
52 #if defined(__linux__) || defined(__sun)
71 * A query as passed to the search function.
74 const char *arch; /* architecture */
75 const char *sec; /* manual section */
76 const char *expr; /* unparsed expression string */
77 int manroot; /* manroot index (or -1)*/
78 int legacy; /* whether legacy mode */
88 static int atou(const char *, unsigned *);
89 static void catman(const struct req *, const char *);
90 static int cmp(const void *, const void *);
91 static void format(const struct req *, const char *);
92 static void html_print(const char *);
93 static void html_printquery(const struct req *);
94 static void html_putchar(char);
95 static int http_decode(char *);
96 static void http_parse(struct req *, char *);
97 static void http_print(const char *);
98 static void http_putchar(char);
99 static void http_printquery(const struct req *);
100 static int pathstop(DIR *);
101 static void pathgen(DIR *, char *, struct req *);
102 static void pg_index(const struct req *, char *);
103 static void pg_search(const struct req *, char *);
104 static void pg_show(const struct req *, char *);
105 static void resp_bad(void);
106 static void resp_baddb(void);
107 static void resp_error400(void);
108 static void resp_error404(const char *);
109 static void resp_begin_html(int, const char *);
110 static void resp_begin_http(int, const char *);
111 static void resp_end_html(void);
112 static void resp_index(const struct req *);
113 static void resp_search(struct res *, size_t, void *);
114 static void resp_searchform(const struct req *);
116 static const char *progname; /* cgi script name */
117 static const char *cache; /* cache directory */
118 static const char *css; /* css directory */
119 static const char *host; /* hostname */
121 static const char * const pages[PAGE__MAX] = {
122 "index", /* PAGE_INDEX */
123 "search", /* PAGE_SEARCH */
124 "show", /* PAGE_SHOW */
128 * This is just OpenBSD's strtol(3) suggestion.
129 * I use it instead of strtonum(3) for portability's sake.
132 atou(const char *buf, unsigned *v)
138 lval = strtol(buf, &ep, 10);
139 if (buf[0] == '\0' || *ep != '\0')
141 if ((errno == ERANGE && (lval == LONG_MAX ||
142 lval == LONG_MIN)) ||
143 (lval > INT_MAX || lval < 0))
146 *v = (unsigned int)lval;
151 * Print a character, escaping HTML along the way.
152 * This will pass non-ASCII straight to output: be warned!
172 putchar((unsigned char)c);
177 http_printquery(const struct req *req)
181 http_print(req->q.expr ? req->q.expr : "");
183 http_print(req->q.sec ? req->q.sec : "");
185 http_print(req->q.arch ? req->q.arch : "");
190 html_printquery(const struct req *req)
193 printf("&expr=");
194 html_print(req->q.expr ? req->q.expr : "");
196 html_print(req->q.sec ? req->q.sec : "");
197 printf("&arch=");
198 html_print(req->q.arch ? req->q.arch : "");
202 http_print(const char *p)
212 * Call through to html_putchar().
213 * Accepts NULL strings.
216 html_print(const char *p)
226 * Parse out key-value pairs from an HTTP request variable.
227 * This can be either a cookie or a POST/GET string, although man.cgi
228 * uses only GET for simplicity.
231 http_parse(struct req *req, char *p)
233 char *key, *val, *manroot;
236 memset(&req->q, 0, sizeof(struct query));
245 p += (int)strcspn(p, ";&");
248 if (NULL != (val = strchr(key, '=')))
251 if ('\0' == *key || NULL == val || '\0' == *val)
254 /* Just abort handling. */
256 if ( ! http_decode(key))
258 if (NULL != val && ! http_decode(val))
261 if (0 == strcmp(key, "expr"))
263 else if (0 == strcmp(key, "query"))
265 else if (0 == strcmp(key, "sec"))
267 else if (0 == strcmp(key, "sektion"))
269 else if (0 == strcmp(key, "arch"))
271 else if (0 == strcmp(key, "manpath"))
273 else if (0 == strcmp(key, "apropos"))
274 legacy = 0 == strcmp(val, "0");
277 /* Test for old man.cgi compatibility mode. */
279 req->q.legacy = legacy > 0;
282 * Section "0" means no section when in legacy mode.
283 * For some man.cgi scripts, "default" arch is none.
286 if (req->q.legacy && NULL != req->q.sec)
287 if (0 == strcmp(req->q.sec, "0"))
289 if (req->q.legacy && NULL != req->q.arch)
290 if (0 == strcmp(req->q.arch, "default"))
293 /* Default to first manroot. */
295 if (NULL != manroot) {
296 for (i = 0; i < (int)req->psz; i++)
297 if (0 == strcmp(req->p[i].name, manroot))
299 req->q.manroot = i < (int)req->psz ? i : -1;
307 if (isalnum((unsigned char)c)) {
308 putchar((unsigned char)c);
310 } else if (' ' == c) {
318 * HTTP-decode a string. The standard explanation is that this turns
319 * "%4e+foo" into "n foo" in the regular way. This is done in-place
320 * over the allocated string.
330 for ( ; '\0' != *p; p++) {
332 if ('\0' == (hex[0] = *(p + 1)))
334 if ('\0' == (hex[1] = *(p + 2)))
336 if (1 != sscanf(hex, "%x", &c))
342 memmove(p + 1, p + 3, strlen(p + 3) + 1);
344 *p = '+' == *p ? ' ' : *p;
352 resp_begin_http(int code, const char *msg)
356 printf("Status: %d %s\n", code, msg);
358 puts("Content-Type: text/html; charset=utf-8\n"
359 "Cache-Control: no-cache\n"
367 resp_begin_html(int code, const char *msg)
370 resp_begin_http(code, msg);
372 printf("<!DOCTYPE HTML PUBLIC "
373 " \"-//W3C//DTD HTML 4.01//EN\""
374 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
377 "<META HTTP-EQUIV=\"Content-Type\""
378 " CONTENT=\"text/html; charset=utf-8\">\n"
379 "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
380 " TYPE=\"text/css\" media=\"all\">\n"
381 "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
382 " TYPE=\"text/css\" media=\"all\">\n"
383 "<TITLE>System Manpage Reference</TITLE>\n"
386 "<!-- Begin page content. //-->\n", css, css);
398 resp_searchform(const struct req *req)
402 puts("<!-- Begin search form. //-->");
403 printf("<DIV ID=\"mancgi\">\n"
404 "<FORM ACTION=\"%s/search.html\" METHOD=\"get\">\n"
406 "<LEGEND>Search Parameters</LEGEND>\n"
407 "<INPUT TYPE=\"submit\" "
408 " VALUE=\"Search\"> for manuals satisfying \n"
409 "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
411 html_print(req->q.expr ? req->q.expr : "");
412 printf("\">, section "
413 "<INPUT TYPE=\"text\""
414 " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
415 html_print(req->q.sec ? req->q.sec : "");
417 "<INPUT TYPE=\"text\""
418 " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
419 html_print(req->q.arch ? req->q.arch : "");
422 puts(", <SELECT NAME=\"manpath\">");
423 for (i = 0; i < (int)req->psz; i++) {
424 printf("<OPTION %s VALUE=\"",
425 (i == req->q.manroot) ||
426 (0 == i && -1 == req->q.manroot) ?
427 "SELECTED=\"selected\"" : "");
428 html_print(req->p[i].name);
430 html_print(req->p[i].name);
436 "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
440 puts("<!-- End search form. //-->");
444 resp_index(const struct req *req)
447 resp_begin_html(200, NULL);
448 resp_searchform(req);
456 resp_begin_html(400, "Query Malformed");
457 printf("<H1>Malformed Query</H1>\n"
459 "The query your entered was malformed.\n"
460 "Try again from the\n"
461 "<A HREF=\"%s/index.html\">main page</A>.\n"
467 resp_error404(const char *page)
470 resp_begin_html(404, "Not Found");
471 puts("<H1>Page Not Found</H1>\n"
473 "The page you're looking for, ");
477 "could not be found.\n"
478 "Try searching from the\n"
479 "<A HREF=\"%s/index.html\">main page</A>.\n"
487 resp_begin_html(500, "Internal Server Error");
488 puts("<P>Generic badness happened.</P>");
496 resp_begin_html(500, "Internal Server Error");
497 puts("<P>Your database is broken.</P>");
502 resp_search(struct res *r, size_t sz, void *arg)
505 const struct req *req;
507 req = (const struct req *)arg;
510 assert(req->q.manroot >= 0);
512 for (matched = i = 0; i < sz; i++)
517 for (i = 0; i < sz; i++)
521 * If we have just one result, then jump there now
524 puts("Status: 303 See Other");
525 printf("Location: http://%s%s/show/%d/%u/%u.html?",
526 host, progname, req->q.manroot,
527 r[i].volume, r[i].rec);
528 http_printquery(req);
530 "Content-Type: text/html; charset=utf-8\n");
534 resp_begin_html(200, NULL);
535 resp_searchform(req);
537 puts("<DIV CLASS=\"results\">");
541 "No results found.\n"
548 qsort(r, sz, sizeof(struct res), cmp);
552 for (i = 0; i < sz; i++) {
556 "<TD CLASS=\"title\">\n"
557 "<A HREF=\"%s/show/%d/%u/%u.html?",
558 progname, req->q.manroot,
559 r[i].volume, r[i].rec);
560 html_printquery(req);
562 html_print(r[i].title);
564 html_print(r[i].cat);
565 if (r[i].arch && '\0' != *r[i].arch) {
567 html_print(r[i].arch);
571 "<TD CLASS=\"desc\">");
572 html_print(r[i].desc);
584 pg_index(const struct req *req, char *path)
591 catman(const struct req *req, const char *file)
599 if (NULL == (f = fopen(file, "r"))) {
604 resp_begin_html(200, NULL);
605 resp_searchform(req);
606 puts("<DIV CLASS=\"catman\">\n"
609 while (NULL != (p = fgetln(f, &len))) {
611 for (i = 0; i < (int)len - 1; i++) {
613 * This means that the catpage is out of state.
614 * Ignore it and keep going (although the
618 if ('\b' == p[i] || '\n' == p[i])
622 * Print a regular character.
623 * Close out any bold/italic scopes.
624 * If we're in back-space mode, make sure we'll
625 * have something to enter when we backspace.
628 if ('\b' != p[i + 1]) {
636 } else if (i + 2 >= (int)len)
654 * Handle funny behaviour troff-isms.
655 * These grok'd from the original man2html.c.
658 if (('+' == p[i] && 'o' == p[i + 2]) ||
659 ('o' == p[i] && '+' == p[i + 2]) ||
660 ('|' == p[i] && '=' == p[i + 2]) ||
661 ('=' == p[i] && '|' == p[i + 2]) ||
662 ('*' == p[i] && '=' == p[i + 2]) ||
663 ('=' == p[i] && '*' == p[i + 2]) ||
664 ('*' == p[i] && '|' == p[i + 2]) ||
665 ('|' == p[i] && '*' == p[i + 2])) {
674 } else if (('|' == p[i] && '-' == p[i + 2]) ||
675 ('-' == p[i] && '|' == p[i + 1]) ||
676 ('+' == p[i] && '-' == p[i + 1]) ||
677 ('-' == p[i] && '+' == p[i + 1]) ||
678 ('+' == p[i] && '|' == p[i + 1]) ||
679 ('|' == p[i] && '+' == p[i + 1])) {
703 * Clean up the last character.
704 * We can get to a newline; don't print that.
712 if (i == (int)len - 1 && '\n' != p[i])
727 format(const struct req *req, const char *file)
735 char opts[PATH_MAX + 128];
737 if (-1 == (fd = open(file, O_RDONLY, 0))) {
742 mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL, NULL);
743 rc = mparse_readfd(mp, fd, file);
746 if (rc >= MANDOCLEVEL_FATAL) {
751 snprintf(opts, sizeof(opts), "fragment,"
752 "man=%s/search.html?sec=%%S&expr=Nm~^%%N$,"
753 /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
756 mparse_result(mp, &mdoc, &man);
757 if (NULL == man && NULL == mdoc) {
763 resp_begin_html(200, NULL);
764 resp_searchform(req);
766 vp = html_alloc(opts);
781 pg_show(const struct req *req, char *path)
789 unsigned int vol, rec, mr;
795 /* Parse out mroot, volume, and record from the path. */
797 if (NULL == path || NULL == (sub = strchr(path, '/'))) {
802 if ( ! atou(path, &mr)) {
807 if (NULL == (sub = strchr(path, '/'))) {
812 if ( ! atou(path, &vol) || ! atou(sub, &rec)) {
815 } else if (mr >= (unsigned int)req->psz) {
821 * Begin by chdir()ing into the manroot.
822 * This way we can pick up the database files, which are
823 * relative to the manpath root.
826 if (-1 == chdir(req->p[(int)mr].path)) {
827 perror(req->p[(int)mr].path);
832 memset(&ps, 0, sizeof(struct manpaths));
833 manpath_manconf(&ps, "etc/catman.conf");
835 if (vol >= (unsigned int)ps.sz) {
840 sz = strlcpy(file, ps.paths[vol], PATH_MAX);
841 assert(sz < PATH_MAX);
842 strlcat(file, "/", PATH_MAX);
843 strlcat(file, MANDOC_IDX, PATH_MAX);
845 /* Open the index recno(3) database. */
847 idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
857 if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
858 rc < 0 ? resp_baddb() : resp_error400();
860 } else if (0 == val.size) {
865 cp = (char *)val.data;
868 if (NULL == memchr(cp, '\0', val.size - 1))
871 file[(int)sz] = '\0';
872 strlcat(file, "/", PATH_MAX);
873 strlcat(file, cp, PATH_MAX);
886 pg_search(const struct req *req, char *path)
891 const char *ep, *start;
897 if (req->q.manroot < 0 || 0 == req->psz) {
898 resp_search(NULL, 0, (void *)req);
902 memset(&opt, 0, sizeof(struct opts));
905 opt.arch = req->q.arch;
906 opt.cat = req->q.sec;
914 * Begin by chdir()ing into the root of the manpath.
915 * This way we can pick up the database files, which are
916 * relative to the manpath root.
919 assert(req->q.manroot < (int)req->psz);
920 if (-1 == (chdir(req->p[req->q.manroot].path))) {
921 perror(req->p[req->q.manroot].path);
922 resp_search(NULL, 0, (void *)req);
926 memset(&ps, 0, sizeof(struct manpaths));
927 manpath_manconf(&ps, "etc/catman.conf");
930 * Poor man's tokenisation: just break apart by spaces.
931 * Yes, this is half-ass. But it works for now.
934 while (ep && isspace((unsigned char)*ep))
937 while (ep && '\0' != *ep) {
938 cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *));
940 while ('\0' != *ep && ! isspace((unsigned char)*ep))
942 cp[sz] = mandoc_malloc((ep - start) + 1);
943 memcpy(cp[sz], start, ep - start);
944 cp[sz++][ep - start] = '\0';
945 while (isspace((unsigned char)*ep))
950 * Pump down into apropos backend.
951 * The resp_search() function is called with the results.
954 expr = req->q.legacy ?
955 termcomp(sz, cp, &tt) : exprcomp(sz, cp, &tt);
959 (ps.sz, ps.paths, &opt, expr, tt,
960 (void *)req, &ressz, &res, resp_search);
962 /* ...unless errors occured. */
967 resp_search(NULL, 0, NULL);
969 for (i = 0; i < sz; i++)
985 char *p, *path, *subpath;
987 /* Scan our run-time environment. */
989 if (NULL == (cache = getenv("CACHE_DIR")))
990 cache = "/cache/man.cgi";
992 if (NULL == (progname = getenv("SCRIPT_NAME")))
995 if (NULL == (css = getenv("CSS_DIR")))
998 if (NULL == (host = getenv("HTTP_HOST")))
1002 * First we change directory into the cache directory so that
1003 * subsequent scanning for manpath directories is rooted
1004 * relative to the same position.
1007 if (-1 == chdir(cache)) {
1010 return(EXIT_FAILURE);
1011 } else if (NULL == (cwd = opendir(cache))) {
1014 return(EXIT_FAILURE);
1017 memset(&req, 0, sizeof(struct req));
1019 strlcpy(buf, ".", PATH_MAX);
1020 pathgen(cwd, buf, &req);
1023 /* Next parse out the query string. */
1025 if (NULL != (p = getenv("QUERY_STRING")))
1026 http_parse(&req, p);
1029 * Now juggle paths to extract information.
1030 * We want to extract our filetype (the file suffix), the
1031 * initial path component, then the trailing component(s).
1032 * Start with leading subpath component.
1035 subpath = path = NULL;
1036 req.page = PAGE__MAX;
1038 if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
1039 req.page = PAGE_INDEX;
1041 if (NULL != path && '/' == *path && '\0' == *++path)
1042 req.page = PAGE_INDEX;
1044 /* Strip file suffix. */
1046 if (NULL != path && NULL != (p = strrchr(path, '.')))
1047 if (NULL != p && NULL == strchr(p, '/'))
1050 /* Resolve subpath component. */
1052 if (NULL != path && NULL != (subpath = strchr(path, '/')))
1055 /* Map path into one we recognise. */
1057 if (NULL != path && '\0' != *path)
1058 for (i = 0; i < (int)PAGE__MAX; i++)
1059 if (0 == strcmp(pages[i], path)) {
1060 req.page = (enum page)i;
1068 pg_index(&req, subpath);
1071 pg_search(&req, subpath);
1074 pg_show(&req, subpath);
1077 resp_error404(path);
1081 for (i = 0; i < (int)req.psz; i++) {
1082 free(req.p[i].path);
1083 free(req.p[i].name);
1087 return(EXIT_SUCCESS);
1091 cmp(const void *p1, const void *p2)
1094 return(strcasecmp(((const struct res *)p1)->title,
1095 ((const struct res *)p2)->title));
1099 * Check to see if an "etc" path consists of a catman.conf file. If it
1100 * does, that means that the path contains a tree created by catman(8)
1101 * and should be used for indexing.
1111 while (NULL != (d = readdir(dir))) {
1113 stat(d->d_name, &sb);
1114 if (S_IFREG & sb.st_mode)
1116 if (DT_REG == d->d_type)
1118 if (0 == strcmp(d->d_name, "catman.conf"))
1126 * Scan for indexable paths.
1127 * This adds all paths with "etc/catman.conf" to the buffer.
1130 pathgen(DIR *dir, char *path, struct req *req)
1141 sz = strlcat(path, "/", PATH_MAX);
1142 if (sz >= PATH_MAX) {
1143 fprintf(stderr, "%s: Path too long", path);
1148 * First, scan for the "etc" directory.
1149 * If it's found, then see if it should cause us to stop. This
1150 * happens when a catman.conf is found in the directory.
1154 while (0 == rc && NULL != (d = readdir(dir))) {
1156 stat(d->d_name, &sb);
1157 if (!(S_IFDIR & sb.st_mode)
1159 if (DT_DIR != d->d_type
1161 || strcmp(d->d_name, "etc"))
1164 path[(int)sz] = '\0';
1165 ssz = strlcat(path, d->d_name, PATH_MAX);
1167 if (ssz >= PATH_MAX) {
1168 fprintf(stderr, "%s: Path too long", path);
1170 } else if (NULL == (cd = opendir(path))) {
1180 /* This also strips the trailing slash. */
1181 path[(int)--sz] = '\0';
1182 req->p = mandoc_realloc
1184 (req->psz + 1) * sizeof(struct paths));
1186 * Strip out the leading "./" unless we're just a ".",
1187 * in which case use an empty string as our name.
1189 req->p[(int)req->psz].path = mandoc_strdup(path);
1190 req->p[(int)req->psz].name =
1191 cp = mandoc_strdup(path + (1 == sz ? 1 : 2));
1194 * The name is just the path with all the slashes taken
1195 * out of it. Simple but effective.
1197 for ( ; '\0' != *cp; cp++)
1204 * If no etc/catman.conf was found, recursively enter child
1205 * directory and continue scanning.
1209 while (NULL != (d = readdir(dir))) {
1211 stat(d->d_name, &sb);
1212 if (!(S_IFDIR & sb.st_mode)
1214 if (DT_DIR != d->d_type
1216 || '.' == d->d_name[0])
1219 path[(int)sz] = '\0';
1220 ssz = strlcat(path, d->d_name, PATH_MAX);
1222 if (ssz >= PATH_MAX) {
1223 fprintf(stderr, "%s: Path too long", path);
1225 } else if (NULL == (cd = opendir(path))) {
1230 pathgen(cd, path, req);