| 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 "NSClient++.h"
|
|---|
| 17 | #include "core_api.h"
|
|---|
| 18 | #include <charEx.h>
|
|---|
| 19 | #include <config.h>
|
|---|
| 20 | #include "../version.hpp"
|
|---|
| 21 | #include <arrayBuffer.h>
|
|---|
| 22 | #include <settings/settings_core.hpp>
|
|---|
| 23 | #include "../helpers/settings_manager/settings_manager_impl.h"
|
|---|
| 24 | #include <b64/b64.h>
|
|---|
| 25 | #include <nscapi/nscapi_helper.hpp>
|
|---|
| 26 | #ifdef _WIN32
|
|---|
| 27 | #include <ServiceCmd.h>
|
|---|
| 28 | #endif
|
|---|
| 29 | #include "logger.hpp"
|
|---|
| 30 |
|
|---|
| 31 | using namespace nscp::helpers;
|
|---|
| 32 |
|
|---|
| 33 | #define LOG_ERROR_STD(msg) LOG_ERROR(((std::wstring)msg).c_str())
|
|---|
| 34 | #define LOG_CRITICAL_STD(msg) LOG_CRITICAL(((std::wstring)msg).c_str())
|
|---|
| 35 | #define LOG_MESSAGE_STD(msg) LOG_MESSAGE(((std::wstring)msg).c_str())
|
|---|
| 36 | #define LOG_DEBUG_STD(msg) LOG_DEBUG(((std::wstring)msg).c_str())
|
|---|
| 37 |
|
|---|
| 38 | #define LOG_ERROR(msg) { std::string s = nsclient::logger_helper::create_error(__FILE__, __LINE__, msg); mainClient.reportMessage(s); }
|
|---|
| 39 | #define LOG_CRITICAL(msg) { std::string s = nsclient::logger_helper::create_error(__FILE__, __LINE__, msg); mainClient.reportMessage(s); }
|
|---|
| 40 | #define LOG_MESSAGE(msg) { std::string s = nsclient::logger_helper::create_info(__FILE__, __LINE__, msg); mainClient.reportMessage(s); }
|
|---|
| 41 | #define LOG_DEBUG(msg) { std::string s = nsclient::logger_helper::create_debug(__FILE__, __LINE__, msg); mainClient.reportMessage(s); }
|
|---|
| 42 |
|
|---|
| 43 | NSCAPI::errorReturn NSAPIExpandPath(const wchar_t* key, wchar_t* buffer,unsigned int bufLen) {
|
|---|
| 44 | try {
|
|---|
| 45 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, mainClient.expand_path(key), NSCAPI::isSuccess);
|
|---|
| 46 | } catch (...) {
|
|---|
| 47 | LOG_ERROR_STD(_T("Failed to getString: ") + key);
|
|---|
| 48 | return NSCAPI::hasFailed;
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | NSCAPI::errorReturn NSAPIGetSettingsString(const wchar_t* section, const wchar_t* key, const wchar_t* defaultValue, wchar_t* buffer, unsigned int bufLen) {
|
|---|
| 53 | try {
|
|---|
| 54 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, settings_manager::get_settings()->get_string(section, key, defaultValue), NSCAPI::isSuccess);
|
|---|
| 55 | } catch (settings::settings_exception e) {
|
|---|
| 56 | LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage());
|
|---|
| 57 | return NSCAPI::hasFailed;
|
|---|
| 58 | } catch (const std::exception &e) {
|
|---|
| 59 | LOG_ERROR_STD(_T("Failed to get key: ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 60 | return NSCAPI::hasFailed;
|
|---|
| 61 | } catch (...) {
|
|---|
| 62 | LOG_ERROR_STD(_T("Failed to get key: <UNKNOWN EXCEPTION>"));
|
|---|
| 63 | return NSCAPI::hasFailed;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | int NSAPIGetSettingsInt(const wchar_t* section, const wchar_t* key, int defaultValue) {
|
|---|
| 67 | try {
|
|---|
| 68 | return settings_manager::get_settings()->get_int(section, key, defaultValue);
|
|---|
| 69 | } catch (settings::settings_exception e) {
|
|---|
| 70 | LOG_ERROR_STD(_T("Failed to set settings file") + e.getMessage());
|
|---|
| 71 | return defaultValue;
|
|---|
| 72 | } catch (const std::exception &e) {
|
|---|
| 73 | LOG_ERROR_STD(_T("Failed to get key: ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 74 | return defaultValue;
|
|---|
| 75 | } catch (...) {
|
|---|
| 76 | LOG_ERROR_STD(_T("Failed to get key: <UNKNOWN EXCEPTION>"));
|
|---|
| 77 | return defaultValue;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | int NSAPIGetSettingsBool(const wchar_t* section, const wchar_t* key, int defaultValue) {
|
|---|
| 81 | try {
|
|---|
| 82 | return settings_manager::get_settings()->get_bool(section, key, defaultValue==1);
|
|---|
| 83 | } catch (settings::settings_exception e) {
|
|---|
| 84 | LOG_ERROR_STD(_T("Failed to get key: ") + e.getMessage());
|
|---|
| 85 | return defaultValue;
|
|---|
| 86 | } catch (const std::exception &e) {
|
|---|
| 87 | LOG_ERROR_STD(_T("Failed to get key: ") + utf8::cvt<std::wstring>(e.what()));
|
|---|
| 88 | return defaultValue;
|
|---|
| 89 | } catch (...) {
|
|---|
| 90 | LOG_ERROR_STD(_T("Failed to get key: <UNKNOWN EXCEPTION>"));
|
|---|
| 91 | return defaultValue;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | NSCAPI::errorReturn NSAPIGetBasePath(wchar_t*buffer, unsigned int bufLen) {
|
|---|
| 95 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, mainClient.getBasePath().string(), NSCAPI::isSuccess);
|
|---|
| 96 | }
|
|---|
| 97 | NSCAPI::errorReturn NSAPIGetApplicationName(wchar_t*buffer, unsigned int bufLen) {
|
|---|
| 98 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, APPLICATION_NAME, NSCAPI::isSuccess);
|
|---|
| 99 | }
|
|---|
| 100 | NSCAPI::errorReturn NSAPIGetApplicationVersionStr(wchar_t*buffer, unsigned int bufLen) {
|
|---|
| 101 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, CURRENT_SERVICE_VERSION, NSCAPI::isSuccess);
|
|---|
| 102 | }
|
|---|
| 103 | void NSAPIMessage(const char* data, unsigned int count) {
|
|---|
| 104 | mainClient.reportMessage(std::string(data, count));
|
|---|
| 105 | }
|
|---|
| 106 | void NSAPIStopServer(void) {
|
|---|
| 107 | mainClient.get_service_control().stop();
|
|---|
| 108 | }
|
|---|
| 109 | NSCAPI::nagiosReturn NSAPIInject(const wchar_t* command, const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len) {
|
|---|
| 110 | std::string request (request_buffer, request_buffer_len), response;
|
|---|
| 111 | NSCAPI::nagiosReturn ret = mainClient.injectRAW(command, request, response);
|
|---|
| 112 | *response_buffer_len = response.size();
|
|---|
| 113 | if (response.empty())
|
|---|
| 114 | *response_buffer = NULL;
|
|---|
| 115 | else {
|
|---|
| 116 | *response_buffer = new char[*response_buffer_len + 10];
|
|---|
| 117 | memcpy(*response_buffer, response.c_str(), *response_buffer_len);
|
|---|
| 118 | }
|
|---|
| 119 | return ret;
|
|---|
| 120 | }
|
|---|
| 121 | NSCAPI::errorReturn NSAPIGetSettingsSection(const wchar_t* section, wchar_t*** aBuffer, unsigned int * bufLen) {
|
|---|
| 122 | try {
|
|---|
| 123 | unsigned int len = 0;
|
|---|
| 124 | *aBuffer = array_buffer::list2arrayBuffer(settings_manager::get_settings()->get_keys(section), len);
|
|---|
| 125 | *bufLen = len;
|
|---|
| 126 | return NSCAPI::isSuccess;
|
|---|
| 127 | } catch (settings::settings_exception e) {
|
|---|
| 128 | LOG_ERROR_STD(_T("Failed to get section: ") + e.getMessage());
|
|---|
| 129 | } catch (...) {
|
|---|
| 130 | LOG_ERROR_STD(_T("Failed to getSection: ") + section);
|
|---|
| 131 | }
|
|---|
| 132 | return NSCAPI::hasFailed;
|
|---|
| 133 | }
|
|---|
| 134 | NSCAPI::errorReturn NSAPIReleaseSettingsSectionBuffer(wchar_t*** aBuffer, unsigned int * bufLen) {
|
|---|
| 135 | array_buffer::destroyArrayBuffer(*aBuffer, *bufLen);
|
|---|
| 136 | *bufLen = 0;
|
|---|
| 137 | *aBuffer = NULL;
|
|---|
| 138 | return NSCAPI::isSuccess;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | NSCAPI::boolReturn NSAPICheckLogMessages(int messageType) {
|
|---|
| 142 | if (mainClient.logDebug())
|
|---|
| 143 | return NSCAPI::istrue;
|
|---|
| 144 | return NSCAPI::isfalse;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | NSCAPI::errorReturn NSAPIEncrypt(unsigned int algorithm, const wchar_t* inBuffer, unsigned int inBufLen, wchar_t* outBuf, unsigned int *outBufLen) {
|
|---|
| 148 | if (algorithm != NSCAPI::encryption_xor) {
|
|---|
| 149 | LOG_ERROR(_T("Unknown algortihm requested."));
|
|---|
| 150 | return NSCAPI::hasFailed;
|
|---|
| 151 | }
|
|---|
| 152 | /*
|
|---|
| 153 | TODO reimplement this
|
|---|
| 154 |
|
|---|
| 155 | std::wstring key = settings_manager::get_settings()->get_string(SETTINGS_KEY(protocol_def::MASTER_KEY));
|
|---|
| 156 | int tcharInBufLen = 0;
|
|---|
| 157 | char *c = charEx::tchar_to_char(inBuffer, inBufLen, tcharInBufLen);
|
|---|
| 158 | std::wstring::size_type j=0;
|
|---|
| 159 | for (int i=0;i<tcharInBufLen;i++,j++) {
|
|---|
| 160 | if (j > key.size())
|
|---|
| 161 | j = 0;
|
|---|
| 162 | c[i] ^= key[j];
|
|---|
| 163 | }
|
|---|
| 164 | size_t cOutBufLen = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, NULL, NULL);
|
|---|
| 165 | if (!outBuf) {
|
|---|
| 166 | *outBufLen = static_cast<unsigned int>(cOutBufLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work
|
|---|
| 167 | return NSCAPI::isSuccess;
|
|---|
| 168 | }
|
|---|
| 169 | char *cOutBuf = new char[cOutBufLen+1];
|
|---|
| 170 | size_t len = b64::b64_encode(reinterpret_cast<void*>(c), tcharInBufLen, cOutBuf, cOutBufLen);
|
|---|
| 171 | delete [] c;
|
|---|
| 172 | if (len == 0) {
|
|---|
| 173 | LOG_ERROR(_T("Invalid out buffer length."));
|
|---|
| 174 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 175 | }
|
|---|
| 176 | int realOutLen;
|
|---|
| 177 | wchar_t *realOut = charEx::char_to_tchar(cOutBuf, cOutBufLen, realOutLen);
|
|---|
| 178 | if (static_cast<unsigned int>(realOutLen) >= *outBufLen) {
|
|---|
| 179 | LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated."));
|
|---|
| 180 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 181 | }
|
|---|
| 182 | wcsncpy(outBuf, *outBufLen, realOut);
|
|---|
| 183 | delete [] realOut;
|
|---|
| 184 | outBuf[realOutLen] = 0;
|
|---|
| 185 | *outBufLen = static_cast<unsigned int>(realOutLen);
|
|---|
| 186 | */
|
|---|
| 187 | return NSCAPI::isSuccess;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | NSCAPI::errorReturn NSAPIDecrypt(unsigned int algorithm, const wchar_t* inBuffer, unsigned int inBufLen, wchar_t* outBuf, unsigned int *outBufLen) {
|
|---|
| 191 | if (algorithm != NSCAPI::encryption_xor) {
|
|---|
| 192 | LOG_ERROR(_T("Unknown algortihm requested."));
|
|---|
| 193 | return NSCAPI::hasFailed;
|
|---|
| 194 | }
|
|---|
| 195 | /*
|
|---|
| 196 | int inBufLenC = 0;
|
|---|
| 197 | char *inBufferC = charEx::tchar_to_char(inBuffer, inBufLen, inBufLenC);
|
|---|
| 198 | size_t cOutLen = b64::b64_decode(inBufferC, inBufLenC, NULL, NULL);
|
|---|
| 199 | if (!outBuf) {
|
|---|
| 200 | *outBufLen = static_cast<unsigned int>(cOutLen*2); // TODO: Guessing wildly here but no proper way to tell without a lot of extra work
|
|---|
| 201 | return NSCAPI::isSuccess;
|
|---|
| 202 | }
|
|---|
| 203 | char *cOutBuf = new char[cOutLen+1];
|
|---|
| 204 | size_t len = b64::b64_decode(inBufferC, inBufLenC, reinterpret_cast<void*>(cOutBuf), cOutLen);
|
|---|
| 205 | delete [] inBufferC;
|
|---|
| 206 | if (len == 0) {
|
|---|
| 207 | LOG_ERROR(_T("Invalid out buffer length."));
|
|---|
| 208 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 209 | }
|
|---|
| 210 | int realOutLen;
|
|---|
| 211 |
|
|---|
| 212 | std::wstring key = settings_manager::get_settings()->get_string(SETTINGS_KEY(protocol_def::MASTER_KEY));
|
|---|
| 213 | std::wstring::size_type j=0;
|
|---|
| 214 | for (int i=0;i<cOutLen;i++,j++) {
|
|---|
| 215 | if (j > key.size())
|
|---|
| 216 | j = 0;
|
|---|
| 217 | cOutBuf[i] ^= key[j];
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | wchar_t *realOut = charEx::char_to_tchar(cOutBuf, cOutLen, realOutLen);
|
|---|
| 221 | if (static_cast<unsigned int>(realOutLen) >= *outBufLen) {
|
|---|
| 222 | LOG_ERROR_STD(_T("Invalid out buffer length: ") + strEx::itos(realOutLen) + _T(" was needed but only ") + strEx::itos(*outBufLen) + _T(" was allocated."));
|
|---|
| 223 | return NSCAPI::isInvalidBufferLen;
|
|---|
| 224 | }
|
|---|
| 225 | wcsncpy(outBuf, *outBufLen, realOut);
|
|---|
| 226 | delete [] realOut;
|
|---|
| 227 | outBuf[realOutLen] = 0;
|
|---|
| 228 | *outBufLen = static_cast<unsigned int>(realOutLen);
|
|---|
| 229 | */
|
|---|
| 230 | return NSCAPI::isSuccess;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | NSCAPI::errorReturn NSAPISetSettingsString(const wchar_t* section, const wchar_t* key, const wchar_t* value) {
|
|---|
| 234 | try {
|
|---|
| 235 | settings_manager::get_settings()->set_string(section, key, value);
|
|---|
| 236 | } catch (...) {
|
|---|
| 237 | LOG_ERROR_STD(_T("Failed to setString: ") + key);
|
|---|
| 238 | return NSCAPI::hasFailed;
|
|---|
| 239 | }
|
|---|
| 240 | return NSCAPI::isSuccess;
|
|---|
| 241 | }
|
|---|
| 242 | NSCAPI::errorReturn NSAPISetSettingsInt(const wchar_t* section, const wchar_t* key, int value) {
|
|---|
| 243 | try {
|
|---|
| 244 | settings_manager::get_settings()->set_int(section, key, value);
|
|---|
| 245 | } catch (...) {
|
|---|
| 246 | LOG_ERROR_STD(_T("Failed to setInt: ") + key);
|
|---|
| 247 | return NSCAPI::hasFailed;
|
|---|
| 248 | }
|
|---|
| 249 | return NSCAPI::isSuccess;
|
|---|
| 250 | }
|
|---|
| 251 | NSCAPI::errorReturn NSAPIWriteSettings(const wchar_t* key) {
|
|---|
| 252 | try {
|
|---|
| 253 | settings::instance_ptr inst = settings_manager::get_core()->create_instance(key);
|
|---|
| 254 | if (!inst) {
|
|---|
| 255 | LOG_ERROR_STD(_T("Failed to create settings: ") + key);
|
|---|
| 256 | return NSCAPI::hasFailed;
|
|---|
| 257 | }
|
|---|
| 258 | settings_manager::get_core()->migrate_to(inst);
|
|---|
| 259 | } catch (settings::settings_exception e) {
|
|---|
| 260 | LOG_ERROR_STD(_T("Failed to write settings: ") + e.getMessage());
|
|---|
| 261 | return NSCAPI::hasFailed;
|
|---|
| 262 | } catch (...) {
|
|---|
| 263 | LOG_ERROR_STD(_T("Failed to write settings"));
|
|---|
| 264 | return NSCAPI::hasFailed;
|
|---|
| 265 | }
|
|---|
| 266 | return NSCAPI::isSuccess;
|
|---|
| 267 | }
|
|---|
| 268 | NSCAPI::errorReturn NSAPIReadSettings(const wchar_t* key) {
|
|---|
| 269 | try {
|
|---|
| 270 | settings::instance_ptr inst = settings_manager::get_core()->create_instance(key);
|
|---|
| 271 | if (!inst) {
|
|---|
| 272 | LOG_ERROR_STD(_T("Failed to create settings: ") + key);
|
|---|
| 273 | return NSCAPI::hasFailed;
|
|---|
| 274 | }
|
|---|
| 275 | settings_manager::get_core()->migrate_from(inst);
|
|---|
| 276 | settings_manager::get_settings()->reload();
|
|---|
| 277 | } catch (settings::settings_exception e) {
|
|---|
| 278 | LOG_ERROR_STD(_T("Failed to read settings: ") + e.getMessage());
|
|---|
| 279 | return NSCAPI::hasFailed;
|
|---|
| 280 | } catch (...) {
|
|---|
| 281 | LOG_ERROR_STD(_T("Failed to read settings"));
|
|---|
| 282 | return NSCAPI::hasFailed;
|
|---|
| 283 | }
|
|---|
| 284 | return NSCAPI::isSuccess;
|
|---|
| 285 | }
|
|---|
| 286 | NSCAPI::errorReturn NSAPIRehash(int flag) {
|
|---|
| 287 | return NSCAPI::hasFailed;
|
|---|
| 288 | }
|
|---|
| 289 | NSCAPI::errorReturn NSAPIDescribeCommand(const wchar_t* command, wchar_t* buffer, unsigned int bufLen) {
|
|---|
| 290 | return nscapi::plugin_helper::wrapReturnString(buffer, bufLen, mainClient.describeCommand(command), NSCAPI::isSuccess);
|
|---|
| 291 | }
|
|---|
| 292 | NSCAPI::errorReturn NSAPIGetAllCommandNames(array_buffer::arrayBuffer* aBuffer, unsigned int *bufLen) {
|
|---|
| 293 | unsigned int len = 0;
|
|---|
| 294 | *aBuffer = array_buffer::list2arrayBuffer(mainClient.getAllCommandNames(), len);
|
|---|
| 295 | *bufLen = len;
|
|---|
| 296 | return NSCAPI::isSuccess;
|
|---|
| 297 | }
|
|---|
| 298 | NSCAPI::errorReturn NSAPIReleaseAllCommandNamessBuffer(wchar_t*** aBuffer, unsigned int * bufLen) {
|
|---|
| 299 | array_buffer::destroyArrayBuffer(*aBuffer, *bufLen);
|
|---|
| 300 | *bufLen = 0;
|
|---|
| 301 | *aBuffer = NULL;
|
|---|
| 302 | return NSCAPI::isSuccess;
|
|---|
| 303 | }
|
|---|
| 304 | NSCAPI::errorReturn NSAPIRegisterCommand(unsigned int id, const wchar_t* cmd,const wchar_t* desc) {
|
|---|
| 305 | try {
|
|---|
| 306 | mainClient.registerCommand(id, cmd, desc);
|
|---|
| 307 | } catch (nsclient::commands::command_exception &e) {
|
|---|
| 308 | LOG_ERROR_STD(_T("Exception registrying command '") + cmd + _T("': ") + ::to_wstring(e.what()) + _T(", from: ") + to_wstring(id));
|
|---|
| 309 | return NSCAPI::isfalse;
|
|---|
| 310 | } catch (...) {
|
|---|
| 311 | LOG_ERROR_STD(_T("Unknown exception registrying command: ") + std::wstring(cmd) + _T(", from: ") + to_wstring(id));
|
|---|
| 312 | return NSCAPI::isfalse;
|
|---|
| 313 | }
|
|---|
| 314 | return NSCAPI::isSuccess;
|
|---|
| 315 | }
|
|---|
| 316 | NSCAPI::errorReturn NSAPISettingsRegKey(const wchar_t* path, const wchar_t* key, int type, const wchar_t* title, const wchar_t* description, const wchar_t* defVal, int advanced) {
|
|---|
| 317 | try {
|
|---|
| 318 | if (type == NSCAPI::key_string)
|
|---|
| 319 | settings_manager::get_core()->register_key(path, key, settings::settings_core::key_string, title, description, defVal, advanced);
|
|---|
| 320 | if (type == NSCAPI::key_bool)
|
|---|
| 321 | settings_manager::get_core()->register_key(path, key, settings::settings_core::key_bool, title, description, defVal, advanced);
|
|---|
| 322 | if (type == NSCAPI::key_integer)
|
|---|
| 323 | settings_manager::get_core()->register_key(path, key, settings::settings_core::key_integer, title, description, defVal, advanced);
|
|---|
| 324 | return NSCAPI::hasFailed;
|
|---|
| 325 | } catch (settings::settings_exception e) {
|
|---|
| 326 | LOG_ERROR_STD(_T("Failed register key: ") + e.getMessage());
|
|---|
| 327 | return NSCAPI::hasFailed;
|
|---|
| 328 | } catch (...) {
|
|---|
| 329 | LOG_ERROR_STD(_T("Failed register key"));
|
|---|
| 330 | return NSCAPI::hasFailed;
|
|---|
| 331 | }
|
|---|
| 332 | return NSCAPI::isSuccess;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 | NSCAPI::errorReturn NSAPISettingsRegPath(const wchar_t* path, const wchar_t* title, const wchar_t* description, int advanced) {
|
|---|
| 337 | try {
|
|---|
| 338 | settings_manager::get_core()->register_path(path, title, description, advanced);
|
|---|
| 339 | } catch (settings::settings_exception e) {
|
|---|
| 340 | LOG_ERROR_STD(_T("Failed register path: ") + e.getMessage());
|
|---|
| 341 | return NSCAPI::hasFailed;
|
|---|
| 342 | } catch (...) {
|
|---|
| 343 | LOG_ERROR_STD(_T("Failed register path"));
|
|---|
| 344 | return NSCAPI::hasFailed;
|
|---|
| 345 | }
|
|---|
| 346 | return NSCAPI::isSuccess;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | //int wmain(int argc, wchar_t* argv[], wchar_t* envp[])
|
|---|
| 350 | wchar_t* copyString(const std::wstring &str) {
|
|---|
| 351 | int sz = str.size();
|
|---|
| 352 | wchar_t *tc = new wchar_t[sz+2];
|
|---|
| 353 | wcsncpy(tc, str.c_str(), sz);
|
|---|
| 354 | return tc;
|
|---|
| 355 | }
|
|---|
| 356 | NSCAPI::errorReturn NSAPIGetPluginList(int *len, NSCAPI::plugin_info *list[]) {
|
|---|
| 357 | // NSClientT::plugin_info_list plugList= mainClient.get_all_plugins();
|
|---|
| 358 | *len = 0; //plugList.size();
|
|---|
| 359 |
|
|---|
| 360 | *list = new NSCAPI::plugin_info[*len+1];
|
|---|
| 361 | /*
|
|---|
| 362 | int i=0;
|
|---|
| 363 | for(NSClientT::plugin_info_list::const_iterator cit = plugList.begin(); cit != plugList.end(); ++cit,i++) {
|
|---|
| 364 | (*list)[i].dll = copyString((*cit).dll);
|
|---|
| 365 | (*list)[i].name = copyString((*cit).name);
|
|---|
| 366 | (*list)[i].version = copyString((*cit).version);
|
|---|
| 367 | (*list)[i].description = copyString((*cit).description);
|
|---|
| 368 | }
|
|---|
| 369 | */
|
|---|
| 370 | return NSCAPI::isSuccess;
|
|---|
| 371 | }
|
|---|
| 372 | NSCAPI::errorReturn NSAPIReleasePluginList(int len, NSCAPI::plugin_info *list[]) {
|
|---|
| 373 | for (int i=0;i<len;i++) {
|
|---|
| 374 | delete [] (*list)[i].dll;
|
|---|
| 375 | delete [] (*list)[i].name;
|
|---|
| 376 | delete [] (*list)[i].version;
|
|---|
| 377 | delete [] (*list)[i].description;
|
|---|
| 378 | }
|
|---|
| 379 | delete [] *list;
|
|---|
| 380 | return NSCAPI::isSuccess;
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 | NSCAPI::errorReturn NSAPISettingsSave(void) {
|
|---|
| 385 | try {
|
|---|
| 386 | settings_manager::get_settings()->save();
|
|---|
| 387 | } catch (settings::settings_exception e) {
|
|---|
| 388 | LOG_ERROR_STD(_T("Failed to save: ") + e.getMessage());
|
|---|
| 389 | return NSCAPI::hasFailed;
|
|---|
| 390 | } catch (...) {
|
|---|
| 391 | LOG_ERROR_STD(_T("Failed to save"));
|
|---|
| 392 | return NSCAPI::hasFailed;
|
|---|
| 393 | }
|
|---|
| 394 | return NSCAPI::isSuccess;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 | LPVOID NSAPILoader(const wchar_t*buffer) {
|
|---|
| 400 | if (wcscasecmp(buffer, _T("NSAPIGetApplicationName")) == 0)
|
|---|
| 401 | return reinterpret_cast<LPVOID>(&NSAPIGetApplicationName);
|
|---|
| 402 | if (wcscasecmp(buffer, _T("NSAPIGetApplicationVersionStr")) == 0)
|
|---|
| 403 | return reinterpret_cast<LPVOID>(&NSAPIGetApplicationVersionStr);
|
|---|
| 404 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsString")) == 0)
|
|---|
| 405 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsString);
|
|---|
| 406 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsSection")) == 0)
|
|---|
| 407 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsSection);
|
|---|
| 408 | if (wcscasecmp(buffer, _T("NSAPIReleaseSettingsSectionBuffer")) == 0)
|
|---|
| 409 | return reinterpret_cast<LPVOID>(&NSAPIReleaseSettingsSectionBuffer);
|
|---|
| 410 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsInt")) == 0)
|
|---|
| 411 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsInt);
|
|---|
| 412 | if (wcscasecmp(buffer, _T("NSAPIGetSettingsBool")) == 0)
|
|---|
| 413 | return reinterpret_cast<LPVOID>(&NSAPIGetSettingsBool);
|
|---|
| 414 | if (wcscasecmp(buffer, _T("NSAPIMessage")) == 0)
|
|---|
| 415 | return reinterpret_cast<LPVOID>(&NSAPIMessage);
|
|---|
| 416 | if (wcscasecmp(buffer, _T("NSAPIStopServer")) == 0)
|
|---|
| 417 | return reinterpret_cast<LPVOID>(&NSAPIStopServer);
|
|---|
| 418 | if (wcscasecmp(buffer, _T("NSAPIInject")) == 0)
|
|---|
| 419 | return reinterpret_cast<LPVOID>(&NSAPIInject);
|
|---|
| 420 | if (wcscasecmp(buffer, _T("NSAPIGetBasePath")) == 0)
|
|---|
| 421 | return reinterpret_cast<LPVOID>(&NSAPIGetBasePath);
|
|---|
| 422 | if (wcscasecmp(buffer, _T("NSAPICheckLogMessages")) == 0)
|
|---|
| 423 | return reinterpret_cast<LPVOID>(&NSAPICheckLogMessages);
|
|---|
| 424 | if (wcscasecmp(buffer, _T("NSAPIEncrypt")) == 0)
|
|---|
| 425 | return reinterpret_cast<LPVOID>(&NSAPIEncrypt);
|
|---|
| 426 | if (wcscasecmp(buffer, _T("NSAPIDecrypt")) == 0)
|
|---|
| 427 | return reinterpret_cast<LPVOID>(&NSAPIDecrypt);
|
|---|
| 428 | if (wcscasecmp(buffer, _T("NSAPISetSettingsString")) == 0)
|
|---|
| 429 | return reinterpret_cast<LPVOID>(&NSAPISetSettingsString);
|
|---|
| 430 | if (wcscasecmp(buffer, _T("NSAPISetSettingsInt")) == 0)
|
|---|
| 431 | return reinterpret_cast<LPVOID>(&NSAPISetSettingsInt);
|
|---|
| 432 | if (wcscasecmp(buffer, _T("NSAPIWriteSettings")) == 0)
|
|---|
| 433 | return reinterpret_cast<LPVOID>(&NSAPIWriteSettings);
|
|---|
| 434 | if (wcscasecmp(buffer, _T("NSAPIReadSettings")) == 0)
|
|---|
| 435 | return reinterpret_cast<LPVOID>(&NSAPIReadSettings);
|
|---|
| 436 | if (wcscasecmp(buffer, _T("NSAPIRehash")) == 0)
|
|---|
| 437 | return reinterpret_cast<LPVOID>(&NSAPIRehash);
|
|---|
| 438 | if (wcscasecmp(buffer, _T("NSAPIDescribeCommand")) == 0)
|
|---|
| 439 | return reinterpret_cast<LPVOID>(&NSAPIDescribeCommand);
|
|---|
| 440 | if (wcscasecmp(buffer, _T("NSAPIGetAllCommandNames")) == 0)
|
|---|
| 441 | return reinterpret_cast<LPVOID>(&NSAPIGetAllCommandNames);
|
|---|
| 442 | if (wcscasecmp(buffer, _T("NSAPIReleaseAllCommandNamessBuffer")) == 0)
|
|---|
| 443 | return reinterpret_cast<LPVOID>(&NSAPIReleaseAllCommandNamessBuffer);
|
|---|
| 444 | if (wcscasecmp(buffer, _T("NSAPIRegisterCommand")) == 0)
|
|---|
| 445 | return reinterpret_cast<LPVOID>(&NSAPIRegisterCommand);
|
|---|
| 446 | if (wcscasecmp(buffer, _T("NSAPISettingsRegKey")) == 0)
|
|---|
| 447 | return reinterpret_cast<LPVOID>(&NSAPISettingsRegKey);
|
|---|
| 448 | if (wcscasecmp(buffer, _T("NSAPISettingsRegPath")) == 0)
|
|---|
| 449 | return reinterpret_cast<LPVOID>(&NSAPISettingsRegPath);
|
|---|
| 450 | if (wcscasecmp(buffer, _T("NSAPIGetPluginList")) == 0)
|
|---|
| 451 | return reinterpret_cast<LPVOID>(&NSAPIGetPluginList);
|
|---|
| 452 | if (wcscasecmp(buffer, _T("NSAPIReleasePluginList")) == 0)
|
|---|
| 453 | return reinterpret_cast<LPVOID>(&NSAPIReleasePluginList);
|
|---|
| 454 | if (wcscasecmp(buffer, _T("NSAPISettingsSave")) == 0)
|
|---|
| 455 | return reinterpret_cast<LPVOID>(&NSAPISettingsSave);
|
|---|
| 456 | if (wcscasecmp(buffer, _T("NSAPINotify")) == 0)
|
|---|
| 457 | return reinterpret_cast<LPVOID>(&NSAPINotify);
|
|---|
| 458 | if (wcscasecmp(buffer, _T("NSAPIDestroyBuffer")) == 0)
|
|---|
| 459 | return reinterpret_cast<LPVOID>(&NSAPIDestroyBuffer);
|
|---|
| 460 | if (wcscasecmp(buffer, _T("NSAPIExpandPath")) == 0)
|
|---|
| 461 | return reinterpret_cast<LPVOID>(&NSAPIExpandPath);
|
|---|
| 462 |
|
|---|
| 463 | LOG_ERROR_STD(_T("Function not found: ") + buffer);
|
|---|
| 464 | return NULL;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | NSCAPI::errorReturn NSAPINotify(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, char* result, unsigned int result_len) {
|
|---|
| 468 | return mainClient.send_notification(channel, command, code, result, result_len);
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | void NSAPIDestroyBuffer(char**buffer) {
|
|---|
| 472 | delete [] *buffer;
|
|---|
| 473 | } |
|---|