]>
git.cameronkatri.com Git - mandoc.git/blob - cgi.c
1 /* $Id: cgi.c,v 1.78 2014/07/21 15:45:17 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 Ingo Schwarze <schwarze@usta.de>
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.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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.
32 #include "mandoc_aux.h"
35 #include "mansearch.h"
39 * A query as passed to the search function.
42 const char *manpath
; /* desired manual directory */
43 const char *arch
; /* architecture */
44 const char *sec
; /* manual section */
45 const char *expr
; /* unparsed expression string */
46 int equal
; /* match whole names, not substrings */
51 char **p
; /* array of available manpaths */
52 size_t psz
; /* number of available manpaths */
55 static void catman(const struct req
*, const char *);
56 static int cmp(const void *, const void *);
57 static void format(const struct req
*, const char *);
58 static void html_print(const char *);
59 static void html_printquery(const struct req
*);
60 static void html_putchar(char);
61 static int http_decode(char *);
62 static void http_parse(struct req
*, char *);
63 static void http_print(const char *);
64 static void http_putchar(char);
65 static void http_printquery(const struct req
*);
66 static void pathgen(struct req
*);
67 static void pg_error_badrequest(const char *);
68 static void pg_error_internal(void);
69 static void pg_index(const struct req
*);
70 static void pg_noresult(const struct req
*, const char *);
71 static void pg_search(const struct req
*);
72 static void pg_searchres(const struct req
*,
73 struct manpage
*, size_t);
74 static void pg_show(const struct req
*, const char *);
75 static void resp_begin_html(int, const char *);
76 static void resp_begin_http(int, const char *);
77 static void resp_end_html(void);
78 static void resp_searchform(const struct req
*);
79 static void resp_show(const struct req
*, const char *);
81 static const char *scriptname
; /* CGI script name */
83 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
84 static const char *const sec_numbers
[] = {
85 "0", "1", "2", "3", "3p", "4", "5", "6", "7", "8", "9"
87 static const char *const sec_names
[] = {
89 "1 - General Commands",
92 "3p - Perl Subroutines",
96 "7 - Macros and Conventions",
97 "8 - Maintenance Commands",
98 "9 - Kernel Interface"
100 static const int sec_MAX
= sizeof(sec_names
) / sizeof(char *);
102 static const char *const arch_names
[] = {
103 "amd64", "alpha", "armish", "armv7",
104 "aviion", "hppa", "hppa64", "i386",
105 "ia64", "landisk", "loongson", "luna88k",
106 "macppc", "mips64", "octeon", "sgi",
107 "socppc", "solbourne", "sparc", "sparc64",
109 "amiga", "arc", "arm32", "atari",
110 "beagle", "cats", "hp300", "mac68k",
111 "mvme68k", "mvme88k", "mvmeppc", "palm",
112 "pc532", "pegasos", "pmax", "powerpc",
113 "sun3", "wgrisc", "x68k"
115 static const int arch_MAX
= sizeof(arch_names
) / sizeof(char *);
118 * Print a character, escaping HTML along the way.
119 * This will pass non-ASCII straight to output: be warned!
139 putchar((unsigned char)c
);
145 http_printquery(const struct req
*req
)
148 if (NULL
!= req
->q
.manpath
) {
150 http_print(req
->q
.manpath
);
152 if (NULL
!= req
->q
.sec
) {
154 http_print(req
->q
.sec
);
156 if (NULL
!= req
->q
.arch
) {
158 http_print(req
->q
.arch
);
160 if (NULL
!= req
->q
.expr
) {
162 http_print(req
->q
.expr
);
164 if (0 == req
->q
.equal
)
165 printf("&apropos=1");
169 html_printquery(const struct req
*req
)
172 if (NULL
!= req
->q
.manpath
) {
173 printf("&manpath=");
174 html_print(req
->q
.manpath
);
176 if (NULL
!= req
->q
.sec
) {
178 html_print(req
->q
.sec
);
180 if (NULL
!= req
->q
.arch
) {
181 printf("&arch=");
182 html_print(req
->q
.arch
);
184 if (NULL
!= req
->q
.expr
) {
185 printf("&query=");
186 html_print(req
->q
.expr
);
188 if (0 == req
->q
.equal
)
189 printf("&apropos=1");
193 http_print(const char *p
)
203 * Call through to html_putchar().
204 * Accepts NULL strings.
207 html_print(const char *p
)
217 * Parse out key-value pairs from an HTTP request variable.
218 * This can be either a cookie or a POST/GET string, although man.cgi
219 * uses only GET for simplicity.
222 http_parse(struct req
*req
, char *p
)
226 memset(&req
->q
, 0, sizeof(struct query
));
227 req
->q
.manpath
= req
->p
[0];
234 p
+= (int)strcspn(p
, ";&");
237 if (NULL
!= (val
= strchr(key
, '=')))
240 if ('\0' == *key
|| NULL
== val
|| '\0' == *val
)
243 /* Just abort handling. */
245 if ( ! http_decode(key
))
247 if (NULL
!= val
&& ! http_decode(val
))
250 if (0 == strcmp(key
, "query"))
252 else if (0 == strcmp(key
, "manpath")) {
254 if (0 == strncmp(val
, "OpenBSD ", 8)) {
260 req
->q
.manpath
= val
;
261 } else if (0 == strcmp(key
, "apropos"))
262 req
->q
.equal
= !strcmp(val
, "0");
263 else if (0 == strcmp(key
, "sec")) {
264 if (strcmp(val
, "0"))
267 } else if (0 == strcmp(key
, "sektion")) {
268 if (strcmp(val
, "0"))
271 } else if (0 == strcmp(key
, "arch")) {
272 if (strcmp(val
, "default"))
282 if (isalnum((unsigned char)c
)) {
283 putchar((unsigned char)c
);
285 } else if (' ' == c
) {
293 * HTTP-decode a string. The standard explanation is that this turns
294 * "%4e+foo" into "n foo" in the regular way. This is done in-place
295 * over the allocated string.
307 for ( ; '\0' != *p
; p
++, q
++) {
309 if ('\0' == (hex
[0] = *(p
+ 1)))
311 if ('\0' == (hex
[1] = *(p
+ 2)))
313 if (1 != sscanf(hex
, "%x", &c
))
321 *q
= '+' == *p
? ' ' : *p
;
329 resp_begin_http(int code
, const char *msg
)
333 printf("Status: %d %s\r\n", code
, msg
);
335 printf("Content-Type: text/html; charset=utf-8\r\n"
336 "Cache-Control: no-cache\r\n"
337 "Pragma: no-cache\r\n"
344 resp_begin_html(int code
, const char *msg
)
347 resp_begin_http(code
, msg
);
349 printf("<!DOCTYPE HTML PUBLIC "
350 " \"-//W3C//DTD HTML 4.01//EN\""
351 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
354 "<META HTTP-EQUIV=\"Content-Type\""
355 " CONTENT=\"text/html; charset=utf-8\">\n"
356 "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
357 " TYPE=\"text/css\" media=\"all\">\n"
358 "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
359 " TYPE=\"text/css\" media=\"all\">\n"
360 "<TITLE>%s</TITLE>\n"
363 "<!-- Begin page content. //-->\n",
364 CSS_DIR
, CSS_DIR
, CUSTOMIZE_TITLE
);
376 resp_searchform(const struct req
*req
)
380 puts(CUSTOMIZE_BEGIN
);
381 puts("<!-- Begin search form. //-->");
382 printf("<DIV ID=\"mancgi\">\n"
383 "<FORM ACTION=\"%s\" METHOD=\"get\">\n"
385 "<LEGEND>Manual Page Search Parameters</LEGEND>\n",
388 /* Write query input box. */
390 printf( "<TABLE><TR><TD>\n"
391 "<INPUT TYPE=\"text\" NAME=\"query\" VALUE=\"");
392 if (NULL
!= req
->q
.expr
)
393 html_print(req
->q
.expr
);
394 puts("\" SIZE=\"40\">");
396 /* Write submission and reset buttons. */
398 printf( "<INPUT TYPE=\"submit\" VALUE=\"Submit\">\n"
399 "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n");
401 /* Write show radio button */
403 printf( "</TD><TD>\n"
404 "<INPUT TYPE=\"radio\" ");
407 printf( "NAME=\"apropos\" ID=\"show\" VALUE=\"0\">\n"
408 "<LABEL FOR=\"show\">Show named manual page</LABEL>\n");
410 /* Write section selector. */
412 printf( "</TD></TR><TR><TD>\n"
413 "<SELECT NAME=\"sec\">");
414 for (i
= 0; i
< sec_MAX
; i
++) {
415 printf("<OPTION VALUE=\"%s\"", sec_numbers
[i
]);
416 if (NULL
!= req
->q
.sec
&&
417 0 == strcmp(sec_numbers
[i
], req
->q
.sec
))
419 printf(">%s</OPTION>\n", sec_names
[i
]);
423 /* Write architecture selector. */
425 puts("<SELECT NAME=\"arch\">");
426 for (i
= 0; i
< arch_MAX
; i
++) {
427 printf("<OPTION VALUE=\"%s\"", arch_names
[i
]);
428 if (NULL
!= req
->q
.arch
&&
429 0 == strcmp(arch_names
[i
], req
->q
.arch
))
431 printf(">%s</OPTION>\n", arch_names
[i
]);
435 /* Write manpath selector. */
438 puts("<SELECT NAME=\"manpath\">");
439 for (i
= 0; i
< (int)req
->psz
; i
++) {
441 if (NULL
== req
->q
.manpath
? 0 == i
:
442 0 == strcmp(req
->q
.manpath
, req
->p
[i
]))
445 html_print(req
->p
[i
]);
447 html_print(req
->p
[i
]);
453 /* Write search radio button */
455 printf( "</TD><TD>\n"
456 "<INPUT TYPE=\"radio\" ");
457 if (0 == req
->q
.equal
)
459 printf( "NAME=\"apropos\" ID=\"search\" VALUE=\"1\">\n"
460 "<LABEL FOR=\"search\">Search with apropos query</LABEL>\n");
462 puts("</TD></TR></TABLE>\n"
466 puts("<!-- End search form. //-->");
470 validate_manpath(const struct req
*req
, const char* manpath
)
474 if ( ! strcmp(manpath
, "mandoc"))
477 for (i
= 0; i
< req
->psz
; i
++)
478 if ( ! strcmp(manpath
, req
->p
[i
]))
485 validate_filename(const char *file
)
488 if ('.' == file
[0] && '/' == file
[1])
491 return ( ! (strstr(file
, "../") || strstr(file
, "/..") ||
492 (strncmp(file
, "man", 3) && strncmp(file
, "cat", 3))));
496 pg_index(const struct req
*req
)
499 resp_begin_html(200, NULL
);
500 resp_searchform(req
);
502 "This web interface is documented in the "
503 "<A HREF=\"%s/mandoc/man8/man.cgi.8\">man.cgi</A> "
505 "<A HREF=\"%s/mandoc/man1/apropos.1\">apropos</A> "
506 "manual explains the query syntax.\n"
508 scriptname
, scriptname
);
513 pg_noresult(const struct req
*req
, const char *msg
)
515 resp_begin_html(200, NULL
);
516 resp_searchform(req
);
524 pg_error_badrequest(const char *msg
)
527 resp_begin_html(400, "Bad Request");
528 puts("<H1>Bad Request</H1>\n"
531 printf("Try again from the\n"
532 "<A HREF=\"%s\">main page</A>.\n"
538 pg_error_internal(void)
540 resp_begin_html(500, "Internal Server Error");
541 puts("<P>Internal Server Error</P>");
546 pg_searchres(const struct req
*req
, struct manpage
*r
, size_t sz
)
548 size_t i
, iuse
, isec
;
552 for (i
= 0; i
< sz
; i
++) {
553 if (validate_filename(r
[i
].file
))
555 fprintf(stderr
, "invalid filename %s in %s database\n",
556 r
[i
].file
, req
->q
.manpath
);
563 * If we have just one result, then jump there now
566 printf("Status: 303 See Other\r\n");
567 printf("Location: http://%s%s/%s/%s?",
568 HTTP_HOST
, scriptname
, req
->q
.manpath
, r
[0].file
);
569 http_printquery(req
);
571 "Content-Type: text/html; charset=utf-8\r\n"
576 qsort(r
, sz
, sizeof(struct manpage
), cmp
);
578 resp_begin_html(200, NULL
);
579 resp_searchform(req
);
580 puts("<DIV CLASS=\"results\">");
583 for (i
= 0; i
< sz
; i
++) {
585 "<TD CLASS=\"title\">\n"
586 "<A HREF=\"%s/%s/%s?",
587 scriptname
, req
->q
.manpath
, r
[i
].file
);
588 html_printquery(req
);
590 html_print(r
[i
].names
);
593 "<TD CLASS=\"desc\">");
594 html_print(r
[i
].output
);
603 * In man(1) mode, show one of the pages
604 * even if more than one is found.
611 for (i
= 0; i
< sz
; i
++) {
612 isec
= strcspn(r
[i
].file
, "123456789");
613 sec
= r
[i
].file
[isec
];
616 prio
= sec_prios
[sec
- '1'];
622 resp_show(req
, r
[iuse
].file
);
629 catman(const struct req
*req
, const char *file
)
637 if (NULL
== (f
= fopen(file
, "r"))) {
638 puts("<P>You specified an invalid manual file.</P>");
642 puts("<DIV CLASS=\"catman\">\n"
645 while (NULL
!= (p
= fgetln(f
, &len
))) {
647 for (i
= 0; i
< (int)len
- 1; i
++) {
649 * This means that the catpage is out of state.
650 * Ignore it and keep going (although the
654 if ('\b' == p
[i
] || '\n' == p
[i
])
658 * Print a regular character.
659 * Close out any bold/italic scopes.
660 * If we're in back-space mode, make sure we'll
661 * have something to enter when we backspace.
664 if ('\b' != p
[i
+ 1]) {
672 } else if (i
+ 2 >= (int)len
)
690 * Handle funny behaviour troff-isms.
691 * These grok'd from the original man2html.c.
694 if (('+' == p
[i
] && 'o' == p
[i
+ 2]) ||
695 ('o' == p
[i
] && '+' == p
[i
+ 2]) ||
696 ('|' == p
[i
] && '=' == p
[i
+ 2]) ||
697 ('=' == p
[i
] && '|' == p
[i
+ 2]) ||
698 ('*' == p
[i
] && '=' == p
[i
+ 2]) ||
699 ('=' == p
[i
] && '*' == p
[i
+ 2]) ||
700 ('*' == p
[i
] && '|' == p
[i
+ 2]) ||
701 ('|' == p
[i
] && '*' == p
[i
+ 2])) {
710 } else if (('|' == p
[i
] && '-' == p
[i
+ 2]) ||
711 ('-' == p
[i
] && '|' == p
[i
+ 1]) ||
712 ('+' == p
[i
] && '-' == p
[i
+ 1]) ||
713 ('-' == p
[i
] && '+' == p
[i
+ 1]) ||
714 ('+' == p
[i
] && '|' == p
[i
+ 1]) ||
715 ('|' == p
[i
] && '+' == p
[i
+ 1])) {
739 * Clean up the last character.
740 * We can get to a newline; don't print that.
748 if (i
== (int)len
- 1 && '\n' != p
[i
])
761 format(const struct req
*req
, const char *file
)
769 char opts
[PATH_MAX
+ 128];
771 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
772 puts("<P>You specified an invalid manual file.</P>");
776 mp
= mparse_alloc(MPARSE_SO
, MANDOCLEVEL_FATAL
, NULL
,
778 rc
= mparse_readfd(mp
, fd
, file
);
781 if (rc
>= MANDOCLEVEL_FATAL
) {
782 fprintf(stderr
, "fatal mandoc error: %s/%s\n",
783 req
->q
.manpath
, file
);
788 snprintf(opts
, sizeof(opts
),
789 "fragment,man=%s?query=%%N&sec=%%S",
792 mparse_result(mp
, &mdoc
, &man
, NULL
);
793 if (NULL
== man
&& NULL
== mdoc
) {
794 fprintf(stderr
, "fatal mandoc error: %s/%s\n",
795 req
->q
.manpath
, file
);
801 vp
= html_alloc(opts
);
813 resp_show(const struct req
*req
, const char *file
)
816 if ('.' == file
[0] && '/' == file
[1])
826 pg_show(const struct req
*req
, const char *path
)
830 if (NULL
== path
|| NULL
== (sub
= strchr(path
, '/'))) {
832 "You did not specify a page to show.");
837 if ( ! validate_manpath(req
, path
)) {
839 "You specified an invalid manpath.");
844 * Begin by chdir()ing into the manpath.
845 * This way we can pick up the database files, which are
846 * relative to the manpath root.
849 if (-1 == chdir(path
)) {
850 fprintf(stderr
, "chdir %s: %s\n",
851 path
, strerror(errno
));
856 if ( ! validate_filename(sub
)) {
858 "You specified an invalid manual file.");
862 resp_begin_html(200, NULL
);
863 resp_searchform(req
);
869 pg_search(const struct req
*req
)
871 struct mansearch search
;
872 struct manpaths paths
;
875 const char *ep
, *start
;
880 * Begin by chdir()ing into the root of the manpath.
881 * This way we can pick up the database files, which are
882 * relative to the manpath root.
885 if (-1 == (chdir(req
->q
.manpath
))) {
886 fprintf(stderr
, "chdir %s: %s\n",
887 req
->q
.manpath
, strerror(errno
));
892 search
.arch
= req
->q
.arch
;
893 search
.sec
= req
->q
.sec
;
894 search
.deftype
= req
->q
.equal
? TYPE_Nm
: (TYPE_Nm
| TYPE_Nd
);
895 search
.flags
= req
->q
.equal
? MANSEARCH_MAN
: 0;
898 paths
.paths
= mandoc_malloc(sizeof(char *));
899 paths
.paths
[0] = mandoc_strdup(".");
902 * Poor man's tokenisation: just break apart by spaces.
903 * Yes, this is half-ass. But it works for now.
907 while (ep
&& isspace((unsigned char)*ep
))
912 while (ep
&& '\0' != *ep
) {
913 cp
= mandoc_reallocarray(cp
, sz
+ 1, sizeof(char *));
915 while ('\0' != *ep
&& ! isspace((unsigned char)*ep
))
917 cp
[sz
] = mandoc_malloc((ep
- start
) + 1);
918 memcpy(cp
[sz
], start
, ep
- start
);
919 cp
[sz
++][ep
- start
] = '\0';
920 while (isspace((unsigned char)*ep
))
924 if (0 == mansearch(&search
, &paths
, sz
, cp
, "Nd", &res
, &ressz
))
925 pg_noresult(req
, "You entered an invalid query.");
927 pg_noresult(req
, "No results found.");
929 pg_searchres(req
, res
, ressz
);
931 for (i
= 0; i
< sz
; i
++)
935 for (i
= 0; i
< (int)ressz
; i
++) {
942 free(paths
.paths
[0]);
954 /* Scan our run-time environment. */
956 if (NULL
== (scriptname
= getenv("SCRIPT_NAME")))
960 * First we change directory into the MAN_DIR so that
961 * subsequent scanning for manpath directories is rooted
962 * relative to the same position.
965 if (-1 == chdir(MAN_DIR
)) {
966 fprintf(stderr
, "MAN_DIR: %s: %s\n",
967 MAN_DIR
, strerror(errno
));
969 return(EXIT_FAILURE
);
972 memset(&req
, 0, sizeof(struct req
));
975 /* Next parse out the query string. */
977 if (NULL
!= (querystring
= getenv("QUERY_STRING")))
978 http_parse(&req
, querystring
);
980 if ( ! validate_manpath(&req
, req
.q
.manpath
)) {
982 "You specified an invalid manpath.");
983 return(EXIT_FAILURE
);
986 /* Dispatch to the three different pages. */
988 path
= getenv("PATH_INFO");
991 else if ('/' == *path
)
996 else if (NULL
!= req
.q
.expr
)
1001 for (i
= 0; i
< (int)req
.psz
; i
++)
1004 return(EXIT_SUCCESS
);
1008 cmp(const void *p1
, const void *p2
)
1011 return(strcasecmp(((const struct manpage
*)p1
)->names
,
1012 ((const struct manpage
*)p2
)->names
));
1016 * Scan for indexable paths.
1019 pathgen(struct req
*req
)
1025 if (NULL
== (fp
= fopen("manpath.conf", "r"))) {
1026 fprintf(stderr
, "%s/manpath.conf: %s\n",
1027 MAN_DIR
, strerror(errno
));
1028 pg_error_internal();
1032 while (NULL
!= (dp
= fgetln(fp
, &dpsz
))) {
1033 if ('\n' == dp
[dpsz
- 1])
1035 req
->p
= mandoc_realloc(req
->p
,
1036 (req
->psz
+ 1) * sizeof(char *));
1037 req
->p
[req
->psz
++] = mandoc_strndup(dp
, dpsz
);
1040 if ( req
->p
== NULL
) {
1041 fprintf(stderr
, "%s/manpath.conf is empty\n", MAN_DIR
);
1042 pg_error_internal();