06a1624a8c4d06c31ad5160890e52932bccd93c0
[public/netxms.git] / src / server / include / nms_core.h
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004 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 ** $module: 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
37 #define LIBNXCL_NO_DECLARATIONS 1
38
39 #include <nms_common.h>
40
41 #ifndef _WIN32
42
43 #if HAVE_SYS_TYPES_H
44 # include <sys/types.h>
45 #endif
46
47 #if HAVE_DLFCN_H
48 #include <dlfcn.h>
49 #endif
50
51 #define WSAGetLastError() (errno)
52
53 #endif /* _WIN32 */
54
55 #include <time.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdarg.h>
59
60 #ifdef _WIN32
61 #define _GETOPT_H_ 1 /* Prevent including getopt.h from net-snmp */
62 #define HAVE_SOCKLEN_T /* Prevent defining socklen_t in net-snmp */
63 #endif /* _WIN32 */
64
65 //#include <net-snmp/net-snmp-config.h>
66 //#include <net-snmp/net-snmp-includes.h>
67
68 #include <nms_threads.h>
69 #include <dbdrv.h>
70 #include <nms_cscp.h>
71 #include <nms_util.h>
72 #include <nxsrvapi.h>
73 #include <nxcscpapi.h>
74 #include <nximage.h>
75 #include <nxqueue.h>
76 #include <nxsnmp.h>
77 #include <nxmodule.h>
78 #include "nms_dcoll.h"
79 #include "nms_users.h"
80 #include "nms_objects.h"
81 #include "messages.h"
82 #include "nms_locks.h"
83 #include "nms_pkg.h"
84
85
86 //
87 // Common constants and macros
88 //
89
90 #ifdef _WIN32
91
92 #define DEFAULT_SHELL "cmd.exe"
93 #define DEFAULT_LOG_FILE "C:\\NetXMS.log"
94 #define DEFAULT_DATA_DIR "C:\\NetXMS\\var"
95
96 #define DDIR_MIBS "\\mibs"
97 #define DDIR_IMAGES "\\images"
98 #define DDIR_PACKAGES "\\packages"
99
100 #else /* _WIN32 */
101
102 #define DEFAULT_SHELL "/bin/sh"
103 #define DEFAULT_LOG_FILE "/var/log/netxmsd.log"
104 #define DEFAULT_DATA_DIR "/var/netxms"
105
106 #define DDIR_MIBS "/mibs"
107 #define DDIR_IMAGES "/images"
108 #define DDIR_PACKAGES "/packages"
109
110 #endif /* _WIN32 */
111
112 #define MAX_LINE_SIZE 4096
113
114 #define GROUP_FLAG_BIT ((DWORD)0x80000000)
115
116 typedef void * HSNMPSESSION;
117
118
119 //
120 // Unique identifier group codes
121 //
122
123 #define IDG_NETWORK_OBJECT 0
124 #define IDG_CONTAINER_CAT 1
125 #define IDG_EVENT 2
126 #define IDG_ITEM 3
127 #define IDG_SNMP_TRAP 4
128 #define IDG_IMAGE 5
129 #define IDG_ACTION 6
130 #define IDG_EVENT_GROUP 7
131 #define IDG_THRESHOLD 8
132 #define IDG_USER 9
133 #define IDG_USER_GROUP 10
134 #define IDG_ALARM 11
135 #define IDG_ALARM_NOTE 12
136 #define IDG_PACKAGE 13
137
138
139 //
140 // Application flags
141 //
142
143 #define AF_STANDALONE 0x00000001
144 #define AF_USE_EVENT_LOG 0x00000002
145 #define AF_ENABLE_ACCESS_CONTROL 0x00000004
146 #define AF_ENABLE_EVENTS_ACCESS_CONTROL 0x00000008
147 #define AF_LOG_SQL_ERRORS 0x00000010
148 #define AF_DELETE_EMPTY_SUBNETS 0x00000020
149 #define AF_ENABLE_SNMP_TRAPD 0x00000040
150 #define AF_DEBUG_EVENTS 0x00000100
151 #define AF_DEBUG_CSCP 0x00000200
152 #define AF_DEBUG_DISCOVERY 0x00000400
153 #define AF_DEBUG_DC 0x00000800
154 #define AF_DEBUG_HOUSEKEEPER 0x00001000
155 #define AF_DEBUG_LOCKS 0x00002000
156 #define AF_DEBUG_ACTIONS 0x00004000
157 #define AF_DEBUG_MISC 0x00008000
158 #define AF_DEBUG_SQL 0x00010000
159 #define AF_DEBUG_SNMP 0x00020000
160 #define AF_DEBUG_ALL 0x0002FF00
161 #define AF_DB_LOCKED 0x01000000
162 #define AF_SHUTDOWN 0x80000000
163
164 #define IsStandalone() (g_dwFlags & AF_STANDALONE)
165 #define ShutdownInProgress() (g_dwFlags & AF_SHUTDOWN)
166
167
168 //
169 // Client session flags
170 //
171
172 #define CSF_EVENT_DB_LOCKED ((DWORD)0x0001)
173 #define CSF_EPP_LOCKED ((DWORD)0x0002)
174 #define CSF_EVENT_DB_MODIFIED ((DWORD)0x0004)
175 #define CSF_USER_DB_LOCKED ((DWORD)0x0008)
176 #define CSF_EPP_UPLOAD ((DWORD)0x0010)
177 #define CSF_ACTION_DB_LOCKED ((DWORD)0x0020)
178 #define CSF_TRAP_CFG_LOCKED ((DWORD)0x0040)
179 #define CSF_AUTHENTICATED ((DWORD)0x0080)
180 #define CSF_PACKAGE_DB_LOCKED ((DWORD)0x0100)
181
182
183 //
184 // Client session states
185 //
186
187 #define SESSION_STATE_INIT 0
188 #define SESSION_STATE_IDLE 1
189 #define SESSION_STATE_PROCESSING 2
190
191
192 //
193 // Information categories for UPDATE_INFO structure
194 //
195
196 #define INFO_CAT_EVENT 1
197 #define INFO_CAT_OBJECT_CHANGE 2
198 #define INFO_CAT_ALARM 3
199 #define INFO_CAT_ACTION 4
200
201
202 //
203 // Event handling subsystem definitions
204 //
205
206 #include "nms_events.h"
207 #include "nms_actions.h"
208 #include "nms_alarm.h"
209
210
211 //
212 // Data update structure for client sessions
213 //
214
215 typedef struct
216 {
217 DWORD dwCategory; // Data category - event, network object, etc.
218 DWORD dwCode; // Data-specific update code
219 void *pData; // Pointer to data block
220 } UPDATE_INFO;
221
222
223 //
224 // Client session
225 //
226
227 class ClientSession
228 {
229 private:
230 SOCKET m_hSocket;
231 Queue *m_pSendQueue;
232 Queue *m_pMessageQueue;
233 Queue *m_pUpdateQueue;
234 DWORD m_dwIndex;
235 int m_iState;
236 WORD m_wCurrentCmd;
237 DWORD m_dwUserId;
238 DWORD m_dwSystemAccess; // User's system access rights
239 DWORD m_dwFlags; // Session flags
240 CSCP_BUFFER *m_pMsgBuffer;
241 THREAD m_hWriteThread;
242 THREAD m_hProcessingThread;
243 THREAD m_hUpdateThread;
244 MUTEX m_mutexSendEvents;
245 MUTEX m_mutexSendObjects;
246 MUTEX m_mutexSendAlarms;
247 MUTEX m_mutexSendActions;
248 MUTEX m_mutexPollerInit;
249 DWORD m_dwHostAddr; // IP address of connected host (network byte order)
250 TCHAR m_szUserName[256]; // String in form login_name@host
251 DWORD m_dwOpenDCIListSize; // Number of open DCI lists
252 DWORD *m_pOpenDCIList; // List of nodes with DCI lists open
253 DWORD m_dwNumRecordsToUpload; // Number of records to be uploaded
254 DWORD m_dwRecordsUploaded;
255 EPRule **m_ppEPPRuleList; // List of loaded EPP rules
256 int m_hCurrFile;
257 DWORD m_dwFileRqId;
258 DWORD m_dwUploadCommand;
259 DWORD m_dwUploadData;
260 TCHAR m_szCurrFileName[MAX_PATH];
261
262 static THREAD_RESULT THREAD_CALL ReadThreadStarter(void *);
263 static THREAD_RESULT THREAD_CALL WriteThreadStarter(void *);
264 static THREAD_RESULT THREAD_CALL ProcessingThreadStarter(void *);
265 static THREAD_RESULT THREAD_CALL UpdateThreadStarter(void *);
266 static THREAD_RESULT THREAD_CALL PollerThreadStarter(void *);
267
268 void ReadThread(void);
269 void WriteThread(void);
270 void ProcessingThread(void);
271 void UpdateThread(void);
272 void PollerThread(Node *pNode, int iPollType, DWORD dwRqId);
273
274 BOOL CheckSysAccessRights(DWORD dwRequiredAccess)
275 {
276 return m_dwUserId == 0 ? TRUE :
277 ((dwRequiredAccess & m_dwSystemAccess) ? TRUE : FALSE);
278 }
279
280 void OnFileUpload(BOOL bSuccess);
281 void DebugPrintf(char *szFormat, ...);
282 void SendServerInfo(DWORD dwRqId);
283 void Login(CSCPMessage *pRequest);
284 void SendAllObjects(DWORD dwRqId);
285 void SendAllEvents(DWORD dwRqId);
286 void SendAllConfigVars(void);
287 void SendUserDB(DWORD dwRqId);
288 void SendAllAlarms(DWORD dwRqId, BOOL bIncludeAck);
289 void CreateUser(CSCPMessage *pRequest);
290 void UpdateUser(CSCPMessage *pRequest);
291 void DeleteUser(CSCPMessage *pRequest);
292 void SetPassword(CSCPMessage *pRequest);
293 void LockUserDB(DWORD dwRqId, BOOL bLock);
294 void SetConfigVariable(CSCPMessage *pRequest);
295 void SendEventDB(DWORD dwRqId);
296 void LockEventDB(DWORD dwRqId);
297 void UnlockEventDB(DWORD dwRqId);
298 void SetEventInfo(CSCPMessage *pRequest);
299 void DeleteEventTemplate(CSCPMessage *pRequest);
300 void GenerateEventCode(DWORD dwRqId);
301 void ModifyObject(CSCPMessage *pRequest);
302 void ChangeObjectMgmtStatus(CSCPMessage *pRequest);
303 void OpenNodeDCIList(CSCPMessage *pRequest);
304 void CloseNodeDCIList(CSCPMessage *pRequest);
305 void ModifyNodeDCI(CSCPMessage *pRequest);
306 void CopyDCI(CSCPMessage *pRequest);
307 void GetCollectedData(CSCPMessage *pRequest);
308 void ChangeDCIStatus(CSCPMessage *pRequest);
309 void OpenEPP(DWORD dwRqId);
310 void CloseEPP(DWORD dwRqId);
311 void SaveEPP(CSCPMessage *pRequest);
312 void ProcessEPPRecord(CSCPMessage *pRequest);
313 void SendMIBList(DWORD dwRqId);
314 void SendMIB(CSCPMessage *pRequest);
315 void CreateObject(CSCPMessage *pRequest);
316 void ChangeObjectBinding(CSCPMessage *pRequest, BOOL bBind);
317 void DeleteObject(CSCPMessage *pRequest);
318 void AcknowlegeAlarm(CSCPMessage *pRequest);
319 void DeleteAlarm(CSCPMessage *pRequest);
320 void CreateAction(CSCPMessage *pRequest);
321 void UpdateAction(CSCPMessage *pRequest);
322 void DeleteAction(CSCPMessage *pRequest);
323 void LockActionDB(DWORD dwRqId, BOOL bLock);
324 void SendAllActions(DWORD dwRqId);
325 void SendContainerCategories(DWORD dwRqId);
326 void ForcedNodePoll(CSCPMessage *pRequest);
327 void OnTrap(CSCPMessage *pRequest);
328 void OnWakeUpNode(CSCPMessage *pRequest);
329 void QueryParameter(CSCPMessage *pRequest);
330 void EditTrap(int iOperation, CSCPMessage *pRequest);
331 void LockTrapCfg(DWORD dwRqId, BOOL bLock);
332 void SendAllTraps(DWORD dwRqId);
333 void LockPackageDB(DWORD dwRqId, BOOL bLock);
334 void SendAllPackages(DWORD dwRqId);
335 void InstallPackage(CSCPMessage *pRequest);
336 void RemovePackage(CSCPMessage *pRequest);
337
338 public:
339 ClientSession(SOCKET hSocket, DWORD dwHostAddr);
340 ~ClientSession();
341
342 void Run(void);
343
344 void SendMessage(CSCPMessage *pMsg) { m_pSendQueue->Put(pMsg->CreateMessage()); }
345 void SendPollerMsg(DWORD dwRqId, TCHAR *pszMsg);
346
347 DWORD GetIndex(void) { return m_dwIndex; }
348 void SetIndex(DWORD dwIndex) { if (m_dwIndex == INVALID_INDEX) m_dwIndex = dwIndex; }
349 int GetState(void) { return m_iState; }
350 const char *GetUserName(void) { return m_szUserName; }
351 DWORD GetUserId(void) { return m_dwUserId; }
352 BOOL IsAuthenticated(void) { return (m_dwFlags & CSF_AUTHENTICATED) ? TRUE : FALSE; }
353 WORD GetCurrentCmd(void) { return m_wCurrentCmd; }
354
355 void Kill(void);
356 void Notify(DWORD dwCode, DWORD dwData = 0);
357
358 void OnNewEvent(Event *pEvent);
359 void OnObjectChange(NetObj *pObject);
360 void OnUserDBUpdate(int iCode, DWORD dwUserId, NMS_USER *pUser, NMS_USER_GROUP *pGroup);
361 void OnAlarmUpdate(DWORD dwCode, NXC_ALARM *pAlarm);
362 void OnActionDBUpdate(DWORD dwCode, NXC_ACTION *pAction);
363 };
364
365
366 //
367 // Functions
368 //
369
370 BOOL NXCORE_EXPORTABLE ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, const char *szDefault);
371 int NXCORE_EXPORTABLE ConfigReadInt(char *szVar, int iDefault);
372 DWORD NXCORE_EXPORTABLE ConfigReadULong(char *szVar, DWORD dwDefault);
373 BOOL NXCORE_EXPORTABLE ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate);
374 BOOL NXCORE_EXPORTABLE ConfigWriteInt(char *szVar, int iValue, BOOL bCreate);
375 BOOL NXCORE_EXPORTABLE ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate);
376
377 BOOL NXCORE_EXPORTABLE LoadConfig(void);
378
379 void NXCORE_EXPORTABLE Shutdown(void);
380 BOOL NXCORE_EXPORTABLE Initialize(void);
381 void NXCORE_EXPORTABLE Main(void);
382 void NXCORE_EXPORTABLE ShutdownDB(void);
383
384 BOOL NXCORE_EXPORTABLE SleepAndCheckForShutdown(int iSeconds);
385
386 void SaveObjects(void);
387
388 void QueueSQLRequest(char *szQuery);
389 void StartDBWriter(void);
390 void StopDBWriter(void);
391
392 void SnmpInit(void);
393 DWORD SnmpGet(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, const char *szOidStr,
394 const DWORD *oidBinary, DWORD dwOidLen, void *pValue,
395 DWORD dwBufferSize, BOOL bVerbose, BOOL bStringResult);
396 DWORD SnmpEnumerate(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, const char *szRootOid,
397 void (* pHandler)(DWORD, DWORD, const char *, SNMP_Variable *, void *),
398 void *pUserArg, BOOL bVerbose);
399 void StrToMac(char *pszStr, BYTE *pBuffer);
400 DWORD OidToType(TCHAR *pszOid, DWORD *pdwFlags);
401
402 void InitLocalNetInfo(void);
403
404 ARP_CACHE *GetLocalArpCache(void);
405 ARP_CACHE *SnmpGetArpCache(DWORD dwVersion, DWORD dwAddr, const char *szCommunity);
406
407 INTERFACE_LIST *SnmpGetInterfaceList(DWORD dwVersion, DWORD dwAddr, const char *szCommunity, DWORD dwNodeType);
408 INTERFACE_LIST *GetLocalInterfaceList(void);
409 void CleanInterfaceList(INTERFACE_LIST *pIfList);
410 int SnmpGetInterfaceStatus(DWORD dwNodeAddr, DWORD dwVersion, char *pszCommunity, DWORD dwIfIndex);
411
412 void WatchdogInit(void);
413 DWORD WatchdogAddThread(char *szName, time_t tNotifyInterval);
414 void WatchdogNotify(DWORD dwId);
415 void WatchdogPrintStatus(void);
416
417 void CheckForMgmtNode(void);
418 NetObj *PollNewNode(DWORD dwIpAddr, DWORD dwNetMask, DWORD dwFlags, TCHAR *pszName);
419
420 void EnumerateClientSessions(void (*pHandler)(ClientSession *, void *), void *pArg);
421 void NotifyClient(ClientSession *pSession, void *pArg);
422
423 void GetSysInfoStr(char *pszBuffer);
424 DWORD GetLocalIpAddr(void);
425
426 BOOL ExecCommand(char *pszCommand);
427 BOOL SendMagicPacket(DWORD dwIpAddr, BYTE *pbMacAddr, int iNumPackets);
428
429 BOOL InitIdTable(void);
430 DWORD CreateUniqueId(int iGroup);
431 QWORD CreateUniqueEventId(void);
432
433 BYTE *LoadFile(char *pszFileName, DWORD *pdwFileSize);
434
435 void UpdateImageHashes(void);
436 void SendImageCatalogue(ClientSession *pSession, DWORD dwRqId, WORD wFormat);
437 void SendImageFile(ClientSession *pSession, DWORD dwRqId, DWORD dwImageId, WORD wFormat);
438 void SendDefaultImageList(ClientSession *pSession, DWORD dwRqId);
439
440 char NXCORE_EXPORTABLE *EncodeSQLString(const char *pszIn);
441 void NXCORE_EXPORTABLE DecodeSQLString(char *pszStr);
442
443 void InitMailer(void);
444 void ShutdownMailer(void);
445 void NXCORE_EXPORTABLE PostMail(char *pszRcpt, char *pszSubject, char *pszText);
446
447 void GetAccelarVLANIfList(DWORD dwVersion, DWORD dwIpAddr, const TCHAR *pszCommunity, INTERFACE_LIST *pIfList);
448
449 void InitTraps(void);
450 void SendTrapsToClient(ClientSession *pSession, DWORD dwRqId);
451 DWORD CreateNewTrap(DWORD *pdwTrapId);
452 DWORD UpdateTrapFromMsg(CSCPMessage *pMsg);
453 DWORD DeleteTrap(DWORD dwId);
454
455 #ifdef _WIN32
456
457 char NXCORE_EXPORTABLE *GetSystemErrorText(DWORD error);
458
459 #else
460
461 void NXCORE_EXPORTABLE OnSignal(int iSignal);
462
463 #endif /* _WIN32 */
464
465 void DbgTestMutex(MUTEX hMutex, TCHAR *szName);
466 void DbgTestRWLock(RWLOCK hLock, TCHAR *szName);
467 void DbgPrintf(DWORD dwFlags, TCHAR *szFormat, ...);
468 void DumpSessions(void);
469
470
471 //
472 // Global variables
473 //
474
475 extern DWORD NXCORE_EXPORTABLE g_dwFlags;
476 extern char NXCORE_EXPORTABLE g_szConfigFile[];
477 extern char NXCORE_EXPORTABLE g_szLogFile[];
478 #ifndef _WIN32
479 extern char NXCORE_EXPORTABLE g_szPIDFile[];
480 #endif
481 extern char g_szDataDir[];
482
483 extern DB_HANDLE g_hCoreDB;
484 extern Queue *g_pLazyRequestQueue;
485
486 extern char *g_pszStatusName[];
487
488 #endif /* _nms_core_h_ */