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