178caadf08b580ba357c1ceda33d9cd927baef59
[public/netxms.git] / src / server / include / nms_core.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2011 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nms_core.h
20 **
21 **/
22
23 #ifndef _nms_core_h_
24 #define _nms_core_h_
25
26 #ifdef _WIN32
27 #ifdef NXCORE_EXPORTS
28 #define NXCORE_EXPORTABLE __declspec(dllexport)
29 #else
30 #define NXCORE_EXPORTABLE __declspec(dllimport)
31 #endif
32 #else /* _WIN32 */
33 #define NXCORE_EXPORTABLE
34 #endif
35
36 #define LIBNXCL_NO_DECLARATIONS 1
37
38 #include <nms_common.h>
39
40 #ifndef _WIN32
41
42 #if HAVE_SYS_TYPES_H
43 # include <sys/types.h>
44 #endif
45
46 #if HAVE_DLFCN_H
47 #include <dlfcn.h>
48 #endif
49
50 #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>
71 #include <nxqueue.h>
72 #include <nxsnmp.h>
73 #include <nxmodule.h>
74 #include <nxsl.h>
75 #include <nxdbapi.h>
76
77
78 //
79 // Console context
80 //
81
82 struct __console_ctx
83 {
84 SOCKET hSocket;
85 CSCPMessage *pMsg;
86 ClientSession *session;
87 };
88
89 typedef __console_ctx * CONSOLE_CTX;
90
91
92 //
93 // Server includes
94 //
95
96 #include "nms_dcoll.h"
97 #include "nms_users.h"
98 #include "nms_objects.h"
99 #include "nms_locks.h"
100 #include "nms_pkg.h"
101 #include "nms_topo.h"
102 #include "nms_script.h"
103 #include "nxcore_maps.h"
104 #include "nxcore_situations.h"
105 #include "nxcore_jobs.h"
106 #include "nxcore_logs.h"
107
108
109 //
110 // Common constants and macros
111 //
112
113 #define MAX_LINE_SIZE 4096
114 #define GROUP_FLAG_BIT ((DWORD)0x80000000)
115 #define CHECKPOINT_SNMP_PORT 260
116 #define DEFAULT_AFFINITY_MASK 0xFFFFFFFF
117
118 typedef void * HSNMPSESSION;
119
120
121 //
122 // Prefixes for poller messages
123 //
124
125 #define POLLER_ERROR _T("\x7F") _T("e")
126 #define POLLER_WARNING _T("\x7Fw")
127 #define POLLER_INFO _T("\x7Fi")
128
129
130 //
131 // Unique identifier group codes
132 //
133
134 #define IDG_NETWORK_OBJECT 0
135 #define IDG_CONTAINER_CAT 1
136 #define IDG_EVENT 2
137 #define IDG_ITEM 3
138 #define IDG_SNMP_TRAP 4
139 //#define IDG_IMAGE 5
140 #define IDG_ACTION 6
141 #define IDG_EVENT_GROUP 7
142 #define IDG_THRESHOLD 8
143 #define IDG_USER 9
144 #define IDG_USER_GROUP 10
145 #define IDG_ALARM 11
146 #define IDG_ALARM_NOTE 12
147 #define IDG_PACKAGE 13
148 //#define IDG_LPP 14
149 #define IDG_OBJECT_TOOL 15
150 #define IDG_SCRIPT 16
151 #define IDG_AGENT_CONFIG 17
152 #define IDG_GRAPH 18
153 #define IDG_CERTIFICATE 19
154 #define IDG_SITUATION 20
155 #define IDG_MAP 21
156
157
158 //
159 // Exit codes for console commands
160 //
161
162 #define CMD_EXIT_CONTINUE 0
163 #define CMD_EXIT_CLOSE_SESSION 1
164 #define CMD_EXIT_SHUTDOWN 2
165
166
167 //
168 // Network discovery mode
169 //
170
171 #define DISCOVERY_DISABLED 0
172 #define DISCOVERY_PASSIVE_ONLY 1
173 #define DISCOVERY_ACTIVE 2
174
175
176 //
177 // Client session flags
178 //
179
180 #define CSF_EPP_LOCKED ((DWORD)0x0002)
181 #define CSF_PACKAGE_DB_LOCKED ((DWORD)0x0004)
182 #define CSF_USER_DB_LOCKED ((DWORD)0x0008)
183 #define CSF_EPP_UPLOAD ((DWORD)0x0010)
184 #define CSF_CONSOLE_OPEN ((DWORD)0x0020)
185 #define CSF_AUTHENTICATED ((DWORD)0x0080)
186 #define CSF_RECEIVING_MAP_DATA ((DWORD)0x0200)
187 #define CSF_SYNC_OBJECT_COMMENTS ((DWORD)0x0400)
188
189
190 //
191 // Client session states
192 //
193
194 #define SESSION_STATE_INIT 0
195 #define SESSION_STATE_IDLE 1
196 #define SESSION_STATE_PROCESSING 2
197
198
199 //
200 // Information categories for UPDATE_INFO structure
201 //
202
203 #define INFO_CAT_EVENT 1
204 #define INFO_CAT_OBJECT_CHANGE 2
205 #define INFO_CAT_ALARM 3
206 #define INFO_CAT_ACTION 4
207 #define INFO_CAT_SYSLOG_MSG 5
208 #define INFO_CAT_SNMP_TRAP 6
209 #define INFO_CAT_AUDIT_RECORD 7
210 #define INFO_CAT_SITUATION 8
211
212
213 //
214 // Certificate types
215 //
216
217 #define CERT_TYPE_TRUSTED_CA 0
218 #define CERT_TYPE_USER 1
219
220
221 //
222 // Audit subsystems
223 //
224
225 #define AUDIT_SECURITY _T("SECURITY")
226 #define AUDIT_OBJECTS _T("OBJECTS")
227 #define AUDIT_SYSCFG _T("SYSCFG")
228 #define AUDIT_CONSOLE _T("CONSOLE")
229
230
231 //
232 // Event handling subsystem definitions
233 //
234
235 #include "nms_events.h"
236 #include "nms_actions.h"
237 #include "nms_alarm.h"
238
239
240 //
241 // New node information
242 //
243
244 typedef struct
245 {
246 DWORD dwIpAddr;
247 DWORD dwNetMask;
248 BOOL ignoreFilter;
249 } NEW_NODE;
250
251
252 //
253 // New node flags
254 //
255
256 #define NNF_IS_SNMP 0x0001
257 #define NNF_IS_AGENT 0x0002
258 #define NNF_IS_ROUTER 0x0004
259 #define NNF_IS_BRIDGE 0x0008
260 #define NNF_IS_PRINTER 0x0010
261 #define NNF_IS_CDP 0x0020
262 #define NNF_IS_SONMP 0x0040
263 #define NNF_IS_LLDP 0x0080
264
265
266 //
267 // Node information for autodiscovery filter
268 //
269
270 typedef struct
271 {
272 DWORD dwIpAddr;
273 DWORD dwNetMask;
274 DWORD dwSubnetAddr;
275 DWORD dwFlags;
276 int nSNMPVersion;
277 TCHAR szObjectId[MAX_OID_LEN * 4]; // SNMP OID
278 TCHAR szAgentVersion[MAX_AGENT_VERSION_LEN];
279 TCHAR szPlatform[MAX_PLATFORM_NAME_LEN];
280 } DISCOVERY_FILTER_DATA;
281
282
283 //
284 // Data update structure for client sessions
285 //
286
287 typedef struct
288 {
289 DWORD dwCategory; // Data category - event, network object, etc.
290 DWORD dwCode; // Data-specific update code
291 void *pData; // Pointer to data block
292 } UPDATE_INFO;
293
294
295 //
296 // Extended agent connection
297 //
298
299 class AgentConnectionEx : public AgentConnection
300 {
301 protected:
302 virtual void onTrap(CSCPMessage *msg);
303 virtual void onDataPush(CSCPMessage *msg);
304
305 public:
306 AgentConnectionEx(DWORD dwAddr, WORD wPort = AGENT_LISTEN_PORT,
307 int iAuthMethod = AUTH_NONE, TCHAR *pszSecret = NULL) :
308 AgentConnection(dwAddr, wPort, iAuthMethod, pszSecret) { }
309 virtual ~AgentConnectionEx();
310
311 DWORD deployPolicy(AgentPolicy *policy);
312 DWORD uninstallPolicy(AgentPolicy *policy);
313 };
314
315
316 //
317 // Client session
318 //
319
320 #define DECLARE_THREAD_STARTER(func) static THREAD_RESULT THREAD_CALL ThreadStarter_##func(void *);
321
322 class NXCORE_EXPORTABLE ClientSession
323 {
324 private:
325 SOCKET m_hSocket;
326 Queue *m_pSendQueue;
327 Queue *m_pMessageQueue;
328 Queue *m_pUpdateQueue;
329 DWORD m_dwIndex;
330 int m_iState;
331 WORD m_wCurrentCmd;
332 DWORD m_dwUserId;
333 DWORD m_dwSystemAccess; // User's system access rights
334 DWORD m_dwFlags; // Session flags
335 CSCP_BUFFER *m_pMsgBuffer;
336 CSCP_ENCRYPTION_CONTEXT *m_pCtx;
337 BYTE m_challenge[CLIENT_CHALLENGE_SIZE];
338 THREAD m_hWriteThread;
339 THREAD m_hProcessingThread;
340 THREAD m_hUpdateThread;
341 MUTEX m_mutexSendEvents;
342 MUTEX m_mutexSendSyslog;
343 MUTEX m_mutexSendTrapLog;
344 MUTEX m_mutexSendObjects;
345 MUTEX m_mutexSendAlarms;
346 MUTEX m_mutexSendActions;
347 MUTEX m_mutexSendAuditLog;
348 MUTEX m_mutexSendSituations;
349 MUTEX m_mutexPollerInit;
350 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
351 TCHAR m_szWorkstation[16]; // IP address of conneced host in textual form
352 TCHAR m_szUserName[MAX_SESSION_NAME]; // String in form login_name@host
353 TCHAR m_szClientInfo[96]; // Client app info string
354 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
355 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
356 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
357 DWORD m_dwRecordsUploaded;
358 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
359 int m_hCurrFile;
360 DWORD m_dwFileRqId;
361 DWORD m_dwUploadCommand;
362 DWORD m_dwUploadData;
363 TCHAR m_szCurrFileName[MAX_PATH];
364 DWORD m_dwRefCount;
365 DWORD m_dwEncryptionRqId;
366 DWORD m_dwEncryptionResult;
367 CONDITION m_condEncryptionSetup;
368 DWORD m_dwActiveChannels; // Active data channels
369 DWORD m_dwMapSaveRqId; // ID of currently active map saving request
370 nxMapSrv *m_pActiveMap; // Map currenly being saved
371 CONSOLE_CTX m_console; // Server console context
372
373 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
374 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
375 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
376 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
377 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
378
379 DECLARE_THREAD_STARTER(GetCollectedData)
380 DECLARE_THREAD_STARTER(ClearDCIData)
381 DECLARE_THREAD_STARTER(queryParameter)
382 DECLARE_THREAD_STARTER(queryAgentTable)
383 DECLARE_THREAD_STARTER(QueryL2Topology)
384 DECLARE_THREAD_STARTER(SendEventLog)
385 DECLARE_THREAD_STARTER(SendSyslog)
386 DECLARE_THREAD_STARTER(createObject)
387 DECLARE_THREAD_STARTER(getServerFile)
388 DECLARE_THREAD_STARTER(queryServerLog)
389 DECLARE_THREAD_STARTER(getServerLogQueryData)
390 DECLARE_THREAD_STARTER(executeAction)
391 DECLARE_THREAD_STARTER(findNodeConnection)
392 DECLARE_THREAD_STARTER(findMacAddress)
393 DECLARE_THREAD_STARTER(processConsoleCommand)
394
395 void readThread();
396 void writeThread();
397 void processingThread();
398 void updateThread();
399 void pollerThread(Node *pNode, int iPollType, DWORD dwRqId);
400
401 void setupEncryption(DWORD dwRqId);
402 void RespondToKeepalive(DWORD dwRqId);
403 void onFileUpload(BOOL bSuccess);
404 void DebugPrintf(int level, const TCHAR *format, ...);
405 void SendServerInfo(DWORD dwRqId);
406 void Login(CSCPMessage *pRequest);
407 void sendAllObjects(CSCPMessage *pRequest);
408 void sendSelectedObjects(CSCPMessage *pRequest);
409 void SendEventLog(CSCPMessage *pRequest);
410 void SendAllConfigVars(DWORD dwRqId);
411 void SetConfigVariable(CSCPMessage *pRequest);
412 void DeleteConfigVariable(CSCPMessage *pRequest);
413 void SendUserDB(DWORD dwRqId);
414 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
415 void CreateUser(CSCPMessage *pRequest);
416 void UpdateUser(CSCPMessage *pRequest);
417 void DeleteUser(CSCPMessage *pRequest);
418 void SetPassword(CSCPMessage *pRequest);
419 void LockUserDB(DWORD dwRqId, BOOL bLock);
420 void sendEventDB(DWORD dwRqId);
421 void modifyEventTemplate(CSCPMessage *pRequest);
422 void deleteEventTemplate(CSCPMessage *pRequest);
423 void generateEventCode(DWORD dwRqId);
424 void ModifyObject(CSCPMessage *pRequest);
425 void changeObjectMgmtStatus(CSCPMessage *pRequest);
426 void OpenNodeDCIList(CSCPMessage *pRequest);
427 void CloseNodeDCIList(CSCPMessage *pRequest);
428 void ModifyNodeDCI(CSCPMessage *pRequest);
429 void CopyDCI(CSCPMessage *pRequest);
430 void ApplyTemplate(CSCPMessage *pRequest);
431 void GetCollectedData(CSCPMessage *pRequest);
432 void ClearDCIData(CSCPMessage *pRequest);
433 void ChangeDCIStatus(CSCPMessage *pRequest);
434 void SendLastValues(CSCPMessage *pRequest);
435 void OpenEPP(DWORD dwRqId);
436 void CloseEPP(DWORD dwRqId);
437 void SaveEPP(CSCPMessage *pRequest);
438 void ProcessEPPRecord(CSCPMessage *pRequest);
439 void SendMIBTimestamp(DWORD dwRqId);
440 void SendMIB(DWORD dwRqId);
441 void createObject(CSCPMessage *pRequest);
442 void changeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
443 void deleteObject(CSCPMessage *pRequest);
444 void AcknowledgeAlarm(CSCPMessage *pRequest);
445 void TerminateAlarm(CSCPMessage *pRequest);
446 void DeleteAlarm(CSCPMessage *pRequest);
447 void CreateAction(CSCPMessage *pRequest);
448 void UpdateAction(CSCPMessage *pRequest);
449 void DeleteAction(CSCPMessage *pRequest);
450 void SendAllActions(DWORD dwRqId);
451 void SendContainerCategories(DWORD dwRqId);
452 void ForcedNodePoll(CSCPMessage *pRequest);
453 void OnTrap(CSCPMessage *pRequest);
454 void OnWakeUpNode(CSCPMessage *pRequest);
455 void queryParameter(CSCPMessage *pRequest);
456 void queryAgentTable(CSCPMessage *pRequest);
457 void EditTrap(int iOperation, CSCPMessage *pRequest);
458 void LockTrapCfg(DWORD dwRqId, BOOL bLock);
459 void SendAllTraps(DWORD dwRqId);
460 void SendAllTraps2(DWORD dwRqId);
461 void LockPackageDB(DWORD dwRqId, BOOL bLock);
462 void SendAllPackages(DWORD dwRqId);
463 void InstallPackage(CSCPMessage *pRequest);
464 void RemovePackage(CSCPMessage *pRequest);
465 void DeployPackage(CSCPMessage *pRequest);
466 void SendParametersList(CSCPMessage *pRequest);
467 void GetUserVariable(CSCPMessage *pRequest);
468 void SetUserVariable(CSCPMessage *pRequest);
469 void CopyUserVariable(CSCPMessage *pRequest);
470 void EnumUserVariables(CSCPMessage *pRequest);
471 void DeleteUserVariable(CSCPMessage *pRequest);
472 void ChangeObjectIP(CSCPMessage *pRequest);
473 void GetAgentConfig(CSCPMessage *pRequest);
474 void UpdateAgentConfig(CSCPMessage *pRequest);
475 void executeAction(CSCPMessage *pRequest);
476 void sendObjectTools(DWORD dwRqId);
477 void sendObjectToolDetails(CSCPMessage *pRequest);
478 void updateObjectTool(CSCPMessage *pRequest);
479 void deleteObjectTool(CSCPMessage *pRequest);
480 void generateObjectToolId(DWORD dwRqId);
481 void execTableTool(CSCPMessage *pRequest);
482 void ChangeSubscription(CSCPMessage *pRequest);
483 void SendSyslog(CSCPMessage *pRequest);
484 void SendServerStats(DWORD dwRqId);
485 void sendScriptList(DWORD dwRqId);
486 void sendScript(CSCPMessage *pRequest);
487 void updateScript(CSCPMessage *pRequest);
488 void renameScript(CSCPMessage *pRequest);
489 void deleteScript(CSCPMessage *pRequest);
490 void SendSessionList(DWORD dwRqId);
491 void KillSession(CSCPMessage *pRequest);
492 void SendTrapLog(CSCPMessage *pRequest);
493 void StartSnmpWalk(CSCPMessage *pRequest);
494 void SendMapList(DWORD dwRqId);
495 void ResolveMapName(CSCPMessage *pRequest);
496 void SaveMap(CSCPMessage *pRequest);
497 void DeleteMap(CSCPMessage *pRequest);
498 void ProcessSubmapData(CSCPMessage *pRequest);
499 void CreateMap(CSCPMessage *pRequest);
500 void RenameMap(CSCPMessage *pRequest);
501 void LoadMap(CSCPMessage *pRequest);
502 void SendSubmapBkImage(CSCPMessage *pRequest);
503 void RecvSubmapBkImage(CSCPMessage *pRequest);
504 void ResolveDCINames(CSCPMessage *pRequest);
505 DWORD ResolveDCIName(DWORD dwNode, DWORD dwItem, TCHAR **ppszName);
506 void SendConfigForAgent(CSCPMessage *pRequest);
507 void SendAgentCfgList(DWORD dwRqId);
508 void OpenAgentConfig(CSCPMessage *pRequest);
509 void SaveAgentConfig(CSCPMessage *pRequest);
510 void DeleteAgentConfig(CSCPMessage *pRequest);
511 void SwapAgentConfigs(CSCPMessage *pRequest);
512 void SendObjectComments(CSCPMessage *pRequest);
513 void UpdateObjectComments(CSCPMessage *pRequest);
514 void PushDCIData(CSCPMessage *pRequest);
515 void GetAddrList(CSCPMessage *pRequest);
516 void SetAddrList(CSCPMessage *pRequest);
517 void ResetComponent(CSCPMessage *pRequest);
518 void sendDCIEventList(CSCPMessage *request);
519 void SendDCIInfo(CSCPMessage *pRequest);
520 void sendPerfTabDCIList(CSCPMessage *pRequest);
521 void exportConfiguration(CSCPMessage *pRequest);
522 void importConfiguration(CSCPMessage *pRequest);
523 void SendGraphList(DWORD dwRqId);
524 void DefineGraph(CSCPMessage *pRequest);
525 void DeleteGraph(CSCPMessage *pRequest);
526 void AddCACertificate(CSCPMessage *pRequest);
527 void DeleteCertificate(CSCPMessage *pRequest);
528 void UpdateCertificateComments(CSCPMessage *pRequest);
529 void SendCertificateList(DWORD dwRqId);
530 void QueryL2Topology(CSCPMessage *pRequest);
531 void SendSMS(CSCPMessage *pRequest);
532 void SendCommunityList(DWORD dwRqId);
533 void UpdateCommunityList(CSCPMessage *pRequest);
534 void SendSituationList(DWORD dwRqId);
535 void CreateSituation(CSCPMessage *pRequest);
536 void UpdateSituation(CSCPMessage *pRequest);
537 void DeleteSituation(CSCPMessage *pRequest);
538 void DeleteSituationInstance(CSCPMessage *pRequest);
539 void setConfigCLOB(CSCPMessage *pRequest);
540 void getConfigCLOB(CSCPMessage *pRequest);
541 void WebMapAdd(CSCPMessage *pRequest);
542 void WebMapUpdateData(CSCPMessage *pRequest);
543 void WebMapUpdateProps(CSCPMessage *pRequest);
544 void WebMapDelete(CSCPMessage *pRequest);
545 void WebMapGetData(CSCPMessage *pRequest);
546 void WebMapGetList(DWORD dwRqId);
547 void registerAgent(CSCPMessage *pRequest);
548 void getServerFile(CSCPMessage *pRequest);
549 void testDCITransformation(CSCPMessage *pRequest);
550 void sendJobList(DWORD dwRqId);
551 void cancelJob(CSCPMessage *pRequest);
552 void deployAgentPolicy(CSCPMessage *pRequest, bool uninstallFlag);
553 void getUserCustomAttribute(CSCPMessage *request);
554 void setUserCustomAttribute(CSCPMessage *request);
555 void openServerLog(CSCPMessage *request);
556 void closeServerLog(CSCPMessage *request);
557 void queryServerLog(CSCPMessage *request);
558 void getServerLogQueryData(CSCPMessage *request);
559 void sendUsmCredentials(DWORD dwRqId);
560 void updateUsmCredentials(CSCPMessage *pRequest);
561 void sendDCIThresholds(CSCPMessage *request);
562 void addClusterNode(CSCPMessage *request);
563 void findNodeConnection(CSCPMessage *request);
564 void findMacAddress(CSCPMessage *request);
565 void sendLibraryImage(CSCPMessage *request);
566 void updateLibraryImage(CSCPMessage *request);
567 void listLibraryImages(CSCPMessage *request);
568 void deleteLibraryImage(CSCPMessage *request);
569 void executeServerCommand(CSCPMessage *request);
570 void uploadFileToAgent(CSCPMessage *request);
571 void listServerFileStore(CSCPMessage *request);
572 void processConsoleCommand(CSCPMessage *msg);
573 void openConsole(DWORD rqId);
574 void closeConsole(DWORD rqId);
575
576 public:
577 ClientSession(SOCKET hSocket, DWORD dwHostAddr);
578 ~ClientSession();
579
580 void incRefCount() { m_dwRefCount++; }
581 void decRefCount() { if (m_dwRefCount > 0) m_dwRefCount--; }
582
583 void run();
584
585 void sendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
586 void sendPollerMsg(DWORD dwRqId, const TCHAR *pszMsg);
587 BOOL sendFile(const TCHAR *file, DWORD dwRqId);
588
589 DWORD getIndex() { return m_dwIndex; }
590 void setIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
591 int getState() { return m_iState; }
592 const TCHAR *getUserName(void) { return m_szUserName; }
593 const TCHAR *getClientInfo(void) { return m_szClientInfo; }
594 DWORD getUserId() { return m_dwUserId; }
595 DWORD getSystemRights() { return m_dwSystemAccess; }
596 bool isAuthenticated() { return (m_dwFlags & CSF_AUTHENTICATED) ? true : false; }
597 bool isConsoleOpen() { return (m_dwFlags & CSF_CONSOLE_OPEN) ? true : false; }
598 bool isSubscribed(DWORD dwChannel) { return (m_dwActiveChannels & dwChannel) ? true : false; }
599 WORD getCurrentCmd() { return m_wCurrentCmd; }
600 int getCipher() { return (m_pCtx == NULL) ? -1 : m_pCtx->nCipher; }
601
602 bool checkSysAccessRights(DWORD requiredAccess)
603 {
604 return m_dwUserId == 0 ? true :
605 ((requiredAccess & m_dwSystemAccess) ? true : false);
606 }
607
608 void kill();
609 void notify(DWORD dwCode, DWORD dwData = 0);
610
611 void queueUpdate(UPDATE_INFO *pUpdate) { m_pUpdateQueue->Put(pUpdate); }
612 void onNewEvent(Event *pEvent);
613 void onSyslogMessage(NX_SYSLOG_RECORD *pRec);
614 void onNewSNMPTrap(CSCPMessage *pMsg);
615 void onObjectChange(NetObj *pObject);
616 void onUserDBUpdate(int code, DWORD id, UserDatabaseObject *user);
617 void onAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
618 void onActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
619 void onSituationChange(CSCPMessage *msg);
620 };
621
622
623 //
624 // Flags for SnmpGet
625 //
626
627 #define SG_VERBOSE 0x0001
628 #define SG_STRING_RESULT 0x0002
629 #define SG_RAW_RESULT 0x0004
630 #define SG_HSTRING_RESULT 0x0008
631
632
633 //
634 // Functions
635 //
636
637 BOOL NXCORE_EXPORTABLE ConfigReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
638 #ifdef UNICODE
639 BOOL NXCORE_EXPORTABLE ConfigReadStrA(const WCHAR *szVar, char *szBuffer, int iBufSize, const char *szDefault);
640 #else
641 #define ConfigReadStrA ConfigReadStr
642 #endif
643 int NXCORE_EXPORTABLE ConfigReadInt(const TCHAR *szVar, int iDefault);
644 DWORD NXCORE_EXPORTABLE ConfigReadULong(const TCHAR *szVar, DWORD dwDefault);
645 BOOL NXCORE_EXPORTABLE ConfigReadByteArray(const TCHAR *pszVar, int *pnArray,
646 int nSize, int nDefault);
647 BOOL NXCORE_EXPORTABLE ConfigWriteStr(const TCHAR *szVar, const TCHAR *szValue, BOOL bCreate,
648 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
649 BOOL NXCORE_EXPORTABLE ConfigWriteInt(const TCHAR *szVar, int iValue, BOOL bCreate,
650 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
651 BOOL NXCORE_EXPORTABLE ConfigWriteULong(const TCHAR *szVar, DWORD dwValue, BOOL bCreate,
652 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
653 BOOL NXCORE_EXPORTABLE ConfigWriteByteArray(const TCHAR *pszVar, int *pnArray,
654 int nSize, BOOL bCreate,
655 BOOL isVisible = TRUE, BOOL needRestart = FALSE);
656 TCHAR NXCORE_EXPORTABLE *ConfigReadCLOB(const TCHAR *var, const TCHAR *defValue);
657 BOOL NXCORE_EXPORTABLE ConfigWriteCLOB(const TCHAR *var, const TCHAR *value, BOOL bCreate);
658
659 BOOL NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
660
661 BOOL NXCORE_EXPORTABLE LoadConfig();
662
663 void NXCORE_EXPORTABLE Shutdown();
664 void NXCORE_EXPORTABLE FastShutdown();
665 BOOL NXCORE_EXPORTABLE Initialize();
666 THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL Main(void *);
667 void NXCORE_EXPORTABLE ShutdownDB();
668 void InitiateShutdown();
669
670 BOOL NXCORE_EXPORTABLE SleepAndCheckForShutdown(int iSeconds);
671
672 void ConsolePrintf(CONSOLE_CTX pCtx, const TCHAR *pszFormat, ...);
673 int ProcessConsoleCommand(const TCHAR *pszCmdLine, CONSOLE_CTX pCtx);
674
675 void SaveObjects(DB_HANDLE hdb);
676
677 void NXCORE_EXPORTABLE QueueSQLRequest(const TCHAR *query);
678 void StartDBWriter();
679 void StopDBWriter();
680
681 void DecodeSQLStringAndSetVariable(CSCPMessage *pMsg, DWORD dwVarId, TCHAR *pszStr);
682
683 void SnmpInit();
684 DWORD SnmpNewRequestId();
685 DWORD SnmpGet(DWORD dwVersion, SNMP_Transport *pTransport,
686 const TCHAR *szOidStr, const DWORD *oidBinary, DWORD dwOidLen, void *pValue,
687 DWORD dwBufferSize, DWORD dwFlags);
688 DWORD SnmpEnumerate(DWORD dwVersion, SNMP_Transport *pTransport, const TCHAR *szRootOid,
689 DWORD (* pHandler)(DWORD, SNMP_Variable *, SNMP_Transport *, void *),
690 void *pUserArg, BOOL bVerbose);
691 SNMP_SecurityContext *SnmpCheckCommSettings(SNMP_Transport *pTransport, int *version, SNMP_SecurityContext *originalContext);
692 void StrToMac(const TCHAR *pszStr, BYTE *pBuffer);
693 DWORD OidToType(TCHAR *pszOid, DWORD *pdwFlags);
694
695 void InitLocalNetInfo();
696
697 ARP_CACHE *GetLocalArpCache();
698 ARP_CACHE *SnmpGetArpCache(DWORD dwVersion, SNMP_Transport *pTransport);
699
700 InterfaceList *SnmpGetInterfaceList(DWORD dwVersion, SNMP_Transport *pTransport, Node *node, BOOL useIfXTable);
701 InterfaceList *GetLocalInterfaceList();
702 int SnmpGetInterfaceStatus(DWORD dwVersion, SNMP_Transport *pTransport, DWORD dwIfIndex);
703
704 ROUTING_TABLE *SnmpGetRoutingTable(DWORD dwVersion, SNMP_Transport *pTransport);
705
706 void WatchdogInit(void);
707 DWORD WatchdogAddThread(const TCHAR *szName, time_t tNotifyInterval);
708 void WatchdogNotify(DWORD dwId);
709 void WatchdogPrintStatus(CONSOLE_CTX pCtx);
710
711 void CheckForMgmtNode(void);
712 Node *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwCreationFlags,
713 TCHAR *pszName, DWORD dwProxyNode, DWORD dwSNMPProxy, Cluster *pCluster);
714
715 void NXCORE_EXPORTABLE EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
716 void NXCORE_EXPORTABLE NotifyClientSessions(DWORD dwCode, DWORD dwData);
717 int GetSessionCount(void);
718
719 void GetSysInfoStr(TCHAR *pszBuffer, int nMaxSize);
720 DWORD GetLocalIpAddr();
721 TCHAR *GetLocalHostName(TCHAR *buffer, size_t bufSize);
722
723 BOOL ExecCommand(TCHAR *pszCommand);
724 BOOL SendMagicPacket(DWORD dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
725
726 BOOL InitIdTable();
727 DWORD CreateUniqueId(int iGroup);
728 QWORD CreateUniqueEventId();
729
730 void InitMailer();
731 void ShutdownMailer();
732 void NXCORE_EXPORTABLE PostMail(const TCHAR *pszRcpt, const TCHAR *pszSubject, const TCHAR *pszText);
733
734 void InitSMSSender();
735 void ShutdownSMSSender();
736 void NXCORE_EXPORTABLE PostSMS(const TCHAR *pszRcpt, const TCHAR *pszText);
737
738 void GetAccelarVLANIfList(DWORD dwVersion, SNMP_Transport *pTransport, InterfaceList *pIfList);
739
740 void InitTraps();
741 void SendTrapsToClient(ClientSession *pSession, DWORD dwRqId);
742 void CreateTrapCfgMessage(CSCPMessage &msg);
743 DWORD CreateNewTrap(DWORD *pdwTrapId);
744 DWORD CreateNewTrap(NXC_TRAP_CFG_ENTRY *pTrap);
745 DWORD UpdateTrapFromMsg(CSCPMessage *pMsg);
746 DWORD DeleteTrap(DWORD dwId);
747 void CreateNXMPTrapRecord(String &str, DWORD dwId);
748
749 BOOL IsTableTool(DWORD dwToolId);
750 BOOL CheckObjectToolAccess(DWORD dwToolId, DWORD dwUserId);
751 DWORD ExecuteTableTool(DWORD dwToolId, Node *pNode, DWORD dwRqId, ClientSession *pSession);
752 DWORD DeleteObjectToolFromDB(DWORD dwToolId);
753 DWORD UpdateObjectToolFromMessage(CSCPMessage *pMsg);
754
755 void CreateMessageFromSyslogMsg(CSCPMessage *pMsg, NX_SYSLOG_RECORD *pRec);
756 void ReinitializeSyslogParser();
757
758 void EscapeString(String &str);
759
760 void InitAuditLog();
761 void NXCORE_EXPORTABLE WriteAuditLog(const TCHAR *subsys, BOOL isSuccess, DWORD userId,
762 const TCHAR *workstation, DWORD objectId,
763 const TCHAR *format, ...);
764
765 void AddObjectToIndex(INDEX **ppIndex, DWORD *pdwIndexSize, DWORD dwKey, void *pObject);
766 void DeleteObjectFromIndex(INDEX **ppIndex, DWORD *pdwIndexSize, DWORD dwKey);
767 DWORD SearchIndex(INDEX *pIndex, DWORD dwIndexSize, DWORD dwKey);
768
769 bool ValidateConfig(Config *config, DWORD flags, TCHAR *errorText, int errorTextLen);
770 DWORD ImportConfig(Config *config, DWORD flags);
771
772 #ifdef _WITH_ENCRYPTION
773 X509 *CertificateFromLoginMessage(CSCPMessage *pMsg);
774 BOOL ValidateUserCertificate(X509 *pCert, const TCHAR *pszLogin, BYTE *pChallenge,
775 BYTE *pSignature, DWORD dwSigLen, int nMappingMethod,
776 const TCHAR *pszMappingData);
777 void ReloadCertificates(void);
778 #endif
779
780 #ifndef _WIN32
781 THREAD_RESULT NXCORE_EXPORTABLE THREAD_CALL SignalHandler(void *);
782 #endif /* not _WIN32 */
783
784 void DbgTestMutex(MUTEX hMutex, const TCHAR *szName, CONSOLE_CTX pCtx);
785 void DbgTestRWLock(RWLOCK hLock, const TCHAR *szName, CONSOLE_CTX pCtx);
786 void DumpSessions(CONSOLE_CTX pCtx);
787 void ShowPollerState(CONSOLE_CTX pCtx);
788 void SetPollerInfo(int nIdx, const TCHAR *pszMsg);
789 void ShowServerStats(CONSOLE_CTX pCtx);
790 void ShowQueueStats(CONSOLE_CTX pCtx, Queue *pQueue, const TCHAR *pszName);
791 void DumpProcess(CONSOLE_CTX pCtx);
792
793
794 //
795 // Global variables
796 //
797
798 extern TCHAR NXCORE_EXPORTABLE g_szConfigFile[];
799 extern TCHAR NXCORE_EXPORTABLE g_szLogFile[];
800 extern TCHAR NXCORE_EXPORTABLE g_szDumpDir[];
801 extern TCHAR NXCORE_EXPORTABLE g_szListenAddress[];
802 #ifndef _WIN32
803 extern TCHAR NXCORE_EXPORTABLE g_szPIDFile[];
804 #endif
805 extern TCHAR g_szDataDir[];
806 extern DWORD NXCORE_EXPORTABLE g_processAffinityMask;
807 extern QWORD g_qwServerId;
808 extern RSA *g_pServerKey;
809 extern DWORD g_dwPingSize;
810 extern DWORD g_dwAuditFlags;
811 extern time_t g_tServerStartTime;
812 extern DWORD g_dwLockTimeout;
813 extern DWORD g_dwSNMPTimeout;
814 extern DWORD g_dwAgentCommandTimeout;
815 extern DWORD g_dwThresholdRepeatInterval;
816 extern int g_nRequiredPolls;
817
818 extern TCHAR g_szDbDriver[];
819 extern TCHAR g_szDbDrvParams[];
820 extern TCHAR g_szDbServer[];
821 extern TCHAR g_szDbLogin[];
822 extern TCHAR g_szDbPassword[];
823 extern TCHAR g_szDbName[];
824 extern DB_DRIVER g_dbDriver;
825 extern DB_HANDLE NXCORE_EXPORTABLE g_hCoreDB;
826 extern Queue *g_pLazyRequestQueue;
827
828 extern int NXCORE_EXPORTABLE g_nDBSyntax;
829
830 #endif /* _nms_core_h_ */