/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mod_lua.h"
#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 */
#endif
#include <unistd.h>
#endif
/* getpid for Windows */
#include <process.h>
#endif
(lua_State *L, apr_pool_t *p),
(lua_State *L, request_rec *r),
typedef struct {
const char *name;
const char *file_name;
const char *function_name;
typedef struct {
typedef struct
{
lua_State *L;
int broken;
char *lua_ivm_shmfile;
if (lua_ivm_shm) {
return apr_shm_destroy(lua_ivm_shm);
}
return OK;
}
/**
* error reporting if lua has an error.
* Extracts the error from lua stack and prints
*/
{
const char *lua_response;
r->content_type = "text/html";
ap_rputs("<h3>Error!</h3>\n", r);
ap_rputs("<pre>", r);
ap_rputs("</pre>\n", r);
}
{
ap_lua_init(L, p);
ap_lua_load_request_lmodule(L, p);
}
{
lua_open_callback(L, p, NULL);
return OK;
}
{
switch (scope) {
case AP_LUA_SCOPE_ONCE:
case AP_LUA_SCOPE_UNSET:
return "once";
case AP_LUA_SCOPE_REQUEST:
return "request";
case AP_LUA_SCOPE_CONN:
return "conn";
#if APR_HAS_THREADS
case AP_LUA_SCOPE_THREAD:
return "thread";
case AP_LUA_SCOPE_SERVER:
return "server";
#endif
default:
ap_assert(0);
return 0;
}
}
char *hash;
lua_settop(L, 0);
}
}
}
}
request_rec *r,
const ap_lua_dir_cfg *cfg,
const ap_lua_server_cfg *server_cfg,
const char *filename,
const char *bytecode,
const char *function,
const char *what)
{
if (filename) {
char *file;
}
else {
}
"%s details: scope: %s, file: %s, func: %s",
case AP_LUA_SCOPE_ONCE:
case AP_LUA_SCOPE_UNSET:
break;
case AP_LUA_SCOPE_REQUEST:
break;
case AP_LUA_SCOPE_CONN:
break;
#if APR_HAS_THREADS
case AP_LUA_SCOPE_THREAD:
break;
case AP_LUA_SCOPE_SERVER:
break;
#endif
default:
ap_assert(0);
}
*lifecycle_pool = pool;
return spec;
}
static const char* ap_lua_interpolate_string(apr_pool_t* pool, const char* string, const char** values)
{
char *stringBetween;
const char* ret;
int srclen,x,y;
ret = "";
y = 0;
for (x=0; x < srclen; x++) {
if (x-y > 0) {
}
else {
stringBetween = "";
}
y = ++x+1;
}
}
if (x-y > 0 && y > 0) {
}
/* If no replacement was made, just return the original string */
else if (y == 0) {
return string;
}
return ret;
}
/**
* "main"
*/
{
return DECLINED;
}
/* Decline the request if the script does not exist (or is a directory),
* rather than just returning internal server error */
if (
) {
return DECLINED;
}
"handling [%s] in mod_lua", r->filename);
/* XXX: This seems wrong because it may generate wrong headers for HEAD requests */
if (!r->header_only) {
lua_State *L;
&lua_module);
0, "handle", "request handler");
if (!L) {
/* TODO annotate spec with failure reason */
ap_rputs("Unable to compile VM, see logs", r);
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
lua_getglobal(L, "handle");
if (!lua_isfunction(L, -1)) {
"lua: Unable to find entry function '%s' in %s (not a valid function)",
"handle",
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
ap_lua_run_lua_request(L, r);
report_lua_error(L, r);
}
if (lua_isnumber(L, -1)) {
}
ap_lua_release_state(L, spec, r);
}
return rc;
}
int n, rc;
lua_State *L;
&lua_module);
&lua_module);
*c = ctx;
/* 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.
*/
continue;
}
NULL,
0,
"filter");
if (L) {
L = lua_newthread(L);
}
if (!L) {
"lua: Failed to obtain lua interpreter for %s %s",
ap_lua_release_state(L, spec, r);
return APR_EGENERAL;
}
if (!lua_isfunction(L, -1)) {
"lua: Unable to find entry function '%s' in %s (not a valid function)",
ap_lua_release_state(L, spec, r);
return APR_EGENERAL;
}
ap_lua_run_lua_request(L, r);
}
else {
int t;
ap_lua_run_lua_request(L, r);
t = lua_gettop(L);
lua_setglobal(L, "r");
lua_settop(L, t);
}
ctx->L = L;
/* If a Lua filter is interested in filtering a request, it must first do a yield,
* otherwise we'll assume that it's not interested and pretend we didn't find it.
*/
/* Not wired by mod_filter */
}
return OK;
}
else {
ap_lua_release_state(L, spec, r);
return APR_ENOENT;
}
}
}
return APR_ENOENT;
}
request_rec *r = f->r;
int rc;
lua_State *L;
conn_rec *c = r->connection;
/* Set up the initial filter context and acquire the function.
* The corresponding Lua function should yield here.
*/
if (!f->ctx) {
if (rc == APR_EGENERAL) {
return HTTP_INTERNAL_SERVER_ERROR;
}
if (rc == APR_ENOENT) {
/* No filter entry found (or the script declined to filter), just pass on the buckets */
}
else {
/* We've got a willing lua filter, setup and check for a prefix */
if (olen > 0) {
if (rv != APR_SUCCESS) {
return rv;
}
}
}
}
L = ctx->L;
/* While the Lua function is still yielding, pass in buckets to the coroutine */
{
const char *data;
/* read the bucket */
/* Push the bucket onto the Lua stack as a global var */
lua_setglobal(L, "bucket");
/* If Lua yielded, it means we have something to pass on */
if (lua_resume(L, 0) == LUA_YIELD) {
if (olen > 0) {
c->bucket_alloc);
if (rv != APR_SUCCESS) {
return rv;
}
}
}
else {
"lua: Error while executing filter: %s",
lua_tostring(L, -1));
return HTTP_INTERNAL_SERVER_ERROR;
}
}
/* If we've safely reached the end, do a final call to Lua to allow for any
finishing moves by the script, such as appending a tail. */
lua_pushnil(L);
lua_setglobal(L, "bucket");
if (lua_resume(L, 0) == LUA_YIELD) {
if (olen > 0) {
c->bucket_alloc);
}
}
if (rv != APR_SUCCESS) {
return rv;
}
}
}
/* Clean up */
return APR_SUCCESS;
}
{
request_rec *r = f->r;
lua_State *L;
conn_rec *c = r->connection;
/* Set up the initial filter context and acquire the function.
* The corresponding Lua function should yield here.
*/
if (!f->ctx) {
if (rc == APR_EGENERAL) {
return HTTP_INTERNAL_SERVER_ERROR;
}
if (rc == APR_ENOENT ) {
}
if (rc == APR_SUCCESS) {
}
}
L = ctx->L;
/* If the Lua script broke or denied serving the request, just pass the buckets through */
}
return ret;
}
/* While the Lua function is still yielding, pass buckets to the coroutine */
lastCall = 0;
const char *data;
if (APR_BUCKET_IS_EOS(pbktIn)) {
break;
}
/* read the bucket */
if (ret != APR_SUCCESS)
return ret;
/* Push the bucket onto the Lua stack as a global var */
lastCall++;
lua_setglobal(L, "bucket");
/* If Lua yielded, it means we have something to pass on */
if (lua_resume(L, 0) == LUA_YIELD) {
return APR_SUCCESS;
}
else {
return HTTP_INTERNAL_SERVER_ERROR;
}
}
/* If we've safely reached the end, do a final call to Lua to allow for any
finishing moves by the script, such as appending a tail. */
if (lastCall == 0) {
lua_pushnil(L);
lua_setglobal(L, "bucket");
if (lua_resume(L, 0) == LUA_YIELD) {
}
}
}
return APR_SUCCESS;
}
/* ---------------- Configury stuff --------------- */
/** harnesses for magic hooks **/
{
int rc;
lua_State *L;
&lua_module);
&lua_module);
if (hook_specs) {
int i;
for (i = 0; i < hook_specs->nelts; i++) {
continue;
}
"request hook");
if (!L) {
"lua: Failed to obtain lua interpreter for entry function '%s' in %s",
return HTTP_INTERNAL_SERVER_ERROR;
}
if (!lua_isfunction(L, -1)) {
"lua: Unable to find entry function '%s' in %s (not a valid function)",
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
ap_lua_run_lua_request(L, r);
}
else {
int t;
ap_lua_run_lua_request(L, r);
t = lua_gettop(L);
lua_setglobal(L, "r");
lua_settop(L, t);
}
report_lua_error(L, r);
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (lua_isnumber(L, -1)) {
}
else {
ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, "Lua hook %s:%s for phase %s did not return a numeric value",
return HTTP_INTERNAL_SERVER_ERROR;
}
ap_lua_release_state(L, spec, r);
return rc;
}
ap_lua_release_state(L, spec, r);
}
}
return DECLINED;
}
/* Fix for making sure that LuaMapHandler works when FallbackResource is set */
{
/* If there is no handler set yet, this might be a LuaMapHandler request */
int n = 0;
&lua_module);
continue;
}
return OK;
}
}
}
return DECLINED;
}
{
int rc, n = 0;
lua_State *L;
&lua_module);
&lua_module);
continue;
}
int i;
for (i=0 ; i < 10; i++) {
}
else values[i] = "";
}
"mapped handler");
if (!L) {
"lua: Failed to obtain Lua interpreter for entry function '%s' in %s",
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (function_name != NULL) {
if (!lua_isfunction(L, -1)) {
"lua: Unable to find entry function '%s' in %s (not a valid function)",
filename);
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
ap_lua_run_lua_request(L, r);
}
else {
int t;
ap_lua_run_lua_request(L, r);
t = lua_gettop(L);
lua_setglobal(L, "r");
lua_settop(L, t);
}
report_lua_error(L, r);
ap_lua_release_state(L, spec, r);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (lua_isnumber(L, -1)) {
}
else {
"lua: Lua handler %s in %s did not return a value, assuming apache2.OK",
filename);
}
ap_lua_release_state(L, spec, r);
return rc;
}
}
}
return DECLINED;
}
{
apr_size_t i = 0;
if (rc == APR_SUCCESS) {
++cfg->line_number;
}
else {
buf[0] = '\0';
i = 0;
}
}
else {
while (i < bufsiz) {
char ch;
if (rc != APR_SUCCESS)
break;
if (ch == '\n') {
++cfg->line_number;
break;
}
}
}
return i;
}
typedef struct cr_ctx
{
const char *endstr;
} cr_ctx;
/* Okay, this deserves a little explaination -- 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.....
*
* be happy. this is cool.
*
*/
static const char *lf =
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
{
const char *p;
return lf;
}
if (p[0] == '<' && p[1] == '/') {
apr_size_t i = 0;
++i;
}
*plen = 0;
return NULL;
}
/*fprintf(stderr, "buf read: %s\n", ctx->buf); */
}
{
(void) L;
return 0;
}
typedef struct hack_section_baton
{
const char *name;
int apr_hook_when;
/* You can be unhappy now.
*
* This is uncool.
*
* When you create a <Section handler in httpd, the only 'easy' way to create
* a directory context is to parse the section, and convert it into a 'normal'
* Configureation option, and then collapse the entire section, in memory,
* back into the parent section -- from which you can then get the new directive
* invoked.... anyways. evil. Rici taught me how to do this hack :-)
*/
const char *arg)
{
if (!hook_specs) {
sizeof(ap_lua_mapped_handler_spec *));
}
return NULL;
}
void *mconfig,
const char *line)
{
"> directive missing closing '>'", NULL);
}
if (line[0]) {
const char *word;
if (*word) {
}
if (*word) {
}
}
else {
"> 2nd argument must be 'early' or 'late'", NULL);
}
}
}
{
char *tmp;
int rv;
if (function) {
}
else {
}
/* This lua State is used only to compile the input strings -> bytecode, so we don't need anything extra. */
lvm = luaL_newstate();
lua_settop(lvm, 0);
if (rv != 0) {
return errstr;
}
else {
luaL_Buffer b;
luaL_buffinit(lvm, &b);
#if LUA_VERSION_NUM >= 503
#else
#endif
luaL_pushresult(&b);
spec->bytecode_len);
}
/* Here, we have to replace our current config node for the next pass */
if (!*current) {
}
}
return NULL;
}
void *_cfg,
const char *file,
const char *function,
int apr_hook_when)
{
if (!hook_specs) {
sizeof(ap_lua_mapped_handler_spec *));
}
return NULL;
}
void *_cfg,
const char *file,
const char *function)
{
return "Invalid regex pattern!";
}
return NULL;
}
void *_cfg,
const char *file,
const char *function,
int direction)
{
/* TODO: Make it work on other types than just AP_FTYPE_RESOURCE? */
if (direction == AP_LUA_FILTER_OUTPUT) {
}
else {
}
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);
}
*/
{
}
/* 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);
}
*/
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
/* ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "LuaHookInsertFilter not yet implemented"); */
}
{
}
{
if (lookup) {
return DECLINED;
}
}
const char *file,
const char *function,
const char *when)
{
if (err) {
return err;
}
if (when) {
}
}
else {
return "Third argument must be 'early' or 'late'";
}
}
}
const char *line)
{
line);
}
const char *file,
const char *function)
{
}
const char *line)
{
}
const char *file,
const char *function)
{
}
const char *file,
const char *function)
{
}
const char *line)
{
line);
}
const char *file,
const char *function,
const char *when)
{
/* XXX: This does not currently work!!
if (when) {
if (!strcasecmp(when, "early")) {
apr_hook_when = AP_LUA_HOOK_FIRST;
}
else if (!strcasecmp(when, "late")) {
apr_hook_when = AP_LUA_HOOK_LAST;
}
else {
return "Third argument must be 'early' or 'late'";
}
}
*/
}
const char *line)
{
line);
}
const char *file,
const char *function)
{
}
const char *line)
{
line);
}
const char *file,
const char *function,
const char *when)
{
if (when) {
}
}
else {
return "Third argument must be 'early' or 'late'";
}
}
}
const char *line)
{
line);
}
const char *file,
const char *function,
const char *when)
{
if (when) {
}
}
else {
return "Third argument must be 'early' or 'late'";
}
}
}
const char *line)
{
line);
}
const char *file,
const char *function)
{
return "LuaHookInsertFilter not yet implemented";
}
{
if (err) {
return err;
}
}
{
if (err) {
return err;
}
function);
}
{
if (err) {
return err;
}
}
{
if (err) {
return err;
}
}
const char *line)
{
line);
}
const char *arg,
{
char *fixed_filename;
arg,
if (rv != APR_SUCCESS) {
"Unable to build full path to file, %s", arg);
}
return NULL;
}
/**
* Called for config directive which looks like
*/
const char *arg)
{
}
/**
* Called for config directive which looks like
*/
void *_cfg,
const char *arg)
{
}
void *_cfg,
const char *arg)
{
}
}
}
else {
"LuaInherit type of '%s' not recognized, valid "
"options are 'none', 'parent-first', and 'parent-last'",
arg);
}
return NULL;
}
void *_cfg,
const char *arg)
{
}
}
}
else {
"LuaCodeCache type of '%s' not recognized, valid "
"options are 'never', 'stat', and 'forever'",
arg);
}
return NULL;
}
void *_cfg,
const char *scope,
const char *min,
const char *max)
{
}
}
}
#if !APR_HAS_THREADS
"Scope type of '%s' cannot be used because this "
"server does not have threading support "
"(APR_HAS_THREADS)"
scope);
#endif
}
#if !APR_HAS_THREADS
"Scope type of '%s' cannot be used because this "
"server does not have threading support "
"(APR_HAS_THREADS)"
scope);
#endif
if (vmin == 0) {
vmin = 1;
}
}
}
else {
"Invalid value for LuaScope, '%s', acceptable "
"values are: 'once', 'request', 'conn'"
#if APR_HAS_THREADS
", 'thread', 'server'"
#endif
,scope);
}
return NULL;
}
const char *root)
{
/* ap_lua_dir_cfg* cfg = (ap_lua_dir_cfg*)_cfg; */
&lua_module);
return NULL;
}
request_rec *r, const char *var)
{
if (lua_ssl_val) {
return (const char *)lua_ssl_val(p, s, c, r, (char *)var);
}
return NULL;
}
{
return lua_ssl_is_https ? lua_ssl_is_https(c) : 0;
}
/*******************************/
const void **parsed_require_line)
{
const char *provider_name;
if (require_line && *require_line) {
const char *arg;
}
}
return NULL;
}
const void *parsed_require_line)
{
lua_State *L;
&lua_module);
&lua_module);
int result;
int nargs = 0;
if (L == NULL) {
return AUTHZ_GENERAL_ERROR;
}
if (!lua_isfunction(L, -1)) {
"Unable to find entry function '%s' in %s (not a valid function)",
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
ap_lua_run_lua_request(L, r);
int i;
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
lua_pushstring(L, arg);
}
}
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
if (!lua_isnumber(L, -1)) {
ap_lua_release_state(L, spec, r);
return AUTHZ_GENERAL_ERROR;
}
ap_lua_release_state(L, spec, r);
switch (result) {
case AUTHZ_DENIED:
case AUTHZ_GRANTED:
case AUTHZ_NEUTRAL:
case AUTHZ_GENERAL_ERROR:
case AUTHZ_DENIED_NO_USER:
return result;
default:
"Error: authz provider %s: invalid return value %d",
}
return AUTHZ_GENERAL_ERROR;
}
{
};
const char *function)
{
if (err)
return err;
return NULL;
}
"Specify the base path for resolving relative paths for mod_lua directives"),
"Add a directory to lua's package.path"),
"Add a directory to lua's package.cpath"),
"Provide an authorization provider"),
"Provide a hook for the translate name phase of request processing"),
NULL,
"Provide a hook for the translate name phase of request processing"),
"Provide a hook for the fixups phase of request processing"),
"Provide a inline hook for the fixups phase of request processing"),
/* todo: test */
"Provide a hook for the map_to_storage phase of request processing"),
NULL,
"Provide a hook for the map_to_storage phase of request processing"),
/* todo: test */
"Provide a hook for the check_user_id phase of request processing"),
NULL,
"Provide a hook for the check_user_id phase of request processing"),
/* todo: test */
"Provide a hook for the type_checker phase of request processing"),
"Provide a hook for the type_checker phase of request processing"),
/* todo: test */
"Provide a hook for the access_checker phase of request processing"),
NULL,
"Provide a hook for the access_checker phase of request processing"),
/* todo: test */
"Provide a hook for the auth_checker phase of request processing"),
"Provide a hook for the auth_checker phase of request processing"),
/* todo: test */
"Provide a hook for the insert_filter phase of request processing"),
"Provide a hook for the logging phase of request processing"),
"One of once, request, conn, server -- default is once"),
"Controls how Lua scripts in parent contexts are merged with the current "
" context: none|parent-last|parent-first (default: parent-first) "),
"Controls the behavior of the in-memory code cache "
" context: stat|forever|never (default: stat) "),
"Provide a hook for the quick handler of request processing"),
"Provide a hook for the quick handler of request processing"),
"(internal) Byte code handler"),
"Maps a path to a lua handler"),
"Registers a Lua function as an output filter"),
"Registers a Lua function as an input filter"),
{NULL}
};
{
return cfg;
}
{
return OK;
}
{
return cfg;
}
{
ap_lua_push_request(L, r);
return OK;
}
{
return OK;
}
{
const char *tempdir;
return OK;
/* Create ivm mutex */
s, pconf, 0);
if (APR_SUCCESS != rs) {
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Create shared memory space */
if (rs != APR_SUCCESS) {
"mod_lua IVM: Failed to find temporary directory");
return HTTP_INTERNAL_SERVER_ERROR;
}
(long int)getpid());
(const char *) lua_ivm_shmfile, pconf);
if (rs != APR_SUCCESS) {
"mod_lua: Failed to create shared memory segment on file %s",
return HTTP_INTERNAL_SERVER_ERROR;
}
return OK;
}
const void *key,
const void *overlay_val,
const void *base_val,
const void *data)
{
}
{
a->codecache = (overrides->codecache == AP_LUA_CACHE_UNSET) ? base->codecache : overrides->codecache;
}
else if (a->inherit == AP_LUA_INHERIT_PARENT_LAST) {
}
else {
}
return a;
}
{
/* ap_register_output_filter("luahood", luahood, NULL, AP_FTYPE_RESOURCE); */
/* http_request.h hooks */
/* XXX: Does not work :(
* ap_hook_check_user_id(lua_check_user_id_harness_first, NULL, NULL,
AP_LUA_HOOK_FIRST);
*/
/* XXX: Does not work :(
* ap_hook_check_user_id(lua_check_user_id_harness_last, NULL, NULL,
AP_LUA_HOOK_LAST);
*/
/* Hook this right before FallbackResource kicks in */
#if APR_HAS_THREADS
#endif
/* providers */
/* Logging catcher */
}
create_dir_config, /* create per-dir config structures */
merge_dir_config, /* merge per-dir config structures */
create_server_config, /* create per-server config structures */
NULL, /* merge per-server config structures */
lua_commands, /* table of config file commands */
lua_register_hooks /* register hooks */
};