| 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 "handler_impl.hpp"
|
|---|
| 26 |
|
|---|
| 27 | #include <settings/client/settings_client.hpp>
|
|---|
| 28 |
|
|---|
| 29 | namespace sh = nscapi::settings_helper;
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | NRPEServer::NRPEServer() : handler_(new handler_impl(1024)) {
|
|---|
| 33 | }
|
|---|
| 34 | NRPEServer::~NRPEServer() {}
|
|---|
| 35 |
|
|---|
| 36 | bool NRPEServer::loadModule() {
|
|---|
| 37 | return false;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | bool NRPEServer::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) {
|
|---|
| 41 | try {
|
|---|
| 42 |
|
|---|
| 43 | sh::settings_registry settings(get_settings_proxy());
|
|---|
| 44 | settings.set_alias(_T("NRPE"), alias, _T("server"));
|
|---|
| 45 |
|
|---|
| 46 | settings.alias().add_path_to_settings()
|
|---|
| 47 | (_T("NRPE SERVER SECTION"), _T("Section for NRPE (NRPEServer.dll) (check_nrpe) protocol options."))
|
|---|
| 48 | ;
|
|---|
| 49 |
|
|---|
| 50 | settings.alias().add_key_to_settings()
|
|---|
| 51 | (_T("port"), sh::uint_key(&info_.port, 5666),
|
|---|
| 52 | _T("PORT NUMBER"), _T("Port to use for NRPE."))
|
|---|
| 53 |
|
|---|
| 54 | (_T("payload length"), sh::int_fun_key<unsigned int>(boost::bind(&nrpe::server::handler::set_payload_length, handler_, _1), 1024),
|
|---|
| 55 | _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."), true)
|
|---|
| 56 |
|
|---|
| 57 | (_T("allow arguments"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_arguments, handler_, _1), false),
|
|---|
| 58 | _T("COMMAND ARGUMENT PROCESSING"), _T("This option determines whether or not the we will allow clients to specify arguments to commands that are executed."))
|
|---|
| 59 |
|
|---|
| 60 | (_T("allow nasty characters"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_allow_nasty_arguments, handler_, _1), false),
|
|---|
| 61 | _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."))
|
|---|
| 62 |
|
|---|
| 63 | (_T("performance data"), sh::bool_fun_key<bool>(boost::bind(&nrpe::server::handler::set_perf_data, handler_, _1), true),
|
|---|
| 64 | _T("PERFORMANCE DATA"), _T("Send performance data back to nagios (set this to 0 to remove all performance data)."), true)
|
|---|
| 65 |
|
|---|
| 66 | ;
|
|---|
| 67 |
|
|---|
| 68 | settings.alias().add_parent(_T("/settings/default")).add_key_to_settings()
|
|---|
| 69 |
|
|---|
| 70 | (_T("thread pool"), sh::uint_key(&info_.thread_pool_size, 10),
|
|---|
| 71 | _T("THREAD POOL"), _T(""), true)
|
|---|
| 72 |
|
|---|
| 73 | (_T("bind to"), sh::string_key(&info_.address),
|
|---|
| 74 | _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."), true)
|
|---|
| 75 |
|
|---|
| 76 | (_T("socket queue size"), sh::int_key(&info_.back_log, 0),
|
|---|
| 77 | _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."), true)
|
|---|
| 78 |
|
|---|
| 79 | (_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")),
|
|---|
| 80 | _T("ALLOWED HOSTS"), _T("A comaseparated list of allowed hosts. You can use netmasks (/ syntax) or * to create ranges."))
|
|---|
| 81 |
|
|---|
| 82 | (_T("cache allowed hosts"), sh::bool_key(&info_.allowed_hosts.cached, true),
|
|---|
| 83 | _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."))
|
|---|
| 84 |
|
|---|
| 85 | (_T("timeout"), sh::uint_key(&info_.timeout, 30),
|
|---|
| 86 | _T("TIMEOUT"), _T("Timeout when reading packets on incoming sockets. If the data has not arrived within this time we will bail out."))
|
|---|
| 87 |
|
|---|
| 88 | (_T("use ssl"), sh::bool_key(&info_.ssl.enabled, false),
|
|---|
| 89 | _T("ENABLE SSL ENCRYPTION"), _T("This option controls if SSL should be enabled."), true)
|
|---|
| 90 |
|
|---|
| 91 | (_T("certificate"), sh::path_key(&info_.ssl.dh_key, "${certificate-path}/nrpe_dh_512.pem"),
|
|---|
| 92 | _T("DH KEY"), _T(""), true)
|
|---|
| 93 |
|
|---|
| 94 | (_T("certificate"), sh::path_key(&info_.ssl.certificate, "${certificate-path}/certificate.pem"),
|
|---|
| 95 | _T("SSL CERTIFICATE"), _T(""), true)
|
|---|
| 96 |
|
|---|
| 97 | (_T("certificate key"), sh::path_key(&info_.ssl.certificate_key, "${certificate-path}/certificate_key.pem"),
|
|---|
| 98 | _T("SSL CERTIFICATE"), _T(""), true)
|
|---|
| 99 |
|
|---|
| 100 | (_T("certificate format"), sh::string_key(&info_.ssl.certificate_format, "PEM"),
|
|---|
| 101 | _T("CERTIFICATE FORMAT"), _T(""), true)
|
|---|
| 102 |
|
|---|
| 103 | (_T("ca"), sh::path_key(&info_.ssl.ca_path, "${certificate-path}/ca.pem"),
|
|---|
| 104 | _T("CA"), _T(""), true)
|
|---|
| 105 |
|
|---|
| 106 | (_T("allowed ciphers"), sh::string_key(&info_.ssl.allowed_ciphers, "ADH"),
|
|---|
| 107 | _T("ALLOWED CIPHERS"), _T("A better value is: ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"), true)
|
|---|
| 108 |
|
|---|
| 109 | (_T("verify mode"), sh::string_key(&info_.ssl.verify_mode, "none"),
|
|---|
| 110 | _T("VERIFY MODE"), _T(""), true)
|
|---|
| 111 | ;
|
|---|
| 112 |
|
|---|
| 113 | settings.register_all();
|
|---|
| 114 | settings.notify();
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | #ifndef USE_SSL
|
|---|
| 118 | if (info_.use_ssl) {
|
|---|
| 119 | NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)"));
|
|---|
| 120 | return false;
|
|---|
| 121 | }
|
|---|
| 122 | #endif
|
|---|
| 123 | if (handler_->get_payload_length() != 1024)
|
|---|
| 124 | NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nrpe changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(handler_->get_payload_length()));
|
|---|
| 125 | NSC_LOG_ERROR_LISTW(info_.validate());
|
|---|
| 126 |
|
|---|
| 127 | std::list<std::string> errors;
|
|---|
| 128 | info_.allowed_hosts.refresh(errors);
|
|---|
| 129 | NSC_LOG_ERROR_LISTS(errors);
|
|---|
| 130 | NSC_DEBUG_MSG_STD(_T("Allowed hosts definition: ") + info_.allowed_hosts.to_wstring());
|
|---|
| 131 |
|
|---|
| 132 | boost::asio::io_service io_service_;
|
|---|
| 133 |
|
|---|
| 134 | if (mode == NSCAPI::normalStart) {
|
|---|
| 135 | server_.reset(new nrpe::server::server(boost::shared_ptr<nrpe::read_protocol>(new nrpe::read_protocol(info_, handler_))));
|
|---|
| 136 | if (!server_) {
|
|---|
| 137 | NSC_LOG_ERROR_STD(_T("Failed to create server instance!"));
|
|---|
| 138 | return false;
|
|---|
| 139 | }
|
|---|
| 140 | server_->start();
|
|---|
| 141 | }
|
|---|
| 142 | } catch (std::exception &e) {
|
|---|
| 143 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what()));
|
|---|
| 144 | return false;
|
|---|
| 145 | } catch (...) {
|
|---|
| 146 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>"));
|
|---|
| 147 | return false;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | return true;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | bool NRPEServer::unloadModule() {
|
|---|
| 155 | try {
|
|---|
| 156 | if (server_) {
|
|---|
| 157 | server_->stop();
|
|---|
| 158 | server_.reset();
|
|---|
| 159 | }
|
|---|
| 160 | } catch (...) {
|
|---|
| 161 | NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN>"));
|
|---|
| 162 | return false;
|
|---|
| 163 | }
|
|---|
| 164 | return true;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | bool NRPEServer::hasCommandHandler() {
|
|---|
| 169 | return false;
|
|---|
| 170 | }
|
|---|
| 171 | bool NRPEServer::hasMessageHandler() {
|
|---|
| 172 | return false;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | NSC_WRAP_DLL();
|
|---|
| 176 | NSC_WRAPPERS_MAIN_DEF(NRPEServer);
|
|---|
| 177 | NSC_WRAPPERS_IGNORE_MSG_DEF();
|
|---|
| 178 | NSC_WRAPPERS_IGNORE_CMD_DEF();
|
|---|