latest changes from RCP console
[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
299
300//
301// Extended agent connection
302//
303
304class AgentConnectionEx : public AgentConnection
305{
306protected:
f480bdd4
VK
307 virtual void onTrap(CSCPMessage *msg);
308 virtual void onDataPush(CSCPMessage *msg);
5039dede
AK
309
310public:
5039dede
AK
311 AgentConnectionEx(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
312 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL) :
313 AgentConnection(dwAddr, wPort, iAuthMethod, pszSecret) { }
314 virtual ~AgentConnectionEx();
45d84f8a
VK
315
316 DWORD deployPolicy(AgentPolicy *policy);
93599cfd 317 DWORD uninstallPolicy(AgentPolicy *policy);
5039dede
AK
318};
319
320
321//
322// Client session
323//
324
325#define DECLARE_THREAD_STARTER(func) static THREAD_RESULT THREAD_CALL ThreadStarter_##func(void *);
326
a0e3917e 327class NXCORE_EXPORTABLE ClientSession
5039dede
AK
328{
329private:
330 SOCKET m_hSocket;
331 Queue *m_pSendQueue;
332 Queue *m_pMessageQueue;
333 Queue *m_pUpdateQueue;
334 DWORD m_dwIndex;
335 int m_iState;
336 WORD m_wCurrentCmd;
337 DWORD m_dwUserId;
338 DWORD m_dwSystemAccess; // User's system access rights
339 DWORD m_dwFlags; // Session flags
f8c6f10f 340 int m_clientType; // Client system type - desktop, web, mobile, etc.
5039dede 341 CSCP_BUFFER *m_pMsgBuffer;
98abc9f1 342 NXCPEncryptionContext *m_pCtx;
5039dede
AK
343 BYTE m_challenge[CLIENT_CHALLENGE_SIZE];
344 THREAD m_hWriteThread;
345 THREAD m_hProcessingThread;
346 THREAD m_hUpdateThread;
d3a7cf4c 347 MUTEX m_mutexSocketWrite;
5039dede
AK
348 MUTEX m_mutexSendEvents;
349 MUTEX m_mutexSendSyslog;
350 MUTEX m_mutexSendTrapLog;
351 MUTEX m_mutexSendObjects;
352 MUTEX m_mutexSendAlarms;
353 MUTEX m_mutexSendActions;
354 MUTEX m_mutexSendAuditLog;
355 MUTEX m_mutexSendSituations;
356 MUTEX m_mutexPollerInit;
60557d06
VK
357 struct sockaddr *m_clientAddr;
358 TCHAR m_szWorkstation[256]; // IP address of name of conneced host in textual form
5039dede
AK
359 TCHAR m_szUserName[MAX_SESSION_NAME]; // String in form login_name@host
360 TCHAR m_szClientInfo[96]; // Client app info string
361 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
362 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
363 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
364 DWORD m_dwRecordsUploaded;
365 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
366 int m_hCurrFile;
367 DWORD m_dwFileRqId;
368 DWORD m_dwUploadCommand;
369 DWORD m_dwUploadData;
370 TCHAR m_szCurrFileName[MAX_PATH];
371 DWORD m_dwRefCount;
372 DWORD m_dwEncryptionRqId;
373 DWORD m_dwEncryptionResult;
374 CONDITION m_condEncryptionSetup;
375 DWORD m_dwActiveChannels; // Active data channels
200d662d 376 CONSOLE_CTX m_console; // Server console context
5039dede
AK
377
378 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
379 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
380 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
381 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
382 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
383
92c51b1d
VK
384 DECLARE_THREAD_STARTER(getCollectedData)
385 DECLARE_THREAD_STARTER(getTableCollectedData)
386 DECLARE_THREAD_STARTER(clearDCIData)
adac28be
VK
387 DECLARE_THREAD_STARTER(queryParameter)
388 DECLARE_THREAD_STARTER(queryAgentTable)
84880c89 389 DECLARE_THREAD_STARTER(queryL2Topology)
5039dede
AK
390 DECLARE_THREAD_STARTER(SendEventLog)
391 DECLARE_THREAD_STARTER(SendSyslog)
1da08bc6
VK
392 DECLARE_THREAD_STARTER(createObject)
393 DECLARE_THREAD_STARTER(getServerFile)
6f16f12d 394 DECLARE_THREAD_STARTER(getAgentFile)
e05b1945
VK
395 DECLARE_THREAD_STARTER(queryServerLog)
396 DECLARE_THREAD_STARTER(getServerLogQueryData)
e31c8e60 397 DECLARE_THREAD_STARTER(executeAction)
630e15d6 398 DECLARE_THREAD_STARTER(findNodeConnection)
06a93345 399 DECLARE_THREAD_STARTER(findMacAddress)
80e0db05 400 DECLARE_THREAD_STARTER(findIpAddress)
200d662d 401 DECLARE_THREAD_STARTER(processConsoleCommand)
b08d31f4 402 DECLARE_THREAD_STARTER(sendMib)
e12e1422 403 DECLARE_THREAD_STARTER(getReportResults)
3932fde6 404 DECLARE_THREAD_STARTER(deleteReportResults)
e12e1422 405 DECLARE_THREAD_STARTER(renderReport)
6d069676 406 DECLARE_THREAD_STARTER(getNetworkPath)
5039dede 407
62768df3
VK
408 void readThread();
409 void writeThread();
410 void processingThread();
411 void updateThread();
412 void pollerThread(Node *pNode, int iPollType, DWORD dwRqId);
5039dede 413
9e9d631e 414 void setupEncryption(CSCPMessage *request);
711e5e9a 415 void respondToKeepalive(DWORD dwRqId);
e05b1945 416 void onFileUpload(BOOL bSuccess);
5039dede 417 void DebugPrintf(int level, const TCHAR *format, ...);
711e5e9a
VK
418 void sendServerInfo(DWORD dwRqId);
419 void login(CSCPMessage *pRequest);
62768df3
VK
420 void sendAllObjects(CSCPMessage *pRequest);
421 void sendSelectedObjects(CSCPMessage *pRequest);
5039dede
AK
422 void SendEventLog(CSCPMessage *pRequest);
423 void SendAllConfigVars(DWORD dwRqId);
424 void SetConfigVariable(CSCPMessage *pRequest);
425 void DeleteConfigVariable(CSCPMessage *pRequest);
426 void SendUserDB(DWORD dwRqId);
1add6d73 427 void sendAllAlarms(DWORD dwRqId);
5039dede
AK
428 void CreateUser(CSCPMessage *pRequest);
429 void UpdateUser(CSCPMessage *pRequest);
430 void DeleteUser(CSCPMessage *pRequest);
431 void SetPassword(CSCPMessage *pRequest);
432 void LockUserDB(DWORD dwRqId, BOOL bLock);
8eb2d92b
VK
433 void sendEventDB(DWORD dwRqId);
434 void modifyEventTemplate(CSCPMessage *pRequest);
435 void deleteEventTemplate(CSCPMessage *pRequest);
436 void generateEventCode(DWORD dwRqId);
92c51b1d 437 void modifyObject(CSCPMessage *pRequest);
1da08bc6 438 void changeObjectMgmtStatus(CSCPMessage *pRequest);
92c51b1d
VK
439 void openNodeDCIList(CSCPMessage *pRequest);
440 void closeNodeDCIList(CSCPMessage *pRequest);
441 void modifyNodeDCI(CSCPMessage *pRequest);
442 void copyDCI(CSCPMessage *pRequest);
443 void applyTemplate(CSCPMessage *pRequest);
444 void getCollectedData(CSCPMessage *pRequest);
445 void getTableCollectedData(CSCPMessage *pRequest);
6fd6de0a 446 bool getCollectedDataFromDB(CSCPMessage *request, CSCPMessage *response, DataCollectionTarget *object, int dciType);
92c51b1d
VK
447 void clearDCIData(CSCPMessage *pRequest);
448 void changeDCIStatus(CSCPMessage *pRequest);
b9a8e081
VK
449 void getLastValues(CSCPMessage *pRequest);
450 void getTableLastValues(CSCPMessage *pRequest);
711e5e9a 451 void getThresholdSummary(CSCPMessage *request);
92c51b1d
VK
452 void openEPP(DWORD dwRqId);
453 void closeEPP(DWORD dwRqId);
454 void saveEPP(CSCPMessage *pRequest);
455 void processEPPRecord(CSCPMessage *pRequest);
456 void sendMIBTimestamp(DWORD dwRqId);
b08d31f4 457 void sendMib(CSCPMessage *request);
1da08bc6
VK
458 void createObject(CSCPMessage *pRequest);
459 void changeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
f5d16551 460 void deleteObject(CSCPMessage *pRequest);
4d00760d 461 void getAlarm(CSCPMessage *pRequest);
1add6d73 462 void acknowledgeAlarm(CSCPMessage *pRequest);
5f6bc78c 463 void resolveAlarm(CSCPMessage *pRequest, bool terminate);
1add6d73 464 void deleteAlarm(CSCPMessage *pRequest);
1b94acbf
VK
465 void getAlarmNotes(CSCPMessage *pRequest);
466 void updateAlarmNote(CSCPMessage *pRequest);
467 void createAction(CSCPMessage *pRequest);
468 void updateAction(CSCPMessage *pRequest);
469 void deleteAction(CSCPMessage *pRequest);
470 void sendAllActions(DWORD dwRqId);
5039dede 471 void SendContainerCategories(DWORD dwRqId);
92c51b1d 472 void forcedNodePoll(CSCPMessage *pRequest);
5039dede
AK
473 void OnTrap(CSCPMessage *pRequest);
474 void OnWakeUpNode(CSCPMessage *pRequest);
adac28be
VK
475 void queryParameter(CSCPMessage *pRequest);
476 void queryAgentTable(CSCPMessage *pRequest);
5039dede
AK
477 void EditTrap(int iOperation, CSCPMessage *pRequest);
478 void LockTrapCfg(DWORD dwRqId, BOOL bLock);
479 void SendAllTraps(DWORD dwRqId);
480 void SendAllTraps2(DWORD dwRqId);
481 void LockPackageDB(DWORD dwRqId, BOOL bLock);
482 void SendAllPackages(DWORD dwRqId);
483 void InstallPackage(CSCPMessage *pRequest);
484 void RemovePackage(CSCPMessage *pRequest);
485 void DeployPackage(CSCPMessage *pRequest);
486 void SendParametersList(CSCPMessage *pRequest);
487 void GetUserVariable(CSCPMessage *pRequest);
488 void SetUserVariable(CSCPMessage *pRequest);
489 void CopyUserVariable(CSCPMessage *pRequest);
490 void EnumUserVariables(CSCPMessage *pRequest);
491 void DeleteUserVariable(CSCPMessage *pRequest);
4c789f9e 492 void changeObjectZone(CSCPMessage *pRequest);
92c51b1d
VK
493 void getAgentConfig(CSCPMessage *pRequest);
494 void updateAgentConfig(CSCPMessage *pRequest);
e31c8e60 495 void executeAction(CSCPMessage *pRequest);
0e950f45
VK
496 void sendObjectTools(DWORD dwRqId);
497 void sendObjectToolDetails(CSCPMessage *pRequest);
498 void updateObjectTool(CSCPMessage *pRequest);
499 void deleteObjectTool(CSCPMessage *pRequest);
500 void generateObjectToolId(DWORD dwRqId);
501 void execTableTool(CSCPMessage *pRequest);
5039dede
AK
502 void ChangeSubscription(CSCPMessage *pRequest);
503 void SendSyslog(CSCPMessage *pRequest);
5039dede 504 void SendServerStats(DWORD dwRqId);
0c7aca90
VK
505 void sendScriptList(DWORD dwRqId);
506 void sendScript(CSCPMessage *pRequest);
507 void updateScript(CSCPMessage *pRequest);
508 void renameScript(CSCPMessage *pRequest);
509 void deleteScript(CSCPMessage *pRequest);
5039dede
AK
510 void SendSessionList(DWORD dwRqId);
511 void KillSession(CSCPMessage *pRequest);
512 void SendTrapLog(CSCPMessage *pRequest);
513 void StartSnmpWalk(CSCPMessage *pRequest);
5039dede
AK
514 void ResolveDCINames(CSCPMessage *pRequest);
515 DWORD ResolveDCIName(DWORD dwNode, DWORD dwItem, TCHAR **ppszName);
516 void SendConfigForAgent(CSCPMessage *pRequest);
517 void SendAgentCfgList(DWORD dwRqId);
518 void OpenAgentConfig(CSCPMessage *pRequest);
519 void SaveAgentConfig(CSCPMessage *pRequest);
520 void DeleteAgentConfig(CSCPMessage *pRequest);
521 void SwapAgentConfigs(CSCPMessage *pRequest);
522 void SendObjectComments(CSCPMessage *pRequest);
6fd6de0a
VK
523 void updateObjectComments(CSCPMessage *pRequest);
524 void pushDCIData(CSCPMessage *pRequest);
525 void getAddrList(CSCPMessage *pRequest);
526 void setAddrList(CSCPMessage *pRequest);
92c51b1d 527 void resetComponent(CSCPMessage *pRequest);
7a46028e 528 void sendDCIEventList(CSCPMessage *request);
5039dede 529 void SendDCIInfo(CSCPMessage *pRequest);
74526d25 530 void sendPerfTabDCIList(CSCPMessage *pRequest);
a7ff20a5
VK
531 void exportConfiguration(CSCPMessage *pRequest);
532 void importConfiguration(CSCPMessage *pRequest);
5039dede
AK
533 void SendGraphList(DWORD dwRqId);
534 void DefineGraph(CSCPMessage *pRequest);
535 void DeleteGraph(CSCPMessage *pRequest);
536 void AddCACertificate(CSCPMessage *pRequest);
537 void DeleteCertificate(CSCPMessage *pRequest);
538 void UpdateCertificateComments(CSCPMessage *pRequest);
539 void SendCertificateList(DWORD dwRqId);
84880c89 540 void queryL2Topology(CSCPMessage *pRequest);
5039dede
AK
541 void SendSMS(CSCPMessage *pRequest);
542 void SendCommunityList(DWORD dwRqId);
543 void UpdateCommunityList(CSCPMessage *pRequest);
544 void SendSituationList(DWORD dwRqId);
545 void CreateSituation(CSCPMessage *pRequest);
546 void UpdateSituation(CSCPMessage *pRequest);
547 void DeleteSituation(CSCPMessage *pRequest);
548 void DeleteSituationInstance(CSCPMessage *pRequest);
1da08bc6
VK
549 void setConfigCLOB(CSCPMessage *pRequest);
550 void getConfigCLOB(CSCPMessage *pRequest);
1da08bc6
VK
551 void registerAgent(CSCPMessage *pRequest);
552 void getServerFile(CSCPMessage *pRequest);
6f16f12d 553 void getAgentFile(CSCPMessage *pRequest);
1da08bc6 554 void testDCITransformation(CSCPMessage *pRequest);
45d84f8a
VK
555 void sendJobList(DWORD dwRqId);
556 void cancelJob(CSCPMessage *pRequest);
8d9f7450
VK
557 void holdJob(CSCPMessage *pRequest);
558 void unholdJob(CSCPMessage *pRequest);
93599cfd 559 void deployAgentPolicy(CSCPMessage *pRequest, bool uninstallFlag);
d83dc1f0
VK
560 void getUserCustomAttribute(CSCPMessage *request);
561 void setUserCustomAttribute(CSCPMessage *request);
e05b1945
VK
562 void openServerLog(CSCPMessage *request);
563 void closeServerLog(CSCPMessage *request);
564 void queryServerLog(CSCPMessage *request);
565 void getServerLogQueryData(CSCPMessage *request);
df8a4ca2
VK
566 void sendUsmCredentials(DWORD dwRqId);
567 void updateUsmCredentials(CSCPMessage *pRequest);
071fd171 568 void sendDCIThresholds(CSCPMessage *request);
1da08bc6 569 void addClusterNode(CSCPMessage *request);
630e15d6 570 void findNodeConnection(CSCPMessage *request);
06a93345 571 void findMacAddress(CSCPMessage *request);
80e0db05 572 void findIpAddress(CSCPMessage *request);
e6b9439a
AK
573 void sendLibraryImage(CSCPMessage *request);
574 void updateLibraryImage(CSCPMessage *request);
575 void listLibraryImages(CSCPMessage *request);
576 void deleteLibraryImage(CSCPMessage *request);
f69c6203 577 void executeServerCommand(CSCPMessage *request);
619e5c9b 578 void uploadFileToAgent(CSCPMessage *request);
1a5e0c22 579 void listServerFileStore(CSCPMessage *request);
200d662d
VK
580 void processConsoleCommand(CSCPMessage *msg);
581 void openConsole(DWORD rqId);
582 void closeConsole(DWORD rqId);
7f632dfe 583 void getVlans(CSCPMessage *msg);
68f898c9
VK
584 void receiveFile(CSCPMessage *request);
585 void deleteFile(CSCPMessage *request);
8c5b6ed3
VK
586 void executeReport(CSCPMessage *msg);
587 void getReportResults(CSCPMessage *msg);
3932fde6 588 void deleteReportResults(CSCPMessage *msg);
e12e1422 589 void renderReport(CSCPMessage *request);
6d069676 590 void getNetworkPath(CSCPMessage *request);
8836184f 591 void getNodeComponents(CSCPMessage *request);
5039dede
AK
592
593public:
60557d06 594 ClientSession(SOCKET hSocket, struct sockaddr *addr);
5039dede
AK
595 ~ClientSession();
596
e05b1945
VK
597 void incRefCount() { m_dwRefCount++; }
598 void decRefCount() { if (m_dwRefCount > 0) m_dwRefCount--; }
599
600 void run();
601
d3a7cf4c
VK
602 void postMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
603 void sendMessage(CSCPMessage *pMsg);
92c51b1d 604 void sendRawMessage(CSCP_MESSAGE *pMsg);
e05b1945
VK
605 void sendPollerMsg(DWORD dwRqId, const TCHAR *pszMsg);
606 BOOL sendFile(const TCHAR *file, DWORD dwRqId);
607
f669df41 608 DWORD getIndex() { return m_dwIndex; }
e05b1945 609 void setIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
f669df41 610 int getState() { return m_iState; }
889d7ff7
VK
611 const TCHAR *getUserName() { return m_szUserName; }
612 const TCHAR *getClientInfo() { return m_szClientInfo; }
613 const TCHAR *getWorkstation() { return m_szWorkstation; }
e05b1945
VK
614 DWORD getUserId() { return m_dwUserId; }
615 DWORD getSystemRights() { return m_dwSystemAccess; }
f669df41
VK
616 bool isAuthenticated() { return (m_dwFlags & CSF_AUTHENTICATED) ? true : false; }
617 bool isConsoleOpen() { return (m_dwFlags & CSF_CONSOLE_OPEN) ? true : false; }
618 bool isSubscribed(DWORD dwChannel) { return (m_dwActiveChannels & dwChannel) ? true : false; }
619 WORD getCurrentCmd() { return m_wCurrentCmd; }
98abc9f1 620 int getCipher() { return (m_pCtx == NULL) ? -1 : m_pCtx->getCipher(); }
f8c6f10f 621 int getClientType() { return m_clientType; }
e05b1945
VK
622
623 bool checkSysAccessRights(DWORD requiredAccess)
624 {
625 return m_dwUserId == 0 ? true :
626 ((requiredAccess & m_dwSystemAccess) ? true : false);
627 }
628
f669df41 629 void kill();
e05b1945
VK
630 void notify(DWORD dwCode, DWORD dwData = 0);
631
632 void queueUpdate(UPDATE_INFO *pUpdate) { m_pUpdateQueue->Put(pUpdate); }
633 void onNewEvent(Event *pEvent);
e0f99bf0 634 void onSyslogMessage(NX_SYSLOG_RECORD *pRec);
e05b1945
VK
635 void onNewSNMPTrap(CSCPMessage *pMsg);
636 void onObjectChange(NetObj *pObject);
637 void onUserDBUpdate(int code, DWORD id, UserDatabaseObject *user);
638 void onAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
639 void onActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
640 void onSituationChange(CSCPMessage *msg);
624f1165 641 void onLibraryImageChange(uuid_t *guid);
5039dede
AK
642};
643
644
7dad92fb
VK
645//
646// Delayed SQL request
647//
648
649typedef struct
650{
651 TCHAR *query;
652 int bindCount;
653 BYTE *sqlTypes;
654 TCHAR *bindings[1]; /* actual size determined by bindCount field */
655} DELAYED_SQL_REQUEST;
656
657
f68d9eac
VK
658//
659// Delayed request for idata_ INSERT
660//
661
662typedef struct
663{
664 time_t timestamp;
665 DWORD nodeId;
666 DWORD dciId;
667 TCHAR value[MAX_RESULT_LENGTH];
668} DELAYED_IDATA_INSERT;
669
670
5039dede
AK
671//
672// Functions
673//
674
35f836fe
VK
675BOOL NXCORE_EXPORTABLE ConfigReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
676#ifdef UNICODE
677BOOL NXCORE_EXPORTABLE ConfigReadStrA(const WCHAR *szVar, char *szBuffer, int iBufSize, const char *szDefault);
678#else
679#define ConfigReadStrA ConfigReadStr
680#endif
5039dede
AK
681int NXCORE_EXPORTABLE ConfigReadInt(const TCHAR *szVar, int iDefault);
682DWORD NXCORE_EXPORTABLE ConfigReadULong(const TCHAR *szVar, DWORD dwDefault);
683BOOL NXCORE_EXPORTABLE ConfigReadByteArray(const TCHAR *pszVar, int *pnArray,
684 int nSize, int nDefault);
685BOOL NXCORE_EXPORTABLE ConfigWriteStr(const TCHAR *szVar, const TCHAR *szValue, BOOL bCreate,
686 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
687BOOL NXCORE_EXPORTABLE ConfigWriteInt(const TCHAR *szVar, int iValue, BOOL bCreate,
688 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
689BOOL NXCORE_EXPORTABLE ConfigWriteULong(const TCHAR *szVar, DWORD dwValue, BOOL bCreate,
690 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
691BOOL NXCORE_EXPORTABLE ConfigWriteByteArray(const TCHAR *pszVar, int *pnArray,
692 int nSize, BOOL bCreate,
693 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
694TCHAR NXCORE_EXPORTABLE *ConfigReadCLOB(const TCHAR *var, const TCHAR *defValue);
695BOOL NXCORE_EXPORTABLE ConfigWriteCLOB(const TCHAR *var, const TCHAR *value, BOOL bCreate);
696
28f5b9a4
VK
697BOOL NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
698
35f836fe 699BOOL NXCORE_EXPORTABLE LoadConfig();
5039dede 700
35f836fe
VK
701void NXCORE_EXPORTABLE Shutdown();
702void NXCORE_EXPORTABLE FastShutdown();
703BOOL NXCORE_EXPORTABLE Initialize();
5039dede 704THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL Main(void *);
35f836fe
VK
705void NXCORE_EXPORTABLE ShutdownDB();
706void InitiateShutdown();
5039dede
AK
707
708BOOL NXCORE_EXPORTABLE SleepAndCheckForShutdown(int iSeconds);
709
35f836fe
VK
710void ConsolePrintf(CONSOLE_CTX pCtx, const TCHAR *pszFormat, ...);
711int ProcessConsoleCommand(const TCHAR *pszCmdLine, CONSOLE_CTX pCtx);
5039dede
AK
712
713void SaveObjects(DB_HANDLE hdb);
714
ce7565e7 715void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query);
7dad92fb 716void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query, int bindCount, int *sqlTypes, const TCHAR **values);
f68d9eac 717void QueueIDataInsert(time_t timestamp, DWORD nodeId, DWORD dciId, const TCHAR *value);
ce7565e7
VK
718void StartDBWriter();
719void StopDBWriter();
5039dede 720
8c08411e 721bool NXCORE_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, DWORD id);
ba89fed2 722
5039dede
AK
723void DecodeSQLStringAndSetVariable(CSCPMessage *pMsg, DWORD dwVarId, TCHAR *pszStr);
724
df8a4ca2 725SNMP_SecurityContext *SnmpCheckCommSettings(SNMP_Transport *pTransport, int *version, SNMP_SecurityContext *originalContext);
35f836fe 726void StrToMac(const TCHAR *pszStr, BYTE *pBuffer);
5039dede 727
f42b8099 728void InitLocalNetInfo();
5039dede 729
f42b8099 730ARP_CACHE *GetLocalArpCache();
5d2c5741 731ARP_CACHE *SnmpGetArpCache(DWORD dwVersion, SNMP_Transport *pTransport);
5039dede 732
98762401 733InterfaceList *GetLocalInterfaceList();
1e6b68a6 734void SnmpGetInterfaceStatus(DWORD dwVersion, SNMP_Transport *pTransport, DWORD dwIfIndex, int *adminState, int *operState);
5039dede 735
5d2c5741 736ROUTING_TABLE *SnmpGetRoutingTable(DWORD dwVersion, SNMP_Transport *pTransport);
5039dede 737
9796ce45 738void LoadNetworkDeviceDrivers();
21254a93 739NetworkDeviceDriver *FindDriverForNode(Node *node, SNMP_Transport *pTransport);
9796ce45 740
5ad2167d 741void WatchdogInit();
5039dede
AK
742DWORD WatchdogAddThread(const TCHAR *szName, time_t tNotifyInterval);
743void WatchdogNotify(DWORD dwId);
744void WatchdogPrintStatus(CONSOLE_CTX pCtx);
745
5ad2167d 746void CheckForMgmtNode();
f1b6417b
VK
747Node *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwCreationFlags, WORD agentPort,
748 WORD snmpPort, TCHAR *pszName, DWORD dwProxyNode, DWORD dwSNMPProxy, Cluster *pCluster,
a749a70a 749 DWORD zoneId, bool doConfPoll, bool discoveredNode);
5039dede
AK
750
751void NXCORE_EXPORTABLE EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
752void NXCORE_EXPORTABLE NotifyClientSessions(DWORD dwCode, DWORD dwData);
5ad2167d 753int GetSessionCount();
5039dede 754
35f836fe
VK
755void GetSysInfoStr(TCHAR *pszBuffer, int nMaxSize);
756DWORD GetLocalIpAddr();
f7694811 757TCHAR *GetLocalHostName(TCHAR *buffer, size_t bufSize);
5039dede 758
35f836fe 759BOOL ExecCommand(TCHAR *pszCommand);
5039dede
AK
760BOOL SendMagicPacket(DWORD dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
761
35f836fe 762BOOL InitIdTable();
5039dede 763DWORD CreateUniqueId(int iGroup);
35f836fe 764QWORD CreateUniqueEventId();
5039dede 765
35f836fe
VK
766void InitMailer();
767void ShutdownMailer();
768void NXCORE_EXPORTABLE PostMail(const TCHAR *pszRcpt, const TCHAR *pszSubject, const TCHAR *pszText);
5039dede 769
35f836fe
VK
770void InitSMSSender();
771void ShutdownSMSSender();
772void NXCORE_EXPORTABLE PostSMS(const TCHAR *pszRcpt, const TCHAR *pszText);
5039dede 773
35f836fe 774void InitTraps();
5039dede
AK
775void SendTrapsToClient(ClientSession *pSession, DWORD dwRqId);
776void CreateTrapCfgMessage(CSCPMessage &msg);
777DWORD CreateNewTrap(DWORD *pdwTrapId);
778DWORD CreateNewTrap(NXC_TRAP_CFG_ENTRY *pTrap);
779DWORD UpdateTrapFromMsg(CSCPMessage *pMsg);
780DWORD DeleteTrap(DWORD dwId);
781void CreateNXMPTrapRecord(String &str, DWORD dwId);
782
783BOOL IsTableTool(DWORD dwToolId);
784BOOL CheckObjectToolAccess(DWORD dwToolId, DWORD dwUserId);
785DWORD ExecuteTableTool(DWORD dwToolId, Node *pNode, DWORD dwRqId, ClientSession *pSession);
786DWORD DeleteObjectToolFromDB(DWORD dwToolId);
787DWORD UpdateObjectToolFromMessage(CSCPMessage *pMsg);
788
e0f99bf0 789void CreateMessageFromSyslogMsg(CSCPMessage *pMsg, NX_SYSLOG_RECORD *pRec);
4262c0dc 790void ReinitializeSyslogParser();
5039dede
AK
791
792void EscapeString(String &str);
793
ce7565e7
VK
794void InitAuditLog();
795void NXCORE_EXPORTABLE WriteAuditLog(const TCHAR *subsys, BOOL isSuccess, DWORD userId,
796 const TCHAR *workstation, DWORD objectId,
797 const TCHAR *format, ...);
5039dede 798
a65c1819
VK
799bool ValidateConfig(Config *config, DWORD flags, TCHAR *errorText, int errorTextLen);
800DWORD ImportConfig(Config *config, DWORD flags);
5039dede
AK
801
802#ifdef _WITH_ENCRYPTION
803X509 *CertificateFromLoginMessage(CSCPMessage *pMsg);
a50eaebe 804BOOL ValidateUserCertificate(X509 *pCert, const TCHAR *pszLogin, BYTE *pChallenge,
5039dede 805 BYTE *pSignature, DWORD dwSigLen, int nMappingMethod,
a50eaebe 806 const TCHAR *pszMappingData);
9796ce45 807void ReloadCertificates();
5039dede
AK
808#endif
809
810#ifndef _WIN32
811THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL SignalHandler(void *);
812#endif /* not _WIN32 */
813
5039dede
AK
814void DbgTestRWLock(RWLOCK hLock, const TCHAR *szName, CONSOLE_CTX pCtx);
815void DumpSessions(CONSOLE_CTX pCtx);
816void ShowPollerState(CONSOLE_CTX pCtx);
35f836fe 817void SetPollerInfo(int nIdx, const TCHAR *pszMsg);
5039dede 818void ShowServerStats(CONSOLE_CTX pCtx);
35f836fe 819void ShowQueueStats(CONSOLE_CTX pCtx, Queue *pQueue, const TCHAR *pszName);
5039dede
AK
820void DumpProcess(CONSOLE_CTX pCtx);
821
822
823//
824// Global variables
825//
826
827extern TCHAR NXCORE_EXPORTABLE g_szConfigFile[];
828extern TCHAR NXCORE_EXPORTABLE g_szLogFile[];
4f8998ca
VK
829extern DWORD g_dwLogRotationMode;
830extern DWORD g_dwMaxLogSize;
831extern DWORD g_dwLogHistorySize;
832extern TCHAR g_szDailyLogFileSuffix[64];
5039dede
AK
833extern TCHAR NXCORE_EXPORTABLE g_szDumpDir[];
834extern TCHAR NXCORE_EXPORTABLE g_szListenAddress[];
835#ifndef _WIN32
836extern TCHAR NXCORE_EXPORTABLE g_szPIDFile[];
837#endif
838extern TCHAR g_szDataDir[];
9796ce45 839extern TCHAR g_szLibDir[];
712dd47d 840extern TCHAR g_szJavaLibDir[];
73869331 841extern DWORD NXCORE_EXPORTABLE g_processAffinityMask;
5039dede
AK
842extern QWORD g_qwServerId;
843extern RSA *g_pServerKey;
844extern DWORD g_dwPingSize;
845extern DWORD g_dwAuditFlags;
846extern time_t g_tServerStartTime;
847extern DWORD g_dwLockTimeout;
5039dede
AK
848extern DWORD g_dwAgentCommandTimeout;
849extern DWORD g_dwThresholdRepeatInterval;
850extern int g_nRequiredPolls;
52c75f5c 851extern DWORD g_dwSlmPollingInterval;
5039dede 852
b8c1ec69
VK
853extern TCHAR g_szDbDriver[];
854extern TCHAR g_szDbDrvParams[];
855extern TCHAR g_szDbServer[];
856extern TCHAR g_szDbLogin[];
857extern TCHAR g_szDbPassword[];
858extern TCHAR g_szDbName[];
f3c30cf5 859extern TCHAR g_szDbSchema[];
712dd47d 860extern TCHAR NXCORE_EXPORTABLE g_szJavaPath[];
b8c1ec69 861extern DB_DRIVER g_dbDriver;
5039dede
AK
862extern DB_HANDLE NXCORE_EXPORTABLE g_hCoreDB;
863extern Queue *g_pLazyRequestQueue;
c9a9601c 864extern Queue *g_pIDataInsertQueue;
5039dede 865
28f5b9a4 866extern int NXCORE_EXPORTABLE g_nDBSyntax;
5039dede
AK
867
868#endif /* _nms_core_h_ */