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