char* b64_decode(char *b64_string, enum B64_CHARSET cs) { static char *lookup_url=NULL; static char *lookup_def=NULL; char *data, *lookup; int i,len, buf, byte; if(lookup_url==NULL) //First function call { lookup_url=calloc(1,256); lookup_def=calloc(1,256); sighndlr_add(sighndlr_free,lookup_def); sighndlr_add(sighndlr_free,lookup_url); for(i=0; i<64; i++) { data=B64_URL_CHARS; lookup_url[(int)data[i]]=(char)i; data=B64_DEFAULT_CHARS; lookup_def[(int)data[i]]=(char)i; } } len= strlen(b64_string); data=calloc(1, len+1); switch(cs) { case B64_URL: lookup=lookup_url; break; case B64_DEFAULT: //Use default if unknown default: lookup=lookup_def; break; } for(i=0, byte=0; i<len; i+=4) { buf = (lookup[(uint8_t)(b64_string[i+0])])<< 18; buf|= (lookup[(uint8_t)(b64_string[i+1])])<< 12; buf|= (lookup[(uint8_t)(b64_string[i+2])])<< 6; buf|= (lookup[(uint8_t)(b64_string[i+3])])<< 0; data[byte++]=(buf>>16)&0xff; data[byte++]=(buf>> 8)&0xff; data[byte++]=(buf>> 0)&0xff; } return data; }
int login_init_module(int id) { Modules[id].id=id; Modules[id].name="Account"; Modules[id].func=login_request; Modules[id].description="Account settings"; struct login_data* md=Modules[id].data=malloc(sizeof(struct login_data)); pthread_mutex_init(&md->search_lock, NULL); pthread_mutex_lock(&md->search_lock); md->search = init_searchtree(); if(md->search == NULL) { pthread_mutex_unlock(&md->search_lock); return 1; } pthread_mutex_unlock(&md->search_lock); //~ parse_logins(md); unsigned char *demo_user = (unsigned char *)"*****@*****.**"; unsigned char *demo_pw = (unsigned char *)"toor"; generate_user(md, demo_user, demo_pw); md->site="<h1>Account</h1>"; sighndlr_add(login_close_module, md); return 0; }
int init_module(struct module_info *m) { log_write(LOG_INFO, "Ticket module intialized"); strncpy(&m->name[0], "Ticket", 80); m->func=answer_request; struct module_data* md=m->data=malloc(sizeof(struct module_data)); sighndlr_add(close_module, md); return 0; }
int init_module(int id, struct module_info *m) { char buf[64]; sprintf(buf, "Dummy module intialized at %d", id); log_write(buf, LOG_INFO); m->id=id; m->name="DUMMY"; m->func=answer_request; m->description="Just an example."; struct module_data* md=m->data=malloc(sizeof(struct module_data)); sighndlr_add(close_module, md); return 0; }
char* sec_path(char *new_path) { static char *path=NULL; if(path==NULL) { sighndlr_add(sighndlr_free, path); } if(new_path!=NULL) { nfree(path); path=strdup(new_path); } else if(path==NULL) { path=realpath("./resources/www", NULL); } return path; }
void load_module(const char *varName) { void *lib_handle; int (*fn)(struct module_info*); char *error; //Script or Binary if(strend(".lua", varName)==0) { lua_State *L=NULL; L=(lua_State*)luaL_newstate(); luaL_openlibs(L); log_write(LOG_INFO, "Loading %s", varName); if(L!=NULL && luaL_loadfile(L, varName)==0 && lua_pcall(L, 0, 0, 0)==0) { struct module_info *module=malloc(sizeof(struct module_info)); lua_getglobal(L,"ModuleName"); if(!lua_isnil(L,-1)) { strncpy(module->name,lua_tostring(L,-1), 79); } else { strncpy(module->name,varName, 79); } lua_pop(L, 1); module->data=malloc(sizeof(struct lua_data)); module->func=lua_answer_request; ((struct lua_data*)module->data)->L=L; pthread_mutex_init(&((struct lua_data*)module->data)->lua_lock,NULL); dict_set_elem(strdup(module->name),module,&Modules, &ModulesLock); sighndlr_add(lua_unload, module->data); } else { if(L!=NULL) lua_close(L); log_write(LOG_ERR, "Invalid LUA script %s", varName); } } else { struct module_info *module=malloc(sizeof(struct module_info)); dlerror(); lib_handle = dlopen(varName, RTLD_NOW); if(!lib_handle) { nfree(module); log_write(LOG_ERR, "Failed at %s: %s", varName, dlerror()); return; } dlerror(); fn = (int(*)(struct module_info *))dlsym(lib_handle, "init_module"); if((error = dlerror()) != NULL) { nfree(module); dlclose(lib_handle); log_write(LOG_ERR, "Failed at %s: %s", varName, error); return; } (*fn)(module); dict_set_elem(module->name,module,&Modules, &ModulesLock); sighndlr_add(dl_unload, lib_handle); } }