]> granicus.if.org Git - apache/blob - modules/generators/mod_info.c
e69d6cd51264ab7120da85772ecca2b094c94e8b
[apache] / modules / generators / mod_info.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 /* 
60  * Info Module.  Display configuration information for the server and
61  * all included modules.
62  *
63  * <Location /server-info>
64  * SetHandler server-info
65  * </Location>
66  *
67  * GET /server-info - Returns full configuration page for server and all modules
68  * GET /server-info?server - Returns server configuration only
69  * GET /server-info?module_name - Returns configuration for a single module
70  * GET /server-info?list - Returns quick list of included modules
71  *
72  * Rasmus Lerdorf <rasmus@vex.net>, May 1996
73  *
74  * 05.01.96 Initial Version
75  *
76  * Lou Langholtz <ldl@usi.utah.edu>, July 1997
77  *
78  * 07.11.97 Addition of the AddModuleInfo directive
79  *
80  * Ryan Morgan <rmorgan@covalent.net>
81  * 
82  * 8.11.00 Port to Apache 2.0.  Read configuation from the configuration
83  * tree rather than reparse the entire configuation file.
84  * 
85  */
86
87 #include "httpd.h"
88 #include "http_config.h"
89 #include "http_core.h"
90 #include "http_log.h"
91 #include "http_main.h"
92 #include "http_protocol.h"
93 #include "util_script.h"
94 #include "http_conf_globals.h"
95
96 typedef struct {
97     const char *name;                 /* matching module name */
98     const char *info;                 /* additional info */
99 } info_entry;
100
101 typedef struct {
102     apr_array_header_t *more_info;
103 } info_svr_conf;
104
105 module AP_MODULE_DECLARE_DATA info_module;
106
107 extern module *top_module;
108 extern ap_directive_t *ap_conftree;
109
110 static void *create_info_config(apr_pool_t *p, server_rec *s)
111 {
112     info_svr_conf *conf = (info_svr_conf *) apr_pcalloc(p, sizeof(info_svr_conf));
113
114     conf->more_info = apr_make_array(p, 20, sizeof(info_entry));
115     return conf;
116 }
117
118 static void *merge_info_config(apr_pool_t *p, void *basev, void *overridesv)
119 {
120     info_svr_conf *new = (info_svr_conf *) apr_pcalloc(p, sizeof(info_svr_conf));
121     info_svr_conf *base = (info_svr_conf *) basev;
122     info_svr_conf *overrides = (info_svr_conf *) overridesv;
123
124     new->more_info = apr_append_arrays(p, overrides->more_info, base->more_info);
125     return new;
126 }
127
128 static char *mod_info_html_cmd_string(const char *string, char *buf, size_t buf_len, int close)
129 {
130     const char *s;
131     char *t;
132     char *end_buf;
133
134     s = string;
135     t = buf;
136     /* keep space for \0 byte */
137     end_buf = buf + buf_len - 1;
138     while ((*s) && (t < end_buf)) {
139         if (*s == '<') {
140             if (close) {
141                 strncpy(t, "&lt;/,", end_buf -t);
142                 t += 5;
143             } else {
144                 strncpy(t, "&lt;", end_buf - t);
145                 t += 4;
146             }
147         }
148         else if (*s == '>') {
149             strncpy(t, "&gt;", end_buf - t);
150             t += 4;
151         }
152         else if (*s == '&') {
153             strncpy(t, "&amp;", end_buf - t);
154             t += 5;
155         }
156         else if (*s == ' ') {
157             if (close) {
158                 strncpy(t, "&gt;", end_buf -t);
159                 t += 4;
160                 break;
161             } else {
162               *t++ = *s;
163             }
164         } else {
165             *t++ = *s;
166         }
167         s++;
168     }
169     /* oops, overflowed... don't overwrite */
170     if (t > end_buf) {
171         *end_buf = '\0';
172     }
173     else {
174         *t = '\0';
175     }
176     return (buf);
177 }
178
179 static void mod_info_module_cmds(request_rec * r, const command_rec * cmds,
180                                  ap_directive_t * conftree)
181 {
182     const command_rec *cmd;
183     ap_directive_t *tmptree = conftree;
184
185     char buf[MAX_STRING_LEN];
186     char htmlstring[MAX_STRING_LEN];
187     int block_start = 0;
188     int nest = 0;
189
190     while (tmptree != NULL) {
191         cmd = cmds;
192         while (cmd->name) {
193             if (!strcasecmp(cmd->name, tmptree->directive)) {
194                 if (nest > block_start) {
195                     block_start++;
196                     apr_snprintf(htmlstring, sizeof(htmlstring), "%s %s",
197                                 tmptree->parent->directive,
198                                 tmptree->parent->args);
199                     ap_rprintf(r, "<dd><tt>%s</tt><br>\n",
200                                mod_info_html_cmd_string(htmlstring, buf,
201                                                         sizeof(buf), 0));
202                 }
203                 if (nest == 2) {
204                     ap_rprintf(r, "<dd><tt>&nbsp;&nbsp;&nbsp;&nbsp;%s "
205                                "<i>%s</i></tt><br>\n",
206                                tmptree->directive, tmptree->args);
207                 } else if (nest == 1) {
208                     ap_rprintf(r,
209                                "<dd><tt>&nbsp;&nbsp;%s <i>%s</i></tt><br>\n",
210                                tmptree->directive, tmptree->args);
211                 } else {
212                     ap_rprintf(r, "<dd><tt>%s <i>%s</i></tt><br>\n",
213                                mod_info_html_cmd_string(tmptree->directive,
214                                                         buf, sizeof(buf),
215                                                         0), tmptree->args);
216                 }
217             }
218             ++cmd;
219         }
220         if (tmptree->first_child != NULL) {
221             tmptree = tmptree->first_child;
222             nest++;
223         } else if (tmptree->next != NULL) {
224             tmptree = tmptree->next;
225         } else {
226             if (block_start) {
227                 apr_snprintf(htmlstring, sizeof(htmlstring), "%s %s",
228                             tmptree->parent->directive,
229                             tmptree->parent->args);
230                 ap_rprintf(r, "<dd><tt>%s</tt><br>\n",
231                            mod_info_html_cmd_string(htmlstring, buf,
232                                                     sizeof(buf), 1));
233                 block_start--;
234             }
235             if (tmptree->parent) {
236                 tmptree = tmptree->parent->next;
237             }
238             else {
239                 tmptree = NULL;
240             }
241             nest--;
242         }
243
244     }
245 }
246 static const char *find_more_info(server_rec *s, const char *module_name)
247 {
248     int i;
249     info_svr_conf *conf = (info_svr_conf *) ap_get_module_config(s->module_config,
250                                                               &info_module);
251     info_entry *entry = (info_entry *) conf->more_info->elts;
252
253     if (!module_name) {
254         return 0;
255     }
256     for (i = 0; i < conf->more_info->nelts; i++) {
257         if (!strcmp(module_name, entry->name)) {
258             return entry->info;
259         }
260         entry++;
261     }
262     return 0;
263 }
264
265 static int display_info(request_rec *r)
266 {
267     module *modp = NULL;
268     char buf[MAX_STRING_LEN];
269     const char *cfname;
270     const char *more_info;
271     const command_rec *cmd = NULL;
272 #ifdef NEVERMORE
273     const handler_rec *hand = NULL;
274 #endif
275     server_rec *serv = r->server;
276     int comma = 0;
277
278     if (strcmp(r->handler, "server-info"))
279         return DECLINED;
280
281     r->allowed |= (1 << M_GET);
282     if (r->method_number != M_GET)
283         return DECLINED;
284
285     r->content_type = "text/html";
286     ap_send_http_header(r);
287     if (r->header_only) {
288         return 0;
289     }
290
291     ap_rputs(DOCTYPE_HTML_3_2
292              "<html><head><title>Server Information</title></head>\n", r);
293     ap_rputs("<body><h1 align=center>Apache Server Information</h1>\n", r);
294     if (!r->args || strcasecmp(r->args, "list")) {
295         cfname = ap_server_root_relative(r->pool, SERVER_CONFIG_FILE);
296         if (!r->args) {
297             ap_rputs("<tt><a href=\"#server\">Server Settings</a>, ", r);
298             for (modp = top_module; modp; modp = modp->next) {
299                 ap_rprintf(r, "<a href=\"#%s\">%s</a>", modp->name, modp->name);
300                 if (modp->next) {
301                     ap_rputs(", ", r);
302                 }
303             }
304             ap_rputs("</tt><hr>", r);
305
306         }
307         if (!r->args || !strcasecmp(r->args, "server")) {
308             ap_rprintf(r, "<a name=\"server\"><strong>Server Version:</strong> "
309                         "<font size=+1><tt>%s</tt></a></font><br>\n",
310                         ap_get_server_version());
311             ap_rprintf(r, "<strong>Server Built:</strong> "
312                         "<font size=+1><tt>%s</tt></a></font><br>\n",
313                         ap_get_server_built());
314             ap_rprintf(r, "<strong>API Version:</strong> "
315                         "<tt>%d:%d</tt><br>\n",
316                         MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
317             ap_rprintf(r, "<strong>Hostname/port:</strong> "
318                         "<tt>%s:%u</tt><br>\n",
319                         serv->server_hostname, serv->port);
320             ap_rprintf(r, "<strong>Timeouts:</strong> "
321                         "<tt>connection: %d &nbsp;&nbsp; "
322                         "keep-alive: %d</tt><br>",
323                         serv->timeout, serv->keep_alive_timeout);
324             ap_rprintf(r, "<strong>Server Root:</strong> "
325                         "<tt>%s</tt><br>\n", ap_server_root);
326             ap_rprintf(r, "<strong>Config File:</strong> "
327                        "<tt>%s</tt><br>\n", SERVER_CONFIG_FILE);
328         }
329         ap_rputs("<hr><dl>", r);
330         for (modp = top_module; modp; modp = modp->next) {
331             if (!r->args || !strcasecmp(modp->name, r->args)) {
332                 ap_rprintf(r, "<dt><a name=\"%s\"><strong>Module Name:</strong> "
333                             "<font size=+1><tt>%s</tt></a></font>\n",
334                             modp->name, modp->name);
335                 ap_rputs("<dt><strong>Content handlers:</strong>", r);
336 #ifdef NEVERMORE
337                 hand = modp->handlers;
338                 if (hand) {
339                     while (hand) {
340                         if (hand->content_type) {
341                             ap_rprintf(r, " <tt>%s</tt>\n", hand->content_type);
342                         }
343                         else {
344                             break;
345                         }
346                         hand++;
347                         if (hand && hand->content_type) {
348                             ap_rputs(",", r);
349                         }
350                     }
351                 }
352                 else {
353                     ap_rputs("<tt> <EM>none</EM></tt>", r);
354                 }
355 #else
356                 ap_rputs("<tt> <EM>(code broken)</EM></tt>", r);
357 #endif
358                 ap_rputs("<dt><strong>Configuration Phase Participation:</strong> \n",
359                       r);
360                 if (modp->create_dir_config) {
361                     if (comma) {
362                         ap_rputs(", ", r);
363                     }
364                     ap_rputs("<tt>Create Directory Config</tt>", r);
365                     comma = 1;
366                 }
367                 if (modp->merge_dir_config) {
368                     if (comma) {
369                         ap_rputs(", ", r);
370                     }
371                     ap_rputs("<tt>Merge Directory Configs</tt>", r);
372                     comma = 1;
373                 }
374                 if (modp->create_server_config) {
375                     if (comma) {
376                         ap_rputs(", ", r);
377                     }
378                     ap_rputs("<tt>Create Server Config</tt>", r);
379                     comma = 1;
380                 }
381                 if (modp->merge_server_config) {
382                     if (comma) {
383                         ap_rputs(", ", r);
384                     }
385                     ap_rputs("<tt>Merge Server Configs</tt>", r);
386                     comma = 1;
387                 }
388                 if (!comma)
389                     ap_rputs("<tt> <EM>none</EM></tt>", r);
390                 comma = 0;
391                 ap_rputs("<dt><strong>Module Directives:</strong> ", r);
392                 cmd = modp->cmds;
393                 if (cmd) {
394                     while (cmd) {
395                         if (cmd->name) {
396                             ap_rprintf(r, "<dd><tt>%s - <i>",
397                                     mod_info_html_cmd_string(cmd->name,
398                                         buf, sizeof(buf), 0));
399                             if (cmd->errmsg) {
400                                 ap_rputs(cmd->errmsg, r);
401                             }
402                             ap_rputs("</i></tt>\n", r);
403                         }
404                         else {
405                             break;
406                         }
407                         cmd++;
408                     }
409                     ap_rputs("<dt><strong>Current Configuration:</strong>\n", r);
410                     mod_info_module_cmds(r, modp->cmds, ap_conftree);
411                 }
412                 else {
413                     ap_rputs("<tt> none</tt>\n", r);
414                 }
415                 more_info = find_more_info(serv, modp->name);
416                 if (more_info) {
417                     ap_rputs("<dt><strong>Additional Information:</strong>\n<dd>",
418                           r);
419                     ap_rputs(more_info, r);
420                 }
421                 ap_rputs("<dt><hr>\n", r);
422                 if (r->args) {
423                     break;
424                 }
425             }
426         }
427         if (!modp && r->args && strcasecmp(r->args, "server")) {
428             ap_rputs("<b>No such module</b>\n", r);
429         }
430     }
431     else {
432         for (modp = top_module; modp; modp = modp->next) {
433             ap_rputs(modp->name, r);
434             if (modp->next) {
435                 ap_rputs("<br>", r);
436             }
437         }
438     }
439     ap_rputs("</dl>\n", r);
440     ap_rputs(ap_psignature("",r), r);
441     ap_rputs("</body></html>\n", r);
442     /* Done, turn off timeout, close file and return */
443     return 0;
444 }
445
446 static const char *add_module_info(cmd_parms *cmd, void *dummy, 
447                                    const char *name, const char *info)
448 {
449     server_rec *s = cmd->server;
450     info_svr_conf *conf = (info_svr_conf *) ap_get_module_config(s->module_config,
451                                                               &info_module);
452     info_entry *new = apr_push_array(conf->more_info);
453
454     new->name = name;
455     new->info = info;
456     return NULL;
457 }
458
459 static const command_rec info_cmds[] =
460 {
461     AP_INIT_TAKE2("AddModuleInfo", add_module_info, NULL, RSRC_CONF,
462                   "a module name and additional information on that module"),
463     {NULL}
464 };
465
466 static void register_hooks(void)
467 {
468     ap_hook_handler(display_info, NULL, NULL, AP_HOOK_MIDDLE);
469 }
470
471 module AP_MODULE_DECLARE_DATA info_module =
472 {
473     STANDARD20_MODULE_STUFF,
474     NULL,                       /* dir config creater */
475     NULL,                       /* dir merger --- default is to override */
476     create_info_config,         /* server config */
477     merge_info_config,          /* merge server config */
478     info_cmds,                  /* command apr_table_t */
479     register_hooks
480 };