]> granicus.if.org Git - apache/blob - modules/loggers/mod_log_debug.c
4ccf9c916f8505200c4a4d000eae73fda8ec4c5d
[apache] / modules / loggers / mod_log_debug.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_strings.h"
18
19 #include "httpd.h"
20 #include "http_config.h"
21 #include "http_log.h"
22 #include "http_protocol.h"
23 #include "http_request.h"
24 #include "ap_expr.h"
25
26 extern module AP_MODULE_DECLARE_DATA log_debug_module;
27
28 typedef struct {
29     ap_expr_info_t *msg_expr;
30     ap_expr_info_t *condition;
31     const char *hook;
32 } msg_entry;
33
34 typedef struct {
35     apr_array_header_t *entries;
36 } log_debug_dirconf;
37
38 const char *allhooks = "all";
39 const char * const hooks[] = {
40     "log_transaction",      /*  0 */
41     "quick_handler",        /*  1 */
42     "handler",              /*  2 */
43     "translate_name",       /*  3 */
44     "map_to_storage",       /*  4 */
45     "fixups",               /*  5 */
46     "type_checker",         /*  6 */
47     "check_access",         /*  7 */
48     "check_access_ex",      /*  8 */
49     "check_authn",          /*  9 */
50     "check_authz",          /* 10 */
51     "insert_filter",        /* 11 */
52     NULL
53 };
54
55 static void do_debug_log(request_rec *r, const char *hookname)
56 {
57     log_debug_dirconf *dconf = ap_get_module_config(r->per_dir_config, &log_debug_module);
58     int i;
59     if (dconf->entries == NULL)
60         return;
61
62     for (i = 0; i < dconf->entries->nelts; i++) {
63         const char *msg, *err;
64         msg_entry *entry = APR_ARRAY_IDX(dconf->entries, i, msg_entry *);
65         if (entry->hook != allhooks && entry->hook != hookname)
66             continue;
67         if (entry->condition) {
68             int ret = ap_expr_exec(r, entry->condition, &err);
69             if (err) {
70                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
71                               "Can't evaluate condition: %s", err);
72                 continue;
73             }
74             if (!ret)
75                 continue;
76         }
77         msg = ap_expr_str_exec(r, entry->msg_expr, &err);
78         if (err)
79             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
80                           "Can't evaluate message expression: %s", err);
81         if (APLOGrdebug(r))
82             ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%s (%s hook, %s:%d)",
83                            msg, hookname, entry->msg_expr->filename,
84                            entry->msg_expr->line_number);
85         else
86             ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, "%s", msg);
87     }
88 }
89
90 static int log_debug_log_transaction(request_rec *r)
91 {
92     do_debug_log(r, hooks[0]);
93     return DECLINED;
94 }
95
96 static int log_debug_quick_handler(request_rec *r, int lookup_uri)
97 {
98     do_debug_log(r, hooks[1]);
99     return DECLINED;
100 }
101
102 static int log_debug_handler(request_rec *r)
103 {
104     do_debug_log(r, hooks[2]);
105     return DECLINED;
106 }
107
108 static int log_debug_translate_name(request_rec *r)
109 {
110     do_debug_log(r, hooks[3]);
111     return DECLINED;
112 }
113
114 static int log_debug_map_to_storage(request_rec *r)
115 {
116     do_debug_log(r, hooks[4]);
117     return DECLINED;
118 }
119
120 static int log_debug_fixups(request_rec *r)
121 {
122     do_debug_log(r, hooks[5]);
123     return DECLINED;
124 }
125
126 static int log_debug_type_checker(request_rec *r)
127 {
128     do_debug_log(r, hooks[6]);
129     return DECLINED;
130 }
131
132 static int log_debug_check_access(request_rec *r)
133 {
134     do_debug_log(r, hooks[7]);
135     return DECLINED;
136 }
137
138 static int log_debug_check_access_ex(request_rec *r)
139 {
140     do_debug_log(r, hooks[8]);
141     return DECLINED;
142 }
143
144 static int log_debug_check_authn(request_rec *r)
145 {
146     do_debug_log(r, hooks[9]);
147     return DECLINED;
148 }
149
150 static int log_debug_check_authz(request_rec *r)
151 {
152     do_debug_log(r, hooks[10]);
153     return DECLINED;
154 }
155
156 static void log_debug_insert_filter(request_rec *r)
157 {
158     do_debug_log(r, hooks[11]);
159 }
160
161 static void *log_debug_create_dconf(apr_pool_t *p, char *dirspec)
162 {
163     log_debug_dirconf *dconf = apr_pcalloc(p, sizeof(log_debug_dirconf));
164     return dconf;
165 }
166
167 static void *log_debug_merge_dconf(apr_pool_t *p, void *parent_conf, void *new_conf)
168 {
169     log_debug_dirconf *merged = apr_pcalloc(p, sizeof(log_debug_dirconf));
170     const log_debug_dirconf *parent = parent_conf;
171     const log_debug_dirconf *new = new_conf;
172
173     if (parent->entries == NULL)
174         merged->entries = new->entries;
175     else if (new->entries == NULL)
176         merged->entries = parent->entries;
177     else
178         /* apr_array_append actually creates a new array */
179         merged->entries = apr_array_append(p, parent->entries, new->entries);
180
181     return merged;
182 }
183
184 static const char *cmd_log_message(cmd_parms *cmd, void *dconf_, const char *arg1,
185                                    const char *arg2, const char *arg3)
186 {
187     msg_entry *entry = apr_pcalloc(cmd->pool, sizeof(msg_entry));
188     log_debug_dirconf *dconf = dconf_;
189     int i, j;
190     const char *err;
191     const char *args[2];
192     args[0] = arg2;
193     args[1] = arg3;
194
195     entry->msg_expr = ap_expr_parse_cmd(cmd, arg1, AP_EXPR_FLAG_STRING_RESULT|
196                                                    AP_EXPR_FLAG_DONT_VARY,
197                                         &err, NULL);
198     if (err)
199         return apr_psprintf(cmd->pool,
200                             "Could not parse message expression '%s': %s",
201                             arg1, err);
202
203     for (i = 0; i < 2; i++) {
204         if (args[i] == NULL)
205             break;
206
207         if (strncasecmp(args[i], "hook=", 5) == 0) {
208             const char *name = args[i] + 5;
209             j = 0;
210             while (hooks[j]) {
211                 if (strcasecmp(hooks[j], name) == 0) {
212                     entry->hook = hooks[j];
213                     break;
214                 }
215                 j++;
216             }
217             if (entry->hook == NULL) {
218                 if (strcmp(name, "*") == 0 || strcasecmp(name, allhooks) == 0)
219                     entry->hook = allhooks;
220                 else
221                     return apr_psprintf(cmd->pool, "Invalid hook name: %s", name);
222             }
223         }
224         else if (strncasecmp(args[i], "expr=", 5) == 0) {
225             const char *expr = args[i] + 5;
226             entry->condition = ap_expr_parse_cmd(cmd, expr,
227                                                  AP_EXPR_FLAG_DONT_VARY,
228                                                  &err, NULL);
229             if (err)
230                 return apr_psprintf(cmd->pool,
231                                     "Could not parse expression '%s': %s",
232                                     expr, err);
233         }
234         else {
235             return apr_psprintf(cmd->pool, "Invalid argument %s", args[i]);
236         }
237     }
238     if (entry->hook == NULL)
239         entry->hook = hooks[0];
240
241     if (!dconf->entries)
242         dconf->entries = apr_array_make(cmd->pool, 4, sizeof(msg_entry *));
243
244     APR_ARRAY_PUSH(dconf->entries, msg_entry *) = entry;
245
246     return NULL;
247 }
248
249 static const command_rec log_debug_cmds[] =
250 {
251     AP_INIT_TAKE123("LogMessage", cmd_log_message, NULL, RSRC_CONF|ACCESS_CONF,
252         "Log a debug message to the error log if this config block is used for "
253         " a request"),
254     {NULL}
255 };
256
257 static void register_hooks(apr_pool_t *p)
258 {
259     ap_hook_log_transaction(log_debug_log_transaction, NULL, NULL, APR_HOOK_FIRST);
260     ap_hook_quick_handler(log_debug_quick_handler, NULL, NULL, APR_HOOK_FIRST);
261     ap_hook_handler(log_debug_handler, NULL, NULL, APR_HOOK_FIRST);
262     ap_hook_translate_name(log_debug_translate_name, NULL, NULL, APR_HOOK_FIRST);
263     ap_hook_map_to_storage(log_debug_map_to_storage, NULL, NULL, APR_HOOK_FIRST);
264     ap_hook_fixups(log_debug_fixups, NULL, NULL, APR_HOOK_FIRST);
265     ap_hook_type_checker(log_debug_type_checker, NULL, NULL, APR_HOOK_FIRST);
266     ap_hook_check_access(log_debug_check_access, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
267     ap_hook_check_access_ex(log_debug_check_access_ex, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
268     ap_hook_check_authn(log_debug_check_authn, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
269     ap_hook_check_authz(log_debug_check_authz, NULL, NULL, APR_HOOK_FIRST, AP_AUTH_INTERNAL_PER_URI);
270     ap_hook_insert_filter(log_debug_insert_filter, NULL, NULL, APR_HOOK_FIRST);
271 }
272
273 AP_DECLARE_MODULE(log_debug) =
274 {
275     STANDARD20_MODULE_STUFF,
276     log_debug_create_dconf,     /* create per-dir config */
277     log_debug_merge_dconf,      /* merge per-dir config */
278     NULL,                       /* server config */
279     NULL,                       /* merge server config */
280     log_debug_cmds,             /* command apr_table_t */
281     register_hooks              /* register hooks */
282 };
283