]> granicus.if.org Git - apache/blob - modules/generators/mod_info.c
a8af1bcbfa3f15706690f0e872824a51543ab50f
[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
95 #if APR_HAVE_STRINGS_H
96 #include <strings.h>
97 #endif
98
99 typedef struct {
100     const char *name;                 /* matching module name */
101     const char *info;                 /* additional info */
102 } info_entry;
103
104 typedef struct {
105     apr_array_header_t *more_info;
106 } info_svr_conf;
107
108 module AP_MODULE_DECLARE_DATA info_module;
109
110 extern module *top_module;
111 extern ap_directive_t *ap_conftree;
112
113 static void *create_info_config(apr_pool_t *p, server_rec *s)
114 {
115     info_svr_conf *conf = (info_svr_conf *) apr_pcalloc(p, sizeof(info_svr_conf));
116
117     conf->more_info = apr_array_make(p, 20, sizeof(info_entry));
118     return conf;
119 }
120
121 static void *merge_info_config(apr_pool_t *p, void *basev, void *overridesv)
122 {
123     info_svr_conf *new = (info_svr_conf *) apr_pcalloc(p, sizeof(info_svr_conf));
124     info_svr_conf *base = (info_svr_conf *) basev;
125     info_svr_conf *overrides = (info_svr_conf *) overridesv;
126
127     new->more_info = apr_array_append(p, overrides->more_info, base->more_info);
128     return new;
129 }
130
131 static char *mod_info_html_cmd_string(const char *string, char *buf, size_t buf_len, int close)
132 {
133     const char *s;
134     char *t;
135     char *end_buf;
136
137     s = string;
138     t = buf;
139     /* keep space for \0 byte */
140     end_buf = buf + buf_len - 1;
141     while ((*s) && (t < end_buf)) {
142         if (*s == '<') {
143             if (close) {
144                 strncpy(t, "&lt;/,", end_buf -t);
145                 t += 5;
146             } else {
147                 strncpy(t, "&lt;", end_buf - t);
148                 t += 4;
149             }
150         }
151         else if (*s == '>') {
152             strncpy(t, "&gt;", end_buf - t);
153             t += 4;
154         }
155         else if (*s == '&') {
156             strncpy(t, "&amp;", end_buf - t);
157             t += 5;
158         }
159         else if (*s == ' ') {
160             if (close) {
161                 strncpy(t, "&gt;", end_buf -t);
162                 t += 4;
163                 break;
164             } else {
165               *t++ = *s;
166             }
167         } else {
168             *t++ = *s;
169         }
170         s++;
171     }
172     /* oops, overflowed... don't overwrite */
173     if (t > end_buf) {
174         *end_buf = '\0';
175     }
176     else {
177         *t = '\0';
178     }
179     return (buf);
180 }
181
182 static void mod_info_module_cmds(request_rec * r, const command_rec * cmds,
183                                  ap_directive_t * conftree)
184 {
185     const command_rec *cmd;
186     ap_directive_t *tmptree = conftree;
187
188     char buf[MAX_STRING_LEN];
189     char htmlstring[MAX_STRING_LEN];
190     int block_start = 0;
191     int nest = 0;
192
193     while (tmptree != NULL) {
194         cmd = cmds;
195         while (cmd->name) {
196             if (!strcasecmp(cmd->name, tmptree->directive)) {
197                 if (nest > block_start) {
198                     block_start++;
199                     apr_snprintf(htmlstring, sizeof(htmlstring), "%s %s",
200                                 tmptree->parent->directive,
201                                 tmptree->parent->args);
202                     ap_rprintf(r, "<dd><tt>%s</tt><br>\n",
203                                mod_info_html_cmd_string(htmlstring, buf,
204                                                         sizeof(buf), 0));
205                 }
206                 if (nest == 2) {
207                     ap_rprintf(r, "<dd><tt>&nbsp;&nbsp;&nbsp;&nbsp;%s "
208                                "<i>%s</i></tt><br>\n",
209                                tmptree->directive, tmptree->args);
210                 } else if (nest == 1) {
211                     ap_rprintf(r,
212                                "<dd><tt>&nbsp;&nbsp;%s <i>%s</i></tt><br>\n",
213                                tmptree->directive, tmptree->args);
214                 } else {
215                     ap_rprintf(r, "<dd><tt>%s <i>%s</i></tt><br>\n",
216                                mod_info_html_cmd_string(tmptree->directive,
217                                                         buf, sizeof(buf),
218                                                         0), tmptree->args);
219                 }
220             }
221             ++cmd;
222         }
223         if (tmptree->first_child != NULL) {
224             tmptree = tmptree->first_child;
225             nest++;
226         } else if (tmptree->next != NULL) {
227             tmptree = tmptree->next;
228         } else {
229             if (block_start) {
230                 apr_snprintf(htmlstring, sizeof(htmlstring), "%s %s",
231                             tmptree->parent->directive,
232                             tmptree->parent->args);
233                 ap_rprintf(r, "<dd><tt>%s</tt><br>\n",
234                            mod_info_html_cmd_string(htmlstring, buf,
235                                                     sizeof(buf), 1));
236                 block_start--;
237             }
238             if (tmptree->parent) {
239                 tmptree = tmptree->parent->next;
240             }
241             else {
242                 tmptree = NULL;
243             }
244             nest--;
245         }
246
247     }
248 }
249 static const char *find_more_info(server_rec *s, const char *module_name)
250 {
251     int i;
252     info_svr_conf *conf = (info_svr_conf *) ap_get_module_config(s->module_config,
253                                                               &info_module);
254     info_entry *entry = (info_entry *) conf->more_info->elts;
255
256     if (!module_name) {
257         return 0;
258     }
259     for (i = 0; i < conf->more_info->nelts; i++) {
260         if (!strcmp(module_name, entry->name)) {
261             return entry->info;
262         }
263         entry++;
264     }
265     return 0;
266 }
267
268 static int display_info(request_rec *r)
269 {
270     module *modp = NULL;
271     char buf[MAX_STRING_LEN];
272     const char *cfname;
273     const char *more_info;
274     const command_rec *cmd = NULL;
275 #ifdef NEVERMORE
276     const handler_rec *hand = NULL;
277 #endif
278     server_rec *serv = r->server;
279     int comma = 0;
280
281     if (strcmp(r->handler, "server-info"))
282         return DECLINED;
283
284     r->allowed |= (1 << M_GET);
285     if (r->method_number != M_GET)
286         return DECLINED;
287
288     r->content_type = "text/html";
289     ap_send_http_header(r);
290     if (r->header_only) {
291         return 0;
292     }
293
294     ap_rputs(DOCTYPE_HTML_3_2
295              "<html><head><title>Server Information</title></head>\n", r);
296     ap_rputs("<body><h1 align=center>Apache Server Information</h1>\n", r);
297     if (!r->args || strcasecmp(r->args, "list")) {
298         cfname = ap_server_root_relative(r->pool, SERVER_CONFIG_FILE);
299         if (!r->args) {
300             ap_rputs("<tt><a href=\"#server\">Server Settings</a>, ", r);
301             for (modp = top_module; modp; modp = modp->next) {
302                 ap_rprintf(r, "<a href=\"#%s\">%s</a>", modp->name, modp->name);
303                 if (modp->next) {
304                     ap_rputs(", ", r);
305                 }
306             }
307             ap_rputs("</tt><hr>", r);
308
309         }
310         if (!r->args || !strcasecmp(r->args, "server")) {
311             ap_rprintf(r, "<a name=\"server\"><strong>Server Version:</strong> "
312                         "<font size=+1><tt>%s</tt></a></font><br>\n",
313                         ap_get_server_version());
314             ap_rprintf(r, "<strong>Server Built:</strong> "
315                         "<font size=+1><tt>%s</tt></a></font><br>\n",
316                         ap_get_server_built());
317             ap_rprintf(r, "<strong>API Version:</strong> "
318                         "<tt>%d:%d</tt><br>\n",
319                         MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
320             ap_rprintf(r, "<strong>Hostname/port:</strong> "
321                         "<tt>%s:%u</tt><br>\n",
322                         serv->server_hostname, serv->port);
323             ap_rprintf(r, "<strong>Timeouts:</strong> "
324                         "<tt>connection: %d &nbsp;&nbsp; "
325                         "keep-alive: %d</tt><br>",
326                         serv->timeout, serv->keep_alive_timeout);
327             ap_rprintf(r, "<strong>Server Root:</strong> "
328                         "<tt>%s</tt><br>\n", ap_server_root);
329             ap_rprintf(r, "<strong>Config File:</strong> "
330                        "<tt>%s</tt><br>\n", SERVER_CONFIG_FILE);
331         }
332         ap_rputs("<hr><dl>", r);
333         for (modp = top_module; modp; modp = modp->next) {
334             if (!r->args || !strcasecmp(modp->name, r->args)) {
335                 ap_rprintf(r, "<dt><a name=\"%s\"><strong>Module Name:</strong> "
336                             "<font size=+1><tt>%s</tt></a></font>\n",
337                             modp->name, modp->name);
338                 ap_rputs("<dt><strong>Content handlers:</strong>", r);
339 #ifdef NEVERMORE
340                 hand = modp->handlers;
341                 if (hand) {
342                     while (hand) {
343                         if (hand->content_type) {
344                             ap_rprintf(r, " <tt>%s</tt>\n", hand->content_type);
345                         }
346                         else {
347                             break;
348                         }
349                         hand++;
350                         if (hand && hand->content_type) {
351                             ap_rputs(",", r);
352                         }
353                     }
354                 }
355                 else {
356                     ap_rputs("<tt> <EM>none</EM></tt>", r);
357                 }
358 #else
359                 ap_rputs("<tt> <EM>(code broken)</EM></tt>", r);
360 #endif
361                 ap_rputs("<dt><strong>Configuration Phase Participation:</strong> \n",
362                       r);
363                 if (modp->create_dir_config) {
364                     if (comma) {
365                         ap_rputs(", ", r);
366                     }
367                     ap_rputs("<tt>Create Directory Config</tt>", r);
368                     comma = 1;
369                 }
370                 if (modp->merge_dir_config) {
371                     if (comma) {
372                         ap_rputs(", ", r);
373                     }
374                     ap_rputs("<tt>Merge Directory Configs</tt>", r);
375                     comma = 1;
376                 }
377                 if (modp->create_server_config) {
378                     if (comma) {
379                         ap_rputs(", ", r);
380                     }
381                     ap_rputs("<tt>Create Server Config</tt>", r);
382                     comma = 1;
383                 }
384                 if (modp->merge_server_config) {
385                     if (comma) {
386                         ap_rputs(", ", r);
387                     }
388                     ap_rputs("<tt>Merge Server Configs</tt>", r);
389                     comma = 1;
390                 }
391                 if (!comma)
392                     ap_rputs("<tt> <EM>none</EM></tt>", r);
393                 comma = 0;
394                 ap_rputs("<dt><strong>Module Directives:</strong> ", r);
395                 cmd = modp->cmds;
396                 if (cmd) {
397                     while (cmd) {
398                         if (cmd->name) {
399                             ap_rprintf(r, "<dd><tt>%s - <i>",
400                                     mod_info_html_cmd_string(cmd->name,
401                                         buf, sizeof(buf), 0));
402                             if (cmd->errmsg) {
403                                 ap_rputs(cmd->errmsg, r);
404                             }
405                             ap_rputs("</i></tt>\n", r);
406                         }
407                         else {
408                             break;
409                         }
410                         cmd++;
411                     }
412                     ap_rputs("<dt><strong>Current Configuration:</strong>\n", r);
413                     mod_info_module_cmds(r, modp->cmds, ap_conftree);
414                 }
415                 else {
416                     ap_rputs("<tt> none</tt>\n", r);
417                 }
418                 more_info = find_more_info(serv, modp->name);
419                 if (more_info) {
420                     ap_rputs("<dt><strong>Additional Information:</strong>\n<dd>",
421                           r);
422                     ap_rputs(more_info, r);
423                 }
424                 ap_rputs("<dt><hr>\n", r);
425                 if (r->args) {
426                     break;
427                 }
428             }
429         }
430         if (!modp && r->args && strcasecmp(r->args, "server")) {
431             ap_rputs("<b>No such module</b>\n", r);
432         }
433     }
434     else {
435         for (modp = top_module; modp; modp = modp->next) {
436             ap_rputs(modp->name, r);
437             if (modp->next) {
438                 ap_rputs("<br>", r);
439             }
440         }
441     }
442     ap_rputs("</dl>\n", r);
443     ap_rputs(ap_psignature("",r), r);
444     ap_rputs("</body></html>\n", r);
445     /* Done, turn off timeout, close file and return */
446     return 0;
447 }
448
449 static const char *add_module_info(cmd_parms *cmd, void *dummy, 
450                                    const char *name, const char *info)
451 {
452     server_rec *s = cmd->server;
453     info_svr_conf *conf = (info_svr_conf *) ap_get_module_config(s->module_config,
454                                                               &info_module);
455     info_entry *new = apr_array_push(conf->more_info);
456
457     new->name = name;
458     new->info = info;
459     return NULL;
460 }
461
462 static const command_rec info_cmds[] =
463 {
464     AP_INIT_TAKE2("AddModuleInfo", add_module_info, NULL, RSRC_CONF,
465                   "a module name and additional information on that module"),
466     {NULL}
467 };
468
469 static void register_hooks(apr_pool_t *p)
470 {
471     ap_hook_handler(display_info, NULL, NULL, APR_HOOK_MIDDLE);
472 }
473
474 module AP_MODULE_DECLARE_DATA info_module =
475 {
476     STANDARD20_MODULE_STUFF,
477     NULL,                       /* dir config creater */
478     NULL,                       /* dir merger --- default is to override */
479     create_info_config,         /* server config */
480     merge_info_config,          /* merge server config */
481     info_cmds,                  /* command apr_table_t */
482     register_hooks
483 };