1 .\" $Id: man.cgi.3,v 1.3 2017/03/15 10:17:29 schwarze Exp $
3 .\" Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
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.
17 .Dd $Mdocdate: March 15 2017 $
22 .Nd internals of the CGI program to search and display manual pages
26 is organized in four levels:
43 program and a few parser routines.
45 .It Ft int Fn main void
49 limits execution time;
53 the data directory containing all the manual trees;
56 .Fn parse_manpath_conf ;
61 .Fn parse_query_string ;
66 validates the manpath and the architecture;
68 calls the appropriate one among the
71 .It Ft void Fn parse_manpath_conf "struct req *req"
78 .It Ft void Fn parse_path_info "struct req *req" "const char *path"
85 .It Ft void Fn parse_query_string "struct req *req" "const char *qs"
92 This function is the only user of the utility functions
98 The purpose of each page generator is to print a complete HTML page,
99 starting with the HTTP headers and continuing to the page footer.
100 Before starting HTML output with
101 .Fn resp_begin_html ,
102 some page generators do some preparatory work, for example to decide
104 Each page generator ends with a call to
107 .It Ft void Fn pg_show "struct req *req" "const char *fullpath"
108 This page generator is used when
110 contains the complete path to a manual page including manpath,
111 section directory, optional architecture subdirectory, manual name
112 and section number suffix.
113 It validates the manpath, changes into it, validate the filename,
115 .Fn resp_begin_html ,
116 .Fn resp_searchform ,
121 .It Ft void Fn pg_search "const struct req *req"
122 This page generator is used when
124 contains a search query in short format or when
129 If possible, requests using
131 are redirected to URIs using
135 Otherwise, it changes into the manpath and calls
137 Depending on the result, it calls either
141 .It Ft void Fn pg_redirect "const struct req *req" "const char *name"
142 This function is special in so far as it does not print an HTML page,
143 but only an HTTP 303 response with a Location: of the form:
147 .Op Ar scriptname Ns /
153 .It Ft void Fn pg_noresult "const struct req *req" "const char *msg"
155 .Fn resp_begin_html ,
156 .Fn resp_searchform ,
159 passed to it, and calls
161 .It Ft void Fn pg_searchres "const struct req *req" "struct manpage *r"\
163 This function first validates the filenames found.
166 was used and there is exactly one result,
167 it writes an HTTP redirect to that result.
168 Otherwise, it writes an HTML result page beginning with
171 .Fn resp_searchform .
172 If there is more than one result, it writes a list of links
178 query or if there is only one single result, it calls
182 .It Ft void Fn pg_index "const struct req *req"
183 This page generator is used when
191 .Fn resp_searchform ,
192 writes links to help pages, and calls
194 .It Ft void Fn pg_error_badrequest "const char *msg"
195 This page generator is used when
199 detect an invalid URI.
201 .Fn resp_begin_html ,
206 .It Ft void Fn pg_error_internal void
207 This page generator is used by various functions when errors are
210 configuration file, in
214 parser, in file system permissions, or when setting up timeouts.
216 .Fn resp_begin_html ,
218 .Qq "Internal Server Error" ,
222 .Fn pg_error_internal ,
227 to log the reason of the error to the
231 .Ss Result generators
232 The purpose of result generators is to print a chunk of HTML code.
233 When they print untrusted strings or characters,
238 The highest level result generators are:
240 .It Ft void Fn resp_begin_html "int code" "const char *msg"
243 to print the HTTP headers, then prints the HTML header up to the
244 opening tag of the <body> element, then copies the file
246 to the output, if it exists and is readable.
247 .It Ft void Fn resp_searchform "const struct req *req" "enum focus focus"
248 This generator prints a search form, filling it with data
249 from the provided request object.
254 it sets the document's autofocus to the query input box.
255 .It Ft void Fn resp_show "const struct req *req" "const char *file"
256 This wrapper dispatches to either
267 .It Ft void Fn resp_catman "const struct req *req" "const char *file"
268 This generator translates a preformatted, backspace-encoded manual
269 page to HTML and prints it to the output.
270 .It Ft void Fn resp_format "const struct req *req" "const char *file"
271 This generator formats a manual page on the standard output,
272 using the functions documented in
276 .It Ft void Fn resp_end_html void
277 This generator copies the file
279 to the output, if it exists and is readable,
280 and closes the <body> and <html> elements.
283 These functions take a string and return 1 if it is valid, or 0 otherwise.
285 .It Ft int Fn validate_urifrag "const char *frag"
286 Checks that the string only contains alphanumeric ASCII characters,
287 dashes, dots, slashes, and underscores.
288 .It Ft int Fn validate_manpath "const struct req *req" "const char* manpath"
289 Checks that the string is either
291 or one of the manpaths configured in
293 .It Ft int Fn validate_filename "const char *file"
294 Checks that the string starts with
298 and does not ascend to parent directories.