source: nscp/NSClient++.h @ 3692371

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

2008-09-17 MickeM - 0.3.4 RC-6

  • Fixed issue on all filters so == takes the "usual" 2 equalsigns (old still works).
  • Added so tray icon can get "propper" name from core for "description".

2008-09-16 MickeM - 0.3.4 RC-5

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