source: nscp/include/NSCHelper.cpp @ 2a94f3f

0.4.00.4.10.4.2stable
Last change on this file since 2a94f3f was 2a94f3f, checked in by Michael Medin <michael@…>, 8 years ago
  • Refactored out NSCLient Listener as a separate module
  • Added initial NRPE listener module (not yet implemented only a shell)
  • Changed Module API (Inject function has new syntax)
  • Added some tokenizer function to charEx
  • Added new wrapper function to inject Command
  • Minor changes in relation to refactor work
  • Property mode set to 100644
File size: 15.7 KB
Line 
1#include "stdafx.h"
2#include <NSCHelper.h>
3#include <assert.h>
4
5#define BUFF_LEN 4096
6
7
8/**
9 * Wrap a return string.
10 * This function copies a string to a char buffer making sure the buffer has the correct length.
11 *
12 * @param *buffer Buffer to copy the string to.
13 * @param bufLen Length of the buffer
14 * @param str Th string to copy
15 * @return NSCAPI::success unless the buffer is to short then it will be NSCAPI::invalidBufferLen
16 */
17int NSCHelper::wrapReturnString(char *buffer, unsigned int bufLen, std::string str, int defaultReturnCode /* = NSCAPI::success */) {
18        if (str.length() >= bufLen)
19                return NSCAPI::invalidBufferLen;
20        strncpy(buffer, str.c_str(), bufLen);
21        return defaultReturnCode;
22}
23/**
24 * Make a list out of a array of char arrays (arguments type)
25 * @param argLen Length of argument array
26 * @param *argument[] Argument array
27 * @return Argument wrapped as a list
28 */
29std::list<std::string> NSCHelper::arrayBuffer2list(const unsigned int argLen, char *argument[]) {
30        std::list<std::string> ret;
31        int i=0;
32        for (unsigned int i=0;i<argLen;i++) {
33                std::string s = argument[i];
34                ret.push_back(s);
35        }
36        return ret;
37}
38/**
39 * Create an arrayBuffer from a list.
40 * This is the reverse of arrayBuffer2list.
41 * <b>Notice</b> it is up to the caller to free the memory allocated in the returned buffer.
42 *
43 * @param lst A list to convert.
44 * @param &argLen Write the length to this argument.
45 * @return A pointer that is managed by the caller.
46 */
47char ** NSCHelper::list2arrayBuffer(const std::list<std::string> lst, unsigned int &argLen) {
48        argLen = static_cast<unsigned int>(lst.size());
49        char **arrayBuffer = new char*[argLen];
50        std::list<std::string>::const_iterator it = lst.begin();
51        for (int i=0;it!=lst.end();++it,i++) {
52                std::string::size_type alen = (*it).size();
53                arrayBuffer[i] = new char[alen+2];
54                strncpy(arrayBuffer[i], (*it).c_str(), alen+1);
55        }
56        assert(i == argLen);
57        return arrayBuffer;
58}
59/**
60 * Creates an empty arrayBuffer (only used to allow consistency)
61 * @param &argLen [OUT] The length (items) of the arrayBuffer
62 * @return The arrayBuffer
63 */
64char ** NSCHelper::createEmptyArrayBuffer(unsigned int &argLen) {
65        argLen = 0;
66        char **arrayBuffer = new char*[0];
67        return arrayBuffer;
68}
69/**
70 * Joins an arrayBuffer back into a string
71 * @param **argument The ArrayBuffer
72 * @param argLen The length of the ArrayBuffer
73 * @param join The char to use as separators when joining
74 * @return The joined arrayBuffer
75 */
76std::string NSCHelper::arrayBuffer2string(char **argument, const unsigned int argLen, std::string join) {
77        std::string ret;
78        for (unsigned int i=0;i<argLen;i++) {
79                ret += argument[i];
80                if (i != argLen-1)
81                        ret += join;
82        }
83        return ret;
84}
85/**
86 * Split a string into elements as an arrayBuffer
87 * @param buffer The CharArray to split along
88 * @param splitChar The char to use as splitter
89 * @param &argLen [OUT] The length of the Array
90 * @return The arrayBuffer
91 */
92char ** NSCHelper::split2arrayBuffer(const char* buffer, char splitChar, unsigned int &argLen) {
93        assert(buffer);
94        argLen = 0;
95        const char *p = buffer;
96        while (*p) {
97                if (*p == splitChar)
98                        argLen++;
99                p++;
100        }
101        argLen++;
102        char **arrayBuffer = new char*[argLen];
103        p = buffer;
104        for (unsigned int i=0;i<argLen;i++) {
105                char *q = strchr(p, (i<argLen-1)?splitChar:0);
106                unsigned int len = q-p;
107                arrayBuffer[i] = new char[len+1];
108                strncpy(arrayBuffer[i], p, len);
109                arrayBuffer[i][len] = 0;
110                p = ++q;
111        }
112        return arrayBuffer;
113}
114
115/**
116 * Destroy an arrayBuffer.
117 * The buffer should have been created with list2arrayBuffer.
118 *
119 * @param **argument
120 * @param argLen
121 */
122void NSCHelper::destroyArrayBuffer(char **argument, const unsigned int argLen) {
123        for (unsigned int i=0;i<argLen;i++) {
124                delete [] argument[i];
125        }
126        delete [] argument;
127}
128
129
130/**
131 * Translate a message type into a human readable string.
132 *
133 * @param msgType The message type
134 * @return A string representing the message type
135 */
136std::string NSCHelper::translateMessageType(NSCAPI::messageTypes msgType) {
137        switch (msgType) {
138                case NSCAPI::error:
139                        return "error";
140                case NSCAPI::critical:
141                        return "critical";
142                case NSCAPI::warning:
143                        return "warning";
144                case NSCAPI::log:
145                        return "message";
146                case NSCAPI::debug:
147                        return "debug";
148        }
149        return "unknown";
150}
151std::string NSCHelper::translateReturn(NSCAPI::returnCodes returnCode) {
152        if (returnCode == NSCAPI::returnOK)
153                return "OK";
154        else if (returnCode == NSCAPI::returnCRIT)
155                return "CRITICAL";
156        else if (returnCode == NSCAPI::returnWARN)
157                return "WARNING";
158        else
159                return "UNKNOWN";
160}
161
162
163
164namespace NSCModuleHelper {
165        lpNSAPIGetBasePath fNSAPIGetBasePath = NULL;
166        lpNSAPIGetApplicationName fNSAPIGetApplicationName = NULL;
167        lpNSAPIGetApplicationVersionStr fNSAPIGetApplicationVersionStr = NULL;
168        lpNSAPIGetSettingsString fNSAPIGetSettingsString = NULL;
169        lpNSAPIGetSettingsInt fNSAPIGetSettingsInt = NULL;
170        lpNSAPIMessage fNSAPIMessage = NULL;
171        lpNSAPIStopServer fNSAPIStopServer = NULL;
172        lpNSAPIInject fNSAPIInject = NULL;
173}
174
175//////////////////////////////////////////////////////////////////////////
176// Callbacks into the core
177//////////////////////////////////////////////////////////////////////////
178
179/**
180 * Callback to send a message through to the core
181 *
182 * @param msgType Message type (debug, warning, etc.)
183 * @param file File where message was generated (__FILE__)
184 * @param line Line where message was generated (__LINE__)
185 * @param message Message in human readable format
186 * @throws NSCMHExcpetion When core pointer set is unavailable.
187 */
188void NSCModuleHelper::Message(int msgType, std::string file, int line, std::string message) {
189        if (!fNSAPIMessage)
190                throw NSCMHExcpetion("NSCore has not been initiated...");
191        return fNSAPIMessage(msgType, file.c_str(), line, message.c_str());
192}
193/**
194 * Inject a request command in the core (this will then be sent to the plug-in stack for processing)
195 * @param command Command to inject (password should not be included.
196 * @return The result (if any) of the command.
197 * @throws NSCMHExcpetion When core pointer set is unavailable or an unknown inject error occurs.
198 */
199int NSCModuleHelper::InjectCommandRAW(const char* command, const unsigned int argLen, char **argument, char *returnBuffer, unsigned int returnBufferLen)
200{
201        if (!fNSAPIInject)
202                throw NSCMHExcpetion("NSCore has not been initiated...");
203        return fNSAPIInject(command, argLen, argument, returnBuffer, returnBufferLen);
204}
205std::string NSCModuleHelper::InjectCommand(const char* command, const unsigned int argLen, char **argument)
206{
207        if (!fNSAPIInject)
208                throw NSCMHExcpetion("NSCore has not been initiated...");
209        char *buffer = new char[BUFF_LEN+1];
210        buffer[0] = 0;
211        std::string ret;
212        int err;
213        if ((err = InjectCommandRAW(command, argLen, argument, buffer, BUFF_LEN)) != NSCAPI::handled) {
214                if (err == NSCAPI::invalidBufferLen)
215                        NSC_LOG_ERROR("Inject command resulted in an invalid buffer size.");
216                else if (err == NSCAPI::isfalse)
217                        NSC_LOG_MESSAGE("No handler for this message.");
218                else
219                        throw NSCMHExcpetion("Unknown inject error.");
220        } else {
221                ret = buffer;
222        }
223        delete [] buffer;
224        return ret;
225}
226/**
227 * A wrapper around the InjetCommand that is simpler to use.
228 * Parses a string by splitting and makes the array and also manages return buffers and such.
229 * @param command The command to execute
230 * @param buffer The buffer to splitwww.ikea.se
231
232 * @param splitChar The char to use as splitter
233 * @return The result of the command
234 */
235std::string NSCModuleHelper::InjectSplitAndCommand(const char* command, char* buffer, char splitChar)
236{
237        if (!fNSAPIInject)
238                throw NSCMHExcpetion("NSCore has not been initiated...");
239        unsigned int argLen = 0;
240        char ** aBuffer;
241        if (buffer)
242                aBuffer= NSCHelper::split2arrayBuffer(buffer, splitChar, argLen);
243        else
244                aBuffer= NSCHelper::createEmptyArrayBuffer(argLen);
245        std::string s = InjectCommand(command, argLen, aBuffer);
246        NSCHelper::destroyArrayBuffer(aBuffer, argLen);
247        return s;
248}
249/**
250 * Ask the core to shutdown (only works when run as a service, o/w does nothing ?
251 * @todo Check if this might cause damage if not run as a service.
252 */
253void NSCModuleHelper::StopService(void) {
254        if (fNSAPIStopServer)
255                fNSAPIStopServer();
256}
257/**
258 * Retrieve a string from the settings subsystem (INI-file)
259 * Might possibly be located in the registry in the future.
260 *
261 * @param section Section key (generally module specific, make sure this is "unique")
262 * @param key The key to retrieve
263 * @param defaultValue A default value (if no value is set in the settings file)
264 * @return the current value or defaultValue if no value is set.
265 * @throws NSCMHExcpetion When core pointer set is unavailable or an error occurs.
266 */
267std::string NSCModuleHelper::getSettingsString(std::string section, std::string key, std::string defaultValue) {
268        if (!fNSAPIGetSettingsString)
269                throw NSCMHExcpetion("NSCore has not been initiated...");
270        char *buffer = new char[BUFF_LEN+1];
271        if (fNSAPIGetSettingsString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, BUFF_LEN) != NSCAPI::success) {
272                delete [] buffer;
273                throw NSCMHExcpetion("Settings could not be retrieved.");
274        }
275        std::string ret = buffer;
276        delete [] buffer;
277        return ret;
278}
279/**
280 * Retrieve an int from the settings subsystem (INI-file)
281 * Might possibly be located in the registry in the future.
282 *
283 * @param section Section key (generally module specific, make sure this is "unique")
284 * @param key The key to retrieve
285 * @param defaultValue A default value (if no value is set in the settings file)
286 * @return the current value or defaultValue if no value is set.
287 * @throws NSCMHExcpetion When core pointer set is unavailable.
288 */
289int NSCModuleHelper::getSettingsInt(std::string section, std::string key, int defaultValue) {
290        if (!fNSAPIGetSettingsInt)
291                throw NSCMHExcpetion("NSCore has not been initiated...");
292        return fNSAPIGetSettingsInt(section.c_str(), key.c_str(), defaultValue);
293}
294/**
295 * Retrieve the application name (in human readable format) from the core.
296 * @return A string representing the application name.
297 * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs.
298 */
299std::string NSCModuleHelper::getApplicationName() {
300        if (!fNSAPIGetApplicationName)
301                throw NSCMHExcpetion("NSCore has not been initiated...");
302        char *buffer = new char[BUFF_LEN+1];
303        if (fNSAPIGetApplicationName(buffer, BUFF_LEN) != NSCAPI::success) {
304                delete [] buffer;
305                throw NSCMHExcpetion("Application name could not be retrieved");
306        }
307        std::string ret = buffer;
308        delete [] buffer;
309        return ret;
310}
311/**
312 * Retrieve the directory root of the application from the core.
313 * @return A string representing the base path.
314 * @throws NSCMHExcpetion When core pointer set is unavailable or an unexpected error occurs.
315 */
316std::string NSCModuleHelper::getBasePath() {
317        if (!fNSAPIGetBasePath)
318                throw NSCMHExcpetion("NSCore has not been initiated...");
319        char *buffer = new char[BUFF_LEN+1];
320        if (fNSAPIGetBasePath(buffer, BUFF_LEN) != NSCAPI::success) {
321                delete [] buffer;
322                throw NSCMHExcpetion("Base path could not be retrieved");
323        }
324        std::string ret = buffer;
325        delete [] buffer;
326        return ret;
327}
328/**
329 * Retrieve the application version as a string (in human readable format) from the core.
330 * @return A string representing the application version.
331 * @throws NSCMHExcpetion When core pointer set is unavailable.
332 */
333std::string NSCModuleHelper::getApplicationVersionString() {
334        if (!fNSAPIGetApplicationVersionStr)
335                throw NSCMHExcpetion("NSCore has not been initiated...");
336        char *buffer = new char[BUFF_LEN+1];
337        int x = fNSAPIGetApplicationVersionStr(buffer, BUFF_LEN);
338        std::string ret = buffer;
339        delete [] buffer;
340        return ret;
341}
342
343//////////////////////////////////////////////////////////////////////////
344// Module helper functions
345//////////////////////////////////////////////////////////////////////////
346namespace NSCModuleWrapper {
347        HINSTANCE hModule_ = NULL;
348}
349/**
350 * Used to help store the module handle (and possibly other things in the future)
351 * @param hModule cf. DllMain
352 * @param ul_reason_for_call cf. DllMain
353 * @return TRUE
354 */
355BOOL NSCModuleWrapper::wrapDllMain(HANDLE hModule, DWORD ul_reason_for_call)
356{
357        switch (ul_reason_for_call)
358        {
359        case DLL_PROCESS_ATTACH:
360        case DLL_THREAD_ATTACH:
361                hModule_ = (HINSTANCE)hModule;
362                break;
363        case DLL_THREAD_DETACH:
364        case DLL_PROCESS_DETACH:
365                break;
366        }
367        return TRUE;
368}
369/**
370 * Retrieve the module handle from the DllMain call.
371 * @return Module handle of this DLL
372 */
373HINSTANCE NSCModuleWrapper::getModule() {
374        return hModule_;
375}
376
377/**
378 * Wrapper function around the ModuleHelperInit call.
379 * This wrapper retrieves all pointers and stores them for future use.
380 * @param f A function pointer to a function that can be used to load function from the core.
381 * @return NSCAPI::success or NSCAPI::failure
382 */
383int NSCModuleWrapper::wrapModuleHelperInit(NSCModuleHelper::lpNSAPILoader f) {
384        NSCModuleHelper::fNSAPIGetApplicationName = (NSCModuleHelper::lpNSAPIGetApplicationName)f("NSAPIGetApplicationName");
385        NSCModuleHelper::fNSAPIGetApplicationVersionStr = (NSCModuleHelper::lpNSAPIGetApplicationVersionStr)f("NSAPIGetApplicationVersionStr");
386        NSCModuleHelper::fNSAPIGetSettingsInt = (NSCModuleHelper::lpNSAPIGetSettingsInt)f("NSAPIGetSettingsInt");
387        NSCModuleHelper::fNSAPIGetSettingsString = (NSCModuleHelper::lpNSAPIGetSettingsString)f("NSAPIGetSettingsString");
388        NSCModuleHelper::fNSAPIMessage = (NSCModuleHelper::lpNSAPIMessage)f("NSAPIMessage");
389        NSCModuleHelper::fNSAPIStopServer = (NSCModuleHelper::lpNSAPIStopServer)f("NSAPIStopServer");
390        NSCModuleHelper::fNSAPIInject = (NSCModuleHelper::lpNSAPIInject)f("NSAPIInject");
391        NSCModuleHelper::fNSAPIGetBasePath = (NSCModuleHelper::lpNSAPIGetBasePath)f("NSAPIGetBasePath");
392        return NSCAPI::success;
393}
394/**
395* Wrap the GetModuleName function call
396* @param buf Buffer to store the module name
397* @param bufLen Length of buffer
398* @param str String to store inside the buffer
399* @return buffer copy status
400*/
401int NSCModuleWrapper::wrapGetModuleName(char* buf, unsigned int bufLen, std::string str) {
402        return NSCHelper::wrapReturnString(buf, bufLen, str);
403}
404/**
405 * Wrap the GetModuleVersion function call
406 * @param *major Major version number
407 * @param *minor Minor version number
408 * @param *revision Revision
409 * @param version version as a module_version
410 * @return NSCAPI::success
411 */
412int NSCModuleWrapper::wrapGetModuleVersion(int *major, int *minor, int *revision, module_version version) {
413        *major = version.major;
414        *minor = version.minor;
415        *revision = version.revision;
416        return NSCAPI::success;
417}
418/**
419 * Wrap the HasCommandHandler function call
420 * @param has true if this module has a command handler
421 * @return NSCAPI::istrue or NSCAPI::isfalse
422 */
423int NSCModuleWrapper::wrapHasCommandHandler(bool has) {
424        if (has)
425                return NSCAPI::istrue;
426        return NSCAPI::isfalse;
427}
428/**
429 * Wrap the HasMessageHandler function call
430 * @param has true if this module has a message handler
431 * @return NSCAPI::istrue or NSCAPI::isfalse
432 */
433int NSCModuleWrapper::wrapHasMessageHandler(bool has) {
434        if (has)
435                return NSCAPI::istrue;
436        return NSCAPI::isfalse;
437}
438/**
439 * Wrap the HandleCommand call
440 * @param retStr The string to return to the core
441 * @param *returnBuffer A buffer to copy the return string to
442 * @param returnBufferLen length of returnBuffer
443 * @return copy status or NSCAPI::isfalse if retStr is empty
444 */
445int NSCModuleWrapper::wrapHandleCommand(const std::string retStr, char *returnBuffer, unsigned int returnBufferLen) {
446        if (retStr.empty())
447                return NSCAPI::isfalse;
448        return NSCHelper::wrapReturnString(returnBuffer, returnBufferLen, retStr, NSCAPI::handled);
449}
450/**
451 * Wrap the NSLoadModule call
452 * @param success true if module load was successfully
453 * @return NSCAPI::success or NSCAPI::failed
454 */
455int NSCModuleWrapper::wrapLoadModule(bool success) {
456        if (success)
457                return NSCAPI::success;
458        return NSCAPI::failed;
459}
460/**
461 * Wrap the NSUnloadModule call
462 * @param success true if module load was successfully
463 * @return NSCAPI::success or NSCAPI::failed
464 */
465int NSCModuleWrapper::wrapUnloadModule(bool success) {
466        if (success)
467                return NSCAPI::success;
468        return NSCAPI::failed;
469}
470
471
472
Note: See TracBrowser for help on using the repository browser.