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 /*
18  * Apache example_hooks module.  Provide demonstrations of how modules do things.
19  * It is not meant to be used in a production server.  Since it participates
20  * in all of the processing phases, it could conceivable interfere with
21  * the proper operation of other modules -- particularly the ones related
22  * to security.
23  *
24  * In the interest of brevity, all functions and structures internal to
25  * this module, but which may have counterparts in *real* modules, are
26  * prefixed with 'x_' instead of 'example_'.
27  *
28  * To use mod_example_hooks, configure the Apache build with
29  * --enable-example-hooks and compile.  Set up a <Location> block in your
30  * configuration file like so:
31  *
32  * <Location /example>
33  *    SetHandler example-hooks-handler
34  * </Location>
35  *
36  * When you look at that location on your server, you will see a backtrace of
37  * the callbacks that have been invoked up to that point.  See the ErrorLog for
38  * more information on code paths that  touch mod_example_hooks.
39  *
40  * IMPORTANT NOTES
41  * ===============
42  *
43  * Do NOT use this module on a production server. It attaches itself to every
44  * phase of the server runtime operations including startup, shutdown and
45  * request processing, and produces copious amounts of logging data.  This will
46  * negatively affect server performance.
47  *
48  * Do NOT use mod_example_hooks as the basis for your own code.  This module
49  * implements every callback hook offered by the Apache core, and your
50  * module will almost certainly not have to implement this much.  If you
51  * want a simple module skeleton to start development, use apxs -g.
52  *
53  * XXX TO DO XXX
54  * =============
55  *
56  * * Enable HTML backtrace entries for more callbacks that are not directly
57  *   associated with a request
58  * * Make sure every callback that posts an HTML backtrace entry does so in the *   right category, so nothing gets overwritten
59  * * Implement some logic to show what happens in the parent, and what in the
60  *   child(ren)
61  */
62 
63 #include "httpd.h"
64 #include "http_config.h"
65 #include "http_core.h"
66 #include "http_log.h"
67 #include "http_main.h"
68 #include "http_protocol.h"
69 #include "http_request.h"
70 #include "util_script.h"
71 #include "http_connection.h"
72 #ifdef HAVE_UNIX_SUEXEC
73 #include "unixd.h"
74 #endif
75 #include "scoreboard.h"
76 #include "mpm_common.h"
77 
78 #include "apr_strings.h"
79 
80 #include <stdio.h>
81 
82 /*--------------------------------------------------------------------------*/
83 /*                                                                          */
84 /* Data declarations.                                                       */
85 /*                                                                          */
86 /* Here are the static cells and structure declarations private to our      */
87 /* module.                                                                  */
88 /*                                                                          */
89 /*--------------------------------------------------------------------------*/
90 
91 /*
92  * Sample configuration record.  Used for both per-directory and per-server
93  * configuration data.
94  *
95  * It's perfectly reasonable to have two different structures for the two
96  * different environments.  The same command handlers will be called for
97  * both, though, so the handlers need to be able to tell them apart.  One
98  * possibility is for both structures to start with an int which is 0 for
99  * one and 1 for the other.
100  *
101  * Note that while the per-directory and per-server configuration records are
102  * available to most of the module handlers, they should be treated as
103  * READ-ONLY by all except the command and merge handlers.  Sometimes handlers
104  * are handed a record that applies to the current location by implication or
105  * inheritance, and modifying it will change the rules for other locations.
106  */
107 typedef struct x_cfg {
108     int cmode;                  /* Environment to which record applies
109                                  * (directory, server, or combination).
110                                  */
111 #define CONFIG_MODE_SERVER 1
112 #define CONFIG_MODE_DIRECTORY 2
113 #define CONFIG_MODE_COMBO 3     /* Shouldn't ever happen. */
114     int local;                  /* Boolean: "Example" directive declared
115                                  * here?
116                                  */
117     int congenital;             /* Boolean: did we inherit an "Example"? */
118     char *trace;                /* Pointer to trace string. */
119     char *loc;                  /* Location to which this record applies. */
120 } x_cfg;
121 
122 /*
123  * String pointer to hold the startup trace. No harm working with a global until
124  * the server is (may be) multi-threaded.
125  */
126 static const char *trace = NULL;
127 
128 /*
129  * Declare ourselves so the configuration routines can find and know us.
130  * We'll fill it in at the end of the module.
131  */
132 module AP_MODULE_DECLARE_DATA example_hooks_module;
133 
134 /*--------------------------------------------------------------------------*/
135 /*                                                                          */
136 /* The following pseudo-prototype declarations illustrate the parameters    */
137 /* passed to command handlers for the different types of directive          */
138 /* syntax.  If an argument was specified in the directive definition        */
139 /* (look for "command_rec" below), it's available to the command handler    */
140 /* via the (void *) info field in the cmd_parms argument passed to the      */
141 /* handler (cmd->info for the examples below).                              */
142 /*                                                                          */
143 /*--------------------------------------------------------------------------*/
144 
145 /*
146  * Command handler for a NO_ARGS directive.  Declared in the command_rec
147  * list with
148  *   AP_INIT_NO_ARGS("directive", function, mconfig, where, help)
149  *
150  * static const char *handle_NO_ARGS(cmd_parms *cmd, void *mconfig);
151  */
152 
153 /*
154  * Command handler for a RAW_ARGS directive.  The "args" argument is the text
155  * of the commandline following the directive itself.  Declared in the
156  * command_rec list with
157  *   AP_INIT_RAW_ARGS("directive", function, mconfig, where, help)
158  *
159  * static const char *handle_RAW_ARGS(cmd_parms *cmd, void *mconfig,
160  *                                    const char *args);
161  */
162 
163 /*
164  * Command handler for a FLAG directive.  The single parameter is passed in
165  * "bool", which is either zero or not for Off or On respectively.
166  * Declared in the command_rec list with
167  *   AP_INIT_FLAG("directive", function, mconfig, where, help)
168  *
169  * static const char *handle_FLAG(cmd_parms *cmd, void *mconfig, int bool);
170  */
171 
172 /*
173  * Command handler for a TAKE1 directive.  The single parameter is passed in
174  * "word1".  Declared in the command_rec list with
175  *   AP_INIT_TAKE1("directive", function, mconfig, where, help)
176  *
177  * static const char *handle_TAKE1(cmd_parms *cmd, void *mconfig,
178  *                                 char *word1);
179  */
180 
181 /*
182  * Command handler for a TAKE2 directive.  TAKE2 commands must always have
183  * exactly two arguments.  Declared in the command_rec list with
184  *   AP_INIT_TAKE2("directive", function, mconfig, where, help)
185  *
186  * static const char *handle_TAKE2(cmd_parms *cmd, void *mconfig,
187  *                                 char *word1, char *word2);
188  */
189 
190 /*
191  * Command handler for a TAKE3 directive.  Like TAKE2, these must have exactly
192  * three arguments, or the parser complains and doesn't bother calling us.
193  * Declared in the command_rec list with
194  *   AP_INIT_TAKE3("directive", function, mconfig, where, help)
195  *
196  * static const char *handle_TAKE3(cmd_parms *cmd, void *mconfig,
197  *                                 char *word1, char *word2, char *word3);
198  */
199 
200 /*
201  * Command handler for a TAKE12 directive.  These can take either one or two
202  * arguments.
203  * - word2 is a NULL pointer if no second argument was specified.
204  * Declared in the command_rec list with
205  *   AP_INIT_TAKE12("directive", function, mconfig, where, help)
206  *
207  * static const char *handle_TAKE12(cmd_parms *cmd, void *mconfig,
208  *                                  char *word1, char *word2);
209  */
210 
211 /*
212  * Command handler for a TAKE123 directive.  A TAKE123 directive can be given,
213  * as might be expected, one, two, or three arguments.
214  * - word2 is a NULL pointer if no second argument was specified.
215  * - word3 is a NULL pointer if no third argument was specified.
216  * Declared in the command_rec list with
217  *   AP_INIT_TAKE123("directive", function, mconfig, where, help)
218  *
219  * static const char *handle_TAKE123(cmd_parms *cmd, void *mconfig,
220  *                                   char *word1, char *word2, char *word3);
221  */
222 
223 /*
224  * Command handler for a TAKE13 directive.  Either one or three arguments are
225  * permitted - no two-parameters-only syntax is allowed.
226  * - word2 and word3 are NULL pointers if only one argument was specified.
227  * Declared in the command_rec list with
228  *   AP_INIT_TAKE13("directive", function, mconfig, where, help)
229  *
230  * static const char *handle_TAKE13(cmd_parms *cmd, void *mconfig,
231  *                                  char *word1, char *word2, char *word3);
232  */
233 
234 /*
235  * Command handler for a TAKE23 directive.  At least two and as many as three
236  * arguments must be specified.
237  * - word3 is a NULL pointer if no third argument was specified.
238  * Declared in the command_rec list with
239  *   AP_INIT_TAKE23("directive", function, mconfig, where, help)
240  *
241  * static const char *handle_TAKE23(cmd_parms *cmd, void *mconfig,
242  *                                  char *word1, char *word2, char *word3);
243  */
244 
245 /*
246  * Command handler for a ITERATE directive.
247  * - Handler is called once for each of n arguments given to the directive.
248  * - word1 points to each argument in turn.
249  * Declared in the command_rec list with
250  *   AP_INIT_ITERATE("directive", function, mconfig, where, help)
251  *
252  * static const char *handle_ITERATE(cmd_parms *cmd, void *mconfig,
253  *                                   char *word1);
254  */
255 
256 /*
257  * Command handler for a ITERATE2 directive.
258  * - Handler is called once for each of the second and subsequent arguments
259  *   given to the directive.
260  * - word1 is the same for each call for a particular directive instance (the
261  *   first argument).
262  * - word2 points to each of the second and subsequent arguments in turn.
263  * Declared in the command_rec list with
264  *   AP_INIT_ITERATE2("directive", function, mconfig, where, help)
265  *
266  * static const char *handle_ITERATE2(cmd_parms *cmd, void *mconfig,
267  *                                    char *word1, char *word2);
268  */
269 
270 /*--------------------------------------------------------------------------*/
271 /*                                                                          */
272 /* These routines are strictly internal to this module, and support its     */
273 /* operation.  They are not referenced by any external portion of the       */
274 /* server.                                                                  */
275 /*                                                                          */
276 /*--------------------------------------------------------------------------*/
277 
278 /*
279  * Locate our directory configuration record for the current request.
280  */
our_dconfig(const request_rec * r)281 static x_cfg *our_dconfig(const request_rec *r)
282 {
283     return (x_cfg *) ap_get_module_config(r->per_dir_config, &example_hooks_module);
284 }
285 
286 /*
287  * The following utility routines are not used in the module. Don't
288  * compile them so -Wall doesn't complain about functions that are
289  * defined but not used.
290  */
291 #if 0
292 /*
293  * Locate our server configuration record for the specified server.
294  */
295 static x_cfg *our_sconfig(const server_rec *s)
296 {
297     return (x_cfg *) ap_get_module_config(s->module_config, &example_hooks_module);
298 }
299 
300 /*
301  * Likewise for our configuration record for the specified request.
302  */
303 static x_cfg *our_rconfig(const request_rec *r)
304 {
305     return (x_cfg *) ap_get_module_config(r->request_config, &example_hooks_module);
306 }
307 #endif /* if 0 */
308 
309 /*
310  * Likewise for our configuration record for a connection.
311  */
our_cconfig(const conn_rec * c)312 static x_cfg *our_cconfig(const conn_rec *c)
313 {
314     return (x_cfg *) ap_get_module_config(c->conn_config, &example_hooks_module);
315 }
316 
317 /*
318  * You *could* change the following if you wanted to see the calling
319  * sequence reported in the server's error_log, but beware - almost all of
320  * these co-routines are called for every single request, and the impact
321  * on the size (and readability) of the error_log is considerable.
322  */
323 #ifndef EXAMPLE_LOG_EACH
324 #define EXAMPLE_LOG_EACH 0
325 #endif
326 
327 #if EXAMPLE_LOG_EACH
example_log_each(apr_pool_t * p,server_rec * s,const char * note)328 static void example_log_each(apr_pool_t *p, server_rec *s, const char *note)
329 {
330     if (s != NULL) {
331         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02991)
332                      "mod_example_hooks: %s", note);
333     }
334     else {
335         apr_file_t *out = NULL;
336         apr_file_open_stderr(&out, p);
337         apr_file_printf(out, "mod_example_hooks traced in non-loggable "
338                         "context: %s\n", note);
339     }
340 }
341 #endif
342 
343 /*
344  * This utility routine traces the hooks called when the server starts up.
345  * It leaves a trace in a global variable, so it should not be called from
346  * a hook handler that runs in a multi-threaded situation.
347  */
348 
trace_startup(apr_pool_t * p,server_rec * s,x_cfg * mconfig,const char * note)349 static void trace_startup(apr_pool_t *p, server_rec *s, x_cfg *mconfig,
350                           const char *note)
351 {
352     const char *sofar;
353     char *where, *addon;
354 
355 #if EXAMPLE_LOG_EACH
356     example_log_each(p, s, note);
357 #endif
358 
359     /*
360      * If we weren't passed a configuration record, we can't figure out to
361      * what location this call applies.  This only happens for co-routines
362      * that don't operate in a particular directory or server context.  If we
363      * got a valid record, extract the location (directory or server) to which
364      * it applies.
365      */
366     where = (mconfig != NULL) ? mconfig->loc : "nowhere";
367     where = (where != NULL) ? where : "";
368 
369     addon = apr_pstrcat(p,
370                         "   <li>\n"
371                         "    <dl>\n"
372                         "     <dt><samp>", note, "</samp></dt>\n"
373                         "     <dd><samp>[", where, "]</samp></dd>\n"
374                         "    </dl>\n"
375                         "   </li>\n",
376                         NULL);
377 
378     /*
379      * Make sure that we start with a valid string, even if we have never been
380      * called.
381      */
382     sofar = (trace == NULL) ? "" : trace;
383 
384     trace = apr_pstrcat(p, sofar, addon, NULL);
385 }
386 
387 
388 /*
389  * This utility route traces the hooks called as a request is handled.
390  * It takes the current request as argument
391  */
392 #define TRACE_NOTE "example-hooks-trace"
393 
trace_request(const request_rec * r,const char * note)394 static void trace_request(const request_rec *r, const char *note)
395 {
396     const char *trace_copy, *sofar;
397     char *addon, *where;
398     x_cfg *cfg;
399 
400 #if EXAMPLE_LOG_EACH
401     example_log_each(r->pool, r->server, note);
402 #endif
403 
404     if ((sofar = apr_table_get(r->notes, TRACE_NOTE)) == NULL) {
405         sofar = "";
406     }
407 
408     cfg = our_dconfig(r);
409 
410     where = (cfg != NULL) ? cfg->loc : "nowhere";
411     where = (where != NULL) ? where : "";
412 
413     addon = apr_pstrcat(r->pool,
414                         "   <li>\n"
415                         "    <dl>\n"
416                         "     <dt><samp>", note, "</samp></dt>\n"
417                         "     <dd><samp>[", where, "]</samp></dd>\n"
418                         "    </dl>\n"
419                         "   </li>\n",
420                         NULL);
421 
422     trace_copy = apr_pstrcat(r->pool, sofar, addon, NULL);
423     apr_table_set(r->notes, TRACE_NOTE, trace_copy);
424 }
425 
426 /*
427  * This utility routine traces the hooks called while processing a
428  * Connection. Its trace is kept in the pool notes of the pool associated
429  * with the Connection.
430  */
431 
432 /*
433  * Key to get and set the userdata.  We should be able to get away
434  * with a constant key, since in prefork mode the process will have
435  * the connection and its pool to itself entirely, and in
436  * multi-threaded mode each connection will have its own pool.
437  */
438 #define CONN_NOTE "example-hooks-connection"
439 
trace_connection(conn_rec * c,const char * note)440 static void trace_connection(conn_rec *c, const char *note)
441 {
442     const char *trace_copy, *sofar;
443     char *addon, *where;
444     void *data;
445     x_cfg *cfg;
446 
447 #if EXAMPLE_LOG_EACH
448     example_log_each(c->pool, c->base_server, note);
449 #endif
450 
451     cfg = our_cconfig(c);
452 
453     where = (cfg != NULL) ? cfg->loc : "nowhere";
454     where = (where != NULL) ? where : "";
455 
456     addon = apr_pstrcat(c->pool,
457                         "   <li>\n"
458                         "    <dl>\n"
459                         "     <dt><samp>", note, "</samp></dt>\n"
460                         "     <dd><samp>[", where, "]</samp></dd>\n"
461                         "    </dl>\n"
462                         "   </li>\n",
463                         NULL);
464 
465     /* Find existing notes and copy */
466     apr_pool_userdata_get(&data, CONN_NOTE, c->pool);
467     sofar = (data == NULL) ? "" : (const char *) data;
468 
469     /* Tack addon onto copy */
470     trace_copy = apr_pstrcat(c->pool, sofar, addon, NULL);
471 
472     /*
473      * Stash copy back into pool notes.  This call has a cleanup
474      * parameter, but we're not using it because the string has been
475      * allocated from that same pool.  There is also an unused return
476      * value: we have nowhere to communicate any error that might
477      * occur, and will have to check for the existence of this data on
478      * the other end.
479      */
480     apr_pool_userdata_set((const void *) trace_copy, CONN_NOTE,
481                           NULL, c->pool);
482 }
483 
trace_nocontext(apr_pool_t * p,const char * file,int line,const char * note)484 static void trace_nocontext(apr_pool_t *p, const char *file, int line,
485                             const char *note)
486 {
487     /*
488      * Since we have no request or connection to trace, or any idea
489      * from where this routine was called, there's really not much we
490      * can do.  If we are not logging everything by way of the
491      * EXAMPLE_LOG_EACH constant, do nothing in this routine.
492      */
493 
494 #ifdef EXAMPLE_LOG_EACH
495     ap_log_perror(file, line, APLOG_MODULE_INDEX, APLOG_NOTICE, 0, p, "%s", note);
496 #endif
497 }
498 
499 
500 /*--------------------------------------------------------------------------*/
501 /* We prototyped the various syntax for command handlers (routines that     */
502 /* are called when the configuration parser detects a directive declared    */
503 /* by our module) earlier.  Now we actually declare a "real" routine that   */
504 /* will be invoked by the parser when our "real" directive is               */
505 /* encountered.                                                             */
506 /*                                                                          */
507 /* If a command handler encounters a problem processing the directive, it   */
508 /* signals this fact by returning a non-NULL pointer to a string            */
509 /* describing the problem.                                                  */
510 /*                                                                          */
511 /* The magic return value DECLINE_CMD is used to deal with directives       */
512 /* that might be declared by multiple modules.  If the command handler      */
513 /* returns NULL, the directive was processed; if it returns DECLINE_CMD,    */
514 /* the next module (if any) that declares the directive is given a chance   */
515 /* at it.  If it returns any other value, it's treated as the text of an    */
516 /* error message.                                                           */
517 /*--------------------------------------------------------------------------*/
518 /*
519  * Command handler for the NO_ARGS "Example" directive.  All we do is mark the
520  * call in the trace log, and flag the applicability of the directive to the
521  * current location in that location's configuration record.
522  */
cmd_example(cmd_parms * cmd,void * mconfig)523 static const char *cmd_example(cmd_parms *cmd, void *mconfig)
524 {
525     x_cfg *cfg = (x_cfg *) mconfig;
526 
527     /*
528      * "Example Wuz Here"
529      */
530     cfg->local = 1;
531     trace_startup(cmd->pool, cmd->server, cfg, "cmd_example()");
532     return NULL;
533 }
534 
535 /*
536  * This function gets called to create a per-directory configuration
537  * record.  This will be called for the "default" server environment, and for
538  * each directory for which the parser finds any of our directives applicable.
539  * If a directory doesn't have any of our directives involved (i.e., they
540  * aren't in the .htaccess file, or a <Location>, <Directory>, or related
541  * block), this routine will *not* be called - the configuration for the
542  * closest ancestor is used.
543  *
544  * The return value is a pointer to the created module-specific
545  * structure.
546  */
x_create_dir_config(apr_pool_t * p,char * dirspec)547 static void *x_create_dir_config(apr_pool_t *p, char *dirspec)
548 {
549     x_cfg *cfg;
550     char *dname = dirspec;
551     char *note;
552 
553     /*
554      * Allocate the space for our record from the pool supplied.
555      */
556     cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
557     /*
558      * Now fill in the defaults.  If there are any `parent' configuration
559      * records, they'll get merged as part of a separate callback.
560      */
561     cfg->local = 0;
562     cfg->congenital = 0;
563     cfg->cmode = CONFIG_MODE_DIRECTORY;
564     /*
565      * Finally, add our trace to the callback list.
566      */
567     dname = (dname != NULL) ? dname : "";
568     cfg->loc = apr_pstrcat(p, "DIR(", dname, ")", NULL);
569     note = apr_psprintf(p, "x_create_dir_config(p == %pp, dirspec == %s)",
570                         (void*) p, dirspec);
571     trace_startup(p, NULL, cfg, note);
572     return (void *) cfg;
573 }
574 
575 /*
576  * This function gets called to merge two per-directory configuration
577  * records.  This is typically done to cope with things like .htaccess files
578  * or <Location> directives for directories that are beneath one for which a
579  * configuration record was already created.  The routine has the
580  * responsibility of creating a new record and merging the contents of the
581  * other two into it appropriately.  If the module doesn't declare a merge
582  * routine, the record for the closest ancestor location (that has one) is
583  * used exclusively.
584  *
585  * The routine MUST NOT modify any of its arguments!
586  *
587  * The return value is a pointer to the created module-specific structure
588  * containing the merged values.
589  */
x_merge_dir_config(apr_pool_t * p,void * parent_conf,void * newloc_conf)590 static void *x_merge_dir_config(apr_pool_t *p, void *parent_conf,
591                                       void *newloc_conf)
592 {
593 
594     x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
595     x_cfg *pconf = (x_cfg *) parent_conf;
596     x_cfg *nconf = (x_cfg *) newloc_conf;
597     char *note;
598 
599     /*
600      * Some things get copied directly from the more-specific record, rather
601      * than getting merged.
602      */
603     merged_config->local = nconf->local;
604     merged_config->loc = apr_pstrdup(p, nconf->loc);
605     /*
606      * Others, like the setting of the `congenital' flag, get ORed in.  The
607      * setting of that particular flag, for instance, is TRUE if it was ever
608      * true anywhere in the upstream configuration.
609      */
610     merged_config->congenital = (pconf->congenital | pconf->local);
611     /*
612      * If we're merging records for two different types of environment (server
613      * and directory), mark the new record appropriately.  Otherwise, inherit
614      * the current value.
615      */
616     merged_config->cmode =
617         (pconf->cmode == nconf->cmode) ? pconf->cmode : CONFIG_MODE_COMBO;
618     /*
619      * Now just record our being called in the trace list.  Include the
620      * locations we were asked to merge.
621      */
622     note = apr_psprintf(p, "x_merge_dir_config(p == %pp, parent_conf == "
623                         "%pp, newloc_conf == %pp)", (void*) p,
624                         (void*) parent_conf, (void*) newloc_conf);
625     trace_startup(p, NULL, merged_config, note);
626     return (void *) merged_config;
627 }
628 
629 /*
630  * This function gets called to create a per-server configuration
631  * record.  It will always be called for the "default" server.
632  *
633  * The return value is a pointer to the created module-specific
634  * structure.
635  */
x_create_server_config(apr_pool_t * p,server_rec * s)636 static void *x_create_server_config(apr_pool_t *p, server_rec *s)
637 {
638 
639     x_cfg *cfg;
640     char *sname = s->server_hostname;
641 
642     /*
643      * As with the x_create_dir_config() reoutine, we allocate and fill
644      * in an empty record.
645      */
646     cfg = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
647     cfg->local = 0;
648     cfg->congenital = 0;
649     cfg->cmode = CONFIG_MODE_SERVER;
650     /*
651      * Note that we were called in the trace list.
652      */
653     sname = (sname != NULL) ? sname : "";
654     cfg->loc = apr_pstrcat(p, "SVR(", sname, ")", NULL);
655     trace_startup(p, s, cfg, "x_create_server_config()");
656     return (void *) cfg;
657 }
658 
659 /*
660  * This function gets called to merge two per-server configuration
661  * records.  This is typically done to cope with things like virtual hosts and
662  * the default server configuration  The routine has the responsibility of
663  * creating a new record and merging the contents of the other two into it
664  * appropriately.  If the module doesn't declare a merge routine, the more
665  * specific existing record is used exclusively.
666  *
667  * The routine MUST NOT modify any of its arguments!
668  *
669  * The return value is a pointer to the created module-specific structure
670  * containing the merged values.
671  */
x_merge_server_config(apr_pool_t * p,void * server1_conf,void * server2_conf)672 static void *x_merge_server_config(apr_pool_t *p, void *server1_conf,
673                                          void *server2_conf)
674 {
675 
676     x_cfg *merged_config = (x_cfg *) apr_pcalloc(p, sizeof(x_cfg));
677     x_cfg *s1conf = (x_cfg *) server1_conf;
678     x_cfg *s2conf = (x_cfg *) server2_conf;
679     char *note;
680 
681     /*
682      * Our inheritance rules are our own, and part of our module's semantics.
683      * Basically, just note whence we came.
684      */
685     merged_config->cmode =
686         (s1conf->cmode == s2conf->cmode) ? s1conf->cmode : CONFIG_MODE_COMBO;
687     merged_config->local = s2conf->local;
688     merged_config->congenital = (s1conf->congenital | s1conf->local);
689     merged_config->loc = apr_pstrdup(p, s2conf->loc);
690     /*
691      * Trace our call, including what we were asked to merge.
692      */
693     note = apr_pstrcat(p, "x_merge_server_config(\"", s1conf->loc, "\",\"",
694                    s2conf->loc, "\")", NULL);
695     trace_startup(p, NULL, merged_config, note);
696     return (void *) merged_config;
697 }
698 
699 
700 /*--------------------------------------------------------------------------*
701  *                                                                          *
702  * Now let's declare routines for each of the callback hooks in order.      *
703  * (That's the order in which they're listed in the callback list, *not     *
704  * the order in which the server calls them!  See the command_rec           *
705  * declaration near the bottom of this file.)  Note that these may be       *
706  * called for situations that don't relate primarily to our function - in   *
707  * other words, the fixup handler shouldn't assume that the request has     *
708  * to do with "example_hooks" stuff.                                        *
709  *                                                                          *
710  * With the exception of the content handler, all of our routines will be   *
711  * called for each request, unless an earlier handler from another module   *
712  * aborted the sequence.                                                    *
713  *                                                                          *
714  * There are three types of hooks (see include/ap_config.h):                *
715  *                                                                          *
716  * VOID      : No return code, run all handlers declared by any module      *
717  * RUN_FIRST : Run all handlers until one returns something other           *
718  *             than DECLINED. Hook runner result is result of last callback *
719  * RUN_ALL   : Run all handlers until one returns something other than OK   *
720  *             or DECLINED. The hook runner returns that other value. If    *
721  *             all hooks run, the hook runner returns OK.                   *
722  *                                                                          *
723  * Handlers that are declared as "int" can return the following:            *
724  *                                                                          *
725  *  OK          Handler accepted the request and did its thing with it.     *
726  *  DECLINED    Handler took no action.                                     *
727  *  HTTP_mumble Handler looked at request and found it wanting.             *
728  *                                                                          *
729  * See include/httpd.h for a list of HTTP_mumble status codes.  Handlers    *
730  * that are not declared as int return a valid pointer, or NULL if they     *
731  * DECLINE to handle their phase for that specific request.  Exceptions, if *
732  * any, are noted with each routine.                                        *
733  *--------------------------------------------------------------------------*/
734 
735 /*
736  * This routine is called before the server processes the configuration
737  * files.  There is no return value.
738  */
x_pre_config(apr_pool_t * pconf,apr_pool_t * plog,apr_pool_t * ptemp)739 static int x_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
740                         apr_pool_t *ptemp)
741 {
742     /*
743      * Log the call and exit.
744      */
745     trace_startup(pconf, NULL, NULL, "x_pre_config()");
746     return OK;
747 }
748 
749 /*
750  * This routine is called after the server processes the configuration
751  * files.  At this point the module may review and adjust its configuration
752  * settings in relation to one another and report any problems.  On restart,
753  * this routine will be called twice, once in the startup process (which
754  * exits shortly after this phase) and once in the running server process.
755  *
756  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, the
757  * server will still call any remaining modules with an handler for this
758  * phase.
759  */
x_check_config(apr_pool_t * pconf,apr_pool_t * plog,apr_pool_t * ptemp,server_rec * s)760 static int x_check_config(apr_pool_t *pconf, apr_pool_t *plog,
761                           apr_pool_t *ptemp, server_rec *s)
762 {
763     /*
764      * Log the call and exit.
765      */
766     trace_startup(pconf, s, NULL, "x_check_config()");
767     return OK;
768 }
769 
770 /*
771  * This routine is called when the -t command-line option is supplied.
772  * It executes only once, in the startup process, after the check_config
773  * phase and just before the process exits.  At this point the module
774  * may output any information useful in configuration testing.
775  *
776  * This is a VOID hook: all defined handlers get called.
777  */
x_test_config(apr_pool_t * pconf,server_rec * s)778 static void x_test_config(apr_pool_t *pconf, server_rec *s)
779 {
780     apr_file_t *out = NULL;
781 
782     apr_file_open_stderr(&out, pconf);
783 
784     apr_file_printf(out, "Example module configuration test routine\n");
785 
786     trace_startup(pconf, s, NULL, "x_test_config()");
787 }
788 
789 /*
790  * This routine is called to perform any module-specific log file
791  * openings. It is invoked just before the post_config phase
792  *
793  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, the
794  * server will still call any remaining modules with an handler for this
795  * phase.
796  */
x_open_logs(apr_pool_t * pconf,apr_pool_t * plog,apr_pool_t * ptemp,server_rec * s)797 static int x_open_logs(apr_pool_t *pconf, apr_pool_t *plog,
798                         apr_pool_t *ptemp, server_rec *s)
799 {
800     /*
801      * Log the call and exit.
802      */
803     trace_startup(pconf, s, NULL, "x_open_logs()");
804     return OK;
805 }
806 
807 /*
808  * This routine is called after the server finishes the configuration
809  * process.  At this point the module may review and adjust its configuration
810  * settings in relation to one another and report any problems.  On restart,
811  * this routine will be called only once, in the running server process.
812  *
813  * The return value is OK, DECLINED, or HTTP_mumble.  If we return OK, the
814  * server will still call any remaining modules with an handler for this
815  * phase.
816  */
x_post_config(apr_pool_t * pconf,apr_pool_t * plog,apr_pool_t * ptemp,server_rec * s)817 static int x_post_config(apr_pool_t *pconf, apr_pool_t *plog,
818                           apr_pool_t *ptemp, server_rec *s)
819 {
820     /*
821      * Log the call and exit.
822      */
823     trace_startup(pconf, s, NULL, "x_post_config()");
824     return OK;
825 }
826 
827 /*
828  * All our process-death routine does is add its trace to the log.
829  */
x_child_exit(void * data)830 static apr_status_t x_child_exit(void *data)
831 {
832     char *note;
833     server_rec *s = data;
834     char *sname = s->server_hostname;
835 
836     /*
837      * The arbitrary text we add to our trace entry indicates for which server
838      * we're being called.
839      */
840     sname = (sname != NULL) ? sname : "";
841     note = apr_pstrcat(s->process->pool, "x_child_exit(", sname, ")", NULL);
842     trace_startup(s->process->pool, s, NULL, note);
843     return APR_SUCCESS;
844 }
845 
846 /*
847  * All our process initialiser does is add its trace to the log.
848  *
849  * This is a VOID hook: all defined handlers get called.
850  */
x_child_init(apr_pool_t * p,server_rec * s)851 static void x_child_init(apr_pool_t *p, server_rec *s)
852 {
853     char *note;
854     char *sname = s->server_hostname;
855 
856     /*
857      * The arbitrary text we add to our trace entry indicates for which server
858      * we're being called.
859      */
860     sname = (sname != NULL) ? sname : "";
861     note = apr_pstrcat(p, "x_child_init(", sname, ")", NULL);
862     trace_startup(p, s, NULL, note);
863 
864     apr_pool_cleanup_register(p, s, x_child_exit, x_child_exit);
865 }
866 
867 /*
868  * The hook runner for ap_hook_http_scheme is aliased to ap_http_scheme(),
869  * a routine that the core and other modules call when they need to know
870  * the URL scheme for the request.  For instance, mod_ssl returns "https"
871  * if the server_rec associated with the request has SSL enabled.
872  *
873  * This hook was named 'ap_hook_http_method' in httpd 2.0.
874  *
875  * This is a RUN_FIRST hook: the first handler to return a non NULL
876  * value aborts the handler chain.  The http_core module inserts a
877  * fallback handler (with APR_HOOK_REALLY_LAST preference) that returns
878  * "http".
879  */
x_http_scheme(const request_rec * r)880 static const char *x_http_scheme(const request_rec *r)
881 {
882     /*
883      * Log the call and exit.
884      */
885     trace_request(r, "x_http_scheme()");
886 
887     /* We have no claims to make about the request scheme */
888     return NULL;
889 }
890 
891 /*
892  * The runner for this hook is aliased to ap_default_port(), which the
893  * core and other modules call when they need to know the default port
894  * for a particular server.  This is used for instance to omit the
895  * port number from a Redirect response Location header URL if the port
896  * number is equal to the default port for the service (like 80 for http).
897  *
898  * This is a RUN_FIRST hook: the first handler to return a non-zero
899  * value is the last one executed.  The http_core module inserts a
900  * fallback handler (with APR_HOOK_REALLY_LAST order specifier) that
901  * returns 80.
902  */
x_default_port(const request_rec * r)903 static apr_port_t x_default_port(const request_rec *r)
904 {
905     /*
906      * Log the call and exit.
907      */
908     trace_request(r, "x_default_port()");
909     return 0;
910 }
911 
912 /*
913  * This routine is called just before the handler gets invoked. It allows
914  * a module to insert a previously defined filter into the filter chain.
915  *
916  * No filter has been defined by this module, so we just log the call
917  * and exit.
918  *
919  * This is a VOID hook: all defined handlers get called.
920  */
x_insert_filter(request_rec * r)921 static void x_insert_filter(request_rec *r)
922 {
923     /*
924      * Log the call and exit.
925      */
926     trace_request(r, "x_insert_filter()");
927 }
928 
929 /*
930  * This routine is called to insert a previously defined error filter into
931  * the filter chain as the request is being processed.
932  *
933  * For the purpose of this example, we don't have a filter to insert,
934  * so just add to the trace and exit.
935  *
936  * This is a VOID hook: all defined handlers get called.
937  */
x_insert_error_filter(request_rec * r)938 static void x_insert_error_filter(request_rec *r)
939 {
940     trace_request(r, "x_insert_error_filter()");
941 }
942 
943 /*--------------------------------------------------------------------------*/
944 /*                                                                          */
945 /* Now we declare our content handlers, which are invoked when the server   */
946 /* encounters a document which our module is supposed to have a chance to   */
947 /* see.  (See mod_mime's SetHandler and AddHandler directives, and the      */
948 /* mod_info and mod_status examples, for more details.)                     */
949 /*                                                                          */
950 /* Since content handlers are dumping data directly into the connection     */
951 /* (using the r*() routines, such as rputs() and rprintf()) without         */
952 /* intervention by other parts of the server, they need to make             */
953 /* sure any accumulated HTTP headers are sent first.  This is done by       */
954 /* calling send_http_header().  Otherwise, no header will be sent at all,   */
955 /* and the output sent to the client will actually be HTTP-uncompliant.     */
956 /*--------------------------------------------------------------------------*/
957 /*
958  * Sample content handler.  All this does is display the call list that has
959  * been built up so far.
960  *
961  * This routine gets called for every request, unless another handler earlier
962  * in the callback chain has already handled the request. It is up to us to
963  * test the request_rec->handler field and see whether we are meant to handle
964  * this request.
965  *
966  * The content handler gets to write directly to the client using calls like
967  * ap_rputs() and ap_rprintf()
968  *
969  * This is a RUN_FIRST hook.
970  */
x_handler(request_rec * r)971 static int x_handler(request_rec *r)
972 {
973     x_cfg *dcfg;
974     char *note;
975     void *conn_data;
976     apr_status_t status;
977 
978     dcfg = our_dconfig(r);
979     /*
980      * Add our trace to the log, and whether we get to write
981      * content for this request.
982      */
983     note = apr_pstrcat(r->pool, "x_handler(), handler is \"",
984                       r->handler, "\"", NULL);
985     trace_request(r, note);
986 
987     /* If it's not for us, get out as soon as possible. */
988     if (strcmp(r->handler, "example-hooks-handler")) {
989         return DECLINED;
990     }
991 
992     /*
993      * Set the Content-type header. Note that we do not actually have to send
994      * the headers: this is done by the http core.
995      */
996     ap_set_content_type(r, "text/html");
997     /*
998      * If we're only supposed to send header information (HEAD request), we're
999      * already there.
1000      */
1001     if (r->header_only) {
1002         return OK;
1003     }
1004 
1005     /*
1006      * Now send our actual output.  Since we tagged this as being
1007      * "text/html", we need to embed any HTML.
1008      */
1009     ap_rputs(DOCTYPE_HTML_3_2, r);
1010     ap_rputs("<HTML>\n", r);
1011     ap_rputs(" <HEAD>\n", r);
1012     ap_rputs("  <TITLE>mod_example_hooks Module Content-Handler Output\n", r);
1013     ap_rputs("  </TITLE>\n", r);
1014     ap_rputs(" </HEAD>\n", r);
1015     ap_rputs(" <BODY>\n", r);
1016     ap_rputs("  <H1><SAMP>mod_example_hooks</SAMP> Module Content-Handler Output\n", r);
1017     ap_rputs("  </H1>\n", r);
1018     ap_rputs("  <P>\n", r);
1019     ap_rprintf(r, "  Apache HTTP Server version: \"%s\"\n",
1020             ap_get_server_banner());
1021     ap_rputs("  <BR>\n", r);
1022     ap_rprintf(r, "  Server built: \"%s\"\n", ap_get_server_built());
1023     ap_rputs("  </P>\n", r);
1024     ap_rputs("  <P>\n", r);
1025     ap_rputs("  The format for the callback trace is:\n", r);
1026     ap_rputs("  </P>\n", r);
1027     ap_rputs("  <DL>\n", r);
1028     ap_rputs("   <DT><EM>n</EM>.<SAMP>&lt;routine-name&gt;", r);
1029     ap_rputs("(&lt;routine-data&gt;)</SAMP>\n", r);
1030     ap_rputs("   </DT>\n", r);
1031     ap_rputs("   <DD><SAMP>[&lt;applies-to&gt;]</SAMP>\n", r);
1032     ap_rputs("   </DD>\n", r);
1033     ap_rputs("  </DL>\n", r);
1034     ap_rputs("  <P>\n", r);
1035     ap_rputs("  The <SAMP>&lt;routine-data&gt;</SAMP> is supplied by\n", r);
1036     ap_rputs("  the routine when it requests the trace,\n", r);
1037     ap_rputs("  and the <SAMP>&lt;applies-to&gt;</SAMP> is extracted\n", r);
1038     ap_rputs("  from the configuration record at the time of the trace.\n", r);
1039     ap_rputs("  <STRONG>SVR()</STRONG> indicates a server environment\n", r);
1040     ap_rputs("  (blank means the main or default server, otherwise it's\n", r);
1041     ap_rputs("  the name of the VirtualHost); <STRONG>DIR()</STRONG>\n", r);
1042     ap_rputs("  indicates a location in the URL or filesystem\n", r);
1043     ap_rputs("  namespace.\n", r);
1044     ap_rputs("  </P>\n", r);
1045     ap_rprintf(r, "  <H2>Startup callbacks so far:</H2>\n  <OL>\n%s  </OL>\n",
1046             trace);
1047     ap_rputs("  <H2>Connection-specific callbacks so far:</H2>\n", r);
1048 
1049     status =  apr_pool_userdata_get(&conn_data, CONN_NOTE,
1050                                     r->connection->pool);
1051     if ((status == APR_SUCCESS) && conn_data) {
1052         ap_rprintf(r, "  <OL>\n%s  </OL>\n", (char *) conn_data);
1053     }
1054     else {
1055         ap_rputs("  <P>No connection-specific callback information was "
1056                  "retrieved.</P>\n", r);
1057     }
1058 
1059     ap_rputs("  <H2>Request-specific callbacks so far:</H2>\n", r);
1060     ap_rprintf(r, "  <OL>\n%s  </OL>\n", apr_table_get(r->notes, TRACE_NOTE));
1061     ap_rputs("  <H2>Environment for <EM>this</EM> call:</H2>\n", r);
1062     ap_rputs("  <UL>\n", r);
1063     ap_rprintf(r, "   <LI>Applies-to: <SAMP>%s</SAMP>\n   </LI>\n", dcfg->loc);
1064     ap_rprintf(r, "   <LI>\"Example\" directive declared here: %s\n   </LI>\n",
1065             (dcfg->local ? "YES" : "NO"));
1066     ap_rprintf(r, "   <LI>\"Example\" inherited: %s\n   </LI>\n",
1067             (dcfg->congenital ? "YES" : "NO"));
1068     ap_rputs("  </UL>\n", r);
1069     ap_rputs(" </BODY>\n", r);
1070     ap_rputs("</HTML>\n", r);
1071     /*
1072      * We're all done, so cancel the timeout we set.  Since this is probably
1073      * the end of the request we *could* assume this would be done during
1074      * post-processing - but it's possible that another handler might be
1075      * called and inherit our outstanding timer.  Not good; to each its own.
1076      */
1077     /*
1078      * We did what we wanted to do, so tell the rest of the server we
1079      * succeeded.
1080      */
1081     return OK;
1082 }
1083 
1084 /*
1085  * The quick_handler hook presents modules with a very powerful opportunity to
1086  * serve their content in a very early request phase.  Note that this handler
1087  * can not serve any requests from the file system because hooks like
1088  * map_to_storage have not run.  The quick_handler hook also runs before any
1089  * authentication and access control.
1090  *
1091  * This hook is used by mod_cache to serve cached content.
1092  *
1093  * This is a RUN_FIRST hook. Return OK if you have served the request,
1094  * DECLINED if you want processing to continue, or a HTTP_* error code to stop
1095  * processing the request.
1096  */
x_quick_handler(request_rec * r,int lookup_uri)1097 static int x_quick_handler(request_rec *r, int lookup_uri)
1098 {
1099     /*
1100      * Log the call and exit.
1101      */
1102     trace_request(r, "x_quick_handler()");
1103     return DECLINED;
1104 }
1105 
1106 /*
1107  * This routine is called just after the server accepts the connection,
1108  * but before it is handed off to a protocol module to be served.  The point
1109  * of this hook is to allow modules an opportunity to modify the connection
1110  * as soon as possible. The core server uses this phase to setup the
1111  * connection record based on the type of connection that is being used.
1112  *
1113  * This is a RUN_ALL hook.
1114  */
x_pre_connection(conn_rec * c,void * csd)1115 static int x_pre_connection(conn_rec *c, void *csd)
1116 {
1117     char *note;
1118 
1119     /*
1120      * Log the call and exit.
1121      */
1122     note = apr_psprintf(c->pool, "x_pre_connection(c = %pp, p = %pp)",
1123                         (void*) c, (void*) c->pool);
1124     trace_connection(c, note);
1125 
1126     return OK;
1127 }
1128 
1129 /* This routine is used to actually process the connection that was received.
1130  * Only protocol modules should implement this hook, as it gives them an
1131  * opportunity to replace the standard HTTP processing with processing for
1132  * some other protocol.  Both echo and POP3 modules are available as
1133  * examples.
1134  *
1135  * This is a RUN_FIRST hook.
1136  */
x_process_connection(conn_rec * c)1137 static int x_process_connection(conn_rec *c)
1138 {
1139     trace_connection(c, "x_process_connection()");
1140     return DECLINED;
1141 }
1142 
1143 /*
1144  * This routine is called after the request has been read but before any other
1145  * phases have been processed.  This allows us to make decisions based upon
1146  * the input header fields.
1147  *
1148  * This is a HOOK_VOID hook.
1149  */
x_pre_read_request(request_rec * r,conn_rec * c)1150 static void x_pre_read_request(request_rec *r, conn_rec *c)
1151 {
1152     /*
1153      * We don't actually *do* anything here, except note the fact that we were
1154      * called.
1155      */
1156     trace_request(r, "x_pre_read_request()");
1157 }
1158 
1159 /*
1160  * This routine is called after the request has been read but before any other
1161  * phases have been processed.  This allows us to make decisions based upon
1162  * the input header fields.
1163  *
1164  * This is a RUN_ALL hook.
1165  */
x_post_read_request(request_rec * r)1166 static int x_post_read_request(request_rec *r)
1167 {
1168     /*
1169      * We don't actually *do* anything here, except note the fact that we were
1170      * called.
1171      */
1172     trace_request(r, "x_post_read_request()");
1173     return DECLINED;
1174 }
1175 
1176 /*
1177  * This routine gives our module an opportunity to translate the URI into an
1178  * actual filename, before URL decoding happens.
1179  *
1180  * This is a RUN_FIRST hook.
1181  */
x_pre_translate_name(request_rec * r)1182 static int x_pre_translate_name(request_rec *r)
1183 {
1184     /*
1185      * We don't actually *do* anything here, except note the fact that we were
1186      * called.
1187      */
1188     trace_request(r, "x_pre_translate_name()");
1189     return DECLINED;
1190 }
1191 
1192 /*
1193  * This routine gives our module an opportunity to translate the URI into an
1194  * actual filename.  If we don't do anything special, the server's default
1195  * rules (Alias directives and the like) will continue to be followed.
1196  *
1197  * This is a RUN_FIRST hook.
1198  */
x_translate_name(request_rec * r)1199 static int x_translate_name(request_rec *r)
1200 {
1201     /*
1202      * We don't actually *do* anything here, except note the fact that we were
1203      * called.
1204      */
1205     trace_request(r, "x_translate_name()");
1206     return DECLINED;
1207 }
1208 
1209 /*
1210  * This routine maps r->filename to a physical file on disk.  Useful for
1211  * overriding default core behavior, including skipping mapping for
1212  * requests that are not file based.
1213  *
1214  * This is a RUN_FIRST hook.
1215  */
x_map_to_storage(request_rec * r)1216 static int x_map_to_storage(request_rec *r)
1217 {
1218     /*
1219      * We don't actually *do* anything here, except note the fact that we were
1220      * called.
1221      */
1222     trace_request(r, "x_map_to_storage()");
1223     return DECLINED;
1224 }
1225 
1226 /*
1227  * this routine gives our module another chance to examine the request
1228  * headers and to take special action. This is the first phase whose
1229  * hooks' configuration directives can appear inside the <Directory>
1230  * and similar sections, because at this stage the URI has been mapped
1231  * to the filename. For example this phase can be used to block evil
1232  * clients, while little resources were wasted on these.
1233  *
1234  * This is a RUN_ALL hook.
1235  */
x_header_parser(request_rec * r)1236 static int x_header_parser(request_rec *r)
1237 {
1238     /*
1239      * We don't actually *do* anything here, except note the fact that we were
1240      * called.
1241      */
1242     trace_request(r, "x_header_parser()");
1243     return DECLINED;
1244 }
1245 
1246 
1247 /*
1248  * This routine is called to check for any module-specific restrictions placed
1249  * upon the requested resource.  (See the mod_access_compat module for an
1250  * example.)
1251  *
1252  * This is a RUN_ALL hook. The first handler to return a status other than OK
1253  * or DECLINED (for instance, HTTP_FORBIDDEN) aborts the callback chain.
1254  */
x_check_access(request_rec * r)1255 static int x_check_access(request_rec *r)
1256 {
1257     trace_request(r, "x_check_access()");
1258     return DECLINED;
1259 }
1260 
1261 /*
1262  * This routine is called to check the authentication information sent with
1263  * the request (such as looking up the user in a database and verifying that
1264  * the [encrypted] password sent matches the one in the database).
1265  *
1266  * This is a RUN_FIRST hook. The return value is OK, DECLINED, or some
1267  * HTTP_mumble error (typically HTTP_UNAUTHORIZED).
1268  */
x_check_authn(request_rec * r)1269 static int x_check_authn(request_rec *r)
1270 {
1271     /*
1272      * Don't do anything except log the call.
1273      */
1274     trace_request(r, "x_check_authn()");
1275     return DECLINED;
1276 }
1277 
1278 /*
1279  * This routine is called to check to see if the resource being requested
1280  * requires authorisation.
1281  *
1282  * This is a RUN_FIRST hook. The return value is OK, DECLINED, or
1283  * HTTP_mumble.  If we return OK, no other modules are called during this
1284  * phase.
1285  *
1286  * If *all* modules return DECLINED, the request is aborted with a server
1287  * error.
1288  */
x_check_authz(request_rec * r)1289 static int x_check_authz(request_rec *r)
1290 {
1291     /*
1292      * Log the call and return OK, or access will be denied (even though we
1293      * didn't actually do anything).
1294      */
1295     trace_request(r, "x_check_authz()");
1296     return DECLINED;
1297 }
1298 
1299 /*
1300  * This routine is called to determine and/or set the various document type
1301  * information bits, like Content-type (via r->content_type), language, et
1302  * cetera.
1303  *
1304  * This is a RUN_FIRST hook.
1305  */
x_type_checker(request_rec * r)1306 static int x_type_checker(request_rec *r)
1307 {
1308     /*
1309      * Log the call, but don't do anything else - and report truthfully that
1310      * we didn't do anything.
1311      */
1312     trace_request(r, "x_type_checker()");
1313     return DECLINED;
1314 }
1315 
1316 /*
1317  * This routine is called to perform any module-specific fixing of header
1318  * fields, et cetera.  It is invoked just before any content-handler.
1319  *
1320  * This is a RUN_ALL HOOK.
1321  */
x_fixups(request_rec * r)1322 static int x_fixups(request_rec *r)
1323 {
1324     /*
1325      * Log the call and exit.
1326      */
1327     trace_request(r, "x_fixups()");
1328     return DECLINED;
1329 }
1330 
1331 /*
1332  * This routine is called to perform any module-specific logging activities
1333  * over and above the normal server things.
1334  *
1335  * This is a RUN_ALL hook.
1336  */
x_log_transaction(request_rec * r)1337 static int x_log_transaction(request_rec *r)
1338 {
1339     trace_request(r, "x_log_transaction()");
1340     return DECLINED;
1341 }
1342 
1343 #ifdef HAVE_UNIX_SUEXEC
1344 
1345 /*
1346  * This routine is called to find out under which user id to run suexec
1347  * Unless our module runs CGI programs, there is no reason for us to
1348  * mess with this information.
1349  *
1350  * This is a RUN_FIRST hook. The return value is a pointer to an
1351  * ap_unix_identity_t or NULL.
1352  */
x_get_suexec_identity(const request_rec * r)1353 static ap_unix_identity_t *x_get_suexec_identity(const request_rec *r)
1354 {
1355     trace_request(r, "x_get_suexec_identity()");
1356     return NULL;
1357 }
1358 #endif
1359 
1360 /*
1361  * This routine is called to create a connection. This hook is implemented
1362  * by the Apache core: there is no known reason a module should override
1363  * it.
1364  *
1365  * This is a RUN_FIRST hook.
1366  *
1367  * Return NULL to decline, a valid conn_rec pointer to accept.
1368  */
x_create_connection(apr_pool_t * p,server_rec * server,apr_socket_t * csd,long conn_id,void * sbh,apr_bucket_alloc_t * alloc)1369 static conn_rec *x_create_connection(apr_pool_t *p, server_rec *server,
1370                                      apr_socket_t *csd, long conn_id,
1371                                      void *sbh, apr_bucket_alloc_t *alloc)
1372 {
1373     trace_nocontext(p, __FILE__, __LINE__, "x_create_connection()");
1374     return NULL;
1375 }
1376 
1377 /*
1378  * This hook is defined in server/core.c, but it is not actually called
1379  * or documented.
1380  *
1381  * This is a RUN_ALL hook.
1382  */
x_get_mgmt_items(apr_pool_t * p,const char * val,apr_hash_t * ht)1383 static int x_get_mgmt_items(apr_pool_t *p, const char *val, apr_hash_t *ht)
1384 {
1385     /* We have nothing to do here but trace the call, and no context
1386      * in which to trace it.
1387      */
1388     trace_nocontext(p, __FILE__, __LINE__, "x_check_config()");
1389     return DECLINED;
1390 }
1391 
1392 /*
1393  * This routine gets called shortly after the request_rec structure
1394  * is created. It provides the opportunity to manipulae the request
1395  * at a very early stage.
1396  *
1397  * This is a RUN_ALL hook.
1398  */
x_create_request(request_rec * r)1399 static int x_create_request(request_rec *r)
1400 {
1401     /*
1402      * We have a request_rec, but it is not filled in enough to give
1403      * us a usable configuration. So, add a trace without context.
1404      */
1405     trace_nocontext( r->pool, __FILE__, __LINE__, "x_create_request()");
1406     return DECLINED;
1407 }
1408 
1409 /*
1410  * This routine gets called during the startup of the MPM.
1411  * No known existing module implements this hook.
1412  *
1413  * This is a RUN_ALL hook.
1414  */
x_pre_mpm(apr_pool_t * p,ap_scoreboard_e sb_type)1415 static int x_pre_mpm(apr_pool_t *p, ap_scoreboard_e sb_type)
1416 {
1417     trace_nocontext(p, __FILE__, __LINE__, "x_pre_mpm()");
1418     return DECLINED;
1419 }
1420 
1421 /*
1422  * This hook gets run periodically by a maintenance function inside
1423  * the MPM. Its exact purpose is unknown and undocumented at this time.
1424  *
1425  * This is a RUN_ALL hook.
1426  */
x_monitor(apr_pool_t * p,server_rec * s)1427 static int x_monitor(apr_pool_t *p, server_rec *s)
1428 {
1429     trace_nocontext(p, __FILE__, __LINE__, "x_monitor()");
1430     return DECLINED;
1431 }
1432 
1433 /*--------------------------------------------------------------------------*/
1434 /*                                                                          */
1435 /* Which functions are responsible for which hooks in the server.           */
1436 /*                                                                          */
1437 /*--------------------------------------------------------------------------*/
1438 /*
1439  * Each function our module provides to handle a particular hook is
1440  * specified here.  The functions are registered using
1441  * ap_hook_foo(name, predecessors, successors, position)
1442  * where foo is the name of the hook.
1443  *
1444  * The args are as follows:
1445  * name         -> the name of the function to call.
1446  * predecessors -> a list of modules whose calls to this hook must be
1447  *                 invoked before this module.
1448  * successors   -> a list of modules whose calls to this hook must be
1449  *                 invoked after this module.
1450  * position     -> The relative position of this module.  One of
1451  *                 APR_HOOK_FIRST, APR_HOOK_MIDDLE, or APR_HOOK_LAST.
1452  *                 Most modules will use APR_HOOK_MIDDLE.  If multiple
1453  *                 modules use the same relative position, Apache will
1454  *                 determine which to call first.
1455  *                 If your module relies on another module to run first,
1456  *                 or another module running after yours, use the
1457  *                 predecessors and/or successors.
1458  *
1459  * The number in brackets indicates the order in which the routine is called
1460  * during request processing.  Note that not all routines are necessarily
1461  * called (such as if a resource doesn't have access restrictions).
1462  * The actual delivery of content to the browser [9] is not handled by
1463  * a hook; see the handler declarations below.
1464  */
x_register_hooks(apr_pool_t * p)1465 static void x_register_hooks(apr_pool_t *p)
1466 {
1467     trace = NULL;
1468     ap_hook_pre_config(x_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1469     ap_hook_check_config(x_check_config, NULL, NULL, APR_HOOK_MIDDLE);
1470     ap_hook_test_config(x_test_config, NULL, NULL, APR_HOOK_MIDDLE);
1471     ap_hook_open_logs(x_open_logs, NULL, NULL, APR_HOOK_MIDDLE);
1472     ap_hook_post_config(x_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1473     ap_hook_child_init(x_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1474     ap_hook_handler(x_handler, NULL, NULL, APR_HOOK_MIDDLE);
1475     ap_hook_quick_handler(x_quick_handler, NULL, NULL, APR_HOOK_MIDDLE);
1476     ap_hook_pre_connection(x_pre_connection, NULL, NULL, APR_HOOK_MIDDLE);
1477     ap_hook_process_connection(x_process_connection, NULL, NULL, APR_HOOK_MIDDLE);
1478     ap_hook_pre_read_request(x_pre_read_request, NULL, NULL,
1479                               APR_HOOK_MIDDLE);
1480     /* [1] post read_request handling */
1481     ap_hook_post_read_request(x_post_read_request, NULL, NULL,
1482                               APR_HOOK_MIDDLE);
1483     ap_hook_log_transaction(x_log_transaction, NULL, NULL, APR_HOOK_MIDDLE);
1484     ap_hook_http_scheme(x_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
1485     ap_hook_default_port(x_default_port, NULL, NULL, APR_HOOK_MIDDLE);
1486     ap_hook_pre_translate_name(x_pre_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1487     ap_hook_translate_name(x_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1488     ap_hook_map_to_storage(x_map_to_storage, NULL,NULL, APR_HOOK_MIDDLE);
1489     ap_hook_header_parser(x_header_parser, NULL, NULL, APR_HOOK_MIDDLE);
1490     ap_hook_fixups(x_fixups, NULL, NULL, APR_HOOK_MIDDLE);
1491     ap_hook_type_checker(x_type_checker, NULL, NULL, APR_HOOK_MIDDLE);
1492     ap_hook_check_access(x_check_access, NULL, NULL, APR_HOOK_MIDDLE,
1493                          AP_AUTH_INTERNAL_PER_CONF);
1494     ap_hook_check_authn(x_check_authn, NULL, NULL, APR_HOOK_MIDDLE,
1495                         AP_AUTH_INTERNAL_PER_CONF);
1496     ap_hook_check_authz(x_check_authz, NULL, NULL, APR_HOOK_MIDDLE,
1497                         AP_AUTH_INTERNAL_PER_CONF);
1498     ap_hook_insert_filter(x_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
1499     ap_hook_insert_error_filter(x_insert_error_filter, NULL, NULL, APR_HOOK_MIDDLE);
1500 #ifdef HAVE_UNIX_SUEXEC
1501     ap_hook_get_suexec_identity(x_get_suexec_identity, NULL, NULL, APR_HOOK_MIDDLE);
1502 #endif
1503     ap_hook_create_connection(x_create_connection, NULL, NULL, APR_HOOK_MIDDLE);
1504     ap_hook_get_mgmt_items(x_get_mgmt_items, NULL, NULL, APR_HOOK_MIDDLE);
1505     ap_hook_create_request(x_create_request, NULL, NULL, APR_HOOK_MIDDLE);
1506     ap_hook_pre_mpm(x_pre_mpm, NULL, NULL, APR_HOOK_MIDDLE);
1507     ap_hook_monitor(x_monitor, NULL, NULL, APR_HOOK_MIDDLE);
1508 }
1509 
1510 /*--------------------------------------------------------------------------*/
1511 /*                                                                          */
1512 /* All of the routines have been declared now.  Here's the list of          */
1513 /* directives specific to our module, and information about where they      */
1514 /* may appear and how the command parser should pass them to us for         */
1515 /* processing.  Note that care must be taken to ensure that there are NO    */
1516 /* collisions of directive names between modules.                           */
1517 /*                                                                          */
1518 /*--------------------------------------------------------------------------*/
1519 /*
1520  * List of directives specific to our module.
1521  */
1522 static const command_rec x_cmds[] =
1523 {
1524     AP_INIT_NO_ARGS(
1525         "Example",                          /* directive name */
1526         cmd_example,                        /* config action routine */
1527         NULL,                               /* argument to include in call */
1528         OR_OPTIONS,                         /* where available */
1529         "Example directive - no arguments"  /* directive description */
1530     ),
1531     {NULL}
1532 };
1533 /*--------------------------------------------------------------------------*/
1534 /*                                                                          */
1535 /* Finally, the list of callback routines and data structures that provide  */
1536 /* the static hooks into our module from the other parts of the server.     */
1537 /*                                                                          */
1538 /*--------------------------------------------------------------------------*/
1539 /*
1540  * Module definition for configuration.  If a particular callback is not
1541  * needed, replace its routine name below with the word NULL.
1542  */
1543 AP_DECLARE_MODULE(example_hooks) =
1544 {
1545     STANDARD20_MODULE_STUFF,
1546     x_create_dir_config,    /* per-directory config creator */
1547     x_merge_dir_config,     /* dir config merger */
1548     x_create_server_config, /* server config creator */
1549     x_merge_server_config,  /* server config merger */
1550     x_cmds,                 /* command table */
1551     x_register_hooks,       /* set up other request processing hooks */
1552 };
1553