source: nscp/NSClient++.h @ 394f7a1

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

+ Added propper output handling to process subsystem (now you can execute programs tat return "much" data.

+ Added select support for SSL_write (now you can send "any amount of data" to the (SSL) socket.

Since check_nrpe doesn't do this it wont work in that end, but still...

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