"alarm details" view mostly working; event history for active alarms preserved; minor...
[public/netxms.git] / src / server / include / nms_core.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
6adc4a1a 3** Copyright (C) 2003-2012 Victor Kirhenshtein
5039dede
AK
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#define WSAGetLastError() (errno)
51
52#endif /* _WIN32 */
53
54#include <time.h>
55#include <stdio.h>
56#include <string.h>
57#include <stdarg.h>
58
59#define SHOW_FLAG_VALUE(x) _T(" %-32s = %d\n"), _T(#x), (g_dwFlags & x) ? 1 : 0
60
61
62//
63// Common includes
64//
65
66#include <nms_util.h>
67#include <dbdrv.h>
68#include <nms_cscp.h>
69#include <uuid.h>
70#include <nxsrvapi.h>
5039dede
AK
71#include <nxqueue.h>
72#include <nxsnmp.h>
73#include <nxmodule.h>
74#include <nxsl.h>
9d88cdc9 75#include <nxdbapi.h>
9796ce45 76#include <nddrv.h>
d41edbd3 77#include <nxcore_smclp.h>
5039dede
AK
78
79
80//
81// Console context
82//
83
84struct __console_ctx
85{
86 SOCKET hSocket;
d3a7cf4c 87 MUTEX socketMutex;
5039dede 88 CSCPMessage *pMsg;
200d662d 89 ClientSession *session;
5039dede
AK
90};
91
92typedef __console_ctx * CONSOLE_CTX;
93
94
95//
96// Server includes
97//
98
99#include "nms_dcoll.h"
100#include "nms_users.h"
101#include "nms_objects.h"
5039dede
AK
102#include "nms_locks.h"
103#include "nms_pkg.h"
104#include "nms_topo.h"
105#include "nms_script.h"
5039dede 106#include "nxcore_situations.h"
ab621f39 107#include "nxcore_jobs.h"
e05b1945 108#include "nxcore_logs.h"
5039dede
AK
109
110
111//
112// Common constants and macros
113//
114
73869331
VK
115#define MAX_LINE_SIZE 4096
116#define GROUP_FLAG_BIT ((DWORD)0x80000000)
117#define CHECKPOINT_SNMP_PORT 260
118#define DEFAULT_AFFINITY_MASK 0xFFFFFFFF
5039dede
AK
119
120typedef void * HSNMPSESSION;
121
5039dede 122
df8a4ca2
VK
123//
124// Prefixes for poller messages
125//
126
127#define POLLER_ERROR _T("\x7F") _T("e")
128#define POLLER_WARNING _T("\x7Fw")
129#define POLLER_INFO _T("\x7Fi")
130
131
5039dede
AK
132//
133// Unique identifier group codes
134//
135
136#define IDG_NETWORK_OBJECT 0
137#define IDG_CONTAINER_CAT 1
138#define IDG_EVENT 2
139#define IDG_ITEM 3
140#define IDG_SNMP_TRAP 4
24dc5346 141#define IDG_JOB 5
5039dede
AK
142#define IDG_ACTION 6
143#define IDG_EVENT_GROUP 7
144#define IDG_THRESHOLD 8
145#define IDG_USER 9
146#define IDG_USER_GROUP 10
147#define IDG_ALARM 11
148#define IDG_ALARM_NOTE 12
149#define IDG_PACKAGE 13
636e0c72 150#define IDG_SLM_TICKET 14
5039dede
AK
151#define IDG_OBJECT_TOOL 15
152#define IDG_SCRIPT 16
153#define IDG_AGENT_CONFIG 17
154#define IDG_GRAPH 18
155#define IDG_CERTIFICATE 19
156#define IDG_SITUATION 20
ba89fed2 157#define IDG_DCT_COLUMN 21
5039dede
AK
158
159
160//
161// Exit codes for console commands
162//
163
164#define CMD_EXIT_CONTINUE 0
165#define CMD_EXIT_CLOSE_SESSION 1
166#define CMD_EXIT_SHUTDOWN 2
167
168
169//
170// Network discovery mode
171//
172
173#define DISCOVERY_DISABLED 0
174#define DISCOVERY_PASSIVE_ONLY 1
175#define DISCOVERY_ACTIVE 2
176
177
178//
179// Client session flags
180//
181
5039dede
AK
182#define CSF_EPP_LOCKED ((DWORD)0x0002)
183#define CSF_PACKAGE_DB_LOCKED ((DWORD)0x0004)
184#define CSF_USER_DB_LOCKED ((DWORD)0x0008)
185#define CSF_EPP_UPLOAD ((DWORD)0x0010)
200d662d 186#define CSF_CONSOLE_OPEN ((DWORD)0x0020)
5039dede 187#define CSF_AUTHENTICATED ((DWORD)0x0080)
5039dede
AK
188#define CSF_RECEIVING_MAP_DATA ((DWORD)0x0200)
189#define CSF_SYNC_OBJECT_COMMENTS ((DWORD)0x0400)
190
191
192//
193// Client session states
194//
195
196#define SESSION_STATE_INIT 0
197#define SESSION_STATE_IDLE 1
198#define SESSION_STATE_PROCESSING 2
199
200
201//
202// Information categories for UPDATE_INFO structure
203//
204
205#define INFO_CAT_EVENT 1
206#define INFO_CAT_OBJECT_CHANGE 2
207#define INFO_CAT_ALARM 3
208#define INFO_CAT_ACTION 4
209#define INFO_CAT_SYSLOG_MSG 5
210#define INFO_CAT_SNMP_TRAP 6
211#define INFO_CAT_AUDIT_RECORD 7
212#define INFO_CAT_SITUATION 8
624f1165 213#define INFO_CAT_LIBRARY_IMAGE 9
5039dede
AK
214
215
216//
217// Certificate types
218//
219
220#define CERT_TYPE_TRUSTED_CA 0
221#define CERT_TYPE_USER 1
222
223
224//
225// Audit subsystems
226//
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
234//
235// Event handling subsystem definitions
236//
237
238#include "nms_events.h"
239#include "nms_actions.h"
240#include "nms_alarm.h"
241
242
243//
244// New node information
245//
246
247typedef struct
248{
249 DWORD dwIpAddr;
250 DWORD dwNetMask;
dc25b21c 251 DWORD zoneId;
5039dede 252 BOOL ignoreFilter;
baa5324c 253 BYTE bMacAddr[MAC_ADDR_LENGTH];
5039dede
AK
254} NEW_NODE;
255
256
257//
258// New node flags
259//
260
261#define NNF_IS_SNMP 0x0001
262#define NNF_IS_AGENT 0x0002
263#define NNF_IS_ROUTER 0x0004
264#define NNF_IS_BRIDGE 0x0008
265#define NNF_IS_PRINTER 0x0010
266#define NNF_IS_CDP 0x0020
267#define NNF_IS_SONMP 0x0040
268#define NNF_IS_LLDP 0x0080
269
270
271//
272// Node information for autodiscovery filter
273//
274
275typedef struct
276{
277 DWORD dwIpAddr;
278 DWORD dwNetMask;
279 DWORD dwSubnetAddr;
280 DWORD dwFlags;
281 int nSNMPVersion;
35f836fe
VK
282 TCHAR szObjectId[MAX_OID_LEN * 4]; // SNMP OID
283 TCHAR szAgentVersion[MAX_AGENT_VERSION_LEN];
284 TCHAR szPlatform[MAX_PLATFORM_NAME_LEN];
5039dede
AK
285} DISCOVERY_FILTER_DATA;
286
287
288//
289// Data update structure for client sessions
290//
291
292typedef struct
293{
294 DWORD dwCategory; // Data category - event, network object, etc.
295 DWORD dwCode; // Data-specific update code
296 void *pData; // Pointer to data block
297} UPDATE_INFO;
298
534e1b83
VK
299/**
300 * Extended agent connection
301 */
5039dede
AK
302class AgentConnectionEx : public AgentConnection
303{
304protected:
f480bdd4
VK
305 virtual void onTrap(CSCPMessage *msg);
306 virtual void onDataPush(CSCPMessage *msg);
5039dede
AK
307
308public:
5039dede
AK
309 AgentConnectionEx(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
310 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL) :
311 AgentConnection(dwAddr, wPort, iAuthMethod, pszSecret) { }
312 virtual ~AgentConnectionEx();
45d84f8a
VK
313
314 DWORD deployPolicy(AgentPolicy *policy);
93599cfd 315 DWORD uninstallPolicy(AgentPolicy *policy);
5039dede
AK
316};
317
534e1b83
VK
318/**
319 * Mobile device session
320 */
321class NXCORE_EXPORTABLE MobileDeviceSession
322{
323private:
324 SOCKET m_hSocket;
325 Queue *m_pSendQueue;
326 Queue *m_pMessageQueue;
327 DWORD m_dwIndex;
328 int m_iState;
329 WORD m_wCurrentCmd;
330 DWORD m_dwUserId;
331 DWORD m_deviceObjectId;
332 CSCP_BUFFER *m_pMsgBuffer;
333 NXCPEncryptionContext *m_pCtx;
334 BYTE m_challenge[CLIENT_CHALLENGE_SIZE];
335 THREAD m_hWriteThread;
336 THREAD m_hProcessingThread;
337 MUTEX m_mutexSocketWrite;
338 struct sockaddr *m_clientAddr;
339 TCHAR m_szHostName[256]; // IP address of name of conneced host in textual form
340 TCHAR m_szUserName[MAX_SESSION_NAME]; // String in form login_name@host
341 TCHAR m_szClientInfo[96]; // Client app info string
342 DWORD m_dwEncryptionRqId;
343 DWORD m_dwEncryptionResult;
344 CONDITION m_condEncryptionSetup;
345 DWORD m_dwRefCount;
346 bool m_isAuthenticated;
347
348 static THREAD_RESULT THREAD_CALL readThreadStarter(void *);
349 static THREAD_RESULT THREAD_CALL writeThreadStarter(void *);
350 static THREAD_RESULT THREAD_CALL processingThreadStarter(void *);
5039dede 351
534e1b83
VK
352 void readThread();
353 void writeThread();
354 void processingThread();
355
356 void setupEncryption(CSCPMessage *request);
357 void respondToKeepalive(DWORD dwRqId);
358 void debugPrintf(int level, const TCHAR *format, ...);
359 void sendServerInfo(DWORD dwRqId);
360 void login(CSCPMessage *pRequest);
361 void updateDeviceInfo(CSCPMessage *pRequest);
362 void updateDeviceStatus(CSCPMessage *pRequest);
363
364public:
365 MobileDeviceSession(SOCKET hSocket, struct sockaddr *addr);
366 ~MobileDeviceSession();
367
368 void incRefCount() { m_dwRefCount++; }
369 void decRefCount() { if (m_dwRefCount > 0) m_dwRefCount--; }
370
371 void run();
372
373 void postMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
374 void sendMessage(CSCPMessage *pMsg);
5039dede 375
534e1b83
VK
376 DWORD getIndex() { return m_dwIndex; }
377 void setIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
378 int getState() { return m_iState; }
379 const TCHAR *getUserName() { return m_szUserName; }
380 const TCHAR *getClientInfo() { return m_szClientInfo; }
381 const TCHAR *getHostName() { return m_szHostName; }
382 DWORD getUserId() { return m_dwUserId; }
383 bool isAuthenticated() { return m_isAuthenticated; }
384 WORD getCurrentCmd() { return m_wCurrentCmd; }
385 int getCipher() { return (m_pCtx == NULL) ? -1 : m_pCtx->getCipher(); }
386};
387
388/**
389 * Client (user) session
390 */
5039dede
AK
391#define DECLARE_THREAD_STARTER(func) static THREAD_RESULT THREAD_CALL ThreadStarter_##func(void *);
392
a0e3917e 393class NXCORE_EXPORTABLE ClientSession
5039dede
AK
394{
395private:
396 SOCKET m_hSocket;
397 Queue *m_pSendQueue;
398 Queue *m_pMessageQueue;
399 Queue *m_pUpdateQueue;
400 DWORD m_dwIndex;
401 int m_iState;
402 WORD m_wCurrentCmd;
403 DWORD m_dwUserId;
404 DWORD m_dwSystemAccess; // User's system access rights
405 DWORD m_dwFlags; // Session flags
f8c6f10f 406 int m_clientType; // Client system type - desktop, web, mobile, etc.
5039dede 407 CSCP_BUFFER *m_pMsgBuffer;
98abc9f1 408 NXCPEncryptionContext *m_pCtx;
5039dede
AK
409 BYTE m_challenge[CLIENT_CHALLENGE_SIZE];
410 THREAD m_hWriteThread;
411 THREAD m_hProcessingThread;
412 THREAD m_hUpdateThread;
d3a7cf4c 413 MUTEX m_mutexSocketWrite;
5039dede
AK
414 MUTEX m_mutexSendEvents;
415 MUTEX m_mutexSendSyslog;
416 MUTEX m_mutexSendTrapLog;
417 MUTEX m_mutexSendObjects;
418 MUTEX m_mutexSendAlarms;
419 MUTEX m_mutexSendActions;
420 MUTEX m_mutexSendAuditLog;
421 MUTEX m_mutexSendSituations;
422 MUTEX m_mutexPollerInit;
60557d06
VK
423 struct sockaddr *m_clientAddr;
424 TCHAR m_szWorkstation[256]; // IP address of name of conneced host in textual form
5039dede
AK
425 TCHAR m_szUserName[MAX_SESSION_NAME]; // String in form login_name@host
426 TCHAR m_szClientInfo[96]; // Client app info string
427 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
428 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
429 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
430 DWORD m_dwRecordsUploaded;
431 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
432 int m_hCurrFile;
433 DWORD m_dwFileRqId;
434 DWORD m_dwUploadCommand;
435 DWORD m_dwUploadData;
436 TCHAR m_szCurrFileName[MAX_PATH];
437 DWORD m_dwRefCount;
438 DWORD m_dwEncryptionRqId;
439 DWORD m_dwEncryptionResult;
440 CONDITION m_condEncryptionSetup;
441 DWORD m_dwActiveChannels; // Active data channels
200d662d 442 CONSOLE_CTX m_console; // Server console context
5039dede 443
534e1b83
VK
444 static THREAD_RESULT THREAD_CALL readThreadStarter(void *);
445 static THREAD_RESULT THREAD_CALL writeThreadStarter(void *);
446 static THREAD_RESULT THREAD_CALL processingThreadStarter(void *);
447 static THREAD_RESULT THREAD_CALL updateThreadStarter(void *);
448 static THREAD_RESULT THREAD_CALL pollerThreadStarter(void *);
5039dede 449
92c51b1d
VK
450 DECLARE_THREAD_STARTER(getCollectedData)
451 DECLARE_THREAD_STARTER(getTableCollectedData)
452 DECLARE_THREAD_STARTER(clearDCIData)
adac28be
VK
453 DECLARE_THREAD_STARTER(queryParameter)
454 DECLARE_THREAD_STARTER(queryAgentTable)
84880c89 455 DECLARE_THREAD_STARTER(queryL2Topology)
b1e9b6b3
VK
456 DECLARE_THREAD_STARTER(sendEventLog)
457 DECLARE_THREAD_STARTER(sendSyslog)
1da08bc6
VK
458 DECLARE_THREAD_STARTER(createObject)
459 DECLARE_THREAD_STARTER(getServerFile)
6f16f12d 460 DECLARE_THREAD_STARTER(getAgentFile)
e05b1945
VK
461 DECLARE_THREAD_STARTER(queryServerLog)
462 DECLARE_THREAD_STARTER(getServerLogQueryData)
e31c8e60 463 DECLARE_THREAD_STARTER(executeAction)
630e15d6 464 DECLARE_THREAD_STARTER(findNodeConnection)
06a93345 465 DECLARE_THREAD_STARTER(findMacAddress)
80e0db05 466 DECLARE_THREAD_STARTER(findIpAddress)
200d662d 467 DECLARE_THREAD_STARTER(processConsoleCommand)
b08d31f4 468 DECLARE_THREAD_STARTER(sendMib)
e12e1422 469 DECLARE_THREAD_STARTER(getReportResults)
3932fde6 470 DECLARE_THREAD_STARTER(deleteReportResults)
e12e1422 471 DECLARE_THREAD_STARTER(renderReport)
6d069676 472 DECLARE_THREAD_STARTER(getNetworkPath)
5039dede 473
62768df3
VK
474 void readThread();
475 void writeThread();
476 void processingThread();
477 void updateThread();
478 void pollerThread(Node *pNode, int iPollType, DWORD dwRqId);
5039dede 479
9e9d631e 480 void setupEncryption(CSCPMessage *request);
711e5e9a 481 void respondToKeepalive(DWORD dwRqId);
e05b1945 482 void onFileUpload(BOOL bSuccess);
534e1b83 483 void debugPrintf(int level, const TCHAR *format, ...);
711e5e9a
VK
484 void sendServerInfo(DWORD dwRqId);
485 void login(CSCPMessage *pRequest);
62768df3
VK
486 void sendAllObjects(CSCPMessage *pRequest);
487 void sendSelectedObjects(CSCPMessage *pRequest);
b1e9b6b3
VK
488 void sendEventLog(CSCPMessage *pRequest);
489 void sendAllConfigVars(DWORD dwRqId);
490 void setConfigVariable(CSCPMessage *pRequest);
491 void deleteConfigVariable(CSCPMessage *pRequest);
492 void sendUserDB(DWORD dwRqId);
1add6d73 493 void sendAllAlarms(DWORD dwRqId);
b1e9b6b3
VK
494 void createUser(CSCPMessage *pRequest);
495 void updateUser(CSCPMessage *pRequest);
496 void deleteUser(CSCPMessage *pRequest);
497 void setPassword(CSCPMessage *pRequest);
498 void lockUserDB(DWORD dwRqId, BOOL bLock);
8eb2d92b
VK
499 void sendEventDB(DWORD dwRqId);
500 void modifyEventTemplate(CSCPMessage *pRequest);
501 void deleteEventTemplate(CSCPMessage *pRequest);
502 void generateEventCode(DWORD dwRqId);
92c51b1d 503 void modifyObject(CSCPMessage *pRequest);
1da08bc6 504 void changeObjectMgmtStatus(CSCPMessage *pRequest);
92c51b1d
VK
505 void openNodeDCIList(CSCPMessage *pRequest);
506 void closeNodeDCIList(CSCPMessage *pRequest);
507 void modifyNodeDCI(CSCPMessage *pRequest);
508 void copyDCI(CSCPMessage *pRequest);
509 void applyTemplate(CSCPMessage *pRequest);
510 void getCollectedData(CSCPMessage *pRequest);
511 void getTableCollectedData(CSCPMessage *pRequest);
6fd6de0a 512 bool getCollectedDataFromDB(CSCPMessage *request, CSCPMessage *response, DataCollectionTarget *object, int dciType);
92c51b1d
VK
513 void clearDCIData(CSCPMessage *pRequest);
514 void changeDCIStatus(CSCPMessage *pRequest);
b9a8e081
VK
515 void getLastValues(CSCPMessage *pRequest);
516 void getTableLastValues(CSCPMessage *pRequest);
711e5e9a 517 void getThresholdSummary(CSCPMessage *request);
92c51b1d
VK
518 void openEPP(DWORD dwRqId);
519 void closeEPP(DWORD dwRqId);
520 void saveEPP(CSCPMessage *pRequest);
521 void processEPPRecord(CSCPMessage *pRequest);
522 void sendMIBTimestamp(DWORD dwRqId);
b08d31f4 523 void sendMib(CSCPMessage *request);
b1e9b6b3
VK
524 void createObject(CSCPMessage *request);
525 void changeObjectBinding(CSCPMessage *request, BOOL bBind);
526 void deleteObject(CSCPMessage *request);
527 void getAlarm(CSCPMessage *request);
528 void getAlarmEvents(CSCPMessage *request);
529 void acknowledgeAlarm(CSCPMessage *request);
530 void resolveAlarm(CSCPMessage *request, bool terminate);
531 void deleteAlarm(CSCPMessage *request);
1b94acbf
VK
532 void getAlarmNotes(CSCPMessage *pRequest);
533 void updateAlarmNote(CSCPMessage *pRequest);
534 void createAction(CSCPMessage *pRequest);
535 void updateAction(CSCPMessage *pRequest);
536 void deleteAction(CSCPMessage *pRequest);
537 void sendAllActions(DWORD dwRqId);
5039dede 538 void SendContainerCategories(DWORD dwRqId);
92c51b1d 539 void forcedNodePoll(CSCPMessage *pRequest);
b1e9b6b3
VK
540 void onTrap(CSCPMessage *pRequest);
541 void onWakeUpNode(CSCPMessage *pRequest);
adac28be
VK
542 void queryParameter(CSCPMessage *pRequest);
543 void queryAgentTable(CSCPMessage *pRequest);
5039dede
AK
544 void EditTrap(int iOperation, CSCPMessage *pRequest);
545 void LockTrapCfg(DWORD dwRqId, BOOL bLock);
546 void SendAllTraps(DWORD dwRqId);
547 void SendAllTraps2(DWORD dwRqId);
548 void LockPackageDB(DWORD dwRqId, BOOL bLock);
549 void SendAllPackages(DWORD dwRqId);
550 void InstallPackage(CSCPMessage *pRequest);
551 void RemovePackage(CSCPMessage *pRequest);
552 void DeployPackage(CSCPMessage *pRequest);
553 void SendParametersList(CSCPMessage *pRequest);
554 void GetUserVariable(CSCPMessage *pRequest);
555 void SetUserVariable(CSCPMessage *pRequest);
556 void CopyUserVariable(CSCPMessage *pRequest);
557 void EnumUserVariables(CSCPMessage *pRequest);
558 void DeleteUserVariable(CSCPMessage *pRequest);
4c789f9e 559 void changeObjectZone(CSCPMessage *pRequest);
92c51b1d
VK
560 void getAgentConfig(CSCPMessage *pRequest);
561 void updateAgentConfig(CSCPMessage *pRequest);
e31c8e60 562 void executeAction(CSCPMessage *pRequest);
0e950f45
VK
563 void sendObjectTools(DWORD dwRqId);
564 void sendObjectToolDetails(CSCPMessage *pRequest);
565 void updateObjectTool(CSCPMessage *pRequest);
566 void deleteObjectTool(CSCPMessage *pRequest);
567 void generateObjectToolId(DWORD dwRqId);
568 void execTableTool(CSCPMessage *pRequest);
b1e9b6b3
VK
569 void changeSubscription(CSCPMessage *pRequest);
570 void sendSyslog(CSCPMessage *pRequest);
571 void sendServerStats(DWORD dwRqId);
0c7aca90
VK
572 void sendScriptList(DWORD dwRqId);
573 void sendScript(CSCPMessage *pRequest);
574 void updateScript(CSCPMessage *pRequest);
575 void renameScript(CSCPMessage *pRequest);
576 void deleteScript(CSCPMessage *pRequest);
5039dede
AK
577 void SendSessionList(DWORD dwRqId);
578 void KillSession(CSCPMessage *pRequest);
579 void SendTrapLog(CSCPMessage *pRequest);
580 void StartSnmpWalk(CSCPMessage *pRequest);
5039dede
AK
581 void ResolveDCINames(CSCPMessage *pRequest);
582 DWORD ResolveDCIName(DWORD dwNode, DWORD dwItem, TCHAR **ppszName);
583 void SendConfigForAgent(CSCPMessage *pRequest);
584 void SendAgentCfgList(DWORD dwRqId);
585 void OpenAgentConfig(CSCPMessage *pRequest);
586 void SaveAgentConfig(CSCPMessage *pRequest);
587 void DeleteAgentConfig(CSCPMessage *pRequest);
588 void SwapAgentConfigs(CSCPMessage *pRequest);
589 void SendObjectComments(CSCPMessage *pRequest);
6fd6de0a
VK
590 void updateObjectComments(CSCPMessage *pRequest);
591 void pushDCIData(CSCPMessage *pRequest);
592 void getAddrList(CSCPMessage *pRequest);
593 void setAddrList(CSCPMessage *pRequest);
92c51b1d 594 void resetComponent(CSCPMessage *pRequest);
7a46028e 595 void sendDCIEventList(CSCPMessage *request);
5039dede 596 void SendDCIInfo(CSCPMessage *pRequest);
74526d25 597 void sendPerfTabDCIList(CSCPMessage *pRequest);
a7ff20a5
VK
598 void exportConfiguration(CSCPMessage *pRequest);
599 void importConfiguration(CSCPMessage *pRequest);
5039dede
AK
600 void SendGraphList(DWORD dwRqId);
601 void DefineGraph(CSCPMessage *pRequest);
602 void DeleteGraph(CSCPMessage *pRequest);
603 void AddCACertificate(CSCPMessage *pRequest);
604 void DeleteCertificate(CSCPMessage *pRequest);
605 void UpdateCertificateComments(CSCPMessage *pRequest);
606 void SendCertificateList(DWORD dwRqId);
84880c89 607 void queryL2Topology(CSCPMessage *pRequest);
b1e9b6b3 608 void sendSMS(CSCPMessage *pRequest);
5039dede
AK
609 void SendCommunityList(DWORD dwRqId);
610 void UpdateCommunityList(CSCPMessage *pRequest);
611 void SendSituationList(DWORD dwRqId);
612 void CreateSituation(CSCPMessage *pRequest);
613 void UpdateSituation(CSCPMessage *pRequest);
614 void DeleteSituation(CSCPMessage *pRequest);
615 void DeleteSituationInstance(CSCPMessage *pRequest);
1da08bc6
VK
616 void setConfigCLOB(CSCPMessage *pRequest);
617 void getConfigCLOB(CSCPMessage *pRequest);
1da08bc6
VK
618 void registerAgent(CSCPMessage *pRequest);
619 void getServerFile(CSCPMessage *pRequest);
6f16f12d 620 void getAgentFile(CSCPMessage *pRequest);
1da08bc6 621 void testDCITransformation(CSCPMessage *pRequest);
45d84f8a
VK
622 void sendJobList(DWORD dwRqId);
623 void cancelJob(CSCPMessage *pRequest);
8d9f7450
VK
624 void holdJob(CSCPMessage *pRequest);
625 void unholdJob(CSCPMessage *pRequest);
93599cfd 626 void deployAgentPolicy(CSCPMessage *pRequest, bool uninstallFlag);
d83dc1f0
VK
627 void getUserCustomAttribute(CSCPMessage *request);
628 void setUserCustomAttribute(CSCPMessage *request);
e05b1945
VK
629 void openServerLog(CSCPMessage *request);
630 void closeServerLog(CSCPMessage *request);
631 void queryServerLog(CSCPMessage *request);
632 void getServerLogQueryData(CSCPMessage *request);
df8a4ca2
VK
633 void sendUsmCredentials(DWORD dwRqId);
634 void updateUsmCredentials(CSCPMessage *pRequest);
071fd171 635 void sendDCIThresholds(CSCPMessage *request);
1da08bc6 636 void addClusterNode(CSCPMessage *request);
630e15d6 637 void findNodeConnection(CSCPMessage *request);
06a93345 638 void findMacAddress(CSCPMessage *request);
80e0db05 639 void findIpAddress(CSCPMessage *request);
e6b9439a
AK
640 void sendLibraryImage(CSCPMessage *request);
641 void updateLibraryImage(CSCPMessage *request);
642 void listLibraryImages(CSCPMessage *request);
643 void deleteLibraryImage(CSCPMessage *request);
f69c6203 644 void executeServerCommand(CSCPMessage *request);
619e5c9b 645 void uploadFileToAgent(CSCPMessage *request);
1a5e0c22 646 void listServerFileStore(CSCPMessage *request);
200d662d
VK
647 void processConsoleCommand(CSCPMessage *msg);
648 void openConsole(DWORD rqId);
649 void closeConsole(DWORD rqId);
7f632dfe 650 void getVlans(CSCPMessage *msg);
68f898c9
VK
651 void receiveFile(CSCPMessage *request);
652 void deleteFile(CSCPMessage *request);
8c5b6ed3
VK
653 void executeReport(CSCPMessage *msg);
654 void getReportResults(CSCPMessage *msg);
3932fde6 655 void deleteReportResults(CSCPMessage *msg);
e12e1422 656 void renderReport(CSCPMessage *request);
6d069676 657 void getNetworkPath(CSCPMessage *request);
8836184f 658 void getNodeComponents(CSCPMessage *request);
5039dede
AK
659
660public:
60557d06 661 ClientSession(SOCKET hSocket, struct sockaddr *addr);
5039dede
AK
662 ~ClientSession();
663
e05b1945
VK
664 void incRefCount() { m_dwRefCount++; }
665 void decRefCount() { if (m_dwRefCount > 0) m_dwRefCount--; }
666
667 void run();
668
d3a7cf4c
VK
669 void postMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
670 void sendMessage(CSCPMessage *pMsg);
92c51b1d 671 void sendRawMessage(CSCP_MESSAGE *pMsg);
e05b1945
VK
672 void sendPollerMsg(DWORD dwRqId, const TCHAR *pszMsg);
673 BOOL sendFile(const TCHAR *file, DWORD dwRqId);
674
f669df41 675 DWORD getIndex() { return m_dwIndex; }
e05b1945 676 void setIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
f669df41 677 int getState() { return m_iState; }
889d7ff7
VK
678 const TCHAR *getUserName() { return m_szUserName; }
679 const TCHAR *getClientInfo() { return m_szClientInfo; }
680 const TCHAR *getWorkstation() { return m_szWorkstation; }
e05b1945
VK
681 DWORD getUserId() { return m_dwUserId; }
682 DWORD getSystemRights() { return m_dwSystemAccess; }
f669df41
VK
683 bool isAuthenticated() { return (m_dwFlags & CSF_AUTHENTICATED) ? true : false; }
684 bool isConsoleOpen() { return (m_dwFlags & CSF_CONSOLE_OPEN) ? true : false; }
685 bool isSubscribed(DWORD dwChannel) { return (m_dwActiveChannels & dwChannel) ? true : false; }
686 WORD getCurrentCmd() { return m_wCurrentCmd; }
98abc9f1 687 int getCipher() { return (m_pCtx == NULL) ? -1 : m_pCtx->getCipher(); }
f8c6f10f 688 int getClientType() { return m_clientType; }
e05b1945
VK
689
690 bool checkSysAccessRights(DWORD requiredAccess)
691 {
692 return m_dwUserId == 0 ? true :
693 ((requiredAccess & m_dwSystemAccess) ? true : false);
694 }
695
f669df41 696 void kill();
e05b1945
VK
697 void notify(DWORD dwCode, DWORD dwData = 0);
698
699 void queueUpdate(UPDATE_INFO *pUpdate) { m_pUpdateQueue->Put(pUpdate); }
700 void onNewEvent(Event *pEvent);
e0f99bf0 701 void onSyslogMessage(NX_SYSLOG_RECORD *pRec);
e05b1945
VK
702 void onNewSNMPTrap(CSCPMessage *pMsg);
703 void onObjectChange(NetObj *pObject);
704 void onUserDBUpdate(int code, DWORD id, UserDatabaseObject *user);
705 void onAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
706 void onActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
707 void onSituationChange(CSCPMessage *msg);
624f1165 708 void onLibraryImageChange(uuid_t *guid);
5039dede
AK
709};
710
711
7dad92fb
VK
712//
713// Delayed SQL request
714//
715
716typedef struct
717{
718 TCHAR *query;
719 int bindCount;
720 BYTE *sqlTypes;
721 TCHAR *bindings[1]; /* actual size determined by bindCount field */
722} DELAYED_SQL_REQUEST;
723
724
f68d9eac
VK
725//
726// Delayed request for idata_ INSERT
727//
728
729typedef struct
730{
731 time_t timestamp;
732 DWORD nodeId;
733 DWORD dciId;
734 TCHAR value[MAX_RESULT_LENGTH];
735} DELAYED_IDATA_INSERT;
736
737
5039dede
AK
738//
739// Functions
740//
741
35f836fe
VK
742BOOL NXCORE_EXPORTABLE ConfigReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
743#ifdef UNICODE
744BOOL NXCORE_EXPORTABLE ConfigReadStrA(const WCHAR *szVar, char *szBuffer, int iBufSize, const char *szDefault);
745#else
746#define ConfigReadStrA ConfigReadStr
747#endif
5039dede
AK
748int NXCORE_EXPORTABLE ConfigReadInt(const TCHAR *szVar, int iDefault);
749DWORD NXCORE_EXPORTABLE ConfigReadULong(const TCHAR *szVar, DWORD dwDefault);
750BOOL NXCORE_EXPORTABLE ConfigReadByteArray(const TCHAR *pszVar, int *pnArray,
751 int nSize, int nDefault);
752BOOL NXCORE_EXPORTABLE ConfigWriteStr(const TCHAR *szVar, const TCHAR *szValue, BOOL bCreate,
753 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
754BOOL NXCORE_EXPORTABLE ConfigWriteInt(const TCHAR *szVar, int iValue, BOOL bCreate,
755 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
756BOOL NXCORE_EXPORTABLE ConfigWriteULong(const TCHAR *szVar, DWORD dwValue, BOOL bCreate,
757 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
758BOOL NXCORE_EXPORTABLE ConfigWriteByteArray(const TCHAR *pszVar, int *pnArray,
759 int nSize, BOOL bCreate,
760 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
761TCHAR NXCORE_EXPORTABLE *ConfigReadCLOB(const TCHAR *var, const TCHAR *defValue);
762BOOL NXCORE_EXPORTABLE ConfigWriteCLOB(const TCHAR *var, const TCHAR *value, BOOL bCreate);
763
28f5b9a4
VK
764BOOL NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
765
35f836fe 766BOOL NXCORE_EXPORTABLE LoadConfig();
5039dede 767
35f836fe
VK
768void NXCORE_EXPORTABLE Shutdown();
769void NXCORE_EXPORTABLE FastShutdown();
770BOOL NXCORE_EXPORTABLE Initialize();
5039dede 771THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL Main(void *);
35f836fe
VK
772void NXCORE_EXPORTABLE ShutdownDB();
773void InitiateShutdown();
5039dede
AK
774
775BOOL NXCORE_EXPORTABLE SleepAndCheckForShutdown(int iSeconds);
776
35f836fe
VK
777void ConsolePrintf(CONSOLE_CTX pCtx, const TCHAR *pszFormat, ...);
778int ProcessConsoleCommand(const TCHAR *pszCmdLine, CONSOLE_CTX pCtx);
5039dede
AK
779
780void SaveObjects(DB_HANDLE hdb);
781
ce7565e7 782void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query);
7dad92fb 783void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query, int bindCount, int *sqlTypes, const TCHAR **values);
f68d9eac 784void QueueIDataInsert(time_t timestamp, DWORD nodeId, DWORD dciId, const TCHAR *value);
ce7565e7
VK
785void StartDBWriter();
786void StopDBWriter();
5039dede 787
8c08411e 788bool NXCORE_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, DWORD id);
ba89fed2 789
5039dede
AK
790void DecodeSQLStringAndSetVariable(CSCPMessage *pMsg, DWORD dwVarId, TCHAR *pszStr);
791
df8a4ca2 792SNMP_SecurityContext *SnmpCheckCommSettings(SNMP_Transport *pTransport, int *version, SNMP_SecurityContext *originalContext);
35f836fe 793void StrToMac(const TCHAR *pszStr, BYTE *pBuffer);
5039dede 794
f42b8099 795void InitLocalNetInfo();
5039dede 796
f42b8099 797ARP_CACHE *GetLocalArpCache();
5d2c5741 798ARP_CACHE *SnmpGetArpCache(DWORD dwVersion, SNMP_Transport *pTransport);
5039dede 799
98762401 800InterfaceList *GetLocalInterfaceList();
1e6b68a6 801void SnmpGetInterfaceStatus(DWORD dwVersion, SNMP_Transport *pTransport, DWORD dwIfIndex, int *adminState, int *operState);
5039dede 802
5d2c5741 803ROUTING_TABLE *SnmpGetRoutingTable(DWORD dwVersion, SNMP_Transport *pTransport);
5039dede 804
9796ce45 805void LoadNetworkDeviceDrivers();
21254a93 806NetworkDeviceDriver *FindDriverForNode(Node *node, SNMP_Transport *pTransport);
9796ce45 807
5ad2167d 808void WatchdogInit();
5039dede
AK
809DWORD WatchdogAddThread(const TCHAR *szName, time_t tNotifyInterval);
810void WatchdogNotify(DWORD dwId);
811void WatchdogPrintStatus(CONSOLE_CTX pCtx);
812
5ad2167d 813void CheckForMgmtNode();
f1b6417b
VK
814Node *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwCreationFlags, WORD agentPort,
815 WORD snmpPort, TCHAR *pszName, DWORD dwProxyNode, DWORD dwSNMPProxy, Cluster *pCluster,
a749a70a 816 DWORD zoneId, bool doConfPoll, bool discoveredNode);
5039dede 817
b1e9b6b3 818void NXCORE_EXPORTABLE EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
5039dede 819void NXCORE_EXPORTABLE NotifyClientSessions(DWORD dwCode, DWORD dwData);
5ad2167d 820int GetSessionCount();
5039dede 821
35f836fe
VK
822void GetSysInfoStr(TCHAR *pszBuffer, int nMaxSize);
823DWORD GetLocalIpAddr();
f7694811 824TCHAR *GetLocalHostName(TCHAR *buffer, size_t bufSize);
5039dede 825
35f836fe 826BOOL ExecCommand(TCHAR *pszCommand);
5039dede
AK
827BOOL SendMagicPacket(DWORD dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
828
35f836fe 829BOOL InitIdTable();
5039dede 830DWORD CreateUniqueId(int iGroup);
35f836fe 831QWORD CreateUniqueEventId();
5039dede 832
35f836fe
VK
833void InitMailer();
834void ShutdownMailer();
835void NXCORE_EXPORTABLE PostMail(const TCHAR *pszRcpt, const TCHAR *pszSubject, const TCHAR *pszText);
5039dede 836
35f836fe
VK
837void InitSMSSender();
838void ShutdownSMSSender();
839void NXCORE_EXPORTABLE PostSMS(const TCHAR *pszRcpt, const TCHAR *pszText);
5039dede 840
35f836fe 841void InitTraps();
5039dede
AK
842void SendTrapsToClient(ClientSession *pSession, DWORD dwRqId);
843void CreateTrapCfgMessage(CSCPMessage &msg);
844DWORD CreateNewTrap(DWORD *pdwTrapId);
845DWORD CreateNewTrap(NXC_TRAP_CFG_ENTRY *pTrap);
846DWORD UpdateTrapFromMsg(CSCPMessage *pMsg);
847DWORD DeleteTrap(DWORD dwId);
848void CreateNXMPTrapRecord(String &str, DWORD dwId);
849
850BOOL IsTableTool(DWORD dwToolId);
851BOOL CheckObjectToolAccess(DWORD dwToolId, DWORD dwUserId);
852DWORD ExecuteTableTool(DWORD dwToolId, Node *pNode, DWORD dwRqId, ClientSession *pSession);
853DWORD DeleteObjectToolFromDB(DWORD dwToolId);
854DWORD UpdateObjectToolFromMessage(CSCPMessage *pMsg);
855
e0f99bf0 856void CreateMessageFromSyslogMsg(CSCPMessage *pMsg, NX_SYSLOG_RECORD *pRec);
4262c0dc 857void ReinitializeSyslogParser();
5039dede
AK
858
859void EscapeString(String &str);
860
ce7565e7
VK
861void InitAuditLog();
862void NXCORE_EXPORTABLE WriteAuditLog(const TCHAR *subsys, BOOL isSuccess, DWORD userId,
863 const TCHAR *workstation, DWORD objectId,
864 const TCHAR *format, ...);
5039dede 865
a65c1819
VK
866bool ValidateConfig(Config *config, DWORD flags, TCHAR *errorText, int errorTextLen);
867DWORD ImportConfig(Config *config, DWORD flags);
5039dede
AK
868
869#ifdef _WITH_ENCRYPTION
870X509 *CertificateFromLoginMessage(CSCPMessage *pMsg);
a50eaebe 871BOOL ValidateUserCertificate(X509 *pCert, const TCHAR *pszLogin, BYTE *pChallenge,
5039dede 872 BYTE *pSignature, DWORD dwSigLen, int nMappingMethod,
a50eaebe 873 const TCHAR *pszMappingData);
9796ce45 874void ReloadCertificates();
5039dede
AK
875#endif
876
877#ifndef _WIN32
878THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL SignalHandler(void *);
879#endif /* not _WIN32 */
880
5039dede 881void DbgTestRWLock(RWLOCK hLock, const TCHAR *szName, CONSOLE_CTX pCtx);
534e1b83
VK
882void DumpClientSessions(CONSOLE_CTX pCtx);
883void DumpMobileDeviceSessions(CONSOLE_CTX pCtx);
5039dede 884void ShowPollerState(CONSOLE_CTX pCtx);
35f836fe 885void SetPollerInfo(int nIdx, const TCHAR *pszMsg);
5039dede 886void ShowServerStats(CONSOLE_CTX pCtx);
35f836fe 887void ShowQueueStats(CONSOLE_CTX pCtx, Queue *pQueue, const TCHAR *pszName);
5039dede
AK
888void DumpProcess(CONSOLE_CTX pCtx);
889
534e1b83
VK
890/**
891 * Global variables
892 */
5039dede
AK
893extern TCHAR NXCORE_EXPORTABLE g_szConfigFile[];
894extern TCHAR NXCORE_EXPORTABLE g_szLogFile[];
4f8998ca
VK
895extern DWORD g_dwLogRotationMode;
896extern DWORD g_dwMaxLogSize;
897extern DWORD g_dwLogHistorySize;
898extern TCHAR g_szDailyLogFileSuffix[64];
5039dede
AK
899extern TCHAR NXCORE_EXPORTABLE g_szDumpDir[];
900extern TCHAR NXCORE_EXPORTABLE g_szListenAddress[];
901#ifndef _WIN32
902extern TCHAR NXCORE_EXPORTABLE g_szPIDFile[];
903#endif
904extern TCHAR g_szDataDir[];
9796ce45 905extern TCHAR g_szLibDir[];
712dd47d 906extern TCHAR g_szJavaLibDir[];
73869331 907extern DWORD NXCORE_EXPORTABLE g_processAffinityMask;
5039dede
AK
908extern QWORD g_qwServerId;
909extern RSA *g_pServerKey;
910extern DWORD g_dwPingSize;
911extern DWORD g_dwAuditFlags;
912extern time_t g_tServerStartTime;
913extern DWORD g_dwLockTimeout;
5039dede
AK
914extern DWORD g_dwAgentCommandTimeout;
915extern DWORD g_dwThresholdRepeatInterval;
916extern int g_nRequiredPolls;
52c75f5c 917extern DWORD g_dwSlmPollingInterval;
5039dede 918
b8c1ec69
VK
919extern TCHAR g_szDbDriver[];
920extern TCHAR g_szDbDrvParams[];
921extern TCHAR g_szDbServer[];
922extern TCHAR g_szDbLogin[];
923extern TCHAR g_szDbPassword[];
924extern TCHAR g_szDbName[];
f3c30cf5 925extern TCHAR g_szDbSchema[];
712dd47d 926extern TCHAR NXCORE_EXPORTABLE g_szJavaPath[];
b8c1ec69 927extern DB_DRIVER g_dbDriver;
5039dede
AK
928extern DB_HANDLE NXCORE_EXPORTABLE g_hCoreDB;
929extern Queue *g_pLazyRequestQueue;
c9a9601c 930extern Queue *g_pIDataInsertQueue;
5039dede 931
28f5b9a4 932extern int NXCORE_EXPORTABLE g_nDBSyntax;
5039dede
AK
933
934#endif /* _nms_core_h_ */