| 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 "NRPEListener.h" |
|---|
| 23 | #include <strEx.h> |
|---|
| 24 | #include <time.h> |
|---|
| 25 | #include <config.h> |
|---|
| 26 | #include "NRPEPacket.h" |
|---|
| 27 | #include <msvc_wrappers.h> |
|---|
| 28 | |
|---|
| 29 | NRPEListener gNRPEListener; |
|---|
| 30 | |
|---|
| 31 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 32 | { |
|---|
| 33 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 34 | return TRUE; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | NRPEListener::NRPEListener() : noPerfData_(false) { |
|---|
| 38 | } |
|---|
| 39 | NRPEListener::~NRPEListener() { |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | std::wstring getAllowedHosts() { |
|---|
| 43 | std::wstring ret = NSCModuleHelper::getSettingsString(NRPE_SECTION_TITLE, MAIN_ALLOWED_HOSTS, _T("")); |
|---|
| 44 | if (ret.empty()) |
|---|
| 45 | ret = NSCModuleHelper::getSettingsString(MAIN_SECTION_TITLE, MAIN_ALLOWED_HOSTS, MAIN_ALLOWED_HOSTS_DEFAULT); |
|---|
| 46 | return ret; |
|---|
| 47 | } |
|---|
| 48 | bool getCacheAllowedHosts() { |
|---|
| 49 | int val = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, MAIN_ALLOWED_HOSTS_CACHE, -1); |
|---|
| 50 | if (val == -1) |
|---|
| 51 | val = NSCModuleHelper::getSettingsInt(MAIN_SECTION_TITLE, MAIN_ALLOWED_HOSTS_CACHE, MAIN_ALLOWED_HOSTS_CACHE_DEFAULT); |
|---|
| 52 | return val==1?true:false; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | void NRPEListener::addAllScriptsFrom(std::wstring path) { |
|---|
| 57 | std::wstring baseDir; |
|---|
| 58 | std::wstring::size_type pos = path.find_last_of('*'); |
|---|
| 59 | if (pos == std::wstring::npos) { |
|---|
| 60 | path += _T("*.*"); |
|---|
| 61 | } |
|---|
| 62 | WIN32_FIND_DATA wfd; |
|---|
| 63 | HANDLE hFind = FindFirstFile(path.c_str(), &wfd); |
|---|
| 64 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 65 | do { |
|---|
| 66 | if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) { |
|---|
| 67 | addCommand(script_dir, wfd.cFileName); |
|---|
| 68 | } |
|---|
| 69 | } while (FindNextFile(hFind, &wfd)); |
|---|
| 70 | } else { |
|---|
| 71 | NSC_LOG_ERROR_STD(_T("No scripts found in path: ") + path); |
|---|
| 72 | return; |
|---|
| 73 | } |
|---|
| 74 | FindClose(hFind); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | bool NRPEListener::loadModule() { |
|---|
| 78 | bUseSSL_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_USE_SSL ,NRPE_SETTINGS_USE_SSL_DEFAULT)==1; |
|---|
| 79 | noPerfData_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_PERFDATA,NRPE_SETTINGS_PERFDATA_DEFAULT)==0; |
|---|
| 80 | timeout = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_TIMEOUT ,NRPE_SETTINGS_TIMEOUT_DEFAULT); |
|---|
| 81 | socketTimeout_ = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_READ_TIMEOUT ,NRPE_SETTINGS_READ_TIMEOUT_DEFAULT); |
|---|
| 82 | scriptDirectory_ = NSCModuleHelper::getSettingsString(NRPE_SECTION_TITLE, NRPE_SETTINGS_SCRIPTDIR ,NRPE_SETTINGS_SCRIPTDIR_DEFAULT); |
|---|
| 83 | std::list<std::wstring> commands = NSCModuleHelper::getSettingsSection(NRPE_HANDLER_SECTION_TITLE); |
|---|
| 84 | std::list<std::wstring>::const_iterator it; |
|---|
| 85 | for (it = commands.begin(); it != commands.end(); ++it) { |
|---|
| 86 | std::wstring command_name; |
|---|
| 87 | if (((*it).length() > 7)&&((*it).substr(0,7) == _T("command"))) { |
|---|
| 88 | strEx::token t = strEx::getToken((*it), '['); |
|---|
| 89 | t = strEx::getToken(t.second, ']'); |
|---|
| 90 | command_name = t.first; |
|---|
| 91 | } else { |
|---|
| 92 | command_name = (*it); |
|---|
| 93 | } |
|---|
| 94 | std::wstring s = NSCModuleHelper::getSettingsString(NRPE_HANDLER_SECTION_TITLE, (*it), _T("")); |
|---|
| 95 | if (command_name.empty() || s.empty()) { |
|---|
| 96 | NSC_LOG_ERROR_STD(_T("Invalid command definition: ") + (*it)); |
|---|
| 97 | } else { |
|---|
| 98 | if ((s.length() > 7)&&(s.substr(0,6) == _T("inject"))) { |
|---|
| 99 | addCommand(inject, command_name.c_str(), s.substr(7)); |
|---|
| 100 | } else { |
|---|
| 101 | addCommand(script, command_name.c_str(), s); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | if (!scriptDirectory_.empty()) { |
|---|
| 107 | addAllScriptsFrom(scriptDirectory_); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | allowedHosts.setAllowedHosts(strEx::splitEx(getAllowedHosts(), _T(",")), getCacheAllowedHosts()); |
|---|
| 111 | try { |
|---|
| 112 | unsigned short port = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_PORT, NRPE_SETTINGS_PORT_DEFAULT); |
|---|
| 113 | std::wstring host = NSCModuleHelper::getSettingsString(NRPE_SECTION_TITLE, NRPE_SETTINGS_BINDADDR, NRPE_SETTINGS_BINDADDR_DEFAULT); |
|---|
| 114 | unsigned int backLog = NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_LISTENQUE, NRPE_SETTINGS_LISTENQUE_DEFAULT); |
|---|
| 115 | if (bUseSSL_) { |
|---|
| 116 | socket_ssl_.setHandler(this); |
|---|
| 117 | socket_ssl_.StartListener(host, port, backLog); |
|---|
| 118 | } else { |
|---|
| 119 | socket_.setHandler(this); |
|---|
| 120 | socket_.StartListener(host, port, backLog); |
|---|
| 121 | } |
|---|
| 122 | } catch (simpleSocket::SocketException e) { |
|---|
| 123 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage()); |
|---|
| 124 | return false; |
|---|
| 125 | } catch (simpleSSL::SSLException e) { |
|---|
| 126 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage()); |
|---|
| 127 | return false; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | return true; |
|---|
| 131 | } |
|---|
| 132 | bool NRPEListener::unloadModule() { |
|---|
| 133 | try { |
|---|
| 134 | if (bUseSSL_) { |
|---|
| 135 | socket_ssl_.removeHandler(this); |
|---|
| 136 | if (socket_ssl_.hasListener()) |
|---|
| 137 | socket_ssl_.StopListener(); |
|---|
| 138 | } else { |
|---|
| 139 | socket_.removeHandler(this); |
|---|
| 140 | if (socket_.hasListener()) |
|---|
| 141 | socket_.StopListener(); |
|---|
| 142 | } |
|---|
| 143 | } catch (simpleSocket::SocketException e) { |
|---|
| 144 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage()); |
|---|
| 145 | return false; |
|---|
| 146 | } catch (simpleSSL::SSLException e) { |
|---|
| 147 | NSC_LOG_ERROR_STD(_T("Exception caught: ") + e.getMessage()); |
|---|
| 148 | return false; |
|---|
| 149 | } |
|---|
| 150 | return true; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | bool NRPEListener::hasCommandHandler() { |
|---|
| 155 | return true; |
|---|
| 156 | } |
|---|
| 157 | bool NRPEListener::hasMessageHandler() { |
|---|
| 158 | return false; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | NSCAPI::nagiosReturn NRPEListener::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) { |
|---|
| 163 | command_list::const_iterator cit = commands.find(command); |
|---|
| 164 | if (cit == commands.end()) |
|---|
| 165 | return NSCAPI::returnIgnored; |
|---|
| 166 | |
|---|
| 167 | const command_data cd = (*cit).second; |
|---|
| 168 | std::wstring args = cd.arguments; |
|---|
| 169 | if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, NRPE_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 1) { |
|---|
| 170 | arrayBuffer::arrayList arr = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 171 | arrayBuffer::arrayList::const_iterator cit2 = arr.begin(); |
|---|
| 172 | int i=1; |
|---|
| 173 | |
|---|
| 174 | for (;cit2!=arr.end();cit2++,i++) { |
|---|
| 175 | if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, NRPE_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { |
|---|
| 176 | if ((*cit2).find_first_of(NASTY_METACHARS) != std::wstring::npos) { |
|---|
| 177 | NSC_LOG_ERROR(_T("Request string contained illegal metachars!")); |
|---|
| 178 | return NSCAPI::returnIgnored; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | strEx::replace(args, _T("$ARG") + strEx::itos(i) + _T("$"), (*cit2)); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | if (cd.type == inject) { |
|---|
| 185 | strEx::token t = strEx::getToken(args, ' '); |
|---|
| 186 | std::wstring s = t.second; |
|---|
| 187 | std::wstring sTarget; |
|---|
| 188 | |
|---|
| 189 | std::wstring::size_type p = 0; |
|---|
| 190 | while(true) { |
|---|
| 191 | std::wstring::size_type pStart = p; |
|---|
| 192 | std::wstring::size_type pEnd = std::wstring::npos; |
|---|
| 193 | if (s[p] == '\"') { |
|---|
| 194 | pStart++; |
|---|
| 195 | while (true) { |
|---|
| 196 | p = s.find(' ', ++p); |
|---|
| 197 | if (p == std::wstring::npos) |
|---|
| 198 | break; |
|---|
| 199 | if ((p>1)&&(s[p-1]=='\"')&&(((p>2)&&(s[p-2]!='\\'))||(p==2))) |
|---|
| 200 | break; |
|---|
| 201 | } |
|---|
| 202 | if (p != std::wstring::npos) |
|---|
| 203 | pEnd = p-1; |
|---|
| 204 | else |
|---|
| 205 | pEnd = s.length()-1; |
|---|
| 206 | if (p != std::wstring::npos) { |
|---|
| 207 | p++; |
|---|
| 208 | } |
|---|
| 209 | } else { |
|---|
| 210 | pEnd = p = s.find(' ', ++p); |
|---|
| 211 | if (p != std::wstring::npos) { |
|---|
| 212 | p = s.find_first_not_of(' ', p); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | if (!sTarget.empty()) |
|---|
| 216 | sTarget += _T("!"); |
|---|
| 217 | if (p == std::wstring::npos) { |
|---|
| 218 | if (pEnd == std::wstring::npos) |
|---|
| 219 | sTarget += s.substr(pStart); |
|---|
| 220 | else |
|---|
| 221 | sTarget += s.substr(pStart, pEnd-pStart); |
|---|
| 222 | break; |
|---|
| 223 | } |
|---|
| 224 | sTarget += s.substr(pStart,pEnd-pStart); |
|---|
| 225 | //p++; |
|---|
| 226 | } |
|---|
| 227 | return NSCModuleHelper::InjectSplitAndCommand(t.first, sTarget, '!', message, perf); |
|---|
| 228 | } else if (cd.type == script) { |
|---|
| 229 | return executeNRPECommand(args, message, perf); |
|---|
| 230 | } else if (cd.type == script_dir) { |
|---|
| 231 | std::wstring args = arrayBuffer::arrayBuffer2string(char_args, argLen, _T(" ")); |
|---|
| 232 | std::wstring cmd = scriptDirectory_ + command.c_str() + _T(" ") +args; |
|---|
| 233 | return executeNRPECommand(cmd, message, perf); |
|---|
| 234 | } else { |
|---|
| 235 | NSC_LOG_ERROR_STD(_T("Unknown script type: ") + command.c_str()); |
|---|
| 236 | return NSCAPI::critical; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | } |
|---|
| 240 | #define MAX_INPUT_BUFFER 1024 |
|---|
| 241 | |
|---|
| 242 | int NRPEListener::executeNRPECommand(std::wstring command, std::wstring &msg, std::wstring &perf) |
|---|
| 243 | { |
|---|
| 244 | NSCAPI::nagiosReturn result; |
|---|
| 245 | PROCESS_INFORMATION pi; |
|---|
| 246 | STARTUPINFO si; |
|---|
| 247 | HANDLE hChildOutR, hChildOutW, hChildInR, hChildInW; |
|---|
| 248 | SECURITY_ATTRIBUTES sec; |
|---|
| 249 | DWORD dwstate, dwexitcode; |
|---|
| 250 | int retval; |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | // Set up members of SECURITY_ATTRIBUTES structure. |
|---|
| 254 | |
|---|
| 255 | sec.nLength = sizeof(SECURITY_ATTRIBUTES); |
|---|
| 256 | sec.bInheritHandle = TRUE; |
|---|
| 257 | sec.lpSecurityDescriptor = NULL; |
|---|
| 258 | |
|---|
| 259 | // Create Pipes |
|---|
| 260 | CreatePipe(&hChildInR, &hChildInW, &sec, 0); |
|---|
| 261 | CreatePipe(&hChildOutR, &hChildOutW, &sec, 0); |
|---|
| 262 | |
|---|
| 263 | // Set up members of STARTUPINFO structure. |
|---|
| 264 | |
|---|
| 265 | ZeroMemory(&si, sizeof(STARTUPINFO)); |
|---|
| 266 | si.cb = sizeof(STARTUPINFO); |
|---|
| 267 | si.dwFlags = STARTF_USESTDHANDLES; |
|---|
| 268 | si.hStdInput = hChildInR; |
|---|
| 269 | si.hStdOutput = hChildOutW; |
|---|
| 270 | si.hStdError = hChildOutW; |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | // CreateProcess doesn't work with a const command |
|---|
| 274 | TCHAR *cmd = new TCHAR[command.length()+1]; |
|---|
| 275 | wcsncpy_s(cmd, command.length()+1, command.c_str(), command.length()); |
|---|
| 276 | cmd[command.length()] = 0; |
|---|
| 277 | std::wstring root = NSCModuleHelper::getBasePath(); |
|---|
| 278 | |
|---|
| 279 | // Create the child process. |
|---|
| 280 | BOOL processOK = CreateProcess(NULL, cmd, // command line |
|---|
| 281 | NULL, // process security attributes |
|---|
| 282 | NULL, // primary thread security attributes |
|---|
| 283 | TRUE, // handles are inherited |
|---|
| 284 | 0, // creation flags |
|---|
| 285 | NULL, // use parent's environment |
|---|
| 286 | root.c_str(), // use parent's current directory |
|---|
| 287 | &si, // STARTUPINFO pointer |
|---|
| 288 | &pi); // receives PROCESS_INFORMATION |
|---|
| 289 | delete [] cmd; |
|---|
| 290 | |
|---|
| 291 | if (processOK) { |
|---|
| 292 | dwstate = WaitForSingleObject(pi.hProcess, 1000*timeout); |
|---|
| 293 | CloseHandle(hChildInR); |
|---|
| 294 | CloseHandle(hChildInW); |
|---|
| 295 | CloseHandle(hChildOutW); |
|---|
| 296 | |
|---|
| 297 | if (dwstate == WAIT_TIMEOUT) { |
|---|
| 298 | TerminateProcess(pi.hProcess, 5); |
|---|
| 299 | msg = _T("The check (") + command + _T(") didn't respond within the timeout period (") + strEx::itos(timeout) + _T("s)!"); |
|---|
| 300 | result = NSCAPI::returnUNKNOWN; |
|---|
| 301 | } else { |
|---|
| 302 | DWORD dwread; |
|---|
| 303 | //TCHAR *buf = new TCHAR[MAX_INPUT_BUFFER+1]; |
|---|
| 304 | char *buf = new char[MAX_INPUT_BUFFER+1]; |
|---|
| 305 | //retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(WCHAR), &dwread, NULL); |
|---|
| 306 | retval = ReadFile(hChildOutR, buf, MAX_INPUT_BUFFER*sizeof(char), &dwread, NULL); |
|---|
| 307 | if (!retval || dwread == 0) { |
|---|
| 308 | msg = _T("No output available from command..."); |
|---|
| 309 | } else { |
|---|
| 310 | buf[dwread] = 0; |
|---|
| 311 | msg = strEx::string_to_wstring(buf); |
|---|
| 312 | //msg = buf; |
|---|
| 313 | //strEx::token t = strEx::getToken(msg, '\n'); |
|---|
| 314 | strEx::token t = strEx::getToken(msg, '|'); |
|---|
| 315 | msg = t.first; |
|---|
| 316 | std::wstring::size_type pos = msg.find_last_not_of(_T("\n\r ")); |
|---|
| 317 | if (pos != std::wstring::npos) { |
|---|
| 318 | if (pos == msg.size()) |
|---|
| 319 | msg = msg.substr(0,pos); |
|---|
| 320 | else |
|---|
| 321 | msg = msg.substr(0,pos+1); |
|---|
| 322 | } |
|---|
| 323 | //if (msg[msg.size()-1] == '\n') |
|---|
| 324 | perf = t.second; |
|---|
| 325 | } |
|---|
| 326 | delete [] buf; |
|---|
| 327 | if (GetExitCodeProcess(pi.hProcess, &dwexitcode) == 0) { |
|---|
| 328 | NSC_LOG_ERROR(_T("Failed to get commands (") + command + _T(") return code: ") + error::lookup::last_error()); |
|---|
| 329 | dwexitcode = NSCAPI::returnUNKNOWN; |
|---|
| 330 | } |
|---|
| 331 | if (!NSCHelper::isNagiosReturnCode(dwexitcode)) { |
|---|
| 332 | NSC_LOG_ERROR(_T("The command (") + command + _T(") returned an invalid return code: ") + strEx::itos(dwexitcode)); |
|---|
| 333 | dwexitcode = NSCAPI::returnUNKNOWN; |
|---|
| 334 | } |
|---|
| 335 | result = NSCHelper::int2nagios(dwexitcode); |
|---|
| 336 | } |
|---|
| 337 | CloseHandle(pi.hThread); |
|---|
| 338 | CloseHandle(pi.hProcess); |
|---|
| 339 | CloseHandle(hChildOutR); |
|---|
| 340 | } |
|---|
| 341 | else { |
|---|
| 342 | msg = _T("NRPE_NT failed to create process (") + command + _T("): ") + error::lookup::last_error(); |
|---|
| 343 | result = NSCAPI::returnUNKNOWN; |
|---|
| 344 | CloseHandle(hChildInR); |
|---|
| 345 | CloseHandle(hChildInW); |
|---|
| 346 | CloseHandle(hChildOutW); |
|---|
| 347 | CloseHandle(pi.hThread); |
|---|
| 348 | CloseHandle(pi.hProcess); |
|---|
| 349 | CloseHandle(hChildOutR); |
|---|
| 350 | } |
|---|
| 351 | return result; |
|---|
| 352 | } |
|---|
| 353 | void NRPEListener::onClose() |
|---|
| 354 | {} |
|---|
| 355 | |
|---|
| 356 | void NRPEListener::onAccept(simpleSocket::Socket *client) |
|---|
| 357 | { |
|---|
| 358 | if (!allowedHosts.inAllowedHosts(client->getAddr())) { |
|---|
| 359 | NSC_LOG_ERROR(_T("Unauthorize access from: ") + client->getAddrString()); |
|---|
| 360 | client->close(); |
|---|
| 361 | return; |
|---|
| 362 | } |
|---|
| 363 | try { |
|---|
| 364 | simpleSocket::DataBuffer block; |
|---|
| 365 | int i; |
|---|
| 366 | int maxWait = socketTimeout_*10; |
|---|
| 367 | for (i=0;i<maxWait;i++) { |
|---|
| 368 | bool lastReadHasMore = false; |
|---|
| 369 | try { |
|---|
| 370 | lastReadHasMore = client->readAll(block, 1048); |
|---|
| 371 | } catch (simpleSocket::SocketException e) { |
|---|
| 372 | NSC_LOG_MESSAGE(_T("Could not read NRPE packet from socket :") + e.getMessage()); |
|---|
| 373 | client->close(); |
|---|
| 374 | return; |
|---|
| 375 | } |
|---|
| 376 | if (block.getLength() >= NRPEPacket::getBufferLength()) |
|---|
| 377 | break; |
|---|
| 378 | if (!lastReadHasMore) { |
|---|
| 379 | client->close(); |
|---|
| 380 | return; |
|---|
| 381 | } |
|---|
| 382 | Sleep(100); |
|---|
| 383 | } |
|---|
| 384 | if (i >= maxWait) { |
|---|
| 385 | NSC_LOG_ERROR_STD(_T("Timeout reading NRPE-packet (increase socket_timeout)")); |
|---|
| 386 | client->close(); |
|---|
| 387 | return; |
|---|
| 388 | } |
|---|
| 389 | if (block.getLength() == NRPEPacket::getBufferLength()) { |
|---|
| 390 | try { |
|---|
| 391 | NRPEPacket out = handlePacket(NRPEPacket(block.getBuffer(), block.getLength())); |
|---|
| 392 | block.copyFrom(out.getBuffer(), out.getBufferLength()); |
|---|
| 393 | } catch (NRPEPacket::NRPEPacketException e) { |
|---|
| 394 | NSC_LOG_ERROR_STD(_T("NRPESocketException: ") + e.getMessage()); |
|---|
| 395 | client->close(); |
|---|
| 396 | return; |
|---|
| 397 | } |
|---|
| 398 | client->send(block); |
|---|
| 399 | } |
|---|
| 400 | } catch (simpleSocket::SocketException e) { |
|---|
| 401 | NSC_LOG_ERROR_STD(_T("SocketException: ") + e.getMessage()); |
|---|
| 402 | } catch (NRPEException e) { |
|---|
| 403 | NSC_LOG_ERROR_STD(_T("NRPEException: ") + e.getMessage()); |
|---|
| 404 | } |
|---|
| 405 | client->close(); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | NRPEPacket NRPEListener::handlePacket(NRPEPacket p) { |
|---|
| 409 | if (p.getType() != NRPEPacket::queryPacket) { |
|---|
| 410 | NSC_LOG_ERROR(_T("Request is not a query.")); |
|---|
| 411 | throw NRPEException(_T("Invalid query type")); |
|---|
| 412 | } |
|---|
| 413 | if (p.getVersion() != NRPEPacket::version2) { |
|---|
| 414 | NSC_LOG_ERROR(_T("Request had unsupported version.")); |
|---|
| 415 | throw NRPEException(_T("Invalid version")); |
|---|
| 416 | } |
|---|
| 417 | if (!p.verifyCRC()) { |
|---|
| 418 | NSC_LOG_ERROR(_T("Request had invalid checksum.")); |
|---|
| 419 | throw NRPEException(_T("Invalid checksum")); |
|---|
| 420 | } |
|---|
| 421 | strEx::token cmd = strEx::getToken(p.getPayload(), '!'); |
|---|
| 422 | if (cmd.first == _T("_NRPE_CHECK")) { |
|---|
| 423 | return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnOK, _T("I (") SZVERSION _T(") seem to be doing fine...")); |
|---|
| 424 | } |
|---|
| 425 | std::wstring msg, perf; |
|---|
| 426 | |
|---|
| 427 | if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_ARGUMENTS, NRPE_SETTINGS_ALLOW_ARGUMENTS_DEFAULT) == 0) { |
|---|
| 428 | if (!cmd.second.empty()) { |
|---|
| 429 | NSC_LOG_ERROR(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); |
|---|
| 430 | throw NRPEException(_T("Request contained arguments (not currently allowed, check the allow_arguments option).")); |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | if (NSCModuleHelper::getSettingsInt(NRPE_SECTION_TITLE, NRPE_SETTINGS_ALLOW_NASTY_META, NRPE_SETTINGS_ALLOW_NASTY_META_DEFAULT) == 0) { |
|---|
| 434 | if (cmd.first.find_first_of(NASTY_METACHARS) != std::wstring::npos) { |
|---|
| 435 | NSC_LOG_ERROR(_T("Request command contained illegal metachars!")); |
|---|
| 436 | throw NRPEException(_T("Request command contained illegal metachars!")); |
|---|
| 437 | } |
|---|
| 438 | if (cmd.second.find_first_of(NASTY_METACHARS) != std::wstring::npos) { |
|---|
| 439 | NSC_LOG_ERROR(_T("Request arguments contained illegal metachars!")); |
|---|
| 440 | throw NRPEException(_T("Request command contained illegal metachars!")); |
|---|
| 441 | } |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | NSCAPI::nagiosReturn ret = -3; |
|---|
| 445 | try { |
|---|
| 446 | ret = NSCModuleHelper::InjectSplitAndCommand(cmd.first, cmd.second, '!', msg, perf); |
|---|
| 447 | } catch (...) { |
|---|
| 448 | return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, NSCAPI::returnUNKNOWN, _T("UNKNOWN: Internal exception")); |
|---|
| 449 | } |
|---|
| 450 | switch (ret) { |
|---|
| 451 | case NSCAPI::returnInvalidBufferLen: |
|---|
| 452 | msg = _T("UNKNOWN: Return buffer to small to handle this command."); |
|---|
| 453 | ret = NSCAPI::returnUNKNOWN; |
|---|
| 454 | break; |
|---|
| 455 | case NSCAPI::returnIgnored: |
|---|
| 456 | msg = _T("UNKNOWN: No handler for that command"); |
|---|
| 457 | ret = NSCAPI::returnUNKNOWN; |
|---|
| 458 | break; |
|---|
| 459 | case NSCAPI::returnOK: |
|---|
| 460 | case NSCAPI::returnWARN: |
|---|
| 461 | case NSCAPI::returnCRIT: |
|---|
| 462 | case NSCAPI::returnUNKNOWN: |
|---|
| 463 | break; |
|---|
| 464 | default: |
|---|
| 465 | msg = _T("UNKNOWN: Internal error."); |
|---|
| 466 | ret = NSCAPI::returnUNKNOWN; |
|---|
| 467 | } |
|---|
| 468 | if (perf.empty()||noPerfData_) { |
|---|
| 469 | return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg); |
|---|
| 470 | } else { |
|---|
| 471 | return NRPEPacket(NRPEPacket::responsePacket, NRPEPacket::version2, ret, msg + _T("|") + perf); |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | NSC_WRAPPERS_MAIN_DEF(gNRPEListener); |
|---|
| 476 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 477 | NSC_WRAPPERS_HANDLE_CMD_DEF(gNRPEListener); |
|---|
| 478 | NSC_WRAPPERS_HANDLE_CONFIGURATION(gNRPEListener); |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | MODULE_SETTINGS_START(NRPEListener, _T("NRPE Listener configuration"), _T("...")) |
|---|
| 482 | |
|---|
| 483 | PAGE(_T("NRPE Listsner configuration")) |
|---|
| 484 | |
|---|
| 485 | ITEM_EDIT_TEXT(_T("port"), _T("This is the port the NRPEListener.dll will listen to.")) |
|---|
| 486 | ITEM_MAP_TO(_T("basic_ini_text_mapper")) |
|---|
| 487 | OPTION(_T("section"), _T("NRPE")) |
|---|
| 488 | OPTION(_T("key"), _T("port")) |
|---|
| 489 | OPTION(_T("default"), _T("5666")) |
|---|
| 490 | ITEM_END() |
|---|
| 491 | |
|---|
| 492 | ITEM_CHECK_BOOL(_T("allow_arguments"), _T("This option determines whether or not the NRPE daemon will allow clients to specify arguments to commands that are executed.")) |
|---|
| 493 | ITEM_MAP_TO(_T("basic_ini_bool_mapper")) |
|---|
| 494 | OPTION(_T("section"), _T("NRPE")) |
|---|
| 495 | OPTION(_T("key"), _T("allow_arguments")) |
|---|
| 496 | OPTION(_T("default"), _T("false")) |
|---|
| 497 | OPTION(_T("true_value"), _T("1")) |
|---|
| 498 | OPTION(_T("false_value"), _T("0")) |
|---|
| 499 | ITEM_END() |
|---|
| 500 | |
|---|
| 501 | ITEM_CHECK_BOOL(_T("allow_nasty_meta_chars"), _T("This might have security implications (depending on what you do with the options)")) |
|---|
| 502 | ITEM_MAP_TO(_T("basic_ini_bool_mapper")) |
|---|
| 503 | OPTION(_T("section"), _T("NRPE")) |
|---|
| 504 | OPTION(_T("key"), _T("allow_nasty_meta_chars")) |
|---|
| 505 | OPTION(_T("default"), _T("false")) |
|---|
| 506 | OPTION(_T("true_value"), _T("1")) |
|---|
| 507 | OPTION(_T("false_value"), _T("0")) |
|---|
| 508 | ITEM_END() |
|---|
| 509 | |
|---|
| 510 | ITEM_CHECK_BOOL(_T("use_ssl"), _T("This option will enable SSL encryption on the NRPE data socket (this increases security somwhat.")) |
|---|
| 511 | ITEM_MAP_TO(_T("basic_ini_bool_mapper")) |
|---|
| 512 | OPTION(_T("section"), _T("NRPE")) |
|---|
| 513 | OPTION(_T("key"), _T("use_ssl")) |
|---|
| 514 | OPTION(_T("default"), _T("true")) |
|---|
| 515 | OPTION(_T("true_value"), _T("1")) |
|---|
| 516 | OPTION(_T("false_value"), _T("0")) |
|---|
| 517 | ITEM_END() |
|---|
| 518 | |
|---|
| 519 | PAGE_END() |
|---|
| 520 | ADVANCED_PAGE(_T("Access configuration")) |
|---|
| 521 | |
|---|
| 522 | ITEM_EDIT_OPTIONAL_LIST(_T("Allow connection from:"), _T("This is the hosts that will be allowed to poll performance data from the NRPE server.")) |
|---|
| 523 | OPTION(_T("disabledCaption"), _T("Use global settings (defined previously)")) |
|---|
| 524 | OPTION(_T("enabledCaption"), _T("Specify hosts for NRPE server")) |
|---|
| 525 | OPTION(_T("listCaption"), _T("Add all IP addresses (not hosts) which should be able to connect:")) |
|---|
| 526 | OPTION(_T("separator"), _T(",")) |
|---|
| 527 | OPTION(_T("disabled"), _T("")) |
|---|
| 528 | ITEM_MAP_TO(_T("basic_ini_text_mapper")) |
|---|
| 529 | OPTION(_T("section"), _T("NRPE")) |
|---|
| 530 | OPTION(_T("key"), _T("allowed_hosts")) |
|---|
| 531 | OPTION(_T("default"), _T("")) |
|---|
| 532 | ITEM_END() |
|---|
| 533 | |
|---|
| 534 | PAGE_END() |
|---|
| 535 | MODULE_SETTINGS_END() |
|---|