Changeset 9b3f53c in nscp
- Timestamp:
- 12/06/09 22:23:10 (3 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 79e734f
- Parents:
- dcd90b2
- Files:
-
- 9 added
- 12 edited
-
CMakeLists.txt (modified) (1 diff)
-
include/NSCHelper.cpp (modified) (1 diff)
-
include/Socket.h (modified) (1 diff)
-
include/nrpe/nrpepacket.cpp (added)
-
include/nrpe/nrpepacket.hpp (modified) (2 diffs)
-
include/socket_helpers.hpp (added)
-
include/types.hpp (modified) (1 diff)
-
modules/NRPEClient/CMakeLists.txt (modified) (3 diffs)
-
modules/NRPEClient/NRPEClient.cpp (modified) (6 diffs)
-
modules/NRPEClient/NRPEClient.h (modified) (2 diffs)
-
modules/NRPEListener/NRPEListener.cpp (modified) (8 diffs)
-
modules/NRPEListener/NRPEListener.h (modified) (5 diffs)
-
modules/NRPEListener/nrpe_connection.cpp (added)
-
modules/NRPEListener/nrpe_connection.hpp (added)
-
modules/NRPEListener/nrpe_handler.cpp (added)
-
modules/NRPEListener/nrpe_handler.hpp (added)
-
modules/NRPEListener/nrpe_parser.hpp (added)
-
modules/NRPEListener/nrpe_server.cpp (added)
-
modules/NRPEListener/nrpe_server.hpp (added)
-
modules/NRPEListener/stdafx.h (modified) (2 diffs)
-
service/NSClient++.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
CMakeLists.txt
rdcd90b2 r9b3f53c 108 108 109 109 110 SET(NSCP_GLOBAL_DEFINES ${NSCP_GLOBAL_DEFINES} -DUNICODE -D_UNICODE )110 SET(NSCP_GLOBAL_DEFINES ${NSCP_GLOBAL_DEFINES} -DUNICODE -D_UNICODE -D_WIN32_WINNT=0x0400) 111 111 112 112 IF(CMAKE_HOST_UNIX) -
include/NSCHelper.cpp
r01a278b r9b3f53c 441 441 } 442 442 namespace NSCModuleHelper { 443 void settings_register_key(std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, intadvanced) {443 void settings_register_key(std::wstring path, std::wstring key, NSCAPI::settings_type type, std::wstring title, std::wstring description, std::wstring defaultValue, bool advanced) { 444 444 if (!fNSAPISettingsRegKey) 445 445 throw NSCMHExcpetion(_T("NSCore has not been initiated...")); -
include/Socket.h
r358cda1 r9b3f53c 723 723 724 724 725 namespace socketHelpers {726 class allowedHosts {727 struct host_record {728 host_record() : mask(0) {}729 host_record(std::wstring r) : mask(0), record(r) {}730 std::wstring record;731 std::wstring host;732 u_long in_addr;733 unsigned long mask;734 };735 public:736 typedef std::list<host_record> host_list;737 private:738 host_list allowedHosts_;739 bool cachedAddresses_;740 public:741 allowedHosts() : cachedAddresses_(true) {}742 743 unsigned int lookupMask(std::wstring mask) {744 unsigned int masklen = 32;745 if (!mask.empty()) {746 std::wstring::size_type pos = mask.find_first_of(_T("0123456789"));747 if (pos != std::wstring::npos) {748 masklen = strEx::stoi(mask.substr(pos));749 }750 }751 if (masklen > 32)752 masklen = 32;753 return (~((unsigned int)0))>>(32-masklen);754 }755 void lookupList() {756 for (host_list::iterator it = allowedHosts_.begin();it!=allowedHosts_.end();++it) {757 std::wstring record = (*it).record;758 if (record.length() > 0) {759 try {760 std::wstring::size_type pos = record.find('/');761 if (pos == std::wstring::npos) {762 (*it).host = record;763 (*it).mask = lookupMask(_T(""));764 } else {765 (*it).host = record.substr(0, pos);766 (*it).mask = lookupMask(record.substr(pos));767 }768 if ((!(*it).host.empty()) && isalpha((*it).host[0]))769 (*it).in_addr = simpleSocket::Socket::getHostByNameAsIN((*it).host).S_un.S_addr;770 else771 (*it).in_addr = ::inet_addr(strEx::wstring_to_string((*it).host).c_str()); // simpleSocket::Socket::getHostByAddrAsIN((*it).host);772 /*773 std::cerr << "Added: "774 + simpleSocket::Socket::inet_ntoa((*it).in_addr)775 + " with mask "776 + simpleSocket::Socket::inet_ntoa((*it).mask)777 + " from "778 + (*it).record <<779 std::endl;780 */781 } catch (simpleSocket::SocketException e) {782 std::wcerr << _T("Filed to lookup host: ") << e.getMessage() << std::endl;783 }784 }785 }786 }787 788 void setAllowedHosts(const std::list<std::wstring> list, bool cachedAddresses) {789 for (std::list<std::wstring>::const_iterator it = list.begin(); it != list.end(); ++it) {790 allowedHosts_.push_back(host_record(*it));791 }792 cachedAddresses_ = cachedAddresses;793 // if ((!allowedHosts.empty()) && (allowedHosts.front() == "") )794 // allowedHosts.pop_front();795 //allowedHosts_ = allowedHosts;796 lookupList();797 }798 bool matchHost(host_record allowed, struct in_addr remote) {799 /*800 if ((allowed.in_addr&allowed.mask)==(remote.S_un.S_addr&allowed.mask)) {801 std::cerr << "Matched: " << simpleSocket::Socket::inet_ntoa(allowed.in_addr) << " with " <<802 simpleSocket::Socket::inet_ntoa(remote.S_un.S_addr) << std::endl;803 }804 */805 return ((allowed.in_addr&allowed.mask)==(remote.S_un.S_addr&allowed.mask));806 }807 bool inAllowedHosts(struct in_addr remote) {808 if (allowedHosts_.empty())809 return true;810 host_list::const_iterator cit;811 if (!cachedAddresses_) {812 lookupList();813 }814 for (cit = allowedHosts_.begin();cit!=allowedHosts_.end();++cit) {815 if (matchHost((*cit), remote))816 return true;817 }818 return false;819 }820 };821 } -
include/nrpe/nrpepacket.hpp
r1e0bbec r9b3f53c 22 22 23 23 #include <types.hpp> 24 25 26 class NRPEPacket { 27 public: 28 static const short unknownPacket = 0; 29 static const short queryPacket = 1; 30 static const short responsePacket = 2; 31 static const short version2 = 2; 32 33 class NRPEPacketException { 24 #include <string> 25 #include <unicode_char.hpp> 26 #include <boost/asio/buffer.hpp> 27 28 29 namespace nrpe { 30 // this function swap the bytes of values given it's size as a template 31 // parameter (could sizeof be used?). 32 template <class T, unsigned int size> 33 inline T SwapBytes(T value) { 34 union { 35 T value; 36 char bytes[size]; 37 } in, out; 38 39 in.value = value; 40 41 for (unsigned int i = 0; i < size / 2; ++i) { 42 out.bytes[i] = in.bytes[size - 1 - i]; 43 out.bytes[size - 1 - i] = in.bytes[i]; 44 } 45 46 return out.value; 47 } 48 49 template<EEndian from, EEndian to, class T> 50 inline T EndianSwapBytes(T value) { 51 BOOST_STATIC_ASSERT(sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8); 52 BOOST_STATIC_ASSERT(boost::is_arithmetic<T>::value); 53 if (from == to) 54 return value; 55 return SwapBytes<T, sizeof(T)>(value); 56 } 57 template<class T> 58 inline T ntoh(T value) { 59 std::cout << "Swaping (in): " << value << " => " << EndianSwapBytes<EEndian::BIG_ENDIAN_ORDER, EEndian::HOST_ENDIAN_ORDER, T>(value) << std::endl; 60 return EndianSwapBytes<EEndian::BIG_ENDIAN_ORDER, EEndian::HOST_ENDIAN_ORDER, T>(value); 61 } 62 template<class T> 63 inline T hton(T value) { 64 std::cout << "Swaping (out): " << value << " => " << EndianSwapBytes<EEndian::HOST_ENDIAN_ORDER, EEndian::BIG_ENDIAN_ORDER, T>(value) << std::endl; 65 return EndianSwapBytes<EEndian::HOST_ENDIAN_ORDER, EEndian::BIG_ENDIAN_ORDER, T>(value); 66 } 67 68 69 70 class data { 71 public: 72 static const short unknownPacket = 0; 73 static const short queryPacket = 1; 74 static const short responsePacket = 2; 75 static const short version2 = 2; 76 77 typedef struct packet { 78 int16_t packet_version; 79 int16_t packet_type; 80 u_int32_t crc32_value; 81 int16_t result_code; 82 char buffer[]; 83 } packet; 84 85 }; 86 87 class length { 88 typedef unsigned int size_type; 89 static size_type payload_length_; 90 public: 91 static void set_payload_length(size_type length) { 92 payload_length_ = length; 93 } 94 static size_type get_packet_length() { 95 return get_packet_length(payload_length_); 96 } 97 static size_type get_packet_length(size_type payload_length) { 98 std::cout << "get_packet: " << sizeof(nrpe::data::packet) << ":" << payload_length << std::endl; 99 return sizeof(nrpe::data::packet)+payload_length*sizeof(char); 100 } 101 static size_type get_payload_length() { 102 return payload_length_; 103 } 104 static size_type get_payload_length(size_type packet_length) { 105 std::cout << "get_payload: " << sizeof(nrpe::data::packet) << ":" << packet_length << std::endl; 106 return (packet_length-sizeof(nrpe::data::packet))/sizeof(char); 107 } 108 }; 109 110 class nrpe_packet_exception { 34 111 std::wstring error_; 35 112 public: 36 NRPEPacketException(std::wstring error) : error_(error) {}113 nrpe_packet_exception(std::wstring error) : error_(error) {} 37 114 std::wstring getMessage() { 38 115 return error_; … … 40 117 }; 41 118 42 private: 43 typedef struct packet { 44 int16_t packet_version; 45 int16_t packet_type; 46 u_int32_t crc32_value; 47 int16_t result_code; 48 char buffer[]; 49 } packet; 50 std::wstring payload_; 51 short type_; 52 short version_; 53 NSCAPI::nagiosReturn result_; 54 unsigned int crc32_; 55 unsigned int calculatedCRC32_; 56 char *tmpBuffer; 57 unsigned int buffer_length_; 58 public: 59 NRPEPacket(unsigned int buffer_length) : tmpBuffer(NULL), buffer_length_(buffer_length) {}; 60 NRPEPacket(const char *buffer, unsigned int length, unsigned int buffer_length) : tmpBuffer(NULL), buffer_length_(buffer_length) { 61 readFrom(buffer, length); 62 }; 63 NRPEPacket(short type, short version, NSCAPI::nagiosReturn result, std::wstring payLoad, unsigned int buffer_length) 64 : tmpBuffer(NULL) 65 ,type_(type) 66 ,version_(version) 67 ,result_(result) 68 ,payload_(payLoad) 69 ,buffer_length_(buffer_length) 70 { 71 } 72 NRPEPacket() 73 : tmpBuffer(NULL) 74 ,type_(unknownPacket) 75 ,version_(version2) 76 ,result_(0) 77 ,buffer_length_(0) 78 { 79 } 80 NRPEPacket(const NRPEPacket &other) : tmpBuffer(NULL) { 81 payload_ = other.payload_; 82 type_ = other.type_; 83 version_ = other.version_; 84 result_ = other.result_; 85 crc32_ = other.crc32_; 86 calculatedCRC32_ = other.calculatedCRC32_; 87 buffer_length_ = other.buffer_length_; 88 } 89 NRPEPacket& operator=(NRPEPacket const& other) { 90 tmpBuffer=NULL; 91 payload_ = other.payload_; 92 type_ = other.type_; 93 version_ = other.version_; 94 result_ = other.result_; 95 crc32_ = other.crc32_; 96 calculatedCRC32_ = other.calculatedCRC32_; 97 buffer_length_ = other.buffer_length_; 98 return *this; 99 } 100 101 ~NRPEPacket() { 102 delete [] tmpBuffer; 103 } 104 static NRPEPacket make_request(std::wstring payload, unsigned int buffer_length) { 105 return NRPEPacket(queryPacket, version2, -1, payload, buffer_length); 106 } 107 108 const char* getBuffer() { 109 delete [] tmpBuffer; 110 tmpBuffer = new char[getBufferLength()+1]; 111 //TODO readd this ZeroMemory(tmpBuffer, getBufferLength()+1); 112 packet *p = reinterpret_cast<packet*>(tmpBuffer); 113 p->result_code = htons(NSCHelper::nagios2int(result_)); 114 p->packet_type = htons(type_); 115 p->packet_version = htons(version_); 116 if (payload_.length() >= buffer_length_-1) 117 throw NRPEPacketException(_T("To much data cant create return packet (truncate datat)")); 118 //ZeroMemory(p->buffer, buffer_length_-1); 119 strncpy(p->buffer, to_string(payload_).c_str(), payload_.length()); 120 p->buffer[payload_.length()] = 0; 121 p->crc32_value = 0; 122 p->crc32_value = htonl(calculate_crc32(tmpBuffer, getBufferLength())); 123 return tmpBuffer; 124 } 125 126 void readFrom(const char *buffer, unsigned int length) { 127 if (buffer == NULL) 128 throw NRPEPacketException(_T("No buffer.")); 129 if (length != getBufferLength()) 130 throw NRPEPacketException(_T("Invalid length: ") + strEx::itos(length) + _T(" != ") + strEx::itos(getBufferLength())); 131 const packet *p = reinterpret_cast<const packet*>(buffer); 132 type_ = ntohs(p->packet_type); 133 if ((type_ != queryPacket)&&(type_ != responsePacket)) 134 throw NRPEPacketException(_T("Invalid packet type.")); 135 version_ = ntohs(p->packet_version); 136 if (version_ != version2) 137 throw NRPEPacketException(_T("Invalid packet version.")); 138 crc32_ = ntohl(p->crc32_value); 139 // Verify CRC32 140 // @todo Fix this, currently we need a const buffer so we cannot change the CRC to 0. 141 char * tb = new char[getBufferLength()+1]; 142 memcpy(tb, buffer, getBufferLength()); 143 packet *p2 = reinterpret_cast<packet*>(tb); 144 p2->crc32_value = 0; 145 calculatedCRC32_ = calculate_crc32(tb, getBufferLength()); 146 delete [] tb; 147 if (crc32_ != calculatedCRC32_) 148 throw NRPEPacketException(_T("Invalid checksum in NRPE packet!")); 149 // Verify CRC32 end 150 result_ = NSCHelper::int2nagios(ntohs(p->result_code)); 151 payload_ = strEx::string_to_wstring(std::string(p->buffer)); 152 } 153 154 unsigned short getVersion() const { return version_; } 155 unsigned short getType() const { return type_; } 156 unsigned short getResult() const { return result_; } 157 std::wstring getPayload() const { return payload_; } 158 bool verifyCRC() { return calculatedCRC32_ == crc32_; } 159 unsigned int getBufferLength() const { return getBufferLength(buffer_length_); } 160 static unsigned int getBufferLength(unsigned int buffer_length) { return sizeof(packet)+buffer_length*sizeof(char); } 161 unsigned int getInternalBufferLength() const { return buffer_length_; } 162 163 164 std::wstring toString() { 165 std::wstringstream ss; 166 ss << _T("type: ") << type_; 167 ss << _T(", version: ") << version_; 168 ss << _T(", result: ") << result_; 169 ss << _T(", payload: ") << payload_; 170 return ss.str(); 171 } 172 173 }; 174 175 176 177 119 class packet { 120 public: 121 122 123 private: 124 std::wstring payload_; 125 short type_; 126 short version_; 127 int result_; 128 unsigned int crc32_; 129 unsigned int calculatedCRC32_; 130 char *tmpBuffer; 131 unsigned int payload_length_; 132 public: 133 packet(unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) {}; 134 packet(std::vector<char> buffer, unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) { 135 char *tmp = new char[buffer.size()+1]; 136 copy( buffer.begin(), buffer.end(), tmp); 137 readFrom(tmp, buffer.size()); 138 delete [] tmp; 139 }; 140 packet(const char *buffer, unsigned int buffer_length, unsigned int payload_length) : tmpBuffer(NULL), payload_length_(payload_length) { 141 readFrom(buffer, buffer_length); 142 }; 143 packet(short type, short version, int result, std::wstring payLoad, unsigned int payload_length) 144 : tmpBuffer(NULL) 145 ,type_(type) 146 ,version_(version) 147 ,result_(result) 148 ,payload_(payLoad) 149 ,payload_length_(payload_length) 150 { 151 } 152 packet() 153 : tmpBuffer(NULL) 154 ,type_(nrpe::data::unknownPacket) 155 ,version_(nrpe::data::version2) 156 ,result_(0) 157 ,payload_length_(nrpe::length::get_payload_length()) 158 { 159 } 160 packet(const packet &other) : tmpBuffer(NULL) { 161 payload_ = other.payload_; 162 type_ = other.type_; 163 version_ = other.version_; 164 result_ = other.result_; 165 crc32_ = other.crc32_; 166 calculatedCRC32_ = other.calculatedCRC32_; 167 payload_length_ = other.payload_length_; 168 } 169 packet& operator=(packet const& other) { 170 tmpBuffer=NULL; 171 payload_ = other.payload_; 172 type_ = other.type_; 173 version_ = other.version_; 174 result_ = other.result_; 175 crc32_ = other.crc32_; 176 calculatedCRC32_ = other.calculatedCRC32_; 177 payload_length_ = other.payload_length_; 178 return *this; 179 } 180 181 ~packet() { 182 delete [] tmpBuffer; 183 } 184 static packet make_request(std::wstring payload, unsigned int buffer_length) { 185 return packet(nrpe::data::queryPacket, nrpe::data::version2, -1, payload, buffer_length); 186 } 187 188 const char* create_buffer() { 189 delete [] tmpBuffer; 190 unsigned int packet_length = nrpe::length::get_packet_length(payload_length_); 191 tmpBuffer = new char[packet_length+1]; 192 //TODO readd this ZeroMemory(tmpBuffer, getBufferLength()+1); 193 nrpe::data::packet *p = reinterpret_cast<nrpe::data::packet*>(tmpBuffer); 194 p->result_code = nrpe::hton<int16_t>(result_); 195 p->packet_type = nrpe::hton<int16_t>(type_); 196 p->packet_version = nrpe::hton<int16_t>(version_); 197 if (payload_.length() >= payload_length_-1) 198 throw nrpe::nrpe_packet_exception(_T("To much data cant create return packet (truncate datat)")); 199 //ZeroMemory(p->buffer, payload_length_-1); 200 strncpy(p->buffer, ::to_string(payload_).c_str(), payload_.length()); 201 p->buffer[payload_.length()] = 0; 202 p->crc32_value = 0; 203 p->crc32_value = nrpe::hton<u_int32_t>(calculate_crc32(tmpBuffer, packet_length)); 204 std::wcout << _T("About to send: ") << to_string() << std::endl; 205 std::wcout << _T("About to send: ") 206 << _T("") << strEx::ihextos(tmpBuffer[0]) 207 << _T(", ") << strEx::ihextos(tmpBuffer[1]) 208 << _T(", ") << strEx::ihextos(tmpBuffer[2]) 209 << _T(", ") << strEx::ihextos(tmpBuffer[3]) 210 << std::endl; 211 return tmpBuffer; 212 } 213 214 void readFrom(const char *buffer, unsigned int length) { 215 std::wcout << _T("Just read: ") 216 << _T("") << strEx::ihextos(buffer[0]) 217 << _T(", ") << strEx::ihextos(buffer[1]) 218 << _T(", ") << strEx::ihextos(buffer[2]) 219 << _T(", ") << strEx::ihextos(buffer[3]) 220 << std::endl; 221 if (buffer == NULL) 222 throw nrpe::nrpe_packet_exception(_T("No buffer.")); 223 if (length != get_packet_length()) 224 throw nrpe::nrpe_packet_exception(_T("Invalid packet length: ") + strEx::itos(length) + _T(" != ") + strEx::itos(get_packet_length()) + _T(" configured payload is: ") + to_wstring(get_payload_length())); 225 const nrpe::data::packet *p = reinterpret_cast<const nrpe::data::packet*>(buffer); 226 type_ = nrpe::ntoh<int16_t>(p->packet_type); 227 if ((type_ != nrpe::data::queryPacket)&&(type_ != nrpe::data::responsePacket)) 228 throw nrpe::nrpe_packet_exception(_T("Invalid packet type: ") + strEx::itos(type_)); 229 version_ = nrpe::ntoh<int16_t>(p->packet_version); 230 if (version_ != nrpe::data::version2) 231 throw nrpe::nrpe_packet_exception(_T("Invalid packet version.") + strEx::itos(version_)); 232 crc32_ = nrpe::ntoh<u_int32_t>(p->crc32_value); 233 // Verify CRC32 234 // @todo Fix this, currently we need a const buffer so we cannot change the CRC to 0. 235 char * tb = new char[length+1]; 236 memcpy(tb, buffer, length); 237 nrpe::data::packet *p2 = reinterpret_cast<nrpe::data::packet*>(tb); 238 p2->crc32_value = 0; 239 calculatedCRC32_ = calculate_crc32(tb, get_packet_length()); 240 delete [] tb; 241 std::wcout << _T("Just read: ") << to_string() << std::endl; 242 if (crc32_ != calculatedCRC32_) 243 throw nrpe::nrpe_packet_exception(_T("Invalid checksum in NRPE packet: ") + strEx::ihextos(crc32_) 244 + _T("!=") + strEx::ihextos(calculatedCRC32_)); 245 // Verify CRC32 end 246 result_ = nrpe::ntoh<u_int32_t>(p->result_code); 247 payload_ = strEx::string_to_wstring(std::string(p->buffer)); 248 } 249 250 unsigned short getVersion() const { return version_; } 251 unsigned short getType() const { return type_; } 252 unsigned short getResult() const { return result_; } 253 std::wstring getPayload() const { return payload_; } 254 bool verifyCRC() { return calculatedCRC32_ == crc32_; } 255 unsigned int get_packet_length() const { return nrpe::length::get_packet_length(payload_length_); } 256 unsigned int get_payload_length() const { return payload_length_; } 257 258 boost::asio::const_buffer to_buffers() { 259 return boost::asio::buffer(create_buffer(), get_packet_length()); 260 } 261 std::wstring to_string() { 262 std::wstringstream ss; 263 ss << _T("type: ") << type_; 264 ss << _T(", version: ") << version_; 265 ss << _T(", result: ") << result_; 266 ss << _T(", payload: ") << payload_; 267 return ss.str(); 268 } 269 static nrpe::packet create_response(int ret, std::wstring string, int buffer_length) { 270 return packet(nrpe::data::responsePacket, nrpe::data::version2, ret, string, buffer_length); 271 } 272 }; 273 } 274 -
include/types.hpp
r1e0bbec r9b3f53c 26 26 #endif 27 27 #endif 28 29 30 #include <boost/detail/endian.hpp> 31 32 enum EEndian 33 { 34 LITTLE_ENDIAN_ORDER, 35 BIG_ENDIAN_ORDER, 36 #if defined(BOOST_LITTLE_ENDIAN) 37 HOST_ENDIAN_ORDER = LITTLE_ENDIAN_ORDER 38 #elif defined(BOOST_BIG_ENDIAN) 39 HOST_ENDIAN_ORDER = BIG_ENDIAN_ORDER 40 #else 41 #error "Impossible de determiner l'indianness du systeme cible." 42 #endif 43 }; -
modules/NRPEClient/CMakeLists.txt
rdcd90b2 r9b3f53c 11 11 ${NSCP_INCLUDE_PATH}/arrayBuffer.cpp 12 12 ${NSCP_INCLUDE_PATH}/utils.cpp 13 ${NSCP_INCLUDE_PATH}/nrpe/nrpepacket.cpp 13 14 ) 14 15 … … 18 19 SET(SRCS ${SRCS} 19 20 stdafx.h 21 "${TARGET}.h" 20 22 ${NSCP_INCLUDE_PATH}/strEx.h 21 23 ${NSCP_INCLUDE_PATH}/config.h … … 24 26 ${NSCP_INCLUDE_PATH}/NSCHelper.h 25 27 ${NSCP_INCLUDE_PATH}/nsc_module_wrapper.hpp 26 ${NSCP_INCLUDE_PATH}/utils.h27 28 ${NSCP_INCLUDE_PATH}/nrpe/nrpepacket.hpp 29 ${NSCP_INCLUDE_PATH}/socket_helpers.hpp 28 30 "${TARGET}.def" 29 31 ) -
modules/NRPEClient/NRPEClient.cpp
rdcd90b2 r9b3f53c 69 69 } 70 70 71 void NRPEClient::add_options(po::options_description &desc, nrpe_connection_data command_data) {71 void NRPEClient::add_options(po::options_description &desc, nrpe_connection_data &command_data) { 72 72 desc.add_options() 73 73 ("help,h", "Show this help message.") … … 102 102 tokenizer_t tok(args, sep); 103 103 for(tokenizer_t::iterator beg=tok.begin(); beg!=tok.end();++beg){ 104 std::wcout << *beg << std::endl;105 104 list.push_back(*beg); 106 105 } … … 192 191 NRPEClient::nrpe_result_data NRPEClient::execute_nrpe_command(nrpe_connection_data con, std::wstring arguments) { 193 192 try { 194 NRPEPacket packet;193 nrpe::packet packet; 195 194 if (!con.no_ssl) { 196 195 #ifdef USE_SSL 197 packet = send_ssl(con.host, con.port, con.timeout, NRPEPacket::make_request(con.get_cli(arguments), con.buffer_length));196 packet = send_ssl(con.host, con.port, con.timeout, nrpe::packet::make_request(con.get_cli(arguments), con.buffer_length)); 198 197 #else 199 198 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("SSL support not available (compiled without USE_SSL)!")); 200 199 #endif 201 200 } else 202 packet = send_nossl(con.host, con.port, con.timeout, NRPEPacket::make_request(con.get_cli(arguments), con.buffer_length));201 packet = send_nossl(con.host, con.port, con.timeout, nrpe::packet::make_request(con.get_cli(arguments), con.buffer_length)); 203 202 return nrpe_result_data(packet.getResult(), packet.getPayload()); 204 } catch ( NRPEPacket::NRPEPacketException &e) {203 } catch (nrpe::nrpe_packet_exception &e) { 205 204 return nrpe_result_data(NSCAPI::returnUNKNOWN, _T("NRPE Packet errro: ") + e.getMessage()); 206 205 } catch (std::runtime_error &e) { … … 323 322 } 324 323 325 void send(NRPEPacket &packet, boost::posix_time::seconds timeout) { 326 std::vector<char> buf(packet.getBufferLength()); 327 write_with_timeout(socket_, socket_, boost::asio::buffer(packet.getBuffer(), packet.getBufferLength()), timeout); 328 } 329 NRPEPacket recv(const NRPEPacket &packet, boost::posix_time::seconds timeout) { 330 std::vector<char> buf(packet.getBufferLength()); 324 void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 325 std::vector<char> buf(packet.get_packet_length()); 326 write_with_timeout(socket_, socket_, boost::asio::buffer(packet.create_buffer(), packet.get_packet_length()), timeout); 327 } 328 nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 329 std::vector<char> buf(packet.get_packet_length()); 330 std::cout << "About to read: " << buf.size() << std::endl; 331 331 read_with_timeout(socket_, socket_, boost::asio::buffer(buf), timeout); 332 return NRPEPacket(&buf[0], buf.size(), packet.getInternalBufferLength()); 332 std::cout << "Read data: " << buf.size() << std::endl; 333 return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 333 334 } 334 335 }; … … 370 371 } 371 372 372 void send( NRPEPacket &packet, boost::posix_time::seconds timeout) {373 void send(nrpe::packet &packet, boost::posix_time::seconds timeout) { 373 374 NSC_LOG_CRITICAL(_T("Writing...")); 374 std::vector<char> buf(packet.get BufferLength());375 write_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(packet. getBuffer(), packet.getBufferLength()), timeout);375 std::vector<char> buf(packet.get_packet_length()); 376 write_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(packet.create_buffer(), packet.get_packet_length()), timeout); 376 377 NSC_LOG_CRITICAL(_T("Written...")); 377 378 } 378 NRPEPacket recv(const NRPEPacket &packet, boost::posix_time::seconds timeout) {379 nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) { 379 380 NSC_LOG_CRITICAL(_T("Reading...")); 380 std::vector<char> buf(packet.get BufferLength());381 std::vector<char> buf(packet.get_packet_length()); 381 382 read_with_timeout(socket_, socket_.lowest_layer(), boost::asio::buffer(buf), timeout); 382 return NRPEPacket(&buf[0], buf.size(), packet.getInternalBufferLength());383 return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length()); 383 384 NSC_LOG_CRITICAL(_T("Read...")); 384 385 } 385 386 }; 386 NRPEPacket NRPEClient::send_ssl(std::wstring host, int port, int timeout, NRPEPacket packet)387 nrpe::packet NRPEClient::send_ssl(std::wstring host, int port, int timeout, nrpe::packet packet) 387 388 { 388 389 boost::asio::io_service io_service; … … 397 398 #endif 398 399 399 NRPEPacket NRPEClient::send_nossl(std::wstring host, int port, int timeout, NRPEPacket packet)400 nrpe::packet NRPEClient::send_nossl(std::wstring host, int port, int timeout, nrpe::packet packet) 400 401 { 401 402 boost::asio::io_service io_service; -
modules/NRPEClient/NRPEClient.h
rdcd90b2 r9b3f53c 73 73 ss << _T(", no_ssl: ") << no_ssl; 74 74 ss << _T(", buffer_length: ") << buffer_length; 75 ss << _T(", command: ") << command; 76 ss << _T(", argument: ") << arguments; 75 77 return ss.str(); 76 78 } … … 121 123 private: 122 124 nrpe_result_data execute_nrpe_command(nrpe_connection_data con, std::wstring arguments); 123 NRPEPacket send_nossl(std::wstring host, int port, int timeout, NRPEPacket packet);124 NRPEPacket send_ssl(std::wstring host, int port, int timeout, NRPEPacket packet);125 void add_options(po::options_description &desc, nrpe_connection_data command_data);125 nrpe::packet send_nossl(std::wstring host, int port, int timeout, nrpe::packet packet); 126 nrpe::packet send_ssl(std::wstring host, int port, int timeout, nrpe::packet packet); 127 void add_options(po::options_description &desc, nrpe_connection_data &command_data); 126 128 127 129 -
modules/NRPEListener/NRPEListener.cpp
rd5356c1 r9b3f53c 37 37 } 38 38 NRPEListener::~NRPEListener() { 39 std::cout << "TERMINATING TERMINATING!!!" << std::endl; 39 40 } 40 41 … … 47 48 48 49 49 void NRPEListener::addAllScriptsFrom(std::wstring path) {50 std::wstring baseDir;51 std::wstring::size_type pos = path.find_last_of('*');52 if (pos == std::wstring::npos) {53 path += _T("*.*");54 }55 WIN32_FIND_DATA wfd;56 HANDLE hFind = FindFirstFile(path.c_str(), &wfd);57 if (hFind != INVALID_HANDLE_VALUE) {58 do {59 if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {60 addCommand(script_dir, wfd.cFileName);61 }62 } while (FindNextFile(hFind, &wfd));63 } else {64 NSC_LOG_ERROR_STD(_T("No scripts found in path: ") + path);65 return;66 }67 FindClose(hFind);68 }69 50 70 51 bool NRPEListener::loadModule(NSCAPI::moduleLoadMode mode) { 71 #ifdef USE_SSL 72 73 #else 52 #ifndef USE_SSL 74 53 if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_USE_SSL ,NRPE_SETTINGS_USE_SSL_DEFAULT)==1) { 75 54 NSC_LOG_ERROR_STD(_T("SSL not avalible! (not compiled with openssl support)")); … … 93 72 bUseSSL_ = SETTINGS_GET_BOOL(nrpe::KEYUSE_SSL)==1; 94 73 noPerfData_ = SETTINGS_GET_INT(nrpe::ALLOW_PERFDATA)==0; 95 timeout = SETTINGS_GET_INT(nrpe::CMD_TIMEOUT); 96 socketTimeout_ = SETTINGS_GET_INT(nrpe::READ_TIMEOUT); 97 scriptDirectory_ = SETTINGS_GET_STRING(nrpe::SCRIPT_PATH); 74 timeout = SETTINGS_GET_INT(nrpe::READ_TIMEOUT); 98 75 buffer_length_ = SETTINGS_GET_INT(nrpe::PAYLOAD_LENGTH); 99 allowArgs_ = SETTINGS_GET_BOOL(nrpe::ALLOW_ARGS);100 allowNasty_ = SETTINGS_GET_BOOL(nrpe::ALLOW_NASTY);101 76 if (buffer_length_ != 1024) 102 77 NSC_DEBUG_MSG_STD(_T("Non-standard buffer length (hope you have recompiled check_nrpe changing #define MAX_PACKETBUFFER_LENGTH = ") + strEx::itos(buffer_length_)); 103 78 NSC_DEBUG_MSG_STD(_T("Loading all commands (from NRPE)")); 104 std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(setting_keys::nrpe::SECTION_HANDLERS_PATH); 105 std::list<std::wstring>::const_iterator it; 106 for (it = commands.begin(); it != commands.end(); ++it) { 107 std::wstring command_name; 108 if (((*it).length() > 7)&&((*it).substr(0,7) == _T("command"))) { 109 strEx::token t = strEx::getToken((*it), '['); 110 t = strEx::getToken(t.second, ']'); 111 command_name = t.first; 112 } else { 113 command_name = (*it); 114 } 115 std::wstring s = NSCModuleHelper::getSettingsString(setting_keys::nrpe::SECTION_HANDLERS_PATH, (*it), _T("")); 116 if (command_name.empty() || s.empty()) { 117 NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it)); 118 } else { 119 if ((s.length() > 7)&&(s.substr(0,6) == _T("inject"))) { 120 addCommand(inject, command_name.c_str(), s.substr(7)); 121 } else { 122 addCommand(script, command_name.c_str(), s); 123 } 124 } 125 } 126 127 if (!scriptDirectory_.empty()) { 128 addAllScriptsFrom(scriptDirectory_); 129 } 130 131 allowedHosts.setAllowedHosts(strEx::splitEx(getAllowedHosts(), _T(",")), getCacheAllowedHosts()); 79 80 boost::asio::io_service io_service_; 81 allowedHosts.setAllowedHosts(strEx::splitEx(getAllowedHosts(), _T(",")), getCacheAllowedHosts(), io_service_); 82 NSC_DEBUG_MSG_STD(_T("Allowed hosts: ") + allowedHosts.to_string()); 132 83 try { 133 84 NSC_DEBUG_MSG_STD(_T("Starting NRPE socket...")); … … 135 86 std::wstring host = SETTINGS_GET_STRING(nrpe::BINDADDR); 136 87 unsigned int backLog = SETTINGS_GET_INT(nrpe::LISTENQUE); 88 unsigned int threadPool = 10; 137 89 if (mode == NSCAPI::normalStart) { 90 server_.reset(new nrpe::server::server(to_string(host), to_string(port), (""), threadPool)); 91 server_->start(); 138 92 #ifdef USE_SSL 139 93 if (bUseSSL_) { 140 socket_ssl_.setHandler(this);141 socket_ssl_.StartListener(host, port, backLog);94 //socket_ssl_.setHandler(this); 95 //socket_ssl_.StartListener(host, port, backLog); 142 96 } else { 143 97 #else 144 98 { 145 99 #endif 146 socket_.setHandler(this);147 socket_.StartListener(host, port, backLog);100 //socket_.setHandler(this); 101 //socket_.StartListener(host, port, backLog); 148 102 } 149 103 } 150 } catch (simpleSocket::SocketException e) {151 NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage());152 return false;153 #ifdef USE_SSL154 } catch (simpleSSL::SSLException e) {155 NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage());156 return false;157 #endif158 104 } catch (...) { 159 105 NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN EXCEPTION>")); … … 161 107 } 162 108 root_ = NSCModuleHelper::getBasePath(); 163 164 109 return true; 165 110 } … … 167 112 bool NRPEListener::unloadModule() { 168 113 try { 114 if (server_) { 115 server_->stop(); 116 server_.reset(); 117 } 169 118 #ifdef USE_SSL 170 119 if (bUseSSL_) { 171 socket_ssl_.removeHandler(this);172 if (socket_ssl_.hasListener())173 socket_ssl_.StopListener();120 //socket_ssl_.removeHandler(this); 121 //if (socket_ssl_.hasListener()) 122 // socket_ssl_.StopListener(); 174 123 } else { 175 124 #else 176 125 { 177 126 #endif 178 socket_.removeHandler(this);179 if (socket_.hasListener())180 socket_.StopListener();127 //socket_.removeHandler(this); 128 //if (socket_.hasListener()) 129 // socket_.StopListener(); 181 130 } 182 } catch ( simpleSocket::SocketException e) {183 NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage());131 } catch (...) { 132 NSC_LOG_ERROR_STD(_T("Exception caught: <UNKNOWN>")); 184 133 return false; 185 #ifdef USE_SSL186 } catch (simpleSSL::SSLException e) {187 NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage());188 return false;189 #endif190 134 } 191 135 return true; … … 194 138 195 139 bool NRPEListener::hasCommandHandler() { 196 return true;140 return false; 197 141 } 198 142 bool NRPEListener::hasMessageHandler() { … … 201 145 202 146 203 NSCAPI::nagiosReturn NRPEListener::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) { 204 command_list::const_iterator cit = commands.find(command); 205 if (cit == commands.end()) 206 return NSCAPI::returnIgnored; 207 208 const command_data cd = (*cit).second; 209 std::wstring args = cd.arguments; 210 if (allowArgs_) { 211 arrayBuffer::arrayList arr = arrayBuffer::arrayBuffer2list(argLen, char_args); 212 arrayBuffer::arrayList::const_iterator cit2 = arr.begin(); 213 int i=1; 214 215 for (;cit2!=arr.end();cit2++,i++) { 216 if (allowNasty_) { 217 if ((*cit2).find_first_of(NASTY_METACHARS) != std::wstring::npos) { 218 NSC_LOG_ERROR(_T("Request string contained illegal metachars!")); 219 return NSCAPI::returnIgnored; 220 } 221 } 222 strEx::replace(args, _T("$ARG") + strEx::itos(i) + _T("$"), (*cit2)); 223 } 224 } 225 if (cd.type == inject) { 226 strEx::token t = strEx::getToken(args, ' '); 227 std::wstring s = t.second; 228 std::wstring sTarget; 229 230 std::wstring::size_type p = 0; 231 while(true) { 232 std::wstring::size_type pStart = p; 233 std::wstring::size_type pEnd = std::wstring::npos; 234 if (s[p] == '\"') { 235 pStart++; 236 while (true) { 237 p = s.find(' ', ++p); 238 if (p == std::wstring::npos) 239 break; 240 if ((p>1)&&(s[p-1]=='\"')&&(((p>2)&&(s[p-2]!='\\'))||(p==2))) 241 break; 242 } 243 if (p != std::wstring::npos) 244 pEnd = p-1; 245 else 246 pEnd = s.length()-1; 247 if (p != std::wstring::npos) { 248 p++; 249 } 250 } else { 251 pEnd = p = s.find(' ', ++p); 252 if (p != std::wstring::npos) { 253 p = s.find_first_not_of(' ', p); 254 } 255 } 256 if (!sTarget.empty()) 257 sTarget += _T("!"); 258 if (p == std::wstring::npos) { 259 if (pEnd == std::wstring::npos) 260 sTarget += s.substr(pStart); 261 else 262 sTarget += s.substr(pStart, pEnd-pStart); 263 break; 264 } 265 sTarget += s.substr(pStart,pEnd-pStart); 266 //p++; 267 } 268 try { 269 return NSCModuleHelper::InjectSplitAndCommand(t.first, sTarget, '!', message, perf); 270 } catch (NSCModuleHelper::NSCMHExcpetion e) { 271 NSC_LOG_ERROR_STD(_T("Failed to inject command (") + command.c_str() + _T("): ") + e.msg_); 272 } catch (...) { 273 NSC_LOG_ERROR_STD(_T("Failed to inject command (") + command.c_str() + _T("): Unknown error REPORT THIS")); 274 } 275 } else if (cd.type == script) { 276 int result = process::executeProcess(root_, args, message, perf, timeout); 277 if (!NSCHelper::isNagiosReturnCode(result)) { 278 NSC_LOG_ERROR_STD(_T("The command (") + command.c_str() + _T(") returned an invalid return code: ") + strEx::itos(result)); 279 return NSCAPI::returnUNKNOWN; 280 } 281 return NSCHelper::int2nagios(result); 282 } else if (cd.type == script_dir) { 283 std::wstring args = arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" ")); 284 std::wstring cmd = scriptDirectory_ + command.c_str() + _T(" ") +args; 285 int result = process::executeProcess(root_, cmd, message, perf, timeout); 286 if (!NSCHelper::isNagiosReturnCode(result)) { 287 NSC_LOG_ERROR_STD(_T("The command (") + command.c_str() + _T(") returned an invalid return code: ") + strEx::itos(result)); 288 return NSCAPI::returnUNKNOWN; 289 } 290 return NSCHelper::int2nagios(result); 291 } else { 292 NSC_LOG_ERROR_STD(_T("Unknown script type: ") + command.c_str()); 293 return NSCAPI::critical; 294 } 295 return NSCAPI::returnIgnored; 296 } 297 298 void NRPEListener::onClose() 299 {} 300 301 void NRPEListener::onAccept(simpleSocket::Socket *client) 302 { 303 if (!allowedHosts.inAllowedHosts(client->getAddr())) { 304 NSC_LOG_ERROR(_T("Unauthorize access from: ") + client->getAddrString()); 305 client->close(); 306 return; 307 } 308 try { 309 simpleSocket::DataBuffer block; 310 int i; 311 int maxWait = socketTimeout_*10; 312 for (i=0;i<maxWait;i++) { 313 bool lastReadHasMore = false; 314 try { 315 lastReadHasMore = client->readAll(block, 1048); 316 } catch (simpleSocket::SocketException e) { 317 NSC_LOG_MESSAGE(_T("Could not read NRPE packet from socket :") + e.getMessage()); 318 client->close(); 319 return; 320 } 321 if (block.getLength() >= NRPEPacket::getBufferLength(buffer_length_)) 322 break; 323 if (!lastReadHasMore) { 324 NSC_LOG_MESSAGE(_T("Could not read a full NRPE packet from socket, only got: ") + strEx::itos(block.getLength())); 325 client->close(); 326 return; 327 } 328 Sleep(100); 329 } 330 if (i >= maxWait) { 331 NSC_LOG_ERROR_STD(_T("Timeout reading NRPE-packet (increase socket_timeout), we only got: ") + strEx::itos(block.getLength())); 332 client->close(); 333 return; 334 } 335 if (block.getLength() == NRPEPacket::getBufferLength(buffer_length_)) { 336 try { 337 NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength(), buffer_length_)); 338 block.copyFrom(out.getBuffer(), out.getBufferLength()); 339 } catch (NRPEPacket::NRPEPacketException e) { 340 NSC_LOG_ERROR_STD(_T("NRPESocketException: ") + e.getMessage()); 341 try { 342 NRPEPacket err(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("Could not construct return paket in NRPE handler check clientside (nsclient.log) logs..."), buffer_length_); 343 block.copyFrom(err.getBuffer(), err.getBufferLength()); 344 } catch (NRPEPacket::NRPEPacketException e) { 345 NSC_LOG_ERROR_STD(_T("NRPESocketException (again): ") + e.getMessage()); 346 client->close(); 347 return; 348 } 349 } 350 int maxWait = socketTimeout_*10; 351 for (i=0;i<maxWait;i++) { 352 bool lastReadHasMore = false; 353 try { 354 if (client->canWrite()) 355 lastReadHasMore = client->sendAll(block); 356 } catch (simpleSocket::SocketException e) { 357 NSC_LOG_MESSAGE(_T("Could not send NRPE packet from socket :") + e.getMessage()); 358 client->close(); 359 return; 360 } 361 if (!lastReadHasMore) { 362 client->close(); 363 return; 364 } 365 Sleep(100); 366 } 367 if (i >= maxWait) { 368 NSC_LOG_ERROR_STD(_T("Timeout reading NRPE-packet (increase socket_timeout)")); 369 client->close(); 370 return; 371 } 372 } else { 373 NSC_LOG_ERROR_STD(_T("We got more then we wanted ") + strEx::itos(NRPEPacket::getBufferLength(buffer_length_)) + _T(", we only got: ") + strEx::itos(block.getLength())); 374 client->close(); 375 return; 376 } 377 } catch (simpleSocket::SocketException e) { 378 NSC_LOG_ERROR_STD(_T("SocketException: ") + e.getMessage()); 379 } catch (NRPEException e) { 380 NSC_LOG_ERROR_STD(_T("NRPEException: ") + e.getMessage()); 381 } catch (...) { 382 NSC_LOG_ERROR_STD(_T("Unhandled Exception in NRPE listner...")); 383 } 384 client->close(); 385 } 386 387 NRPEPacket NRPEListener::handlePacket(NRPEPacket p) { 388 if (p.getType() != NRPEPacket::queryPacket) { 389 NSC_LOG_ERROR(_T("Request is not a query.")); 390 throw NRPEException(_T("Invalid query type: ") + strEx::itos(p.getType())); 391 } 392 if (p.getVersion() != NRPEPacket::version2) { 393 NSC_LOG_ERROR(_T("Request had unsupported version.")); 394 throw NRPEException(_T("Invalid version")); 395 } 396 if (!p.verifyCRC()) { 397 NSC_LOG_ERROR(_T("Request had invalid checksum.")); 398 throw NRPEException(_T("Invalid checksum")); 399 } 400 strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 401 if (cmd.first == _T("_NRPE_CHECK")) { 402 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnOK, _T("I (") + NSCModuleHelper::getApplicationVersionString() + _T(") seem to be doing fine..."), buffer_length_); 403 } 404 std::wstring msg, perf; 405 406 if (allowArgs_) { 407 if (!cmd.second.empty()) { 408 NSC_LOG_ERROR(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 409 throw NRPEException(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 410 } 411 } 412 if (allowNasty_) { 413 if (cmd.first.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 414 NSC_LOG_ERROR(_T("Request command contained illegal metachars!")); 415 throw NRPEException(_T("Request command contained illegal metachars!")); 416 } 417 if (cmd.second.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 418 NSC_LOG_ERROR(_T("Request arguments contained illegal metachars!")); 419 throw NRPEException(_T("Request command contained illegal metachars!")); 420 } 421 } 422 //TODO REMOVE THIS 423 //return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("TEST TEST TEST"), buffer_length_); 424 425 NSCAPI::nagiosReturn ret = -3; 426 try { 427 ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 428 } catch (...) { 429 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal exception"), buffer_length_); 430 } 431 switch (ret) { 432 case NSCAPI::returnInvalidBufferLen: 433 msg = _T("UNKNOWN: Return buffer to small to handle this command."); 434 ret = NSCAPI::returnUNKNOWN; 435 break; 436 case NSCAPI::returnIgnored: 437 msg = _T("UNKNOWN: No handler for that command"); 438 ret = NSCAPI::returnUNKNOWN; 439 break; 440 case NSCAPI::returnOK: 441 case NSCAPI::returnWARN: 442 case NSCAPI::returnCRIT: 443 case NSCAPI::returnUNKNOWN: 444 break; 445 default: 446 msg = _T("UNKNOWN: Internal error."); 447 ret = NSCAPI::returnUNKNOWN; 448 } 449 if (msg.length() >= buffer_length_-1) { 450 NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 451 msg = msg.substr(0,buffer_length_-2); 452 } 453 if (perf.empty()||noPerfData_) { 454 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg, buffer_length_); 455 } else { 456 return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + _T("|") + perf, buffer_length_); 457 } 458 } 147 148 149 // void NRPEListener::onAccept(simpleSocket::Socket *client) 150 // { 151 // if (!allowedHosts.inAllowedHosts(client->getAddr())) { 152 // NSC_LOG_ERROR(_T("Unauthorize access from: ") + client->getAddrString()); 153 // client->close(); 154 // return; 155 // } 156 // try { 157 // simpleSocket::DataBuffer block; 158 // int i; 159 // int maxWait = socketTimeout_*10; 160 // for (i=0;i<maxWait;i++) { 161 // bool lastReadHasMore = false; 162 // try { 163 // lastReadHasMore = client->readAll(block, 1048); 164 // } catch (simpleSocket::SocketException e) { 165 // NSC_LOG_MESSAGE(_T("Could not read NRPE packet from socket :") + e.getMessage()); 166 // client->close(); 167 // return; 168 // } 169 // if (block.getLength() >= NRPEPacket::getBufferLength(buffer_length_)) 170 // break; 171 // if (!lastReadHasMore) { 172 // NSC_LOG_MESSAGE(_T("Could not read a full NRPE packet from socket, only got: ") + strEx::itos(block.getLength())); 173 // client->close(); 174 // return; 175 // } 176 // Sleep(100); 177 // } 178 // if (i >= maxWait) { 179 // NSC_LOG_ERROR_STD(_T("Timeout reading NRPE-packet (increase socket_timeout), we only got: ") + strEx::itos(block.getLength())); 180 // client->close(); 181 // return; 182 // } 183 // if (block.getLength() == NRPEPacket::getBufferLength(buffer_length_)) { 184 // try { 185 // NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength(), buffer_length_)); 186 // block.copyFrom(out.getBuffer(), out.getBufferLength()); 187 // } catch (NRPEPacket::NRPEPacketException e) { 188 // NSC_LOG_ERROR_STD(_T("NRPESocketException: ") + e.getMessage()); 189 // try { 190 // NRPEPacket err(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("Could not construct return paket in NRPE handler check clientside (nsclient.log) logs..."), buffer_length_); 191 // block.copyFrom(err.getBuffer(), err.getBufferLength()); 192 // } catch (NRPEPacket::NRPEPacketException e) { 193 // NSC_LOG_ERROR_STD(_T("NRPESocketException (again): ") + e.getMessage()); 194 // client->close(); 195 // return; 196 // } 197 // } 198 // int maxWait = socketTimeout_*10; 199 // for (i=0;i<maxWait;i++) { 200 // bool lastReadHasMore = false; 201 // try { 202 // if (client->canWrite()) 203 // lastReadHasMore = client->sendAll(block); 204 // } catch (simpleSocket::SocketException e) { 205 // NSC_LOG_MESSAGE(_T("Could not send NRPE packet from socket :") + e.getMessage()); 206 // client->close(); 207 // return; 208 // } 209 // if (!lastReadHasMore) { 210 // client->close(); 211 // return; 212 // } 213 // Sleep(100); 214 // } 215 // if (i >= maxWait) { 216 // NSC_LOG_ERROR_STD(_T("Timeout reading NRPE-packet (increase socket_timeout)")); 217 // client->close(); 218 // return; 219 // } 220 // } else { 221 // NSC_LOG_ERROR_STD(_T("We got more then we wanted ") + strEx::itos(NRPEPacket::getBufferLength(buffer_length_)) + _T(", we only got: ") + strEx::itos(block.getLength())); 222 // client->close(); 223 // return; 224 // } 225 // } catch (simpleSocket::SocketException e) { 226 // NSC_LOG_ERROR_STD(_T("SocketException: ") + e.getMessage()); 227 // } catch (NRPEException e) { 228 // NSC_LOG_ERROR_STD(_T("NRPEException: ") + e.getMessage()); 229 // } catch (...) { 230 // NSC_LOG_ERROR_STD(_T("Unhandled Exception in NRPE listner...")); 231 // } 232 // client->close(); 233 // } 234 // 235 // NRPEPacket NRPEListener::handlePacket(NRPEPacket p) { 236 // if (p.getType() != NRPEPacket::queryPacket) { 237 // NSC_LOG_ERROR(_T("Request is not a query.")); 238 // throw NRPEException(_T("Invalid query type: ") + strEx::itos(p.getType())); 239 // } 240 // if (p.getVersion() != NRPEPacket::version2) { 241 // NSC_LOG_ERROR(_T("Request had unsupported version.")); 242 // throw NRPEException(_T("Invalid version")); 243 // } 244 // if (!p.verifyCRC()) { 245 // NSC_LOG_ERROR(_T("Request had invalid checksum.")); 246 // throw NRPEException(_T("Invalid checksum")); 247 // } 248 // strEx::token cmd = strEx::getToken(p.getPayload(), '!'); 249 // if (cmd.first == _T("_NRPE_CHECK")) { 250 // return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnOK, _T("I (") + NSCModuleHelper::getApplicationVersionString() + _T(") seem to be doing fine..."), buffer_length_); 251 // } 252 // std::wstring msg, perf; 253 // 254 // if (allowArgs_) { 255 // if (!cmd.second.empty()) { 256 // NSC_LOG_ERROR(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 257 // throw NRPEException(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); 258 // } 259 // } 260 // if (allowNasty_) { 261 // if (cmd.first.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 262 // NSC_LOG_ERROR(_T("Request command contained illegal metachars!")); 263 // throw NRPEException(_T("Request command contained illegal metachars!")); 264 // } 265 // if (cmd.second.find_first_of(NASTY_METACHARS) != std::wstring::npos) { 266 // NSC_LOG_ERROR(_T("Request arguments contained illegal metachars!")); 267 // throw NRPEException(_T("Request command contained illegal metachars!")); 268 // } 269 // } 270 // //TODO REMOVE THIS 271 // //return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("TEST TEST TEST"), buffer_length_); 272 // 273 // NSCAPI::nagiosReturn ret = -3; 274 // try { 275 // ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); 276 // } catch (...) { 277 // return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal exception"), buffer_length_); 278 // } 279 // switch (ret) { 280 // case NSCAPI::returnInvalidBufferLen: 281 // msg = _T("UNKNOWN: Return buffer to small to handle this command."); 282 // ret = NSCAPI::returnUNKNOWN; 283 // break; 284 // case NSCAPI::returnIgnored: 285 // msg = _T("UNKNOWN: No handler for that command"); 286 // ret = NSCAPI::returnUNKNOWN; 287 // break; 288 // case NSCAPI::returnOK: 289 // case NSCAPI::returnWARN: 290 // case NSCAPI::returnCRIT: 291 // case NSCAPI::returnUNKNOWN: 292 // break; 293 // default: 294 // msg = _T("UNKNOWN: Internal error."); 295 // ret = NSCAPI::returnUNKNOWN; 296 // } 297 // if (msg.length() >= buffer_length_-1) { 298 // NSC_LOG_ERROR(_T("Truncating returndata as it is bigger then NRPE allowes :(")); 299 // msg = msg.substr(0,buffer_length_-2); 300 // } 301 // if (perf.empty()||noPerfData_) { 302 // return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg, buffer_length_); 303 // } else { 304 // return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + _T("|") + perf, buffer_length_); 305 // } 306 // } 459 307 460 308 NSC_WRAPPERS_MAIN_DEF(gNRPEListener); 461 309 NSC_WRAPPERS_IGNORE_MSG_DEF(); 462 NSC_WRAPPERS_ HANDLE_CMD_DEF(gNRPEListener);310 NSC_WRAPPERS_IGNORE_CMD_DEF(); 463 311 NSC_WRAPPERS_HANDLE_CONFIGURATION(gNRPEListener); 464 312 -
modules/NRPEListener/NRPEListener.h
r5da0459 r9b3f53c 20 20 ***************************************************************************/ 21 21 22 #include <socket_helpers.hpp> 23 #include "nrpe_server.hpp" 24 22 25 NSC_WRAPPERS_MAIN(); 23 #include <Socket.h>24 #ifdef USE_SSL25 #include <SSLSocket.h>26 #endif27 #include <map>28 #include <nrpe/NRPEPacket.hpp>29 #include <execute_process.hpp>30 26 31 class NRPEListener : public simpleSocket::ListenerHandler{27 class NRPEListener { 32 28 private: 33 29 typedef enum { … … 41 37 }; 42 38 43 #ifdef USE_SSL44 bool bUseSSL_;45 simpleSSL::Listener socket_ssl_;46 #endif47 simpleSocket::Listener<> socket_;48 typedef std::map<strEx::blindstr, command_data> command_list;49 command_list commands;50 39 unsigned int timeout; 51 unsigned int socketTimeout_;52 40 socketHelpers::allowedHosts allowedHosts; 53 41 bool noPerfData_; 54 std::wstring scriptDirectory_;55 42 unsigned int buffer_length_; 56 43 std::wstring root_; 57 bool allowNasty_; 58 bool allowArgs_; 44 bool bUseSSL_; 59 45 60 46 public: … … 85 71 NSCAPI::nagiosReturn handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf); 86 72 std::wstring getConfigurationMeta(); 73 boost::shared_ptr<nrpe::server::server> server_; 87 74 88 75 private: … … 90 77 std::wstring error_; 91 78 public: 92 /* NRPESocketException(simpleSSL::SSLException e) {93 error_ = e.getMessage();94 }95 NRPEException(NRPEPacket::NRPEPacketException e) {96 error_ = e.getMessage();97 }98 */99 79 NRPEException(std::wstring s) { 100 80 error_ = s; … … 104 84 } 105 85 }; 106 107 108 private:109 void onAccept(simpleSocket::Socket *client);110 void onClose();111 112 113 NRPEPacket handlePacket(NRPEPacket p);114 int executeNRPECommand(std::wstring command, std::wstring &msg, std::wstring &perf);115 void addAllScriptsFrom(std::wstring path);116 void addCommand(command_type type, strEx::blindstr key, std::wstring args = _T("")) {117 addCommand(key, command_data(type, args));118 }119 void addCommand(strEx::blindstr key, command_data args) {120 commands[key] = args;121 }122 123 86 }; 124 87 -
modules/NRPEListener/stdafx.h
r7f9c823 r9b3f53c 27 27 #include <string> 28 28 #include <functional> 29 #include <map> 30 #include <vector> 29 31 30 32 #include <config.h> … … 34 36 #include <NSCHelper.h> 35 37 #include <nsc_module_wrapper.hpp> 38 #include <nrpe/nrpepacket.hpp> 36 39 37 #ifdef MEMCHECK 38 #include <vld.h> 40 #include <boost/thread.hpp> 41 #include <boost/bind.hpp> 42 #include <boost/shared_ptr.hpp> 43 #include <boost/array.hpp> 44 #include <boost/asio.hpp> 45 #include <boost/optional.hpp> 46 #include <boost/bind.hpp> 47 #include <boost/logic/tribool.hpp> 48 #include <boost/utility.hpp> 49 #include <boost/tuple/tuple.hpp> 50 51 #ifdef USE_SSL 52 #include <boost/asio/ssl.hpp> 39 53 #endif -
service/NSClient++.cpp
r1e0bbec r9b3f53c 1031 1031 } 1032 1032 } 1033 /* 1033 1034 for (pluginList::iterator it=plugins_.begin(); it != plugins_.end();) { 1034 1035 LOG_DEBUG_STD(_T("Loading plugin: ") + (*it)->getName() + _T("...")); … … 1044 1045 } 1045 1046 } 1047 */ 1046 1048 plugins_loaded_ = true; 1047 1049 }
Note: See TracChangeset
for help on using the changeset viewer.








