1 .\" $Id: man.cgi.3,v 1.1 2016/04/15 21:16:22 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: April 15 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 ;
62 .Fn parse_query_string
67 validates the manpath and the architecture;
69 calls the appropriate one among the
72 .It Ft void Fn parse_manpath_conf "struct req *req"
79 .It Ft void Fn parse_path_info "struct req *req" "const char *path"
86 .It Ft void Fn parse_query_string "struct req *req" "const char *qs"
93 This function is the only user of the utility functions
99 The purpose of each page generator is to print a complete HTML page,
100 starting with the HTTP headers and continuing to the page footer.
101 Before starting HTML output with
102 .Fn resp_begin_html ,
103 some page generators do some preparatory work, for example to decide
105 Each page generator ends with a call to
108 .It Ft void Fn pg_show "struct req *req" "const char *fullpath"
109 This page generator is used when
111 contains the complete path to a manual page including manpath,
112 section directory, optional architecture subdirectory, manual name
113 and section number suffix.
114 It validates the manpath, changes into it, validate the filename,
116 .Fn resp_begin_html ,
117 .Fn resp_searchform ,
122 .It Ft void Fn pg_search "const struct req *req"
123 This page generator is used when
125 contains a search query in short format or when
130 It changes into the manpath and calls
132 Depending on the result, it calls either
136 .It Ft void Fn pg_noresult "const struct req *req" "const char *msg"
138 .Fn resp_begin_html ,
139 .Fn resp_searchform ,
142 passed to it, and calls
144 .It Ft void Fn pg_searchres "const struct req *req" "struct manpage *r"\
146 This function first validates the filenames found.
149 was used and there is exactly one result,
150 it writes an HTTP redirect to that result.
151 Otherwise, it writes an HTML result page beginning with
154 .Fn resp_searchform .
155 If there is more than one result, it writes a list of links
161 query or if there is only one single result, it calls
165 .It Ft void Fn pg_index "const struct req *req"
166 This page generator is used when
174 .Fn resp_searchform ,
175 writes links to help pages, and calls
177 .It Ft void Fn pg_error_badrequest "const char *msg"
178 This page generator is used when
182 detect an invalid URI.
184 .Fn resp_begin_html ,
189 .It Ft void Fn pg_error_internal void
190 This page generator is used by various functions when errors are
193 configuration file, in
197 parser, in file system permissions, or when setting up timeouts.
199 .Fn resp_begin_html ,
201 .Qq "Internal Server Error" ,
205 .Fn pg_error_internal ,
210 to log the reason of the error to the
214 .Ss Result generators
215 The purpose of result generators is to print a chunk of HTML code.
216 When they print untrusted strings or characters,
221 The highest level result generators are:
223 .It Ft void Fn resp_begin_html "int code" "const char *msg"
226 to print the HTTP headers, then prints the HTML header up to the
227 opening tag of the <body> element, then copies the file
229 to the output, if it exists and is readable.
230 .It Ft void Fn resp_searchform "const struct req *req"
231 This generator prints a search form, filling it with data
232 from the provided request object.
233 .It Ft void Fn resp_show "const struct req *req" "const char *file"
234 This wrapper dispatches to either
245 .It Ft void Fn resp_catman "const struct req *req" "const char *file"
246 This generator translates a preformatted, backspace-encoded manual
247 page to HTML and prints it to the output.
248 .It Ft void Fn resp_format "const struct req *req" "const char *file"
249 This generator formats a manual page on the standard output,
250 using the functions documented in
254 .It Ft void Fn resp_end_html void
255 This generator copies the file
257 to the output, if it exists and is readable,
258 and closes the <body> and <html> elements.
261 These functions take a string and return 1 if it is valid, or 0 otherwise.
263 .It Ft int Fn validate_urifrag "const char *frag"
264 Checks that the string only contains alphanumeric ASCII characters,
265 dashes, dots, slashes, and underscores.
266 .It Ft int Fn validate_manpath "const struct req *req" "const char* manpath"
267 Checks that the string is either
269 or one of the manpaths configured in
271 .It Ft int Fn validate_filename "const char *file"
272 Checks that the string starts with
276 and does not ascend to parent directories.