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