clients can connect to server via IPv6
[public/netxms.git] / src / server / include / nxsrvapi.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Server Library
619e5c9b 4** Copyright (C) 2003-2011 Victor Kirhenshtein
5039dede
AK
5**
6** This program is free software; you can redistribute it and/or modify
0702ed69
VK
7** it under the terms of the GNU Lesser General Public License as published by
8** the Free Software Foundation; either version 3 of the License, or
5039dede
AK
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**
0702ed69 16** You should have received a copy of the GNU Lesser General Public License
5039dede
AK
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: nxsrvapi.h
21**
22**/
23
24#ifndef _nxsrvapi_h_
25#define _nxsrvapi_h_
26
27#ifdef _WIN32
28#ifdef LIBNXSRV_EXPORTS
29#define LIBNXSRV_EXPORTABLE __declspec(dllexport)
30#else
31#define LIBNXSRV_EXPORTABLE __declspec(dllimport)
32#endif
33#else /* _WIN32 */
34#define LIBNXSRV_EXPORTABLE
35#endif
36
37#ifndef LIBNXCL_NO_DECLARATIONS
38#define LIBNXCL_NO_DECLARATIONS 1
39#endif
40#include <nxclapi.h>
41#include <nxcpapi.h>
42#include <nms_agent.h>
944016d6 43#include "../libnxsrv/messages.h"
5039dede
AK
44#include <nxsnmp.h>
45#include <netxms_isc.h>
46
47
48//
49// Default files
50//
51
52#ifdef _WIN32
53
9796ce45
VK
54#define DEFAULT_CONFIG_FILE _T("C:\\netxmsd.conf")
55
56#define DEFAULT_SHELL _T("cmd.exe")
57#define DEFAULT_LOG_FILE _T("C:\\NetXMS.log")
58#define DEFAULT_DATA_DIR _T("C:\\NetXMS\\var")
59#define DEFAULT_LIBDIR _T("C:\\NetXMS\\lib")
60#define DEFAULT_DUMP_DIR _T("C:\\")
61
62#define LDIR_NDD _T("\\ndd")
63
64#define DDIR_MIBS _T("\\mibs")
65#define DDIR_PACKAGES _T("\\packages")
66#define DDIR_BACKGROUNDS _T("\\backgrounds")
67#define DDIR_SHARED_FILES _T("\\shared")
68#define DFILE_KEYS _T("\\server_key")
69#define DFILE_COMPILED_MIB _T("\\mibs\\netxms.mib")
70#define DDIR_IMAGES _T("\\images")
71#define DDIR_FILES _T("\\files")
5039dede
AK
72
73#else /* _WIN32 */
74
9796ce45 75#define DEFAULT_CONFIG_FILE _T("{search}")
5039dede 76
9796ce45 77#define DEFAULT_SHELL _T("/bin/sh")
5039dede 78
9796ce45
VK
79#ifndef DATADIR
80#define DATADIR _T("/var/netxms")
81#endif
5039dede 82
9796ce45
VK
83#ifndef LIBDIR
84#define LIBDIR _T("/usr/lib")
85#endif
5039dede 86
644f97f5
VK
87#ifndef PKGLIBDIR
88#define PKGLIBDIR _T("/usr/lib/netxms")
89#endif
90
9796ce45
VK
91#define DEFAULT_LOG_FILE DATADIR _T("/log/netxmsd.log")
92#define DEFAULT_DATA_DIR DATADIR
644f97f5 93#define DEFAULT_LIBDIR PKGLIBDIR
9796ce45
VK
94#define DEFAULT_DUMP_DIR _T("/")
95
96#define LDIR_NDD _T("/ndd")
97
98#define DDIR_MIBS _T("/mibs")
99#define DDIR_PACKAGES _T("/packages")
100#define DDIR_BACKGROUNDS _T("/backgrounds")
101#define DDIR_SHARED_FILES _T("/shared")
102#define DFILE_KEYS _T("/.server_key")
103#define DFILE_COMPILED_MIB _T("/mibs/netxms.mib")
104#define DDIR_IMAGES _T("/images")
105#define DDIR_FILES _T("/files")
5039dede
AK
106
107#endif /* _WIN32 */
108
109
110//
111// Application flags
112//
113
114#define AF_DAEMON 0x00000001
115#define AF_USE_SYSLOG 0x00000002
116#define AF_ENABLE_NETWORK_DISCOVERY 0x00000004
117#define AF_ACTIVE_NETWORK_DISCOVERY 0x00000008
118#define AF_LOG_SQL_ERRORS 0x00000010
119#define AF_DELETE_EMPTY_SUBNETS 0x00000020
120#define AF_ENABLE_SNMP_TRAPD 0x00000040
121#define AF_ENABLE_ZONING 0x00000080
122#define AF_SYNC_NODE_NAMES_WITH_DNS 0x00000100
123#define AF_CHECK_TRUSTED_NODES 0x00000200
cbc777ee 124#define AF_WRITE_FULL_DUMP 0x00080000
5039dede
AK
125#define AF_RESOLVE_NODE_NAMES 0x00100000
126#define AF_CATCH_EXCEPTIONS 0x00200000
127#define AF_INTERNAL_CA 0x00400000
128#define AF_DB_LOCKED 0x01000000
129#define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
130#define AF_DB_CONNECTION_LOST 0x04000000
131#define AF_NO_NETWORK_CONNECTIVITY 0x08000000
132#define AF_EVENT_STORM_DETECTED 0x08000000
133#define AF_SERVER_INITIALIZED 0x40000000
134#define AF_SHUTDOWN 0x80000000
135
136#define IsStandalone() (!(g_dwFlags & AF_DAEMON))
137#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
138
139
140//
141// Encryption usage policies
142//
143
144#define ENCRYPTION_DISABLED 0
145#define ENCRYPTION_ALLOWED 1
146#define ENCRYPTION_PREFERRED 2
147#define ENCRYPTION_REQUIRED 3
148
149
5ad2167d
VK
150//
151// Flags for SnmpGet
152//
153
154#define SG_VERBOSE 0x0001
155#define SG_STRING_RESULT 0x0002
156#define SG_RAW_RESULT 0x0004
157#define SG_HSTRING_RESULT 0x0008
158
159
5039dede
AK
160//
161// Win32 service and syslog constants
162//
163
164#ifdef _WIN32
165
166#define CORE_SERVICE_NAME _T("NetXMSCore")
167#define CORE_EVENT_SOURCE _T("NetXMSCore")
168#define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
169
170#else
171
172#define NETXMSD_SYSLOG_NAME _T("netxmsd")
173
174#endif /* _WIN32 */
175
176
177//
178// Single ARP cache entry
179//
180
181typedef struct
182{
183 DWORD dwIndex; // Interface index
184 DWORD dwIpAddr;
185 BYTE bMacAddr[MAC_ADDR_LENGTH];
186} ARP_ENTRY;
187
188
189//
190// ARP cache structure used by discovery functions and AgentConnection class
191//
192
193typedef struct
194{
195 DWORD dwNumEntries;
196 ARP_ENTRY *pEntries;
197} ARP_CACHE;
198
199
200//
201// Interface information structure used by discovery functions and AgentConnection class
202//
203
204typedef struct
205{
206 TCHAR szName[MAX_DB_STRING];
207 DWORD dwIndex;
208 DWORD dwType;
eec253a8
VK
209 DWORD dwBridgePortNumber;
210 DWORD dwSlotNumber;
211 DWORD dwPortNumber;
5039dede
AK
212 DWORD dwIpAddr;
213 DWORD dwIpNetMask;
214 BYTE bMacAddr[MAC_ADDR_LENGTH];
215 int iNumSecondary; // Number of secondary IP's on this interface
36e44abe 216} NX_INTERFACE_INFO;
5039dede
AK
217
218
219//
220// Interface list used by discovery functions and AgentConnection class
221//
222
98762401 223class LIBNXSRV_EXPORTABLE InterfaceList
5039dede 224{
98762401
VK
225private:
226 int m_size; // Number of valid entries
227 int m_allocated; // Number of allocated entries
228 void *m_data; // Can be used by custom enumeration handlers
36e44abe 229 NX_INTERFACE_INFO *m_interfaces; // Interface entries
98762401
VK
230
231public:
232 InterfaceList(int initialAlloc = 8);
233 ~InterfaceList();
234
36e44abe 235 void add(NX_INTERFACE_INFO *iface);
98762401
VK
236 void remove(int index);
237 void removeLoopbacks();
238
239 int getSize() { return m_size; }
36e44abe 240 NX_INTERFACE_INFO *get(int index) { return ((index >= 0) && (index < m_size)) ? &m_interfaces[index] : NULL; }
98762401
VK
241
242 void setData(void *data) { m_data = data; }
243 void *getData() { return m_data; }
244};
5039dede
AK
245
246
247//
248// Route information
249//
250
251typedef struct
252{
253 DWORD dwDestAddr;
254 DWORD dwDestMask;
255 DWORD dwNextHop;
256 DWORD dwIfIndex;
257 DWORD dwRouteType;
258} ROUTE;
259
260
261//
262// Routing table
263//
264
265typedef struct
266{
267 int iNumEntries; // Number of entries
268 ROUTE *pRoutes; // Route list
269} ROUTING_TABLE;
270
271
1f385e47
VK
272//
273// Information about policies installed on agent
274//
275
276class LIBNXSRV_EXPORTABLE AgentPolicyInfo
277{
278private:
279 int m_size;
280 BYTE *m_guidList;
281 int *m_typeList;
282 TCHAR **m_serverList;
283
284public:
285 AgentPolicyInfo(CSCPMessage *msg);
286 ~AgentPolicyInfo();
287
288 int getSize() { return m_size; }
289 bool getGuid(int index, uuid_t guid);
290 int getType(int index) { return ((index >= 0) && (index < m_size)) ? m_typeList[index] : -1; }
291 const TCHAR *getServer(int index) { return ((index >= 0) && (index < m_size)) ? m_serverList[index] : NULL; }
292};
293
294
5039dede
AK
295//
296// Agent connection
297//
298
299class LIBNXSRV_EXPORTABLE AgentConnection
300{
301private:
302 DWORD m_dwAddr;
303 int m_nProtocolVersion;
304 int m_iAuthMethod;
08b214c6 305 char m_szSecret[MAX_SECRET_LENGTH];
5039dede
AK
306 time_t m_tLastCommandTime;
307 SOCKET m_hSocket;
308 DWORD m_dwNumDataLines;
309 DWORD m_dwRequestId;
310 DWORD m_dwCommandTimeout;
7c521895 311 DWORD m_connectionTimeout;
5039dede
AK
312 DWORD m_dwRecvTimeout;
313 TCHAR **m_ppDataLines;
314 MsgWaitQueue *m_pMsgWaitQueue;
315 BOOL m_bIsConnected;
316 MUTEX m_mutexDataLock;
317 THREAD m_hReceiverThread;
318 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
319 int m_iEncryptionPolicy;
320 BOOL m_bUseProxy;
321 DWORD m_dwProxyAddr;
322 WORD m_wPort;
323 WORD m_wProxyPort;
324 int m_iProxyAuth;
08b214c6 325 char m_szProxySecret[MAX_SECRET_LENGTH];
5039dede 326 int m_hCurrFile;
9f6d453a 327 TCHAR m_currentFileName[MAX_PATH];
bb85e341
VK
328 DWORD m_dwDownloadRequestId;
329 CONDITION m_condFileDownload;
330 BOOL m_fileDownloadSucceeded;
331 void (*m_downloadProgressCallback)(size_t, void *);
332 void *m_downloadProgressCallbackArg;
901a5a9b 333 bool m_deleteFileOnDownloadFailure;
4685a2ad 334 bool m_fileUploadInProgress;
5039dede
AK
335
336 void ReceiverThread(void);
337 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
338
339protected:
7c521895
VK
340 void destroyResultData();
341 BOOL sendMessage(CSCPMessage *pMsg);
342 CSCPMessage *waitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
343 DWORD waitForRCC(DWORD dwRqId, DWORD dwTimeOut);
344 DWORD setupEncryption(RSA *pServerKey);
345 DWORD authenticate(BOOL bProxyData);
346 DWORD setupProxyConnection();
347 DWORD getIpAddr() { return ntohl(m_dwAddr); }
348 DWORD prepareFileDownload(const TCHAR *fileName, DWORD rqId, bool append, void (*downloadProgressCallback)(size_t, void *), void *cbArg);
5039dede
AK
349
350 virtual void PrintMsg(const TCHAR *pszFormat, ...);
f480bdd4
VK
351 virtual void onTrap(CSCPMessage *pMsg);
352 virtual void onDataPush(CSCPMessage *msg);
90284364 353 virtual bool processCustomMessage(CSCPMessage *pMsg);
f480bdd4 354 virtual void onFileDownload(BOOL success);
5039dede
AK
355
356 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
357 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
358
359public:
5039dede
AK
360 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
361 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
362 virtual ~AgentConnection();
363
c3acd0f6 364 BOOL connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL, DWORD *pdwSocketError = NULL);
7c521895
VK
365 void disconnect();
366 BOOL isConnected() { return m_bIsConnected; }
367 int getProtocolVersion() { return m_nProtocolVersion; }
5039dede 368
45d84f8a 369 SOCKET getSocket() { return m_hSocket; }
5a9e58a3 370
4687826e 371 ARP_CACHE *getArpCache();
98762401 372 InterfaceList *getInterfaceList();
4687826e
VK
373 ROUTING_TABLE *getRoutingTable();
374 DWORD getParameter(const TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
375 DWORD getList(const TCHAR *pszParam);
376 DWORD getTable(const TCHAR *pszParam, Table **table);
7c521895 377 DWORD nop();
4687826e 378 DWORD execAction(const TCHAR *pszAction, int argc, TCHAR **argv);
619e5c9b 379 DWORD uploadFile(const TCHAR *localFile, const TCHAR *destinationFile = NULL, void (* progressCallback)(INT64, void *) = NULL, void *cbArg = NULL);
4687826e
VK
380 DWORD startUpgrade(const TCHAR *pszPkgName);
381 DWORD checkNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
5039dede 382 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL);
4687826e
VK
383 DWORD getSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
384 DWORD getConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
45d84f8a 385 DWORD updateConfigFile(const TCHAR *pszConfig);
7c521895 386 DWORD enableTraps();
1f385e47
VK
387 DWORD getPolicyInventory(AgentPolicyInfo **info);
388 DWORD uninstallPolicy(uuid_t guid);
e58b5e54
VK
389
390 DWORD generateRequestId() { return m_dwRequestId++; }
45d84f8a 391 CSCPMessage *customRequest(CSCPMessage *pRequest, const TCHAR *recvFile = NULL, bool appendFile = false,
f2665675 392 void (*downloadProgressCallback)(size_t, void *) = NULL, void *cbArg = NULL);
5039dede 393
7c521895
VK
394 DWORD getNumDataLines() { return m_dwNumDataLines; }
395 const TCHAR *getDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
5039dede 396
7c521895
VK
397 void setConnectionTimeout(DWORD dwTimeout) { m_connectionTimeout = max(dwTimeout, 1000); }
398 DWORD getConnectionTimeout() { return m_connectionTimeout; }
45d84f8a
VK
399 void setCommandTimeout(DWORD dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
400 DWORD getCommandTimeout() { return m_dwCommandTimeout; }
7c521895
VK
401 void setRecvTimeout(DWORD dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
402 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
403 void setProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
5039dede 404 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
7c521895 405 void setPort(WORD wPort) { m_wPort = wPort; }
08b214c6 406 void setAuthData(int nMethod, const char *pszSecret) { m_iAuthMethod = nMethod; strncpy(m_szSecret, pszSecret, MAX_SECRET_LENGTH); m_szSecret[MAX_SECRET_LENGTH - 1] = 0; }
901a5a9b 407 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
5039dede
AK
408};
409
410
411//
412// Proxy SNMP transport
413//
414
415class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
416{
417protected:
418 AgentConnection *m_pAgentConnection;
419 CSCPMessage *m_pResponse;
420 DWORD m_dwIpAddr;
421 WORD m_wPort;
422
423public:
424 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
425 virtual ~SNMP_ProxyTransport();
426
c4366266 427 virtual int readMessage(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
cd9f247e
VK
428 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
429 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
c4366266 430 virtual int sendMessage(SNMP_PDU *pdu);
5039dede
AK
431};
432
433
434//
435// ISC flags
436//
437
438#define ISCF_IS_CONNECTED ((DWORD)0x00000001)
439#define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
440
441
442//
443// Inter-server connection (ISC)
444//
445
446class LIBNXSRV_EXPORTABLE ISC
447{
448private:
449 DWORD m_flags;
450 DWORD m_addr;
451 WORD m_port;
452 SOCKET m_socket;
453 int m_protocolVersion;
454 DWORD m_requestId;
455 DWORD m_recvTimeout;
456 MsgWaitQueue *m_msgWaitQueue;
457 MUTEX m_mutexDataLock;
458 THREAD m_hReceiverThread;
459 CSCP_ENCRYPTION_CONTEXT *m_ctx;
460 DWORD m_commandTimeout;
461
462 void ReceiverThread(void);
463 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
464
465protected:
466 void DestroyResultData(void);
467 DWORD SetupEncryption(RSA *pServerKey);
468 DWORD ConnectToService(DWORD service);
469
470 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
471 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
472
473 virtual void PrintMsg(const TCHAR *format, ...);
474
475public:
476 ISC();
477 ISC(DWORD addr, WORD port = NETXMS_ISC_PORT);
478 virtual ~ISC();
479
480 DWORD Connect(DWORD service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
481 void Disconnect();
482
483 BOOL SendMessage(CSCPMessage *msg);
484 CSCPMessage *WaitForMessage(WORD code, DWORD id, DWORD timeOut) { return m_msgWaitQueue->WaitForMessage(code, id, timeOut); }
485 DWORD WaitForRCC(DWORD rqId, DWORD timeOut);
486
487 DWORD Nop(void);
488};
489
490
491//
492// Functions
493//
494
495void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
5039dede
AK
496void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
497void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
498const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
499
500void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...);
501void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...);
9d88cdc9 502void LIBNXSRV_EXPORTABLE DbgPrintf2(int level, const TCHAR *format, va_list args);
c20b2798 503
5039dede
AK
504void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
505
506const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(DWORD code);
507
5ad2167d
VK
508DWORD LIBNXSRV_EXPORTABLE SnmpNewRequestId();
509DWORD LIBNXSRV_EXPORTABLE SnmpGet(DWORD dwVersion, SNMP_Transport *pTransport,
510 const TCHAR *szOidStr, const DWORD *oidBinary, DWORD dwOidLen, void *pValue,
511 DWORD dwBufferSize, DWORD dwFlags);
512DWORD LIBNXSRV_EXPORTABLE SnmpEnumerate(DWORD dwVersion, SNMP_Transport *pTransport, const TCHAR *szRootOid,
513 DWORD (* pHandler)(DWORD, SNMP_Variable *, SNMP_Transport *, void *),
514 void *pUserArg, BOOL bVerbose);
515
5039dede
AK
516
517//
518// Variables
519//
520
521extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
5ad2167d 522extern DWORD LIBNXSRV_EXPORTABLE g_dwSNMPTimeout;
5039dede 523extern int LIBNXSRV_EXPORTABLE g_nDebugLevel;
5039dede
AK
524
525#endif /* _nxsrvapi_h_ */