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