| 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 | |
|---|
| 24 | |
|---|
| 25 | #include <types.hpp> |
|---|
| 26 | #include <service/system_service.hpp> |
|---|
| 27 | |
|---|
| 28 | #include "NSCPlugin.h" |
|---|
| 29 | #include "commands.hpp" |
|---|
| 30 | #include "channels.hpp" |
|---|
| 31 | #include "routers.hpp" |
|---|
| 32 | #include <nsclient/logger.hpp> |
|---|
| 33 | |
|---|
| 34 | class NSClientT; |
|---|
| 35 | typedef service_helper::impl<NSClientT>::system_service NSClient; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * @ingroup NSClient++ |
|---|
| 39 | * Main NSClient++ core class. This is the service core and as such is responsible for pretty much everything. |
|---|
| 40 | * It also acts as a broker for all plugins and other sub threads and such. |
|---|
| 41 | * |
|---|
| 42 | * @version 1.0 |
|---|
| 43 | * first version |
|---|
| 44 | * |
|---|
| 45 | * @date 02-12-2005 |
|---|
| 46 | * |
|---|
| 47 | * @author mickem |
|---|
| 48 | * |
|---|
| 49 | * @par license |
|---|
| 50 | * This code is absolutely free to use and modify. The code is provided "as is" with |
|---|
| 51 | * no expressed or implied warranty. The author accepts no liability if it causes |
|---|
| 52 | * any damage to your computer, causes your pet to fall ill, increases baldness |
|---|
| 53 | * or makes your car start emitting strange noises when you start it up. |
|---|
| 54 | * This code has no bugs, just undocumented features! |
|---|
| 55 | * |
|---|
| 56 | * @todo Plugininfy the socket somehow ? |
|---|
| 57 | * It is technically possible to make the socket a plug-in but would it be a good idea ? |
|---|
| 58 | * |
|---|
| 59 | * @bug |
|---|
| 60 | * |
|---|
| 61 | */ |
|---|
| 62 | class NSClientT { |
|---|
| 63 | |
|---|
| 64 | public: |
|---|
| 65 | typedef boost::shared_ptr<NSCPlugin> plugin_type; |
|---|
| 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 | |
|---|
| 84 | typedef std::vector<plugin_type> pluginList; |
|---|
| 85 | pluginList plugins_; |
|---|
| 86 | boost::filesystem::path basePath; |
|---|
| 87 | boost::filesystem::path tempPath; |
|---|
| 88 | boost::timed_mutex internalVariables; |
|---|
| 89 | boost::shared_mutex m_mutexRW; |
|---|
| 90 | |
|---|
| 91 | std::string context_; |
|---|
| 92 | |
|---|
| 93 | bool enable_shared_session_; |
|---|
| 94 | nsclient::commands commands_; |
|---|
| 95 | nsclient::channels channels_; |
|---|
| 96 | nsclient::routers routers_; |
|---|
| 97 | unsigned int next_plugin_id_; |
|---|
| 98 | std::wstring service_name_; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | public: |
|---|
| 102 | typedef std::multimap<std::string,std::string> plugin_alias_list_type; |
|---|
| 103 | // c-tor, d-tor |
|---|
| 104 | NSClientT(); |
|---|
| 105 | virtual ~NSClientT(); |
|---|
| 106 | |
|---|
| 107 | // Service helper functions |
|---|
| 108 | bool boot_init(std::string log_level = ""); |
|---|
| 109 | bool boot_load_all_plugins(); |
|---|
| 110 | bool boot_load_plugin(std::string plugin); |
|---|
| 111 | bool boot_start_plugins(bool boot); |
|---|
| 112 | |
|---|
| 113 | bool stop_unload_plugins_pre(); |
|---|
| 114 | bool stop_exit_pre(); |
|---|
| 115 | bool stop_exit_post(); |
|---|
| 116 | void set_settings_context(std::string context) { context_ = context; } |
|---|
| 117 | void service_on_session_changed(DWORD dwSessionId, bool logon, DWORD dwEventType); |
|---|
| 118 | |
|---|
| 119 | // Service API |
|---|
| 120 | static NSClient* get_global_instance(); |
|---|
| 121 | void handle_startup(std::wstring service_name); |
|---|
| 122 | void handle_shutdown(std::wstring service_name); |
|---|
| 123 | #ifdef _WIN32 |
|---|
| 124 | void handle_session_change(unsigned long dwSessionId, bool logon); |
|---|
| 125 | #endif |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | // Member functions |
|---|
| 129 | boost::filesystem::path getBasePath(); |
|---|
| 130 | boost::filesystem::path getTempPath(); |
|---|
| 131 | |
|---|
| 132 | NSCAPI::errorReturn reroute(std::string &channel, std::string &buffer); |
|---|
| 133 | NSCAPI::errorReturn send_notification(const char* channel, std::string &request, std::string &response); |
|---|
| 134 | NSCAPI::nagiosReturn injectRAW(std::string &request, std::string &response); |
|---|
| 135 | NSCAPI::nagiosReturn inject(std::string command, std::string arguments, std::string &msg, std::string & perf); |
|---|
| 136 | std::wstring execute(std::wstring password, std::wstring cmd, std::list<std::wstring> args); |
|---|
| 137 | int simple_exec(std::string command, std::vector<std::string> arguments, std::list<std::string> &resp); |
|---|
| 138 | int simple_query(std::string module, std::string command, std::vector<std::string> arguments, std::list<std::string> &resp); |
|---|
| 139 | NSCAPI::nagiosReturn exec_command(const char* target, std::string &request, std::string &response); |
|---|
| 140 | NSCAPI::errorReturn register_submission_listener(unsigned int plugin_id, const char* channel); |
|---|
| 141 | NSCAPI::errorReturn register_routing_listener(unsigned int plugin_id, const char* channel); |
|---|
| 142 | NSCAPI::errorReturn settings_query(const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len); |
|---|
| 143 | NSCAPI::errorReturn registry_query(const char *request_buffer, const unsigned int request_buffer_len, char **response_buffer, unsigned int *response_buffer_len); |
|---|
| 144 | |
|---|
| 145 | NSCAPI::errorReturn reload(const std::string module); |
|---|
| 146 | bool do_reload(const bool delay, const std::string module); |
|---|
| 147 | |
|---|
| 148 | struct service_controller { |
|---|
| 149 | std::wstring service; |
|---|
| 150 | service_controller(std::wstring service) : service(service) {} |
|---|
| 151 | service_controller(const service_controller & other) : service(other.service) {} |
|---|
| 152 | service_controller& operator=(const service_controller & other) { |
|---|
| 153 | service = other.service; |
|---|
| 154 | return *this; |
|---|
| 155 | } |
|---|
| 156 | void stop(); |
|---|
| 157 | void start(); |
|---|
| 158 | std::wstring get_service_name() { |
|---|
| 159 | return service; |
|---|
| 160 | } |
|---|
| 161 | bool is_started(); |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | service_controller get_service_control(); |
|---|
| 165 | |
|---|
| 166 | //plugin_type loadPlugin(const boost::filesystem::path plugin, std::wstring alias); |
|---|
| 167 | void loadPlugins(NSCAPI::moduleLoadMode mode); |
|---|
| 168 | void unloadPlugins(); |
|---|
| 169 | std::string describeCommand(std::string command); |
|---|
| 170 | std::list<std::string> getAllCommandNames(); |
|---|
| 171 | void registerCommand(unsigned int id, std::string cmd, std::string desc); |
|---|
| 172 | void startTrayIcons(); |
|---|
| 173 | void startTrayIcon(DWORD dwSessionId); |
|---|
| 174 | |
|---|
| 175 | void listPlugins(); |
|---|
| 176 | plugin_info_list get_all_plugins(); |
|---|
| 177 | std::string get_plugin_module_name(unsigned int plugin_id); |
|---|
| 178 | plugin_alias_list_type find_all_plugins(bool active); |
|---|
| 179 | std::list<std::string> list_commands(); |
|---|
| 180 | |
|---|
| 181 | std::string getFolder(std::string key); |
|---|
| 182 | std::string expand_path(std::string file); |
|---|
| 183 | |
|---|
| 184 | typedef boost::function<int(plugin_type)> run_function; |
|---|
| 185 | int load_and_run(std::string module, run_function fun, std::list<std::string> &errors); |
|---|
| 186 | |
|---|
| 187 | public: |
|---|
| 188 | void preboot_load_all_plugin_files(); |
|---|
| 189 | |
|---|
| 190 | private: |
|---|
| 191 | plugin_type addPlugin(boost::filesystem::path file, std::string alias); |
|---|
| 192 | }; |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | extern NSClient mainClient; // Global core instance forward declaration. |
|---|