diff --git a/include/LuaEngine.h b/include/LuaEngine.h index c8ddb28a4f..282e2ba0d2 100644 --- a/include/LuaEngine.h +++ b/include/LuaEngine.h @@ -90,7 +90,7 @@ class LuaEngine { static void luaRegister(lua_State *L, const ntop_class_reg *reg); static void luaRegisterInternalRegs(lua_State *L); - bool setUserInterface(lua_State *L, const char * const user, char * const if_name, ssize_t if_name_len); + bool getUserInterface(const char * const user, char * const if_name, ssize_t if_name_len, bool *is_allowed); }; /** diff --git a/include/ntop_typedefs.h b/include/ntop_typedefs.h index 1725b99b95..4405c1ee69 100644 --- a/include/ntop_typedefs.h +++ b/include/ntop_typedefs.h @@ -440,7 +440,7 @@ typedef struct { class SNMP; /* Forward */ struct ntopngLuaContext { - char *ifname, *user; + char *allowed_ifname, *user; void *zmq_context, *zmq_subscriber; struct mg_connection *conn; AddressTree *allowedNets; diff --git a/src/LuaEngine.cpp b/src/LuaEngine.cpp index 85d7da1017..494d0168c1 100644 --- a/src/LuaEngine.cpp +++ b/src/LuaEngine.cpp @@ -273,7 +273,7 @@ static int ntop_set_active_interface_id(lua_State* vm) { // ***API*** static int ntop_get_interface_names(lua_State* vm) { - char *allowed_ifname = getLuaVMUserdata(vm, ifname); + char *allowed_ifname = getLuaVMUserdata(vm, allowed_ifname); lua_newtable(vm); @@ -8174,7 +8174,7 @@ void LuaEngine::purifyHTTPParameter(char *param) { /* ****************************************** */ -bool LuaEngine::setUserInterface(lua_State *L, const char * const user, char * const if_name, ssize_t if_name_len) { +bool LuaEngine::getUserInterface(const char * const user, char * const if_name, ssize_t if_name_len, bool *is_allowed) { NetworkInterface *iface = NULL; char key[CONST_MAX_LEN_REDIS_KEY]; const char * cur_user = user; @@ -8184,17 +8184,19 @@ bool LuaEngine::setUserInterface(lua_State *L, const char * const user, char * c cur_user = NTOP_NOLOGIN_USER; if_name[0] = '\0'; + if(is_allowed) *is_allowed = false; snprintf(key, sizeof(key), CONST_STR_USER_ALLOWED_IFNAME, cur_user); res = ntop->getRedis()->get(key, if_name, if_name_len); /* First check if there's an allowed interface for the user ... */ if(if_name[0] != '\0') { + if(is_allowed) *is_allowed = true; + if(!ntop->isExistingInterface(if_name)) { ntop->getTrace()->traceEvent(TRACE_ERROR, "Interface %s not existing for user %s", if_name, cur_user); return false; /* Cannot serve the request as the allowed interface isn't instantiated */ } else { - getLuaVMUservalue(L,ifname) = if_name; ntop->getTrace()->traceEvent(TRACE_DEBUG, "Setting %s as allowed interface for user %s", if_name, cur_user); } @@ -8217,14 +8219,14 @@ bool LuaEngine::setUserInterface(lua_State *L, const char * const user, char * c /* If here there was a preferred interface but we still make sure it exists and it is instantiated in ntopng before setting it. */ } else { - iface = ntop->getNetworkInterface(L, if_name); + iface = ntop->getNetworkInterface(NULL, if_name); if(!iface) goto set_first_interface; else ntop->getTrace()->traceEvent(TRACE_DEBUG, "Using cached %s interface for user %s", if_name, cur_user); } - lua_push_str_table_entry(L, "ifname", if_name); + return true; } @@ -8323,6 +8325,7 @@ int LuaEngine::handle_script_request(struct mg_connection *conn, const struct mg_request_info *request_info, char *script_path, bool *attack_attempt) { char buf[64], key[64], ifname[MAX_INTERFACE_NAME_LEN]; + bool is_allowed_interface; char *_cookies, user[64] = { '\0' }; AddressTree ptree; int rc, post_data_len; @@ -8443,15 +8446,16 @@ int LuaEngine::handle_script_request(struct mg_connection *conn, lua_push_str_table_entry(L, "user", user); mg_get_cookie(conn, "session", buf, sizeof(buf)); lua_push_str_table_entry(L, "session", buf); - + // now it's time to set the interface. - if(!setUserInterface(L, user, ifname, sizeof(ifname))) { + if(!getUserInterface(user, ifname, sizeof(ifname), &is_allowed_interface)) { return(send_error(conn, 401 /* Unauthorized */, "Unauthorized to see the requested interface", PAGE_ERROR, script_path, "Authenticated user is not authorized to see any interface. Try clearing cookies and login with another user.")); - } + } else + lua_push_str_table_entry(L, "ifname", ifname); lua_setglobal(L, "_SESSION"); /* Like in php */ @@ -8480,6 +8484,10 @@ int LuaEngine::handle_script_request(struct mg_connection *conn, } + if(is_allowed_interface) + getLuaVMUservalue(L, allowed_ifname) = ifname; + getLuaVMUservalue(L, iface) = ntop->getNetworkInterface(NULL, ifname); + #ifndef NTOPNG_PRO rc = luaL_dofile(L, script_path); #else diff --git a/src/Ntop.cpp b/src/Ntop.cpp index 4242f4d4ff..2375b0a769 100644 --- a/src/Ntop.cpp +++ b/src/Ntop.cpp @@ -940,7 +940,7 @@ bool Ntop::isInterfaceAllowed(lua_State* vm, const char *ifname) const { if(vm == NULL || ifname == NULL) return true; /* Always return true when no lua state is passed */ - allowed_ifname = getLuaVMUserdata(vm, ifname); + allowed_ifname = getLuaVMUserdata(vm, allowed_ifname); if((allowed_ifname == NULL) || (allowed_ifname[0] == '\0')) { ntop->getTrace()->traceEvent(TRACE_DEBUG,