1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/**
* Copyright 2004-2005 Paul Querna
*
* Licensed 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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_gnutls.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include <ctype.h>
static char *MGS_LUA_RRKEY = "request_rec";
static request_rec *mgs_lua_getrr(lua_State * lvm)
{
request_rec *r;
/* Push the request_rec off the registry, onto the stack. */
lua_pushlightuserdata(lvm, MGS_LUA_RRKEY);
lua_gettable(lvm, LUA_REGISTRYINDEX);
r = lua_touserdata(lvm, -1);
lua_pop(lvm, 1);
return r;
}
static int get_request_table(lua_State * lvm, long offset)
{
const char *key;
request_rec *r;
const char *value;
apr_table_t *t;
key = luaL_checkstring(lvm, 1);
r = mgs_lua_getrr(lvm);
t = *(apr_table_t **) ((char *) r + offset);
value = apr_table_get(t, key);
if (value) {
lua_pushstring(lvm, value);
return 1;
} else {
return 0;
}
}
static int mgs_lua_getenv(lua_State * lvm)
{
return get_request_table(lvm,
APR_OFFSETOF(request_rec,
subprocess_env));
}
static int mgs_lua_getheader(lua_State * lvm)
{
return get_request_table(lvm,
APR_OFFSETOF(request_rec, headers_in));
}
static const luaL_reg mgs_lua_reg[] = {
{"getenv", mgs_lua_getenv},
{"header", mgs_lua_getheader},
{NULL, NULL}
};
lua_State *get_luastate()
{
lua_State *lvm = lua_open();
luaopen_base(lvm);
luaopen_io(lvm);
luaopen_table(lvm);
luaopen_string(lvm);
luaopen_math(lvm);
luaopen_loadlib(lvm);
luaL_openlib(lvm, "ap", mgs_lua_reg, 0);
return lvm;
}
int mgs_authz_lua(request_rec * r)
{
int rv;
lua_State *lvm;
mgs_dirconf_rec *dc = ap_get_module_config(r->per_dir_config,
&gnutls_module);
if (dc->lua_bytecode_len <= 0) {
return 0;
}
lvm = get_luastate();
lua_pushlightuserdata(lvm, MGS_LUA_RRKEY);
lua_pushlightuserdata(lvm, r);
lua_settable(lvm, LUA_REGISTRYINDEX);
/* Push Bytecode onto the stack */
rv = luaL_loadbuffer(lvm, dc->lua_bytecode, dc->lua_bytecode_len,
"gnutls-lua");
if (rv != 0) {
/* Get the Error message */
const char *error = lua_tostring(lvm, -1);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"GnuTLS: Error Loading Lua Bytecode: %s",
error);
lua_pop(lvm, 1);
return -1;
}
rv = lua_pcall(lvm, 0, 1, 0);
if (rv != 0) {
/* Get the Error message */
const char *error = lua_tostring(lvm, -1);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"GnuTLS: Error Running Lua: %s", error);
lua_pop(lvm, 1);
return -1;
}
rv = (int) lua_tonumber(lvm, -1);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"GnuTLS: (%d) Lua Return: %d",
dc->lua_bytecode_len, rv);
lua_pop(lvm, 1);
lua_close(lvm);
return rv;
}
static apr_size_t config_getstr(ap_configfile_t * cfg, char *buf,
size_t bufsiz)
{
apr_size_t i = 0;
if (cfg->getstr) {
const char *res = (cfg->getstr) (buf, bufsiz, cfg->param);
if (res) {
i = strlen(buf);
if (i && buf[i - 1] == '\n')
++cfg->line_number;
} else {
buf[0] = '\0';
i = 0;
}
} else {
while (i < bufsiz) {
int ch = (cfg->getch) (cfg->param);
if (ch == EOF)
break;
buf[i++] = ch;
if (ch == '\n') {
++cfg->line_number;
break;
}
}
}
return i;
}
struct cr_ctx {
ap_configfile_t *cfp;
size_t startline;
char buf[HUGE_STRING_LEN];
};
static const char *LUACMD = "gnutlsrequire";
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";
#define N_LF 32
static const char *direct_chunkreader(lua_State * lvm, void *udata,
size_t * plen)
{
const char *p;
struct cr_ctx *ctx = udata;
if (ctx->startline) {
*plen = ctx->startline > N_LF ? N_LF : ctx->startline;
ctx->startline -= *plen;
return lf;
}
*plen = config_getstr(ctx->cfp, ctx->buf, HUGE_STRING_LEN);
for (p = ctx->buf; isspace(*p); ++p);
if (p[0] == '<' && p[1] == '/') {
int i = 0;
while (i < strlen(LUACMD)) {
if (tolower(p[i + 2]) != LUACMD[i])
return ctx->buf;
++i;
}
*plen = 0;
return NULL;
}
return ctx->buf;
}
static int ldump_writer(lua_State * L, const void *b, size_t size, void *B)
{
(void) L;
luaL_addlstring((luaL_Buffer *) B, (const char *) b, size);
return 1;
}
/* a bytecode buffer*/
typedef struct bcbuf_ctx {
apr_size_t buflen;
char *buf;
} bcbuf_ctx;
const char *mgs_set_require_section(cmd_parms * cmd, void *mconfig,
const char *arg)
{
apr_size_t bytecode_len;
const char *bytecode;
bcbuf_ctx *bcbuf;
luaL_Buffer b;
ap_directive_t **current = mconfig;
struct cr_ctx ctx[1];
int result;
const char *filename =
apr_psprintf(cmd->pool, "@%s", cmd->config_file->name);
// get a word argument
const char *word;
apr_size_t wordlen;
lua_State *lvm = get_luastate();
word = ap_getword_conf(cmd->pool, &arg);
wordlen = strlen(word);
do {
if (wordlen) {
if (word[wordlen - 1] == '>') {
--wordlen;
break;
}
if (*arg == '>')
break;
}
return apr_pstrcat(cmd->pool, "<", LUACMD,
"> takes exactly one argument", NULL);
} while (0);
ctx->cfp = cmd->config_file;
ctx->startline = cmd->config_file->line_number;
lua_settop(lvm, 0);
result = lua_load(lvm, direct_chunkreader, ctx, filename);
if (result != 0) {
word =
apr_pstrcat(cmd->pool, "Lua Error:",
lua_tostring(lvm, -1), NULL);
lua_close(lvm);
return word;
} else {
luaL_buffinit(lvm, &b);
lua_dump(lvm, ldump_writer, &b);
luaL_pushresult(&b);
bytecode = lua_tostring(lvm, -1);
bytecode_len = lua_strlen(lvm, -1);
}
/* Here, we have to replace our current config node for the next pass */
if (!*current) {
*current = apr_pcalloc(cmd->pool, sizeof(**current));
}
(*current)->filename = cmd->config_file->name;
(*current)->line_num = ctx->startline;
(*current)->directive =
apr_pstrdup(cmd->pool, "GnuTLSRequireByteCode");
(*current)->args = NULL;
bcbuf = apr_pcalloc(cmd->pool, sizeof(bcbuf));
bcbuf->buflen = bytecode_len;
bcbuf->buf = apr_pstrmemdup(cmd->pool, bytecode, bytecode_len);
(*current)->data = bcbuf;
lua_close(lvm);
return NULL;
}
const char *mgs_set_require_bytecode(cmd_parms * cmd, void *mconfig,
const char *arg)
{
bcbuf_ctx *bcbuf;
ap_directive_t *directive = cmd->directive;
mgs_dirconf_rec *dc = mconfig;
bcbuf = directive->data;
dc->lua_bytecode_len = bcbuf->buflen;
dc->lua_bytecode =
apr_pstrmemdup(cmd->pool, bcbuf->buf, bcbuf->buflen);
return NULL;
}
|