| 1 | |
|---|
| 2 | /************************************************************************** |
|---|
| 3 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 4 | * * |
|---|
| 5 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 6 | * * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 8 | * it under the terms of the GNU General Public License as published by * |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 10 | * (at your option) any later version. * |
|---|
| 11 | * * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, * |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 15 | * GNU General Public License for more details. * |
|---|
| 16 | * * |
|---|
| 17 | * You should have received a copy of the GNU General Public License * |
|---|
| 18 | * along with this program; if not, write to the * |
|---|
| 19 | * Free Software Foundation, Inc., * |
|---|
| 20 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 21 | ***************************************************************************/ |
|---|
| 22 | #pragma once |
|---|
| 23 | #ifdef WIN32 |
|---|
| 24 | #include <com_helpers.hpp> |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | #include <types.hpp> |
|---|
| 28 | #include <config.h> |
|---|
| 29 | #include <service/system_service.hpp> |
|---|
| 30 | |
|---|
| 31 | #include "NSCPlugin.h" |
|---|
| 32 | #include "commands.hpp" |
|---|
| 33 | #include "channels.hpp" |
|---|
| 34 | #include "logger.hpp" |
|---|
| 35 | |
|---|
| 36 | //#include <nsclient_session.hpp> |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class NSClientT; |
|---|
| 40 | typedef service_helper::impl<NSClientT>::system_service NSClient; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * @ingroup NSClient++ |
|---|
| 44 | * Main NSClient++ core class. This is the service core and as such is responsible for pretty much everything. |
|---|
| 45 | * It also acts as a broker for all plugins and other sub threads and such. |
|---|
| 46 | * |
|---|
| 47 | * @version 1.0 |
|---|
| 48 | * first version |
|---|
| 49 | * |
|---|
| 50 | * @date 02-12-2005 |
|---|
| 51 | * |
|---|
| 52 | * @author mickem |
|---|
| 53 | * |
|---|
| 54 | * @par license |
|---|
| 55 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 56 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 57 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 58 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 59 | * This code has no bugs, just undocumented features! |
|---|
| 60 | * |
|---|
| 61 | * @todo Plugininfy the socket somehow ? |
|---|
| 62 | * It is technically possible to make the socket a plug-in but would it be a good idea ? |
|---|
| 63 | * |
|---|
| 64 | * @bug |
|---|
| 65 | * |
|---|
| 66 | */ |
|---|
| 67 | class NSClientT : public nsclient::logger /*: public nsclient_session::session_handler_interface*/ { |
|---|
| 68 | |
|---|
| 69 | public: |
|---|
| 70 | struct plugin_info_type { |
|---|
| 71 | std::wstring dll; |
|---|
| 72 | std::wstring name; |
|---|
| 73 | std::wstring version; |
|---|
| 74 | std::wstring description; |
|---|
| 75 | }; |
|---|
| 76 | typedef std::list<plugin_info_type> plugin_info_list; |
|---|
| 77 | nsclient::logging_queue::master logger_master_; |
|---|
| 78 | private: |
|---|
| 79 | |
|---|
| 80 | class NSException { |
|---|
| 81 | std::wstring what_; |
|---|
| 82 | public: |
|---|
| 83 | NSException(std::wstring what) : what_(what){} |
|---|
| 84 | std::wstring what() { |
|---|
| 85 | return what_; |
|---|
| 86 | } |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | typedef boost::shared_ptr<NSCPlugin> plugin_type; |
|---|
| 90 | typedef std::vector<plugin_type> pluginList; |
|---|
| 91 | pluginList plugins_; |
|---|
| 92 | boost::filesystem::wpath basePath; |
|---|
| 93 | boost::timed_mutex internalVariables; |
|---|
| 94 | boost::shared_mutex m_mutexRW; |
|---|
| 95 | |
|---|
| 96 | //boost::shared_mutex m_mutexRWcmdDescriptions; |
|---|
| 97 | //cmdMap cmdDescriptions_; |
|---|
| 98 | enum log_status {log_unknown, log_looking, log_debug, log_nodebug }; |
|---|
| 99 | log_status debug_; |
|---|
| 100 | std::wstring context_; |
|---|
| 101 | #ifdef WIN32 |
|---|
| 102 | com_helper::initialize_com com_helper_; |
|---|
| 103 | #endif |
|---|
| 104 | /* |
|---|
| 105 | std::auto_ptr<nsclient_session::shared_client_session> shared_client_; |
|---|
| 106 | std::auto_ptr<nsclient_session::shared_server_session> shared_server_; |
|---|
| 107 | */ |
|---|
| 108 | |
|---|
| 109 | bool enable_shared_session_; |
|---|
| 110 | nsclient::commands commands_; |
|---|
| 111 | nsclient::channels channels_; |
|---|
| 112 | unsigned int next_plugin_id_; |
|---|
| 113 | std::wstring service_name_; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | public: |
|---|
| 117 | typedef std::multimap<std::wstring,std::wstring> plugin_alias_list_type; |
|---|
| 118 | // c-tor, d-tor |
|---|
| 119 | NSClientT(void) : debug_(log_unknown), enable_shared_session_(false), commands_(this), channels_(this), next_plugin_id_(0), service_name_(DEFAULT_SERVICE_NAME) { |
|---|
| 120 | logger_master_.start_slave(); |
|---|
| 121 | } |
|---|
| 122 | virtual ~NSClientT(void) {} |
|---|
| 123 | void enableDebug(bool debug = true) { |
|---|
| 124 | if (debug) |
|---|
| 125 | debug_ = log_debug; |
|---|
| 126 | else |
|---|
| 127 | debug_ = log_nodebug; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | // Service helper functions |
|---|
| 131 | bool initCore(bool boot); |
|---|
| 132 | bool exitCore(bool boot); |
|---|
| 133 | void set_settings_context(std::wstring context) { context_ = context; } |
|---|
| 134 | #ifdef WIN32x |
|---|
| 135 | static void WINAPI service_main_dispatch(DWORD dwArgc, LPTSTR *lpszArgv); |
|---|
| 136 | static void WINAPI service_ctrl_dispatch(DWORD dwCtrlCode); |
|---|
| 137 | static DWORD WINAPI service_ctrl_dispatch_ex(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext); |
|---|
| 138 | #endif |
|---|
| 139 | void service_on_session_changed(DWORD dwSessionId, bool logon, DWORD dwEventType); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | // Logger impl |
|---|
| 143 | void nsclient_log_error(std::string file, int line, std::wstring error); |
|---|
| 144 | |
|---|
| 145 | // Service API |
|---|
| 146 | static NSClient* get_global_instance(); |
|---|
| 147 | void handle_error(unsigned int line, const char *file, std::wstring message) { |
|---|
| 148 | std::string s = nsclient::logger_helper::create_error(file, line, message); |
|---|
| 149 | reportMessage(s.c_str()); |
|---|
| 150 | } |
|---|
| 151 | void handle_startup(std::wstring service_name); |
|---|
| 152 | void handle_shutdown(std::wstring service_name); |
|---|
| 153 | #ifdef _WIN32 |
|---|
| 154 | void handle_session_change(unsigned long dwSessionId, bool logon); |
|---|
| 155 | #endif |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | // Member functions |
|---|
| 159 | boost::filesystem::wpath getBasePath(void); |
|---|
| 160 | NSCAPI::errorReturn send_notification(const wchar_t* channel, const wchar_t* command, NSCAPI::nagiosReturn code, char* result, unsigned int result_len); |
|---|
| 161 | NSCAPI::nagiosReturn injectRAW(const wchar_t* command, std::string &request, std::string &response); |
|---|
| 162 | NSCAPI::nagiosReturn inject(std::wstring command, std::wstring arguments, std::wstring &msg, std::wstring & perf); |
|---|
| 163 | // std::wstring inject(const std::wstring buffer); |
|---|
| 164 | std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); |
|---|
| 165 | void reportMessage(std::string data); |
|---|
| 166 | int commandLineExec(const wchar_t* module, const unsigned int argLen, wchar_t** args); |
|---|
| 167 | |
|---|
| 168 | struct service_controller { |
|---|
| 169 | std::wstring service; |
|---|
| 170 | service_controller(std::wstring service) : service(service) {} |
|---|
| 171 | service_controller(const service_controller & other) : service(other.service) {} |
|---|
| 172 | service_controller& operator=(const service_controller & other) { |
|---|
| 173 | service = other.service; |
|---|
| 174 | return *this; |
|---|
| 175 | } |
|---|
| 176 | void stop(); |
|---|
| 177 | void start(); |
|---|
| 178 | std::wstring get_service_name() { |
|---|
| 179 | return service; |
|---|
| 180 | } |
|---|
| 181 | bool is_started(); |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | service_controller get_service_control(); |
|---|
| 185 | |
|---|
| 186 | //plugin_type loadPlugin(const boost::filesystem::wpath plugin, std::wstring alias); |
|---|
| 187 | void loadPlugins(NSCAPI::moduleLoadMode mode); |
|---|
| 188 | void unloadPlugins(bool unloadLoggers); |
|---|
| 189 | std::wstring describeCommand(std::wstring command); |
|---|
| 190 | std::list<std::wstring> getAllCommandNames(); |
|---|
| 191 | void registerCommand(unsigned int id, std::wstring cmd, std::wstring desc); |
|---|
| 192 | void startTrayIcons(); |
|---|
| 193 | void startTrayIcon(DWORD dwSessionId); |
|---|
| 194 | |
|---|
| 195 | bool logDebug(); |
|---|
| 196 | void listPlugins(); |
|---|
| 197 | plugin_info_list get_all_plugins(); |
|---|
| 198 | plugin_alias_list_type find_all_plugins(bool active); |
|---|
| 199 | std::list<std::wstring> list_commands(); |
|---|
| 200 | |
|---|
| 201 | // Shared session interface: |
|---|
| 202 | void session_error(std::string file, unsigned int line, std::wstring msg); |
|---|
| 203 | void session_info(std::string file, unsigned int line, std::wstring msg); |
|---|
| 204 | void session_log_message(int msgType, const char* file, const int line, std::wstring message) { |
|---|
| 205 | std::string s = nsclient::logger_helper::create_info(file, line, message); |
|---|
| 206 | reportMessage(s.c_str()); |
|---|
| 207 | } |
|---|
| 208 | int session_inject(std::wstring command, std::wstring arguments, wchar_t splitter, bool escape, std::wstring &msg, std::wstring & perf) { |
|---|
| 209 | return 0; // TODO: Readd this!!! inject(command, arguments, splitter, escape, msg, perf); |
|---|
| 210 | } |
|---|
| 211 | // std::pair<std::wstring,std::wstring> session_get_name() { |
|---|
| 212 | // return std::pair<std::wstring,std::wstring>(SZAPPNAME,SZVERSION); |
|---|
| 213 | // } |
|---|
| 214 | |
|---|
| 215 | std::wstring expand_path(std::wstring file); |
|---|
| 216 | void set_console_log() { |
|---|
| 217 | logger_master_.set_console_log(); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | public: |
|---|
| 222 | void load_all_plugins(int mode); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | private: |
|---|
| 226 | plugin_type addPlugin(boost::filesystem::wpath file, std::wstring alias); |
|---|
| 227 | }; |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | extern NSClient mainClient; // Global core instance forward declaration. |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | std::wstring Encrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor); |
|---|
| 234 | std::wstring Decrypt(std::wstring str, unsigned int algorithm = NSCAPI::encryption_xor); |
|---|