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