1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "lua_config.h"
19 #include "lua_vmprep.h"
20 
21 APLOG_USE_MODULE(lua);
22 
check_dir_config(lua_State * L,int index)23 static ap_lua_dir_cfg *check_dir_config(lua_State *L, int index)
24 {
25     ap_lua_dir_cfg *cfg;
26     luaL_checkudata(L, index, "Apache2.DirConfig");
27     cfg = (ap_lua_dir_cfg *) lua_unboxpointer(L, index);
28     return cfg;
29 }
30 
check_cmd_parms(lua_State * L,int index)31 static cmd_parms *check_cmd_parms(lua_State *L, int index)
32 {
33     cmd_parms *cmd;
34     luaL_checkudata(L, index, "Apache2.CommandParameters");
35     cmd = (cmd_parms *) lua_unboxpointer(L, index);
36     return cmd;
37 }
38 
apl_toscope(const char * name)39 static int apl_toscope(const char *name)
40 {
41     if (0 == strcmp("once", name))
42         return AP_LUA_SCOPE_ONCE;
43     if (0 == strcmp("request", name))
44         return AP_LUA_SCOPE_REQUEST;
45     if (0 == strcmp("connection", name))
46         return AP_LUA_SCOPE_CONN;
47     if (0 == strcmp("conn", name))
48         return AP_LUA_SCOPE_CONN;
49     if (0 == strcmp("thread", name))
50         return AP_LUA_SCOPE_THREAD;
51     return AP_LUA_SCOPE_ONCE;
52 }
53 
ap_lua_map_handler(ap_lua_dir_cfg * cfg,const char * file,const char * function,const char * pattern,const char * scope)54 apr_status_t ap_lua_map_handler(ap_lua_dir_cfg *cfg,
55                                                  const char *file,
56                                                  const char *function,
57                                                  const char *pattern,
58                                                  const char *scope)
59 {
60     ap_regex_t *uri_pattern;
61     apr_status_t rv;
62     ap_lua_mapped_handler_spec *handler =
63         apr_pcalloc(cfg->pool, sizeof(ap_lua_mapped_handler_spec));
64     handler->uri_pattern = NULL;
65     handler->function_name = NULL;
66 
67     uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
68     if ((rv = ap_regcomp(uri_pattern, pattern, 0)) != APR_SUCCESS) {
69         return rv;
70     }
71     handler->file_name = apr_pstrdup(cfg->pool, file);
72     handler->uri_pattern = uri_pattern;
73     handler->scope = apl_toscope(scope);
74 
75     handler->function_name = apr_pstrdup(cfg->pool, function);
76     *(const ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) =
77         handler;
78     return APR_SUCCESS;
79 }
80 
81 /* Change to use ap_lua_map_handler */
cfg_lua_map_handler(lua_State * L)82 static int cfg_lua_map_handler(lua_State *L)
83 {
84     ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
85     ap_lua_mapped_handler_spec *handler =
86         apr_pcalloc(cfg->pool, sizeof(ap_lua_mapped_handler_spec));
87     handler->uri_pattern = NULL;
88     handler->function_name = NULL;
89 
90     luaL_checktype(L, 2, LUA_TTABLE);
91     lua_getfield(L, 2, "file");
92     if (lua_isstring(L, -1)) {
93         const char *file = lua_tostring(L, -1);
94         handler->file_name = apr_pstrdup(cfg->pool, file);
95     }
96     lua_pop(L, 1);
97 
98     lua_getfield(L, 2, "pattern");
99     if (lua_isstring(L, -1)) {
100         const char *pattern = lua_tostring(L, -1);
101 
102         ap_regex_t *uri_pattern = apr_palloc(cfg->pool, sizeof(ap_regex_t));
103         if (ap_regcomp(uri_pattern, pattern, 0) != OK) {
104             return luaL_error(L, "Unable to compile regular expression, '%s'",
105                               pattern);
106         }
107         handler->uri_pattern = uri_pattern;
108     }
109     lua_pop(L, 1);
110 
111     lua_getfield(L, 2, "scope");
112     if (lua_isstring(L, -1)) {
113         const char *scope = lua_tostring(L, -1);
114         handler->scope = apl_toscope(scope);
115     }
116     else {
117         handler->scope = AP_LUA_SCOPE_ONCE;
118     }
119     lua_pop(L, 1);
120 
121     lua_getfield(L, 2, "func");
122     if (lua_isstring(L, -1)) {
123         const char *value = lua_tostring(L, -1);
124         handler->function_name = apr_pstrdup(cfg->pool, value);
125     }
126     else {
127         handler->function_name = "handle";
128     }
129     lua_pop(L, 1);
130 
131 
132     *(const ap_lua_mapped_handler_spec **) apr_array_push(cfg->mapped_handlers) =
133         handler;
134     return 0;
135 }
136 
cfg_directory(lua_State * L)137 static int cfg_directory(lua_State *L)
138 {
139     ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
140     lua_pushstring(L, cfg->dir);
141     return 1;
142 }
143 
144 /*static int cfg_root(lua_State *L)
145 {
146     ap_lua_dir_cfg *cfg = check_dir_config(L, 1);
147     lua_pushstring(L, cfg->root_path);
148     return 1;
149 }*/
150 
151 static const struct luaL_Reg cfg_methods[] = {
152     {"match_handler", cfg_lua_map_handler},
153     {"directory", cfg_directory},
154     /* {"root", cfg_root}, */
155     {NULL, NULL}
156 };
157 
158 /* helper function for the logging functions below */
cmd_log_at(lua_State * L,int level)159 static int cmd_log_at(lua_State *L, int level)
160 {
161     const char *msg;
162     cmd_parms *cmd = check_cmd_parms(L, 1);
163     lua_Debug dbg;
164 
165     lua_getstack(L, 1, &dbg);
166     lua_getinfo(L, "Sl", &dbg);
167 
168     msg = luaL_checkstring(L, 2);
169     /* Intentional no APLOGNO */
170     ap_log_error(dbg.source, dbg.currentline, APLOG_MODULE_INDEX, level, 0,
171                  cmd->server, "%s", msg);
172     return 0;
173 }
174 
175 /* r:debug(String) and friends which use apache logging */
cmd_emerg(lua_State * L)176 static int cmd_emerg(lua_State *L)
177 {
178     return cmd_log_at(L, APLOG_EMERG);
179 }
cmd_alert(lua_State * L)180 static int cmd_alert(lua_State *L)
181 {
182     return cmd_log_at(L, APLOG_ALERT);
183 }
cmd_crit(lua_State * L)184 static int cmd_crit(lua_State *L)
185 {
186     return cmd_log_at(L, APLOG_CRIT);
187 }
cmd_err(lua_State * L)188 static int cmd_err(lua_State *L)
189 {
190     return cmd_log_at(L, APLOG_ERR);
191 }
cmd_warn(lua_State * L)192 static int cmd_warn(lua_State *L)
193 {
194     return cmd_log_at(L, APLOG_WARNING);
195 }
cmd_notice(lua_State * L)196 static int cmd_notice(lua_State *L)
197 {
198     return cmd_log_at(L, APLOG_NOTICE);
199 }
cmd_info(lua_State * L)200 static int cmd_info(lua_State *L)
201 {
202     return cmd_log_at(L, APLOG_INFO);
203 }
cmd_debug(lua_State * L)204 static int cmd_debug(lua_State *L)
205 {
206     return cmd_log_at(L, APLOG_DEBUG);
207 }
cmd_trace1(lua_State * L)208 static int cmd_trace1(lua_State *L)
209 {
210     return cmd_log_at(L, APLOG_TRACE1);
211 }
cmd_trace2(lua_State * L)212 static int cmd_trace2(lua_State *L)
213 {
214     return cmd_log_at(L, APLOG_TRACE2);
215 }
cmd_trace3(lua_State * L)216 static int cmd_trace3(lua_State *L)
217 {
218     return cmd_log_at(L, APLOG_TRACE3);
219 }
cmd_trace4(lua_State * L)220 static int cmd_trace4(lua_State *L)
221 {
222     return cmd_log_at(L, APLOG_TRACE4);
223 }
cmd_trace5(lua_State * L)224 static int cmd_trace5(lua_State *L)
225 {
226     return cmd_log_at(L, APLOG_TRACE5);
227 }
cmd_trace6(lua_State * L)228 static int cmd_trace6(lua_State *L)
229 {
230     return cmd_log_at(L, APLOG_TRACE6);
231 }
cmd_trace7(lua_State * L)232 static int cmd_trace7(lua_State *L)
233 {
234     return cmd_log_at(L, APLOG_TRACE7);
235 }
cmd_trace8(lua_State * L)236 static int cmd_trace8(lua_State *L)
237 {
238     return cmd_log_at(L, APLOG_TRACE8);
239 }
240 
241 static const struct luaL_Reg cmd_methods[] = {
242     {"trace8", cmd_trace8},
243     {"trace7", cmd_trace7},
244     {"trace6", cmd_trace6},
245     {"trace5", cmd_trace5},
246     {"trace4", cmd_trace4},
247     {"trace3", cmd_trace3},
248     {"trace2", cmd_trace2},
249     {"trace1", cmd_trace1},
250     {"debug", cmd_debug},
251     {"info", cmd_info},
252     {"notice", cmd_notice},
253     {"warn", cmd_warn},
254     {"err", cmd_err},
255     {"crit", cmd_crit},
256     {"alert", cmd_alert},
257     {"emerg", cmd_emerg},
258 
259     {NULL, NULL}
260 };
261 
ap_lua_load_config_lmodule(lua_State * L)262 void ap_lua_load_config_lmodule(lua_State *L)
263 {
264     luaL_newmetatable(L, "Apache2.DirConfig");  /* [metatable] */
265     lua_pushvalue(L, -1);
266 
267     lua_setfield(L, -2, "__index");
268     luaL_setfuncs_compat(L, cfg_methods);       /* [metatable] */
269 
270 
271     luaL_newmetatable(L, "Apache2.CommandParameters");
272     lua_pushvalue(L, -1);
273 
274     lua_setfield(L, -2, "__index");
275     luaL_setfuncs_compat(L, cmd_methods);       /* [metatable] */
276 
277 }
278