dial charts improved
[public/netxms.git] / src / server / include / nms_core.h
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
85f1fea1 3** Copyright (C) 2003-2011 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
5039dede
AK
157
158
159//
160// Exit codes for console commands
161//
162
163#define CMD_EXIT_CONTINUE 0
164#define CMD_EXIT_CLOSE_SESSION 1
165#define CMD_EXIT_SHUTDOWN 2
166
167
168//
169// Network discovery mode
170//
171
172#define DISCOVERY_DISABLED 0
173#define DISCOVERY_PASSIVE_ONLY 1
174#define DISCOVERY_ACTIVE 2
175
176
177//
178// Client session flags
179//
180
5039dede
AK
181#define CSF_EPP_LOCKED ((DWORD)0x0002)
182#define CSF_PACKAGE_DB_LOCKED ((DWORD)0x0004)
183#define CSF_USER_DB_LOCKED ((DWORD)0x0008)
184#define CSF_EPP_UPLOAD ((DWORD)0x0010)
200d662d 185#define CSF_CONSOLE_OPEN ((DWORD)0x0020)
5039dede 186#define CSF_AUTHENTICATED ((DWORD)0x0080)
5039dede
AK
187#define CSF_RECEIVING_MAP_DATA ((DWORD)0x0200)
188#define CSF_SYNC_OBJECT_COMMENTS ((DWORD)0x0400)
189
190
191//
192// Client session states
193//
194
195#define SESSION_STATE_INIT 0
196#define SESSION_STATE_IDLE 1
197#define SESSION_STATE_PROCESSING 2
198
199
200//
201// Information categories for UPDATE_INFO structure
202//
203
204#define INFO_CAT_EVENT 1
205#define INFO_CAT_OBJECT_CHANGE 2
206#define INFO_CAT_ALARM 3
207#define INFO_CAT_ACTION 4
208#define INFO_CAT_SYSLOG_MSG 5
209#define INFO_CAT_SNMP_TRAP 6
210#define INFO_CAT_AUDIT_RECORD 7
211#define INFO_CAT_SITUATION 8
212
213
214//
215// Certificate types
216//
217
218#define CERT_TYPE_TRUSTED_CA 0
219#define CERT_TYPE_USER 1
220
221
222//
223// Audit subsystems
224//
225
226#define AUDIT_SECURITY _T("SECURITY")
227#define AUDIT_OBJECTS _T("OBJECTS")
228#define AUDIT_SYSCFG _T("SYSCFG")
229#define AUDIT_CONSOLE _T("CONSOLE")
230
231
232//
233// Event handling subsystem definitions
234//
235
236#include "nms_events.h"
237#include "nms_actions.h"
238#include "nms_alarm.h"
239
240
241//
242// New node information
243//
244
245typedef struct
246{
247 DWORD dwIpAddr;
248 DWORD dwNetMask;
dc25b21c 249 DWORD zoneId;
5039dede
AK
250 BOOL ignoreFilter;
251} NEW_NODE;
252
253
254//
255// New node flags
256//
257
258#define NNF_IS_SNMP 0x0001
259#define NNF_IS_AGENT 0x0002
260#define NNF_IS_ROUTER 0x0004
261#define NNF_IS_BRIDGE 0x0008
262#define NNF_IS_PRINTER 0x0010
263#define NNF_IS_CDP 0x0020
264#define NNF_IS_SONMP 0x0040
265#define NNF_IS_LLDP 0x0080
266
267
268//
269// Node information for autodiscovery filter
270//
271
272typedef struct
273{
274 DWORD dwIpAddr;
275 DWORD dwNetMask;
276 DWORD dwSubnetAddr;
277 DWORD dwFlags;
278 int nSNMPVersion;
35f836fe
VK
279 TCHAR szObjectId[MAX_OID_LEN * 4]; // SNMP OID
280 TCHAR szAgentVersion[MAX_AGENT_VERSION_LEN];
281 TCHAR szPlatform[MAX_PLATFORM_NAME_LEN];
5039dede
AK
282} DISCOVERY_FILTER_DATA;
283
284
285//
286// Data update structure for client sessions
287//
288
289typedef struct
290{
291 DWORD dwCategory; // Data category - event, network object, etc.
292 DWORD dwCode; // Data-specific update code
293 void *pData; // Pointer to data block
294} UPDATE_INFO;
295
296
297//
298// Extended agent connection
299//
300
301class AgentConnectionEx : public AgentConnection
302{
303protected:
f480bdd4
VK
304 virtual void onTrap(CSCPMessage *msg);
305 virtual void onDataPush(CSCPMessage *msg);
5039dede
AK
306
307public:
5039dede
AK
308 AgentConnectionEx(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
309 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL) :
310 AgentConnection(dwAddr, wPort, iAuthMethod, pszSecret) { }
311 virtual ~AgentConnectionEx();
45d84f8a
VK
312
313 DWORD deployPolicy(AgentPolicy *policy);
93599cfd 314 DWORD uninstallPolicy(AgentPolicy *policy);
5039dede
AK
315};
316
317
318//
319// Client session
320//
321
322#define DECLARE_THREAD_STARTER(func) static THREAD_RESULT THREAD_CALL ThreadStarter_##func(void *);
323
a0e3917e 324class NXCORE_EXPORTABLE ClientSession
5039dede
AK
325{
326private:
327 SOCKET m_hSocket;
328 Queue *m_pSendQueue;
329 Queue *m_pMessageQueue;
330 Queue *m_pUpdateQueue;
331 DWORD m_dwIndex;
332 int m_iState;
333 WORD m_wCurrentCmd;
334 DWORD m_dwUserId;
335 DWORD m_dwSystemAccess; // User's system access rights
336 DWORD m_dwFlags; // Session flags
337 CSCP_BUFFER *m_pMsgBuffer;
98abc9f1 338 NXCPEncryptionContext *m_pCtx;
5039dede
AK
339 BYTE m_challenge[CLIENT_CHALLENGE_SIZE];
340 THREAD m_hWriteThread;
341 THREAD m_hProcessingThread;
342 THREAD m_hUpdateThread;
d3a7cf4c 343 MUTEX m_mutexSocketWrite;
5039dede
AK
344 MUTEX m_mutexSendEvents;
345 MUTEX m_mutexSendSyslog;
346 MUTEX m_mutexSendTrapLog;
347 MUTEX m_mutexSendObjects;
348 MUTEX m_mutexSendAlarms;
349 MUTEX m_mutexSendActions;
350 MUTEX m_mutexSendAuditLog;
351 MUTEX m_mutexSendSituations;
352 MUTEX m_mutexPollerInit;
60557d06
VK
353 struct sockaddr *m_clientAddr;
354 TCHAR m_szWorkstation[256]; // IP address of name of conneced host in textual form
5039dede
AK
355 TCHAR m_szUserName[MAX_SESSION_NAME]; // String in form login_name@host
356 TCHAR m_szClientInfo[96]; // Client app info string
357 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
358 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
359 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
360 DWORD m_dwRecordsUploaded;
361 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
362 int m_hCurrFile;
363 DWORD m_dwFileRqId;
364 DWORD m_dwUploadCommand;
365 DWORD m_dwUploadData;
366 TCHAR m_szCurrFileName[MAX_PATH];
367 DWORD m_dwRefCount;
368 DWORD m_dwEncryptionRqId;
369 DWORD m_dwEncryptionResult;
370 CONDITION m_condEncryptionSetup;
371 DWORD m_dwActiveChannels; // Active data channels
200d662d 372 CONSOLE_CTX m_console; // Server console context
5039dede
AK
373
374 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
375 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
376 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
377 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
378 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
379
380 DECLARE_THREAD_STARTER(GetCollectedData)
381 DECLARE_THREAD_STARTER(ClearDCIData)
adac28be
VK
382 DECLARE_THREAD_STARTER(queryParameter)
383 DECLARE_THREAD_STARTER(queryAgentTable)
5039dede
AK
384 DECLARE_THREAD_STARTER(QueryL2Topology)
385 DECLARE_THREAD_STARTER(SendEventLog)
386 DECLARE_THREAD_STARTER(SendSyslog)
1da08bc6
VK
387 DECLARE_THREAD_STARTER(createObject)
388 DECLARE_THREAD_STARTER(getServerFile)
6f16f12d 389 DECLARE_THREAD_STARTER(getAgentFile)
e05b1945
VK
390 DECLARE_THREAD_STARTER(queryServerLog)
391 DECLARE_THREAD_STARTER(getServerLogQueryData)
e31c8e60 392 DECLARE_THREAD_STARTER(executeAction)
630e15d6 393 DECLARE_THREAD_STARTER(findNodeConnection)
06a93345 394 DECLARE_THREAD_STARTER(findMacAddress)
200d662d 395 DECLARE_THREAD_STARTER(processConsoleCommand)
b08d31f4 396 DECLARE_THREAD_STARTER(sendMib)
e12e1422 397 DECLARE_THREAD_STARTER(getReportResults)
3932fde6 398 DECLARE_THREAD_STARTER(deleteReportResults)
e12e1422 399 DECLARE_THREAD_STARTER(renderReport)
6d069676 400 DECLARE_THREAD_STARTER(getNetworkPath)
5039dede 401
62768df3
VK
402 void readThread();
403 void writeThread();
404 void processingThread();
405 void updateThread();
406 void pollerThread(Node *pNode, int iPollType, DWORD dwRqId);
5039dede 407
e05b1945 408 void setupEncryption(DWORD dwRqId);
5039dede 409 void RespondToKeepalive(DWORD dwRqId);
e05b1945 410 void onFileUpload(BOOL bSuccess);
5039dede
AK
411 void DebugPrintf(int level, const TCHAR *format, ...);
412 void SendServerInfo(DWORD dwRqId);
413 void Login(CSCPMessage *pRequest);
62768df3
VK
414 void sendAllObjects(CSCPMessage *pRequest);
415 void sendSelectedObjects(CSCPMessage *pRequest);
5039dede
AK
416 void SendEventLog(CSCPMessage *pRequest);
417 void SendAllConfigVars(DWORD dwRqId);
418 void SetConfigVariable(CSCPMessage *pRequest);
419 void DeleteConfigVariable(CSCPMessage *pRequest);
420 void SendUserDB(DWORD dwRqId);
421 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
422 void CreateUser(CSCPMessage *pRequest);
423 void UpdateUser(CSCPMessage *pRequest);
424 void DeleteUser(CSCPMessage *pRequest);
425 void SetPassword(CSCPMessage *pRequest);
426 void LockUserDB(DWORD dwRqId, BOOL bLock);
8eb2d92b
VK
427 void sendEventDB(DWORD dwRqId);
428 void modifyEventTemplate(CSCPMessage *pRequest);
429 void deleteEventTemplate(CSCPMessage *pRequest);
430 void generateEventCode(DWORD dwRqId);
5039dede 431 void ModifyObject(CSCPMessage *pRequest);
1da08bc6 432 void changeObjectMgmtStatus(CSCPMessage *pRequest);
5039dede
AK
433 void OpenNodeDCIList(CSCPMessage *pRequest);
434 void CloseNodeDCIList(CSCPMessage *pRequest);
435 void ModifyNodeDCI(CSCPMessage *pRequest);
436 void CopyDCI(CSCPMessage *pRequest);
437 void ApplyTemplate(CSCPMessage *pRequest);
438 void GetCollectedData(CSCPMessage *pRequest);
439 void ClearDCIData(CSCPMessage *pRequest);
440 void ChangeDCIStatus(CSCPMessage *pRequest);
441 void SendLastValues(CSCPMessage *pRequest);
442 void OpenEPP(DWORD dwRqId);
443 void CloseEPP(DWORD dwRqId);
444 void SaveEPP(CSCPMessage *pRequest);
445 void ProcessEPPRecord(CSCPMessage *pRequest);
446 void SendMIBTimestamp(DWORD dwRqId);
b08d31f4 447 void sendMib(CSCPMessage *request);
1da08bc6
VK
448 void createObject(CSCPMessage *pRequest);
449 void changeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
f5d16551 450 void deleteObject(CSCPMessage *pRequest);
5039dede
AK
451 void AcknowledgeAlarm(CSCPMessage *pRequest);
452 void TerminateAlarm(CSCPMessage *pRequest);
453 void DeleteAlarm(CSCPMessage *pRequest);
454 void CreateAction(CSCPMessage *pRequest);
455 void UpdateAction(CSCPMessage *pRequest);
456 void DeleteAction(CSCPMessage *pRequest);
5039dede
AK
457 void SendAllActions(DWORD dwRqId);
458 void SendContainerCategories(DWORD dwRqId);
459 void ForcedNodePoll(CSCPMessage *pRequest);
460 void OnTrap(CSCPMessage *pRequest);
461 void OnWakeUpNode(CSCPMessage *pRequest);
adac28be
VK
462 void queryParameter(CSCPMessage *pRequest);
463 void queryAgentTable(CSCPMessage *pRequest);
5039dede
AK
464 void EditTrap(int iOperation, CSCPMessage *pRequest);
465 void LockTrapCfg(DWORD dwRqId, BOOL bLock);
466 void SendAllTraps(DWORD dwRqId);
467 void SendAllTraps2(DWORD dwRqId);
468 void LockPackageDB(DWORD dwRqId, BOOL bLock);
469 void SendAllPackages(DWORD dwRqId);
470 void InstallPackage(CSCPMessage *pRequest);
471 void RemovePackage(CSCPMessage *pRequest);
472 void DeployPackage(CSCPMessage *pRequest);
473 void SendParametersList(CSCPMessage *pRequest);
474 void GetUserVariable(CSCPMessage *pRequest);
475 void SetUserVariable(CSCPMessage *pRequest);
476 void CopyUserVariable(CSCPMessage *pRequest);
477 void EnumUserVariables(CSCPMessage *pRequest);
478 void DeleteUserVariable(CSCPMessage *pRequest);
4c789f9e 479 void changeObjectZone(CSCPMessage *pRequest);
5039dede
AK
480 void GetAgentConfig(CSCPMessage *pRequest);
481 void UpdateAgentConfig(CSCPMessage *pRequest);
e31c8e60 482 void executeAction(CSCPMessage *pRequest);
0e950f45
VK
483 void sendObjectTools(DWORD dwRqId);
484 void sendObjectToolDetails(CSCPMessage *pRequest);
485 void updateObjectTool(CSCPMessage *pRequest);
486 void deleteObjectTool(CSCPMessage *pRequest);
487 void generateObjectToolId(DWORD dwRqId);
488 void execTableTool(CSCPMessage *pRequest);
5039dede
AK
489 void ChangeSubscription(CSCPMessage *pRequest);
490 void SendSyslog(CSCPMessage *pRequest);
5039dede 491 void SendServerStats(DWORD dwRqId);
0c7aca90
VK
492 void sendScriptList(DWORD dwRqId);
493 void sendScript(CSCPMessage *pRequest);
494 void updateScript(CSCPMessage *pRequest);
495 void renameScript(CSCPMessage *pRequest);
496 void deleteScript(CSCPMessage *pRequest);
5039dede
AK
497 void SendSessionList(DWORD dwRqId);
498 void KillSession(CSCPMessage *pRequest);
499 void SendTrapLog(CSCPMessage *pRequest);
500 void StartSnmpWalk(CSCPMessage *pRequest);
5039dede
AK
501 void ResolveDCINames(CSCPMessage *pRequest);
502 DWORD ResolveDCIName(DWORD dwNode, DWORD dwItem, TCHAR **ppszName);
503 void SendConfigForAgent(CSCPMessage *pRequest);
504 void SendAgentCfgList(DWORD dwRqId);
505 void OpenAgentConfig(CSCPMessage *pRequest);
506 void SaveAgentConfig(CSCPMessage *pRequest);
507 void DeleteAgentConfig(CSCPMessage *pRequest);
508 void SwapAgentConfigs(CSCPMessage *pRequest);
509 void SendObjectComments(CSCPMessage *pRequest);
510 void UpdateObjectComments(CSCPMessage *pRequest);
511 void PushDCIData(CSCPMessage *pRequest);
512 void GetAddrList(CSCPMessage *pRequest);
513 void SetAddrList(CSCPMessage *pRequest);
514 void ResetComponent(CSCPMessage *pRequest);
7a46028e 515 void sendDCIEventList(CSCPMessage *request);
5039dede 516 void SendDCIInfo(CSCPMessage *pRequest);
74526d25 517 void sendPerfTabDCIList(CSCPMessage *pRequest);
a7ff20a5
VK
518 void exportConfiguration(CSCPMessage *pRequest);
519 void importConfiguration(CSCPMessage *pRequest);
5039dede
AK
520 void SendGraphList(DWORD dwRqId);
521 void DefineGraph(CSCPMessage *pRequest);
522 void DeleteGraph(CSCPMessage *pRequest);
523 void AddCACertificate(CSCPMessage *pRequest);
524 void DeleteCertificate(CSCPMessage *pRequest);
525 void UpdateCertificateComments(CSCPMessage *pRequest);
526 void SendCertificateList(DWORD dwRqId);
527 void QueryL2Topology(CSCPMessage *pRequest);
528 void SendSMS(CSCPMessage *pRequest);
529 void SendCommunityList(DWORD dwRqId);
530 void UpdateCommunityList(CSCPMessage *pRequest);
531 void SendSituationList(DWORD dwRqId);
532 void CreateSituation(CSCPMessage *pRequest);
533 void UpdateSituation(CSCPMessage *pRequest);
534 void DeleteSituation(CSCPMessage *pRequest);
535 void DeleteSituationInstance(CSCPMessage *pRequest);
1da08bc6
VK
536 void setConfigCLOB(CSCPMessage *pRequest);
537 void getConfigCLOB(CSCPMessage *pRequest);
1da08bc6
VK
538 void registerAgent(CSCPMessage *pRequest);
539 void getServerFile(CSCPMessage *pRequest);
6f16f12d 540 void getAgentFile(CSCPMessage *pRequest);
1da08bc6 541 void testDCITransformation(CSCPMessage *pRequest);
45d84f8a
VK
542 void sendJobList(DWORD dwRqId);
543 void cancelJob(CSCPMessage *pRequest);
8d9f7450
VK
544 void holdJob(CSCPMessage *pRequest);
545 void unholdJob(CSCPMessage *pRequest);
93599cfd 546 void deployAgentPolicy(CSCPMessage *pRequest, bool uninstallFlag);
d83dc1f0
VK
547 void getUserCustomAttribute(CSCPMessage *request);
548 void setUserCustomAttribute(CSCPMessage *request);
e05b1945
VK
549 void openServerLog(CSCPMessage *request);
550 void closeServerLog(CSCPMessage *request);
551 void queryServerLog(CSCPMessage *request);
552 void getServerLogQueryData(CSCPMessage *request);
df8a4ca2
VK
553 void sendUsmCredentials(DWORD dwRqId);
554 void updateUsmCredentials(CSCPMessage *pRequest);
071fd171 555 void sendDCIThresholds(CSCPMessage *request);
1da08bc6 556 void addClusterNode(CSCPMessage *request);
630e15d6 557 void findNodeConnection(CSCPMessage *request);
06a93345 558 void findMacAddress(CSCPMessage *request);
e6b9439a
AK
559 void sendLibraryImage(CSCPMessage *request);
560 void updateLibraryImage(CSCPMessage *request);
561 void listLibraryImages(CSCPMessage *request);
562 void deleteLibraryImage(CSCPMessage *request);
f69c6203 563 void executeServerCommand(CSCPMessage *request);
619e5c9b 564 void uploadFileToAgent(CSCPMessage *request);
1a5e0c22 565 void listServerFileStore(CSCPMessage *request);
200d662d
VK
566 void processConsoleCommand(CSCPMessage *msg);
567 void openConsole(DWORD rqId);
568 void closeConsole(DWORD rqId);
7f632dfe 569 void getVlans(CSCPMessage *msg);
68f898c9
VK
570 void receiveFile(CSCPMessage *request);
571 void deleteFile(CSCPMessage *request);
8c5b6ed3
VK
572 void executeReport(CSCPMessage *msg);
573 void getReportResults(CSCPMessage *msg);
3932fde6 574 void deleteReportResults(CSCPMessage *msg);
e12e1422 575 void renderReport(CSCPMessage *request);
6d069676 576 void getNetworkPath(CSCPMessage *request);
5039dede
AK
577
578public:
60557d06 579 ClientSession(SOCKET hSocket, struct sockaddr *addr);
5039dede
AK
580 ~ClientSession();
581
e05b1945
VK
582 void incRefCount() { m_dwRefCount++; }
583 void decRefCount() { if (m_dwRefCount > 0) m_dwRefCount--; }
584
585 void run();
586
d3a7cf4c
VK
587 void postMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
588 void sendMessage(CSCPMessage *pMsg);
e05b1945
VK
589 void sendPollerMsg(DWORD dwRqId, const TCHAR *pszMsg);
590 BOOL sendFile(const TCHAR *file, DWORD dwRqId);
591
f669df41 592 DWORD getIndex() { return m_dwIndex; }
e05b1945 593 void setIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
f669df41 594 int getState() { return m_iState; }
889d7ff7
VK
595 const TCHAR *getUserName() { return m_szUserName; }
596 const TCHAR *getClientInfo() { return m_szClientInfo; }
597 const TCHAR *getWorkstation() { return m_szWorkstation; }
e05b1945
VK
598 DWORD getUserId() { return m_dwUserId; }
599 DWORD getSystemRights() { return m_dwSystemAccess; }
f669df41
VK
600 bool isAuthenticated() { return (m_dwFlags & CSF_AUTHENTICATED) ? true : false; }
601 bool isConsoleOpen() { return (m_dwFlags & CSF_CONSOLE_OPEN) ? true : false; }
602 bool isSubscribed(DWORD dwChannel) { return (m_dwActiveChannels & dwChannel) ? true : false; }
603 WORD getCurrentCmd() { return m_wCurrentCmd; }
98abc9f1 604 int getCipher() { return (m_pCtx == NULL) ? -1 : m_pCtx->getCipher(); }
e05b1945
VK
605
606 bool checkSysAccessRights(DWORD requiredAccess)
607 {
608 return m_dwUserId == 0 ? true :
609 ((requiredAccess & m_dwSystemAccess) ? true : false);
610 }
611
f669df41 612 void kill();
e05b1945
VK
613 void notify(DWORD dwCode, DWORD dwData = 0);
614
615 void queueUpdate(UPDATE_INFO *pUpdate) { m_pUpdateQueue->Put(pUpdate); }
616 void onNewEvent(Event *pEvent);
e0f99bf0 617 void onSyslogMessage(NX_SYSLOG_RECORD *pRec);
e05b1945
VK
618 void onNewSNMPTrap(CSCPMessage *pMsg);
619 void onObjectChange(NetObj *pObject);
620 void onUserDBUpdate(int code, DWORD id, UserDatabaseObject *user);
621 void onAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
622 void onActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
623 void onSituationChange(CSCPMessage *msg);
5039dede
AK
624};
625
626
7dad92fb
VK
627//
628// Delayed SQL request
629//
630
631typedef struct
632{
633 TCHAR *query;
634 int bindCount;
635 BYTE *sqlTypes;
636 TCHAR *bindings[1]; /* actual size determined by bindCount field */
637} DELAYED_SQL_REQUEST;
638
639
5039dede
AK
640//
641// Functions
642//
643
35f836fe
VK
644BOOL NXCORE_EXPORTABLE ConfigReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
645#ifdef UNICODE
646BOOL NXCORE_EXPORTABLE ConfigReadStrA(const WCHAR *szVar, char *szBuffer, int iBufSize, const char *szDefault);
647#else
648#define ConfigReadStrA ConfigReadStr
649#endif
5039dede
AK
650int NXCORE_EXPORTABLE ConfigReadInt(const TCHAR *szVar, int iDefault);
651DWORD NXCORE_EXPORTABLE ConfigReadULong(const TCHAR *szVar, DWORD dwDefault);
652BOOL NXCORE_EXPORTABLE ConfigReadByteArray(const TCHAR *pszVar, int *pnArray,
653 int nSize, int nDefault);
654BOOL NXCORE_EXPORTABLE ConfigWriteStr(const TCHAR *szVar, const TCHAR *szValue, BOOL bCreate,
655 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
656BOOL NXCORE_EXPORTABLE ConfigWriteInt(const TCHAR *szVar, int iValue, BOOL bCreate,
657 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
658BOOL NXCORE_EXPORTABLE ConfigWriteULong(const TCHAR *szVar, DWORD dwValue, BOOL bCreate,
659 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
660BOOL NXCORE_EXPORTABLE ConfigWriteByteArray(const TCHAR *pszVar, int *pnArray,
661 int nSize, BOOL bCreate,
662 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
663TCHAR NXCORE_EXPORTABLE *ConfigReadCLOB(const TCHAR *var, const TCHAR *defValue);
664BOOL NXCORE_EXPORTABLE ConfigWriteCLOB(const TCHAR *var, const TCHAR *value, BOOL bCreate);
665
28f5b9a4
VK
666BOOL NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
667
35f836fe 668BOOL NXCORE_EXPORTABLE LoadConfig();
5039dede 669
35f836fe
VK
670void NXCORE_EXPORTABLE Shutdown();
671void NXCORE_EXPORTABLE FastShutdown();
672BOOL NXCORE_EXPORTABLE Initialize();
5039dede 673THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL Main(void *);
35f836fe
VK
674void NXCORE_EXPORTABLE ShutdownDB();
675void InitiateShutdown();
5039dede
AK
676
677BOOL NXCORE_EXPORTABLE SleepAndCheckForShutdown(int iSeconds);
678
35f836fe
VK
679void ConsolePrintf(CONSOLE_CTX pCtx, const TCHAR *pszFormat, ...);
680int ProcessConsoleCommand(const TCHAR *pszCmdLine, CONSOLE_CTX pCtx);
5039dede
AK
681
682void SaveObjects(DB_HANDLE hdb);
683
ce7565e7 684void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query);
7dad92fb 685void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query, int bindCount, int *sqlTypes, const TCHAR **values);
ce7565e7
VK
686void StartDBWriter();
687void StopDBWriter();
5039dede
AK
688
689void DecodeSQLStringAndSetVariable(CSCPMessage *pMsg, DWORD dwVarId, TCHAR *pszStr);
690
df8a4ca2 691SNMP_SecurityContext *SnmpCheckCommSettings(SNMP_Transport *pTransport, int *version, SNMP_SecurityContext *originalContext);
35f836fe 692void StrToMac(const TCHAR *pszStr, BYTE *pBuffer);
5039dede 693
f42b8099 694void InitLocalNetInfo();
5039dede 695
f42b8099 696ARP_CACHE *GetLocalArpCache();
5d2c5741 697ARP_CACHE *SnmpGetArpCache(DWORD dwVersion, SNMP_Transport *pTransport);
5039dede 698
98762401 699InterfaceList *GetLocalInterfaceList();
5d2c5741 700int SnmpGetInterfaceStatus(DWORD dwVersion, SNMP_Transport *pTransport, DWORD dwIfIndex);
5039dede 701
5d2c5741 702ROUTING_TABLE *SnmpGetRoutingTable(DWORD dwVersion, SNMP_Transport *pTransport);
5039dede 703
9796ce45
VK
704void LoadNetworkDeviceDrivers();
705NetworkDeviceDriver *FindDriverForNode(Node *node);
706
5ad2167d 707void WatchdogInit();
5039dede
AK
708DWORD WatchdogAddThread(const TCHAR *szName, time_t tNotifyInterval);
709void WatchdogNotify(DWORD dwId);
710void WatchdogPrintStatus(CONSOLE_CTX pCtx);
711
5ad2167d 712void CheckForMgmtNode();
07f6978d 713Node *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwCreationFlags,
588f7351
VK
714 TCHAR *pszName, DWORD dwProxyNode, DWORD dwSNMPProxy, Cluster *pCluster,
715 DWORD zoneId, bool doConfPoll);
5039dede
AK
716
717void NXCORE_EXPORTABLE EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
718void NXCORE_EXPORTABLE NotifyClientSessions(DWORD dwCode, DWORD dwData);
5ad2167d 719int GetSessionCount();
5039dede 720
35f836fe
VK
721void GetSysInfoStr(TCHAR *pszBuffer, int nMaxSize);
722DWORD GetLocalIpAddr();
f7694811 723TCHAR *GetLocalHostName(TCHAR *buffer, size_t bufSize);
5039dede 724
35f836fe 725BOOL ExecCommand(TCHAR *pszCommand);
5039dede
AK
726BOOL SendMagicPacket(DWORD dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
727
35f836fe 728BOOL InitIdTable();
5039dede 729DWORD CreateUniqueId(int iGroup);
35f836fe 730QWORD CreateUniqueEventId();
5039dede 731
35f836fe
VK
732void InitMailer();
733void ShutdownMailer();
734void NXCORE_EXPORTABLE PostMail(const TCHAR *pszRcpt, const TCHAR *pszSubject, const TCHAR *pszText);
5039dede 735
35f836fe
VK
736void InitSMSSender();
737void ShutdownSMSSender();
738void NXCORE_EXPORTABLE PostSMS(const TCHAR *pszRcpt, const TCHAR *pszText);
5039dede 739
35f836fe 740void InitTraps();
5039dede
AK
741void SendTrapsToClient(ClientSession *pSession, DWORD dwRqId);
742void CreateTrapCfgMessage(CSCPMessage &msg);
743DWORD CreateNewTrap(DWORD *pdwTrapId);
744DWORD CreateNewTrap(NXC_TRAP_CFG_ENTRY *pTrap);
745DWORD UpdateTrapFromMsg(CSCPMessage *pMsg);
746DWORD DeleteTrap(DWORD dwId);
747void CreateNXMPTrapRecord(String &str, DWORD dwId);
748
749BOOL IsTableTool(DWORD dwToolId);
750BOOL CheckObjectToolAccess(DWORD dwToolId, DWORD dwUserId);
751DWORD ExecuteTableTool(DWORD dwToolId, Node *pNode, DWORD dwRqId, ClientSession *pSession);
752DWORD DeleteObjectToolFromDB(DWORD dwToolId);
753DWORD UpdateObjectToolFromMessage(CSCPMessage *pMsg);
754
e0f99bf0 755void CreateMessageFromSyslogMsg(CSCPMessage *pMsg, NX_SYSLOG_RECORD *pRec);
4262c0dc 756void ReinitializeSyslogParser();
5039dede
AK
757
758void EscapeString(String &str);
759
ce7565e7
VK
760void InitAuditLog();
761void NXCORE_EXPORTABLE WriteAuditLog(const TCHAR *subsys, BOOL isSuccess, DWORD userId,
762 const TCHAR *workstation, DWORD objectId,
763 const TCHAR *format, ...);
5039dede 764
a65c1819
VK
765bool ValidateConfig(Config *config, DWORD flags, TCHAR *errorText, int errorTextLen);
766DWORD ImportConfig(Config *config, DWORD flags);
5039dede
AK
767
768#ifdef _WITH_ENCRYPTION
769X509 *CertificateFromLoginMessage(CSCPMessage *pMsg);
a50eaebe 770BOOL ValidateUserCertificate(X509 *pCert, const TCHAR *pszLogin, BYTE *pChallenge,
5039dede 771 BYTE *pSignature, DWORD dwSigLen, int nMappingMethod,
a50eaebe 772 const TCHAR *pszMappingData);
9796ce45 773void ReloadCertificates();
5039dede
AK
774#endif
775
776#ifndef _WIN32
777THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL SignalHandler(void *);
778#endif /* not _WIN32 */
779
5039dede
AK
780void DbgTestRWLock(RWLOCK hLock, const TCHAR *szName, CONSOLE_CTX pCtx);
781void DumpSessions(CONSOLE_CTX pCtx);
782void ShowPollerState(CONSOLE_CTX pCtx);
35f836fe 783void SetPollerInfo(int nIdx, const TCHAR *pszMsg);
5039dede 784void ShowServerStats(CONSOLE_CTX pCtx);
35f836fe 785void ShowQueueStats(CONSOLE_CTX pCtx, Queue *pQueue, const TCHAR *pszName);
5039dede
AK
786void DumpProcess(CONSOLE_CTX pCtx);
787
788
789//
790// Global variables
791//
792
793extern TCHAR NXCORE_EXPORTABLE g_szConfigFile[];
794extern TCHAR NXCORE_EXPORTABLE g_szLogFile[];
4f8998ca
VK
795extern DWORD g_dwLogRotationMode;
796extern DWORD g_dwMaxLogSize;
797extern DWORD g_dwLogHistorySize;
798extern TCHAR g_szDailyLogFileSuffix[64];
5039dede
AK
799extern TCHAR NXCORE_EXPORTABLE g_szDumpDir[];
800extern TCHAR NXCORE_EXPORTABLE g_szListenAddress[];
801#ifndef _WIN32
802extern TCHAR NXCORE_EXPORTABLE g_szPIDFile[];
803#endif
804extern TCHAR g_szDataDir[];
9796ce45 805extern TCHAR g_szLibDir[];
712dd47d 806extern TCHAR g_szJavaLibDir[];
73869331 807extern DWORD NXCORE_EXPORTABLE g_processAffinityMask;
5039dede
AK
808extern QWORD g_qwServerId;
809extern RSA *g_pServerKey;
810extern DWORD g_dwPingSize;
811extern DWORD g_dwAuditFlags;
812extern time_t g_tServerStartTime;
813extern DWORD g_dwLockTimeout;
5039dede
AK
814extern DWORD g_dwAgentCommandTimeout;
815extern DWORD g_dwThresholdRepeatInterval;
816extern int g_nRequiredPolls;
52c75f5c 817extern DWORD g_dwSlmPollingInterval;
5039dede 818
b8c1ec69
VK
819extern TCHAR g_szDbDriver[];
820extern TCHAR g_szDbDrvParams[];
821extern TCHAR g_szDbServer[];
822extern TCHAR g_szDbLogin[];
823extern TCHAR g_szDbPassword[];
824extern TCHAR g_szDbName[];
f3c30cf5 825extern TCHAR g_szDbSchema[];
712dd47d 826extern TCHAR NXCORE_EXPORTABLE g_szJavaPath[];
b8c1ec69 827extern DB_DRIVER g_dbDriver;
5039dede
AK
828extern DB_HANDLE NXCORE_EXPORTABLE g_hCoreDB;
829extern Queue *g_pLazyRequestQueue;
830
28f5b9a4 831extern int NXCORE_EXPORTABLE g_nDBSyntax;
5039dede
AK
832
833#endif /* _nms_core_h_ */