| 1 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // NSClient++ Base Service |
|---|
| 3 | // |
|---|
| 4 | // Copyright (c) 2004 MySolutions NORDIC (http://www.medin.name) |
|---|
| 5 | // |
|---|
| 6 | // Date: 2004-03-13 |
|---|
| 7 | // Author: Michael Medin (michael@medin.name) |
|---|
| 8 | // |
|---|
| 9 | // Part of this file is based on work by Bruno Vais (bvais@usa.net) |
|---|
| 10 | // |
|---|
| 11 | // This software is provided "AS IS", without a warranty of any kind. |
|---|
| 12 | // You are free to use/modify this code but leave this header intact. |
|---|
| 13 | // |
|---|
| 14 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 15 | #include "stdafx.h" |
|---|
| 16 | #include <winsvc.h> |
|---|
| 17 | #include "NSClient++.h" |
|---|
| 18 | #include <Settings.h> |
|---|
| 19 | #include <charEx.h> |
|---|
| 20 | #include <Socket.h> |
|---|
| 21 | #include <b64/b64.h> |
|---|
| 22 | #include <config.h> |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | NSClient mainClient; // Global core instance. |
|---|
| 26 | bool g_bConsoleLog = false; |
|---|
| 27 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 28 | // Startup code |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Application startup point |
|---|
| 32 | * |
|---|
| 33 | * @param argc Argument count |
|---|
| 34 | * @param argv[] Argument array |
|---|
| 35 | * @param envp[] Environment array |
|---|
| 36 | * @return exit status |
|---|
| 37 | */ |
|---|
| 38 | int main(int argc, TCHAR* argv[], TCHAR* envp[]) |
|---|
| 39 | { |
|---|
| 40 | int nRetCode = 0; |
|---|
| 41 | |
|---|
| 42 | if ( (argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/')) ) { |
|---|
| 43 | if ( _stricmp( "install", argv[1]+1 ) == 0 ) { |
|---|
| 44 | g_bConsoleLog = true; |
|---|
| 45 | try { |
|---|
| 46 | serviceControll::Install(SZSERVICENAME, SZSERVICEDISPLAYNAME, SZDEPENDENCIES); |
|---|
| 47 | } catch (const serviceControll::SCException& e) { |
|---|
| 48 | LOG_MESSAGE_STD("Service installation failed: " + e.error_); |
|---|
| 49 | return -1; |
|---|
| 50 | } |
|---|
| 51 | try { |
|---|
| 52 | serviceControll::SetDescription(SZSERVICENAME, SZSERVICEDESCRIPTION); |
|---|
| 53 | } catch (const serviceControll::SCException& e) { |
|---|
| 54 | LOG_MESSAGE_STD("Couldn't set service description: " + e.error_); |
|---|
| 55 | } |
|---|
| 56 | LOG_MESSAGE("Service installed!"); |
|---|
| 57 | } else if ( _stricmp( "uninstall", argv[1]+1 ) == 0 ) { |
|---|
| 58 | g_bConsoleLog = true; |
|---|
| 59 | try { |
|---|
| 60 | serviceControll::Uninstall(SZSERVICENAME); |
|---|
| 61 | } catch (const serviceControll::SCException& e) { |
|---|
| 62 | LOG_MESSAGE_STD("Service deinstallation failed; " + e.error_); |
|---|
| 63 | return -1; |
|---|
| 64 | } |
|---|
| 65 | } else if ( _stricmp( "encrypt", argv[1]+1 ) == 0 ) { |
|---|
| 66 | g_bConsoleLog = true; |
|---|
| 67 | std::string password; |
|---|
| 68 | try { |
|---|
| 69 | Settings::getInstance()->setFile(mainClient.getBasePath() + "NSC.ini"); |
|---|
| 70 | } catch (SettingsException e) { |
|---|
| 71 | std::cout << "Could not find settings: " << e.getMessage() << std::endl;; |
|---|
| 72 | return 1; |
|---|
| 73 | } |
|---|
| 74 | std::cout << "Enter password to encrypt (has to be a single word): "; |
|---|
| 75 | std::cin >> password; |
|---|
| 76 | std::string xor_pwd = Encrypt(password); |
|---|
| 77 | std::cout << "obfuscated_password=" << xor_pwd << std::endl; |
|---|
| 78 | if (password != Decrypt(xor_pwd)) |
|---|
| 79 | std::cout << "ERROR: Password did not match!" << std::endl; |
|---|
| 80 | Settings::destroyInstance(); |
|---|
| 81 | return 0; |
|---|
| 82 | } else if ( _stricmp( "start", argv[1]+1 ) == 0 ) { |
|---|
| 83 | g_bConsoleLog = true; |
|---|
| 84 | serviceControll::Start(SZSERVICENAME); |
|---|
| 85 | } else if ( _stricmp( "stop", argv[1]+1 ) == 0 ) { |
|---|
| 86 | g_bConsoleLog = true; |
|---|
| 87 | serviceControll::Stop(SZSERVICENAME); |
|---|
| 88 | } else if ( _stricmp( "about", argv[1]+1 ) == 0 ) { |
|---|
| 89 | g_bConsoleLog = true; |
|---|
| 90 | LOG_MESSAGE(SZAPPNAME " (C) Michael Medin"); |
|---|
| 91 | LOG_MESSAGE("Version " SZVERSION); |
|---|
| 92 | } else if ( _stricmp( "version", argv[1]+1 ) == 0 ) { |
|---|
| 93 | g_bConsoleLog = true; |
|---|
| 94 | LOG_MESSAGE(SZAPPNAME " Version: " SZVERSION); |
|---|
| 95 | } else if ( _stricmp( "test", argv[1]+1 ) == 0 ) { |
|---|
| 96 | #ifdef _DEBUG |
|---|
| 97 | /* |
|---|
| 98 | strEx::run_test_getToken(); |
|---|
| 99 | strEx::run_test_replace(); |
|---|
| 100 | charEx::run_test_getToken(); |
|---|
| 101 | arrayBuffer::run_testArrayBuffer(); |
|---|
| 102 | */ |
|---|
| 103 | #endif |
|---|
| 104 | g_bConsoleLog = true; |
|---|
| 105 | if (!mainClient.InitiateService()) { |
|---|
| 106 | LOG_ERROR_STD("Service *NOT* started!"); |
|---|
| 107 | return -1; |
|---|
| 108 | } |
|---|
| 109 | LOG_MESSAGE_STD("Using settings from: " + Settings::getInstance()->getActiveType()); |
|---|
| 110 | LOG_MESSAGE("Enter command to inject or exit to terminate..."); |
|---|
| 111 | std::string s = ""; |
|---|
| 112 | std::string buff = ""; |
|---|
| 113 | std::cin >> s; |
|---|
| 114 | while (s != "exit") { |
|---|
| 115 | if (std::cin.peek() < 15) { |
|---|
| 116 | buff += s; |
|---|
| 117 | strEx::token t = strEx::getToken(buff, ' '); |
|---|
| 118 | std::string msg, perf; |
|---|
| 119 | NSCAPI::nagiosReturn ret = mainClient.inject(t.first, t.second, ' ', msg, perf); |
|---|
| 120 | if (perf.empty()) |
|---|
| 121 | std::cout << NSCHelper::translateReturn(ret) << ":" << msg << std::endl; |
|---|
| 122 | else |
|---|
| 123 | std::cout << NSCHelper::translateReturn(ret) << ":" << msg << "|" << perf << std::endl; |
|---|
| 124 | buff = ""; |
|---|
| 125 | } else { |
|---|
| 126 | buff += s + " "; |
|---|
| 127 | } |
|---|
| 128 | std::cin >> s; |
|---|
| 129 | } |
|---|
| 130 | mainClient.TerminateService(); |
|---|
| 131 | return 0; |
|---|
| 132 | } else { |
|---|
| 133 | LOG_MESSAGE("Usage: -version, -about, -install, -uninstall, -start, -stop, -encrypt"); |
|---|
| 134 | LOG_MESSAGE("Usage: <ModuleName> <commnd> [arguments]"); |
|---|
| 135 | } |
|---|
| 136 | return nRetCode; |
|---|
| 137 | } else if (argc > 2) { |
|---|
| 138 | g_bConsoleLog = true; |
|---|
| 139 | mainClient.InitiateService(); |
|---|
| 140 | if (argc>=3) |
|---|
| 141 | mainClient.commandLineExec(argv[1], argv[2], argc-3, &argv[3]); |
|---|
| 142 | else |
|---|
| 143 | mainClient.commandLineExec(argv[1], argv[2], 0, NULL); |
|---|
| 144 | mainClient.TerminateService(); |
|---|
| 145 | return nRetCode; |
|---|
| 146 | } |
|---|
| 147 | mainClient.StartServiceCtrlDispatcher(); |
|---|
| 148 | return nRetCode; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 152 | // Service functions |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Service control handler startup point. |
|---|
| 156 | * When the program is started as a service this will be the entry point. |
|---|
| 157 | */ |
|---|
| 158 | bool NSClientT::InitiateService(void) { |
|---|
| 159 | try { |
|---|
| 160 | Settings::getInstance()->setFile(getBasePath() + "NSC.ini"); |
|---|
| 161 | } catch (SettingsException e) { |
|---|
| 162 | LOG_ERROR_STD("Could not find settings: " + e.getMessage()); |
|---|
| 163 | return false; |
|---|
| 164 | } |
|---|
| 165 | try { |
|---|
| 166 | simpleSocket::WSAStartup(); |
|---|
| 167 | } catch (simpleSocket::SocketException e) { |
|---|
| 168 | LOG_ERROR_STD("Uncaught exception: " + e.getMessage()); |
|---|
| 169 | return false; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | SettingsT::sectionList list = Settings::getInstance()->getSection("modules"); |
|---|
| 173 | for (SettingsT::sectionList::iterator it = list.begin(); it != list.end(); it++) { |
|---|
| 174 | try { |
|---|
| 175 | loadPlugin(getBasePath() + "modules\\" + (*it)); |
|---|
| 176 | } catch(const NSPluginException& e) { |
|---|
| 177 | LOG_ERROR_STD("Exception raised: " + e.error_ + " in module: " + e.file_); |
|---|
| 178 | return false; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | loadPlugins(); |
|---|
| 182 | return true; |
|---|
| 183 | } |
|---|
| 184 | /** |
|---|
| 185 | * Service control handler termination point. |
|---|
| 186 | * When the program is stopped as a service this will be the "exit point". |
|---|
| 187 | */ |
|---|
| 188 | void NSClientT::TerminateService(void) { |
|---|
| 189 | try { |
|---|
| 190 | mainClient.unloadPlugins(); |
|---|
| 191 | } catch(NSPluginException *e) { |
|---|
| 192 | std::cout << "Exception raised: " << e->error_ << " in module: " << e->file_ << std::endl;; |
|---|
| 193 | } |
|---|
| 194 | try { |
|---|
| 195 | simpleSocket::WSACleanup(); |
|---|
| 196 | } catch (simpleSocket::SocketException e) { |
|---|
| 197 | LOG_ERROR_STD("Uncaught exception: " + e.getMessage()); |
|---|
| 198 | } |
|---|
| 199 | Settings::destroyInstance(); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * Forward this to the main service dispatcher helper class |
|---|
| 204 | * @param dwArgc |
|---|
| 205 | * @param *lpszArgv |
|---|
| 206 | */ |
|---|
| 207 | void WINAPI NSClientT::service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv) { |
|---|
| 208 | mainClient.service_main(dwArgc, lpszArgv); |
|---|
| 209 | } |
|---|
| 210 | /** |
|---|
| 211 | * Forward this to the main service dispatcher helper class |
|---|
| 212 | * @param dwCtrlCode |
|---|
| 213 | */ |
|---|
| 214 | void WINAPI NSClientT::service_ctrl_dispatch(DWORD dwCtrlCode) { |
|---|
| 215 | mainClient.service_ctrl(dwCtrlCode); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 219 | // Member functions |
|---|
| 220 | |
|---|
| 221 | int NSClientT::commandLineExec(const char* module, const char* command, const unsigned int argLen, char** args) { |
|---|
| 222 | std::string sModule = module; |
|---|
| 223 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 224 | if (!readLock.IsLocked()) { |
|---|
| 225 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 226 | return -1; |
|---|
| 227 | } |
|---|
| 228 | for (pluginList::size_type i=0;i<plugins_.size();++i) { |
|---|
| 229 | NSCPlugin *p = plugins_[i]; |
|---|
| 230 | if (p->getName() == sModule) { |
|---|
| 231 | LOG_DEBUG_STD("Found module: " + p->getName() + "..."); |
|---|
| 232 | try { |
|---|
| 233 | return p->commandLineExec(command, argLen, args); |
|---|
| 234 | } catch (NSPluginException e) { |
|---|
| 235 | LOG_ERROR_STD("Could not execute command: " + e.error_ + " in " + e.file_); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | LOG_ERROR("Module not found."); |
|---|
| 240 | return 0; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | /** |
|---|
| 244 | * Load a list of plug-ins |
|---|
| 245 | * @param plugins A list with plug-ins (DLL files) to load |
|---|
| 246 | */ |
|---|
| 247 | void NSClientT::addPlugins(const std::list<std::string> plugins) { |
|---|
| 248 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 249 | if (!readLock.IsLocked()) { |
|---|
| 250 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 251 | return; |
|---|
| 252 | } |
|---|
| 253 | std::list<std::string>::const_iterator it; |
|---|
| 254 | for (it = plugins.begin(); it != plugins.end(); ++it) { |
|---|
| 255 | loadPlugin(*it); |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | /** |
|---|
| 259 | * Unload all plug-ins (in reversed order) |
|---|
| 260 | */ |
|---|
| 261 | void NSClientT::unloadPlugins() { |
|---|
| 262 | { |
|---|
| 263 | WriteLock writeLock(&m_mutexRW, true, 10000); |
|---|
| 264 | if (!writeLock.IsLocked()) { |
|---|
| 265 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 266 | return; |
|---|
| 267 | } |
|---|
| 268 | commandHandlers_.clear(); |
|---|
| 269 | messageHandlers_.clear(); |
|---|
| 270 | } |
|---|
| 271 | { |
|---|
| 272 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 273 | if (!readLock.IsLocked()) { |
|---|
| 274 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 275 | return; |
|---|
| 276 | } |
|---|
| 277 | for (pluginList::size_type i=plugins_.size();i>0;i--) { |
|---|
| 278 | NSCPlugin *p = plugins_[i-1]; |
|---|
| 279 | LOG_DEBUG_STD("Unloading plugin: " + p->getName() + "..."); |
|---|
| 280 | p->unload(); |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | { |
|---|
| 284 | WriteLock writeLock(&m_mutexRW, true, 10000); |
|---|
| 285 | if (!writeLock.IsLocked()) { |
|---|
| 286 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 287 | return; |
|---|
| 288 | } |
|---|
| 289 | for (unsigned int i=plugins_.size();i>0;i--) { |
|---|
| 290 | NSCPlugin *p = plugins_[i-1]; |
|---|
| 291 | plugins_[i-1] = NULL; |
|---|
| 292 | delete p; |
|---|
| 293 | } |
|---|
| 294 | plugins_.clear(); |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | void NSClientT::loadPlugins() { |
|---|
| 299 | ReadLock readLock(&m_mutexRW, true, 10000); |
|---|
| 300 | if (!readLock.IsLocked()) { |
|---|
| 301 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 302 | return; |
|---|
| 303 | } |
|---|
| 304 | for (pluginList::iterator it=plugins_.begin(); it != plugins_.end(); ++it) { |
|---|
| 305 | LOG_DEBUG_STD("Loading plugin: " + (*it)->getName() + "..."); |
|---|
| 306 | (*it)->load_plugin(); |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | /** |
|---|
| 310 | * Load a single plug-in using a DLL filename |
|---|
| 311 | * @param file The DLL file |
|---|
| 312 | */ |
|---|
| 313 | void NSClientT::loadPlugin(const std::string file) { |
|---|
| 314 | addPlugin(new NSCPlugin(file)); |
|---|
| 315 | } |
|---|
| 316 | /** |
|---|
| 317 | * Load and add a plugin to various internal structures |
|---|
| 318 | * @param *plugin The plug-ininstance to load. The pointer is managed by the |
|---|
| 319 | */ |
|---|
| 320 | void NSClientT::addPlugin(plugin_type plugin) { |
|---|
| 321 | plugin->load_dll(); |
|---|
| 322 | { |
|---|
| 323 | WriteLock writeLock(&m_mutexRW, true, 10000); |
|---|
| 324 | if (!writeLock.IsLocked()) { |
|---|
| 325 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 326 | return; |
|---|
| 327 | } |
|---|
| 328 | plugins_.insert(plugins_.end(), plugin); |
|---|
| 329 | if (plugin->hasCommandHandler()) |
|---|
| 330 | commandHandlers_.insert(commandHandlers_.end(), plugin); |
|---|
| 331 | if (plugin->hasMessageHandler()) |
|---|
| 332 | messageHandlers_.insert(messageHandlers_.end(), plugin); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | NSCAPI::nagiosReturn NSClientT::inject(std::string command, std::string arguments, char splitter, std::string &msg, std::string & perf) { |
|---|
| 338 | unsigned int aLen = 0; |
|---|
| 339 | char ** aBuf = arrayBuffer::split2arrayBuffer(arguments, splitter, aLen); |
|---|
| 340 | char * mBuf = new char[1024]; |
|---|
| 341 | char * pBuf = new char[1024]; |
|---|
| 342 | NSCAPI::nagiosReturn ret = injectRAW(command.c_str(), aLen, aBuf, mBuf, 1023, pBuf, 1023); |
|---|
| 343 | arrayBuffer::destroyArrayBuffer(aBuf, aLen); |
|---|
| 344 | if ( (ret == NSCAPI::returnInvalidBufferLen) || (ret == NSCAPI::returnIgnored) ) { |
|---|
| 345 | delete [] mBuf; |
|---|
| 346 | delete [] pBuf; |
|---|
| 347 | return ret; |
|---|
| 348 | } |
|---|
| 349 | msg = mBuf; |
|---|
| 350 | perf = pBuf; |
|---|
| 351 | delete [] mBuf; |
|---|
| 352 | delete [] pBuf; |
|---|
| 353 | return ret; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | /** |
|---|
| 357 | * Inject a command into the plug-in stack. |
|---|
| 358 | * |
|---|
| 359 | * @param command Command to inject |
|---|
| 360 | * @param argLen Length of argument buffer |
|---|
| 361 | * @param **argument Argument buffer |
|---|
| 362 | * @param *returnMessageBuffer Message buffer |
|---|
| 363 | * @param returnMessageBufferLen Length of returnMessageBuffer |
|---|
| 364 | * @param *returnPerfBuffer Performance data buffer |
|---|
| 365 | * @param returnPerfBufferLen Length of returnPerfBuffer |
|---|
| 366 | * @return The command status |
|---|
| 367 | */ |
|---|
| 368 | NSCAPI::nagiosReturn NSClientT::injectRAW(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen) { |
|---|
| 369 | if (logDebug()) { |
|---|
| 370 | LOG_DEBUG_STD("Injecting: " + (std::string) command + ": " + arrayBuffer::arrayBuffer2string(argument, argLen, ", ")); |
|---|
| 371 | } |
|---|
| 372 | ReadLock readLock(&m_mutexRW, true, 5000); |
|---|
| 373 | if (!readLock.IsLocked()) { |
|---|
| 374 | LOG_ERROR("FATAL ERROR: Could not get read-mutex."); |
|---|
| 375 | return NSCAPI::returnUNKNOWN; |
|---|
| 376 | } |
|---|
| 377 | for (pluginList::size_type i = 0; i < commandHandlers_.size(); i++) { |
|---|
| 378 | try { |
|---|
| 379 | NSCAPI::nagiosReturn c = commandHandlers_[i]->handleCommand(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); |
|---|
| 380 | switch (c) { |
|---|
| 381 | case NSCAPI::returnInvalidBufferLen: |
|---|
| 382 | LOG_ERROR("UNKNOWN: Return buffer to small to handle this command."); |
|---|
| 383 | return c; |
|---|
| 384 | case NSCAPI::returnIgnored: |
|---|
| 385 | break; |
|---|
| 386 | case NSCAPI::returnOK: |
|---|
| 387 | case NSCAPI::returnWARN: |
|---|
| 388 | case NSCAPI::returnCRIT: |
|---|
| 389 | case NSCAPI::returnUNKNOWN: |
|---|
| 390 | LOG_DEBUG_STD("Injected Result: " + NSCHelper::translateReturn(c) + " -- " + (std::string)(returnMessageBuffer)); |
|---|
| 391 | LOG_DEBUG_STD("Injected Performance Result: " +(std::string) returnPerfBuffer); |
|---|
| 392 | return c; |
|---|
| 393 | default: |
|---|
| 394 | LOG_ERROR_STD("Unknown error from handleCommand: " + strEx::itos(c)); |
|---|
| 395 | return c; |
|---|
| 396 | } |
|---|
| 397 | } catch(const NSPluginException& e) { |
|---|
| 398 | LOG_ERROR_STD("Exception raised: " + e.error_ + " in module: " + e.file_); |
|---|
| 399 | return NSCAPI::returnCRIT; |
|---|
| 400 | } |
|---|
| 401 | } |
|---|
| 402 | LOG_MESSAGE_STD("No handler for command: '" + command + "'"); |
|---|
| 403 | return NSCAPI::returnIgnored; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | bool NSClientT::logDebug() { |
|---|
| 407 | typedef enum status {unknown, debug, nodebug }; |
|---|
| 408 | static status d = unknown; |
|---|
| 409 | if (d == unknown) { |
|---|
| 410 | if (Settings::getInstance()->getInt("log", "debug", 0) == 1) |
|---|
| 411 | d = debug; |
|---|
| 412 | else |
|---|
| 413 | d = nodebug; |
|---|
| 414 | } |
|---|
| 415 | return (d == debug); |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | /** |
|---|
| 419 | * Report a message to all logging enabled modules. |
|---|
| 420 | * |
|---|
| 421 | * @param msgType Message type |
|---|
| 422 | * @param file Filename generally __FILE__ |
|---|
| 423 | * @param line Line number, generally __LINE__ |
|---|
| 424 | * @param message The message as a human readable string. |
|---|
| 425 | */ |
|---|
| 426 | void NSClientT::reportMessage(int msgType, const char* file, const int line, std::string message) { |
|---|
| 427 | if ((msgType == NSCAPI::debug)&&(!logDebug())) { |
|---|
| 428 | return; |
|---|
| 429 | } |
|---|
| 430 | { |
|---|
| 431 | ReadLock readLock(&m_mutexRW, true, 5000); |
|---|
| 432 | if (!readLock.IsLocked()) { |
|---|
| 433 | std::cout << "Message was lost as the core was locked..." << std::endl; |
|---|
| 434 | return; |
|---|
| 435 | } |
|---|
| 436 | MutexLock lock(messageMutex); |
|---|
| 437 | if (!lock.hasMutex()) { |
|---|
| 438 | std::cout << "Message was lost as the core was locked..." << std::endl; |
|---|
| 439 | std::cout << message << std::endl; |
|---|
| 440 | return; |
|---|
| 441 | } |
|---|
| 442 | if (g_bConsoleLog) { |
|---|
| 443 | std::string k = "?"; |
|---|
| 444 | switch (msgType) { |
|---|
| 445 | case NSCAPI::critical: |
|---|
| 446 | k ="c"; |
|---|
| 447 | break; |
|---|
| 448 | case NSCAPI::warning: |
|---|
| 449 | k ="w"; |
|---|
| 450 | break; |
|---|
| 451 | case NSCAPI::error: |
|---|
| 452 | k ="e"; |
|---|
| 453 | break; |
|---|
| 454 | case NSCAPI::log: |
|---|
| 455 | k ="l"; |
|---|
| 456 | break; |
|---|
| 457 | case NSCAPI::debug: |
|---|
| 458 | k ="d"; |
|---|
| 459 | break; |
|---|
| 460 | } |
|---|
| 461 | std::cout << k << " " << file << "(" << line << ") " << message << std::endl; |
|---|
| 462 | } |
|---|
| 463 | for (pluginList::size_type i = 0; i< messageHandlers_.size(); i++) { |
|---|
| 464 | try { |
|---|
| 465 | messageHandlers_[i]->handleMessage(msgType, file, line, message.c_str()); |
|---|
| 466 | } catch(const NSPluginException& e) { |
|---|
| 467 | // Here we are pretty much fucked! (as logging this might cause a loop :) |
|---|
| 468 | std::cout << "Caught: " << e.error_ << " when trying to log a message..." << std::endl; |
|---|
| 469 | std::cout << "This is *really really* bad, now the world is about to end..." << std::endl; |
|---|
| 470 | } |
|---|
| 471 | } |
|---|
| 472 | } |
|---|
| 473 | } |
|---|
| 474 | std::string NSClientT::getBasePath(void) { |
|---|
| 475 | MutexLock lock(internalVariables); |
|---|
| 476 | if (!lock.hasMutex()) { |
|---|
| 477 | LOG_ERROR("FATAL ERROR: Could not get mutex."); |
|---|
| 478 | return "FATAL ERROR"; |
|---|
| 479 | } |
|---|
| 480 | if (!basePath.empty()) |
|---|
| 481 | return basePath; |
|---|
| 482 | char* buffer = new char[1024]; |
|---|
| 483 | GetModuleFileName(NULL, buffer, 1023); |
|---|
| 484 | std::string path = buffer; |
|---|
| 485 | std::string::size_type pos = path.rfind('\\'); |
|---|
| 486 | basePath = path.substr(0, pos) + "\\"; |
|---|
| 487 | delete [] buffer; |
|---|
| 488 | Settings::getInstance()->setFile(basePath + "NSC.ini"); |
|---|
| 489 | return basePath; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | NSCAPI::errorReturn NSAPIGetSettingsString(const char* section, const char* key, const char* defaultValue, char* buffer, unsigned int bufLen) { |
|---|
| 494 | return NSCHelper::wrapReturnString(buffer, bufLen, Settings::getInstance()->getString(section, key, defaultValue), NSCAPI::isSuccess); |
|---|
| 495 | } |
|---|
| 496 | int NSAPIGetSettingsInt(const char* section, const char* key, int defaultValue) { |
|---|
| 497 | return Settings::getInstance()->getInt(section, key, defaultValue); |
|---|
| 498 | } |
|---|
| 499 | NSCAPI::errorReturn NSAPIGetBasePath(char*buffer, unsigned int bufLen) { |
|---|
| 500 | return NSCHelper::wrapReturnString(buffer, bufLen, mainClient.getBasePath(), NSCAPI::isSuccess); |
|---|
| 501 | } |
|---|
| 502 | NSCAPI::errorReturn NSAPIGetApplicationName(char*buffer, unsigned int bufLen) { |
|---|
| 503 | return NSCHelper::wrapReturnString(buffer, bufLen, SZAPPNAME, NSCAPI::isSuccess); |
|---|
| 504 | } |
|---|
| 505 | NSCAPI::errorReturn NSAPIGetApplicationVersionStr(char*buffer, unsigned int bufLen) { |
|---|
| 506 | return NSCHelper::wrapReturnString(buffer, bufLen, SZVERSION, NSCAPI::isSuccess); |
|---|
| 507 | } |
|---|
| 508 | void NSAPIMessage(int msgType, const char* file, const int line, const char* message) { |
|---|
| 509 | mainClient.reportMessage(msgType, file, line, message); |
|---|
| 510 | } |
|---|
| 511 | void NSAPIStopServer(void) { |
|---|
| 512 | serviceControll::Stop(SZSERVICENAME); |
|---|
| 513 | } |
|---|
| 514 | NSCAPI::nagiosReturn NSAPIInject(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen) { |
|---|
| 515 | return mainClient.injectRAW(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); |
|---|
| 516 | } |
|---|
| 517 | NSCAPI::errorReturn NSAPIGetSettingsSection(const char* section, char*** aBuffer, unsigned int * bufLen) { |
|---|
| 518 | unsigned int len = 0; |
|---|
| 519 | *aBuffer = arrayBuffer::list2arrayBuffer(Settings::getInstance()->getSection(section), len); |
|---|
| 520 | *bufLen = len; |
|---|
| 521 | return NSCAPI::isSuccess; |
|---|
| 522 | } |
|---|
| 523 | NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(char*** aBuffer, unsigned int * bufLen) { |
|---|
| 524 | arrayBuffer::destroyArrayBuffer(*aBuffer, *bufLen); |
|---|
| 525 | *bufLen = 0; |
|---|
| 526 | *aBuffer = NULL; |
|---|
| 527 | return NSCAPI::isSuccess; |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | NSCAPI::boolReturn NSAPICheckLogMessages(int messageType) { |
|---|
| 531 | if (mainClient.logDebug()) |
|---|
| 532 | return NSCAPI::istrue; |
|---|
| 533 | return NSCAPI::isfalse; |
|---|
| 534 | } |
|---|
| 535 | |
|---|
| 536 | std::string Encrypt(std::string str, unsigned int algorithm) { |
|---|
| 537 | unsigned int len = 0; |
|---|
| 538 | NSAPIEncrypt(algorithm, str.c_str(), str.size(), NULL, &len); |
|---|
| 539 | len+=2; |
|---|
| 540 | char *buf = new char[len+1]; |
|---|
| 541 | NSCAPI::errorReturn ret = NSAPIEncrypt(algorithm, str.c_str(), str.size(), buf, &len); |
|---|
| 542 | if (ret == NSCAPI::isSuccess) { |
|---|
| 543 | std::string ret = buf; |
|---|
| 544 | delete [] buf; |
|---|
| 545 | return ret; |
|---|
| 546 | } |
|---|
| 547 | return ""; |
|---|
| 548 | } |
|---|
| 549 | std::string Decrypt(std::string str, unsigned int algorithm) { |
|---|
| 550 | unsigned int len = 0; |
|---|
| 551 | NSAPIDecrypt(algorithm, str.c_str(), str.size(), NULL, &len); |
|---|
| 552 | len+=2; |
|---|
| 553 | char *buf = new char[len+1]; |
|---|
| 554 | NSCAPI::errorReturn ret = NSAPIDecrypt(algorithm, str.c_str(), str.size(), buf, &len); |
|---|
| 555 | if (ret == NSCAPI::isSuccess) { |
|---|
| 556 | std::string ret = buf; |
|---|
| 557 | delete [] buf; |
|---|
| 558 | return ret; |
|---|
| 559 | } |
|---|
| 560 | return ""; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | NSCAPI::errorReturn NSAPIEncrypt(unsigned int algorithm, const char* inBuffer, unsigned int inBufLen, char* outBuf, unsigned int *outBufLen) { |
|---|
| 564 | if (algorithm != NSCAPI::xor) { |
|---|
| 565 | LOG_ERROR("Unknown algortihm requested."); |
|---|
| 566 | return NSCAPI::hasFailed; |
|---|
| 567 | } |
|---|
| 568 | std::string s = inBuffer; |
|---|
| 569 | std::string key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT); |
|---|
| 570 | char *c = new char[inBufLen+1]; |
|---|
| 571 | strncpy(c, inBuffer, inBufLen); |
|---|
| 572 | for (unsigned int i=0,j=0;i<inBufLen;i++,j++) { |
|---|
| 573 | if (j > key.size()) |
|---|
| 574 | j = 0; |
|---|
| 575 | c[i] ^= key[j]; |
|---|
| 576 | } |
|---|
| 577 | unsigned int len = b64::b64_encode(reinterpret_cast<void*>(c), inBufLen, outBuf, *outBufLen); |
|---|
| 578 | delete [] c; |
|---|
| 579 | if (outBuf) { |
|---|
| 580 | if ((len == 0)||(len >= *outBufLen)) { |
|---|
| 581 | LOG_ERROR("Invalid out buffer length."); |
|---|
| 582 | return NSCAPI::isInvalidBufferLen; |
|---|
| 583 | } |
|---|
| 584 | outBuf[len] = 0; |
|---|
| 585 | *outBufLen = len; |
|---|
| 586 | } else { |
|---|
| 587 | *outBufLen = len; |
|---|
| 588 | } |
|---|
| 589 | return NSCAPI::isSuccess; |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | NSCAPI::errorReturn NSAPIDecrypt(unsigned int algorithm, const char* inBuffer, unsigned int inBufLen, char* outBuf, unsigned int *outBufLen) { |
|---|
| 593 | if (algorithm != NSCAPI::xor) { |
|---|
| 594 | LOG_ERROR("Unknown algortihm requested."); |
|---|
| 595 | return NSCAPI::hasFailed; |
|---|
| 596 | } |
|---|
| 597 | unsigned int len = b64::b64_decode(inBuffer, inBufLen, reinterpret_cast<void*>(outBuf), *outBufLen); |
|---|
| 598 | if (outBuf) { |
|---|
| 599 | if ((len == 0)||(len >= *outBufLen)) { |
|---|
| 600 | LOG_ERROR("Invalid out buffer length."); |
|---|
| 601 | return NSCAPI::isInvalidBufferLen; |
|---|
| 602 | } |
|---|
| 603 | std::string key = Settings::getInstance()->getString(MAIN_SECTION_TITLE, MAIN_MASTERKEY, MAIN_MASTERKEY_DEFAULT); |
|---|
| 604 | for (unsigned int i=0,j=0;i<len;i++,j++) { |
|---|
| 605 | if (j > key.size()) |
|---|
| 606 | j = 0; |
|---|
| 607 | outBuf[i] ^= key[j]; |
|---|
| 608 | } |
|---|
| 609 | outBuf[len] = 0; |
|---|
| 610 | *outBufLen = len; |
|---|
| 611 | } else { |
|---|
| 612 | *outBufLen = len; |
|---|
| 613 | } |
|---|
| 614 | return NSCAPI::isSuccess; |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | NSCAPI::errorReturn NSAPISetSettingsString(const char* section, const char* key, const char* value) { |
|---|
| 618 | Settings::getInstance()->setString(section, key, value); |
|---|
| 619 | return NSCAPI::isSuccess; |
|---|
| 620 | } |
|---|
| 621 | NSCAPI::errorReturn NSAPISetSettingsInt(const char* section, const char* key, int value) { |
|---|
| 622 | Settings::getInstance()->setInt(section, key, value); |
|---|
| 623 | return NSCAPI::isSuccess; |
|---|
| 624 | } |
|---|
| 625 | NSCAPI::errorReturn NSAPIWriteSettings(int type) { |
|---|
| 626 | try { |
|---|
| 627 | if (type == NSCAPI::settings_registry) |
|---|
| 628 | Settings::getInstance()->write(REGSettings::getType()); |
|---|
| 629 | else if (type == NSCAPI::settings_inifile) |
|---|
| 630 | Settings::getInstance()->write(INISettings::getType()); |
|---|
| 631 | else |
|---|
| 632 | Settings::getInstance()->write(); |
|---|
| 633 | } catch (SettingsException e) { |
|---|
| 634 | LOG_ERROR_STD(e.getMessage()); |
|---|
| 635 | return NSCAPI::hasFailed; |
|---|
| 636 | } |
|---|
| 637 | return NSCAPI::isSuccess; |
|---|
| 638 | } |
|---|
| 639 | NSCAPI::errorReturn NSAPIReadSettings(int type) { |
|---|
| 640 | try { |
|---|
| 641 | if (type == NSCAPI::settings_registry) |
|---|
| 642 | Settings::getInstance()->read(REGSettings::getType()); |
|---|
| 643 | else if (type == NSCAPI::settings_inifile) |
|---|
| 644 | Settings::getInstance()->read(INISettings::getType()); |
|---|
| 645 | else |
|---|
| 646 | Settings::getInstance()->read(); |
|---|
| 647 | } catch (SettingsException e) { |
|---|
| 648 | LOG_ERROR_STD(e.getMessage()); |
|---|
| 649 | return NSCAPI::hasFailed; |
|---|
| 650 | } |
|---|
| 651 | return NSCAPI::isSuccess; |
|---|
| 652 | } |
|---|
| 653 | NSCAPI::errorReturn NSAPIRehash(int flag) { |
|---|
| 654 | return NSCAPI::critical; |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | LPVOID NSAPILoader(char*buffer) { |
|---|
| 659 | if (_stricmp(buffer, "NSAPIGetApplicationName") == 0) |
|---|
| 660 | return &NSAPIGetApplicationName; |
|---|
| 661 | if (_stricmp(buffer, "NSAPIGetApplicationVersionStr") == 0) |
|---|
| 662 | return &NSAPIGetApplicationVersionStr; |
|---|
| 663 | if (_stricmp(buffer, "NSAPIGetSettingsString") == 0) |
|---|
| 664 | return &NSAPIGetSettingsString; |
|---|
| 665 | if (_stricmp(buffer, "NSAPIGetSettingsSection") == 0) |
|---|
| 666 | return &NSAPIGetSettingsSection; |
|---|
| 667 | if (_stricmp(buffer, "NSAPIReleaseSettingsSectionBuffer") == 0) |
|---|
| 668 | return &NSAPIReleaseSettingsSectionBuffer; |
|---|
| 669 | if (_stricmp(buffer, "NSAPIGetSettingsInt") == 0) |
|---|
| 670 | return &NSAPIGetSettingsInt; |
|---|
| 671 | if (_stricmp(buffer, "NSAPIMessage") == 0) |
|---|
| 672 | return &NSAPIMessage; |
|---|
| 673 | if (_stricmp(buffer, "NSAPIStopServer") == 0) |
|---|
| 674 | return &NSAPIStopServer; |
|---|
| 675 | if (_stricmp(buffer, "NSAPIInject") == 0) |
|---|
| 676 | return &NSAPIInject; |
|---|
| 677 | if (_stricmp(buffer, "NSAPIGetBasePath") == 0) |
|---|
| 678 | return &NSAPIGetBasePath; |
|---|
| 679 | if (_stricmp(buffer, "NSAPICheckLogMessages") == 0) |
|---|
| 680 | return &NSAPICheckLogMessages; |
|---|
| 681 | if (_stricmp(buffer, "NSAPIEncrypt") == 0) |
|---|
| 682 | return &NSAPIEncrypt; |
|---|
| 683 | if (_stricmp(buffer, "NSAPIDecrypt") == 0) |
|---|
| 684 | return &NSAPIDecrypt; |
|---|
| 685 | if (_stricmp(buffer, "NSAPISetSettingsString") == 0) |
|---|
| 686 | return &NSAPISetSettingsString; |
|---|
| 687 | if (_stricmp(buffer, "NSAPISetSettingsInt") == 0) |
|---|
| 688 | return &NSAPISetSettingsInt; |
|---|
| 689 | if (_stricmp(buffer, "NSAPIWriteSettings") == 0) |
|---|
| 690 | return &NSAPIWriteSettings; |
|---|
| 691 | if (_stricmp(buffer, "NSAPIReadSettings") == 0) |
|---|
| 692 | return &NSAPIReadSettings; |
|---|
| 693 | if (_stricmp(buffer, "NSAPIRehash") == 0) |
|---|
| 694 | return &NSAPIRehash; |
|---|
| 695 | return NULL; |
|---|
| 696 | } |
|---|