#include #include #ifdef BUILD_PROD #include #else #define LUA_OK 0 #endif #include #include #include #include "smr.h" #include "libkore.h" #include "libcrypto.h" #include #include int home(struct http_request *); int post_story(struct http_request *); int edit_story(struct http_request *); int edit_bio(struct http_request *); int read_story(struct http_request *); int login(struct http_request *); int logout(struct http_request *); int claim(struct http_request *); int download(struct http_request *); int preview(struct http_request *); int search(struct http_request *); int archive(struct http_request *); int style(struct http_request *); int miligram(struct http_request *); int do_lua(struct http_request *req, const char *name); int errhandeler(lua_State *); lua_State *L; /* static / index static / _post post static / _edit edit dynamic / .* read static / _login login static / _claim claim */ /*Allow seccomp things for luajit and sqlite*/ KORE_SECCOMP_FILTER("app", KORE_SYSCALL_ALLOW(pread64), KORE_SYSCALL_ALLOW(pwrite64), KORE_SYSCALL_ALLOW(fdatasync), KORE_SYSCALL_ALLOW(unlinkat), KORE_SYSCALL_ALLOW(mremap), KORE_SYSCALL_ALLOW(newfstatat), KORE_SYSCALL_ALLOW(getdents64), KORE_SYSCALL_ALLOW(gettimeofday), KORE_SYSCALL_ALLOW(fchown) ); int errhandeler(lua_State *L){ printf("Error: %s\n",lua_tostring(L,1));//"error" lua_getglobal(L,"debug");//"error",{debug} lua_getglobal(L,"print");//"error",{debug},print() lua_getfield(L,-2,"traceback");//"error",{debug},print(),traceback() lua_call(L,0,1);//"error",{debug},print(),"traceback" lua_call(L,1,0);//"error",{debug} printf("Called print()\n"); lua_getfield(L,-1,"traceback");//"error",{debug},traceback() printf("got traceback\n"); lua_call(L,0,1);//"error",{debug},"traceback" lua_pushstring(L,"\n"); printf("called traceback\n"); lua_pushvalue(L,-4);//"error",{debug},"traceback","error" printf("pushed error\n"); lua_concat(L,3);//"error",{debug},"traceback .. error" printf("concated\n"); int ref = luaL_ref(L,LUA_REGISTRYINDEX);//"error",{debug} lua_pop(L,2);// lua_rawgeti(L,LUA_REGISTRYINDEX,ref);//"traceback .. error" return 1; } int do_lua(struct http_request *req, const char *name){ printf("About to do lua %s\n",name); lua_pushcfunction(L,errhandeler); lua_getglobal(L,name);//err(),name() if(!lua_isfunction(L,-1)){ printf("Could not find a method named %s\n",name); lua_pop(L,2);// return (KORE_RESULT_OK); } lua_pushlightuserdata(L,req);//err,name(),ud_req int err = lua_pcall(L,1,0,-3); if(err != LUA_OK){ size_t retlen; const char *ret = lua_tolstring(L,-1,&retlen); printf("Failed to run %s: %s\n",name,lua_tostring(L,-1)); http_response(req, 500, ret, retlen); lua_pop(L,lua_gettop(L)); return (KORE_RESULT_OK); } return KORE_RESULT_OK; } int post_story(struct http_request *req){ printf("We want to post!\n"); return do_lua(req,"paste"); } int edit_story(struct http_request *req){ printf("We want to edit!\n"); return do_lua(req,"edit"); } int edit_bio(struct http_request *req){ printf("We want to edit bio!\n"); return do_lua(req,"edit_bio"); } int read_story(struct http_request *req){ printf("We want to read!\n"); return do_lua(req,"read"); } int login(struct http_request *req){ printf("We want to login!\n"); return do_lua(req,"login"); } int logout(struct http_request *req){ printf("We want to log out!\n"); return do_lua(req,"logout"); } int claim(struct http_request *req){ printf("We want to claim!\n"); return do_lua(req,"claim"); } int download(struct http_request *req){ printf("We want to do download!\n"); return do_lua(req,"download"); } int preview(struct http_request *req){ printf("We want to do preview!\n"); return do_lua(req,"preview"); } int search(struct http_request *req){ printf("We want to do search!\n"); return do_lua(req,"search"); } int archive(struct http_request *req){ /* struct kore_fileref *ref = kore_fileref_get("data/archive.zip",1); if(ref != NULL){ http_response_fileref(ref,HTTP_STATUS_OK,ref); kore_fileref_release(ref); return KORE_RESULT_OK; }else{ char msg[] = "Failed to create file ref"; http_response(req,200,msg,strlen(msg)); return KORE_RESULT_OK; } */ printf("We want to do archive!\n"); return do_lua(req,"archive"); } int home(struct http_request *req){ return do_lua(req,"home"); } void kore_worker_configure(void){ printf("Configuring worker...\n"); int err; /*DIR *dp;*/ /*struct dirent *ep;*/ /*dp = opendir("./");*/ /*if(dp != NULL){*/ /*while(ep = readdir(dp)){*/ /*printf("%s\n",ep->d_name);*/ /*}*/ /*closedir(dp);*/ /*}*/ L = luaL_newstate(); luaL_openlibs(L); load_kore_libs(L); load_crypto_libs(L); lua_pushcfunction(L,errhandeler); printf("About to run loadfile...\n"); luaL_loadfile(L,SM_INIT); printf("Done running loadfile...\n"); if(!lua_isfunction(L,-1)){//failed to loadfile() printf("Failed to load %s: %s\n",SM_INIT,lua_tostring(L,-1)); lua_pop(L,1); return; } printf("About to pcall\n"); err = lua_pcall(L,0,LUA_MULTRET,-2); printf("Done pcalling\n"); //err = luaL_dofile(L,"init.lua"); if(err){ printf("Failed to run %s\n",SM_INIT); return; } lua_pushcfunction(L,errhandeler); lua_getglobal(L,"configure"); err = lua_pcall(L,0,0,-2); if(err != LUA_OK){ printf("Failed to run configure(): %s\n",lua_tostring(L,-1)); lua_pop(L,2); return; } lua_pop(L,lua_gettop(L)); printf("Finished configuring worker\n"); } void kore_worker_teardown(void){ int err; lua_pushcfunction(L,errhandeler); lua_getglobal(L,"teardown"); err = lua_pcall(L,0,0,1); if(err != LUA_OK){ printf("Failed to run configure(): %s\n",lua_tostring(L,-1)); lua_pop(L,2); return; } lua_close(L); }