1 .\" $Id: man.cgi.3,v 1.2 2016/07/07 19:19:01 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: July 7 2016 $
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 It changes into the manpath and calls
131 Depending on the result, it calls either
135 .It Ft void Fn pg_noresult "const struct req *req" "const char *msg"
137 .Fn resp_begin_html ,
138 .Fn resp_searchform ,
141 passed to it, and calls
143 .It Ft void Fn pg_searchres "const struct req *req" "struct manpage *r"\
145 This function first validates the filenames found.
148 was used and there is exactly one result,
149 it writes an HTTP redirect to that result.
150 Otherwise, it writes an HTML result page beginning with
153 .Fn resp_searchform .
154 If there is more than one result, it writes a list of links
160 query or if there is only one single result, it calls
164 .It Ft void Fn pg_index "const struct req *req"
165 This page generator is used when
173 .Fn resp_searchform ,
174 writes links to help pages, and calls
176 .It Ft void Fn pg_error_badrequest "const char *msg"
177 This page generator is used when
181 detect an invalid URI.
183 .Fn resp_begin_html ,
188 .It Ft void Fn pg_error_internal void
189 This page generator is used by various functions when errors are
192 configuration file, in
196 parser, in file system permissions, or when setting up timeouts.
198 .Fn resp_begin_html ,
200 .Qq "Internal Server Error" ,
204 .Fn pg_error_internal ,
209 to log the reason of the error to the
213 .Ss Result generators
214 The purpose of result generators is to print a chunk of HTML code.
215 When they print untrusted strings or characters,
220 The highest level result generators are:
222 .It Ft void Fn resp_begin_html "int code" "const char *msg"
225 to print the HTTP headers, then prints the HTML header up to the
226 opening tag of the <body> element, then copies the file
228 to the output, if it exists and is readable.
229 .It Ft void Fn resp_searchform "const struct req *req" "enum focus focus"
230 This generator prints a search form, filling it with data
231 from the provided request object.
236 it sets the document's autofocus to the query input box.
237 .It Ft void Fn resp_show "const struct req *req" "const char *file"
238 This wrapper dispatches to either
249 .It Ft void Fn resp_catman "const struct req *req" "const char *file"
250 This generator translates a preformatted, backspace-encoded manual
251 page to HTML and prints it to the output.
252 .It Ft void Fn resp_format "const struct req *req" "const char *file"
253 This generator formats a manual page on the standard output,
254 using the functions documented in
258 .It Ft void Fn resp_end_html void
259 This generator copies the file
261 to the output, if it exists and is readable,
262 and closes the <body> and <html> elements.
265 These functions take a string and return 1 if it is valid, or 0 otherwise.
267 .It Ft int Fn validate_urifrag "const char *frag"
268 Checks that the string only contains alphanumeric ASCII characters,
269 dashes, dots, slashes, and underscores.
270 .It Ft int Fn validate_manpath "const struct req *req" "const char* manpath"
271 Checks that the string is either
273 or one of the manpaths configured in
275 .It Ft int Fn validate_filename "const char *file"
276 Checks that the string starts with
280 and does not ascend to parent directories.