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