]> granicus.if.org Git - apache/blobdiff - modules/lua/mod_lua.c
Fix spelling in comments and text files.
[apache] / modules / lua / mod_lua.c
index 59ce62c497dfe8dd585666aaffa2232826f8a892..0f0c2596e2b41f4148e338b165b142eced0c177f 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
-
+#include <apr_thread_mutex.h>
+#include <apr_pools.h>
 #include "lua_apr.h"
 #include "lua_config.h"
 #include "apr_optional.h"
 #include "mod_ssl.h"
 #include "mod_auth.h"
+#include "util_mutex.h"
+
 
 #ifdef APR_HAS_THREADS
 #include "apr_thread_proc.h"
 #endif
 
+/* getpid for *NIX */
+#if APR_HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#if APR_HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+/* getpid for Windows */
+#if APR_HAVE_PROCESS_H
+#include <process.h>
+#endif
+
 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ap_lua, AP_LUA, int, lua_open,
                                     (lua_State *L, apr_pool_t *p),
                                     (L, p), OK, DECLINED)
@@ -50,9 +66,13 @@ typedef struct {
     const char *file_name;
     const char *function_name;
     ap_lua_vm_spec *spec;
-    apr_array_header_t *args;
 } lua_authz_provider_spec;
 
+typedef struct {
+    lua_authz_provider_spec *spec;
+    apr_array_header_t *args;
+} lua_authz_provider_func;
+
 apr_hash_t *lua_authz_providers;
 
 typedef struct
@@ -63,6 +83,18 @@ typedef struct
     int broken;
 } lua_filter_ctx;
 
+#define DEFAULT_LUA_SHMFILE "lua_ivm_shm"
+
+apr_global_mutex_t *lua_ivm_mutex;
+apr_shm_t *lua_ivm_shm;
+char *lua_ivm_shmfile;
+
+static apr_status_t shm_cleanup_wrapper(void *unused) {
+    if (lua_ivm_shm) {
+        return apr_shm_destroy(lua_ivm_shm);
+    }
+    return OK;
+}
 
 /**
  * error reporting if lua has an error.
@@ -73,11 +105,11 @@ static void report_lua_error(lua_State *L, request_rec *r)
     const char *lua_response;
     r->status = HTTP_INTERNAL_SERVER_ERROR;
     r->content_type = "text/html";
-    ap_rputs("<b>Error!</b>\n", r);
-    ap_rputs("<p>", r);
+    ap_rputs("<h3>Error!</h3>\n", r);
+    ap_rputs("<pre>", r);
     lua_response = lua_tostring(L, -1);
-    ap_rputs(lua_response, r);
-    ap_rputs("</p>\n", r);
+    ap_rputs(ap_escape_html(r->pool, lua_response), r);
+    ap_rputs("</pre>\n", r);
 
     ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, r->pool, APLOGNO(01471) "Lua error: %s",
                   lua_response);
@@ -115,6 +147,7 @@ static const char *scope_to_string(unsigned int scope)
 #endif
     default:
         ap_assert(0);
+        return 0;
     }
 }
 
@@ -230,7 +263,7 @@ static const char* ap_lua_interpolate_string(apr_pool_t* pool, const char* strin
         ret = apr_pstrcat(pool, ret, stringBetween, NULL);
     }
     /* If no replacement was made, just return the original string */
-    else if (y==0) {
+    else if (y == 0) {
         return string;
     }
     return ret;
@@ -279,7 +312,7 @@ static int lua_handler(request_rec *r)
         lua_getglobal(L, "handle");
         if (!lua_isfunction(L, -1)) {
             ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01475)
-                          "lua: Unable to find function %s in %s",
+                          "lua: Unable to find entry function '%s' in %s (not a valid function)",
                           "handle",
                           spec->file);
             ap_lua_release_state(L, spec, r);
@@ -315,7 +348,10 @@ static apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_fil
     ctx = apr_pcalloc(r->pool, sizeof(lua_filter_ctx));
     ctx->broken = 0;
     *c = ctx;
-    /* Find the filter that was called */
+    /* Find the filter that was called.
+     * XXX: If we were wired with mod_filter, the filter (mod_filters name)
+     *      and the provider (our underlying filters name) need to have matched.
+     */
     for (n = 0; n < cfg->mapped_filters->nelts; n++) {
         ap_lua_filter_handler_spec *hook_spec =
             ((ap_lua_filter_handler_spec **) cfg->mapped_filters->elts)[n];
@@ -346,7 +382,7 @@ static apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_fil
                 lua_getglobal(L, hook_spec->function_name);
                 if (!lua_isfunction(L, -1)) {
                     ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02329)
-                                    "lua: Unable to find function %s in %s",
+                                "lua: Unable to find entry function '%s' in %s (not a valid function)",
                                     hook_spec->function_name,
                                     hook_spec->file_name);
                     ap_lua_release_state(L, spec, r);
@@ -371,6 +407,12 @@ static apr_status_t lua_setup_filter_ctx(ap_filter_t* f, request_rec* r, lua_fil
              */
             rc = lua_resume(L, 1);
             if (rc == LUA_YIELD) {
+                if (f->frec->providers == NULL) { 
+                    /* Not wired by mod_filter */
+                    apr_table_unset(r->headers_out, "Content-Length");
+                    apr_table_unset(r->headers_out, "Content-MD5");
+                    apr_table_unset(r->headers_out, "ETAG");
+                }
                 return OK;
             }
             else {
@@ -404,8 +446,25 @@ static apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade
             ap_remove_output_filter(f);
             return ap_pass_brigade(f->next,pbbIn);
         }
-        f->ctx = ctx;
-        ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
+        else { 
+            /* We've got a willing lua filter, setup and check for a prefix */
+            size_t olen;
+            apr_bucket *pbktOut;
+            const char* output = lua_tolstring(ctx->L, 1, &olen);
+
+            f->ctx = ctx;
+            ctx->tmpBucket = apr_brigade_create(r->pool, c->bucket_alloc);
+
+            if (olen > 0) { 
+                pbktOut = apr_bucket_heap_create(output, olen, NULL, c->bucket_alloc);
+                APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
+                rv = ap_pass_brigade(f->next, ctx->tmpBucket);
+                apr_brigade_cleanup(ctx->tmpBucket);
+                if (rv != APR_SUCCESS) {
+                    return rv;
+                }
+            }
+        }
     }
     ctx = (lua_filter_ctx*) f->ctx;
     L = ctx->L;
@@ -430,13 +489,15 @@ static apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade
             if (lua_resume(L, 0) == LUA_YIELD) {
                 size_t olen;
                 const char* output = lua_tolstring(L, 1, &olen);
-                pbktOut = apr_bucket_heap_create(output, olen, NULL,
-                                        c->bucket_alloc);
-                APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
-                rv = ap_pass_brigade(f->next, ctx->tmpBucket);
-                apr_brigade_cleanup(ctx->tmpBucket);
-                if (rv != APR_SUCCESS) {
-                    return rv;
+                if (olen > 0) { 
+                    pbktOut = apr_bucket_heap_create(output, olen, NULL,
+                                            c->bucket_alloc);
+                    APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
+                    rv = ap_pass_brigade(f->next, ctx->tmpBucket);
+                    apr_brigade_cleanup(ctx->tmpBucket);
+                    if (rv != APR_SUCCESS) {
+                        return rv;
+                    }
                 }
             }
             else {
@@ -445,6 +506,9 @@ static apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade
                 ap_remove_output_filter(f);
                 apr_brigade_cleanup(pbbIn);
                 apr_brigade_cleanup(ctx->tmpBucket);
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02663)
+                              "lua: Error while executing filter: %s",
+                              lua_tostring(L, -1));
                 return HTTP_INTERNAL_SERVER_ERROR;
             }
         }
@@ -458,9 +522,11 @@ static apr_status_t lua_output_filter_handle(ap_filter_t *f, apr_bucket_brigade
                 apr_bucket *pbktOut;
                 size_t olen;
                 const char* output = lua_tolstring(L, 1, &olen);
-                pbktOut = apr_bucket_heap_create(output, olen, NULL,
-                                        c->bucket_alloc);
-                APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
+                if (olen > 0) { 
+                    pbktOut = apr_bucket_heap_create(output, olen, NULL,
+                            c->bucket_alloc);
+                    APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktOut);
+                }
             }
             pbktEOS = apr_bucket_eos_create(c->bucket_alloc);
             APR_BRIGADE_INSERT_TAIL(ctx->tmpBucket, pbktEOS);
@@ -533,16 +599,15 @@ static apr_status_t lua_input_filter_handle(ap_filter_t *f,
             const char *data;
             apr_size_t len;
             
-            if(APR_BUCKET_IS_EOS(pbktIn)) {
+            if (APR_BUCKET_IS_EOS(pbktIn)) {
                 APR_BUCKET_REMOVE(pbktIn);
                 break;
             }
 
             /* read the bucket */
             ret = apr_bucket_read(pbktIn, &data, &len, eBlock);
-            if(ret != APR_SUCCESS) {
+            if (ret != APR_SUCCESS)
                 return ret;
-            }
 
             /* Push the bucket onto the Lua stack as a global var */
             lastCall++;
@@ -624,7 +689,7 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
 
             if (!L) {
                 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01477)
-                              "lua: Failed to obtain lua interpreter for %s %s",
+                    "lua: Failed to obtain lua interpreter for entry function '%s' in %s",
                               hook_spec->function_name, hook_spec->file_name);
                 return HTTP_INTERNAL_SERVER_ERROR;
             }
@@ -633,7 +698,7 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
                 lua_getglobal(L, hook_spec->function_name);
                 if (!lua_isfunction(L, -1)) {
                     ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01478)
-                                  "lua: Unable to find function %s in %s",
+                               "lua: Unable to find entry function '%s' in %s (not a valid function)",
                                   hook_spec->function_name,
                                   hook_spec->file_name);
                     ap_lua_release_state(L, spec, r);
@@ -659,6 +724,14 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
             rc = DECLINED;
             if (lua_isnumber(L, -1)) {
                 rc = lua_tointeger(L, -1);
+                ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, "Lua hook %s:%s for phase %s returned %d", 
+                              hook_spec->file_name, hook_spec->function_name, name, rc);
+            }
+            else { 
+                ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(03017)
+                              "Lua hook %s:%s for phase %s did not return a numeric value",
+                              hook_spec->file_name, hook_spec->function_name, name);
+                return HTTP_INTERNAL_SERVER_ERROR;
             }
             if (rc != DECLINED) {
                 ap_lua_release_state(L, spec, r);
@@ -671,6 +744,32 @@ static int lua_request_rec_hook_harness(request_rec *r, const char *name, int ap
 }
 
 
+/* Fix for making sure that LuaMapHandler works when FallbackResource is set */
+static int lua_map_handler_fixups(request_rec *r)
+{
+    /* If there is no handler set yet, this might be a LuaMapHandler request */
+    if (r->handler == NULL) {
+        int n = 0;
+        ap_regmatch_t match[10];
+        const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
+                                                     &lua_module);
+        for (n = 0; n < cfg->mapped_handlers->nelts; n++) {
+            ap_lua_mapped_handler_spec *hook_spec =
+            ((ap_lua_mapped_handler_spec **) cfg->mapped_handlers->elts)[n];
+
+            if (hook_spec == NULL) {
+                continue;
+            }
+            if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) {
+                r->handler = apr_pstrdup(r->pool, "lua-map-handler");
+                return OK;
+            }
+        }
+    }
+    return DECLINED;
+}
+
+
 static int lua_map_handler(request_rec *r)
 {
     int rc, n = 0;
@@ -693,7 +792,7 @@ static int lua_map_handler(request_rec *r)
         }
         if (!ap_regexec(hook_spec->uri_pattern, r->uri, 10, match, 0)) {
             int i;
-            for (i=0;i<10;i++) {
+            for (i=0 ; i < 10; i++) {
                 if (match[i].rm_eo >= 0) {
                     values[i] = apr_pstrndup(r->pool, r->uri+match[i].rm_so, match[i].rm_eo - match[i].rm_so);
                 }
@@ -711,7 +810,7 @@ static int lua_map_handler(request_rec *r)
 
             if (!L) {
                 ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02330)
-                                "lua: Failed to obtain lua interpreter for %s %s",
+                                "lua: Failed to obtain Lua interpreter for entry function '%s' in %s",
                                 function_name, filename);
                 ap_lua_release_state(L, spec, r);
                 return HTTP_INTERNAL_SERVER_ERROR;
@@ -721,7 +820,7 @@ static int lua_map_handler(request_rec *r)
                 lua_getglobal(L, function_name);
                 if (!lua_isfunction(L, -1)) {
                     ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02331)
-                                    "lua: Unable to find function %s in %s",
+                                    "lua: Unable to find entry function '%s' in %s (not a valid function)",
                                     function_name,
                                     filename);
                     ap_lua_release_state(L, spec, r);
@@ -748,11 +847,17 @@ static int lua_map_handler(request_rec *r)
             if (lua_isnumber(L, -1)) {
                 rc = lua_tointeger(L, -1);
             }
+            else { 
+                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02483)
+                              "lua: Lua handler %s in %s did not return a value, assuming apache2.OK",
+                              function_name,
+                              filename);
+                rc = OK;
+            }
+            ap_lua_release_state(L, spec, r);
             if (rc != DECLINED) {
-                ap_lua_release_state(L, spec, r);
                 return rc;
             }
-            ap_lua_release_state(L, spec, r);
         }
     }
     return DECLINED;
@@ -802,7 +907,7 @@ typedef struct cr_ctx
 } cr_ctx;
 
 
-/* Okay, this deserves a little explaination -- in order for the errors that lua
+/* Okay, this deserves a little explanation -- in order for the errors that lua
  * generates to be 'accuarate', including line numbers, we basically inject
  * N line number new lines into the 'top' of the chunk reader.....
  *
@@ -909,11 +1014,11 @@ static const char *register_named_block_function_hook(const char *name,
     if (line[0]) { 
         const char *word;
         word = ap_getword_conf(cmd->temp_pool, &line);
-        if (word && *word) {
+        if (*word) {
             function = apr_pstrdup(cmd->pool, word);
         }
         word = ap_getword_conf(cmd->temp_pool, &line);
-        if (word && *word) {
+        if (*word) {
             if (!strcasecmp("early", word)) { 
                 when = AP_LUA_HOOK_FIRST;
             }
@@ -970,7 +1075,11 @@ static const char *register_named_block_function_hook(const char *name,
         else {
             luaL_Buffer b;
             luaL_buffinit(lvm, &b);
+#if LUA_VERSION_NUM >= 503
+            lua_dump(lvm, ldump_writer, &b, 0);
+#else
             lua_dump(lvm, ldump_writer, &b);
+#endif
             luaL_pushresult(&b);
             spec->bytecode_len = lua_strlen(lvm, -1);
             spec->bytecode = apr_pstrmemdup(cmd->pool, lua_tostring(lvm, -1),
@@ -1068,7 +1177,8 @@ static const char *register_filter_function_hook(const char *filter,
     /* TODO: Make it work on other types than just AP_FTYPE_RESOURCE? */
     if (direction == AP_LUA_FILTER_OUTPUT) {
         spec->direction = AP_LUA_FILTER_OUTPUT;
-        ap_register_output_filter(filter, lua_output_filter_handle, NULL, AP_FTYPE_RESOURCE);
+        ap_register_output_filter_protocol(filter, lua_output_filter_handle, NULL, AP_FTYPE_RESOURCE,
+                                            AP_FILTER_PROTO_CHANGE|AP_FILTER_PROTO_CHANGE_LENGTH);
     }
     else {
         spec->direction = AP_LUA_FILTER_INPUT;
@@ -1076,18 +1186,22 @@ static const char *register_filter_function_hook(const char *filter,
     }
     return NULL;
 }
+/* disabled (see reference below)
 static int lua_check_user_id_harness_first(request_rec *r)
 {
     return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_FIRST);
 }
+*/
 static int lua_check_user_id_harness(request_rec *r)
 {
     return lua_request_rec_hook_harness(r, "check_user_id", APR_HOOK_MIDDLE);
 }
+/* disabled (see reference below)
 static int lua_check_user_id_harness_last(request_rec *r)
 {
     return lua_request_rec_hook_harness(r, "check_user_id", AP_LUA_HOOK_LAST);
 }
+*/
 
 static int lua_translate_name_harness_first(request_rec *r)
 {
@@ -1144,7 +1258,13 @@ static int lua_auth_checker_harness_last(request_rec *r)
 }
 static void lua_insert_filter_harness(request_rec *r)
 {
-    /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
+    /* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(03223)
+     *               "LuaHookInsertFilter not yet implemented"); */
+}
+
+static int lua_log_transaction_harness(request_rec *r)
+{
+    return lua_request_rec_hook_harness(r, "log_transaction", APR_HOOK_FIRST);
 }
 
 static int lua_quick_harness(request_rec *r, int lookup)
@@ -1211,6 +1331,15 @@ static const char *register_map_to_storage_hook(cmd_parms *cmd, void *_cfg,
     return register_named_file_function_hook("map_to_storage", cmd, _cfg,
                                              file, function, APR_HOOK_MIDDLE);
 }
+
+static const char *register_log_transaction_hook(cmd_parms *cmd, void *_cfg,
+                                                const char *file,
+                                                const char *function)
+{
+    return register_named_file_function_hook("log_transaction", cmd, _cfg,
+                                             file, function, APR_HOOK_FIRST);
+}
+
 static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
                                                  const char *line)
 {
@@ -1218,13 +1347,14 @@ static const char *register_map_to_storage_block(cmd_parms *cmd, void *_cfg,
                                               line);
 }
 
+
 static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
                                                const char *file,
                                                const char *function,
                                                const char *when)
 {
     int apr_hook_when = APR_HOOK_MIDDLE;
-
+/* XXX: This does not currently work!!
     if (when) {
         if (!strcasecmp(when, "early")) {
             apr_hook_when = AP_LUA_HOOK_FIRST;
@@ -1236,7 +1366,7 @@ static const char *register_check_user_id_hook(cmd_parms *cmd, void *_cfg,
             return "Third argument must be 'early' or 'late'";
         }
     }
-
+*/
     return register_named_file_function_hook("check_user_id", cmd, _cfg, file,
                                              function, apr_hook_when);
 }
@@ -1552,7 +1682,9 @@ static const char *register_lua_root(cmd_parms *cmd, void *_cfg,
     cfg->root_path = root;
     return NULL;
 }
-AP_LUA_DECLARE(const char *) ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var)
+
+const char *ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c,
+                           request_rec *r, const char *var)
 {
     if (lua_ssl_val) { 
         return (const char *)lua_ssl_val(p, s, c, r, (char *)var);
@@ -1560,7 +1692,7 @@ AP_LUA_DECLARE(const char *) ap_lua_ssl_val(apr_pool_t *p, server_rec *s, conn_r
     return NULL;
 }
 
-AP_LUA_DECLARE(int) ap_lua_ssl_is_https(conn_rec *c)
+int ap_lua_ssl_is_https(conn_rec *c)
 {
     return lua_ssl_is_https ? lua_ssl_is_https(c) : 0;
 }
@@ -1572,6 +1704,7 @@ static const char *lua_authz_parse(cmd_parms *cmd, const char *require_line,
 {
     const char *provider_name;
     lua_authz_provider_spec *spec;
+    lua_authz_provider_func *func = apr_pcalloc(cmd->pool, sizeof(lua_authz_provider_func));
 
     apr_pool_userdata_get((void**)&provider_name, AUTHZ_PROVIDER_NAME_NOTE,
                           cmd->temp_pool);
@@ -1579,16 +1712,17 @@ static const char *lua_authz_parse(cmd_parms *cmd, const char *require_line,
 
     spec = apr_hash_get(lua_authz_providers, provider_name, APR_HASH_KEY_STRING);
     ap_assert(spec != NULL);
+    func->spec = spec;
 
     if (require_line && *require_line) {
         const char *arg;
-        spec->args = apr_array_make(cmd->pool, 2, sizeof(const char *));
+        func->args = apr_array_make(cmd->pool, 2, sizeof(const char *));
         while ((arg = ap_getword_conf(cmd->pool, &require_line)) && *arg) {
-            APR_ARRAY_PUSH(spec->args, const char *) = arg;
+            APR_ARRAY_PUSH(func->args, const char *) = arg;
         }
     }
 
-    *parsed_require_line = spec;
+    *parsed_require_line = func;
     return NULL;
 }
 
@@ -1602,7 +1736,8 @@ static authz_status lua_authz_check(request_rec *r, const char *require_line,
                                                          &lua_module);
     const ap_lua_dir_cfg *cfg = ap_get_module_config(r->per_dir_config,
                                                      &lua_module);
-    const lua_authz_provider_spec *prov_spec = parsed_require_line;
+    const lua_authz_provider_func *prov_func = parsed_require_line;
+    const lua_authz_provider_spec *prov_spec = prov_func->spec;
     int result;
     int nargs = 0;
 
@@ -1618,25 +1753,25 @@ static authz_status lua_authz_check(request_rec *r, const char *require_line,
     lua_getglobal(L, prov_spec->function_name);
     if (!lua_isfunction(L, -1)) {
         ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(02319)
-                      "Unable to find function %s in %s",
+                      "Unable to find entry function '%s' in %s (not a valid function)",
                       prov_spec->function_name, prov_spec->file_name);
         ap_lua_release_state(L, spec, r);
         return AUTHZ_GENERAL_ERROR;
     }
     ap_lua_run_lua_request(L, r);
-    if (prov_spec->args) {
+    if (prov_func->args) {
         int i;
-        if (!lua_checkstack(L, prov_spec->args->nelts)) {
+        if (!lua_checkstack(L, prov_func->args->nelts)) {
             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02315)
                           "Error: authz provider %s: too many arguments", prov_spec->name);
             ap_lua_release_state(L, spec, r);
             return AUTHZ_GENERAL_ERROR;
         }
-        for (i = 0; i < prov_spec->args->nelts; i++) {
-            const char *arg = APR_ARRAY_IDX(prov_spec->args, i, const char *);
+        for (i = 0; i < prov_func->args->nelts; i++) {
+            const char *arg = APR_ARRAY_IDX(prov_func->args, i, const char *);
             lua_pushstring(L, arg);
         }
-        nargs = prov_spec->args->nelts;
+        nargs = prov_func->args->nelts;
     }
     if (lua_pcall(L, 1 + nargs, 1, 0)) {
         const char *err = lua_tostring(L, -1);
@@ -1773,6 +1908,10 @@ command_rec lua_commands[] = {
     AP_INIT_TAKE2("LuaHookInsertFilter", register_insert_filter_hook, NULL,
                   OR_ALL,
                   "Provide a hook for the insert_filter phase of request processing"),
+    
+    AP_INIT_TAKE2("LuaHookLog", register_log_transaction_hook, NULL,
+                  OR_ALL,
+                  "Provide a hook for the logging phase of request processing"),
 
     AP_INIT_TAKE123("LuaScope", register_lua_scope, NULL, OR_ALL,
                     "One of once, request, conn, server -- default is once"),
@@ -1836,8 +1975,6 @@ static void *create_server_config(apr_pool_t *p, server_rec *s)
 {
 
     ap_lua_server_cfg *cfg = apr_pcalloc(p, sizeof(ap_lua_server_cfg));
-    cfg->vm_reslists = apr_hash_make(p);
-    apr_thread_rwlock_create(&cfg->vm_reslists_lock, p);
     cfg->root_path = NULL;
 
     return cfg;
@@ -1849,11 +1986,52 @@ static int lua_request_hook(lua_State *L, request_rec *r)
     return OK;
 }
 
+static int lua_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
+                            apr_pool_t *ptemp)
+{
+    ap_mutex_register(pconf, "lua-ivm-shm", NULL, APR_LOCK_DEFAULT, 0);
+    return OK;
+}
+
 static int lua_post_config(apr_pool_t *pconf, apr_pool_t *plog,
                              apr_pool_t *ptemp, server_rec *s)
 {
+    apr_pool_t **pool;
+    apr_status_t rs;
+
     lua_ssl_val = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
     lua_ssl_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
+    
+    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
+        return OK;
+
+    /* Create ivm mutex */
+    rs = ap_global_mutex_create(&lua_ivm_mutex, NULL, "lua-ivm-shm", NULL,
+                            s, pconf, 0);
+    if (APR_SUCCESS != rs) {
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    /* Create shared memory space, anonymous first if possible. */
+    rs = apr_shm_create(&lua_ivm_shm, sizeof pool, NULL, pconf);
+    if (APR_STATUS_IS_ENOTIMPL(rs)) {
+        /* Fall back to filename-based; nuke any left-over first. */
+        lua_ivm_shmfile = ap_runtime_dir_relative(pconf, DEFAULT_LUA_SHMFILE);
+
+        apr_shm_remove(lua_ivm_shmfile, pconf);
+        
+        rs = apr_shm_create(&lua_ivm_shm, sizeof pool, lua_ivm_shmfile, pconf);
+    }
+    if (rs != APR_SUCCESS) {
+        ap_log_error(APLOG_MARK, APLOG_ERR, rs, s, APLOGNO(02665)
+            "mod_lua: Failed to create shared memory segment on file %s",
+                     lua_ivm_shmfile ? lua_ivm_shmfile : "(anonymous)");
+        return HTTP_INTERNAL_SERVER_ERROR;
+    }
+    pool = (apr_pool_t **)apr_shm_baseaddr_get(lua_ivm_shm);
+    apr_pool_create(pool, pconf);
+    apr_pool_cleanup_register(pconf, NULL, shm_cleanup_wrapper,
+                          apr_pool_cleanup_null);
     return OK;
 }
 static void *overlay_hook_specs(apr_pool_t *p,
@@ -1880,11 +2058,11 @@ static void *merge_dir_config(apr_pool_t *p, void *basev, void *overridesv)
     a->dir = apr_pstrdup(p, overrides->dir);
 
     a->vm_scope = (overrides->vm_scope == AP_LUA_SCOPE_UNSET) ? base->vm_scope: overrides->vm_scope;
-    a->inherit = (overrides->inherit== AP_LUA_INHERIT_UNSET) ? base->inherit : overrides->inherit;
-    a->codecache = (overrides->codecache== AP_LUA_CACHE_UNSET) ? base->codecache : overrides->codecache;
+    a->inherit = (overrides->inherit == AP_LUA_INHERIT_UNSET) ? base->inherit : overrides->inherit;
+    a->codecache = (overrides->codecache == AP_LUA_CACHE_UNSET) ? base->codecache : overrides->codecache;
     
-    a->vm_min = (overrides->vm_min== 0) ? base->vm_min : overrides->vm_min;
-    a->vm_max = (overrides->vm_max== 0) ? base->vm_max : overrides->vm_max;
+    a->vm_min = (overrides->vm_min == 0) ? base->vm_min : overrides->vm_min;
+    a->vm_max = (overrides->vm_max == 0) ? base->vm_max : overrides->vm_max;
 
     if (a->inherit == AP_LUA_INHERIT_UNSET || a->inherit == AP_LUA_INHERIT_PARENT_FIRST) { 
         a->package_paths = apr_array_append(p, base->package_paths, overrides->package_paths);
@@ -1930,13 +2108,16 @@ static void lua_register_hooks(apr_pool_t *p)
     ap_hook_map_to_storage(lua_map_to_storage_harness, NULL, NULL,
                            APR_HOOK_MIDDLE);
 
-    ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL,
+/*  XXX: Does not work :(  
+ *  ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL,
                           AP_LUA_HOOK_FIRST);
+ */
     ap_hook_check_user_id(lua_check_user_id_harness, NULL, NULL,
                            APR_HOOK_MIDDLE);
-    ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
+/*  XXX: Does not work :(
+ * ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
                           AP_LUA_HOOK_LAST);
-
+*/
     ap_hook_type_checker(lua_type_checker_harness, NULL, NULL,
                          APR_HOOK_MIDDLE);
 
@@ -1958,6 +2139,7 @@ static void lua_register_hooks(apr_pool_t *p)
     ap_hook_quick_handler(lua_quick_harness, NULL, NULL, APR_HOOK_FIRST);
 
     ap_hook_post_config(lua_post_config, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_pre_config(lua_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
 
     APR_OPTIONAL_HOOK(ap_lua, lua_open, lua_open_hook, NULL, NULL,
                       APR_HOOK_REALLY_FIRST);
@@ -1965,11 +2147,18 @@ static void lua_register_hooks(apr_pool_t *p)
     APR_OPTIONAL_HOOK(ap_lua, lua_request, lua_request_hook, NULL, NULL,
                       APR_HOOK_REALLY_FIRST);
     ap_hook_handler(lua_map_handler, NULL, NULL, AP_LUA_HOOK_FIRST);
+    
+    /* Hook this right before FallbackResource kicks in */
+    ap_hook_fixups(lua_map_handler_fixups, NULL, NULL, AP_LUA_HOOK_LAST-2);
 #if APR_HAS_THREADS
     ap_hook_child_init(ap_lua_init_mutex, NULL, NULL, APR_HOOK_MIDDLE);
 #endif
     /* providers */
     lua_authz_providers = apr_hash_make(p);
+    
+    /* Logging catcher */
+    ap_hook_log_transaction(lua_log_transaction_harness,NULL,NULL,
+                            APR_HOOK_FIRST);
 }
 
 AP_DECLARE_MODULE(lua) = {