source: nscp/service/NSClient++.h @ d05c3f0

0.4.00.4.10.4.2
Last change on this file since d05c3f0 was d05c3f0, checked in by Michael Medin <michael@…>, 4 years ago

Next major "milestone" we now have one working plugin (CheckHelpers) which loads and works both on Linux and Windows.
This is (as always) a broken build which many many features disabled and non functional...

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