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

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

tried boost::asio in the NRPECLient and split settings to a new file (reduces build time)

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