| 1 | /**************************************************************************
|
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> *
|
|---|
| 3 | * *
|
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp *
|
|---|
| 5 | * *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by *
|
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or *
|
|---|
| 9 | * (at your option) any later version. *
|
|---|
| 10 | * *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|---|
| 14 | * GNU General Public License for more details. *
|
|---|
| 15 | * *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License *
|
|---|
| 17 | * along with this program; if not, write to the *
|
|---|
| 18 | * Free Software Foundation, Inc., *
|
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|---|
| 20 | ***************************************************************************/
|
|---|
| 21 | #include "stdafx.h"
|
|---|
| 22 | #include "NRPEServer.h"
|
|---|
| 23 | #include <strEx.h>
|
|---|
| 24 | #include <time.h>
|
|---|
| 25 | //#include <config.h>
|
|---|
| 26 | #include "handler_impl.hpp"
|
|---|
| 27 |
|
|---|
| 28 | #include <settings/client/settings_client.hpp>
|
|---|
| 29 |
|
|---|
| 30 | namespace sh = nscapi::settings_helper;
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | NRPEListener::NRPEListener() : info_(boost::shared_ptr<nrpe::server::handler>(new handler_impl(1024))) {
|
|---|
| 34 | }
|
|---|
| 35 | NRPEListener::~NRPEListener() {}
|
|---|
| 36 |
|
|---|
| 37 | bool NRPEListener::loadModule() {
|
|---|
| 38 | return false;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | bool NRPEListener::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
|
|---|
| 42 |
|
|---|
| 43 | /*
|
|---|
| 44 | DEFINE_SETTING_S(ALLOWED_HOSTS, NRPE_SECTION_PROTOCOL, GENERIC_KEY_ALLOWED_HOSTS, "");
|
|---|
| 45 | DESCRIBE_SETTING(ALLOWED_HOSTS, "ALLOWED HOST ADDRESSES", "This is a comma-delimited list of IP address of hosts that are allowed to talk to NSClient deamon. If you leave this blank the global version will be used instead.");
|
|---|
| 46 |
|
|---|
| 47 | DEFINE_SETTING_B(CACHE_ALLOWED, NRPE_SECTION_PROTOCOL, GENERIC_KEY_SOCK_CACHE_ALLOWED, false);
|
|---|
| 48 | DESCRIBE_SETTING_ADVANCED(CACHE_ALLOWED, "ALLOWED HOSTS CACHING", "Used to cache looked up hosts if you check dynamic/changing hosts set this to false.");
|
|---|
| 49 | */
|
|---|
| 50 | try {
|
|---|
| 51 |
|
|---|
| 52 | sh::settings_registry settings(get_settings_proxy());
|
|---|
| 53 | settings.set_alias(_T("NRPE"), alias, _T("server"));
|
|---|
| 54 |
|
|---|
| 55 | settings.alias().add_path_to_settings()
|
|---|
| 56 | (_T("NRPE SERVER SECTION"), _T("Section for NRPE (NRPEListener.dll) (check_nrpe) protocol options."))
|
|---|
| 57 | ;
|
|---|
| 58 |
|
|---|
| 59 | settings.alias().add_key_to_settings()
|
|---|
| 60 | (_T("port"), sh::uint_key(&info_.port, 5666),
|
|---|
| 61 | _T("PORT NUMBER"), _T("Port to use for NRPE."))
|
|---|
| 62 |
|
|---|
| 63 | (_T("payload length"), sh::int_fun_key<unsigned int>(boost::bind(&nrpe::server::handler::set_payload_length, info_.request_handler, _1), 1024),
|
|---|
| 64 | _T("PAYLOAD LENGTH"), _T("Length of payload to/from the NRPE agent. This is a hard specific value so you have to \"configure\" (read recompile) your NRPE agent to use the same value for it to work."))
|
|---|
| 65 |
|
|---|
| 66 | (_T("allow arguments"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_arguments, info_.request_handler, _1), false),
|
|---|
| 67 | _T("COMMAND ARGUMENT PROCESSING"), _T("This option determines whether or not the we will allow clients to specify arguments to commands that are executed."))
|
|---|
| 68 |
|
|---|
| 69 | (_T("allow nasty characters"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_nasty_arguments, info_.request_handler, _1), false),
|
|---|
| 70 | _T("COMMAND ALLOW NASTY META CHARS"), _T("This option determines whether or not the we will allow clients to specify nasty (as in |`&><'\"\\[]{}) characters in arguments."))
|
|---|
| 71 |
|
|---|
| 72 | (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_perf_data, info_.request_handler, _1), true),
|
|---|
| 73 | _T("PERFORMANCE DATA"), _T("Send performance data back to nagios (set this to 0 to remove all performance data)."))
|
|---|
| 74 |
|
|---|
| 75 | ;
|
|---|
| 76 |
|
|---|
| 77 | settings.alias().add_parent(_T("/settings/default")).add_key_to_settings()
|
|---|
| 78 |
|
|---|
| 79 | (_T("thread pool"), sh::uint_key(&info_.thread_pool_size, 10),
|
|---|
| 80 | _T("THREAD POOL"), _T(""))
|
|---|
| 81 |
|
|---|
| 82 | (_T("bind to"), sh::string_key(&info_.address),
|
|---|
| 83 | _T("BIND TO ADDRESS"), _T("Allows you to bind server to a specific local address. This has to be a dotted ip address not a host name. Leaving this blank will bind to all available IP addresses."))
|
|---|
| 84 |
|
|---|
| 85 | (_T("socket queue size"), sh::int_key(&info_.back_log, 0),
|
|---|
| 86 | _T("LISTEN QUEUE"), _T("Number of sockets to queue before starting to refuse new incoming connections. This can be used to tweak the amount of simultaneous sockets that the server accepts."))
|
|---|
| 87 |
|
|---|
| 88 | (_T("allowed hosts"), sh::string_fun_key<std::wstring>(boost::bind(&socket_helpers::allowed_hosts_manager::set_source, &info_.allowed_hosts, _1), _T("127.0.0.1")),
|
|---|
| 89 | _T("ALLOWED HOSTS"), _T("A comaseparated list of allowed hosts. You can use netmasks (/ syntax) or * to create ranges."))
|
|---|
| 90 |
|
|---|
| 91 | (_T("cache allowed hosts"), sh::bool_key(&info_.allowed_hosts.cached, true),
|
|---|
| 92 | _T("CACHE ALLOWED HOSTS"), _T("If hostnames should be cached, improves speed and security somewhat but wont allow you to have dynamic IPs for your nagios server."))
|
|---|
| 93 |
|
|---|
| 94 | (_T("timeout"), sh::uint_key(&info_.timeout, 30),
|
|---|
| 95 | _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived within this time we will bail out."))
|
|---|
| 96 |
|
|---|
| 97 | (_T("use ssl"), sh::bool_key(&info_.use_ssl, true),
|
|---|
| 98 | _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled."))
|
|---|
| 99 |
|
|---|
| 100 | (_T("certificate"), sh::wpath_key(&info_.certificate, _T("${certificate-path}/nrpe_dh_512.pem")),
|
|---|
| 101 | _T("SSL CERTIFICATE"), _T(""))
|
|---|
| 102 |
|
|---|
| 103 | ;
|
|---|
| 104 |
|
|---|
| 105 | settings.register_all();
|
|---|
| 106 | settings.notify();
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | #ifndef USE_SSL
|
|---|
| 110 | if (info_.use_ssl) {
|
|---|
| 111 | NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)"));
|
|---|
| 112 | }
|
|---|
| 113 | #endif
|
|---|
| 114 | if (info_.request_handler->get_payload_length() != 1024)
|
|---|
| 115 | NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nrpe changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(info_.request_handler->get_payload_length()));
|
|---|
| 116 | if (!boost::filesystem::is_regular(info_.certificate))
|
|---|
| 117 | NSC_LOG_ERROR_STD(_T("Certificate not found: ") + info_.certificate);
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | std::list<std::string> errors;
|
|---|
| 121 | info_.allowed_hosts.refresh(errors);
|
|---|
| 122 | BOOST_FOREACH(const std::string &e, errors) {
|
|---|
| 123 | NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(e));
|
|---|
| 124 | }
|
|---|
| 125 | NSC_DEBUG_MSG_STD(_T("Allowed hosts definition: ") + info_.allowed_hosts.to_wstring());
|
|---|
| 126 |
|
|---|
| 127 | boost::asio::io_service io_service_;
|
|---|
| 128 |
|
|---|
| 129 | if (mode == NSCAPI::normalStart) {
|
|---|
| 130 | if (info_.use_ssl) {
|
|---|
| 131 | #ifdef USE_SSL
|
|---|
| 132 | server_.reset(new nrpe::server::server(info_));
|
|---|
| 133 | #else
|
|---|
| 134 | NSC_LOG_ERROR_STD(_T("SSL is not supported (not compiled with openssl)"));
|
|---|
| 135 | return false;
|
|---|
| 136 | #endif
|
|---|
| 137 | } else {
|
|---|
| 138 | server_.reset(new nrpe::server::server(info_));
|
|---|
| 139 | }
|
|---|
| 140 | if (!server_) {
|
|---|
| 141 | NSC_LOG_ERROR_STD(_T("Failed to create server instance!"));
|
|---|
| 142 | return false;
|
|---|
| 143 | }
|
|---|
| 144 | server_->start();
|
|---|
| 145 | }
|
|---|
| 146 | } catch (nrpe::server::nrpe_exception &e) {
|
|---|
| 147 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.what());
|
|---|
| 148 | return false;
|
|---|
| 149 | } catch (std::exception &e) {
|
|---|
| 150 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what()));
|
|---|
| 151 | return false;
|
|---|
| 152 | } catch (...) {
|
|---|
| 153 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>"));
|
|---|
| 154 | return false;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | return true;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | bool NRPEListener::unloadModule() {
|
|---|
| 162 | try {
|
|---|
| 163 | if (server_) {
|
|---|
| 164 | server_->stop();
|
|---|
| 165 | server_.reset();
|
|---|
| 166 | }
|
|---|
| 167 | } catch (...) {
|
|---|
| 168 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN>"));
|
|---|
| 169 | return false;
|
|---|
| 170 | }
|
|---|
| 171 | return true;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 | bool NRPEListener::hasCommandHandler() {
|
|---|
| 176 | return false;
|
|---|
| 177 | }
|
|---|
| 178 | bool NRPEListener::hasMessageHandler() {
|
|---|
| 179 | return false;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | NSC_WRAP_DLL();
|
|---|
| 183 | NSC_WRAPPERS_MAIN_DEF(NRPEListener);
|
|---|
| 184 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 185 | NSC_WRAPPERS_IGNORE_CMD_DEF();
|
|---|