| 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 | #include <com_helpers.hpp> |
|---|
| 32 | #include <nsclient_session.hpp> |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * @ingroup NSClient++ |
|---|
| 37 | * Main NSClient++ core class. This is the service core and as such is responsible for pretty much everything. |
|---|
| 38 | * It also acts as a broker for all plugins and other sub threads and such. |
|---|
| 39 | * |
|---|
| 40 | * @version 1.0 |
|---|
| 41 | * first version |
|---|
| 42 | * |
|---|
| 43 | * @date 02-12-2005 |
|---|
| 44 | * |
|---|
| 45 | * @author mickem |
|---|
| 46 | * |
|---|
| 47 | * @par license |
|---|
| 48 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 49 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 50 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 51 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 52 | * This code has no bugs, just undocumented features! |
|---|
| 53 | * |
|---|
| 54 | * @todo Plugininfy the socket somehow ? |
|---|
| 55 | * It is technically possible to make the socket a plug-in but would it be a good idea ? |
|---|
| 56 | * |
|---|
| 57 | * @bug |
|---|
| 58 | * |
|---|
| 59 | */ |
|---|
| 60 | class NSClientT : public nsclient_session::session_handler_interface { |
|---|
| 61 | |
|---|
| 62 | public: |
|---|
| 63 | struct plugin_info_type { |
|---|
| 64 | std::wstring dll; |
|---|
| 65 | std::wstring name; |
|---|
| 66 | std::wstring version; |
|---|
| 67 | std::wstring description; |
|---|
| 68 | }; |
|---|
| 69 | typedef std::list<plugin_info_type> plugin_info_list; |
|---|
| 70 | private: |
|---|
| 71 | |
|---|
| 72 | class NSException { |
|---|
| 73 | std::wstring what_; |
|---|
| 74 | public: |
|---|
| 75 | NSException(std::wstring what) : what_(what){} |
|---|
| 76 | std::wstring what() { |
|---|
| 77 | return what_; |
|---|
| 78 | } |
|---|
| 79 | }; |
|---|
| 80 | struct cached_log_entry { |
|---|
| 81 | cached_log_entry(int msgType_, std::wstring file_, int line_, std::wstring message_) |
|---|
| 82 | : msgType(msgType_), |
|---|
| 83 | file(file_), |
|---|
| 84 | line(line_), |
|---|
| 85 | message(message_) |
|---|
| 86 | {} |
|---|
| 87 | int msgType; |
|---|
| 88 | std::wstring file; |
|---|
| 89 | int line; |
|---|
| 90 | std::wstring message; |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | typedef NSCPlugin* plugin_type; |
|---|
| 94 | typedef std::vector<plugin_type> pluginList; |
|---|
| 95 | typedef std::map<std::wstring,std::wstring> cmdMap; |
|---|
| 96 | typedef std::list<cached_log_entry> log_cache_type; |
|---|
| 97 | pluginList plugins_; |
|---|
| 98 | pluginList commandHandlers_; |
|---|
| 99 | pluginList messageHandlers_; |
|---|
| 100 | std::wstring basePath; |
|---|
| 101 | MutexHandler internalVariables; |
|---|
| 102 | MutexHandler messageMutex; |
|---|
| 103 | MutexRW m_mutexRW; |
|---|
| 104 | MutexRW m_mutexRWcmdDescriptions; |
|---|
| 105 | cmdMap cmdDescriptions_; |
|---|
| 106 | typedef enum log_status {log_unknown, log_looking, log_debug, log_nodebug }; |
|---|
| 107 | log_status debug_; |
|---|
| 108 | com_helper::initialize_com com_helper_; |
|---|
| 109 | std::auto_ptr<nsclient_session::shared_client_session> shared_client_; |
|---|
| 110 | std::auto_ptr<nsclient_session::shared_server_session> shared_server_; |
|---|
| 111 | log_cache_type log_cache_; |
|---|
| 112 | bool plugins_loaded_; |
|---|
| 113 | bool enable_shared_session_; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | public: |
|---|
| 117 | // c-tor, d-tor |
|---|
| 118 | NSClientT(void) : debug_(log_unknown), plugins_loaded_(false), enable_shared_session_(false) {} |
|---|
| 119 | virtual ~NSClientT(void) {} |
|---|
| 120 | void enableDebug(bool debug = true) { |
|---|
| 121 | if (debug) |
|---|
| 122 | debug_ = log_debug; |
|---|
| 123 | else |
|---|
| 124 | debug_ = log_nodebug; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | // Service helper functions |
|---|
| 128 | bool InitiateService(); |
|---|
| 129 | void TerminateService(void); |
|---|
| 130 | bool initCore(bool boot); |
|---|
| 131 | bool exitCore(bool boot); |
|---|
| 132 | static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv); |
|---|
| 133 | static void WINAPI service_ctrl_dispatch(DWORD dwCtrlCode); |
|---|
| 134 | static DWORD WINAPI service_ctrl_dispatch_ex(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext); |
|---|
| 135 | void service_on_session_changed(DWORD dwSessionId, bool logon, DWORD dwEventType); |
|---|
| 136 | |
|---|
| 137 | // Member functions |
|---|
| 138 | std::wstring getBasePath(void); |
|---|
| 139 | NSCAPI::nagiosReturn injectRAW(const TCHAR* command, const unsigned int argLen, TCHAR **argument, TCHAR *returnMessageBuffer, unsigned int returnMessageBufferLen, TCHAR *returnPerfBuffer, unsigned int returnPerfBufferLen); |
|---|
| 140 | NSCAPI::nagiosReturn NSClientT::inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf); |
|---|
| 141 | // std::wstring inject(const std::wstring buffer); |
|---|
| 142 | std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); |
|---|
| 143 | void reportMessage(int msgType, const TCHAR* file, const int line, std::wstring message); |
|---|
| 144 | int commandLineExec(const TCHAR* module, const TCHAR* command, const unsigned int argLen, TCHAR** args); |
|---|
| 145 | |
|---|
| 146 | void addPlugins(const std::list<std::wstring> plugins); |
|---|
| 147 | plugin_type loadPlugin(const std::wstring plugin); |
|---|
| 148 | void loadPlugins(NSCAPI::moduleLoadMode mode); |
|---|
| 149 | void unloadPlugins(bool unloadLoggers); |
|---|
| 150 | std::wstring describeCommand(std::wstring command); |
|---|
| 151 | std::list<std::wstring> getAllCommandNames(); |
|---|
| 152 | void registerCommand(std::wstring cmd, std::wstring desc); |
|---|
| 153 | unsigned int getBufferLength(); |
|---|
| 154 | void HandleSettingsCLI(TCHAR* arg, int argc, TCHAR* argv[]); |
|---|
| 155 | void startTrayIcons(); |
|---|
| 156 | void startTrayIcon(DWORD dwSessionId); |
|---|
| 157 | |
|---|
| 158 | bool logDebug(); |
|---|
| 159 | void listPlugins(); |
|---|
| 160 | plugin_info_list get_all_plugins(); |
|---|
| 161 | |
|---|
| 162 | // Shared session interface: |
|---|
| 163 | void session_error(std::wstring file, unsigned int line, std::wstring msg); |
|---|
| 164 | void session_info(std::wstring file, unsigned int line, std::wstring msg); |
|---|
| 165 | void session_log_message(int msgType, const TCHAR* file, const int line, std::wstring message) { |
|---|
| 166 | reportMessage(msgType, file, line, message); |
|---|
| 167 | } |
|---|
| 168 | int session_inject(std::wstring command, std::wstring arguments, TCHAR splitter, bool escape, std::wstring &msg, std::wstring & perf) { |
|---|
| 169 | return inject(command, arguments, splitter, escape, msg, perf); |
|---|
| 170 | } |
|---|
| 171 | std::pair<std::wstring,std::wstring> session_get_name() { |
|---|
| 172 | return std::pair<std::wstring,std::wstring>(SZAPPNAME,SZVERSION); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | private: |
|---|
| 178 | plugin_type addPlugin(plugin_type plugin); |
|---|
| 179 | void load_all_plugins(int mode); |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | typedef service_helper::NTService<NSClientT> NSClient; |
|---|
| 183 | |
|---|
| 184 | extern NSClient mainClient; // Global core instance forward declaration. |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | std::wstring Encrypt(std::wstring str, unsigned int algorithm = NSCAPI::xor); |
|---|
| 188 | std::wstring Decrypt(std::wstring str, unsigned int algorithm = NSCAPI::xor); |
|---|
| 189 | |
|---|
| 190 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 191 | // Log macros to simplify logging |
|---|
| 192 | // Generally names are of the form LOG_<severity>[_STD] |
|---|
| 193 | // Where _STD indicates that strings are force wrapped inside a std::wstring |
|---|
| 194 | // |
|---|
| 195 | #define LOG_ERROR_STD(msg) LOG_ERROR(((std::wstring)msg).c_str()) |
|---|
| 196 | #define LOG_ERROR(msg) \ |
|---|
| 197 | NSAPIMessage(NSCAPI::error, _T(__FILE__), __LINE__, msg) |
|---|
| 198 | #define LOG_CRITICAL_STD(msg) LOG_CRITICAL(((std::wstring)msg).c_str()) |
|---|
| 199 | #define LOG_CRITICAL(msg) \ |
|---|
| 200 | NSAPIMessage(NSCAPI::critical, _T(__FILE__), __LINE__, msg) |
|---|
| 201 | #define LOG_MESSAGE_STD(msg) LOG_MESSAGE(((std::wstring)msg).c_str()) |
|---|
| 202 | #define LOG_MESSAGE(msg) \ |
|---|
| 203 | NSAPIMessage(NSCAPI::log, _T(__FILE__), __LINE__, msg) |
|---|
| 204 | |
|---|
| 205 | #define LOG_DEBUG_STD(msg) LOG_DEBUG(((std::wstring)msg).c_str()) |
|---|
| 206 | #define LOG_DEBUG(msg) \ |
|---|
| 207 | NSAPIMessage(NSCAPI::debug, _T(__FILE__), __LINE__, msg) |
|---|
| 208 | /* |
|---|
| 209 | #define LOG_DEBUG_STD(msg) |
|---|
| 210 | #define LOG_DEBUG(msg) |
|---|
| 211 | */ |
|---|