| 1 | #include "stdafx.h" |
|---|
| 2 | #include <NSCHelper.h> |
|---|
| 3 | #include <assert.h> |
|---|
| 4 | |
|---|
| 5 | #define BUFF_LEN 4096 |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #ifdef DEBUG |
|---|
| 9 | /** |
|---|
| 10 | * Wrap a return string. |
|---|
| 11 | * This function copies a string to a char buffer making sure the buffer has the correct length. |
|---|
| 12 | * |
|---|
| 13 | * @param *buffer Buffer to copy the string to. |
|---|
| 14 | * @param bufLen Length of the buffer |
|---|
| 15 | * @param str Th string to copy |
|---|
| 16 | * @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen |
|---|
| 17 | */ |
|---|
| 18 | NSCAPI::nagiosReturn NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::nagiosReturn defaultReturnCode /* = NSCAPI::success */) { |
|---|
| 19 | if (str.length() >= bufLen) |
|---|
| 20 | return NSCAPI::returnInvalidBufferLen; |
|---|
| 21 | strncpy(buffer, str.c_str(), bufLen); |
|---|
| 22 | return defaultReturnCode; |
|---|
| 23 | } |
|---|
| 24 | /** |
|---|
| 25 | * Wrap a return string. |
|---|
| 26 | * This function copies a string to a char buffer making sure the buffer has the correct length. |
|---|
| 27 | * |
|---|
| 28 | * @param *buffer Buffer to copy the string to. |
|---|
| 29 | * @param bufLen Length of the buffer |
|---|
| 30 | * @param str Th string to copy |
|---|
| 31 | * @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen |
|---|
| 32 | */ |
|---|
| 33 | NSCAPI::errorReturn NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, NSCAPI::errorReturn defaultReturnCode /* = NSCAPI::success */) { |
|---|
| 34 | if (str.length() >= bufLen) |
|---|
| 35 | return NSCAPI::isInvalidBufferLen; |
|---|
| 36 | strncpy(buffer, str.c_str(), bufLen); |
|---|
| 37 | return defaultReturnCode; |
|---|
| 38 | } |
|---|
| 39 | #else |
|---|
| 40 | /** |
|---|
| 41 | * Wrap a return string. |
|---|
| 42 | * This function copies a string to a char buffer making sure the buffer has the correct length. |
|---|
| 43 | * |
|---|
| 44 | * @param *buffer Buffer to copy the string to. |
|---|
| 45 | * @param bufLen Length of the buffer |
|---|
| 46 | * @param str Th string to copy |
|---|
| 47 | * @param defaultReturnCode The default return code |
|---|
| 48 | * @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen |
|---|
| 49 | */ |
|---|
| 50 | int NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode ) { |
|---|
| 51 | // @todo deprecate this |
|---|
| 52 | if (str.length() >= bufLen) |
|---|
| 53 | return NSCAPI::isInvalidBufferLen; |
|---|
| 54 | strncpy(buffer, str.c_str(), bufLen); |
|---|
| 55 | return defaultReturnCode; |
|---|
| 56 | } |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Translate a message type into a human readable string. |
|---|
| 62 | * |
|---|
| 63 | * @param msgType The message type |
|---|
| 64 | * @return A string representing the message type |
|---|
| 65 | */ |
|---|
| 66 | std::string NSCHelper::translateMessageType(NSCAPI::messageTypes msgType) { |
|---|
| 67 | switch (msgType) { |
|---|
| 68 | case NSCAPI::error: |
|---|
| 69 | return "error"; |
|---|
| 70 | case NSCAPI::critical: |
|---|
| 71 | return "critical"; |
|---|
| 72 | case NSCAPI::warning: |
|---|
| 73 | return "warning"; |
|---|
| 74 | case NSCAPI::log: |
|---|
| 75 | return "message"; |
|---|
| 76 | case NSCAPI::debug: |
|---|
| 77 | return "debug"; |
|---|
| 78 | } |
|---|
| 79 | return "unknown"; |
|---|
| 80 | } |
|---|
| 81 | /** |
|---|
| 82 | * Translate a return code into the corresponding string |
|---|
| 83 | * @param returnCode |
|---|
| 84 | * @return |
|---|
| 85 | */ |
|---|
| 86 | std::string NSCHelper::translateReturn(NSCAPI::nagiosReturn returnCode) { |
|---|
| 87 | if (returnCode == NSCAPI::returnOK) |
|---|
| 88 | return "OK"; |
|---|
| 89 | else if (returnCode == NSCAPI::returnCRIT) |
|---|
| 90 | return "CRITICAL"; |
|---|
| 91 | else if (returnCode == NSCAPI::returnWARN) |
|---|
| 92 | return "WARNING"; |
|---|
| 93 | else |
|---|
| 94 | return "UNKNOWN"; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | namespace NSCModuleHelper { |
|---|
| 100 | lpNSAPIGetBasePath fNSAPIGetBasePath = NULL; |
|---|
| 101 | lpNSAPIGetApplicationName fNSAPIGetApplicationName = NULL; |
|---|
| 102 | lpNSAPIGetApplicationVersionStr fNSAPIGetApplicationVersionStr = NULL; |
|---|
| 103 | lpNSAPIGetSettingsSection fNSAPIGetSettingsSection = NULL; |
|---|
| 104 | lpNSAPIReleaseSettingsSectionBuffer fNSAPIReleaseSettingsSectionBuffer = NULL; |
|---|
| 105 | lpNSAPIGetSettingsString fNSAPIGetSettingsString = NULL; |
|---|
| 106 | lpNSAPIGetSettingsInt fNSAPIGetSettingsInt = NULL; |
|---|
| 107 | lpNSAPIMessage fNSAPIMessage = NULL; |
|---|
| 108 | lpNSAPIStopServer fNSAPIStopServer = NULL; |
|---|
| 109 | lpNSAPIInject fNSAPIInject = NULL; |
|---|
| 110 | lpNSAPICheckLogMessages fNSAPICheckLogMessages = NULL; |
|---|
| 111 | lpNSAPIEncrypt fNSAPIEncrypt = NULL; |
|---|
| 112 | lpNSAPIDecrypt fNSAPIDecrypt = NULL; |
|---|
| 113 | lpNSAPISetSettingsString fNSAPISetSettingsString = NULL; |
|---|
| 114 | lpNSAPISetSettingsInt fNSAPISetSettingsInt = NULL; |
|---|
| 115 | lpNSAPIWriteSettings fNSAPIWriteSettings = NULL; |
|---|
| 116 | lpNSAPIReadSettings fNSAPIReadSettings = NULL; |
|---|
| 117 | lpNSAPIRehash fNSAPIRehash = NULL; |
|---|
| 118 | |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 122 | // Callbacks into the core |
|---|
| 123 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Callback to send a message through to the core |
|---|
| 127 | * |
|---|
| 128 | * @param msgType Message type (debug, warning, etc.) |
|---|
| 129 | * @param file File where message was generated (__FILE__) |
|---|
| 130 | * @param line Line where message was generated (__LINE__) |
|---|
| 131 | * @param message Message in human readable format |
|---|
| 132 | * @throws NSCMHExcpetion When core pointer set is unavailable. |
|---|
| 133 | */ |
|---|
| 134 | void NSCModuleHelper::Message(int msgType, std::string file, int line, std::string message) { |
|---|
| 135 | if (fNSAPIMessage) { |
|---|
| 136 | if ((msgType == NSCAPI::debug) && (!logDebug())) |
|---|
| 137 | return; |
|---|
| 138 | return fNSAPIMessage(msgType, file.c_str(), line, message.c_str()); |
|---|
| 139 | } |
|---|
| 140 | else |
|---|
| 141 | std::cout << "NSCore not loaded..." << std::endl << message << std::endl; |
|---|
| 142 | } |
|---|
| 143 | /** |
|---|
| 144 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing) |
|---|
| 145 | * @param command Command to inject (password should not be included. |
|---|
| 146 | * @return The result (if any) of the command. |
|---|
| 147 | * @throws NSCMHExcpetion When core pointer set is unavailable or an unknown inject error occurs. |
|---|
| 148 | */ |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing) |
|---|
| 152 | * @param command Command to inject |
|---|
| 153 | * @param argLen The length of the argument buffer |
|---|
| 154 | * @param **argument The argument buffer |
|---|
| 155 | * @param *returnMessageBuffer Buffer to hold the returned message |
|---|
| 156 | * @param returnMessageBufferLen Length of returnMessageBuffer |
|---|
| 157 | * @param *returnPerfBuffer Buffer to hold the returned performance data |
|---|
| 158 | * @param returnPerfBufferLen returnPerfBuffer |
|---|
| 159 | * @return The returned status of the command |
|---|
| 160 | */ |
|---|
| 161 | NSCAPI::nagiosReturn NSCModuleHelper::InjectCommandRAW(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen) |
|---|
| 162 | { |
|---|
| 163 | if (!fNSAPIInject) |
|---|
| 164 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 165 | return fNSAPIInject(command, argLen, argument, returnMessageBuffer, returnMessageBufferLen, returnPerfBuffer, returnPerfBufferLen); |
|---|
| 166 | } |
|---|
| 167 | /** |
|---|
| 168 | * Inject a request command in the core (this will then be sent to the plug-in stack for processing) |
|---|
| 169 | * @param command Command to inject (password should not be included. |
|---|
| 170 | * @param argLen The length of the argument buffer |
|---|
| 171 | * @param **argument The argument buffer |
|---|
| 172 | * @param message The return message buffer |
|---|
| 173 | * @param perf The return performance data buffer |
|---|
| 174 | * @return The return of the command |
|---|
| 175 | */ |
|---|
| 176 | NSCAPI::nagiosReturn NSCModuleHelper::InjectCommand(const char* command, const unsigned int argLen, char **argument, std::string & message, std::string & perf) |
|---|
| 177 | { |
|---|
| 178 | if (!fNSAPIInject) |
|---|
| 179 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 180 | char *msgBuffer = new char[BUFF_LEN+1]; |
|---|
| 181 | char *perfBuffer = new char[BUFF_LEN+1]; |
|---|
| 182 | msgBuffer[0] = 0; |
|---|
| 183 | perfBuffer[0] = 0; |
|---|
| 184 | // @todo message here ! |
|---|
| 185 | NSCAPI::nagiosReturn retC = InjectCommandRAW(command, argLen, argument, msgBuffer, BUFF_LEN, perfBuffer, BUFF_LEN); |
|---|
| 186 | switch (retC) { |
|---|
| 187 | case NSCAPI::returnIgnored: |
|---|
| 188 | NSC_LOG_MESSAGE_STD("No handler for command '" + command + "'."); |
|---|
| 189 | break; |
|---|
| 190 | case NSCAPI::returnInvalidBufferLen: |
|---|
| 191 | NSC_LOG_ERROR("Inject command resulted in an invalid buffer size."); |
|---|
| 192 | break; |
|---|
| 193 | case NSCAPI::returnOK: |
|---|
| 194 | case NSCAPI::returnCRIT: |
|---|
| 195 | case NSCAPI::returnWARN: |
|---|
| 196 | case NSCAPI::returnUNKNOWN: |
|---|
| 197 | message = msgBuffer; |
|---|
| 198 | perf = perfBuffer; |
|---|
| 199 | break; |
|---|
| 200 | default: |
|---|
| 201 | throw NSCMHExcpetion("Unknown inject error."); |
|---|
| 202 | } |
|---|
| 203 | delete [] msgBuffer; |
|---|
| 204 | delete [] perfBuffer; |
|---|
| 205 | return retC; |
|---|
| 206 | } |
|---|
| 207 | /** |
|---|
| 208 | * A wrapper around the InjetCommand that is simpler to use. |
|---|
| 209 | * Parses a string by splitting and makes the array and also manages return buffers and such. |
|---|
| 210 | * @param command The command to execute |
|---|
| 211 | * @param buffer The buffer to split |
|---|
| 212 | * @param splitChar The char to use as splitter |
|---|
| 213 | * @param message The return message buffer |
|---|
| 214 | * @param perf The return performance data buffer |
|---|
| 215 | * @return The result of the command |
|---|
| 216 | */ |
|---|
| 217 | NSCAPI::nagiosReturn NSCModuleHelper::InjectSplitAndCommand(const char* command, char* buffer, char splitChar, std::string & message, std::string & perf) |
|---|
| 218 | { |
|---|
| 219 | if (!fNSAPIInject) |
|---|
| 220 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 221 | unsigned int argLen = 0; |
|---|
| 222 | char ** aBuffer; |
|---|
| 223 | if (buffer) |
|---|
| 224 | aBuffer= arrayBuffer::split2arrayBuffer(buffer, splitChar, argLen); |
|---|
| 225 | else |
|---|
| 226 | aBuffer= arrayBuffer::createEmptyArrayBuffer(argLen); |
|---|
| 227 | NSCAPI::nagiosReturn ret = InjectCommand(command, argLen, aBuffer, message, perf); |
|---|
| 228 | arrayBuffer::destroyArrayBuffer(aBuffer, argLen); |
|---|
| 229 | return ret; |
|---|
| 230 | } |
|---|
| 231 | /** |
|---|
| 232 | * A wrapper around the InjetCommand that is simpler to use. |
|---|
| 233 | * @param command The command to execute |
|---|
| 234 | * @param buffer The buffer to split |
|---|
| 235 | * @param splitChar The char to use as splitter |
|---|
| 236 | * @param message The return message buffer |
|---|
| 237 | * @param perf The return performance data buffer |
|---|
| 238 | * @return The result of the command |
|---|
| 239 | */ |
|---|
| 240 | NSCAPI::nagiosReturn NSCModuleHelper::InjectSplitAndCommand(const std::string command, const std::string buffer, char splitChar, std::string & message, std::string & perf) |
|---|
| 241 | { |
|---|
| 242 | if (!fNSAPIInject) |
|---|
| 243 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 244 | unsigned int argLen = 0; |
|---|
| 245 | char ** aBuffer; |
|---|
| 246 | if (buffer.empty()) |
|---|
| 247 | aBuffer= arrayBuffer::createEmptyArrayBuffer(argLen); |
|---|
| 248 | else |
|---|
| 249 | aBuffer= arrayBuffer::split2arrayBuffer(buffer, splitChar, argLen); |
|---|
| 250 | NSCAPI::nagiosReturn ret = InjectCommand(command.c_str(), argLen, aBuffer, message, perf); |
|---|
| 251 | arrayBuffer::destroyArrayBuffer(aBuffer, argLen); |
|---|
| 252 | return ret; |
|---|
| 253 | } |
|---|
| 254 | /** |
|---|
| 255 | * Ask the core to shutdown (only works when run as a service, o/w does nothing ? |
|---|
| 256 | * @todo Check if this might cause damage if not run as a service. |
|---|
| 257 | */ |
|---|
| 258 | void NSCModuleHelper::StopService(void) { |
|---|
| 259 | if (fNSAPIStopServer) |
|---|
| 260 | fNSAPIStopServer(); |
|---|
| 261 | } |
|---|
| 262 | /** |
|---|
| 263 | * Retrieve a string from the settings subsystem (INI-file) |
|---|
| 264 | * Might possibly be located in the registry in the future. |
|---|
| 265 | * |
|---|
| 266 | * @param section Section key (generally module specific, make sure this is "unique") |
|---|
| 267 | * @param key The key to retrieve |
|---|
| 268 | * @param defaultValue A default value (if no value is set in the settings file) |
|---|
| 269 | * @return the current value or defaultValue if no value is set. |
|---|
| 270 | * @throws NSCMHExcpetion When core pointer set is unavailable or an error occurs. |
|---|
| 271 | */ |
|---|
| 272 | std::string NSCModuleHelper::getSettingsString(std::string section, std::string key, std::string defaultValue) { |
|---|
| 273 | if (!fNSAPIGetSettingsString) |
|---|
| 274 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 275 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 276 | if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, BUFF_LEN) != NSCAPI::isSuccess) { |
|---|
| 277 | delete [] buffer; |
|---|
| 278 | throw NSCMHExcpetion("Settings could not be retrieved."); |
|---|
| 279 | } |
|---|
| 280 | std::string ret = buffer; |
|---|
| 281 | delete [] buffer; |
|---|
| 282 | return ret; |
|---|
| 283 | } |
|---|
| 284 | /** |
|---|
| 285 | * Get a section of settings strings |
|---|
| 286 | * @param section The section to retrieve |
|---|
| 287 | * @return The keys in the section |
|---|
| 288 | */ |
|---|
| 289 | std::list<std::string> NSCModuleHelper::getSettingsSection(std::string section) { |
|---|
| 290 | if (!fNSAPIGetSettingsSection) |
|---|
| 291 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 292 | arrayBuffer::arrayBuffer aBuffer = NULL; |
|---|
| 293 | unsigned int argLen = 0; |
|---|
| 294 | if (fNSAPIGetSettingsSection(section.c_str(), &aBuffer, &argLen) != NSCAPI::isSuccess) { |
|---|
| 295 | throw NSCMHExcpetion("Settings could not be retrieved."); |
|---|
| 296 | } |
|---|
| 297 | std::list<std::string> ret = arrayBuffer::arrayBuffer2list(argLen, aBuffer); |
|---|
| 298 | if (fNSAPIReleaseSettingsSectionBuffer(&aBuffer, &argLen) != NSCAPI::isSuccess) { |
|---|
| 299 | throw NSCMHExcpetion("Settings could not be destroyed."); |
|---|
| 300 | } |
|---|
| 301 | assert(aBuffer == NULL); |
|---|
| 302 | return ret; |
|---|
| 303 | } |
|---|
| 304 | /** |
|---|
| 305 | * Retrieve an int from the settings subsystem (INI-file) |
|---|
| 306 | * Might possibly be located in the registry in the future. |
|---|
| 307 | * |
|---|
| 308 | * @param section Section key (generally module specific, make sure this is "unique") |
|---|
| 309 | * @param key The key to retrieve |
|---|
| 310 | * @param defaultValue A default value (if no value is set in the settings file) |
|---|
| 311 | * @return the current value or defaultValue if no value is set. |
|---|
| 312 | * @throws NSCMHExcpetion When core pointer set is unavailable. |
|---|
| 313 | */ |
|---|
| 314 | int NSCModuleHelper::getSettingsInt(std::string section, std::string key, int defaultValue) { |
|---|
| 315 | if (!fNSAPIGetSettingsInt) |
|---|
| 316 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 317 | return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue); |
|---|
| 318 | } |
|---|
| 319 | /** |
|---|
| 320 | * Returns the biggest of the two states |
|---|
| 321 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL |
|---|
| 322 | * @param a |
|---|
| 323 | * @param b |
|---|
| 324 | * @return |
|---|
| 325 | */ |
|---|
| 326 | NSCAPI::nagiosReturn NSCHelper::maxState(NSCAPI::nagiosReturn a, NSCAPI::nagiosReturn b) |
|---|
| 327 | { |
|---|
| 328 | if (a == NSCAPI::returnCRIT || b == NSCAPI::returnCRIT) |
|---|
| 329 | return NSCAPI::returnCRIT; |
|---|
| 330 | else if (a == NSCAPI::returnWARN || b == NSCAPI::returnWARN) |
|---|
| 331 | return NSCAPI::returnWARN; |
|---|
| 332 | else if (a == NSCAPI::returnOK || b == NSCAPI::returnOK) |
|---|
| 333 | return NSCAPI::returnOK; |
|---|
| 334 | else if (a == NSCAPI::returnUNKNOWN || b == NSCAPI::returnUNKNOWN) |
|---|
| 335 | return NSCAPI::returnUNKNOWN; |
|---|
| 336 | return NSCAPI::returnUNKNOWN; |
|---|
| 337 | } |
|---|
| 338 | /** |
|---|
| 339 | * Retrieve the application name (in human readable format) from the core. |
|---|
| 340 | * @return A string representing the application name. |
|---|
| 341 | * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs. |
|---|
| 342 | */ |
|---|
| 343 | std::string NSCModuleHelper::getApplicationName() { |
|---|
| 344 | if (!fNSAPIGetApplicationName) |
|---|
| 345 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 346 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 347 | if (fNSAPIGetApplicationName(buffer, BUFF_LEN) != NSCAPI::isSuccess) { |
|---|
| 348 | delete [] buffer; |
|---|
| 349 | throw NSCMHExcpetion("Application name could not be retrieved"); |
|---|
| 350 | } |
|---|
| 351 | std::string ret = buffer; |
|---|
| 352 | delete [] buffer; |
|---|
| 353 | return ret; |
|---|
| 354 | } |
|---|
| 355 | /** |
|---|
| 356 | * Retrieve the directory root of the application from the core. |
|---|
| 357 | * @return A string representing the base path. |
|---|
| 358 | * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs. |
|---|
| 359 | */ |
|---|
| 360 | std::string NSCModuleHelper::getBasePath() { |
|---|
| 361 | if (!fNSAPIGetBasePath) |
|---|
| 362 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 363 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 364 | if (fNSAPIGetBasePath(buffer, BUFF_LEN) != NSCAPI::isSuccess) { |
|---|
| 365 | delete [] buffer; |
|---|
| 366 | throw NSCMHExcpetion("Base path could not be retrieved"); |
|---|
| 367 | } |
|---|
| 368 | std::string ret = buffer; |
|---|
| 369 | delete [] buffer; |
|---|
| 370 | return ret; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | bool NSCModuleHelper::logDebug() { |
|---|
| 375 | typedef enum status {unknown, debug, nodebug }; |
|---|
| 376 | static status d = unknown; |
|---|
| 377 | if (d == unknown) { |
|---|
| 378 | if (checkLogMessages(debug)== NSCAPI::istrue) |
|---|
| 379 | d = debug; |
|---|
| 380 | else |
|---|
| 381 | d = nodebug; |
|---|
| 382 | } |
|---|
| 383 | return (d == debug); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | std::string NSCModuleHelper::Encrypt(std::string str, unsigned int algorithm) { |
|---|
| 387 | if (!fNSAPIEncrypt) |
|---|
| 388 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 389 | unsigned int len = 0; |
|---|
| 390 | // @todo investigate potential problems with static_cast<unsigned int> |
|---|
| 391 | fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len); |
|---|
| 392 | len+=2; |
|---|
| 393 | char *buf = new char[len+1]; |
|---|
| 394 | NSCAPI::errorReturn ret = fNSAPIEncrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len); |
|---|
| 395 | if (ret == NSCAPI::isSuccess) { |
|---|
| 396 | std::string ret = buf; |
|---|
| 397 | delete [] buf; |
|---|
| 398 | return ret; |
|---|
| 399 | } |
|---|
| 400 | return ""; |
|---|
| 401 | } |
|---|
| 402 | std::string NSCModuleHelper::Decrypt(std::string str, unsigned int algorithm) { |
|---|
| 403 | if (!fNSAPIDecrypt) |
|---|
| 404 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 405 | unsigned int len = 0; |
|---|
| 406 | // @todo investigate potential problems with: static_cast<unsigned int>(str.size()) |
|---|
| 407 | fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), NULL, &len); |
|---|
| 408 | len+=2; |
|---|
| 409 | char *buf = new char[len+1]; |
|---|
| 410 | NSCAPI::errorReturn ret = fNSAPIDecrypt(algorithm, str.c_str(), static_cast<unsigned int>(str.size()), buf, &len); |
|---|
| 411 | if (ret == NSCAPI::isSuccess) { |
|---|
| 412 | std::string ret = buf; |
|---|
| 413 | delete [] buf; |
|---|
| 414 | return ret; |
|---|
| 415 | } |
|---|
| 416 | return ""; |
|---|
| 417 | } |
|---|
| 418 | NSCAPI::errorReturn NSCModuleHelper::SetSettingsString(std::string section, std::string key, std::string value) { |
|---|
| 419 | if (!fNSAPISetSettingsString) |
|---|
| 420 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 421 | return fNSAPISetSettingsString(section.c_str(), key.c_str(), value.c_str()); |
|---|
| 422 | } |
|---|
| 423 | NSCAPI::errorReturn NSCModuleHelper::SetSettingsInt(std::string section, std::string key, int value) { |
|---|
| 424 | if (!fNSAPISetSettingsInt) |
|---|
| 425 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 426 | return fNSAPISetSettingsInt(section.c_str(), key.c_str(), value); |
|---|
| 427 | } |
|---|
| 428 | NSCAPI::errorReturn NSCModuleHelper::WriteSettings(int type) { |
|---|
| 429 | if (!fNSAPIWriteSettings) |
|---|
| 430 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 431 | return fNSAPIWriteSettings(type); |
|---|
| 432 | } |
|---|
| 433 | NSCAPI::errorReturn NSCModuleHelper::ReadSettings(int type) { |
|---|
| 434 | if (!fNSAPIReadSettings) |
|---|
| 435 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 436 | return fNSAPIReadSettings(type); |
|---|
| 437 | } |
|---|
| 438 | NSCAPI::errorReturn NSCModuleHelper::Rehash(int flag) { |
|---|
| 439 | if (!fNSAPIRehash) |
|---|
| 440 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 441 | return fNSAPIRehash(flag); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | bool NSCModuleHelper::checkLogMessages(int type) { |
|---|
| 446 | if (!fNSAPICheckLogMessages) |
|---|
| 447 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 448 | return fNSAPICheckLogMessages(type) == NSCAPI::istrue; |
|---|
| 449 | } |
|---|
| 450 | /** |
|---|
| 451 | * Retrieve the application version as a string (in human readable format) from the core. |
|---|
| 452 | * @return A string representing the application version. |
|---|
| 453 | * @throws NSCMHExcpetion When core pointer set is unavailable. |
|---|
| 454 | */ |
|---|
| 455 | std::string NSCModuleHelper::getApplicationVersionString() { |
|---|
| 456 | if (!fNSAPIGetApplicationVersionStr) |
|---|
| 457 | throw NSCMHExcpetion("NSCore has not been initiated..."); |
|---|
| 458 | char *buffer = new char[BUFF_LEN+1]; |
|---|
| 459 | int x = fNSAPIGetApplicationVersionStr(buffer, BUFF_LEN); |
|---|
| 460 | std::string ret = buffer; |
|---|
| 461 | delete [] buffer; |
|---|
| 462 | return ret; |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | namespace NSCModuleWrapper { |
|---|
| 466 | HINSTANCE hModule_ = NULL; |
|---|
| 467 | } |
|---|
| 468 | /** |
|---|
| 469 | * Used to help store the module handle (and possibly other things in the future) |
|---|
| 470 | * @param hModule cf. DllMain |
|---|
| 471 | * @param ul_reason_for_call cf. DllMain |
|---|
| 472 | * @return TRUE |
|---|
| 473 | */ |
|---|
| 474 | BOOL NSCModuleWrapper::wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call) |
|---|
| 475 | { |
|---|
| 476 | switch (ul_reason_for_call) |
|---|
| 477 | { |
|---|
| 478 | case DLL_PROCESS_ATTACH: |
|---|
| 479 | case DLL_THREAD_ATTACH: |
|---|
| 480 | hModule_ = (HINSTANCE)hModule; |
|---|
| 481 | break; |
|---|
| 482 | case DLL_THREAD_DETACH: |
|---|
| 483 | case DLL_PROCESS_DETACH: |
|---|
| 484 | break; |
|---|
| 485 | } |
|---|
| 486 | return TRUE; |
|---|
| 487 | } |
|---|
| 488 | /** |
|---|
| 489 | * Retrieve the module handle from the DllMain call. |
|---|
| 490 | * @return Module handle of this DLL |
|---|
| 491 | */ |
|---|
| 492 | HINSTANCE NSCModuleWrapper::getModule() { |
|---|
| 493 | return hModule_; |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | /** |
|---|
| 497 | * Wrapper function around the ModuleHelperInit call. |
|---|
| 498 | * This wrapper retrieves all pointers and stores them for future use. |
|---|
| 499 | * @param f A function pointer to a function that can be used to load function from the core. |
|---|
| 500 | * @return NSCAPI::success or NSCAPI::failure |
|---|
| 501 | */ |
|---|
| 502 | int NSCModuleWrapper::wrapModuleHelperInit(NSCModuleHelper::lpNSAPILoader f) { |
|---|
| 503 | NSCModuleHelper::fNSAPIGetApplicationName = (NSCModuleHelper::lpNSAPIGetApplicationName)f("NSAPIGetApplicationName"); |
|---|
| 504 | NSCModuleHelper::fNSAPIGetApplicationVersionStr = (NSCModuleHelper::lpNSAPIGetApplicationVersionStr)f("NSAPIGetApplicationVersionStr"); |
|---|
| 505 | NSCModuleHelper::fNSAPIGetSettingsInt = (NSCModuleHelper::lpNSAPIGetSettingsInt)f("NSAPIGetSettingsInt"); |
|---|
| 506 | NSCModuleHelper::fNSAPIGetSettingsString = (NSCModuleHelper::lpNSAPIGetSettingsString)f("NSAPIGetSettingsString"); |
|---|
| 507 | NSCModuleHelper::fNSAPIGetSettingsSection = (NSCModuleHelper::lpNSAPIGetSettingsSection)f("NSAPIGetSettingsSection"); |
|---|
| 508 | NSCModuleHelper::fNSAPIReleaseSettingsSectionBuffer = (NSCModuleHelper::lpNSAPIReleaseSettingsSectionBuffer)f("NSAPIReleaseSettingsSectionBuffer"); |
|---|
| 509 | NSCModuleHelper::fNSAPIMessage = (NSCModuleHelper::lpNSAPIMessage)f("NSAPIMessage"); |
|---|
| 510 | NSCModuleHelper::fNSAPIStopServer = (NSCModuleHelper::lpNSAPIStopServer)f("NSAPIStopServer"); |
|---|
| 511 | NSCModuleHelper::fNSAPIInject = (NSCModuleHelper::lpNSAPIInject)f("NSAPIInject"); |
|---|
| 512 | NSCModuleHelper::fNSAPIGetBasePath = (NSCModuleHelper::lpNSAPIGetBasePath)f("NSAPIGetBasePath"); |
|---|
| 513 | NSCModuleHelper::fNSAPICheckLogMessages = (NSCModuleHelper::lpNSAPICheckLogMessages)f("NSAPICheckLogMessages"); |
|---|
| 514 | NSCModuleHelper::fNSAPIDecrypt = (NSCModuleHelper::lpNSAPIDecrypt)f("NSAPIDecrypt"); |
|---|
| 515 | NSCModuleHelper::fNSAPIEncrypt = (NSCModuleHelper::lpNSAPIEncrypt)f("NSAPIEncrypt"); |
|---|
| 516 | NSCModuleHelper::fNSAPISetSettingsString = (NSCModuleHelper::lpNSAPISetSettingsString)f("NSAPISetSettingsString"); |
|---|
| 517 | NSCModuleHelper::fNSAPISetSettingsInt = (NSCModuleHelper::lpNSAPISetSettingsInt)f("NSAPISetSettingsInt"); |
|---|
| 518 | NSCModuleHelper::fNSAPIWriteSettings = (NSCModuleHelper::lpNSAPIWriteSettings)f("NSAPIWriteSettings"); |
|---|
| 519 | NSCModuleHelper::fNSAPIReadSettings = (NSCModuleHelper::lpNSAPIReadSettings)f("NSAPIReadSettings"); |
|---|
| 520 | NSCModuleHelper::fNSAPIRehash = (NSCModuleHelper::lpNSAPIRehash)f("NSAPIRehash"); |
|---|
| 521 | return NSCAPI::isSuccess; |
|---|
| 522 | } |
|---|
| 523 | /** |
|---|
| 524 | * Wrap the GetModuleName function call |
|---|
| 525 | * @param buf Buffer to store the module name |
|---|
| 526 | * @param bufLen Length of buffer |
|---|
| 527 | * @param str String to store inside the buffer |
|---|
| 528 | * @ copy status |
|---|
| 529 | */ |
|---|
| 530 | NSCAPI::errorReturn NSCModuleWrapper::wrapGetModuleName(char* buf, unsigned int bufLen, std::string str) { |
|---|
| 531 | return NSCHelper::wrapReturnString(buf, bufLen, str, NSCAPI::isSuccess); |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | NSCAPI::errorReturn NSCModuleWrapper::wrapGetConfigurationMeta(char* buf, unsigned int bufLen, std::string str) { |
|---|
| 535 | return NSCHelper::wrapReturnString(buf, bufLen, str, NSCAPI::isSuccess); |
|---|
| 536 | } |
|---|
| 537 | /** |
|---|
| 538 | * Wrap the GetModuleVersion function call |
|---|
| 539 | * @param *major Major version number |
|---|
| 540 | * @param *minor Minor version number |
|---|
| 541 | * @param *revision Revision |
|---|
| 542 | * @param version version as a module_version |
|---|
| 543 | * @return NSCAPI::success |
|---|
| 544 | */ |
|---|
| 545 | NSCAPI::errorReturn NSCModuleWrapper::wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version) { |
|---|
| 546 | *major = version.major; |
|---|
| 547 | *minor = version.minor; |
|---|
| 548 | *revision = version.revision; |
|---|
| 549 | return NSCAPI::isSuccess; |
|---|
| 550 | } |
|---|
| 551 | /** |
|---|
| 552 | * Wrap the HasCommandHandler function call |
|---|
| 553 | * @param has true if this module has a command handler |
|---|
| 554 | * @return NSCAPI::istrue or NSCAPI::isfalse |
|---|
| 555 | */ |
|---|
| 556 | NSCAPI::boolReturn NSCModuleWrapper::wrapHasCommandHandler(bool has) { |
|---|
| 557 | if (has) |
|---|
| 558 | return NSCAPI::istrue; |
|---|
| 559 | return NSCAPI::isfalse; |
|---|
| 560 | } |
|---|
| 561 | /** |
|---|
| 562 | * Wrap the HasMessageHandler function call |
|---|
| 563 | * @param has true if this module has a message handler |
|---|
| 564 | * @return NSCAPI::istrue or NSCAPI::isfalse |
|---|
| 565 | */ |
|---|
| 566 | NSCAPI::boolReturn NSCModuleWrapper::wrapHasMessageHandler(bool has) { |
|---|
| 567 | if (has) |
|---|
| 568 | return NSCAPI::istrue; |
|---|
| 569 | return NSCAPI::isfalse; |
|---|
| 570 | } |
|---|
| 571 | /** |
|---|
| 572 | * Wrap the HandleCommand call |
|---|
| 573 | * @param retResult The returned result |
|---|
| 574 | * @param retMessage The returned message |
|---|
| 575 | * @param retPerformance The returned performance data |
|---|
| 576 | * @param *returnBufferMessage The return message buffer |
|---|
| 577 | * @param returnBufferMessageLen The return message buffer length |
|---|
| 578 | * @param *returnBufferPerf The return perfomance data buffer |
|---|
| 579 | * @param returnBufferPerfLen The return perfomance data buffer length |
|---|
| 580 | * @return the return code |
|---|
| 581 | */ |
|---|
| 582 | NSCAPI::nagiosReturn NSCModuleWrapper::wrapHandleCommand(NSCAPI::nagiosReturn retResult, const std::string retMessage, const std::string retPerformance, char *returnBufferMessage, unsigned int returnBufferMessageLen, char *returnBufferPerf, unsigned int returnBufferPerfLen) { |
|---|
| 583 | if (retMessage.empty()) |
|---|
| 584 | return NSCAPI::returnIgnored; |
|---|
| 585 | NSCAPI::nagiosReturn ret = NSCHelper::wrapReturnString(returnBufferMessage, returnBufferMessageLen, retMessage, retResult); |
|---|
| 586 | return NSCHelper::wrapReturnString(returnBufferPerf, returnBufferPerfLen, retPerformance, ret); |
|---|
| 587 | } |
|---|
| 588 | /** |
|---|
| 589 | * Wrap the NSLoadModule call |
|---|
| 590 | * @param success true if module load was successfully |
|---|
| 591 | * @return NSCAPI::success or NSCAPI::failed |
|---|
| 592 | */ |
|---|
| 593 | int NSCModuleWrapper::wrapLoadModule(bool success) { |
|---|
| 594 | if (success) |
|---|
| 595 | return NSCAPI::isSuccess; |
|---|
| 596 | return NSCAPI::hasFailed; |
|---|
| 597 | } |
|---|
| 598 | /** |
|---|
| 599 | * Wrap the NSUnloadModule call |
|---|
| 600 | * @param success true if module load was successfully |
|---|
| 601 | * @return NSCAPI::success or NSCAPI::failed |
|---|
| 602 | */ |
|---|
| 603 | int NSCModuleWrapper::wrapUnloadModule(bool success) { |
|---|
| 604 | if (success) |
|---|
| 605 | return NSCAPI::isSuccess; |
|---|
| 606 | return NSCAPI::hasFailed; |
|---|
| 607 | } |
|---|
| 608 | |
|---|
| 609 | |
|---|
| 610 | |
|---|