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