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