server main thread pool handle made exportable
[public/netxms.git] / src / server / include / nms_core.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2016 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nms_core.h
20 **
21 **/
22
23 #ifndef _nms_core_h_
24 #define _nms_core_h_
25
26 #ifdef _WIN32
27 #ifdef NXCORE_EXPORTS
28 #define NXCORE_EXPORTABLE __declspec(dllexport)
29 #else
30 #define NXCORE_EXPORTABLE __declspec(dllimport)
31 #endif
32 #else /* _WIN32 */
33 #define NXCORE_EXPORTABLE
34 #endif
35
36 #define LIBNXCL_NO_DECLARATIONS 1
37
38 #include <nms_common.h>
39
40 #ifndef _WIN32
41
42 #if HAVE_SYS_TYPES_H
43 # include <sys/types.h>
44 #endif
45
46 #if HAVE_DLFCN_H
47 #include <dlfcn.h>
48 #endif
49
50
51 #define WSAGetLastError() (errno)
52
53 #endif /* _WIN32 */
54
55 #include <time.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdarg.h>
59
60 #ifdef _WITH_ENCRYPTION
61 #include <openssl/ssl.h>
62 #endif
63
64 //
65 // Common includes
66 //
67
68 #include <nms_util.h>
69 #include <dbdrv.h>
70 #include <nms_cscp.h>
71 #include <uuid.h>
72 #include <nxsrvapi.h>
73 #include <nxqueue.h>
74 #include <nxsnmp.h>
75 #include <nxmodule.h>
76 #include <nxsl.h>
77 #include <nxdbapi.h>
78 #include <nddrv.h>
79 #include <nxcore_smclp.h>
80
81 /**
82 * Console context
83 */
84 struct __console_ctx
85 {
86 SOCKET hSocket;
87 MUTEX socketMutex;
88 NXCPMessage *pMsg;
89 ClientSession *session;
90 String *output;
91 };
92
93 typedef __console_ctx * CONSOLE_CTX;
94
95 /**
96 * Server includes
97 */
98 #include "nms_dcoll.h"
99 #include "nms_users.h"
100 #include "nxcore_winperf.h"
101 #include "nms_objects.h"
102 #include "nms_locks.h"
103 #include "nms_pkg.h"
104 #include "nms_topo.h"
105 #include "nms_script.h"
106 #include "nxcore_situations.h"
107 #include "nxcore_jobs.h"
108 #include "nxcore_logs.h"
109 #include "nxcore_schedule.h"
110 #ifdef WITH_ZMQ
111 #include "zeromq.h"
112 #endif
113
114 /**
115 * Common constants and macros
116 */
117 #define MAX_LINE_SIZE 4096
118 #define GROUP_FLAG_BIT ((UINT32)0x80000000)
119 #define CHECKPOINT_SNMP_PORT 260
120 #define DEFAULT_AFFINITY_MASK 0xFFFFFFFF
121
122 #define MAX_CLIENT_SESSIONS 128
123 #define MAX_DEVICE_SESSIONS 256
124
125 #define PING_TIME_TIMEOUT 10000
126
127 typedef void * HSNMPSESSION;
128
129 /**
130 * Prefixes for poller messages
131 */
132 #define POLLER_ERROR _T("\x7F") _T("e")
133 #define POLLER_WARNING _T("\x7Fw")
134 #define POLLER_INFO _T("\x7Fi")
135
136 /**
137 * Unique identifier group codes
138 */
139 #define IDG_NETWORK_OBJECT 0
140 #define IDG_EVENT 1
141 #define IDG_ITEM 2
142 #define IDG_SNMP_TRAP 3
143 #define IDG_JOB 4
144 #define IDG_ACTION 5
145 #define IDG_EVENT_GROUP 6
146 #define IDG_THRESHOLD 7
147 #define IDG_USER 8
148 #define IDG_USER_GROUP 9
149 #define IDG_ALARM 10
150 #define IDG_ALARM_NOTE 11
151 #define IDG_PACKAGE 12
152 #define IDG_SLM_TICKET 13
153 #define IDG_OBJECT_TOOL 14
154 #define IDG_SCRIPT 15
155 #define IDG_AGENT_CONFIG 16
156 #define IDG_GRAPH 17
157 #define IDG_CERTIFICATE 18
158 #define IDG_SITUATION 19
159 #define IDG_DCT_COLUMN 20
160 #define IDG_MAPPING_TABLE 21
161 #define IDG_DCI_SUMMARY_TABLE 22
162 #define IDG_SCHEDULED_TASK 23
163
164 /**
165 * Exit codes for console commands
166 */
167 #define CMD_EXIT_CONTINUE 0
168 #define CMD_EXIT_CLOSE_SESSION 1
169 #define CMD_EXIT_SHUTDOWN 2
170
171 /**
172 * Network discovery mode
173 */
174 #define DISCOVERY_DISABLED 0
175 #define DISCOVERY_PASSIVE_ONLY 1
176 #define DISCOVERY_ACTIVE 2
177
178 /**
179 * Client session flags
180 */
181 #define CSF_TERMINATED ((UINT32)0x00000001)
182 #define CSF_EPP_LOCKED ((UINT32)0x00000002)
183 #define CSF_PACKAGE_DB_LOCKED ((UINT32)0x00000004)
184 #define CSF_USER_DB_LOCKED ((UINT32)0x00000008)
185 #define CSF_EPP_UPLOAD ((UINT32)0x00000010)
186 #define CSF_CONSOLE_OPEN ((UINT32)0x00000020)
187 #define CSF_AUTHENTICATED ((UINT32)0x00000080)
188 #define CSF_RECEIVING_MAP_DATA ((UINT32)0x00000200)
189 #define CSF_SYNC_OBJECT_COMMENTS ((UINT32)0x00000400)
190 #define CSF_CUSTOM_LOCK_1 ((UINT32)0x01000000)
191 #define CSF_CUSTOM_LOCK_2 ((UINT32)0x02000000)
192 #define CSF_CUSTOM_LOCK_3 ((UINT32)0x04000000)
193 #define CSF_CUSTOM_LOCK_4 ((UINT32)0x08000000)
194 #define CSF_CUSTOM_LOCK_5 ((UINT32)0x10000000)
195 #define CSF_CUSTOM_LOCK_6 ((UINT32)0x20000000)
196 #define CSF_CUSTOM_LOCK_7 ((UINT32)0x40000000)
197 #define CSF_CUSTOM_LOCK_8 ((UINT32)0x80000000)
198
199 /**
200 * Client session states
201 */
202 #define SESSION_STATE_INIT 0
203 #define SESSION_STATE_IDLE 1
204 #define SESSION_STATE_PROCESSING 2
205
206 /**
207 * Information categories for UPDATE_INFO structure
208 */
209 #define INFO_CAT_EVENT 1
210 #define INFO_CAT_OBJECT_CHANGE 2
211 #define INFO_CAT_ALARM 3
212 #define INFO_CAT_ACTION 4
213 #define INFO_CAT_SYSLOG_MSG 5
214 #define INFO_CAT_SNMP_TRAP 6
215 #define INFO_CAT_AUDIT_RECORD 7
216 #define INFO_CAT_SITUATION 8
217 #define INFO_CAT_LIBRARY_IMAGE 9
218
219 /**
220 * Certificate types
221 */
222 #define CERT_TYPE_TRUSTED_CA 0
223 #define CERT_TYPE_USER 1
224
225 /**
226 * Audit subsystems
227 */
228 #define AUDIT_SECURITY _T("SECURITY")
229 #define AUDIT_OBJECTS _T("OBJECTS")
230 #define AUDIT_SYSCFG _T("SYSCFG")
231 #define AUDIT_CONSOLE _T("CONSOLE")
232
233 #define AUDIT_SYSTEM_SID (-1)
234
235 /**
236 * Event handling subsystem definitions
237 */
238 #include "nms_events.h"
239 #include "nms_actions.h"
240 #include "nms_alarm.h"
241
242 /**
243 * New node information
244 */
245 typedef struct
246 {
247 InetAddress ipAddr;
248 UINT32 zoneId;
249 BOOL ignoreFilter;
250 BYTE bMacAddr[MAC_ADDR_LENGTH];
251 } NEW_NODE;
252
253 /**
254 * New node flags
255 */
256 #define NNF_IS_SNMP 0x0001
257 #define NNF_IS_AGENT 0x0002
258 #define NNF_IS_ROUTER 0x0004
259 #define NNF_IS_BRIDGE 0x0008
260 #define NNF_IS_PRINTER 0x0010
261 #define NNF_IS_CDP 0x0020
262 #define NNF_IS_SONMP 0x0040
263 #define NNF_IS_LLDP 0x0080
264
265 /**
266 * Address list element types
267 */
268 #define InetAddressListElement_SUBNET 0
269 #define InetAddressListElement_RANGE 1
270
271 /**
272 * IP address list element
273 */
274 class NXCORE_EXPORTABLE InetAddressListElement
275 {
276 private:
277 int m_type;
278 InetAddress m_baseAddress;
279 InetAddress m_endAddress;
280
281 public:
282 InetAddressListElement(NXCPMessage *msg, UINT32 baseId);
283 InetAddressListElement(const InetAddress& baseAddr, const InetAddress& endAddr);
284 InetAddressListElement(const InetAddress& baseAddr, int maskBits);
285 InetAddressListElement(DB_RESULT hResult, int row);
286
287 void fillMessage(NXCPMessage *msg, UINT32 baseId) const;
288
289 bool contains(const InetAddress& addr) const;
290
291 int getType() const { return m_type; }
292 const InetAddress& getBaseAddress() const { return m_baseAddress; }
293 const InetAddress& getEndAddress() const { return m_endAddress; }
294
295 String toString() const;
296 };
297
298 /**
299 * Node information for autodiscovery filter
300 */
301 typedef struct
302 {
303 InetAddress ipAddr;
304 UINT32 dwFlags;
305 int nSNMPVersion;
306 TCHAR szObjectId[MAX_OID_LEN * 4]; // SNMP OID
307 TCHAR szAgentVersion[MAX_AGENT_VERSION_LEN];
308 TCHAR szPlatform[MAX_PLATFORM_NAME_LEN];
309 } DISCOVERY_FILTER_DATA;
310
311 /**
312 * Data update structure for client sessions
313 */
314 typedef struct
315 {
316 UINT32 dwCategory; // Data category - event, network object, etc.
317 UINT32 dwCode; // Data-specific update code
318 void *pData; // Pointer to data block
319 } UPDATE_INFO;
320
321 /**
322 * Mobile device session
323 */
324 class NXCORE_EXPORTABLE MobileDeviceSession
325 {
326 private:
327 SOCKET m_hSocket;
328 Queue *m_pSendQueue;
329 Queue *m_pMessageQueue;
330 int m_id;
331 int m_state;
332 WORD m_wCurrentCmd;
333 UINT32 m_dwUserId;
334 UINT32 m_deviceObjectId;
335 NXCP_BUFFER *m_pMsgBuffer;
336 NXCPEncryptionContext *m_pCtx;
337 BYTE m_challenge[CLIENT_CHALLENGE_SIZE];
338 THREAD m_hWriteThread;
339 THREAD m_hProcessingThread;
340 MUTEX m_mutexSocketWrite;
341 struct sockaddr *m_clientAddr;
342 TCHAR m_szHostName[256]; // IP address of name of conneced host in textual form
343 TCHAR m_szUserName[MAX_SESSION_NAME]; // String in form login_name@host
344 TCHAR m_szClientInfo[96]; // Client app info string
345 UINT32 m_dwEncryptionRqId;
346 UINT32 m_dwEncryptionResult;
347 CONDITION m_condEncryptionSetup;
348 UINT32 m_dwRefCount;
349 bool m_isAuthenticated;
350
351 static THREAD_RESULT THREAD_CALL readThreadStarter(void *);
352 static THREAD_RESULT THREAD_CALL writeThreadStarter(void *);
353 static THREAD_RESULT THREAD_CALL processingThreadStarter(void *);
354
355 void readThread();
356 void writeThread();
357 void processingThread();
358
359 void setupEncryption(NXCPMessage *request);
360 void respondToKeepalive(UINT32 dwRqId);
361 void debugPrintf(int level, const TCHAR *format, ...);
362 void sendServerInfo(UINT32 dwRqId);
363 void login(NXCPMessage *pRequest);
364 void updateDeviceInfo(NXCPMessage *pRequest);
365 void updateDeviceStatus(NXCPMessage *pRequest);
366 void pushData(NXCPMessage *request);
367
368 public:
369 MobileDeviceSession(SOCKET hSocket, struct sockaddr *addr);
370 ~MobileDeviceSession();
371
372 void run();
373
374 void postMessage(NXCPMessage *pMsg) { m_pSendQueue->put(pMsg->createMessage()); }
375 void sendMessage(NXCPMessage *pMsg);
376
377 int getId() { return m_id; }
378 void setId(int id) { if (m_id == -1) m_id = id; }
379 int getState() { return m_state; }
380 const TCHAR *getUserName() { return m_szUserName; }
381 const TCHAR *getClientInfo() { return m_szClientInfo; }
382 const TCHAR *getHostName() { return m_szHostName; }
383 UINT32 getUserId() { return m_dwUserId; }
384 bool isAuthenticated() { return m_isAuthenticated; }
385 WORD getCurrentCmd() { return m_wCurrentCmd; }
386 int getCipher() { return (m_pCtx == NULL) ? -1 : m_pCtx->getCipher(); }
387 };
388
389 /**
390 * Processing thread starter declaration for client session
391 */
392 #define DECLARE_THREAD_STARTER(func) static void ThreadStarter_##func(void *);
393
394 /**
395 * Client (user) session
396 */
397 class NXCORE_EXPORTABLE ClientSession
398 {
399 private:
400 SOCKET m_hSocket;
401 Queue *m_pSendQueue;
402 Queue *m_pMessageQueue;
403 Queue *m_pUpdateQueue;
404 int m_id;
405 int m_state;
406 WORD m_wCurrentCmd;
407 UINT32 m_dwUserId;
408 UINT64 m_dwSystemAccess; // User's system access rights
409 UINT32 m_dwFlags; // Session flags
410 int m_clientType; // Client system type - desktop, web, mobile, etc.
411 NXCPEncryptionContext *m_pCtx;
412 BYTE m_challenge[CLIENT_CHALLENGE_SIZE];
413 THREAD m_hWriteThread;
414 THREAD m_hProcessingThread;
415 THREAD m_hUpdateThread;
416 MUTEX m_mutexSocketWrite;
417 MUTEX m_mutexSendEvents;
418 MUTEX m_mutexSendSyslog;
419 MUTEX m_mutexSendTrapLog;
420 MUTEX m_mutexSendObjects;
421 MUTEX m_mutexSendAlarms;
422 MUTEX m_mutexSendActions;
423 MUTEX m_mutexSendAuditLog;
424 MUTEX m_mutexSendSituations;
425 MUTEX m_mutexPollerInit;
426 struct sockaddr *m_clientAddr;
427 TCHAR m_workstation[256]; // IP address or name of connected host in textual form
428 TCHAR m_webServerAddress[256]; // IP address or name of web server for web sessions
429 TCHAR m_loginName[MAX_USER_NAME];
430 TCHAR m_sessionName[MAX_SESSION_NAME]; // String in form login_name@host
431 TCHAR m_clientInfo[96]; // Client app info string
432 TCHAR m_language[8]; // Client's desired language
433 time_t m_loginTime;
434 UINT32 m_dwOpenDCIListSize; // Number of open DCI lists
435 UINT32 *m_pOpenDCIList; // List of nodes with DCI lists open
436 UINT32 m_dwNumRecordsToUpload; // Number of records to be uploaded
437 UINT32 m_dwRecordsUploaded;
438 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
439 int m_hCurrFile;
440 UINT32 m_dwFileRqId;
441 UINT32 m_dwUploadCommand;
442 UINT32 m_dwUploadData;
443 uuid_t m_uploadImageGuid;
444 TCHAR m_szCurrFileName[MAX_PATH];
445 VolatileCounter m_refCount;
446 UINT32 m_dwEncryptionRqId;
447 UINT32 m_dwEncryptionResult;
448 CONDITION m_condEncryptionSetup;
449 CONSOLE_CTX m_console; // Server console context
450 StringList m_musicTypeList;
451 ObjectIndex m_agentConn;
452 StringObjectMap<UINT32> *m_subscriptions;
453 MUTEX m_subscriptionLock;
454
455 static THREAD_RESULT THREAD_CALL readThreadStarter(void *);
456 static THREAD_RESULT THREAD_CALL writeThreadStarter(void *);
457 static THREAD_RESULT THREAD_CALL processingThreadStarter(void *);
458 static THREAD_RESULT THREAD_CALL updateThreadStarter(void *);
459 static void pollerThreadStarter(void *);
460
461 DECLARE_THREAD_STARTER(cancelFileMonitoring)
462 DECLARE_THREAD_STARTER(clearDCIData)
463 DECLARE_THREAD_STARTER(createObject)
464 DECLARE_THREAD_STARTER(executeAction)
465 DECLARE_THREAD_STARTER(executeScript)
466 DECLARE_THREAD_STARTER(fileManagerControl)
467 DECLARE_THREAD_STARTER(findIpAddress)
468 DECLARE_THREAD_STARTER(findMacAddress)
469 DECLARE_THREAD_STARTER(findNodeConnection)
470 DECLARE_THREAD_STARTER(forceDCIPoll)
471 DECLARE_THREAD_STARTER(forwardToReportingServer)
472 DECLARE_THREAD_STARTER(getAgentFile)
473 DECLARE_THREAD_STARTER(getAlarmEvents)
474 DECLARE_THREAD_STARTER(getAlarms)
475 DECLARE_THREAD_STARTER(getCollectedData)
476 DECLARE_THREAD_STARTER(getLocationHistory)
477 DECLARE_THREAD_STARTER(getNetworkPath)
478 DECLARE_THREAD_STARTER(getRoutingTable)
479 DECLARE_THREAD_STARTER(getServerFile)
480 DECLARE_THREAD_STARTER(getServerLogQueryData)
481 DECLARE_THREAD_STARTER(getSwitchForwardingDatabase)
482 DECLARE_THREAD_STARTER(getTableCollectedData)
483 DECLARE_THREAD_STARTER(importConfiguration)
484 DECLARE_THREAD_STARTER(openHelpdeskIssue)
485 DECLARE_THREAD_STARTER(processConsoleCommand)
486 DECLARE_THREAD_STARTER(queryAgentTable)
487 DECLARE_THREAD_STARTER(queryL2Topology)
488 DECLARE_THREAD_STARTER(queryParameter)
489 DECLARE_THREAD_STARTER(queryServerLog)
490 DECLARE_THREAD_STARTER(sendEventLog)
491 DECLARE_THREAD_STARTER(sendMib)
492 DECLARE_THREAD_STARTER(sendSyslog)
493 DECLARE_THREAD_STARTER(uploadUserFileToAgent)
494 DECLARE_THREAD_STARTER(getRepositories)
495 DECLARE_THREAD_STARTER(addRepository)
496 DECLARE_THREAD_STARTER(modifyRepository)
497 DECLARE_THREAD_STARTER(deleteRepository)
498
499 void readThread();
500 void writeThread();
501 void processingThread();
502 void updateThread();
503 void pollerThread(Node *pNode, int iPollType, UINT32 dwRqId);
504
505 void debugPrintf(int level, const TCHAR *format, ...);
506
507 void setupEncryption(NXCPMessage *request);
508 void respondToKeepalive(UINT32 dwRqId);
509 void onFileUpload(BOOL bSuccess);
510 void sendServerInfo(UINT32 dwRqId);
511 void login(NXCPMessage *pRequest);
512 void sendAllObjects(NXCPMessage *pRequest);
513 void sendSelectedObjects(NXCPMessage *pRequest);
514 void sendEventLog(NXCPMessage *pRequest);
515 void getConfigurationVariables(UINT32 dwRqId);
516 void getPublicConfigurationVariable(NXCPMessage *request);
517 void setConfigurationVariable(NXCPMessage *pRequest);
518 void deleteConfigurationVariable(NXCPMessage *pRequest);
519 void sendUserDB(UINT32 dwRqId);
520 void createUser(NXCPMessage *pRequest);
521 void updateUser(NXCPMessage *pRequest);
522 void detachLdapUser(NXCPMessage *pRequest);
523 void deleteUser(NXCPMessage *pRequest);
524 void setPassword(NXCPMessage *request);
525 void validatePassword(NXCPMessage *request);
526 void lockUserDB(UINT32 dwRqId, BOOL bLock);
527 void sendEventDB(UINT32 dwRqId);
528 void modifyEventTemplate(NXCPMessage *pRequest);
529 void deleteEventTemplate(NXCPMessage *pRequest);
530 void generateEventCode(UINT32 dwRqId);
531 void modifyObject(NXCPMessage *pRequest);
532 void changeObjectMgmtStatus(NXCPMessage *pRequest);
533 void enterMaintenanceMode(NXCPMessage *request);
534 void leaveMaintenanceMode(NXCPMessage *request);
535 void openNodeDCIList(NXCPMessage *pRequest);
536 void closeNodeDCIList(NXCPMessage *pRequest);
537 void modifyNodeDCI(NXCPMessage *pRequest);
538 void copyDCI(NXCPMessage *pRequest);
539 void applyTemplate(NXCPMessage *pRequest);
540 void getCollectedData(NXCPMessage *pRequest);
541 void getTableCollectedData(NXCPMessage *pRequest);
542 bool getCollectedDataFromDB(NXCPMessage *request, NXCPMessage *response, DataCollectionTarget *object, int dciType);
543 void clearDCIData(NXCPMessage *pRequest);
544 void forceDCIPoll(NXCPMessage *pRequest);
545 void changeDCIStatus(NXCPMessage *pRequest);
546 void getLastValues(NXCPMessage *pRequest);
547 void getLastValuesByDciId(NXCPMessage *pRequest);
548 void getTableLastValues(NXCPMessage *pRequest);
549 void getThresholdSummary(NXCPMessage *request);
550 void openEPP(NXCPMessage *request);
551 void closeEPP(UINT32 dwRqId);
552 void saveEPP(NXCPMessage *pRequest);
553 void processEPPRecord(NXCPMessage *pRequest);
554 void sendMIBTimestamp(UINT32 dwRqId);
555 void sendMib(NXCPMessage *request);
556 void createObject(NXCPMessage *request);
557 void changeObjectBinding(NXCPMessage *request, BOOL bBind);
558 void deleteObject(NXCPMessage *request);
559 void getAlarms(NXCPMessage *request);
560 void getAlarm(NXCPMessage *request);
561 void getAlarmEvents(NXCPMessage *request);
562 void acknowledgeAlarm(NXCPMessage *request);
563 void resolveAlarm(NXCPMessage *request, bool terminate);
564 void deleteAlarm(NXCPMessage *request);
565 void openHelpdeskIssue(NXCPMessage *request);
566 void getHelpdeskUrl(NXCPMessage *request);
567 void unlinkHelpdeskIssue(NXCPMessage *request);
568 void getAlarmComments(NXCPMessage *pRequest);
569 void updateAlarmComment(NXCPMessage *pRequest);
570 void deleteAlarmComment(NXCPMessage *request);
571 void updateAlarmStatusFlow(NXCPMessage *request);
572 void createAction(NXCPMessage *pRequest);
573 void updateAction(NXCPMessage *pRequest);
574 void deleteAction(NXCPMessage *pRequest);
575 void sendAllActions(UINT32 dwRqId);
576 void forcedNodePoll(NXCPMessage *pRequest);
577 void onTrap(NXCPMessage *pRequest);
578 void onWakeUpNode(NXCPMessage *pRequest);
579 void queryParameter(NXCPMessage *pRequest);
580 void queryAgentTable(NXCPMessage *pRequest);
581 void editTrap(int iOperation, NXCPMessage *pRequest);
582 void LockTrapCfg(UINT32 dwRqId, BOOL bLock);
583 void sendAllTraps(UINT32 dwRqId);
584 void sendAllTraps2(UINT32 dwRqId);
585 void LockPackageDB(UINT32 dwRqId, BOOL bLock);
586 void SendAllPackages(UINT32 dwRqId);
587 void InstallPackage(NXCPMessage *pRequest);
588 void RemovePackage(NXCPMessage *pRequest);
589 void DeployPackage(NXCPMessage *pRequest);
590 void getParametersList(NXCPMessage *pRequest);
591 void getUserVariable(NXCPMessage *pRequest);
592 void setUserVariable(NXCPMessage *pRequest);
593 void copyUserVariable(NXCPMessage *pRequest);
594 void enumUserVariables(NXCPMessage *pRequest);
595 void deleteUserVariable(NXCPMessage *pRequest);
596 void changeObjectZone(NXCPMessage *pRequest);
597 void getAgentConfig(NXCPMessage *pRequest);
598 void updateAgentConfig(NXCPMessage *pRequest);
599 void executeAction(NXCPMessage *pRequest);
600 void getObjectTools(UINT32 requestId);
601 void getObjectToolDetails(NXCPMessage *request);
602 void updateObjectTool(NXCPMessage *pRequest);
603 void deleteObjectTool(NXCPMessage *pRequest);
604 void changeObjectToolStatus(NXCPMessage *pRequest);
605 void generateObjectToolId(UINT32 dwRqId);
606 void execTableTool(NXCPMessage *pRequest);
607 void changeSubscription(NXCPMessage *pRequest);
608 void sendSyslog(NXCPMessage *pRequest);
609 void sendServerStats(UINT32 dwRqId);
610 void sendScriptList(UINT32 dwRqId);
611 void sendScript(NXCPMessage *pRequest);
612 void updateScript(NXCPMessage *pRequest);
613 void renameScript(NXCPMessage *pRequest);
614 void deleteScript(NXCPMessage *pRequest);
615 void SendSessionList(UINT32 dwRqId);
616 void KillSession(NXCPMessage *pRequest);
617 void SendTrapLog(NXCPMessage *pRequest);
618 void StartSnmpWalk(NXCPMessage *pRequest);
619 void resolveDCINames(NXCPMessage *pRequest);
620 UINT32 resolveDCIName(UINT32 dwNode, UINT32 dwItem, TCHAR **ppszName);
621 void sendConfigForAgent(NXCPMessage *pRequest);
622 void sendAgentCfgList(UINT32 dwRqId);
623 void OpenAgentConfig(NXCPMessage *pRequest);
624 void SaveAgentConfig(NXCPMessage *pRequest);
625 void DeleteAgentConfig(NXCPMessage *pRequest);
626 void SwapAgentConfigs(NXCPMessage *pRequest);
627 void SendObjectComments(NXCPMessage *pRequest);
628 void updateObjectComments(NXCPMessage *pRequest);
629 void pushDCIData(NXCPMessage *pRequest);
630 void getAddrList(NXCPMessage *request);
631 void setAddrList(NXCPMessage *request);
632 void resetComponent(NXCPMessage *pRequest);
633 void getDCIEventList(NXCPMessage *request);
634 void getDCIScriptList(NXCPMessage *request);
635 void SendDCIInfo(NXCPMessage *pRequest);
636 void sendPerfTabDCIList(NXCPMessage *pRequest);
637 void exportConfiguration(NXCPMessage *pRequest);
638 void importConfiguration(NXCPMessage *pRequest);
639 void sendGraphList(NXCPMessage *request);
640 void saveGraph(NXCPMessage *pRequest);
641 void deleteGraph(NXCPMessage *pRequest);
642 void addCACertificate(NXCPMessage *pRequest);
643 void deleteCertificate(NXCPMessage *pRequest);
644 void updateCertificateComments(NXCPMessage *pRequest);
645 void getCertificateList(UINT32 dwRqId);
646 void queryL2Topology(NXCPMessage *pRequest);
647 void sendSMS(NXCPMessage *pRequest);
648 void SendCommunityList(UINT32 dwRqId);
649 void UpdateCommunityList(NXCPMessage *pRequest);
650 void getSituationList(UINT32 dwRqId);
651 void createSituation(NXCPMessage *pRequest);
652 void updateSituation(NXCPMessage *pRequest);
653 void deleteSituation(NXCPMessage *pRequest);
654 void deleteSituationInstance(NXCPMessage *pRequest);
655 void setConfigCLOB(NXCPMessage *pRequest);
656 void getConfigCLOB(NXCPMessage *pRequest);
657 void registerAgent(NXCPMessage *pRequest);
658 void getServerFile(NXCPMessage *pRequest);
659 void cancelFileMonitoring(NXCPMessage *request);
660 void getAgentFile(NXCPMessage *pRequest);
661 void testDCITransformation(NXCPMessage *pRequest);
662 void sendJobList(UINT32 dwRqId);
663 void cancelJob(NXCPMessage *pRequest);
664 void holdJob(NXCPMessage *pRequest);
665 void unholdJob(NXCPMessage *pRequest);
666 void deployAgentPolicy(NXCPMessage *pRequest, bool uninstallFlag);
667 void getUserCustomAttribute(NXCPMessage *request);
668 void setUserCustomAttribute(NXCPMessage *request);
669 void openServerLog(NXCPMessage *request);
670 void closeServerLog(NXCPMessage *request);
671 void queryServerLog(NXCPMessage *request);
672 void getServerLogQueryData(NXCPMessage *request);
673 void sendUsmCredentials(UINT32 dwRqId);
674 void updateUsmCredentials(NXCPMessage *pRequest);
675 void sendDCIThresholds(NXCPMessage *request);
676 void addClusterNode(NXCPMessage *request);
677 void findNodeConnection(NXCPMessage *request);
678 void findMacAddress(NXCPMessage *request);
679 void findIpAddress(NXCPMessage *request);
680 void sendLibraryImage(NXCPMessage *request);
681 void updateLibraryImage(NXCPMessage *request);
682 void listLibraryImages(NXCPMessage *request);
683 void deleteLibraryImage(NXCPMessage *request);
684 void executeServerCommand(NXCPMessage *request);
685 void uploadFileToAgent(NXCPMessage *request);
686 void listServerFileStore(NXCPMessage *request);
687 void processConsoleCommand(NXCPMessage *msg);
688 void openConsole(UINT32 rqId);
689 void closeConsole(UINT32 rqId);
690 void getVlans(NXCPMessage *msg);
691 void receiveFile(NXCPMessage *request);
692 void deleteFile(NXCPMessage *request);
693 void getNetworkPath(NXCPMessage *request);
694 void getNodeComponents(NXCPMessage *request);
695 void getNodeSoftware(NXCPMessage *request);
696 void getWinPerfObjects(NXCPMessage *request);
697 void listMappingTables(NXCPMessage *request);
698 void getMappingTable(NXCPMessage *request);
699 void updateMappingTable(NXCPMessage *request);
700 void deleteMappingTable(NXCPMessage *request);
701 void getWirelessStations(NXCPMessage *request);
702 void getSummaryTables(UINT32 rqId);
703 void getSummaryTableDetails(NXCPMessage *request);
704 void modifySummaryTable(NXCPMessage *request);
705 void deleteSummaryTable(NXCPMessage *request);
706 void querySummaryTable(NXCPMessage *request);
707 void queryAdHocSummaryTable(NXCPMessage *request);
708 void forwardToReportingServer(NXCPMessage *request);
709 void getSubnetAddressMap(NXCPMessage *request);
710 void getEffectiveRights(NXCPMessage *request);
711 void fileManagerControl(NXCPMessage *request);
712 void uploadUserFileToAgent(NXCPMessage *request);
713 void getSwitchForwardingDatabase(NXCPMessage *request);
714 void getRoutingTable(NXCPMessage *request);
715 void getLocationHistory(NXCPMessage *request);
716 void getScreenshot(NXCPMessage *request);
717 void executeScript(NXCPMessage *request);
718 void compileScript(NXCPMessage *request);
719 void resyncAgentDciConfiguration(NXCPMessage *request);
720 void cleanAgentDciConfiguration(NXCPMessage *request);
721 void getSchedulerTaskHandlers(NXCPMessage *request);
722 void getScheduledTasks(NXCPMessage *request);
723 void addScheduledTask(NXCPMessage *request);
724 void updateScheduledTask(NXCPMessage *request);
725 void removeScheduledTask(NXCPMessage *request);
726 void getRepositories(NXCPMessage *request);
727 void addRepository(NXCPMessage *request);
728 void modifyRepository(NXCPMessage *request);
729 void deleteRepository(NXCPMessage *request);
730 #ifdef WITH_ZMQ
731 void zmqManageSubscription(NXCPMessage *request, zmq::SubscriptionType type, bool subscribe);
732 void zmqListSubscriptions(NXCPMessage *request, zmq::SubscriptionType type);
733 #endif
734
735 public:
736 ClientSession(SOCKET hSocket, struct sockaddr *addr);
737 ~ClientSession();
738
739 void incRefCount() { InterlockedIncrement(&m_refCount); }
740 void decRefCount() { InterlockedDecrement(&m_refCount); }
741
742 void run();
743
744 void postMessage(NXCPMessage *pMsg) { m_pSendQueue->put(pMsg->createMessage()); }
745 bool sendMessage(NXCPMessage *pMsg);
746 void sendRawMessage(NXCP_MESSAGE *pMsg);
747 void sendPollerMsg(UINT32 dwRqId, const TCHAR *pszMsg);
748 BOOL sendFile(const TCHAR *file, UINT32 dwRqId, long offset);
749
750 void writeAuditLog(const TCHAR *subsys, bool success, UINT32 objectId, const TCHAR *format, ...);
751
752 int getId() const { return m_id; }
753 void setId(int id) { if (m_id == -1) m_id = id; }
754
755 int getState() const { return m_state; }
756 const TCHAR *getLoginName() const { return m_loginName; }
757 const TCHAR *getSessionName() const { return m_sessionName; }
758 const TCHAR *getClientInfo() const { return m_clientInfo; }
759 const TCHAR *getWorkstation() const { return m_workstation; }
760 const TCHAR *getWebServerAddress() const { return m_webServerAddress; }
761 UINT32 getUserId() const { return m_dwUserId; }
762 UINT64 getSystemRights() const { return m_dwSystemAccess; }
763 UINT32 getFlags() const { return m_dwFlags; }
764 bool isAuthenticated() const { return (m_dwFlags & CSF_AUTHENTICATED) ? true : false; }
765 bool isTerminated() const { return (m_dwFlags & CSF_TERMINATED) ? true : false; }
766 bool isConsoleOpen() const { return (m_dwFlags & CSF_CONSOLE_OPEN) ? true : false; }
767 WORD getCurrentCmd() const { return m_wCurrentCmd; }
768 int getCipher() const { return (m_pCtx == NULL) ? -1 : m_pCtx->getCipher(); }
769 int getClientType() const { return m_clientType; }
770 time_t getLoginTime() const { return m_loginTime; }
771 bool isSubscribedTo(const TCHAR *channel) const;
772
773 bool checkSysAccessRights(UINT64 requiredAccess) const
774 {
775 return (m_dwUserId == 0) ? true :
776 ((requiredAccess & m_dwSystemAccess) == requiredAccess);
777 }
778
779 void setCustomLock(UINT32 bit, bool value)
780 {
781 if (value)
782 m_dwFlags |= (bit & 0xFF000000);
783 else
784 m_dwFlags &= ~(bit & 0xFF000000);
785 }
786
787 void kill();
788 void notify(UINT32 dwCode, UINT32 dwData = 0);
789
790 void queueUpdate(UPDATE_INFO *pUpdate) { m_pUpdateQueue->put(pUpdate); }
791 void onNewEvent(Event *pEvent);
792 void onSyslogMessage(NX_SYSLOG_RECORD *pRec);
793 void onNewSNMPTrap(NXCPMessage *pMsg);
794 void onObjectChange(NetObj *pObject);
795 void onAlarmUpdate(UINT32 dwCode, const Alarm *alarm);
796 void onActionDBUpdate(UINT32 dwCode, NXC_ACTION *pAction);
797 void onSituationChange(NXCPMessage *msg);
798 void onLibraryImageChange(uuid_t *guid, bool removed = false);
799 };
800
801 /**
802 * Delayed SQL request
803 */
804 typedef struct
805 {
806 TCHAR *query;
807 int bindCount;
808 BYTE *sqlTypes;
809 TCHAR *bindings[1]; /* actual size determined by bindCount field */
810 } DELAYED_SQL_REQUEST;
811
812 /**
813 * Delayed request for idata_ INSERT
814 */
815 typedef struct
816 {
817 time_t timestamp;
818 UINT32 nodeId;
819 UINT32 dciId;
820 TCHAR value[MAX_RESULT_LENGTH];
821 } DELAYED_IDATA_INSERT;
822
823 /**
824 * Delayed request for raw_dci_values UPDATE
825 */
826 typedef struct
827 {
828 time_t timestamp;
829 UINT32 dciId;
830 TCHAR rawValue[MAX_RESULT_LENGTH];
831 TCHAR transformedValue[MAX_RESULT_LENGTH];
832 } DELAYED_RAW_DATA_UPDATE;
833
834 /**
835 * Graph ACL entry
836 */
837 struct GRAPH_ACL_ENTRY
838 {
839 UINT32 dwGraphId;
840 UINT32 dwUserId;
841 UINT32 dwAccess;
842 };
843
844 /**
845 * Information about graph with name existence and it's ID.
846 */
847 struct GRAPH_ACL_AND_ID
848 {
849 UINT32 graphId;
850 UINT32 status;
851 };
852
853 /**
854 * Thread pool stats
855 */
856 enum ThreadPoolStat
857 {
858 THREAD_POOL_CURR_SIZE,
859 THREAD_POOL_MIN_SIZE,
860 THREAD_POOL_MAX_SIZE,
861 THREAD_POOL_REQUESTS,
862 THREAD_POOL_LOAD,
863 THREAD_POOL_USAGE,
864 THREAD_POOL_LOADAVG_1,
865 THREAD_POOL_LOADAVG_5,
866 THREAD_POOL_LOADAVG_15
867 };
868
869 /**
870 * Server command execution data
871 */
872 class ServerCommandExecData
873 {
874 private:
875 TCHAR *m_command;
876 bool m_sendOutput;
877 UINT32 m_requestId;
878 ClientSession *m_session;
879
880 public:
881 ServerCommandExecData(TCHAR *command, bool sendOutput, UINT32 requestId, ClientSession *session);
882 ~ServerCommandExecData();
883
884 bool sendOutput() { return m_sendOutput; }
885 const TCHAR *getCommand() { return m_command; }
886 UINT32 getRequestId() { return m_requestId; }
887 ClientSession *getSession() { return m_session; }
888 };
889
890 /**
891 * Functions
892 */
893 bool NXCORE_EXPORTABLE ConfigReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
894 bool NXCORE_EXPORTABLE ConfigReadStrEx(DB_HANDLE hdb, const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
895 #ifdef UNICODE
896 bool NXCORE_EXPORTABLE ConfigReadStrA(const WCHAR *szVar, char *szBuffer, int iBufSize, const char *szDefault);
897 #else
898 #define ConfigReadStrA ConfigReadStr
899 #endif
900 bool NXCORE_EXPORTABLE ConfigReadStrUTF8(const TCHAR *szVar, char *szBuffer, int iBufSize, const char *szDefault);
901 int NXCORE_EXPORTABLE ConfigReadInt(const TCHAR *szVar, int iDefault);
902 int NXCORE_EXPORTABLE ConfigReadIntEx(DB_HANDLE hdb, const TCHAR *szVar, int iDefault);
903 UINT32 NXCORE_EXPORTABLE ConfigReadULong(const TCHAR *szVar, UINT32 dwDefault);
904 bool NXCORE_EXPORTABLE ConfigReadByteArray(const TCHAR *pszVar, int *pnArray, int nSize, int nDefault);
905 bool NXCORE_EXPORTABLE ConfigWriteStr(const TCHAR *szVar, const TCHAR *szValue, bool bCreate, bool isVisible = true, bool needRestart = false);
906 bool NXCORE_EXPORTABLE ConfigWriteInt(const TCHAR *szVar, int iValue, bool bCreate, bool isVisible = true, bool needRestart = false);
907 bool NXCORE_EXPORTABLE ConfigWriteULong(const TCHAR *szVar, UINT32 dwValue, bool bCreate, bool isVisible = true, bool needRestart = false);
908 bool NXCORE_EXPORTABLE ConfigWriteByteArray(const TCHAR *pszVar, int *pnArray, int nSize, bool bCreate, bool isVisible = true, bool needRestart = false);
909 TCHAR NXCORE_EXPORTABLE *ConfigReadCLOB(const TCHAR *var, const TCHAR *defValue);
910 bool NXCORE_EXPORTABLE ConfigWriteCLOB(const TCHAR *var, const TCHAR *value, bool bCreate);
911 bool NXCORE_EXPORTABLE ConfigDelete(const TCHAR *name);
912
913 bool NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
914 INT32 NXCORE_EXPORTABLE MetaDataReadInt(const TCHAR *var, UINT32 defaultValue);
915 bool NXCORE_EXPORTABLE MetaDataWriteStr(const TCHAR *varName, const TCHAR *value);
916
917 bool NXCORE_EXPORTABLE LoadConfig(int *debugLevel);
918
919 void NXCORE_EXPORTABLE Shutdown();
920 void NXCORE_EXPORTABLE FastShutdown();
921 BOOL NXCORE_EXPORTABLE Initialize();
922 THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL Main(void *);
923 void NXCORE_EXPORTABLE ShutdownDB();
924 void InitiateShutdown();
925
926 bool NXCORE_EXPORTABLE SleepAndCheckForShutdown(int iSeconds);
927
928 void ConsolePrintf(CONSOLE_CTX pCtx, const TCHAR *pszFormat, ...)
929 #if !defined(UNICODE) && (defined(__GNUC__) || defined(__clang__))
930 __attribute__ ((format(printf, 2, 3)))
931 #endif
932 ;
933 void ConsoleWrite(CONSOLE_CTX pCtx, const TCHAR *text);
934 int ProcessConsoleCommand(const TCHAR *pszCmdLine, CONSOLE_CTX pCtx);
935
936 void SaveObjects(DB_HANDLE hdb);
937 void NXCORE_EXPORTABLE ObjectTransactionStart();
938 void NXCORE_EXPORTABLE ObjectTransactionEnd();
939
940 void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query);
941 void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query, int bindCount, int *sqlTypes, const TCHAR **values);
942 void QueueIDataInsert(time_t timestamp, UINT32 nodeId, UINT32 dciId, const TCHAR *value);
943 void QueueRawDciDataUpdate(time_t timestamp, UINT32 dciId, const TCHAR *rawValue, const TCHAR *transformedValue);
944 void StartDBWriter();
945 void StopDBWriter();
946
947 void PerfDataStorageRequest(DCItem *dci, time_t timestamp, const TCHAR *value);
948 void PerfDataStorageRequest(DCTable *dci, time_t timestamp, Table *value);
949
950 void DecodeSQLStringAndSetVariable(NXCPMessage *pMsg, UINT32 dwVarId, TCHAR *pszStr);
951
952 SNMP_Transport *SnmpCheckCommSettings(UINT32 snmpProxy, const InetAddress& ipAddr, INT16 *version, UINT16 originalPort, SNMP_SecurityContext *originalContext, StringList *customTestOids);
953 void StrToMac(const TCHAR *pszStr, BYTE *pBuffer);
954
955 void InitLocalNetInfo();
956
957 ARP_CACHE *GetLocalArpCache();
958 ARP_CACHE *SnmpGetArpCache(UINT32 dwVersion, SNMP_Transport *pTransport);
959
960 InterfaceList *GetLocalInterfaceList();
961
962 ROUTING_TABLE *SnmpGetRoutingTable(UINT32 dwVersion, SNMP_Transport *pTransport);
963
964 void LoadNetworkDeviceDrivers();
965 NetworkDeviceDriver *FindDriverForNode(Node *node, SNMP_Transport *pTransport);
966 NetworkDeviceDriver *FindDriverByName(const TCHAR *name);
967 void AddDriverSpecificOids(StringList *list);
968
969 void WatchdogInit();
970 UINT32 WatchdogAddThread(const TCHAR *szName, time_t tNotifyInterval);
971 void WatchdogNotify(UINT32 dwId);
972 void WatchdogPrintStatus(CONSOLE_CTX pCtx);
973
974 void CheckForMgmtNode();
975 Node NXCORE_EXPORTABLE *PollNewNode(const InetAddress& ipAddr, UINT32 dwCreationFlags, WORD agentPort,
976 WORD snmpPort, const TCHAR *pszName, UINT32 dwProxyNode, UINT32 dwSNMPProxy, Cluster *pCluster,
977 UINT32 zoneId, bool doConfPoll, bool discoveredNode);
978
979 void NXCORE_EXPORTABLE EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
980 void NXCORE_EXPORTABLE NotifyClientSessions(UINT32 dwCode, UINT32 dwData);
981 void NXCORE_EXPORTABLE NotifyClientGraphUpdate(NXCPMessage *update, UINT32 graphId);
982 int GetSessionCount(bool includeSystemAccount);
983 bool IsLoggedIn(UINT32 dwUserId);
984 bool NXCORE_EXPORTABLE KillClientSession(int id);
985 void CloseOtherSessions(UINT32 userId, UINT32 thisSession);
986
987 void GetSysInfoStr(TCHAR *pszBuffer, int nMaxSize);
988 InetAddress GetLocalIpAddr();
989 TCHAR *GetLocalHostName(TCHAR *buffer, size_t bufSize);
990
991 BOOL ExecCommand(TCHAR *pszCommand);
992 BOOL SendMagicPacket(UINT32 dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
993
994 BOOL InitIdTable();
995 UINT32 CreateUniqueId(int iGroup);
996 QWORD CreateUniqueEventId();
997 void SaveCurrentFreeId();
998
999 void InitMailer();
1000 void ShutdownMailer();
1001 void NXCORE_EXPORTABLE PostMail(const TCHAR *pszRcpt, const TCHAR *pszSubject, const TCHAR *pszText);
1002
1003 void InitSMSSender();
1004 void ShutdownSMSSender();
1005 void NXCORE_EXPORTABLE PostSMS(const TCHAR *pszRcpt, const TCHAR *pszText);
1006
1007 void InitTraps();
1008 void SendTrapsToClient(ClientSession *pSession, UINT32 dwRqId);
1009 void CreateTrapCfgMessage(NXCPMessage &msg);
1010 UINT32 CreateNewTrap(UINT32 *pdwTrapId);
1011 UINT32 CreateNewTrap(NXC_TRAP_CFG_ENTRY *pTrap);
1012 UINT32 UpdateTrapFromMsg(NXCPMessage *pMsg);
1013 UINT32 DeleteTrap(UINT32 dwId);
1014 void CreateTrapExportRecord(String &xml, UINT32 id);
1015
1016 BOOL IsTableTool(UINT32 dwToolId);
1017 BOOL CheckObjectToolAccess(UINT32 dwToolId, UINT32 dwUserId);
1018 UINT32 ExecuteTableTool(UINT32 dwToolId, Node *pNode, UINT32 dwRqId, ClientSession *pSession);
1019 UINT32 DeleteObjectToolFromDB(UINT32 dwToolId);
1020 UINT32 ChangeObjectToolStatus(UINT32 toolId, bool enabled);
1021 UINT32 UpdateObjectToolFromMessage(NXCPMessage *pMsg);
1022 void CreateObjectToolExportRecord(String &xml, UINT32 id);
1023 bool ImportObjectTool(ConfigEntry *config);
1024 UINT32 GetObjectToolsIntoMessage(NXCPMessage *msg, UINT32 userId, bool fullAccess);
1025 UINT32 GetObjectToolDetailsIntoMessage(UINT32 toolId, NXCPMessage *msg);
1026
1027 void ExecuteServerCommand(void *arg);
1028
1029 UINT32 ModifySummaryTable(NXCPMessage *msg, LONG *newId);
1030 UINT32 DeleteSummaryTable(LONG tableId);
1031 Table *QuerySummaryTable(LONG tableId, SummaryTable *adHocDefinition, UINT32 baseObjectId, UINT32 userId, UINT32 *rcc);
1032 bool CreateSummaryTableExportRecord(INT32 id, String &xml);
1033 bool ImportSummaryTable(ConfigEntry *config);
1034
1035 void CreateMessageFromSyslogMsg(NXCPMessage *pMsg, NX_SYSLOG_RECORD *pRec);
1036 void ReinitializeSyslogParser();
1037 void OnSyslogConfigurationChange(const TCHAR *name, const TCHAR *value);
1038
1039 void EscapeString(String &str);
1040
1041 void InitAuditLog();
1042 void NXCORE_EXPORTABLE WriteAuditLog(const TCHAR *subsys, bool isSuccess, UINT32 userId,
1043 const TCHAR *workstation, int sessionId, UINT32 objectId,
1044 const TCHAR *format, ...);
1045 void NXCORE_EXPORTABLE WriteAuditLog2(const TCHAR *subsys, bool isSuccess, UINT32 userId,
1046 const TCHAR *workstation, int sessionId, UINT32 objectId,
1047 const TCHAR *format, va_list args);
1048
1049 bool ValidateConfig(Config *config, UINT32 flags, TCHAR *errorText, int errorTextLen);
1050 UINT32 ImportConfig(Config *config, UINT32 flags);
1051
1052 #ifdef _WITH_ENCRYPTION
1053 X509 *CertificateFromLoginMessage(NXCPMessage *pMsg);
1054 BOOL ValidateUserCertificate(X509 *pCert, const TCHAR *pszLogin, BYTE *pChallenge,
1055 BYTE *pSignature, UINT32 dwSigLen, int nMappingMethod,
1056 const TCHAR *pszMappingData);
1057 void ReloadCertificates();
1058 #endif
1059
1060 #ifndef _WIN32
1061 THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL SignalHandler(void *);
1062 #endif /* not _WIN32 */
1063
1064 void DbgTestRWLock(RWLOCK hLock, const TCHAR *szName, CONSOLE_CTX console);
1065 void DumpClientSessions(CONSOLE_CTX console);
1066 void DumpMobileDeviceSessions(CONSOLE_CTX console);
1067 void ShowServerStats(CONSOLE_CTX console);
1068 void ShowQueueStats(CONSOLE_CTX console, Queue *pQueue, const TCHAR *pszName);
1069 void ShowThreadPool(CONSOLE_CTX console, ThreadPool *p);
1070 LONG GetThreadPoolStat(ThreadPoolStat stat, const TCHAR *param, TCHAR *value);
1071 void DumpProcess(CONSOLE_CTX console);
1072
1073 #define GRAPH_FLAG_TEMPLATE 1
1074
1075 GRAPH_ACL_ENTRY *LoadGraphACL(DB_HANDLE hdb, UINT32 graphId, int *pnACLSize);
1076 BOOL CheckGraphAccess(GRAPH_ACL_ENTRY *pACL, int nACLSize, UINT32 graphId, UINT32 graphUserId, UINT32 graphDesiredAccess);
1077 UINT32 GetGraphAccessCheckResult(UINT32 graphId, UINT32 graphUserId);
1078 GRAPH_ACL_AND_ID IsGraphNameExists(const TCHAR *graphName);
1079 void FillGraphListMsg(NXCPMessage *msg, UINT32 userId, bool templageGraphs);
1080 void SaveGraph(NXCPMessage *pRequest, UINT32 userId, NXCPMessage *msg);
1081 UINT32 DeleteGraph(UINT32 graphId, UINT32 userId);
1082
1083 #if XMPP_SUPPORTED
1084 void SendXMPPMessage(const TCHAR *rcpt, const TCHAR *text);
1085 #endif
1086
1087 const TCHAR NXCORE_EXPORTABLE *CountryAlphaCode(const TCHAR *code);
1088 const TCHAR NXCORE_EXPORTABLE *CountryName(const TCHAR *code);
1089 const TCHAR NXCORE_EXPORTABLE *CurrencyAlphaCode(const TCHAR *numericCode);
1090 int NXCORE_EXPORTABLE CurrencyExponent(const TCHAR *code);
1091 const TCHAR NXCORE_EXPORTABLE *CurrencyName(const TCHAR *code);
1092
1093 /**
1094 * Housekeeper control
1095 */
1096 void StartHouseKeeper();
1097 void StopHouseKeeper();
1098 void RunHouseKeeper();
1099
1100 /**
1101 * File monitoring
1102 */
1103 struct MONITORED_FILE
1104 {
1105 TCHAR fileName[MAX_PATH];
1106 ClientSession *session;
1107 UINT32 nodeID;
1108 };
1109
1110 class FileMonitoringList
1111 {
1112 private:
1113 MUTEX m_mutex;
1114 ObjectArray<MONITORED_FILE> m_monitoredFiles;
1115
1116 public:
1117 FileMonitoringList();
1118 ~FileMonitoringList();
1119 void addMonitoringFile(MONITORED_FILE *fileForAdd, Node *obj, AgentConnection *conn);
1120 bool checkDuplicate(MONITORED_FILE *fileForAdd);
1121 ObjectArray<ClientSession>* findClientByFNameAndNodeID(const TCHAR *fileName, UINT32 nodeID);
1122 bool removeMonitoringFile(MONITORED_FILE *fileForRemove);
1123 void removeDisconnectedNode(UINT32 nodeId);
1124
1125 private:
1126 void lock();
1127 void unlock();
1128 };
1129
1130 /**
1131 * Global variables
1132 */
1133 extern TCHAR NXCORE_EXPORTABLE g_szConfigFile[];
1134 extern TCHAR NXCORE_EXPORTABLE g_szLogFile[];
1135 extern UINT32 g_dwLogRotationMode;
1136 extern UINT32 g_dwMaxLogSize;
1137 extern UINT32 g_dwLogHistorySize;
1138 extern TCHAR g_szDailyLogFileSuffix[64];
1139 extern TCHAR NXCORE_EXPORTABLE g_szDumpDir[];
1140 extern TCHAR NXCORE_EXPORTABLE g_szListenAddress[];
1141 #ifndef _WIN32
1142 extern TCHAR NXCORE_EXPORTABLE g_szPIDFile[];
1143 #endif
1144 extern TCHAR NXCORE_EXPORTABLE g_netxmsdDataDir[];
1145 extern TCHAR NXCORE_EXPORTABLE g_netxmsdLibDir[];
1146 extern UINT32 NXCORE_EXPORTABLE g_processAffinityMask;
1147 extern UINT64 g_serverId;
1148 extern RSA *g_pServerKey;
1149 extern UINT32 g_icmpPingSize;
1150 extern UINT32 g_icmpPingTimeout;
1151 extern UINT32 g_auditFlags;
1152 extern time_t g_serverStartTime;
1153 extern UINT32 g_lockTimeout;
1154 extern UINT32 g_agentCommandTimeout;
1155 extern UINT32 g_thresholdRepeatInterval;
1156 extern int g_requiredPolls;
1157 extern UINT32 g_slmPollingInterval;
1158 extern UINT32 g_offlineDataRelevanceTime;
1159
1160 extern TCHAR g_szDbDriver[];
1161 extern TCHAR g_szDbDrvParams[];
1162 extern TCHAR g_szDbServer[];
1163 extern TCHAR g_szDbLogin[];
1164 extern TCHAR g_szDbPassword[];
1165 extern TCHAR g_szDbName[];
1166 extern TCHAR g_szDbSchema[];
1167 extern DB_DRIVER g_dbDriver;
1168 extern Queue *g_dbWriterQueue;
1169 extern Queue *g_dciDataWriterQueue;
1170 extern Queue *g_dciRawDataWriterQueue;
1171 extern UINT64 g_idataWriteRequests;
1172 extern UINT64 g_rawDataWriteRequests;
1173 extern UINT64 g_otherWriteRequests;
1174
1175 extern int NXCORE_EXPORTABLE g_dbSyntax;
1176 extern FileMonitoringList g_monitoringList;
1177
1178 extern ThreadPool NXCORE_EXPORTABLE *g_mainThreadPool;
1179
1180 #endif /* _nms_core_h_ */