license for libnxdb and libnxsrv changed to LGPL
[public/netxms.git] / src / server / include / nxsrvapi.h
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
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
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 Lesser 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>
43 #include "../libnxsrv/messages.h"
44 #include <nxsnmp.h>
45 #include <netxms_isc.h>
46
47
48 //
49 // Default files
50 //
51
52 #ifdef _WIN32
53
54 # define DEFAULT_CONFIG_FILE _T("C:\\netxmsd.conf")
55
56 # define DEFAULT_SHELL "cmd.exe"
57 # define DEFAULT_LOG_FILE "C:\\NetXMS.log"
58 # define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
59 # define DEFAULT_DUMP_DIR "C:\\"
60
61 # define DDIR_MIBS "\\mibs"
62 # define DDIR_PACKAGES "\\packages"
63 # define DDIR_BACKGROUNDS "\\backgrounds"
64 # define DDIR_SHARED_FILES "\\shared"
65 # define DFILE_KEYS "\\server_key"
66 # define DFILE_COMPILED_MIB "\\mibs\\netxms.mib"
67
68 #else /* _WIN32 */
69
70 # define DEFAULT_CONFIG_FILE _T("{search}")
71
72 # define DEFAULT_SHELL "/bin/sh"
73
74 # ifndef DATADIR
75 # define DATADIR "/var/netxms"
76 # endif
77
78 # define DEFAULT_LOG_FILE DATADIR"/log/netxmsd.log"
79 # define DEFAULT_DATA_DIR DATADIR
80 # define DEFAULT_DUMP_DIR "/"
81
82 # define DDIR_MIBS "/mibs"
83 # define DDIR_PACKAGES "/packages"
84 # define DDIR_BACKGROUNDS "/backgrounds"
85 # define DDIR_SHARED_FILES "/shared"
86 # define DFILE_KEYS "/.server_key"
87 # define DFILE_COMPILED_MIB "/mibs/netxms.mib"
88
89 #endif /* _WIN32 */
90
91
92 //
93 // Application flags
94 //
95
96 #define AF_DAEMON 0x00000001
97 #define AF_USE_SYSLOG 0x00000002
98 #define AF_ENABLE_NETWORK_DISCOVERY 0x00000004
99 #define AF_ACTIVE_NETWORK_DISCOVERY 0x00000008
100 #define AF_LOG_SQL_ERRORS 0x00000010
101 #define AF_DELETE_EMPTY_SUBNETS 0x00000020
102 #define AF_ENABLE_SNMP_TRAPD 0x00000040
103 #define AF_ENABLE_ZONING 0x00000080
104 #define AF_SYNC_NODE_NAMES_WITH_DNS 0x00000100
105 #define AF_CHECK_TRUSTED_NODES 0x00000200
106 #define AF_WRITE_FULL_DUMP 0x00080000
107 #define AF_RESOLVE_NODE_NAMES 0x00100000
108 #define AF_CATCH_EXCEPTIONS 0x00200000
109 #define AF_INTERNAL_CA 0x00400000
110 #define AF_DB_LOCKED 0x01000000
111 #define AF_ENABLE_MULTIPLE_DB_CONN 0x02000000
112 #define AF_DB_CONNECTION_LOST 0x04000000
113 #define AF_NO_NETWORK_CONNECTIVITY 0x08000000
114 #define AF_EVENT_STORM_DETECTED 0x08000000
115 #define AF_SERVER_INITIALIZED 0x40000000
116 #define AF_SHUTDOWN 0x80000000
117
118 #define IsStandalone() (!(g_dwFlags & AF_DAEMON))
119 #define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
120
121
122 //
123 // Encryption usage policies
124 //
125
126 #define ENCRYPTION_DISABLED 0
127 #define ENCRYPTION_ALLOWED 1
128 #define ENCRYPTION_PREFERRED 2
129 #define ENCRYPTION_REQUIRED 3
130
131
132 //
133 // Win32 service and syslog constants
134 //
135
136 #ifdef _WIN32
137
138 #define CORE_SERVICE_NAME _T("NetXMSCore")
139 #define CORE_EVENT_SOURCE _T("NetXMSCore")
140 #define NETXMSD_SYSLOG_NAME CORE_EVENT_SOURCE
141
142 #else
143
144 #define NETXMSD_SYSLOG_NAME _T("netxmsd")
145
146 #endif /* _WIN32 */
147
148
149 //
150 // Single ARP cache entry
151 //
152
153 typedef struct
154 {
155 DWORD dwIndex; // Interface index
156 DWORD dwIpAddr;
157 BYTE bMacAddr[MAC_ADDR_LENGTH];
158 } ARP_ENTRY;
159
160
161 //
162 // ARP cache structure used by discovery functions and AgentConnection class
163 //
164
165 typedef struct
166 {
167 DWORD dwNumEntries;
168 ARP_ENTRY *pEntries;
169 } ARP_CACHE;
170
171
172 //
173 // Interface information structure used by discovery functions and AgentConnection class
174 //
175
176 typedef struct
177 {
178 TCHAR szName[MAX_DB_STRING];
179 DWORD dwIndex;
180 DWORD dwType;
181 DWORD dwIpAddr;
182 DWORD dwIpNetMask;
183 BYTE bMacAddr[MAC_ADDR_LENGTH];
184 int iNumSecondary; // Number of secondary IP's on this interface
185 } INTERFACE_INFO;
186
187
188 //
189 // Interface list used by discovery functions and AgentConnection class
190 //
191
192 typedef struct
193 {
194 int iNumEntries; // Number of entries in pInterfaces
195 int iEnumPos; // Used by index enumeration handler
196 void *pArg; // Can be used by custom enumeration handlers
197 INTERFACE_INFO *pInterfaces; // Interface entries
198 } INTERFACE_LIST;
199
200
201 //
202 // Route information
203 //
204
205 typedef struct
206 {
207 DWORD dwDestAddr;
208 DWORD dwDestMask;
209 DWORD dwNextHop;
210 DWORD dwIfIndex;
211 DWORD dwRouteType;
212 } ROUTE;
213
214
215 //
216 // Routing table
217 //
218
219 typedef struct
220 {
221 int iNumEntries; // Number of entries
222 ROUTE *pRoutes; // Route list
223 } ROUTING_TABLE;
224
225
226 //
227 // Agent connection
228 //
229
230 class LIBNXSRV_EXPORTABLE AgentConnection
231 {
232 private:
233 DWORD m_dwAddr;
234 int m_nProtocolVersion;
235 int m_iAuthMethod;
236 TCHAR m_szSecret[MAX_SECRET_LENGTH];
237 time_t m_tLastCommandTime;
238 SOCKET m_hSocket;
239 DWORD m_dwNumDataLines;
240 DWORD m_dwRequestId;
241 DWORD m_dwCommandTimeout;
242 DWORD m_connectionTimeout;
243 DWORD m_dwRecvTimeout;
244 TCHAR **m_ppDataLines;
245 MsgWaitQueue *m_pMsgWaitQueue;
246 BOOL m_bIsConnected;
247 MUTEX m_mutexDataLock;
248 THREAD m_hReceiverThread;
249 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
250 int m_iEncryptionPolicy;
251 BOOL m_bUseProxy;
252 DWORD m_dwProxyAddr;
253 WORD m_wPort;
254 WORD m_wProxyPort;
255 int m_iProxyAuth;
256 TCHAR m_szProxySecret[MAX_SECRET_LENGTH];
257 int m_hCurrFile;
258 TCHAR m_currentFileName[MAX_PATH];
259 DWORD m_dwDownloadRequestId;
260 CONDITION m_condFileDownload;
261 BOOL m_fileDownloadSucceeded;
262 void (*m_downloadProgressCallback)(size_t, void *);
263 void *m_downloadProgressCallbackArg;
264 bool m_deleteFileOnDownloadFailure;
265 bool m_fileUploadInProgress;
266
267 void ReceiverThread(void);
268 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
269
270 protected:
271 void destroyResultData();
272 BOOL sendMessage(CSCPMessage *pMsg);
273 CSCPMessage *waitForMessage(WORD wCode, DWORD dwId, DWORD dwTimeOut) { return m_pMsgWaitQueue->WaitForMessage(wCode, dwId, dwTimeOut); }
274 DWORD waitForRCC(DWORD dwRqId, DWORD dwTimeOut);
275 DWORD setupEncryption(RSA *pServerKey);
276 DWORD authenticate(BOOL bProxyData);
277 DWORD setupProxyConnection();
278 DWORD getIpAddr() { return ntohl(m_dwAddr); }
279 DWORD prepareFileDownload(const TCHAR *fileName, DWORD rqId, bool append, void (*downloadProgressCallback)(size_t, void *), void *cbArg);
280
281 virtual void PrintMsg(const TCHAR *pszFormat, ...);
282 virtual void onTrap(CSCPMessage *pMsg);
283 virtual void onDataPush(CSCPMessage *msg);
284 virtual void onFileDownload(BOOL success);
285
286 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
287 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
288
289 public:
290 AgentConnection(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
291 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
292 virtual ~AgentConnection();
293
294 BOOL connect(RSA *pServerKey = NULL, BOOL bVerbose = FALSE, DWORD *pdwError = NULL);
295 void disconnect();
296 BOOL isConnected() { return m_bIsConnected; }
297 int getProtocolVersion() { return m_nProtocolVersion; }
298
299 SOCKET getSocket() { return m_hSocket; }
300
301 ARP_CACHE *GetArpCache(void);
302 INTERFACE_LIST *GetInterfaceList();
303 ROUTING_TABLE *GetRoutingTable();
304 DWORD GetParameter(const TCHAR *pszParam, DWORD dwBufSize, TCHAR *pszBuffer);
305 DWORD GetList(const TCHAR *pszParam);
306 DWORD nop();
307 DWORD ExecAction(const TCHAR *pszAction, int argc, TCHAR **argv);
308 DWORD UploadFile(const TCHAR *pszFile, void (* progressCallback)(INT64, void *) = NULL, void *cbArg = NULL);
309 DWORD StartUpgrade(const TCHAR *pszPkgName);
310 DWORD CheckNetworkService(DWORD *pdwStatus, DWORD dwIpAddr, int iServiceType, WORD wPort = 0,
311 WORD wProto = 0, const TCHAR *pszRequest = NULL, const TCHAR *pszResponse = NULL);
312 DWORD GetSupportedParameters(DWORD *pdwNumParams, NXC_AGENT_PARAM **ppParamList);
313 DWORD GetConfigFile(TCHAR **ppszConfig, DWORD *pdwSize);
314 DWORD updateConfigFile(const TCHAR *pszConfig);
315 DWORD enableTraps();
316
317 DWORD generateRequestId() { return m_dwRequestId++; }
318 CSCPMessage *customRequest(CSCPMessage *pRequest, const TCHAR *recvFile = NULL, bool appendFile = false,
319 void (*downloadProgressCallback)(size_t, void *) = NULL, void *cbArg = NULL);
320
321 DWORD getNumDataLines() { return m_dwNumDataLines; }
322 const TCHAR *getDataLine(DWORD dwIndex) { return dwIndex < m_dwNumDataLines ? m_ppDataLines[dwIndex] : _T("(error)"); }
323
324 void setConnectionTimeout(DWORD dwTimeout) { m_connectionTimeout = max(dwTimeout, 1000); }
325 DWORD getConnectionTimeout() { return m_connectionTimeout; }
326 void setCommandTimeout(DWORD dwTimeout) { m_dwCommandTimeout = max(dwTimeout, 500); }
327 DWORD getCommandTimeout() { return m_dwCommandTimeout; }
328 void setRecvTimeout(DWORD dwTimeout) { m_dwRecvTimeout = max(dwTimeout, 10000); }
329 void setEncryptionPolicy(int iPolicy) { m_iEncryptionPolicy = iPolicy; }
330 void setProxy(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
331 int iAuthMethod = AUTH_NONE, const TCHAR *pszSecret = NULL);
332 void setPort(WORD wPort) { m_wPort = wPort; }
333 void setAuthData(int nMethod, const char *pszSecret) { m_iAuthMethod = nMethod; nx_strncpy(m_szSecret, pszSecret, MAX_SECRET_LENGTH); }
334 void setDeleteFileOnDownloadFailure(bool flag) { m_deleteFileOnDownloadFailure = flag; }
335 };
336
337
338 //
339 // Proxy SNMP transport
340 //
341
342 class LIBNXSRV_EXPORTABLE SNMP_ProxyTransport : public SNMP_Transport
343 {
344 protected:
345 AgentConnection *m_pAgentConnection;
346 CSCPMessage *m_pResponse;
347 DWORD m_dwIpAddr;
348 WORD m_wPort;
349
350 public:
351 SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort);
352 virtual ~SNMP_ProxyTransport();
353
354 virtual int readMessage(SNMP_PDU **ppData, DWORD dwTimeout = INFINITE,
355 struct sockaddr *pSender = NULL, socklen_t *piAddrSize = NULL,
356 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t) = NULL);
357 virtual int sendMessage(SNMP_PDU *pdu);
358 };
359
360
361 //
362 // ISC flags
363 //
364
365 #define ISCF_IS_CONNECTED ((DWORD)0x00000001)
366 #define ISCF_REQUIRE_ENCRYPTION ((DWORD)0x00000002)
367
368
369 //
370 // Inter-server connection (ISC)
371 //
372
373 class LIBNXSRV_EXPORTABLE ISC
374 {
375 private:
376 DWORD m_flags;
377 DWORD m_addr;
378 WORD m_port;
379 SOCKET m_socket;
380 int m_protocolVersion;
381 DWORD m_requestId;
382 DWORD m_recvTimeout;
383 MsgWaitQueue *m_msgWaitQueue;
384 MUTEX m_mutexDataLock;
385 THREAD m_hReceiverThread;
386 CSCP_ENCRYPTION_CONTEXT *m_ctx;
387 DWORD m_commandTimeout;
388
389 void ReceiverThread(void);
390 static THREAD_RESULT THREAD_CALL ReceiverThreadStarter(void *);
391
392 protected:
393 void DestroyResultData(void);
394 DWORD SetupEncryption(RSA *pServerKey);
395 DWORD ConnectToService(DWORD service);
396
397 void Lock(void) { MutexLock(m_mutexDataLock, INFINITE); }
398 void Unlock(void) { MutexUnlock(m_mutexDataLock); }
399
400 virtual void PrintMsg(const TCHAR *format, ...);
401
402 public:
403 ISC();
404 ISC(DWORD addr, WORD port = NETXMS_ISC_PORT);
405 virtual ~ISC();
406
407 DWORD Connect(DWORD service, RSA *serverKey = NULL, BOOL requireEncryption = FALSE);
408 void Disconnect();
409
410 BOOL SendMessage(CSCPMessage *msg);
411 CSCPMessage *WaitForMessage(WORD code, DWORD id, DWORD timeOut) { return m_msgWaitQueue->WaitForMessage(code, id, timeOut); }
412 DWORD WaitForRCC(DWORD rqId, DWORD timeOut);
413
414 DWORD Nop(void);
415 };
416
417
418 //
419 // Functions
420 //
421
422 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache);
423 void LIBNXSRV_EXPORTABLE DestroyInterfaceList(INTERFACE_LIST *pIfList);
424 void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT);
425 void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT);
426 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(int iError);
427
428 void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...);
429 void LIBNXSRV_EXPORTABLE DbgPrintf(int level, const TCHAR *format, ...);
430 void LIBNXSRV_EXPORTABLE DbgPrintf2(int level, const TCHAR *format, va_list args);
431
432 void LIBNXSRV_EXPORTABLE SetAgentDEP(int iPolicy);
433
434 const TCHAR LIBNXSRV_EXPORTABLE *ISCErrorCodeToText(DWORD code);
435
436
437 //
438 // Variables
439 //
440
441 extern DWORD LIBNXSRV_EXPORTABLE g_dwFlags;
442 extern int LIBNXSRV_EXPORTABLE g_nDebugLevel;
443
444 #endif /* _nxsrvapi_h_ */