| 1 | #pragma once |
|---|
| 2 | |
|---|
| 3 | #include "resource.h" |
|---|
| 4 | #include <ServiceCmd.h> |
|---|
| 5 | #include <NTService.h> |
|---|
| 6 | #include "NSCPlugin.h" |
|---|
| 7 | #include <Mutex.h> |
|---|
| 8 | #include <NSCAPI.h> |
|---|
| 9 | #include <MutexRW.h> |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * @ingroup NSClient++ |
|---|
| 14 | * Main NSClient++ core class. This is the service core and as such is responsible for pretty much everything. |
|---|
| 15 | * It also acts as a broker for all plugins and other sub threads and such. |
|---|
| 16 | * |
|---|
| 17 | * @version 1.0 |
|---|
| 18 | * first version |
|---|
| 19 | * |
|---|
| 20 | * @date 02-12-2005 |
|---|
| 21 | * |
|---|
| 22 | * @author mickem |
|---|
| 23 | * |
|---|
| 24 | * @par license |
|---|
| 25 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 26 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 27 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 28 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 29 | * This code has no bugs, just undocumented features! |
|---|
| 30 | * |
|---|
| 31 | * @todo Plugininfy the socket somehow ? |
|---|
| 32 | * It is technically possible to make the socket a plug-in but would it be a good idea ? |
|---|
| 33 | * |
|---|
| 34 | * @bug |
|---|
| 35 | * |
|---|
| 36 | */ |
|---|
| 37 | class NSClientT { |
|---|
| 38 | private: |
|---|
| 39 | typedef NSCPlugin* plugin_type; |
|---|
| 40 | typedef std::vector<plugin_type> pluginList; |
|---|
| 41 | pluginList plugins_; |
|---|
| 42 | pluginList commandHandlers_; |
|---|
| 43 | pluginList messageHandlers_; |
|---|
| 44 | std::string basePath; |
|---|
| 45 | MutexHandler internalVariables; |
|---|
| 46 | MutexHandler messageMutex; |
|---|
| 47 | MutexRW m_mutexRW; |
|---|
| 48 | |
|---|
| 49 | public: |
|---|
| 50 | // c-tor, d-tor |
|---|
| 51 | NSClientT(void) {} |
|---|
| 52 | virtual ~NSClientT(void) {} |
|---|
| 53 | |
|---|
| 54 | // Service helper functions |
|---|
| 55 | bool InitiateService(void); |
|---|
| 56 | void TerminateService(void); |
|---|
| 57 | static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv); |
|---|
| 58 | static void WINAPI service_ctrl_dispatch(DWORD dwCtrlCode); |
|---|
| 59 | |
|---|
| 60 | // Member functions |
|---|
| 61 | std::string getBasePath(void); |
|---|
| 62 | NSCAPI::nagiosReturn injectRAW(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen); |
|---|
| 63 | NSCAPI::nagiosReturn NSClientT::inject(std::string command, std::string arguments, char splitter, std::string &msg, std::string & perf); |
|---|
| 64 | // std::string inject(const std::string buffer); |
|---|
| 65 | std::string execute(std::string password, std::string cmd, std::list<std::string> args); |
|---|
| 66 | void reportMessage(int msgType, const char* file, const int line, std::string message); |
|---|
| 67 | int commandLineExec(const char* module, const char* command, const unsigned int argLen, char** args); |
|---|
| 68 | |
|---|
| 69 | void addPlugins(const std::list<std::string> plugins); |
|---|
| 70 | void loadPlugin(const std::string plugin); |
|---|
| 71 | void loadPlugins(void); |
|---|
| 72 | void unloadPlugins(void); |
|---|
| 73 | |
|---|
| 74 | bool logDebug(); |
|---|
| 75 | |
|---|
| 76 | private: |
|---|
| 77 | void addPlugin(plugin_type plugin); |
|---|
| 78 | |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | typedef NTService<NSClientT> NSClient; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | std::string Encrypt(std::string str, unsigned int algorithm = NSCAPI::xor); |
|---|
| 85 | std::string Decrypt(std::string str, unsigned int algorithm = NSCAPI::xor); |
|---|
| 86 | |
|---|
| 87 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 88 | // Various NSAPI callback functions (available for plug-ins to make calls back to the core. |
|---|
| 89 | // <b>NOTICE</b> No threading is allowed so technically every thread is responsible for marshaling things back. |
|---|
| 90 | // Though I think this is not the case at the moment. |
|---|
| 91 | // |
|---|
| 92 | |
|---|
| 93 | LPVOID NSAPILoader(char*buffer); |
|---|
| 94 | NSCAPI::errorReturn NSAPIGetApplicationName(char*buffer, unsigned int bufLen); |
|---|
| 95 | NSCAPI::errorReturn NSAPIGetBasePath(char*buffer, unsigned int bufLen); |
|---|
| 96 | NSCAPI::errorReturn NSAPIGetApplicationVersionStr(char*buffer, unsigned int bufLen); |
|---|
| 97 | NSCAPI::errorReturn NSAPIGetSettingsString(const char* section, const char* key, const char* defaultValue, char* buffer, unsigned int bufLen); |
|---|
| 98 | int NSAPIGetSettingsInt(const char* section, const char* key, int defaultValue); |
|---|
| 99 | void NSAPIMessage(int msgType, const char* file, const int line, const char* message); |
|---|
| 100 | void NSAPIStopServer(void); |
|---|
| 101 | NSCAPI::nagiosReturn NSAPIInject(const char* command, const unsigned int argLen, char **argument, char *returnMessageBuffer, unsigned int returnMessageBufferLen, char *returnPerfBuffer, unsigned int returnPerfBufferLen); |
|---|
| 102 | NSCAPI::errorReturn NSAPIGetSettingsSection(const char*, char***, unsigned int *); |
|---|
| 103 | NSCAPI::boolReturn NSAPICheckLogMessages(int messageType); |
|---|
| 104 | NSCAPI::errorReturn NSAPIEncrypt(unsigned int algorithm, const char* inBuffer, unsigned int inBufLen, char* outBuf, unsigned int *outBufLen); |
|---|
| 105 | NSCAPI::errorReturn NSAPIDecrypt(unsigned int algorithm, const char* inBuffer, unsigned int inBufLen, char* outBuf, unsigned int *outBufLen); |
|---|
| 106 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 107 | // Log macros to simplify logging |
|---|
| 108 | // Generally names are of the form LOG_<severity>[_STD] |
|---|
| 109 | // Where _STD indicates that strings are force wrapped inside a std::string |
|---|
| 110 | // |
|---|
| 111 | #define LOG_ERROR_STD(msg) LOG_ERROR(((std::string)msg).c_str()) |
|---|
| 112 | #define LOG_ERROR(msg) \ |
|---|
| 113 | NSAPIMessage(NSCAPI::error, __FILE__, __LINE__, msg) |
|---|
| 114 | #define LOG_CRITICAL_STD(msg) LOG_CRITICAL(((std::string)msg).c_str()) |
|---|
| 115 | #define LOG_CRITICAL(msg) \ |
|---|
| 116 | NSAPIMessage(NSCAPI::critical, __FILE__, __LINE__, msg) |
|---|
| 117 | #define LOG_MESSAGE_STD(msg) LOG_MESSAGE(((std::string)msg).c_str()) |
|---|
| 118 | #define LOG_MESSAGE(msg) \ |
|---|
| 119 | NSAPIMessage(NSCAPI::log, __FILE__, __LINE__, msg) |
|---|
| 120 | |
|---|
| 121 | #define LOG_DEBUG_STD(msg) LOG_DEBUG(((std::string)msg).c_str()) |
|---|
| 122 | #define LOG_DEBUG(msg) \ |
|---|
| 123 | NSAPIMessage(NSCAPI::debug, __FILE__, __LINE__, msg) |
|---|
| 124 | /* |
|---|
| 125 | #define LOG_DEBUG_STD(msg) |
|---|
| 126 | #define LOG_DEBUG(msg) |
|---|
| 127 | */ |
|---|