schema-related information (like schema version, DB syntax, etc.) moved from "config...
[public/netxms.git] / src / server / include / nxsrvapi.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Server Library
ea6f474a 4** Copyright (C) 2003-2009 Victor Kirhenshtein
5039dede
AK
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 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 <dbdrv.h>
45#include <nxsnmp.h>
46#include <netxms_isc.h>
47
48
49//
50// Default files
51//
52
53#ifdef _WIN32
54
55# define DEFAULT_CONFIG_FILE _T("C:\\netxmsd.conf")
56
57# define DEFAULT_SHELL "cmd.exe"
58# define DEFAULT_LOG_FILE "C:\\NetXMS.log"
59# define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
60# define DEFAULT_DUMP_DIR "C:\\"
61
62# define DDIR_MIBS "\\mibs"
63# define DDIR_IMAGES "\\images"
64# define DDIR_PACKAGES "\\packages"
65# define DDIR_BACKGROUNDS "\\backgrounds"
66# define DDIR_SHARED_FILES "\\shared"
67# define DFILE_KEYS "\\server_key"
68# define DFILE_COMPILED_MIB "\\mibs\\netxms.mib"
69
70#else /* _WIN32 */
71
72# define DEFAULT_CONFIG_FILE _T("{search}")
73
74# define DEFAULT_SHELL "/bin/sh"
75
76# ifndef DATADIR
77# define DATADIR "/var/netxms"
78# endif
79
80# define DEFAULT_LOG_FILE DATADIR"/log/netxmsd.log"
81# define DEFAULT_DATA_DIR DATADIR
82# define DEFAULT_DUMP_DIR "/"
83
84# define DDIR_MIBS "/mibs"
85# define DDIR_IMAGES "/images"
86# define DDIR_PACKAGES "/packages"
87# define DDIR_BACKGROUNDS "/backgrounds"
88# define DDIR_SHARED_FILES "/shared"
89# define DFILE_KEYS "/.server_key"
90# define DFILE_COMPILED_MIB "/mibs/netxms.mib"
91
92#endif /* _WIN32 */
93
94
95//
96// Application flags
97//
98
99#define AF_DAEMON 0x00000001
100#define AF_USE_SYSLOG 0x00000002
101#define AF_ENABLE_NETWORK_DISCOVERY 0x00000004
102#define AF_ACTIVE_NETWORK_DISCOVERY 0x00000008
103#define AF_LOG_SQL_ERRORS 0x00000010
104#define AF_DELETE_EMPTY_SUBNETS 0x00000020
105#define AF_ENABLE_SNMP_TRAPD 0x00000040
106#define AF_ENABLE_ZONING 0x00000080
107#define AF_SYNC_NODE_NAMES_WITH_DNS 0x00000100
108#define AF_CHECK_TRUSTED_NODES 0x00000200
109#define AF_RESOLVE_NODE_NAMES 0x00100000
110#define AF_CATCH_EXCEPTIONS 0x00200000
111#define AF_INTERNAL_CA 0x00400000
112#define AF_DB_LOCKED 0x01000000
113#define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
114#define AF_DB_CONNECTION_LOST 0x04000000
115#define AF_NO_NETWORK_CONNECTIVITY 0x08000000
116#define AF_EVENT_STORM_DETECTED 0x08000000
117#define AF_SERVER_INITIALIZED 0x40000000
118#define AF_SHUTDOWN 0x80000000
119
120#define IsStandalone() (!(g_dwFlags & AF_DAEMON))
121#define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
122
123
124//
125// Encryption usage policies
126//
127
128#define ENCRYPTION_DISABLED 0
129#define ENCRYPTION_ALLOWED 1
130#define ENCRYPTION_PREFERRED 2
131#define ENCRYPTION_REQUIRED 3
132
133
134//
135// DB-related constants
136//
137
138#define MAX_DB_LOGIN 64
139#define MAX_DB_PASSWORD 64
140#define MAX_DB_NAME 256
141
142
143//
144// DB events
145//
146
147#define DBEVENT_CONNECTION_LOST 0
148#define DBEVENT_CONNECTION_RESTORED 1
149#define DBEVENT_QUERY_FAILED 2
150
151
28f5b9a4
VK
152//
153// Database syntax codes
154//
155
156#define DB_SYNTAX_MYSQL 0
157#define DB_SYNTAX_PGSQL 1
158#define DB_SYNTAX_MSSQL 2
159#define DB_SYNTAX_ORACLE 3
160#define DB_SYNTAX_SQLITE 4
161#define DB_SYNTAX_UNKNOWN -1
162
163
5039dede
AK
164//
165// Database connection structure
166//
167
168struct db_handle_t
169{
170 DB_CONNECTION hConn;
171 MUTEX mutexTransLock; // Transaction lock
172 int nTransactionLevel;
173 TCHAR *pszServer;
174 TCHAR *pszLogin;
175 TCHAR *pszPassword;
176 TCHAR *pszDBName;
177};
178typedef db_handle_t * DB_HANDLE;
179
180
181//
182// Win32 service and syslog constants
183//
184
185#ifdef _WIN32
186
187#define CORE_SERVICE_NAME _T("NetXMSCore")
188#define CORE_EVENT_SOURCE _T("NetXMSCore")
189#define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
190
191#else
192
193#define NETXMSD_SYSLOG_NAME _T("netxmsd")
194
195#endif /* _WIN32 */
196
197
198//
199// Single ARP cache entry
200//
201
202typedef struct
203{
204 DWORD dwIndex; // Interface index
205 DWORD dwIpAddr;
206 BYTE bMacAddr[MAC_ADDR_LENGTH];
207} ARP_ENTRY;
208
209
210//
211// ARP cache structure used by discovery functions and AgentConnection class
212//
213
214typedef struct
215{
216 DWORD dwNumEntries;
217 ARP_ENTRY *pEntries;
218} ARP_CACHE;
219
220
221//
222// Interface information structure used by discovery functions and AgentConnection class
223//
224
225typedef struct
226{
227 TCHAR szName[MAX_DB_STRING];
228 DWORD dwIndex;
229 DWORD dwType;
230 DWORD dwIpAddr;
231 DWORD dwIpNetMask;
232 BYTE bMacAddr[MAC_ADDR_LENGTH];
233 int iNumSecondary; // Number of secondary IP's on this interface
234} INTERFACE_INFO;
235
236
237//
238// Interface list used by discovery functions and AgentConnection class
239//
240
241typedef struct
242{
243 int iNumEntries; // Number of entries in pInterfaces
244 int iEnumPos; // Used by index enumeration handler
245 void *pArg; // Can be used by custom enumeration handlers
246 INTERFACE_INFO *pInterfaces; // Interface entries
247} INTERFACE_LIST;
248
249
250//
251// Route information
252//
253
254typedef struct
255{
256 DWORD dwDestAddr;
257 DWORD dwDestMask;
258 DWORD dwNextHop;
259 DWORD dwIfIndex;
260 DWORD dwRouteType;
261} ROUTE;
262
263
264//
265// Routing table
266//
267
268typedef struct
269{
270 int iNumEntries; // Number of entries
271 ROUTE *pRoutes; // Route list
272} ROUTING_TABLE;
273
274
275//
276// Agent connection
277//
278
279class LIBNXSRV_EXPORTABLE AgentConnection
280{
281private:
282 DWORD m_dwAddr;
283 int m_nProtocolVersion;
284 int m_iAuthMethod;
285 TCHAR m_szSecret[MAX_SECRET_LENGTH];
286 time_t m_tLastCommandTime;
287 SOCKET m_hSocket;
288 DWORD m_dwNumDataLines;
289 DWORD m_dwRequestId;
290 DWORD m_dwCommandTimeout;
291 DWORD m_dwRecvTimeout;
292 TCHAR **m_ppDataLines;
293 MsgWaitQueue *m_pMsgWaitQueue;
294 BOOL m_bIsConnected;
295 MUTEX m_mutexDataLock;
296 THREAD m_hReceiverThread;
297 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
298 int m_iEncryptionPolicy;
299 BOOL m_bUseProxy;
300 DWORD m_dwProxyAddr;
301 WORD m_wPort;
302 WORD m_wProxyPort;
303 int m_iProxyAuth;
304 TCHAR m_szProxySecret[MAX_SECRET_LENGTH];
305 int m_hCurrFile;
306 DWORD m_dwUploadRequestId;
307 CONDITION m_condFileUpload;
308 BOOL m_fileUploadSucceeded;
309
310 void ReceiverThread(void);
311 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
312
313protected:
314 void DestroyResultData(void);
315 BOOL SendMessage(CSCPMessage *pMsg);
316 CSCPMessage *WaitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
317 DWORD WaitForRCC(DWORD dwRqId, DWORD dwTimeOut);
318 DWORD SetupEncryption(RSA *pServerKey);
319 DWORD Authenticate(BOOL bProxyData);
320 DWORD SetupProxyConnection(void);
321 DWORD GetIpAddr(void) { return ntohl(m_dwAddr); }
322 DWORD PrepareFileUpload(const TCHAR *fileName, DWORD rqId);
323
324 virtual void PrintMsg(const TCHAR *pszFormat, ...);
325 virtual void OnTrap(CSCPMessage *pMsg);
326 virtual void OnFileUpload(BOOL success);
327
328 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
329 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
330
331public:
332 AgentConnection();
333 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
334 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
335 virtual ~AgentConnection();
336
337 BOOL Connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL);
338 void Disconnect(void);
339 BOOL IsConnected(void) { return m_bIsConnected; }
340 int GetProtocolVersion(void) { return m_nProtocolVersion; }
341
342 ARP_CACHE *GetArpCache(void);
343 INTERFACE_LIST *GetInterfaceList(void);
344 ROUTING_TABLE *GetRoutingTable(void);
345 DWORD GetParameter(const TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
346 DWORD GetList(const TCHAR *pszParam);
347 DWORD Nop(void);
348 DWORD ExecAction(const TCHAR *pszAction, int argc, TCHAR **argv);
349 DWORD UploadFile(const TCHAR *pszFile);
350 DWORD StartUpgrade(const TCHAR *pszPkgName);
351 DWORD CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
352 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL);
353 DWORD GetSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
354 DWORD GetConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
355 DWORD UpdateConfigFile(const TCHAR *pszConfig);
356 DWORD EnableTraps(void);
357 CSCPMessage *CustomRequest(CSCPMessage *pRequest, const TCHAR *recvFile = NULL);
358
359 DWORD GetNumDataLines(void) { return m_dwNumDataLines; }
360 const TCHAR *GetDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
361
362 void SetCommandTimeout(DWORD dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
363 void SetRecvTimeout(DWORD dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
364 void SetEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
365 void SetProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
366 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
367 void SetPort(WORD wPort) { m_wPort = wPort; }
a23d8e0d 368 void SetAuthData(int nMethod, const char *pszSecret) { m_iAuthMethod = nMethod; nx_strncpy(m_szSecret, pszSecret, MAX_SECRET_LENGTH); }
5039dede
AK
369};
370
371
372//
373// Proxy SNMP transport
374//
375
376class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
377{
378protected:
379 AgentConnection *m_pAgentConnection;
380 CSCPMessage *m_pResponse;
381 DWORD m_dwIpAddr;
382 WORD m_wPort;
383
384public:
385 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
386 virtual ~SNMP_ProxyTransport();
387
388 virtual int Read(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
389 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL);
390 virtual int Send(SNMP_PDU *pPDU);
391};
392
393
394//
395// ISC flags
396//
397
398#define ISCF_IS_CONNECTED ((DWORD)0x00000001)
399#define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
400
401
402//
403// Inter-server connection (ISC)
404//
405
406class LIBNXSRV_EXPORTABLE ISC
407{
408private:
409 DWORD m_flags;
410 DWORD m_addr;
411 WORD m_port;
412 SOCKET m_socket;
413 int m_protocolVersion;
414 DWORD m_requestId;
415 DWORD m_recvTimeout;
416 MsgWaitQueue *m_msgWaitQueue;
417 MUTEX m_mutexDataLock;
418 THREAD m_hReceiverThread;
419 CSCP_ENCRYPTION_CONTEXT *m_ctx;
420 DWORD m_commandTimeout;
421
422 void ReceiverThread(void);
423 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
424
425protected:
426 void DestroyResultData(void);
427 DWORD SetupEncryption(RSA *pServerKey);
428 DWORD ConnectToService(DWORD service);
429
430 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
431 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
432
433 virtual void PrintMsg(const TCHAR *format, ...);
434
435public:
436 ISC();
437 ISC(DWORD addr, WORD port = NETXMS_ISC_PORT);
438 virtual ~ISC();
439
440 DWORD Connect(DWORD service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
441 void Disconnect();
442
443 BOOL SendMessage(CSCPMessage *msg);
444 CSCPMessage *WaitForMessage(WORD code, DWORD id, DWORD timeOut) { return m_msgWaitQueue->WaitForMessage(code, id, timeOut); }
445 DWORD WaitForRCC(DWORD rqId, DWORD timeOut);
446
447 DWORD Nop(void);
448};
449
450
451//
452// Functions
453//
454
455void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
456void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
457void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
458void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
459const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
460
461void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...);
462void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...);
463
464BOOL LIBNXSRV_EXPORTABLE DBInit(BOOL bWriteLog, BOOL bLogErrors, BOOL bDumpSQL,
465 void (* fpEventHandler)(DWORD, TCHAR *));
466DB_HANDLE LIBNXSRV_EXPORTABLE DBConnect(void);
467DB_HANDLE LIBNXSRV_EXPORTABLE DBConnectEx(const TCHAR *pszServer, const TCHAR *pszDBName,
468 const TCHAR *pszLogin, const TCHAR *pszPassword);
469void LIBNXSRV_EXPORTABLE DBDisconnect(DB_HANDLE hConn);
470BOOL LIBNXSRV_EXPORTABLE DBQuery(DB_HANDLE hConn, const TCHAR *szQuery);
471BOOL LIBNXSRV_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
472DB_RESULT LIBNXSRV_EXPORTABLE DBSelect(DB_HANDLE hConn, const TCHAR *szQuery);
473DB_RESULT LIBNXSRV_EXPORTABLE DBSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
474DB_ASYNC_RESULT LIBNXSRV_EXPORTABLE DBAsyncSelect(DB_HANDLE hConn, const TCHAR *szQuery);
4c4c9b03 475DB_ASYNC_RESULT LIBNXSRV_EXPORTABLE DBAsyncSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
5039dede 476BOOL LIBNXSRV_EXPORTABLE DBFetch(DB_ASYNC_RESULT hResult);
ea6f474a
VK
477int LIBNXSRV_EXPORTABLE DBGetColumnCount(DB_RESULT hResult);
478BOOL LIBNXSRV_EXPORTABLE DBGetColumnName(DB_RESULT hResult, int column, TCHAR *buffer, int bufSize);
479int LIBNXSRV_EXPORTABLE DBGetColumnCountAsync(DB_ASYNC_RESULT hResult);
480BOOL LIBNXSRV_EXPORTABLE DBGetColumnNameAsync(DB_ASYNC_RESULT hResult, int column, TCHAR *buffer, int bufSize);
5039dede
AK
481TCHAR LIBNXSRV_EXPORTABLE *DBGetField(DB_RESULT hResult, int iRow, int iColumn,
482 TCHAR *pszBuffer, int nBufLen);
483LONG LIBNXSRV_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
484DWORD LIBNXSRV_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
485INT64 LIBNXSRV_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
486QWORD LIBNXSRV_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
487double LIBNXSRV_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
488DWORD LIBNXSRV_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
489BOOL LIBNXSRV_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
490 int *pnArray, int nSize, int nDefault);
491BOOL LIBNXSRV_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow,
492 int iColumn, uuid_t guid);
493TCHAR LIBNXSRV_EXPORTABLE *DBGetFieldAsync(DB_ASYNC_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize);
494LONG LIBNXSRV_EXPORTABLE DBGetFieldAsyncLong(DB_RESULT hResult, int iColumn);
495DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncULong(DB_ASYNC_RESULT hResult, int iColumn);
496INT64 LIBNXSRV_EXPORTABLE DBGetFieldAsyncInt64(DB_RESULT hResult, int iColumn);
497QWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncUInt64(DB_ASYNC_RESULT hResult, int iColumn);
498double LIBNXSRV_EXPORTABLE DBGetFieldAsyncDouble(DB_RESULT hResult, int iColumn);
499DWORD LIBNXSRV_EXPORTABLE DBGetFieldAsyncIPAddr(DB_RESULT hResult, int iColumn);
500int LIBNXSRV_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
501void LIBNXSRV_EXPORTABLE DBFreeResult(DB_RESULT hResult);
502void LIBNXSRV_EXPORTABLE DBFreeAsyncResult(DB_HANDLE hConn, DB_ASYNC_RESULT hResult);
503BOOL LIBNXSRV_EXPORTABLE DBBegin(DB_HANDLE hConn);
504BOOL LIBNXSRV_EXPORTABLE DBCommit(DB_HANDLE hConn);
505BOOL LIBNXSRV_EXPORTABLE DBRollback(DB_HANDLE hConn);
506void LIBNXSRV_EXPORTABLE DBUnloadDriver(void);
507
28f5b9a4
VK
508int LIBNXSRV_EXPORTABLE DBGetSchemaVersion(DB_HANDLE conn);
509int LIBNXSRV_EXPORTABLE DBGetSyntax(DB_HANDLE conn);
510
5039dede
AK
511TCHAR LIBNXSRV_EXPORTABLE *EncodeSQLString(const TCHAR *pszIn);
512void LIBNXSRV_EXPORTABLE DecodeSQLString(TCHAR *pszStr);
513
514void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
515
516const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(DWORD code);
517
518
519//
520// Variables
521//
522
523extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
524extern int LIBNXSRV_EXPORTABLE g_nDebugLevel;
525extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDriver[];
526extern TCHAR LIBNXSRV_EXPORTABLE g_szDbDrvParams[];
527extern TCHAR LIBNXSRV_EXPORTABLE g_szDbServer[];
528extern TCHAR LIBNXSRV_EXPORTABLE g_szDbLogin[];
529extern TCHAR LIBNXSRV_EXPORTABLE g_szDbPassword[];
530extern TCHAR LIBNXSRV_EXPORTABLE g_szDbName[];
531
532#endif /* _nxsrvapi_h_ */