1 #include "buffer.h"
2 #include "server.h"
3 #include "log.h"
4 #include "plugin.h"
5 #include "response.h"
6 
7 #include "mod_cml.h"
8 #include "mod_cml_funcs.h"
9 
10 #include <sys/stat.h>
11 #include <time.h>
12 
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <unistd.h>
17 #include <dirent.h>
18 #include <stdio.h>
19 
20 #include "md5.h"
21 
22 #define HASHLEN 16
23 typedef unsigned char HASH[HASHLEN];
24 #define HASHHEXLEN 32
25 typedef char HASHHEX[HASHHEXLEN+1];
26 #ifdef USE_OPENSSL
27 #define IN const
28 #else
29 #define IN
30 #endif
31 #define OUT
32 
33 #ifdef HAVE_LUA_H
34 
f_crypto_md5(lua_State * L)35 int f_crypto_md5(lua_State *L) {
36 	li_MD5_CTX Md5Ctx;
37 	HASH HA1;
38 	buffer b;
39 	char hex[33];
40 	int n = lua_gettop(L);
41 
42 	b.ptr = hex;
43 	b.used = 0;
44 	b.size = sizeof(hex);
45 
46 	if (n != 1) {
47 		lua_pushstring(L, "md5: expected one argument");
48 		lua_error(L);
49 	}
50 
51 	if (!lua_isstring(L, 1)) {
52 		lua_pushstring(L, "md5: argument has to be a string");
53 		lua_error(L);
54 	}
55 
56 	li_MD5_Init(&Md5Ctx);
57 	li_MD5_Update(&Md5Ctx, (unsigned char *)lua_tostring(L, 1), lua_strlen(L, 1));
58 	li_MD5_Final(HA1, &Md5Ctx);
59 
60 	buffer_copy_string_hex(&b, (char *)HA1, 16);
61 
62 	lua_pushstring(L, b.ptr);
63 
64 	return 1;
65 }
66 
67 
f_file_mtime(lua_State * L)68 int f_file_mtime(lua_State *L) {
69 	struct stat st;
70 	int n = lua_gettop(L);
71 
72 	if (n != 1) {
73 		lua_pushstring(L, "file_mtime: expected one argument");
74 		lua_error(L);
75 	}
76 
77 	if (!lua_isstring(L, 1)) {
78 		lua_pushstring(L, "file_mtime: argument has to be a string");
79 		lua_error(L);
80 	}
81 
82 	if (-1 == stat(lua_tostring(L, 1), &st)) {
83 		lua_pushnil(L);
84 		return 1;
85 	}
86 
87 	lua_pushnumber(L, st.st_mtime);
88 
89 	return 1;
90 }
91 
f_dir_files_iter(lua_State * L)92 static int f_dir_files_iter(lua_State *L) {
93 	DIR *d;
94 	struct dirent *de;
95 
96 	d = lua_touserdata(L, lua_upvalueindex(1));
97 
98 	if (NULL == (de = readdir(d))) {
99 		/* EOF */
100 		closedir(d);
101 
102 		return 0;
103 	} else {
104 		lua_pushstring(L, de->d_name);
105 		return 1;
106 	}
107 }
108 
f_dir_files(lua_State * L)109 int f_dir_files(lua_State *L) {
110 	DIR *d;
111 	int n = lua_gettop(L);
112 
113 	if (n != 1) {
114 		lua_pushstring(L, "dir_files: expected one argument");
115 		lua_error(L);
116 	}
117 
118 	if (!lua_isstring(L, 1)) {
119 		lua_pushstring(L, "dir_files: argument has to be a string");
120 		lua_error(L);
121 	}
122 
123 	/* check if there is a valid DIR handle on the stack */
124 	if (NULL == (d = opendir(lua_tostring(L, 1)))) {
125 		lua_pushnil(L);
126 		return 1;
127 	}
128 
129 	/* push d into registry */
130 	lua_pushlightuserdata(L, d);
131 	lua_pushcclosure(L, f_dir_files_iter, 1);
132 
133 	return 1;
134 }
135 
f_file_isreg(lua_State * L)136 int f_file_isreg(lua_State *L) {
137 	struct stat st;
138 	int n = lua_gettop(L);
139 
140 	if (n != 1) {
141 		lua_pushstring(L, "file_isreg: expected one argument");
142 		lua_error(L);
143 	}
144 
145 	if (!lua_isstring(L, 1)) {
146 		lua_pushstring(L, "file_isreg: argument has to be a string");
147 		lua_error(L);
148 	}
149 
150 	if (-1 == stat(lua_tostring(L, 1), &st)) {
151 		lua_pushnil(L);
152 		return 1;
153 	}
154 
155 	lua_pushnumber(L, S_ISREG(st.st_mode));
156 
157 	return 1;
158 }
159 
f_file_isdir(lua_State * L)160 int f_file_isdir(lua_State *L) {
161 	struct stat st;
162 	int n = lua_gettop(L);
163 
164 	if (n != 1) {
165 		lua_pushstring(L, "file_isreg: expected one argument");
166 		lua_error(L);
167 	}
168 
169 	if (!lua_isstring(L, 1)) {
170 		lua_pushstring(L, "file_isreg: argument has to be a string");
171 		lua_error(L);
172 	}
173 
174 	if (-1 == stat(lua_tostring(L, 1), &st)) {
175 		lua_pushnil(L);
176 		return 1;
177 	}
178 
179 	lua_pushnumber(L, S_ISDIR(st.st_mode));
180 
181 	return 1;
182 }
183 
184 
185 
186 #ifdef HAVE_MEMCACHE_H
f_memcache_exists(lua_State * L)187 int f_memcache_exists(lua_State *L) {
188 	char *r;
189 	int n = lua_gettop(L);
190 	struct memcache *mc;
191 
192 	if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
193 		lua_pushstring(L, "where is my userdata ?");
194 		lua_error(L);
195 	}
196 
197 	mc = lua_touserdata(L, lua_upvalueindex(1));
198 
199 	if (n != 1) {
200 		lua_pushstring(L, "expected one argument");
201 		lua_error(L);
202 	}
203 
204 	if (!lua_isstring(L, 1)) {
205 		lua_pushstring(L, "argument has to be a string");
206 		lua_error(L);
207 	}
208 
209 	if (NULL == (r = mc_aget(mc,
210 				 (char*) lua_tostring(L, 1), lua_strlen(L, 1)))) {
211 
212 		lua_pushboolean(L, 0);
213 		return 1;
214 	}
215 
216 	free(r);
217 
218 	lua_pushboolean(L, 1);
219 	return 1;
220 }
221 
f_memcache_get_string(lua_State * L)222 int f_memcache_get_string(lua_State *L) {
223 	char *r;
224 	int n = lua_gettop(L);
225 
226 	struct memcache *mc;
227 
228 	if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
229 		lua_pushstring(L, "where is my userdata ?");
230 		lua_error(L);
231 	}
232 
233 	mc = lua_touserdata(L, lua_upvalueindex(1));
234 
235 
236 	if (n != 1) {
237 		lua_pushstring(L, "expected one argument");
238 		lua_error(L);
239 	}
240 
241 	if (!lua_isstring(L, 1)) {
242 		lua_pushstring(L, "argument has to be a string");
243 		lua_error(L);
244 	}
245 
246 	if (NULL == (r = mc_aget(mc,
247 				 (char*) lua_tostring(L, 1), lua_strlen(L, 1)))) {
248 		lua_pushnil(L);
249 		return 1;
250 	}
251 
252 	lua_pushstring(L, r);
253 
254 	free(r);
255 
256 	return 1;
257 }
258 
f_memcache_get_long(lua_State * L)259 int f_memcache_get_long(lua_State *L) {
260 	char *r;
261 	int n = lua_gettop(L);
262 
263 	struct memcache *mc;
264 
265 	if (!lua_islightuserdata(L, lua_upvalueindex(1))) {
266 		lua_pushstring(L, "where is my userdata ?");
267 		lua_error(L);
268 	}
269 
270 	mc = lua_touserdata(L, lua_upvalueindex(1));
271 
272 
273 	if (n != 1) {
274 		lua_pushstring(L, "expected one argument");
275 		lua_error(L);
276 	}
277 
278 	if (!lua_isstring(L, 1)) {
279 		lua_pushstring(L, "argument has to be a string");
280 		lua_error(L);
281 	}
282 
283 	if (NULL == (r = mc_aget(mc,
284 				 (char*) lua_tostring(L, 1), lua_strlen(L, 1)))) {
285 		lua_pushnil(L);
286 		return 1;
287 	}
288 
289 	lua_pushnumber(L, strtol(r, NULL, 10));
290 
291 	free(r);
292 
293 	return 1;
294 }
295 #endif
296 
297 #endif
298