Exemple #1
0
bool CheckExternalScripts::commandLineExec(const int target_mode, const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message) {
	std::string command = request.command();
	if (command == "ext-scr" && request.arguments_size() > 0)
		command = request.arguments(0);
	else if (command.empty() && target_mode == NSCAPI::target_module && request.arguments_size() > 0)
		command = request.arguments(0);
	else if (command.empty() && target_mode == NSCAPI::target_module)
		command = "help";
	try {
		if (command == "help") {
			nscapi::protobuf::functions::set_response_bad(*response, "Usage: nscp ext-scr [add|list|show|install|delete] --help");
			return true;
		} else {
			if (!provider_) {
				nscapi::protobuf::functions::set_response_bad(*response, "Failed to create provider");
			}
			extscr_cli client(provider_);
			return client.run(command, request, response);
		}
	} catch (const std::exception &e) {
		nscapi::protobuf::functions::set_response_bad(*response, "Error: " + utf8::utf8_from_native(e.what()));
		return true;
	} catch (...) {
		nscapi::protobuf::functions::set_response_bad(*response, "Error: ");
		return true;
	}
	return false;
}
bool CheckExternalScripts::commandLineExec(const int target_mode, const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message) {
	std::string command = request.command();
	if (command == "ext-scr" && request.arguments_size() > 0)
		command = request.arguments(0);
	else if (command.empty() && target_mode == NSCAPI::target_module && request.arguments_size() > 0)
		command = request.arguments(0);
	else if (command.empty() && target_mode == NSCAPI::target_module)
		command = "help";
	try {
		if (command == "add")
			add_script(request, response);
		else if (command == "install")
			configure(request, response);
		else if (command == "list")
			list(request, response);
		else if (command == "help") {
			nscapi::protobuf::functions::set_response_bad(*response, "Usage: nscp ext-scr [add|list|install] --help");
		} else
			return false;
		return true;
	} catch (const std::exception &e) {
		nscapi::protobuf::functions::set_response_bad(*response, "Error: " + utf8::utf8_from_native(e.what()));
	} catch (...) {
		nscapi::protobuf::functions::set_response_bad(*response, "Error: ");
	}
	return false;
}
Exemple #3
0
void extscr_cli::list(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response) {
	po::variables_map vm;
	po::options_description desc;
	bool json = false, query = false, lib = false;

	desc.add_options()
		("help", "Show help.")

#ifdef HAVE_JSON_SPIRIT
		("json", po::bool_switch(&json),
			"Return the list in json format.")
#endif
		("query", po::bool_switch(&query),
			"List queries instead of scripts (for aliases).")
		("include-lib", po::bool_switch(&lib),
			"Do not ignore any lib folders.")

		;

	try {
		npo::basic_command_line_parser cmd(request);
		cmd.options(desc);

		po::parsed_options parsed = cmd.run();
		po::store(parsed, vm);
		po::notify(vm);
	} catch (const std::exception &e) {
		return npo::invalid_syntax(desc, request.command(), "Invalid command line: " + utf8::utf8_from_native(e.what()), *response);
	}

	if (vm.count("help")) {
		nscapi::protobuf::functions::set_response_good(*response, npo::help(desc));
		return;
	}
	std::string resp;
#ifdef HAVE_JSON_SPIRIT
	json_spirit::Array data;
#endif
	if (query) {
		Plugin::RegistryRequestMessage rrm;
		Plugin::RegistryResponseMessage response;
		Plugin::RegistryRequestMessage::Request *payload = rrm.add_payload();
		payload->mutable_inventory()->set_fetch_all(true);
		payload->mutable_inventory()->add_type(Plugin::Registry_ItemType_QUERY);
		std::string pb_response;
		provider_->get_core()->registry_query(rrm.SerializeAsString(), pb_response);
		response.ParseFromString(pb_response);
		BOOST_FOREACH(const ::Plugin::RegistryResponseMessage_Response &p, response.payload()) {
			BOOST_FOREACH(const ::Plugin::RegistryResponseMessage_Response_Inventory &i, p.inventory()) {
				if (json) {
#ifdef HAVE_JSON_SPIRIT
					data.push_back(i.name());
#endif
				} else {
					resp += i.name() + "\n";
				}
			}
		}
	} else {
Exemple #4
0
void lua::lua_runtime::on_exec(std::string command, script_information *information, lua::lua_traits::function_type function, bool simple, const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message)
{
	lua_wrapper lua(prep_function(information, function));
	int args = 2;
	if (function.object_ref != 0)
		args = 3;
	if (simple) {
		std::list<std::string> argslist;
		for (int i=0;i<request.arguments_size();i++)
			argslist.push_back(request.arguments(i));
		lua.push_string(command);
		lua.push_array(argslist);
		if (lua.pcall(args, 3, 0) != 0)
			return nscapi::protobuf::functions::set_response_bad(*response, "Failed to handle command: " + command + ": " + lua.pop_string());
		NSCAPI::nagiosReturn ret = NSCAPI::returnUNKNOWN;
		if (lua.size() < 3) {
			NSC_LOG_ERROR_STD("Invalid return: " + lua.dump_stack());
			nscapi::protobuf::functions::append_simple_exec_response_payload(response, command, NSCAPI::returnUNKNOWN, "Invalid return");
			return;
		}
		std::string msg, perf;
		msg = lua.pop_string();
		ret = lua.pop_code();
		lua.gc(LUA_GCCOLLECT, 0);
		nscapi::protobuf::functions::append_simple_exec_response_payload(response, command, ret, msg);
	} else {
		lua.push_string(command);
		lua.push_raw_string(request.SerializeAsString());
		lua.push_raw_string(request_message.SerializeAsString());
		args++;
		if (lua.pcall(args, 1, 0) != 0)
			return nscapi::protobuf::functions::set_response_bad(*response, "Failed to handle command: " + command + ": " + lua.pop_string());
		if (lua.size() < 1) {
			NSC_LOG_ERROR_STD("Invalid return: " + lua.dump_stack());
			nscapi::protobuf::functions::append_simple_exec_response_payload(response, command, NSCAPI::returnUNKNOWN, "Invalid return data");
			return;
		}
		Plugin::QueryResponseMessage local_response;
		std::string data = lua.pop_raw_string();
		response->ParseFromString(data);
		lua.gc(LUA_GCCOLLECT, 0);
	}
}
Exemple #5
0
bool LUAScript::commandLineExec(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message) {
	if (request.command() != "lua-script" && request.command() != "lua-run"
		&& request.command() != "run" && request.command() != "execute" && request.command() != "") {
			boost::optional<scripts::command_definition<lua::lua_traits> > cmd = scripts_->find_command(scripts::nscp::tags::simple_exec_tag, request.command());
			if (cmd) {
				lua_runtime_->on_exec(request.command(), cmd->information, cmd->function, true, request, response, request_message);
			}
		return false;
	}

	try {
		po::options_description desc = nscapi::program_options::create_desc(request);
		std::string file;
		desc.add_options()
			("script", po::value<std::string>(&file), "The script to run")
			("file", po::value<std::string>(&file), "The script to run")
			;
		boost::program_options::variables_map vm;
		nscapi::program_options::unrecognized_map script_options;
		if (!nscapi::program_options::process_arguments_unrecognized(vm, script_options, desc, request, *response))
			return true;

		boost::optional<boost::filesystem::path> ofile = lua::lua_script::find_script(root_, file);
		if (!ofile) {
			nscapi::protobuf::functions::set_response_bad(*response, "Script not found: " + file);
			return true;
		}
		scripts::script_information<lua::lua_traits> *instance = scripts_->add_and_load("exec", (*ofile).string());
		lua_runtime_->exec_main(instance, script_options, response);
		return true;
	} catch (const std::exception &e) {
		nscapi::protobuf::functions::set_response_bad(*response, "Failed to execute script " + utf8::utf8_from_native(e.what()));
		return true;
	} catch (...) {
		nscapi::protobuf::functions::set_response_bad(*response, "Failed to execute script.");
		return true;
	}
}
Exemple #6
0
bool CheckExternalScripts::commandLineExec(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message) {
	try {
		if (request.arguments_size() > 0 && request.arguments(0) == "add")
			add_script(request, response);
		else if (request.arguments_size() > 0 && request.arguments(0) == "install")
			configure(request, response);
		else if (request.arguments_size() > 0 && request.arguments(0) == "help") {
			nscapi::protobuf::functions::set_response_bad(*response, "Usage: nscp ext-scr add --help");
		} else 
			return false;
	} catch (const std::exception &e) {
		nscapi::protobuf::functions::set_response_bad(*response, "Error: " + utf8::utf8_from_native(e.what()));
	} catch (...) {
		nscapi::protobuf::functions::set_response_bad(*response, "Error: ");
	}
	return true;
}
bool CheckMKClient::commandLineExec(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message) {
	client::configuration config(command_prefix, boost::shared_ptr<clp_handler_impl>(new clp_handler_impl(this)), boost::shared_ptr<target_handler>(new target_handler(targets)));
	setup(config, request_message.header());
	return commands.parse_exec(command_prefix, default_command, request.command(), config, request, *response, request_message);
}
Exemple #8
0
bool NRPEClient::commandLineExec(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message) {
	client::configuration config(nrpe_client::command_prefix, 
		boost::shared_ptr<nrpe_client::clp_handler_impl>(new nrpe_client::clp_handler_impl(boost::shared_ptr<socket_helpers::client::client_handler>(new client_handler()))), 
		boost::shared_ptr<nrpe_client::target_handler>(new nrpe_client::target_handler(targets)));
	nrpe_client::setup(config, request_message.header());
	return commands.parse_exec(nrpe_client::command_prefix, nrpe_client::default_command, request.command(), config, request, *response, request_message);
}
Exemple #9
0
bool SMTPClient::commandLineExec(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response, const Plugin::ExecuteRequestMessage &request_message) {
	client::configuration config(command_prefix);
	setup(config, request_message.header());
	return commands.parse_exec(command_prefix, default_command, request.command(), config, request, *response, request_message);
}
Exemple #10
0
void CheckExternalScripts::configure(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response) {

	namespace po = boost::program_options;
	namespace pf = nscapi::protobuf::functions;
	po::variables_map vm;
	po::options_description desc;
	std::string arguments = "false";
	const std::string path = "/settings/external scripts/server";

	pf::settings_query q(get_id());
	q.get(path, "allow arguments", false);
	q.get(path, "allow nasty characters", false);


	get_core()->settings_query(q.request(), q.response());
	if (!q.validate_response()) {
		nscapi::protobuf::functions::set_response_bad(*response, q.get_response_error());
		return;
	}
	BOOST_FOREACH(const pf::settings_query::key_values &val, q.get_query_key_response()) {
		if (val.path == path && val.key && *val.key == "allow arguments" && val.get_bool())
			arguments = "true";
		else if (val.path == path && val.key && *val.key == "allow nasty characters" && val.get_bool())
			arguments = "safe";
	}
	desc.add_options()
		("help", "Show help.")

		("arguments", po::value<std::string>(&arguments)->default_value(arguments)->implicit_value("safe"), 
		"Allow arguments. false=don't allow, safe=allow non escape chars, all=allow all arguments.")

		;

	try {
		nscapi::program_options::basic_command_line_parser cmd(request);
		cmd.options(desc);

		po::parsed_options parsed = cmd.run();
		po::store(parsed, vm);
		po::notify(vm);
	} catch (const std::exception &e) {
		return nscapi::program_options::invalid_syntax(desc, request.command(), "Invalid command line: " + utf8::utf8_from_native(e.what()), *response);
	}

	if (vm.count("help")) {
		nscapi::protobuf::functions::set_response_good(*response, nscapi::program_options::help(desc));
		return;
	}
	std::stringstream result;

	nscapi::protobuf::functions::settings_query s(get_id());
	s.set(MAIN_MODULES_SECTION, "CheckExternalScripts", "enabled");
	if (arguments == "all" || arguments == "unsafe") {
		result << "UNSAFE Arguments are allowed." << std::endl;
		s.set(path, "allow arguments", "true");
		s.set(path, "allow nasty characters", "true");
	} else if (arguments == "safe" || arguments == "true") {
		result << "SAFE Arguments are allowed." << std::endl;
		s.set(path, "allow arguments", "true");
		s.set(path, "allow nasty characters", "false");
	} else {
		result << "Arguments are NOT allowed." << std::endl;
		s.set(path, "allow arguments", "false");
		s.set(path, "allow nasty characters", "false");
	}
	s.save();
	get_core()->settings_query(s.request(), s.response());
	if (!s.validate_response())
		nscapi::protobuf::functions::set_response_bad(*response, s.get_response_error());
	else
		nscapi::protobuf::functions::set_response_good(*response, result.str());
}
Exemple #11
0
void CheckExternalScripts::add_script(const Plugin::ExecuteRequestMessage::Request &request, Plugin::ExecuteResponseMessage::Response *response) {

	namespace po = boost::program_options;
	namespace pf = nscapi::protobuf::functions;
	po::variables_map vm;
	po::options_description desc;
	std::string script, arguments, alias;
	bool wrapped;

	desc.add_options()
		("help", "Show help.")

		("script", po::value<std::string>(&script), 
		"Script to add")

		("alias", po::value<std::string>(&alias), 
		"Name of command to execute script (defaults to basename of script)")

		("arguments", po::value<std::string>(&arguments), 
		"Arguments for script.")

		("wrapped", po::bool_switch(&wrapped), 
		"Add this to add a wrapped script such as ps1, vbs or similar..")

		;

	try {
		nscapi::program_options::basic_command_line_parser cmd(request);
		cmd.options(desc);

		po::parsed_options parsed = cmd.run();
		po::store(parsed, vm);
		po::notify(vm);
	} catch (const std::exception &e) {
			return nscapi::program_options::invalid_syntax(desc, request.command(), "Invalid command line: " + utf8::utf8_from_native(e.what()), *response);
	}

	if (vm.count("help")) {
		nscapi::protobuf::functions::set_response_good(*response, nscapi::program_options::help(desc));
		return;
	}
	boost::filesystem::path file = get_core()->expand_path(script);
	if (!wrapped) {
		if (!boost::filesystem::is_regular(file)) {
			nscapi::protobuf::functions::set_response_bad(*response, "Script not found: " + file.string());
			return;
		}
	}
	if (alias.empty()) {
		alias = boost::filesystem::basename(file.filename());
	}

	nscapi::protobuf::functions::settings_query s(get_id());
	if (!wrapped)
		s.set("/settings/external scripts/scripts", alias, script + " " + arguments);
	else
		s.set("/settings/external scripts/wrapped scripts", alias, script + " " + arguments);
	s.set(MAIN_MODULES_SECTION, "CheckExternalScripts", "enabled");
	s.save();
	get_core()->settings_query(s.request(), s.response());
	if (!s.validate_response()) {
		nscapi::protobuf::functions::set_response_bad(*response, s.get_response_error());
		return;
	}
	std::string actual = "";
	if (wrapped)
		actual = "\nActual command is: " + generate_wrapped_command(script + " " + arguments);
	nscapi::protobuf::functions::set_response_good(*response, "Added " + alias + " as " + script + actual);
}