all pollers converted to single thread pool
[public/netxms.git] / src / server / core / session.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2014 Raden Solutions
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: session.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24 #include <netxms_mt.h>
25 #include <nxtools.h>
26
27 #ifdef _WIN32
28 #include <psapi.h>
29 #define write _write
30 #define close _close
31 #else
32 #include <dirent.h>
33 #endif
34
35 #ifndef min
36 #define min(a,b) ((a) < (b) ? (a) : (b))
37 #endif
38
39 #ifndef S_ISREG
40 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
41 #endif
42
43 // WARNING! this hack works only for d2i_X509(); be carefull when adding new code
44 #ifdef OPENSSL_CONST
45 # undef OPENSSL_CONST
46 #endif
47 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
48 # define OPENSSL_CONST const
49 #else
50 # define OPENSSL_CONST
51 #endif
52
53 /**
54 * Constants
55 */
56 #define TRAP_CREATE 1
57 #define TRAP_UPDATE 2
58 #define TRAP_DELETE 3
59
60 #define MAX_MSG_SIZE 4194304
61
62 /**
63 * Externals
64 */
65 extern Queue g_nodePollerQueue;
66 extern Queue g_dataCollectionQueue;
67 extern Queue g_dciCacheLoaderQueue;
68
69 void UnregisterClientSession(int id);
70 void ResetDiscoveryPoller();
71 NXCPMessage *ForwardMessageToReportingServer(NXCPMessage *request, ClientSession *session);
72 void RemovePendingFileTransferRequests(ClientSession *session);
73
74 /**
75 * Node poller start data
76 */
77 typedef struct
78 {
79 ClientSession *pSession;
80 Node *pNode;
81 int iPollType;
82 UINT32 dwRqId;
83 } POLLER_START_DATA;
84
85 /**
86 * Additional processing thread start data
87 */
88 typedef struct
89 {
90 ClientSession *pSession;
91 NXCPMessage *pMsg;
92 } PROCTHREAD_START_DATA;
93
94 /**
95 * Object tool acl entry
96 */
97 typedef struct
98 {
99 UINT32 dwToolId;
100 UINT32 dwUserId;
101 } OBJECT_TOOL_ACL;
102
103 /**
104 *
105 */
106 typedef struct
107 {
108 uuid_t *guid;
109 bool removed;
110 } LIBRARY_IMAGE_UPDATE_INFO;
111
112 /**
113 * Callback to delete agent connections for loading files in distructor
114 */
115 static void DeleteCallback(NetObj* obj, void *data)
116 {
117 delete (AgentConnection *)obj;
118 }
119
120 /**
121 * Callback for sending image library update notifications
122 */
123 static void ImageLibraryUpdateCallback(ClientSession *pSession, void *pArg)
124 {
125 pSession->onLibraryImageChange((uuid_t *)pArg, false);
126 }
127
128 /**
129 * Callback for sending image library delete notifications
130 */
131 static void ImageLibraryDeleteCallback(ClientSession *pSession, void *pArg)
132 {
133 pSession->onLibraryImageChange((uuid_t *)pArg, true);
134 }
135
136 /**
137 * Additional message processing thread starters
138 */
139 #define CALL_IN_NEW_THREAD(func, msg) \
140 { \
141 PROCTHREAD_START_DATA *pData = (PROCTHREAD_START_DATA *)malloc(sizeof(PROCTHREAD_START_DATA)); \
142 pData->pSession = this; \
143 pData->pMsg = msg; \
144 msg = NULL; /* prevent deletion by main processing thread*/ \
145 m_dwRefCount++; \
146 ThreadPoolExecute(g_mainThreadPool, ThreadStarter_##func, pData); \
147 }
148
149 #define DEFINE_THREAD_STARTER(func) \
150 void ClientSession::ThreadStarter_##func(void *pArg) \
151 { \
152 ((PROCTHREAD_START_DATA *)pArg)->pSession->func(((PROCTHREAD_START_DATA *)pArg)->pMsg); \
153 ((PROCTHREAD_START_DATA *)pArg)->pSession->m_dwRefCount--; \
154 delete ((PROCTHREAD_START_DATA *)pArg)->pMsg; \
155 free(pArg); \
156 }
157
158 DEFINE_THREAD_STARTER(getCollectedData)
159 DEFINE_THREAD_STARTER(getTableCollectedData)
160 DEFINE_THREAD_STARTER(clearDCIData)
161 DEFINE_THREAD_STARTER(forceDCIPoll)
162 DEFINE_THREAD_STARTER(queryL2Topology)
163 DEFINE_THREAD_STARTER(sendEventLog)
164 DEFINE_THREAD_STARTER(sendSyslog)
165 DEFINE_THREAD_STARTER(createObject)
166 DEFINE_THREAD_STARTER(getServerFile)
167 DEFINE_THREAD_STARTER(getAgentFile)
168 DEFINE_THREAD_STARTER(cancelFileMonitoring)
169 DEFINE_THREAD_STARTER(queryServerLog)
170 DEFINE_THREAD_STARTER(getServerLogQueryData)
171 DEFINE_THREAD_STARTER(executeAction)
172 DEFINE_THREAD_STARTER(findNodeConnection)
173 DEFINE_THREAD_STARTER(findMacAddress)
174 DEFINE_THREAD_STARTER(findIpAddress)
175 DEFINE_THREAD_STARTER(processConsoleCommand)
176 DEFINE_THREAD_STARTER(sendMib)
177 DEFINE_THREAD_STARTER(getNetworkPath)
178 DEFINE_THREAD_STARTER(queryParameter)
179 DEFINE_THREAD_STARTER(queryAgentTable)
180 DEFINE_THREAD_STARTER(getAlarmEvents)
181 DEFINE_THREAD_STARTER(openHelpdeskIssue)
182 DEFINE_THREAD_STARTER(forwardToReportingServer)
183 DEFINE_THREAD_STARTER(fileManagerControl)
184 DEFINE_THREAD_STARTER(uploadUserFileToAgent)
185 DEFINE_THREAD_STARTER(getSwitchForwardingDatabase)
186 DEFINE_THREAD_STARTER(getRoutingTable)
187 DEFINE_THREAD_STARTER(getLocationHistory)
188 DEFINE_THREAD_STARTER(executeScript)
189
190 /**
191 * Client communication read thread starter
192 */
193 THREAD_RESULT THREAD_CALL ClientSession::readThreadStarter(void *pArg)
194 {
195 ((ClientSession *)pArg)->readThread();
196
197 // When ClientSession::ReadThread exits, all other session
198 // threads are already stopped, so we can safely destroy
199 // session object
200 UnregisterClientSession(((ClientSession *)pArg)->getId());
201 delete (ClientSession *)pArg;
202 return THREAD_OK;
203 }
204
205 /**
206 * Client communication write thread starter
207 */
208 THREAD_RESULT THREAD_CALL ClientSession::writeThreadStarter(void *pArg)
209 {
210 ((ClientSession *)pArg)->writeThread();
211 return THREAD_OK;
212 }
213
214 /**
215 * Received message processing thread starter
216 */
217 THREAD_RESULT THREAD_CALL ClientSession::processingThreadStarter(void *pArg)
218 {
219 ((ClientSession *)pArg)->processingThread();
220 return THREAD_OK;
221 }
222
223 /**
224 * Information update processing thread starter
225 */
226 THREAD_RESULT THREAD_CALL ClientSession::updateThreadStarter(void *pArg)
227 {
228 ((ClientSession *)pArg)->updateThread();
229 return THREAD_OK;
230 }
231
232 /**
233 * Client session class constructor
234 */
235 ClientSession::ClientSession(SOCKET hSocket, struct sockaddr *addr)
236 {
237 m_pSendQueue = new Queue;
238 m_pMessageQueue = new Queue;
239 m_pUpdateQueue = new Queue;
240 m_hSocket = hSocket;
241 m_id = -1;
242 m_state = SESSION_STATE_INIT;
243 m_pMsgBuffer = (NXCP_BUFFER *)malloc(sizeof(NXCP_BUFFER));
244 m_pCtx = NULL;
245 m_hWriteThread = INVALID_THREAD_HANDLE;
246 m_hProcessingThread = INVALID_THREAD_HANDLE;
247 m_hUpdateThread = INVALID_THREAD_HANDLE;
248 m_mutexSocketWrite = MutexCreate();
249 m_mutexSendEvents = MutexCreate();
250 m_mutexSendSyslog = MutexCreate();
251 m_mutexSendTrapLog = MutexCreate();
252 m_mutexSendObjects = MutexCreate();
253 m_mutexSendAlarms = MutexCreate();
254 m_mutexSendActions = MutexCreate();
255 m_mutexSendAuditLog = MutexCreate();
256 m_mutexSendSituations = MutexCreate();
257 m_mutexPollerInit = MutexCreate();
258 m_dwFlags = 0;
259 m_clientType = CLIENT_TYPE_DESKTOP;
260 m_clientAddr = (struct sockaddr *)nx_memdup(addr, (addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
261 if (addr->sa_family == AF_INET)
262 IpToStr(ntohl(((struct sockaddr_in *)m_clientAddr)->sin_addr.s_addr), m_workstation);
263 #ifdef WITH_IPV6
264 else
265 Ip6ToStr(((struct sockaddr_in6 *)m_clientAddr)->sin6_addr.s6_addr, m_workstation);
266 #endif
267 m_webServerAddress[0] = 0;
268 _tcscpy(m_szUserName, _T("<not logged in>"));
269 _tcscpy(m_szClientInfo, _T("n/a"));
270 m_dwUserId = INVALID_INDEX;
271 m_dwOpenDCIListSize = 0;
272 m_pOpenDCIList = NULL;
273 m_ppEPPRuleList = NULL;
274 m_hCurrFile = -1;
275 m_dwFileRqId = 0;
276 m_dwRefCount = 0;
277 m_dwEncryptionRqId = 0;
278 m_condEncryptionSetup = INVALID_CONDITION_HANDLE;
279 m_dwActiveChannels = 0;
280 m_console = NULL;
281 m_loginTime = time(NULL);
282 m_musicTypeList.add(_T("wav"));
283 _tcscpy(m_language, _T("en"));
284 }
285
286 /**
287 * Destructor
288 */
289 ClientSession::~ClientSession()
290 {
291 if (m_hSocket != -1)
292 closesocket(m_hSocket);
293 delete m_pSendQueue;
294 delete m_pMessageQueue;
295 delete m_pUpdateQueue;
296 safe_free(m_pMsgBuffer);
297 safe_free(m_clientAddr);
298 MutexDestroy(m_mutexSocketWrite);
299 MutexDestroy(m_mutexSendEvents);
300 MutexDestroy(m_mutexSendSyslog);
301 MutexDestroy(m_mutexSendTrapLog);
302 MutexDestroy(m_mutexSendObjects);
303 MutexDestroy(m_mutexSendAlarms);
304 MutexDestroy(m_mutexSendActions);
305 MutexDestroy(m_mutexSendAuditLog);
306 MutexDestroy(m_mutexSendSituations);
307 MutexDestroy(m_mutexPollerInit);
308 safe_free(m_pOpenDCIList);
309 if (m_ppEPPRuleList != NULL)
310 {
311 UINT32 i;
312
313 if (m_dwFlags & CSF_EPP_UPLOAD) // Aborted in the middle of EPP transfer
314 for(i = 0; i < m_dwRecordsUploaded; i++)
315 delete m_ppEPPRuleList[i];
316 free(m_ppEPPRuleList);
317 }
318 if (m_pCtx != NULL)
319 m_pCtx->decRefCount();
320 if (m_condEncryptionSetup != INVALID_CONDITION_HANDLE)
321 ConditionDestroy(m_condEncryptionSetup);
322
323 if (m_console != NULL)
324 {
325 delete m_console->pMsg;
326 free(m_console);
327 }
328 m_musicTypeList.clear();
329 if (m_agentConn.size() > 0)
330 {
331 m_agentConn.forEach(&DeleteCallback, NULL);
332 }
333 }
334
335 /**
336 * Start all threads
337 */
338 void ClientSession::run()
339 {
340 m_hWriteThread = ThreadCreateEx(writeThreadStarter, 0, this);
341 m_hProcessingThread = ThreadCreateEx(processingThreadStarter, 0, this);
342 m_hUpdateThread = ThreadCreateEx(updateThreadStarter, 0, this);
343 ThreadCreate(readThreadStarter, 0, this);
344 }
345
346 /**
347 * Print debug information
348 */
349 void ClientSession::debugPrintf(int level, const TCHAR *format, ...)
350 {
351 if (level <= (int)g_debugLevel)
352 {
353 va_list args;
354 TCHAR buffer[8192];
355
356 va_start(args, format);
357 _vsntprintf(buffer, 8192, format, args);
358 va_end(args);
359 DbgPrintf(level, _T("[CLSN-%d] %s"), m_id, buffer);
360 }
361 }
362
363 /**
364 * Read thread
365 */
366 void ClientSession::readThread()
367 {
368 TCHAR szBuffer[256];
369 UINT32 i;
370 NetObj *object;
371
372 SocketMessageReceiver receiver(m_hSocket, 4096, MAX_MSG_SIZE);
373 while(true)
374 {
375 MessageReceiverResult result;
376 NXCPMessage *msg = receiver.readMessage(900000, &result);
377
378 // Check for decryption error
379 if (result == MSGRECV_DECRYPTION_FAILURE)
380 {
381 debugPrintf(4, _T("readThread: Unable to decrypt received message"));
382 continue;
383 }
384
385 // Receive error
386 if (msg == NULL)
387 {
388 debugPrintf(5, _T("readThread: message receiving error (%s)"), AbstractMessageReceiver::resultToText(result));
389 break;
390 }
391
392 if (g_debugLevel >= 8)
393 {
394 String msgDump = NXCPMessage::dump(receiver.getRawMessageBuffer(), NXCP_VERSION);
395 debugPrintf(8, _T("Message dump:\n%s"), (const TCHAR *)msgDump);
396 }
397
398 // Special handling for raw messages
399 if (msg->isBinary())
400 {
401 debugPrintf(6, _T("Received raw message %s"), NXCPMessageCodeName(msg->getCode(), szBuffer));
402
403 if ((msg->getCode() == CMD_FILE_DATA) ||
404 (msg->getCode() == CMD_ABORT_FILE_TRANSFER))
405 {
406 if ((m_hCurrFile != -1) && (m_dwFileRqId == msg->getId()))
407 {
408 if (msg->getCode() == CMD_FILE_DATA)
409 {
410 if (write(m_hCurrFile, msg->getBinaryData(), (int)msg->getBinaryDataSize()) == (int)msg->getBinaryDataSize())
411 {
412 if (msg->isEndOfFile())
413 {
414 debugPrintf(6, _T("Got end of file marker"));
415 NXCPMessage response;
416
417 close(m_hCurrFile);
418 m_hCurrFile = -1;
419
420 response.setCode(CMD_REQUEST_COMPLETED);
421 response.setId(msg->getId());
422 response.setField(VID_RCC, RCC_SUCCESS);
423 sendMessage(&response);
424
425 onFileUpload(TRUE);
426 }
427 }
428 else
429 {
430 debugPrintf(6, _T("I/O error"));
431 // I/O error
432 NXCPMessage response;
433
434 close(m_hCurrFile);
435 m_hCurrFile = -1;
436
437 response.setCode(CMD_REQUEST_COMPLETED);
438 response.setId(msg->getId());
439 response.setField(VID_RCC, RCC_IO_ERROR);
440 sendMessage(&response);
441
442 onFileUpload(FALSE);
443 }
444 }
445 else
446 {
447 // Abort current file transfer because of client's problem
448 close(m_hCurrFile);
449 m_hCurrFile = -1;
450
451 onFileUpload(FALSE);
452 }
453 }
454 else
455 {
456 AgentConnection *conn = (AgentConnection *)m_agentConn.get(msg->getId());
457 if (conn != NULL)
458 {
459 if (msg->getCode() == CMD_FILE_DATA)
460 {
461 if (conn->sendMessage(msg)) //send raw message
462 {
463 if (msg->isEndOfFile())
464 {
465 debugPrintf(6, _T("Got end of file marker"));
466 //get response with specific ID if ok< then send ok, else send error
467 m_agentConn.remove(msg->getId());
468 delete conn;
469
470 NXCPMessage response;
471 response.setCode(CMD_REQUEST_COMPLETED);
472 response.setId(msg->getId());
473 response.setField(VID_RCC, RCC_SUCCESS);
474 sendMessage(&response);
475 }
476 }
477 else
478 {
479 debugPrintf(6, _T("Error while sending to agent"));
480 // I/O error
481 m_agentConn.remove(msg->getId());
482 delete conn;
483
484 NXCPMessage response;
485 response.setCode(CMD_REQUEST_COMPLETED);
486 response.setId(msg->getId());
487 response.setField(VID_RCC, RCC_IO_ERROR); //set result that came from agent
488 sendMessage(&response);
489 }
490 }
491 else
492 {
493 // Resend abort message
494 conn->sendMessage(msg);
495 m_agentConn.remove(msg->getId());
496 delete conn;
497 }
498 }
499 else
500 {
501 debugPrintf(4, _T("Out of state message (ID: %d)"), msg->getId());
502 }
503 }
504 }
505 delete msg;
506 }
507 else
508 {
509 if ((msg->getCode() == CMD_SESSION_KEY) && (msg->getId() == m_dwEncryptionRqId))
510 {
511 debugPrintf(6, _T("Received message %s"), NXCPMessageCodeName(msg->getCode(), szBuffer));
512 m_dwEncryptionResult = SetupEncryptionContext(msg, &m_pCtx, NULL, g_pServerKey, NXCP_VERSION);
513 receiver.setEncryptionContext(m_pCtx);
514 ConditionSet(m_condEncryptionSetup);
515 m_dwEncryptionRqId = 0;
516 delete msg;
517 }
518 else if (msg->getCode() == CMD_KEEPALIVE)
519 {
520 debugPrintf(6, _T("Received message %s"), NXCPMessageCodeName(msg->getCode(), szBuffer));
521 respondToKeepalive(msg->getId());
522 delete msg;
523 }
524 else
525 {
526 m_pMessageQueue->put(msg);
527 }
528 }
529 }
530
531 // Mark as terminated (sendMessage calls will not work after that point)
532 m_dwFlags |= CSF_TERMINATED;
533
534 // Finish update thread first
535 m_pUpdateQueue->put(INVALID_POINTER_VALUE);
536 ThreadJoin(m_hUpdateThread);
537
538 // Notify other threads to exit
539 NXCP_MESSAGE *rawMsg;
540 while((rawMsg = (NXCP_MESSAGE *)m_pSendQueue->get()) != NULL)
541 free(rawMsg);
542 m_pSendQueue->put(INVALID_POINTER_VALUE);
543
544 NXCPMessage *msg;
545 while((msg = (NXCPMessage *)m_pMessageQueue->get()) != NULL)
546 delete msg;
547 m_pMessageQueue->put(INVALID_POINTER_VALUE);
548
549 // Wait for other threads to finish
550 ThreadJoin(m_hWriteThread);
551 ThreadJoin(m_hProcessingThread);
552
553 // Abort current file upload operation, if any
554 if (m_hCurrFile != -1)
555 {
556 close(m_hCurrFile);
557 m_hCurrFile = -1;
558 onFileUpload(FALSE);
559 }
560
561 // remove all pending file transfers from reporting server
562 RemovePendingFileTransferRequests(this);
563
564 // Remove all locks created by this session
565 RemoveAllSessionLocks(m_id);
566 for(i = 0; i < m_dwOpenDCIListSize; i++)
567 {
568 object = FindObjectById(m_pOpenDCIList[i]);
569 if (object != NULL)
570 if ((object->getObjectClass() == OBJECT_NODE) ||
571 (object->getObjectClass() == OBJECT_CLUSTER) ||
572 (object->getObjectClass() == OBJECT_TEMPLATE))
573 ((Template *)object)->unlockDCIList(m_id);
574 }
575
576 // Waiting while reference count becomes 0
577 if (m_dwRefCount > 0)
578 {
579 debugPrintf(3, _T("Waiting for pending requests..."));
580 do
581 {
582 ThreadSleep(1);
583 } while(m_dwRefCount > 0);
584 }
585
586 if (m_dwFlags & CSF_AUTHENTICATED)
587 {
588 CALL_ALL_MODULES(pfClientSessionClose, (this));
589 WriteAuditLog(AUDIT_SECURITY, TRUE, m_dwUserId, m_workstation, m_id, 0, _T("User logged out (client: %s)"), m_szClientInfo);
590 }
591 debugPrintf(3, _T("Session closed"));
592 }
593
594 /**
595 * Network write thread
596 */
597 void ClientSession::writeThread()
598 {
599 while(true)
600 {
601 NXCP_MESSAGE *rawMsg = (NXCP_MESSAGE *)m_pSendQueue->getOrBlock();
602 if (rawMsg == INVALID_POINTER_VALUE) // Session termination indicator
603 break;
604
605 sendRawMessage(rawMsg);
606 free(rawMsg);
607 }
608 }
609
610 /**
611 * Update processing thread
612 */
613 void ClientSession::updateThread()
614 {
615 UPDATE_INFO *pUpdate;
616 NXCPMessage msg;
617
618 while(1)
619 {
620 pUpdate = (UPDATE_INFO *)m_pUpdateQueue->getOrBlock();
621 if (pUpdate == INVALID_POINTER_VALUE) // Session termination indicator
622 break;
623
624 switch(pUpdate->dwCategory)
625 {
626 case INFO_CAT_EVENT:
627 MutexLock(m_mutexSendEvents);
628 sendMessage((NXCPMessage *)pUpdate->pData);
629 MutexUnlock(m_mutexSendEvents);
630 delete (NXCPMessage *)pUpdate->pData;
631 break;
632 case INFO_CAT_SYSLOG_MSG:
633 MutexLock(m_mutexSendSyslog);
634 msg.setCode(CMD_SYSLOG_RECORDS);
635 CreateMessageFromSyslogMsg(&msg, (NX_SYSLOG_RECORD *)pUpdate->pData);
636 sendMessage(&msg);
637 MutexUnlock(m_mutexSendSyslog);
638 free(pUpdate->pData);
639 break;
640 case INFO_CAT_SNMP_TRAP:
641 MutexLock(m_mutexSendTrapLog);
642 sendMessage((NXCPMessage *)pUpdate->pData);
643 MutexUnlock(m_mutexSendTrapLog);
644 delete (NXCPMessage *)pUpdate->pData;
645 break;
646 case INFO_CAT_AUDIT_RECORD:
647 MutexLock(m_mutexSendAuditLog);
648 sendMessage((NXCPMessage *)pUpdate->pData);
649 MutexUnlock(m_mutexSendAuditLog);
650 delete (NXCPMessage *)pUpdate->pData;
651 break;
652 case INFO_CAT_OBJECT_CHANGE:
653 MutexLock(m_mutexSendObjects);
654 msg.setCode(CMD_OBJECT_UPDATE);
655 if (!((NetObj *)pUpdate->pData)->isDeleted())
656 {
657 ((NetObj *)pUpdate->pData)->fillMessage(&msg);
658 if (m_dwFlags & CSF_SYNC_OBJECT_COMMENTS)
659 ((NetObj *)pUpdate->pData)->commentsToMessage(&msg);
660 }
661 else
662 {
663 msg.setField(VID_OBJECT_ID, ((NetObj *)pUpdate->pData)->getId());
664 msg.setField(VID_IS_DELETED, (WORD)1);
665 }
666 sendMessage(&msg);
667 MutexUnlock(m_mutexSendObjects);
668 msg.deleteAllFields();
669 ((NetObj *)pUpdate->pData)->decRefCount();
670 break;
671 case INFO_CAT_ALARM:
672 MutexLock(m_mutexSendAlarms);
673 msg.setCode(CMD_ALARM_UPDATE);
674 msg.setField(VID_NOTIFICATION_CODE, pUpdate->dwCode);
675 FillAlarmInfoMessage(&msg, (NXC_ALARM *)pUpdate->pData);
676 sendMessage(&msg);
677 MutexUnlock(m_mutexSendAlarms);
678 msg.deleteAllFields();
679 free(pUpdate->pData);
680 break;
681 case INFO_CAT_ACTION:
682 MutexLock(m_mutexSendActions);
683 msg.setCode(CMD_ACTION_DB_UPDATE);
684 msg.setField(VID_NOTIFICATION_CODE, pUpdate->dwCode);
685 msg.setField(VID_ACTION_ID, ((NXC_ACTION *)pUpdate->pData)->dwId);
686 if (pUpdate->dwCode != NX_NOTIFY_ACTION_DELETED)
687 FillActionInfoMessage(&msg, (NXC_ACTION *)pUpdate->pData);
688 sendMessage(&msg);
689 MutexUnlock(m_mutexSendActions);
690 msg.deleteAllFields();
691 free(pUpdate->pData);
692 break;
693 case INFO_CAT_SITUATION:
694 MutexLock(m_mutexSendSituations);
695 sendMessage((NXCPMessage *)pUpdate->pData);
696 MutexUnlock(m_mutexSendSituations);
697 delete (NXCPMessage *)pUpdate->pData;
698 break;
699 case INFO_CAT_LIBRARY_IMAGE:
700 {
701 LIBRARY_IMAGE_UPDATE_INFO *info = (LIBRARY_IMAGE_UPDATE_INFO *)pUpdate->pData;
702 msg.setCode(CMD_IMAGE_LIBRARY_UPDATE);
703 msg.setField(VID_GUID, (BYTE *)info->guid, UUID_LENGTH);
704 if (info->removed)
705 {
706 msg.setField(VID_FLAGS, (UINT32)1);
707 }
708 else
709 {
710 msg.setField(VID_FLAGS, (UINT32)0);
711 }
712 sendMessage(&msg);
713 msg.deleteAllFields();
714 free(info->guid);
715 free(info);
716 }
717 break;
718 default:
719 break;
720 }
721
722 free(pUpdate);
723 }
724 }
725
726 /**
727 * Message processing thread
728 */
729 void ClientSession::processingThread()
730 {
731 NXCPMessage *pMsg;
732 TCHAR szBuffer[128];
733 UINT32 i;
734 int status;
735
736 while(1)
737 {
738 pMsg = (NXCPMessage *)m_pMessageQueue->getOrBlock();
739 if (pMsg == INVALID_POINTER_VALUE) // Session termination indicator
740 break;
741
742 m_wCurrentCmd = pMsg->getCode();
743 debugPrintf(6, _T("Received message %s"), NXCPMessageCodeName(m_wCurrentCmd, szBuffer));
744 if (!(m_dwFlags & CSF_AUTHENTICATED) &&
745 (m_wCurrentCmd != CMD_LOGIN) &&
746 (m_wCurrentCmd != CMD_GET_SERVER_INFO) &&
747 (m_wCurrentCmd != CMD_REQUEST_ENCRYPTION) &&
748 (m_wCurrentCmd != CMD_GET_MY_CONFIG) &&
749 (m_wCurrentCmd != CMD_REGISTER_AGENT))
750 {
751 delete pMsg;
752 continue;
753 }
754
755 m_state = SESSION_STATE_PROCESSING;
756 switch(m_wCurrentCmd)
757 {
758 case CMD_LOGIN:
759 login(pMsg);
760 break;
761 case CMD_GET_SERVER_INFO:
762 sendServerInfo(pMsg->getId());
763 break;
764 case CMD_GET_MY_CONFIG:
765 sendConfigForAgent(pMsg);
766 break;
767 case CMD_GET_OBJECTS:
768 sendAllObjects(pMsg);
769 break;
770 case CMD_GET_SELECTED_OBJECTS:
771 sendSelectedObjects(pMsg);
772 break;
773 case CMD_GET_EVENTS:
774 CALL_IN_NEW_THREAD(sendEventLog, pMsg);
775 break;
776 case CMD_GET_CONFIG_VARLIST:
777 sendAllConfigVars(pMsg->getId());
778 break;
779 case CMD_SET_CONFIG_VARIABLE:
780 setConfigVariable(pMsg);
781 break;
782 case CMD_DELETE_CONFIG_VARIABLE:
783 deleteConfigVariable(pMsg);
784 break;
785 case CMD_CONFIG_GET_CLOB:
786 getConfigCLOB(pMsg);
787 break;
788 case CMD_CONFIG_SET_CLOB:
789 setConfigCLOB(pMsg);
790 break;
791 case CMD_LOAD_EVENT_DB:
792 sendEventDB(pMsg->getId());
793 break;
794 case CMD_SET_EVENT_INFO:
795 modifyEventTemplate(pMsg);
796 break;
797 case CMD_DELETE_EVENT_TEMPLATE:
798 deleteEventTemplate(pMsg);
799 break;
800 case CMD_GENERATE_EVENT_CODE:
801 generateEventCode(pMsg->getId());
802 break;
803 case CMD_MODIFY_OBJECT:
804 modifyObject(pMsg);
805 break;
806 case CMD_SET_OBJECT_MGMT_STATUS:
807 changeObjectMgmtStatus(pMsg);
808 break;
809 case CMD_LOAD_USER_DB:
810 sendUserDB(pMsg->getId());
811 break;
812 case CMD_CREATE_USER:
813 createUser(pMsg);
814 break;
815 case CMD_UPDATE_USER:
816 updateUser(pMsg);
817 break;
818 case CMD_DELETE_USER:
819 deleteUser(pMsg);
820 break;
821 case CMD_LOCK_USER_DB:
822 lockUserDB(pMsg->getId(), TRUE);
823 break;
824 case CMD_UNLOCK_USER_DB:
825 lockUserDB(pMsg->getId(), FALSE);
826 break;
827 case CMD_SET_PASSWORD:
828 setPassword(pMsg);
829 break;
830 case CMD_GET_NODE_DCI_LIST:
831 openNodeDCIList(pMsg);
832 break;
833 case CMD_UNLOCK_NODE_DCI_LIST:
834 closeNodeDCIList(pMsg);
835 break;
836 case CMD_CREATE_NEW_DCI:
837 case CMD_MODIFY_NODE_DCI:
838 case CMD_DELETE_NODE_DCI:
839 modifyNodeDCI(pMsg);
840 break;
841 case CMD_SET_DCI_STATUS:
842 changeDCIStatus(pMsg);
843 break;
844 case CMD_COPY_DCI:
845 copyDCI(pMsg);
846 break;
847 case CMD_APPLY_TEMPLATE:
848 applyTemplate(pMsg);
849 break;
850 case CMD_GET_DCI_DATA:
851 CALL_IN_NEW_THREAD(getCollectedData, pMsg);
852 break;
853 case CMD_GET_TABLE_DCI_DATA:
854 CALL_IN_NEW_THREAD(getTableCollectedData, pMsg);
855 break;
856 case CMD_CLEAR_DCI_DATA:
857 CALL_IN_NEW_THREAD(clearDCIData, pMsg);
858 break;
859 case CMD_FORCE_DCI_POLL:
860 CALL_IN_NEW_THREAD(forceDCIPoll, pMsg);
861 break;
862 case CMD_OPEN_EPP:
863 openEPP(pMsg);
864 break;
865 case CMD_CLOSE_EPP:
866 closeEPP(pMsg->getId());
867 break;
868 case CMD_SAVE_EPP:
869 saveEPP(pMsg);
870 break;
871 case CMD_EPP_RECORD:
872 processEPPRecord(pMsg);
873 break;
874 case CMD_GET_MIB_TIMESTAMP:
875 sendMIBTimestamp(pMsg->getId());
876 break;
877 case CMD_GET_MIB:
878 CALL_IN_NEW_THREAD(sendMib, pMsg);
879 break;
880 case CMD_CREATE_OBJECT:
881 CALL_IN_NEW_THREAD(createObject, pMsg);
882 break;
883 case CMD_BIND_OBJECT:
884 changeObjectBinding(pMsg, TRUE);
885 break;
886 case CMD_UNBIND_OBJECT:
887 changeObjectBinding(pMsg, FALSE);
888 break;
889 case CMD_ADD_CLUSTER_NODE:
890 addClusterNode(pMsg);
891 break;
892 case CMD_GET_ALL_ALARMS:
893 sendAllAlarms(pMsg->getId());
894 break;
895 case CMD_GET_ALARM_COMMENTS:
896 getAlarmComments(pMsg);
897 break;
898 case CMD_SET_ALARM_STATUS_FLOW:
899 updateAlarmStatusFlow(pMsg);
900 break;
901 case CMD_UPDATE_ALARM_COMMENT:
902 updateAlarmComment(pMsg);
903 break;
904 case CMD_DELETE_ALARM_COMMENT:
905 deleteAlarmComment(pMsg);
906 break;
907 case CMD_GET_ALARM:
908 getAlarm(pMsg);
909 break;
910 case CMD_GET_ALARM_EVENTS:
911 CALL_IN_NEW_THREAD(getAlarmEvents, pMsg);
912 break;
913 case CMD_ACK_ALARM:
914 acknowledgeAlarm(pMsg);
915 break;
916 case CMD_RESOLVE_ALARM:
917 resolveAlarm(pMsg, false);
918 break;
919 case CMD_TERMINATE_ALARM:
920 resolveAlarm(pMsg, true);
921 break;
922 case CMD_DELETE_ALARM:
923 deleteAlarm(pMsg);
924 break;
925 case CMD_OPEN_HELPDESK_ISSUE:
926 CALL_IN_NEW_THREAD(openHelpdeskIssue, pMsg);
927 break;
928 case CMD_GET_HELPDESK_URL:
929 getHelpdeskUrl(pMsg);
930 break;
931 case CMD_UNLINK_HELPDESK_ISSUE:
932 unlinkHelpdeskIssue(pMsg);
933 break;
934 case CMD_CREATE_ACTION:
935 createAction(pMsg);
936 break;
937 case CMD_MODIFY_ACTION:
938 updateAction(pMsg);
939 break;
940 case CMD_DELETE_ACTION:
941 deleteAction(pMsg);
942 break;
943 case CMD_LOAD_ACTIONS:
944 sendAllActions(pMsg->getId());
945 break;
946 case CMD_GET_CONTAINER_CAT_LIST:
947 SendContainerCategories(pMsg->getId());
948 break;
949 case CMD_DELETE_OBJECT:
950 deleteObject(pMsg);
951 break;
952 case CMD_POLL_NODE:
953 forcedNodePoll(pMsg);
954 break;
955 case CMD_TRAP:
956 onTrap(pMsg);
957 break;
958 case CMD_WAKEUP_NODE:
959 onWakeUpNode(pMsg);
960 break;
961 case CMD_CREATE_TRAP:
962 editTrap(TRAP_CREATE, pMsg);
963 break;
964 case CMD_MODIFY_TRAP:
965 editTrap(TRAP_UPDATE, pMsg);
966 break;
967 case CMD_DELETE_TRAP:
968 editTrap(TRAP_DELETE, pMsg);
969 break;
970 case CMD_LOAD_TRAP_CFG:
971 sendAllTraps(pMsg->getId());
972 break;
973 case CMD_GET_TRAP_CFG_RO:
974 sendAllTraps2(pMsg->getId());
975 break;
976 case CMD_QUERY_PARAMETER:
977 CALL_IN_NEW_THREAD(queryParameter, pMsg);
978 break;
979 case CMD_QUERY_TABLE:
980 CALL_IN_NEW_THREAD(queryAgentTable, pMsg);
981 break;
982 case CMD_LOCK_PACKAGE_DB:
983 LockPackageDB(pMsg->getId(), TRUE);
984 break;
985 case CMD_UNLOCK_PACKAGE_DB:
986 LockPackageDB(pMsg->getId(), FALSE);
987 break;
988 case CMD_GET_PACKAGE_LIST:
989 SendAllPackages(pMsg->getId());
990 break;
991 case CMD_INSTALL_PACKAGE:
992 InstallPackage(pMsg);
993 break;
994 case CMD_REMOVE_PACKAGE:
995 RemovePackage(pMsg);
996 break;
997 case CMD_GET_PARAMETER_LIST:
998 getParametersList(pMsg);
999 break;
1000 case CMD_DEPLOY_PACKAGE:
1001 DeployPackage(pMsg);
1002 break;
1003 case CMD_GET_LAST_VALUES:
1004 getLastValues(pMsg);
1005 break;
1006 case CMD_GET_DCI_VALUES:
1007 getLastValuesByDciId(pMsg);
1008 break;
1009 case CMD_GET_TABLE_LAST_VALUES:
1010 getTableLastValues(pMsg);
1011 break;
1012 case CMD_GET_THRESHOLD_SUMMARY:
1013 getThresholdSummary(pMsg);
1014 break;
1015 case CMD_GET_USER_VARIABLE:
1016 getUserVariable(pMsg);
1017 break;
1018 case CMD_SET_USER_VARIABLE:
1019 setUserVariable(pMsg);
1020 break;
1021 case CMD_DELETE_USER_VARIABLE:
1022 deleteUserVariable(pMsg);
1023 break;
1024 case CMD_ENUM_USER_VARIABLES:
1025 enumUserVariables(pMsg);
1026 break;
1027 case CMD_COPY_USER_VARIABLE:
1028 copyUserVariable(pMsg);
1029 break;
1030 case CMD_CHANGE_ZONE:
1031 changeObjectZone(pMsg);
1032 break;
1033 case CMD_REQUEST_ENCRYPTION:
1034 setupEncryption(pMsg);
1035 break;
1036 case CMD_GET_AGENT_CONFIG:
1037 getAgentConfig(pMsg);
1038 break;
1039 case CMD_UPDATE_AGENT_CONFIG:
1040 updateAgentConfig(pMsg);
1041 break;
1042 case CMD_EXECUTE_ACTION:
1043 CALL_IN_NEW_THREAD(executeAction, pMsg);
1044 break;
1045 case CMD_GET_OBJECT_TOOLS:
1046 sendObjectTools(pMsg->getId());
1047 break;
1048 case CMD_EXEC_TABLE_TOOL:
1049 execTableTool(pMsg);
1050 break;
1051 case CMD_GET_OBJECT_TOOL_DETAILS:
1052 sendObjectToolDetails(pMsg);
1053 break;
1054 case CMD_UPDATE_OBJECT_TOOL:
1055 updateObjectTool(pMsg);
1056 break;
1057 case CMD_DELETE_OBJECT_TOOL:
1058 deleteObjectTool(pMsg);
1059 break;
1060 case CMD_CHANGE_OBJECT_TOOL_STATUS:
1061 changeObjectToolStatus(pMsg);
1062 break;
1063 case CMD_GENERATE_OBJECT_TOOL_ID:
1064 generateObjectToolId(pMsg->getId());
1065 break;
1066 case CMD_CHANGE_SUBSCRIPTION:
1067 changeSubscription(pMsg);
1068 break;
1069 case CMD_GET_SYSLOG:
1070 CALL_IN_NEW_THREAD(sendSyslog, pMsg);
1071 break;
1072 case CMD_GET_SERVER_STATS:
1073 sendServerStats(pMsg->getId());
1074 break;
1075 case CMD_GET_SCRIPT_LIST:
1076 sendScriptList(pMsg->getId());
1077 break;
1078 case CMD_GET_SCRIPT:
1079 sendScript(pMsg);
1080 break;
1081 case CMD_UPDATE_SCRIPT:
1082 updateScript(pMsg);
1083 break;
1084 case CMD_RENAME_SCRIPT:
1085 renameScript(pMsg);
1086 break;
1087 case CMD_DELETE_SCRIPT:
1088 deleteScript(pMsg);
1089 break;
1090 case CMD_GET_SESSION_LIST:
1091 SendSessionList(pMsg->getId());
1092 break;
1093 case CMD_KILL_SESSION:
1094 KillSession(pMsg);
1095 break;
1096 case CMD_GET_TRAP_LOG:
1097 SendTrapLog(pMsg);
1098 break;
1099 case CMD_START_SNMP_WALK:
1100 StartSnmpWalk(pMsg);
1101 break;
1102 case CMD_RESOLVE_DCI_NAMES:
1103 resolveDCINames(pMsg);
1104 break;
1105 case CMD_GET_DCI_INFO:
1106 SendDCIInfo(pMsg);
1107 break;
1108 case CMD_GET_DCI_THRESHOLDS:
1109 sendDCIThresholds(pMsg);
1110 break;
1111 case CMD_GET_DCI_EVENTS_LIST:
1112 getDCIEventList(pMsg);
1113 break;
1114 case CMD_GET_DCI_SCRIPT_LIST:
1115 getDCIScriptList(pMsg);
1116 break;
1117 case CMD_GET_PERFTAB_DCI_LIST:
1118 sendPerfTabDCIList(pMsg);
1119 break;
1120 case CMD_PUSH_DCI_DATA:
1121 pushDCIData(pMsg);
1122 break;
1123 case CMD_GET_AGENT_CFG_LIST:
1124 sendAgentCfgList(pMsg->getId());
1125 break;
1126 case CMD_OPEN_AGENT_CONFIG:
1127 OpenAgentConfig(pMsg);
1128 break;
1129 case CMD_SAVE_AGENT_CONFIG:
1130 SaveAgentConfig(pMsg);
1131 break;
1132 case CMD_DELETE_AGENT_CONFIG:
1133 DeleteAgentConfig(pMsg);
1134 break;
1135 case CMD_SWAP_AGENT_CONFIGS:
1136 SwapAgentConfigs(pMsg);
1137 break;
1138 case CMD_GET_OBJECT_COMMENTS:
1139 SendObjectComments(pMsg);
1140 break;
1141 case CMD_UPDATE_OBJECT_COMMENTS:
1142 updateObjectComments(pMsg);
1143 break;
1144 case CMD_GET_ADDR_LIST:
1145 getAddrList(pMsg);
1146 break;
1147 case CMD_SET_ADDR_LIST:
1148 setAddrList(pMsg);
1149 break;
1150 case CMD_RESET_COMPONENT:
1151 resetComponent(pMsg);
1152 break;
1153 case CMD_EXPORT_CONFIGURATION:
1154 exportConfiguration(pMsg);
1155 break;
1156 case CMD_IMPORT_CONFIGURATION:
1157 importConfiguration(pMsg);
1158 break;
1159 case CMD_GET_GRAPH_LIST:
1160 sendGraphList(pMsg->getId());
1161 break;
1162 case CMD_SAVE_GRAPH:
1163 saveGraph(pMsg);
1164 break;
1165 case CMD_DELETE_GRAPH:
1166 deleteGraph(pMsg);
1167 break;
1168 case CMD_ADD_CA_CERTIFICATE:
1169 AddCACertificate(pMsg);
1170 break;
1171 case CMD_DELETE_CERTIFICATE:
1172 DeleteCertificate(pMsg);
1173 break;
1174 case CMD_UPDATE_CERT_COMMENTS:
1175 UpdateCertificateComments(pMsg);
1176 break;
1177 case CMD_GET_CERT_LIST:
1178 getCertificateList(pMsg->getId());
1179 break;
1180 case CMD_QUERY_L2_TOPOLOGY:
1181 CALL_IN_NEW_THREAD(queryL2Topology, pMsg);
1182 break;
1183 case CMD_SEND_SMS:
1184 sendSMS(pMsg);
1185 break;
1186 case CMD_GET_COMMUNITY_LIST:
1187 SendCommunityList(pMsg->getId());
1188 break;
1189 case CMD_UPDATE_COMMUNITY_LIST:
1190 UpdateCommunityList(pMsg);
1191 break;
1192 case CMD_GET_USM_CREDENTIALS:
1193 sendUsmCredentials(pMsg->getId());
1194 break;
1195 case CMD_UPDATE_USM_CREDENTIALS:
1196 updateUsmCredentials(pMsg);
1197 break;
1198 case CMD_GET_SITUATION_LIST:
1199 getSituationList(pMsg->getId());
1200 break;
1201 case CMD_CREATE_SITUATION:
1202 createSituation(pMsg);
1203 break;
1204 case CMD_UPDATE_SITUATION:
1205 updateSituation(pMsg);
1206 break;
1207 case CMD_DELETE_SITUATION:
1208 deleteSituation(pMsg);
1209 break;
1210 case CMD_DEL_SITUATION_INSTANCE:
1211 deleteSituationInstance(pMsg);
1212 break;
1213 case CMD_REGISTER_AGENT:
1214 registerAgent(pMsg);
1215 break;
1216 case CMD_GET_SERVER_FILE:
1217 CALL_IN_NEW_THREAD(getServerFile, pMsg);
1218 break;
1219 case CMD_GET_AGENT_FILE:
1220 CALL_IN_NEW_THREAD(getAgentFile, pMsg);
1221 break;
1222 case CMD_CANCEL_FILE_MONITORING:
1223 CALL_IN_NEW_THREAD(cancelFileMonitoring, pMsg);
1224 break;
1225 case CMD_TEST_DCI_TRANSFORMATION:
1226 testDCITransformation(pMsg);
1227 break;
1228 case CMD_EXECUTE_SCRIPT:
1229 CALL_IN_NEW_THREAD(executeScript, pMsg);
1230 break;
1231 case CMD_GET_JOB_LIST:
1232 sendJobList(pMsg->getId());
1233 break;
1234 case CMD_CANCEL_JOB:
1235 cancelJob(pMsg);
1236 break;
1237 case CMD_HOLD_JOB:
1238 holdJob(pMsg);
1239 break;
1240 case CMD_UNHOLD_JOB:
1241 unholdJob(pMsg);
1242 break;
1243 case CMD_DEPLOY_AGENT_POLICY:
1244 deployAgentPolicy(pMsg, false);
1245 break;
1246 case CMD_UNINSTALL_AGENT_POLICY:
1247 deployAgentPolicy(pMsg, true);
1248 break;
1249 case CMD_GET_CURRENT_USER_ATTR:
1250 getUserCustomAttribute(pMsg);
1251 break;
1252 case CMD_SET_CURRENT_USER_ATTR:
1253 setUserCustomAttribute(pMsg);
1254 break;
1255 case CMD_OPEN_SERVER_LOG:
1256 openServerLog(pMsg);
1257 break;
1258 case CMD_CLOSE_SERVER_LOG:
1259 closeServerLog(pMsg);
1260 break;
1261 case CMD_QUERY_LOG:
1262 CALL_IN_NEW_THREAD(queryServerLog, pMsg);
1263 break;
1264 case CMD_GET_LOG_DATA:
1265 CALL_IN_NEW_THREAD(getServerLogQueryData, pMsg);
1266 break;
1267 case CMD_FIND_NODE_CONNECTION:
1268 CALL_IN_NEW_THREAD(findNodeConnection, pMsg);
1269 break;
1270 case CMD_FIND_MAC_LOCATION:
1271 CALL_IN_NEW_THREAD(findMacAddress, pMsg);
1272 break;
1273 case CMD_FIND_IP_LOCATION:
1274 CALL_IN_NEW_THREAD(findIpAddress, pMsg);
1275 break;
1276 case CMD_GET_IMAGE:
1277 sendLibraryImage(pMsg);
1278 break;
1279 case CMD_CREATE_IMAGE:
1280 updateLibraryImage(pMsg);
1281 break;
1282 case CMD_DELETE_IMAGE:
1283 deleteLibraryImage(pMsg);
1284 break;
1285 case CMD_MODIFY_IMAGE:
1286 updateLibraryImage(pMsg);
1287 break;
1288 case CMD_LIST_IMAGES:
1289 listLibraryImages(pMsg);
1290 break;
1291 case CMD_EXECUTE_SERVER_COMMAND:
1292 executeServerCommand(pMsg);
1293 break;
1294 case CMD_LIST_SERVER_FILES:
1295 listServerFileStore(pMsg);
1296 break;
1297 case CMD_UPLOAD_FILE_TO_AGENT:
1298 uploadFileToAgent(pMsg);
1299 break;
1300 case CMD_UPLOAD_FILE:
1301 receiveFile(pMsg);
1302 break;
1303 case CMD_DELETE_FILE:
1304 deleteFile(pMsg);
1305 break;
1306 case CMD_OPEN_CONSOLE:
1307 openConsole(pMsg->getId());
1308 break;
1309 case CMD_CLOSE_CONSOLE:
1310 closeConsole(pMsg->getId());
1311 break;
1312 case CMD_ADM_REQUEST:
1313 CALL_IN_NEW_THREAD(processConsoleCommand, pMsg);
1314 break;
1315 case CMD_GET_VLANS:
1316 getVlans(pMsg);
1317 break;
1318 case CMD_GET_NETWORK_PATH:
1319 CALL_IN_NEW_THREAD(getNetworkPath, pMsg);
1320 break;
1321 case CMD_GET_NODE_COMPONENTS:
1322 getNodeComponents(pMsg);
1323 break;
1324 case CMD_GET_NODE_SOFTWARE:
1325 getNodeSoftware(pMsg);
1326 break;
1327 case CMD_GET_WINPERF_OBJECTS:
1328 getWinPerfObjects(pMsg);
1329 break;
1330 case CMD_LIST_MAPPING_TABLES:
1331 listMappingTables(pMsg);
1332 break;
1333 case CMD_GET_MAPPING_TABLE:
1334 getMappingTable(pMsg);
1335 break;
1336 case CMD_UPDATE_MAPPING_TABLE:
1337 updateMappingTable(pMsg);
1338 break;
1339 case CMD_DELETE_MAPPING_TABLE:
1340 deleteMappingTable(pMsg);
1341 break;
1342 case CMD_GET_WIRELESS_STATIONS:
1343 getWirelessStations(pMsg);
1344 break;
1345 case CMD_GET_SUMMARY_TABLES:
1346 getSummaryTables(pMsg->getId());
1347 break;
1348 case CMD_GET_SUMMARY_TABLE_DETAILS:
1349 getSummaryTableDetails(pMsg);
1350 break;
1351 case CMD_MODIFY_SUMMARY_TABLE:
1352 modifySummaryTable(pMsg);
1353 break;
1354 case CMD_DELETE_SUMMARY_TABLE:
1355 deleteSummaryTable(pMsg);
1356 break;
1357 case CMD_QUERY_SUMMARY_TABLE:
1358 querySummaryTable(pMsg);
1359 break;
1360 case CMD_QUERY_ADHOC_SUMMARY_TABLE:
1361 queryAdHocSummaryTable(pMsg);
1362 break;
1363 case CMD_GET_SUBNET_ADDRESS_MAP:
1364 getSubnetAddressMap(pMsg);
1365 break;
1366 case CMD_GET_EFFECTIVE_RIGHTS:
1367 getEffectiveRights(pMsg);
1368 break;
1369 case CMD_GET_FOLDER_CONTENT:
1370 case CMD_FILEMGR_DELETE_FILE:
1371 case CMD_FILEMGR_RENAME_FILE:
1372 case CMD_FILEMGR_MOVE_FILE:
1373 case CMD_FILEMGR_CREATE_FOLDER:
1374 CALL_IN_NEW_THREAD(fileManagerControl, pMsg);
1375 break;
1376 case CMD_FILEMGR_UPLOAD:
1377 CALL_IN_NEW_THREAD(uploadUserFileToAgent, pMsg);
1378 break;
1379 case CMD_GET_SWITCH_FDB:
1380 CALL_IN_NEW_THREAD(getSwitchForwardingDatabase, pMsg);
1381 break;
1382 case CMD_GET_ROUTING_TABLE:
1383 CALL_IN_NEW_THREAD(getRoutingTable, pMsg);
1384 break;
1385 case CMD_GET_LOC_HISTORY:
1386 CALL_IN_NEW_THREAD(getLocationHistory, pMsg);
1387 break;
1388 case CMD_TAKE_SCREENSHOT:
1389 getScreenshot(pMsg);
1390 break;
1391 default:
1392 if ((m_wCurrentCmd >> 8) == 0x11)
1393 {
1394 // Reporting Server range (0x1100 - 0x11FF)
1395 CALL_IN_NEW_THREAD(forwardToReportingServer, pMsg);
1396 break;
1397 }
1398
1399 // Pass message to loaded modules
1400 for(i = 0; i < g_dwNumModules; i++)
1401 {
1402 if (g_pModuleList[i].pfClientCommandHandler != NULL)
1403 {
1404 status = g_pModuleList[i].pfClientCommandHandler(m_wCurrentCmd, pMsg, this);
1405 if (status != NXMOD_COMMAND_IGNORED)
1406 {
1407 if (status == NXMOD_COMMAND_ACCEPTED_ASYNC)
1408 {
1409 pMsg = NULL; // Prevent deletion
1410 m_dwRefCount++;
1411 }
1412 break; // Message was processed by the module
1413 }
1414 }
1415 }
1416 if (i == g_dwNumModules)
1417 {
1418 NXCPMessage response;
1419
1420 response.setId(pMsg->getId());
1421 response.setCode(CMD_REQUEST_COMPLETED);
1422 response.setField(VID_RCC, RCC_NOT_IMPLEMENTED);
1423 sendMessage(&response);
1424 }
1425 break;
1426 }
1427 delete pMsg;
1428 m_state = (m_dwFlags & CSF_AUTHENTICATED) ? SESSION_STATE_IDLE : SESSION_STATE_INIT;
1429 }
1430 }
1431
1432 /**
1433 * Respond to client's keepalive message
1434 */
1435 void ClientSession::respondToKeepalive(UINT32 dwRqId)
1436 {
1437 NXCPMessage msg;
1438
1439 msg.setCode(CMD_REQUEST_COMPLETED);
1440 msg.setId(dwRqId);
1441 msg.setField(VID_RCC, RCC_SUCCESS);
1442 sendMessage(&msg);
1443 }
1444
1445 /**
1446 * Process received file
1447 */
1448 void ClientSession::onFileUpload(BOOL bSuccess)
1449 {
1450 // Do processing specific to command initiated file upload
1451 switch(m_dwUploadCommand)
1452 {
1453 case CMD_INSTALL_PACKAGE:
1454 if (!bSuccess)
1455 {
1456 TCHAR szQuery[256];
1457
1458 _sntprintf(szQuery, 256, _T("DELETE FROM agent_pkg WHERE pkg_id=%d"), m_dwUploadData);
1459 DBQuery(g_hCoreDB, szQuery);
1460 }
1461 break;
1462 case CMD_MODIFY_IMAGE:
1463 EnumerateClientSessions(ImageLibraryUpdateCallback, (void *)&m_uploadImageGuid);
1464 break;
1465 default:
1466 break;
1467 }
1468
1469 // Remove received file in case of failure
1470 if (!bSuccess)
1471 _tunlink(m_szCurrFileName);
1472 }
1473
1474 /**
1475 * Send message to client
1476 */
1477 void ClientSession::sendMessage(NXCPMessage *msg)
1478 {
1479 if (isTerminated())
1480 return;
1481
1482 if (msg->getCode() != CMD_ADM_MESSAGE)
1483 {
1484 TCHAR buffer[128];
1485 debugPrintf(6, _T("Sending message %s"), NXCPMessageCodeName(msg->getCode(), buffer));
1486 }
1487
1488 NXCP_MESSAGE *rawMsg = msg->createMessage();
1489 if ((g_debugLevel >= 8) && (msg->getCode() != CMD_ADM_MESSAGE))
1490 {
1491 String msgDump = NXCPMessage::dump(rawMsg, NXCP_VERSION);
1492 debugPrintf(8, _T("Message dump:\n%s"), (const TCHAR *)msgDump);
1493 }
1494
1495 bool result;
1496 if (m_pCtx != NULL)
1497 {
1498 NXCP_ENCRYPTED_MESSAGE *enMsg = m_pCtx->encryptMessage(rawMsg);
1499 if (enMsg != NULL)
1500 {
1501 result = (SendEx(m_hSocket, (char *)enMsg, ntohl(enMsg->size), 0, m_mutexSocketWrite) == (int)ntohl(enMsg->size));
1502 free(enMsg);
1503 }
1504 else
1505 {
1506 result = false;
1507 }
1508 }
1509 else
1510 {
1511 result = (SendEx(m_hSocket, (const char *)rawMsg, ntohl(rawMsg->size), 0, m_mutexSocketWrite) == (int)ntohl(rawMsg->size));
1512 }
1513 free(rawMsg);
1514
1515 if (!result)
1516 {
1517 closesocket(m_hSocket);
1518 m_hSocket = -1;
1519 }
1520 }
1521
1522 /**
1523 * Send raw message to client
1524 */
1525 void ClientSession::sendRawMessage(NXCP_MESSAGE *msg)
1526 {
1527 if (isTerminated())
1528 return;
1529
1530 UINT16 code = htons(msg->code);
1531 if (code != CMD_ADM_MESSAGE)
1532 {
1533 TCHAR buffer[128];
1534 debugPrintf(6, _T("Sending message %s"), NXCPMessageCodeName(ntohs(msg->code), buffer));
1535 }
1536
1537 if ((g_debugLevel >= 8) && (code != CMD_ADM_MESSAGE))
1538 {
1539 String msgDump = NXCPMessage::dump(msg, NXCP_VERSION);
1540 debugPrintf(8, _T("Message dump:\n%s"), (const TCHAR *)msgDump);
1541 }
1542
1543 bool result;
1544 if (m_pCtx != NULL)
1545 {
1546 NXCP_ENCRYPTED_MESSAGE *enMsg = m_pCtx->encryptMessage(msg);
1547 if (enMsg != NULL)
1548 {
1549 result = (SendEx(m_hSocket, (char *)enMsg, ntohl(enMsg->size), 0, m_mutexSocketWrite) == (int)ntohl(enMsg->size));
1550 free(enMsg);
1551 }
1552 else
1553 {
1554 result = false;
1555 }
1556 }
1557 else
1558 {
1559 result = (SendEx(m_hSocket, (const char *)msg, ntohl(msg->size), 0, m_mutexSocketWrite) == (int)ntohl(msg->size));
1560 }
1561
1562 if (!result)
1563 {
1564 closesocket(m_hSocket);
1565 m_hSocket = -1;
1566 }
1567 }
1568
1569 /**
1570 * Send file to client
1571 */
1572 BOOL ClientSession::sendFile(const TCHAR *file, UINT32 dwRqId, long ofset)
1573 {
1574 return !isTerminated() ? SendFileOverNXCP(m_hSocket, dwRqId, file, m_pCtx, ofset, NULL, NULL, m_mutexSocketWrite) : FALSE;
1575 }
1576
1577 /**
1578 * Send server information to client
1579 */
1580 void ClientSession::sendServerInfo(UINT32 dwRqId)
1581 {
1582 static UINT32 protocolVersions[] = {
1583 CLIENT_PROTOCOL_VERSION_BASE,
1584 CLIENT_PROTOCOL_VERSION_ALARMS,
1585 CLIENT_PROTOCOL_VERSION_PUSH,
1586 CLIENT_PROTOCOL_VERSION_TRAP,
1587 CLIENT_PROTOCOL_VERSION_MOBILE,
1588 CLIENT_PROTOCOL_VERSION_FULL
1589 };
1590 NXCPMessage msg;
1591 TCHAR szBuffer[1024];
1592 String strURL;
1593
1594 // Prepare response message
1595 msg.setCode(CMD_REQUEST_COMPLETED);
1596 msg.setId(dwRqId);
1597
1598 // Generate challenge for certificate authentication
1599 #ifdef _WITH_ENCRYPTION
1600 RAND_bytes(m_challenge, CLIENT_CHALLENGE_SIZE);
1601 #else
1602 memset(m_challenge, 0, CLIENT_CHALLENGE_SIZE);
1603 #endif
1604
1605 // Fill message with server info
1606 msg.setField(VID_RCC, RCC_SUCCESS);
1607 msg.setField(VID_SERVER_VERSION, NETXMS_VERSION_STRING);
1608 msg.setField(VID_SERVER_ID, g_serverId);
1609 msg.setField(VID_SUPPORTED_ENCRYPTION, (UINT32)0);
1610 msg.setField(VID_PROTOCOL_VERSION, (UINT32)CLIENT_PROTOCOL_VERSION_BASE);
1611 msg.setFieldFromInt32Array(VID_PROTOCOL_VERSION_EX, sizeof(protocolVersions) / sizeof(UINT32), protocolVersions);
1612 msg.setField(VID_CHALLENGE, m_challenge, CLIENT_CHALLENGE_SIZE);
1613 msg.setField(VID_TIMESTAMP, (UINT32)time(NULL));
1614
1615 #if defined(_WIN32)
1616 TIME_ZONE_INFORMATION tz;
1617 WCHAR wst[4], wdt[8], *curr;
1618 int i;
1619
1620 DWORD tzType = GetTimeZoneInformation(&tz);
1621
1622 // Create 3 letter abbreviation for standard name
1623 for(i = 0, curr = tz.StandardName; (*curr != 0) && (i < 3); curr++)
1624 if (iswupper(*curr))
1625 wst[i++] = *curr;
1626 while(i < 3)
1627 wst[i++] = L'X';
1628 wst[i] = 0;
1629
1630 // Create abbreviation for DST name
1631 for(i = 0, curr = tz.DaylightName; (*curr != 0) && (i < 7); curr++)
1632 if (iswupper(*curr))
1633 wdt[i++] = *curr;
1634 while(i < 3)
1635 wdt[i++] = L'X';
1636 wdt[i] = 0;
1637
1638 LONG effectiveBias;
1639 switch(tzType)
1640 {
1641 case TIME_ZONE_ID_STANDARD:
1642 effectiveBias = tz.Bias + tz.StandardBias;
1643 break;
1644 case TIME_ZONE_ID_DAYLIGHT:
1645 effectiveBias = tz.Bias + tz.DaylightBias;
1646 break;
1647 case TIME_ZONE_ID_UNKNOWN:
1648 effectiveBias = tz.Bias;
1649 break;
1650 default: // error
1651 effectiveBias = 0;
1652 debugPrintf(4, _T("GetTimeZoneInformation() call failed"));
1653 break;
1654 }
1655
1656 #ifdef UNICODE
1657 swprintf(szBuffer, 1024, L"%s%c%02d%s", wst, (effectiveBias > 0) ? '-' : '+',
1658 abs(effectiveBias) / 60, (tz.DaylightBias != 0) ? wdt : L"");
1659 #else
1660 sprintf(szBuffer, "%S%c%02d%S", wst, (effectiveBias > 0) ? '-' : '+',
1661 abs(effectiveBias) / 60, (tz.DaylightBias != 0) ? wdt : L"");
1662 #endif
1663
1664 #elif HAVE_TM_GMTOFF /* not Windows but have tm_gmtoff */
1665
1666 time_t t = time(NULL);
1667 int gmtOffset;
1668 #if HAVE_LOCALTIME_R
1669 struct tm tmbuff;
1670 struct tm *loc = localtime_r(&t, &tmbuff);
1671 #else
1672 struct tm *loc = localtime(&t);
1673 #endif
1674 gmtOffset = loc->tm_gmtoff / 3600;
1675 #ifdef UNICODE
1676 swprintf(szBuffer, 1024, L"%hs%hc%02d%hs", tzname[0], (gmtOffset >= 0) ? '+' : '-',
1677 abs(gmtOffset), (tzname[1] != NULL) ? tzname[1] : "");
1678 #else
1679 sprintf(szBuffer, "%s%c%02d%s", tzname[0], (gmtOffset >= 0) ? '+' : '-',
1680 abs(gmtOffset), (tzname[1] != NULL) ? tzname[1] : "");
1681 #endif
1682
1683 #else /* not Windows and no tm_gmtoff */
1684
1685 time_t t = time(NULL);
1686 #if HAVE_GMTIME_R
1687 struct tm tmbuff;
1688 struct tm *gmt = gmtime_r(&t, &tmbuff);
1689 #else
1690 struct tm *gmt = gmtime(&t);
1691 #endif
1692 gmt->tm_isdst = -1;
1693 int gmtOffset = (int)((t - mktime(gmt)) / 3600);
1694 #ifdef UNICODE
1695 swprintf(szBuffer, 1024, L"%hs%hc%02d%hs", tzname[0], (gmtOffset >= 0) ? '+' : '-',
1696 abs(gmtOffset), (tzname[1] != NULL) ? tzname[1] : "");
1697 #else
1698 sprintf(szBuffer, "%s%c%02d%s", tzname[0], (gmtOffset >= 0) ? '+' : '-',
1699 abs(gmtOffset), (tzname[1] != NULL) ? tzname[1] : "");
1700 #endif
1701
1702 #endif
1703
1704 msg.setField(VID_TIMEZONE, szBuffer);
1705 debugPrintf(2, _T("Server time zone: %s"), szBuffer);
1706
1707 ConfigReadStr(_T("TileServerURL"), szBuffer, 1024, _T("http://tile.openstreetmap.org/"));
1708 msg.setField(VID_TILE_SERVER_URL, szBuffer);
1709
1710 ConfigReadStr(_T("DefaultConsoleDateFormat"), szBuffer, 1024, _T("dd.MM.yyyy"));
1711 msg.setField(VID_DATE_FORMAT, szBuffer);
1712
1713 ConfigReadStr(_T("DefaultConsoleTimeFormat"), szBuffer, 1024, _T("HH:mm:ss"));
1714 msg.setField(VID_TIME_FORMAT, szBuffer);
1715
1716 ConfigReadStr(_T("DefaultConsoleShortTimeFormat"), szBuffer, 1024, _T("HH:mm"));
1717 msg.setField(VID_SHORT_TIME_FORMAT, szBuffer);
1718
1719 // Send response
1720 sendMessage(&msg);
1721 }
1722
1723 /**
1724 * Authenticate client
1725 */
1726 void ClientSession::login(NXCPMessage *pRequest)
1727 {
1728 NXCPMessage msg;
1729 TCHAR szLogin[MAX_USER_NAME], szPassword[1024];
1730 int nAuthType;
1731 bool changePasswd = false, intruderLockout = false;
1732 UINT32 dwResult;
1733 #ifdef _WITH_ENCRYPTION
1734 X509 *pCert;
1735 #endif
1736
1737 // Prepare response message
1738 msg.setCode(CMD_LOGIN_RESP);
1739 msg.setId(pRequest->getId());
1740
1741 // Get client info string
1742 if (pRequest->isFieldExist(VID_CLIENT_INFO))
1743 {
1744 TCHAR szClientInfo[32], szOSInfo[32], szLibVersion[16];
1745
1746 pRequest->getFieldAsString(VID_CLIENT_INFO, szClientInfo, 32);
1747 pRequest->getFieldAsString(VID_OS_INFO, szOSInfo, 32);
1748 pRequest->getFieldAsString(VID_LIBNXCL_VERSION, szLibVersion, 16);
1749 _sntprintf(m_szClientInfo, 96, _T("%s (%s; libnxcl %s)"),
1750 szClientInfo, szOSInfo, szLibVersion);
1751 }
1752
1753 m_clientType = pRequest->getFieldAsUInt16(VID_CLIENT_TYPE);
1754 if ((m_clientType < 0) || (m_clientType > CLIENT_TYPE_APPLICATION))
1755 m_clientType = CLIENT_TYPE_DESKTOP;
1756
1757 if (m_clientType == CLIENT_TYPE_WEB)
1758 {
1759 _tcscpy(m_webServerAddress, m_workstation);
1760 if (pRequest->isFieldExist(VID_CLIENT_ADDRESS))
1761 {
1762 pRequest->getFieldAsString(VID_CLIENT_ADDRESS, m_workstation, 256);
1763 debugPrintf(5, _T("Real web client address is %s"), m_workstation);
1764 }
1765 }
1766
1767 if (pRequest->isFieldExist(VID_LANGUAGE))
1768 {
1769 pRequest->getFieldAsString(VID_LANGUAGE, m_language, 8);
1770 }
1771
1772 if (!(m_dwFlags & CSF_AUTHENTICATED))
1773 {
1774 pRequest->getFieldAsString(VID_LOGIN_NAME, szLogin, MAX_USER_NAME);
1775 nAuthType = (int)pRequest->getFieldAsUInt16(VID_AUTH_TYPE);
1776 debugPrintf(6, _T("authentication type %d"), nAuthType);
1777 switch(nAuthType)
1778 {
1779 case NETXMS_AUTH_TYPE_PASSWORD:
1780 #ifdef UNICODE
1781 pRequest->getFieldAsString(VID_PASSWORD, szPassword, 256);
1782 #else
1783 pRequest->getFieldAsUtf8String(VID_PASSWORD, szPassword, 1024);
1784 #endif
1785 dwResult = AuthenticateUser(szLogin, szPassword, 0, NULL, NULL, &m_dwUserId,
1786 &m_dwSystemAccess, &changePasswd, &intruderLockout, false);
1787 break;
1788 case NETXMS_AUTH_TYPE_CERTIFICATE:
1789 #ifdef _WITH_ENCRYPTION
1790 pCert = CertificateFromLoginMessage(pRequest);
1791 if (pCert != NULL)
1792 {
1793 BYTE signature[256];
1794 UINT32 dwSigLen;
1795
1796 dwSigLen = pRequest->getFieldAsBinary(VID_SIGNATURE, signature, 256);
1797 dwResult = AuthenticateUser(szLogin, (TCHAR *)signature, dwSigLen, pCert,
1798 m_challenge, &m_dwUserId, &m_dwSystemAccess,
1799 &changePasswd, &intruderLockout, false);
1800 X509_free(pCert);
1801 }
1802 else
1803 {
1804 dwResult = RCC_BAD_CERTIFICATE;
1805 }
1806 #else
1807 dwResult = RCC_NOT_IMPLEMENTED;
1808 #endif
1809 break;
1810 case NETXMS_AUTH_TYPE_SSO_TICKET:
1811 char ticket[1024];
1812 pRequest->getFieldAsMBString(VID_PASSWORD, ticket, 1024);
1813 if (CASAuthenticate(ticket, szLogin))
1814 {
1815 debugPrintf(5, _T("SSO ticket %hs is valid, login name %s"), ticket, szLogin);
1816 dwResult = AuthenticateUser(szLogin, NULL, 0, NULL, NULL, &m_dwUserId,
1817 &m_dwSystemAccess, &changePasswd, &intruderLockout, true);
1818 }
1819 else
1820 {
1821 debugPrintf(5, _T("SSO ticket %hs is invalid"), ticket);
1822 dwResult = RCC_ACCESS_DENIED;
1823 }
1824 break;
1825 default:
1826 dwResult = RCC_UNSUPPORTED_AUTH_TYPE;
1827 break;
1828 }
1829
1830 // Additional validation by loaded modules
1831 if (dwResult == RCC_SUCCESS)
1832 {
1833 for(UINT32 i = 0; i < g_dwNumModules; i++)
1834 {
1835 if (g_pModuleList[i].pfAdditionalLoginCheck != NULL)
1836 {
1837 dwResult = g_pModuleList[i].pfAdditionalLoginCheck(m_dwUserId, pRequest);
1838 if (dwResult != RCC_SUCCESS)
1839 {
1840 debugPrintf(4, _T("Login blocked by module %s (rcc=%d)"), g_pModuleList[i].szName, dwResult);
1841 break;
1842 }
1843 }
1844 }
1845 }
1846
1847 if (dwResult == RCC_SUCCESS)
1848 {
1849 m_dwFlags |= CSF_AUTHENTICATED;
1850 _sntprintf(m_szUserName, MAX_SESSION_NAME, _T("%s@%s"), szLogin, m_workstation);
1851 m_loginTime = time(NULL);
1852 msg.setField(VID_RCC, RCC_SUCCESS);
1853 msg.setField(VID_USER_SYS_RIGHTS, m_dwSystemAccess);
1854 msg.setField(VID_USER_ID, m_dwUserId);
1855 msg.setField(VID_SESSION_ID, (UINT32)m_id);
1856 msg.setField(VID_CHANGE_PASSWD_FLAG, (WORD)changePasswd);
1857 msg.setField(VID_DBCONN_STATUS, (UINT16)((g_flags & AF_DB_CONNECTION_LOST) ? 0 : 1));
1858 msg.setField(VID_ZONING_ENABLED, (UINT16)((g_flags & AF_ENABLE_ZONING) ? 1 : 0));
1859 msg.setField(VID_POLLING_INTERVAL, ConfigReadULong(_T("DefaultDCIPollingInterval"), 60));
1860 msg.setField(VID_RETENTION_TIME, ConfigReadULong(_T("DefaultDCIRetentionTime"), 30));
1861 msg.setField(VID_ALARM_STATUS_FLOW_STATE, (UINT16)ConfigReadInt(_T("StrictAlarmStatusFlow"), 0));
1862 msg.setField(VID_TIMED_ALARM_ACK_ENABLED, (UINT16)ConfigReadInt(_T("EnableTimedAlarmAck"), 0));
1863 msg.setField(VID_VIEW_REFRESH_INTERVAL, (UINT16)ConfigReadInt(_T("MinViewRefreshInterval"), 200));
1864 msg.setField(VID_HELPDESK_LINK_ACTIVE, (UINT16)((g_flags & AF_HELPDESK_LINK_ACTIVE) ? 1 : 0));
1865 msg.setField(VID_ALARM_LIST_DISP_LIMIT, ConfigReadULong(_T("AlarmListDisplayLimit"), 4096));
1866 debugPrintf(3, _T("User %s authenticated (language=%s clientInfo=\"%s\")"), m_szUserName, m_language, m_szClientInfo);
1867 WriteAuditLog(AUDIT_SECURITY, TRUE, m_dwUserId, m_workstation, m_id, 0,
1868 _T("User \"%s\" logged in (language: %s; client info: %s)"), szLogin, m_language, m_szClientInfo);
1869 }
1870 else
1871 {
1872 msg.setField(VID_RCC, dwResult);
1873 WriteAuditLog(AUDIT_SECURITY, FALSE, m_dwUserId, m_workstation, m_id, 0,
1874 _T("User \"%s\" login failed with error code %d (client info: %s)"),
1875 szLogin, dwResult, m_szClientInfo);
1876 if (intruderLockout)
1877 {
1878 WriteAuditLog(AUDIT_SECURITY, FALSE, m_dwUserId, m_workstation, m_id, 0,
1879 _T("User account \"%s\" temporary disabled due to excess count of failed authentication attempts"), szLogin);
1880 }
1881 }
1882 }
1883 else
1884 {
1885 msg.setField(VID_RCC, RCC_OUT_OF_STATE_REQUEST);
1886 }
1887
1888 // Send response
1889 sendMessage(&msg);
1890 }
1891
1892 /**
1893 * Send event configuration to client
1894 */
1895 void ClientSession::sendEventDB(UINT32 dwRqId)
1896 {
1897 DB_ASYNC_RESULT hResult;
1898 NXCPMessage msg;
1899 TCHAR szBuffer[4096];
1900
1901 // Prepare response message
1902 msg.setCode(CMD_REQUEST_COMPLETED);
1903 msg.setId(dwRqId);
1904
1905 if (checkSysAccessRights(SYSTEM_ACCESS_VIEW_EVENT_DB) || checkSysAccessRights(SYSTEM_ACCESS_EDIT_EVENT_DB) || checkSysAccessRights(SYSTEM_ACCESS_EPP))
1906 {
1907 if (!(g_flags & AF_DB_CONNECTION_LOST))
1908 {
1909 msg.setField(VID_RCC, RCC_SUCCESS);
1910 sendMessage(&msg);
1911 msg.deleteAllFields();
1912
1913 // Prepare data message
1914 msg.setCode(CMD_EVENT_DB_RECORD);
1915 msg.setId(dwRqId);
1916
1917 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
1918 hResult = DBAsyncSelect(hdb, _T("SELECT event_code,event_name,severity,flags,message,description FROM event_cfg"));
1919 if (hResult != NULL)
1920 {
1921 while(DBFetch(hResult))
1922 {
1923 msg.setField(VID_EVENT_CODE, DBGetFieldAsyncULong(hResult, 0));
1924 msg.setField(VID_NAME, DBGetFieldAsync(hResult, 1, szBuffer, 1024));
1925 msg.setField(VID_SEVERITY, DBGetFieldAsyncULong(hResult, 2));
1926 msg.setField(VID_FLAGS, DBGetFieldAsyncULong(hResult, 3));
1927
1928 DBGetFieldAsync(hResult, 4, szBuffer, 4096);
1929 msg.setField(VID_MESSAGE, szBuffer);
1930
1931 DBGetFieldAsync(hResult, 5, szBuffer, 4096);
1932 msg.setField(VID_DESCRIPTION, szBuffer);
1933
1934 sendMessage(&msg);
1935 msg.deleteAllFields();
1936 }
1937 DBFreeAsyncResult(hResult);
1938 }
1939 DBConnectionPoolReleaseConnection(hdb);
1940
1941 // End-of-list indicator
1942 msg.setField(VID_EVENT_CODE, (UINT32)0);
1943 msg.setEndOfSequence();
1944 }
1945 else
1946 {
1947 msg.setField(VID_RCC, RCC_DB_CONNECTION_LOST);
1948 }
1949 }
1950 else
1951 {
1952 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
1953 }
1954 sendMessage(&msg);
1955 }
1956
1957 /**
1958 * Callback for sending event configuration change notifications
1959 */
1960 static void SendEventDBChangeNotification(ClientSession *session, void *arg)
1961 {
1962 if (session->isAuthenticated() &&
1963 (session->checkSysAccessRights(SYSTEM_ACCESS_VIEW_EVENT_DB) ||
1964 session->checkSysAccessRights(SYSTEM_ACCESS_EDIT_EVENT_DB) ||
1965 session->checkSysAccessRights(SYSTEM_ACCESS_EPP)))
1966 session->postMessage((NXCPMessage *)arg);
1967 }
1968
1969 /**
1970 * Update event template
1971 */
1972 void ClientSession::modifyEventTemplate(NXCPMessage *pRequest)
1973 {
1974 NXCPMessage msg;
1975
1976 // Prepare reply message
1977 msg.setCode(CMD_REQUEST_COMPLETED);
1978 msg.setId(pRequest->getId());
1979
1980 // Check access rights
1981 if (checkSysAccessRights(SYSTEM_ACCESS_EDIT_EVENT_DB))
1982 {
1983 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
1984
1985 // Check if event with specific code exists
1986 UINT32 dwEventCode = pRequest->getFieldAsUInt32(VID_EVENT_CODE);
1987 bool bEventExist = IsDatabaseRecordExist(hdb, _T("event_cfg"), _T("event_code"), dwEventCode);
1988
1989 // Check that we are not trying to create event below 100000
1990 if (bEventExist || (dwEventCode >= FIRST_USER_EVENT_ID))
1991 {
1992 // Prepare and execute SQL query
1993 TCHAR name[MAX_EVENT_NAME];
1994 pRequest->getFieldAsString(VID_NAME, name, MAX_EVENT_NAME);
1995 if (IsValidObjectName(name, TRUE))
1996 {
1997 DB_STATEMENT hStmt;
1998 if (bEventExist)
1999 {
2000 hStmt = DBPrepare(hdb, _T("UPDATE event_cfg SET event_name=?,severity=?,flags=?,message=?,description=? WHERE event_code=?"));
2001 }
2002 else
2003 {
2004 hStmt = DBPrepare(hdb, _T("INSERT INTO event_cfg (event_name,severity,flags,message,description,event_code) VALUES (?,?,?,?,?,?)"));
2005 }
2006
2007 if (hStmt != NULL)
2008 {
2009 DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, name, DB_BIND_STATIC);
2010 DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, pRequest->getFieldAsInt32(VID_SEVERITY));
2011 DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, pRequest->getFieldAsInt32(VID_FLAGS));
2012 DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, pRequest->getFieldAsString(VID_MESSAGE), DB_BIND_DYNAMIC, MAX_EVENT_MSG_LENGTH - 1);
2013 DBBind(hStmt, 5, DB_SQLTYPE_TEXT, pRequest->getFieldAsString(VID_DESCRIPTION), DB_BIND_DYNAMIC);
2014 DBBind(hStmt, 6, DB_SQLTYPE_INTEGER, dwEventCode);
2015
2016 if (DBExecute(hStmt))
2017 {
2018 msg.setField(VID_RCC, RCC_SUCCESS);
2019 ReloadEvents();
2020
2021 NXCPMessage nmsg(pRequest);
2022 nmsg.setCode(CMD_EVENT_DB_UPDATE);
2023 nmsg.setField(VID_NOTIFICATION_CODE, (WORD)NX_NOTIFY_ETMPL_CHANGED);
2024 EnumerateClientSessions(SendEventDBChangeNotification, &nmsg);
2025 }
2026 else
2027 {
2028 msg.setField(VID_RCC, RCC_DB_FAILURE);
2029 }
2030 DBFreeStatement(hStmt);
2031 }
2032 else
2033 {
2034 msg.setField(VID_RCC, RCC_DB_FAILURE);
2035 }
2036 }
2037 else
2038 {
2039 msg.setField(VID_RCC, RCC_INVALID_OBJECT_NAME);
2040 }
2041 }
2042 else
2043 {
2044 msg.setField(VID_RCC, RCC_INVALID_EVENT_CODE);
2045 }
2046 DBConnectionPoolReleaseConnection(hdb);
2047 }
2048 else
2049 {
2050 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2051 }
2052
2053 // Send response
2054 sendMessage(&msg);
2055 }
2056
2057 /**
2058 * Delete event template
2059 */
2060 void ClientSession::deleteEventTemplate(NXCPMessage *pRequest)
2061 {
2062 NXCPMessage msg;
2063 UINT32 dwEventCode;
2064
2065 // Prepare reply message
2066 msg.setCode(CMD_REQUEST_COMPLETED);
2067 msg.setId(pRequest->getId());
2068
2069 dwEventCode = pRequest->getFieldAsUInt32(VID_EVENT_CODE);
2070
2071 // Check access rights
2072 if (checkSysAccessRights(SYSTEM_ACCESS_EDIT_EVENT_DB) && (dwEventCode >= FIRST_USER_EVENT_ID))
2073 {
2074 TCHAR szQuery[256];
2075
2076 _sntprintf(szQuery, 256, _T("DELETE FROM event_cfg WHERE event_code=%d"), dwEventCode);
2077 if (DBQuery(g_hCoreDB, szQuery))
2078 {
2079 DeleteEventTemplateFromList(dwEventCode);
2080
2081 NXCPMessage nmsg;
2082 nmsg.setCode(CMD_EVENT_DB_UPDATE);
2083 nmsg.setField(VID_NOTIFICATION_CODE, (WORD)NX_NOTIFY_ETMPL_DELETED);
2084 nmsg.setField(VID_EVENT_CODE, dwEventCode);
2085 EnumerateClientSessions(SendEventDBChangeNotification, &nmsg);
2086
2087 msg.setField(VID_RCC, RCC_SUCCESS);
2088
2089 WriteAuditLog(AUDIT_SYSCFG, TRUE, m_dwUserId, m_workstation, m_id, 0, _T("Event template %d deleted"), dwEventCode);
2090 }
2091 else
2092 {
2093 msg.setField(VID_RCC, RCC_DB_FAILURE);
2094 }
2095 }
2096 else
2097 {
2098 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2099 }
2100
2101 // Send response
2102 sendMessage(&msg);
2103 }
2104
2105 /**
2106 * Generate event code for new event template
2107 */
2108 void ClientSession::generateEventCode(UINT32 dwRqId)
2109 {
2110 NXCPMessage msg;
2111
2112 // Prepare reply message
2113 msg.setCode(CMD_REQUEST_COMPLETED);
2114 msg.setId(dwRqId);
2115
2116 // Check access rights
2117 if (checkSysAccessRights(SYSTEM_ACCESS_EDIT_EVENT_DB))
2118 {
2119 msg.setField(VID_EVENT_CODE, CreateUniqueId(IDG_EVENT));
2120 }
2121 else
2122 {
2123 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2124 }
2125
2126 // Send response
2127 sendMessage(&msg);
2128 }
2129
2130 /**
2131 * Send all objects to client
2132 */
2133 void ClientSession::sendAllObjects(NXCPMessage *pRequest)
2134 {
2135 NXCPMessage msg;
2136
2137 // Send confirmation message
2138 msg.setCode(CMD_REQUEST_COMPLETED);
2139 msg.setId(pRequest->getId());
2140 msg.setField(VID_RCC, RCC_SUCCESS);
2141 sendMessage(&msg);
2142 msg.deleteAllFields();
2143
2144 // Change "sync comments" flag
2145 if (pRequest->getFieldAsUInt16(VID_SYNC_COMMENTS))
2146 m_dwFlags |= CSF_SYNC_OBJECT_COMMENTS;
2147 else
2148 m_dwFlags &= ~CSF_SYNC_OBJECT_COMMENTS;
2149
2150 // Get client's last known time stamp
2151 UINT32 dwTimeStamp = pRequest->getFieldAsUInt32(VID_TIMESTAMP);
2152
2153 // Prepare message
2154 msg.setCode(CMD_OBJECT);
2155
2156 // Send objects, one per message
2157 ObjectArray<NetObj> *objects = g_idxObjectById.getObjects(true);
2158 MutexLock(m_mutexSendObjects);
2159 for(int i = 0; i < objects->size(); i++)
2160 {
2161 NetObj *object = objects->get(i);
2162 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ) &&
2163 (object->getTimeStamp() >= dwTimeStamp) &&
2164 !object->isHidden() && !object->isSystem())
2165 {
2166 object->fillMessage(&msg);
2167 if (m_dwFlags & CSF_SYNC_OBJECT_COMMENTS)
2168 object->commentsToMessage(&msg);
2169 if (!object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_MODIFY))
2170 {
2171 // mask passwords
2172 msg.setField(VID_SHARED_SECRET, _T("********"));
2173 msg.setField(VID_SNMP_AUTH_PASSWORD, _T("********"));
2174 msg.setField(VID_SNMP_PRIV_PASSWORD, _T("********"));
2175 }
2176 sendMessage(&msg);
2177 msg.deleteAllFields();
2178 }
2179 object->decRefCount();
2180 }
2181 delete objects;
2182
2183 // Send end of list notification
2184 msg.setCode(CMD_OBJECT_LIST_END);
2185 sendMessage(&msg);
2186
2187 MutexUnlock(m_mutexSendObjects);
2188 }
2189
2190 /**
2191 * Send selected objects to client
2192 */
2193 void ClientSession::sendSelectedObjects(NXCPMessage *pRequest)
2194 {
2195 NXCPMessage msg;
2196
2197 // Send confirmation message
2198 msg.setCode(CMD_REQUEST_COMPLETED);
2199 msg.setId(pRequest->getId());
2200 msg.setField(VID_RCC, RCC_SUCCESS);
2201 sendMessage(&msg);
2202 msg.deleteAllFields();
2203
2204 // Change "sync comments" flag
2205 if (pRequest->getFieldAsBoolean(VID_SYNC_COMMENTS))
2206 m_dwFlags |= CSF_SYNC_OBJECT_COMMENTS;
2207 else
2208 m_dwFlags &= ~CSF_SYNC_OBJECT_COMMENTS;
2209
2210 UINT32 dwTimeStamp = pRequest->getFieldAsUInt32(VID_TIMESTAMP);
2211 UINT32 numObjects = pRequest->getFieldAsUInt32(VID_NUM_OBJECTS);
2212 UINT32 *objects = (UINT32 *)malloc(sizeof(UINT32) * numObjects);
2213 pRequest->getFieldAsInt32Array(VID_OBJECT_LIST, numObjects, objects);
2214 UINT32 options = pRequest->getFieldAsUInt16(VID_FLAGS);
2215
2216 MutexLock(m_mutexSendObjects);
2217
2218 // Prepare message
2219 msg.setCode((options & OBJECT_SYNC_SEND_UPDATES) ? CMD_OBJECT_UPDATE : CMD_OBJECT);
2220
2221 // Send objects, one per message
2222 for(UINT32 i = 0; i < numObjects; i++)
2223 {
2224 NetObj *object = FindObjectById(objects[i]);
2225 if ((object != NULL) &&
2226 object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ) &&
2227 (object->getTimeStamp() >= dwTimeStamp) &&
2228 !object->isHidden() && !object->isSystem())
2229 {
2230 object->fillMessage(&msg);
2231 if (m_dwFlags & CSF_SYNC_OBJECT_COMMENTS)
2232 object->commentsToMessage(&msg);
2233 if (!object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_MODIFY))
2234 {
2235 // mask passwords
2236 msg.setField(VID_SHARED_SECRET, _T("********"));
2237 msg.setField(VID_SNMP_AUTH_PASSWORD, _T("********"));
2238 msg.setField(VID_SNMP_PRIV_PASSWORD, _T("********"));
2239 }
2240 sendMessage(&msg);
2241 msg.deleteAllFields();
2242 }
2243 }
2244
2245 MutexUnlock(m_mutexSendObjects);
2246 safe_free(objects);
2247
2248 if (options & OBJECT_SYNC_DUAL_CONFIRM)
2249 {
2250 msg.setCode(CMD_REQUEST_COMPLETED);
2251 msg.setField(VID_RCC, RCC_SUCCESS);
2252 sendMessage(&msg);
2253 }
2254 }
2255
2256 /**
2257 * Send event log records to client
2258 */
2259 void ClientSession::sendEventLog(NXCPMessage *pRequest)
2260 {
2261 NXCPMessage msg;
2262 DB_ASYNC_RESULT hResult = NULL;
2263 DB_RESULT hTempResult;
2264 UINT32 dwRqId, dwMaxRecords, dwNumRows, dwId;
2265 TCHAR szQuery[1024], szBuffer[1024];
2266 WORD wRecOrder;
2267
2268 dwRqId = pRequest->getId();
2269 dwMaxRecords = pRequest->getFieldAsUInt32(VID_MAX_RECORDS);
2270 wRecOrder = ((g_dbSyntax == DB_SYNTAX_MSSQL) || (g_dbSyntax == DB_SYNTAX_ORACLE)) ? RECORD_ORDER_REVERSED : RECORD_ORDER_NORMAL;
2271
2272 // Prepare confirmation message
2273 msg.setCode(CMD_REQUEST_COMPLETED);
2274 msg.setId(dwRqId);
2275
2276 MutexLock(m_mutexSendEvents);
2277 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
2278
2279 // Retrieve events from database
2280 switch(g_dbSyntax)
2281 {
2282 case DB_SYNTAX_MYSQL:
2283 case DB_SYNTAX_PGSQL:
2284 case DB_SYNTAX_SQLITE:
2285 dwNumRows = 0;
2286 hTempResult = DBSelect(hdb, _T("SELECT count(*) FROM event_log"));
2287 if (hTempResult != NULL)
2288 {
2289 if (DBGetNumRows(hTempResult) > 0)
2290 {
2291 dwNumRows = DBGetFieldULong(hTempResult, 0, 0);
2292 }
2293 DBFreeResult(hTempResult);
2294 }
2295 _sntprintf(szQuery, 1024,
2296 _T("SELECT event_id,event_code,event_timestamp,event_source,")
2297 _T("event_severity,event_message,user_tag FROM event_log ")
2298 _T("ORDER BY event_id LIMIT %u OFFSET %u"),
2299 dwMaxRecords, dwNumRows - min(dwNumRows, dwMaxRecords));
2300 break;
2301 case DB_SYNTAX_MSSQL:
2302 _sntprintf(szQuery, 1024,
2303 _T("SELECT TOP %u event_id,event_code,event_timestamp,event_source,")
2304 _T("event_severity,event_message,user_tag FROM event_log ")
2305 _T("ORDER BY event_id DESC"), dwMaxRecords);
2306 break;
2307 case DB_SYNTAX_ORACLE:
2308 _sntprintf(szQuery, 1024,
2309 _T("SELECT event_id,event_code,event_timestamp,event_source,")
2310 _T("event_severity,event_message,user_tag FROM event_log ")
2311 _T("WHERE ROWNUM <= %u ORDER BY event_id DESC"), dwMaxRecords);
2312 break;
2313 case DB_SYNTAX_DB2:
2314 _sntprintf(szQuery, 1024,
2315 _T("SELECT event_id,event_code,event_timestamp,event_source,")
2316 _T("event_severity,event_message,user_tag FROM event_log ")
2317 _T("ORDER BY event_id DESC FETCH FIRST %u ROWS ONLY"), dwMaxRecords);
2318 break;
2319 default:
2320 szQuery[0] = 0;
2321 break;
2322 }
2323 hResult = DBAsyncSelect(hdb, szQuery);
2324 if (hResult != NULL)
2325 {
2326 msg.setField(VID_RCC, RCC_SUCCESS);
2327 sendMessage(&msg);
2328 msg.deleteAllFields();
2329 msg.setCode(CMD_EVENTLOG_RECORDS);
2330
2331 for(dwId = VID_EVENTLOG_MSG_BASE, dwNumRows = 0; DBFetch(hResult); dwNumRows++)
2332 {
2333 if (dwNumRows == 10)
2334 {
2335 msg.setField(VID_NUM_RECORDS, dwNumRows);
2336 msg.setField(VID_RECORDS_ORDER, wRecOrder);
2337 sendMessage(&msg);
2338 msg.deleteAllFields();
2339 dwNumRows = 0;
2340 dwId = VID_EVENTLOG_MSG_BASE;
2341 }
2342 msg.setField(dwId++, DBGetFieldAsyncUInt64(hResult, 0));
2343 msg.setField(dwId++, DBGetFieldAsyncULong(hResult, 1));
2344 msg.setField(dwId++, DBGetFieldAsyncULong(hResult, 2));
2345 msg.setField(dwId++, DBGetFieldAsyncULong(hResult, 3));
2346 msg.setField(dwId++, (WORD)DBGetFieldAsyncLong(hResult, 4));
2347 DBGetFieldAsync(hResult, 5, szBuffer, 1024);
2348 msg.setField(dwId++, szBuffer);
2349 DBGetFieldAsync(hResult, 6, szBuffer, 1024);
2350 msg.setField(dwId++, szBuffer);
2351 msg.setField(dwId++, (UINT32)0); // Do not send parameters
2352 }
2353 DBFreeAsyncResult(hResult);
2354
2355 // Send remaining records with End-Of-Sequence notification
2356 msg.setField(VID_NUM_RECORDS, dwNumRows);
2357 msg.setField(VID_RECORDS_ORDER, wRecOrder);
2358 msg.setEndOfSequence();
2359 sendMessage(&msg);
2360 }
2361 else
2362 {
2363 msg.setField(VID_RCC, RCC_DB_FAILURE);
2364 sendMessage(&msg);
2365 }
2366
2367 DBConnectionPoolReleaseConnection(hdb);
2368 MutexUnlock(m_mutexSendEvents);
2369 }
2370
2371 /**
2372 * Send all configuration variables to client
2373 */
2374 void ClientSession::sendAllConfigVars(UINT32 dwRqId)
2375 {
2376 UINT32 i, dwId, dwNumRecords;
2377 NXCPMessage msg;
2378 DB_RESULT hResult;
2379 TCHAR szBuffer[MAX_DB_STRING];
2380
2381 // Prepare message
2382 msg.setCode(CMD_REQUEST_COMPLETED);
2383 msg.setId(dwRqId);
2384
2385 // Check user rights
2386 if ((m_dwUserId == 0) || (m_dwSystemAccess & SYSTEM_ACCESS_SERVER_CONFIG))
2387 {
2388 // Retrieve configuration variables from database
2389 hResult = DBSelect(g_hCoreDB, _T("SELECT var_name,var_value,need_server_restart FROM config WHERE is_visible=1"));
2390 if (hResult != NULL)
2391 {
2392 // Send events, one per message
2393 dwNumRecords = DBGetNumRows(hResult);
2394 msg.setField(VID_NUM_VARIABLES, dwNumRecords);
2395 for(i = 0, dwId = VID_VARLIST_BASE; i < dwNumRecords; i++)
2396 {
2397 msg.setField(dwId++, DBGetField(hResult, i, 0, szBuffer, MAX_DB_STRING));
2398 DBGetField(hResult, i, 1, szBuffer, MAX_DB_STRING);
2399 DecodeSQLString(szBuffer);
2400 msg.setField(dwId++, szBuffer);
2401 msg.setField(dwId++, (WORD)DBGetFieldLong(hResult, i, 2));
2402 }
2403 DBFreeResult(hResult);
2404 }
2405 msg.setField(VID_RCC, RCC_SUCCESS);
2406 }
2407 else
2408 {
2409 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2410 }
2411
2412 // Send response
2413 sendMessage(&msg);
2414 }
2415
2416 /**
2417 * Set configuration variable's value
2418 */
2419 void ClientSession::setConfigVariable(NXCPMessage *pRequest)
2420 {
2421 NXCPMessage msg;
2422 TCHAR szName[MAX_OBJECT_NAME], szValue[MAX_DB_STRING];
2423
2424 // Prepare response message
2425 msg.setCode(CMD_REQUEST_COMPLETED);
2426 msg.setId(pRequest->getId());
2427
2428 // Check user rights
2429 if ((m_dwUserId == 0) || (m_dwSystemAccess & SYSTEM_ACCESS_SERVER_CONFIG))
2430 {
2431 pRequest->getFieldAsString(VID_NAME, szName, MAX_OBJECT_NAME);
2432 pRequest->getFieldAsString(VID_VALUE, szValue, MAX_DB_STRING);
2433 if (ConfigWriteStr(szName, szValue, TRUE))
2434 {
2435 msg.setField(VID_RCC, RCC_SUCCESS);
2436 WriteAuditLog(AUDIT_SYSCFG, TRUE, m_dwUserId, m_workstation, m_id, 0,
2437 _T("Server configuration variable \"%s\" set to \"%s\""), szName, szValue);
2438 }
2439 else
2440 {
2441 msg.setField(VID_RCC, RCC_DB_FAILURE);
2442 }
2443 }
2444 else
2445 {
2446 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2447 }
2448
2449 // Send response
2450 sendMessage(&msg);
2451 }
2452
2453 /**
2454 * Delete configuration variable
2455 */
2456 void ClientSession::deleteConfigVariable(NXCPMessage *pRequest)
2457 {
2458 NXCPMessage msg;
2459
2460 // Prepare response message
2461 msg.setCode(CMD_REQUEST_COMPLETED);
2462 msg.setId(pRequest->getId());
2463
2464 // Check user rights
2465 if ((m_dwUserId == 0) || (m_dwSystemAccess & SYSTEM_ACCESS_SERVER_CONFIG))
2466 {
2467 TCHAR name[MAX_OBJECT_NAME];
2468 pRequest->getFieldAsString(VID_NAME, name, MAX_OBJECT_NAME);
2469 if (ConfigDelete(name))
2470 {
2471 msg.setField(VID_RCC, RCC_SUCCESS);
2472 WriteAuditLog(AUDIT_SYSCFG, TRUE, m_dwUserId, m_workstation, m_id, 0, _T("Server configuration variable \"%s\" deleted"), name);
2473 }
2474 else
2475 {
2476 msg.setField(VID_RCC, RCC_DB_FAILURE);
2477 }
2478 }
2479 else
2480 {
2481 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2482 }
2483
2484 // Send response
2485 sendMessage(&msg);
2486 }
2487
2488 /**
2489 * Set configuration clob
2490 */
2491 void ClientSession::setConfigCLOB(NXCPMessage *pRequest)
2492 {
2493 NXCPMessage msg;
2494 TCHAR name[MAX_OBJECT_NAME], *value;
2495
2496 msg.setId(pRequest->getId());
2497 msg.setCode(CMD_REQUEST_COMPLETED);
2498
2499 if (m_dwSystemAccess & SYSTEM_ACCESS_SERVER_CONFIG)
2500 {
2501 pRequest->getFieldAsString(VID_NAME, name, MAX_OBJECT_NAME);
2502 value = pRequest->getFieldAsString(VID_VALUE);
2503 if (value != NULL)
2504 {
2505 if (ConfigWriteCLOB(name, value, TRUE))
2506 {
2507 msg.setField(VID_RCC, RCC_SUCCESS);
2508 WriteAuditLog(AUDIT_SYSCFG, TRUE, m_dwUserId, m_workstation, m_id, 0,
2509 _T("Server configuration variable \"%s\" set to \"%s\""), name, value);
2510 free(value);
2511 }
2512 else
2513 {
2514 msg.setField(VID_RCC, RCC_DB_FAILURE);
2515 }
2516 }
2517 else
2518 {
2519 msg.setField(VID_RCC, RCC_INVALID_REQUEST);
2520 }
2521 }
2522 else
2523 {
2524 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2525 }
2526
2527 sendMessage(&msg);
2528 }
2529
2530 /**
2531 * Get value of configuration clob
2532 */
2533 void ClientSession::getConfigCLOB(NXCPMessage *pRequest)
2534 {
2535 NXCPMessage msg;
2536 TCHAR name[MAX_OBJECT_NAME], *value;
2537
2538 msg.setId(pRequest->getId());
2539 msg.setCode(CMD_REQUEST_COMPLETED);
2540
2541 if (m_dwSystemAccess & SYSTEM_ACCESS_SERVER_CONFIG)
2542 {
2543 pRequest->getFieldAsString(VID_NAME, name, MAX_OBJECT_NAME);
2544 value = ConfigReadCLOB(name, NULL);
2545 if (value != NULL)
2546 {
2547 msg.setField(VID_VALUE, value);
2548 msg.setField(VID_RCC, RCC_SUCCESS);
2549 free(value);
2550 }
2551 else
2552 {
2553 msg.setField(VID_RCC, RCC_UNKNOWN_VARIABLE);
2554 }
2555 }
2556 else
2557 {
2558 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2559 }
2560
2561 sendMessage(&msg);
2562 }
2563
2564 /**
2565 * Close session
2566 */
2567 void ClientSession::kill()
2568 {
2569 notify(NX_NOTIFY_SESSION_KILLED);
2570
2571 // We shutdown socket connection, which will cause
2572 // read thread to stop, and other threads will follow
2573 shutdown(m_hSocket, 2);
2574 }
2575
2576 /**
2577 * Handler for new events
2578 */
2579 void ClientSession::onNewEvent(Event *pEvent)
2580 {
2581 UPDATE_INFO *pUpdate;
2582 NXCPMessage *msg;
2583 if (isAuthenticated() && (m_dwActiveChannels & NXC_CHANNEL_EVENTS) && (m_dwSystemAccess & SYSTEM_ACCESS_VIEW_EVENT_LOG))
2584 {
2585 NetObj *object = FindObjectById(pEvent->getSourceId());
2586 //If can't find object - just send to all events, if object found send to thous who have rights
2587 if (object == NULL || object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ))
2588 {
2589 pUpdate = (UPDATE_INFO *)malloc(sizeof(UPDATE_INFO));
2590 pUpdate->dwCategory = INFO_CAT_EVENT;
2591 msg = new NXCPMessage;
2592 msg->setCode(CMD_EVENTLOG_RECORDS);
2593 pEvent->prepareMessage(msg);
2594 pUpdate->pData = msg;
2595 m_pUpdateQueue->put(pUpdate);
2596 }
2597 }
2598 }
2599
2600 /**
2601 * Handler for object changes
2602 */
2603 void ClientSession::onObjectChange(NetObj *object)
2604 {
2605 UPDATE_INFO *pUpdate;
2606
2607 if (isAuthenticated() && (m_dwActiveChannels & NXC_CHANNEL_OBJECTS))
2608 if (object->isDeleted() || object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ))
2609 {
2610 pUpdate = (UPDATE_INFO *)malloc(sizeof(UPDATE_INFO));
2611 pUpdate->dwCategory = INFO_CAT_OBJECT_CHANGE;
2612 pUpdate->pData = object;
2613 object->incRefCount();
2614 m_pUpdateQueue->put(pUpdate);
2615 }
2616 }
2617
2618 /**
2619 * Send notification message to server
2620 */
2621 void ClientSession::notify(UINT32 dwCode, UINT32 dwData)
2622 {
2623 NXCPMessage msg;
2624
2625 msg.setCode(CMD_NOTIFY);
2626 msg.setField(VID_NOTIFICATION_CODE, dwCode);
2627 msg.setField(VID_NOTIFICATION_DATA, dwData);
2628 sendMessage(&msg);
2629 }
2630
2631 static void SetNodesConflictString(NXCPMessage *msg, UINT32 zoneId, InetAddress ipAddr)
2632 {
2633 if(ipAddr.isValid())
2634 {
2635 TCHAR value[512];
2636
2637 Node *sameNode = FindNodeByIP(zoneId, ipAddr);
2638 Subnet *sameSubnet = FindSubnetByIP(zoneId, ipAddr);
2639 if(sameNode != NULL)
2640 {
2641 _sntprintf(value, 512, _T("%s"), sameNode->getName());
2642 }
2643 else if (sameSubnet != NULL)
2644 {
2645 _sntprintf(value, 512, _T("%s"), sameSubnet->getName());
2646 }
2647 else
2648 {
2649 _tcscpy(value, _T(""));
2650 }
2651 msg->setField(VID_VALUE, value);
2652 }
2653 }
2654
2655 /**
2656 * Modify object
2657 */
2658 void ClientSession::modifyObject(NXCPMessage *pRequest)
2659 {
2660 UINT32 dwObjectId, dwResult = RCC_SUCCESS;
2661 NetObj *object;
2662 NXCPMessage msg;
2663
2664 // Prepare reply message
2665 msg.setCode(CMD_REQUEST_COMPLETED);
2666 msg.setId(pRequest->getId());
2667
2668 dwObjectId = pRequest->getFieldAsUInt32(VID_OBJECT_ID);
2669 object = FindObjectById(dwObjectId);
2670 if (object != NULL)
2671 {
2672 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_MODIFY))
2673 {
2674 // If user attempts to change object's ACL, check
2675 // if he has OBJECT_ACCESS_ACL permission
2676 if (pRequest->isFieldExist(VID_ACL_SIZE))
2677 if (!object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_ACL))
2678 dwResult = RCC_ACCESS_DENIED;
2679
2680 // If user attempts to rename object, check object's name
2681 if (pRequest->isFieldExist(VID_OBJECT_NAME))
2682 {
2683 TCHAR name[256];
2684 pRequest->getFieldAsString(VID_OBJECT_NAME, name, 256);
2685 if (!IsValidObjectName(name, TRUE))
2686 dwResult = RCC_INVALID_OBJECT_NAME;
2687 }
2688
2689 // If allowed, change object and set completion code
2690 if (dwResult == RCC_SUCCESS)
2691 {
2692 dwResult = object->modifyFromMessage(pRequest);
2693 if (dwResult == RCC_SUCCESS)
2694 {
2695 object->postModify();
2696 }
2697 else if (dwResult == RCC_ALREADY_EXIST)
2698 {
2699 //Add information about conflicting nodes
2700 InetAddress ipAddr;
2701
2702 if (pRequest->isFieldExist(VID_IP_ADDRESS))
2703 {
2704 ipAddr = pRequest->getFieldAsInetAddress(VID_IP_ADDRESS);
2705 } else if (pRequest->isFieldExist(VID_PRIMARY_NAME))
2706 {
2707 TCHAR primaryName[MAX_DNS_NAME];
2708 pRequest->getFieldAsString(VID_PRIMARY_NAME, primaryName, MAX_DNS_NAME);
2709 ipAddr = InetAddress::resolveHostName(primaryName);
2710 }
2711 SetNodesConflictString(&msg, ((Node*)object)->getZoneId(), ipAddr);
2712 }
2713 }
2714 msg.setField(VID_RCC, dwResult);
2715
2716 if (dwResult == RCC_SUCCESS)
2717 {
2718 WriteAuditLog(AUDIT_OBJECTS, TRUE, m_dwUserId, m_workstation, m_id, dwObjectId,
2719 _T("Object %s modified from client"), object->getName());
2720 }
2721 else
2722 {
2723 WriteAuditLog(AUDIT_OBJECTS, FALSE, m_dwUserId, m_workstation, m_id, dwObjectId,
2724 _T("Failed to modify object from client - error %d"), dwResult);
2725 }
2726 }
2727 else
2728 {
2729 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2730 WriteAuditLog(AUDIT_OBJECTS, FALSE, m_dwUserId, m_workstation, m_id, dwObjectId,
2731 _T("Failed to modify object from client - access denied"), dwResult);
2732 }
2733 }
2734 else
2735 {
2736 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
2737 }
2738
2739 // Send response
2740 sendMessage(&msg);
2741 }
2742
2743 /**
2744 * Send users database to client
2745 */
2746 void ClientSession::sendUserDB(UINT32 dwRqId)
2747 {
2748 NXCPMessage msg;
2749 UserDatabaseObject **users;
2750 int i, userCount;
2751
2752 // Prepare response message
2753 msg.setCode(CMD_REQUEST_COMPLETED);
2754 msg.setId(dwRqId);
2755 msg.setField(VID_RCC, RCC_SUCCESS);
2756 sendMessage(&msg);
2757 msg.deleteAllFields();
2758
2759 // Send user database
2760 users = OpenUserDatabase(&userCount);
2761 for(i = 0; i < userCount; i++)
2762 {
2763 msg.setCode((users[i]->getId() & GROUP_FLAG) ? CMD_GROUP_DATA : CMD_USER_DATA);
2764 users[i]->fillMessage(&msg);
2765 sendMessage(&msg);
2766 msg.deleteAllFields();
2767 }
2768 CloseUserDatabase();
2769
2770 // Send end-of-database notification
2771 msg.setCode(CMD_USER_DB_EOF);
2772 sendMessage(&msg);
2773 }
2774
2775 /**
2776 * Create new user
2777 */
2778 void ClientSession::createUser(NXCPMessage *pRequest)
2779 {
2780 NXCPMessage msg;
2781
2782 // Prepare response message
2783 msg.setCode(CMD_REQUEST_COMPLETED);
2784 msg.setId(pRequest->getId());
2785
2786 // Check user rights
2787 if (!(m_dwSystemAccess & SYSTEM_ACCESS_MANAGE_USERS))
2788 {
2789 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2790 }
2791 else if (!(m_dwFlags & CSF_USER_DB_LOCKED))
2792 {
2793 // User database have to be locked before any
2794 // changes to user database can be made
2795 msg.setField(VID_RCC, RCC_OUT_OF_STATE_REQUEST);
2796 }
2797 else
2798 {
2799 UINT32 dwResult, dwUserId;
2800 BOOL bIsGroup;
2801 TCHAR szUserName[MAX_USER_NAME];
2802
2803 pRequest->getFieldAsString(VID_USER_NAME, szUserName, MAX_USER_NAME);
2804 if (IsValidObjectName(szUserName))
2805 {
2806 bIsGroup = pRequest->getFieldAsUInt16(VID_IS_GROUP);
2807 dwResult = CreateNewUser(szUserName, bIsGroup, &dwUserId);
2808 msg.setField(VID_RCC, dwResult);
2809 if (dwResult == RCC_SUCCESS)
2810 {
2811 msg.setField(VID_USER_ID, dwUserId); // Send id of new user to client
2812 WriteAuditLog(AUDIT_SECURITY, TRUE, m_dwUserId, m_workstation, m_id, dwUserId, _T("%s %s created"), bIsGroup ? _T("Group") : _T("User"), szUserName);
2813 }
2814 }
2815 else
2816 {
2817 msg.setField(VID_RCC, RCC_INVALID_OBJECT_NAME);
2818 }
2819 }
2820
2821 // Send response
2822 sendMessage(&msg);
2823 }
2824
2825 /**
2826 * Update existing user's data
2827 */
2828 void ClientSession::updateUser(NXCPMessage *pRequest)
2829 {
2830 NXCPMessage msg;
2831
2832 // Prepare response message
2833 msg.setCode(CMD_REQUEST_COMPLETED);
2834 msg.setId(pRequest->getId());
2835
2836 // Check user rights
2837 if (!(m_dwSystemAccess & SYSTEM_ACCESS_MANAGE_USERS))
2838 {
2839 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2840 }
2841 else if (!(m_dwFlags & CSF_USER_DB_LOCKED))
2842 {
2843 // User database have to be locked before any
2844 // changes to user database can be made
2845 msg.setField(VID_RCC, RCC_OUT_OF_STATE_REQUEST);
2846 }
2847 else
2848 {
2849 UINT32 result = ModifyUserDatabaseObject(pRequest);
2850 if (result == RCC_SUCCESS)
2851 {
2852 TCHAR name[MAX_DB_STRING];
2853 UINT32 id = pRequest->getFieldAsUInt32(VID_USER_ID);
2854 ResolveUserId(id, name, MAX_DB_STRING);
2855 WriteAuditLog(AUDIT_SECURITY, TRUE, m_dwUserId, m_workstation, m_id, id,
2856 _T("%s %s modified"), (id & GROUP_FLAG) ? _T("Group") : _T("User"), name);
2857 }
2858 msg.setField(VID_RCC, result);
2859 }
2860
2861 // Send response
2862 sendMessage(&msg);
2863 }
2864
2865 /**
2866 * Delete user
2867 */
2868 void ClientSession::deleteUser(NXCPMessage *pRequest)
2869 {
2870 NXCPMessage msg;
2871 UINT32 dwUserId;
2872
2873 // Prepare response message
2874 msg.setCode(CMD_REQUEST_COMPLETED);
2875 msg.setId(pRequest->getId());
2876
2877 // Check user rights
2878 if (!(m_dwSystemAccess & SYSTEM_ACCESS_MANAGE_USERS))
2879 {
2880 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2881 }
2882 else if (!(m_dwFlags & CSF_USER_DB_LOCKED))
2883 {
2884 // User database have to be locked before any
2885 // changes to user database can be made
2886 msg.setField(VID_RCC, RCC_OUT_OF_STATE_REQUEST);
2887 }
2888 else
2889 {
2890 // Get Id of user to be deleted
2891 dwUserId = pRequest->getFieldAsUInt32(VID_USER_ID);
2892
2893 if ((dwUserId != 0) && (dwUserId != GROUP_EVERYONE))
2894 {
2895 if (!IsLoggedIn(dwUserId))
2896 {
2897 TCHAR name[MAX_DB_STRING];
2898 ResolveUserId(dwUserId, name, MAX_DB_STRING);
2899 UINT32 rcc = DeleteUserDatabaseObject(dwUserId);
2900 msg.setField(VID_RCC, rcc);
2901 if(rcc == RCC_SUCCESS)
2902 {
2903 WriteAuditLog(AUDIT_SECURITY, TRUE, m_dwUserId, m_workstation, m_id, dwUserId,
2904 _T("%s %s [%d] deleted"), (dwUserId & GROUP_FLAG) ? _T("Group") : _T("User"), name, dwUserId);
2905 }
2906 }
2907 else
2908 {
2909 // logger in users cannot be deleted
2910 msg.setField(VID_RCC, RCC_USER_LOGGED_IN);
2911 }
2912 }
2913 else
2914 {
2915 // System administrator account and everyone group cannot be deleted
2916 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2917 }
2918 }
2919
2920 // Send response
2921 sendMessage(&msg);
2922 }
2923
2924 /**
2925 * Lock/unlock user database
2926 */
2927 void ClientSession::lockUserDB(UINT32 dwRqId, BOOL bLock)
2928 {
2929 NXCPMessage msg;
2930 TCHAR szBuffer[256];
2931
2932 // Prepare response message
2933 msg.setCode(CMD_REQUEST_COMPLETED);
2934 msg.setId(dwRqId);
2935
2936 if (m_dwSystemAccess & SYSTEM_ACCESS_MANAGE_USERS)
2937 {
2938 if (bLock)
2939 {
2940 if (!LockComponent(CID_USER_DB, m_id, m_szUserName, NULL, szBuffer))
2941 {
2942 msg.setField(VID_RCC, RCC_COMPONENT_LOCKED);
2943 msg.setField(VID_LOCKED_BY, szBuffer);
2944 }
2945 else
2946 {
2947 m_dwFlags |= CSF_USER_DB_LOCKED;
2948 msg.setField(VID_RCC, RCC_SUCCESS);
2949 }
2950 }
2951 else
2952 {
2953 if (m_dwFlags & CSF_USER_DB_LOCKED)
2954 {
2955 UnlockComponent(CID_USER_DB);
2956 m_dwFlags &= ~CSF_USER_DB_LOCKED;
2957 }
2958 msg.setField(VID_RCC, RCC_SUCCESS);
2959 }
2960 }
2961 else
2962 {
2963 // Current user has no rights for user account management
2964 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
2965 }
2966
2967 // Send response
2968 sendMessage(&msg);
2969 }
2970
2971 /**
2972 * Notify client on user database update
2973 */
2974 void ClientSession::onUserDBUpdate(int code, UINT32 id, UserDatabaseObject *object)
2975 {
2976 NXCPMessage msg;
2977
2978 if (isAuthenticated())
2979 {
2980 msg.setCode(CMD_USER_DB_UPDATE);
2981 msg.setId(0);
2982 msg.setField(VID_UPDATE_TYPE, (WORD)code);
2983
2984 switch(code)
2985 {
2986 case USER_DB_CREATE:
2987 case USER_DB_MODIFY:
2988 object->fillMessage(&msg);
2989 break;
2990 default:
2991 msg.setField(VID_USER_ID, id);
2992 break;
2993 }
2994
2995 sendMessage(&msg);
2996 }
2997 }
2998
2999 /**
3000 * Change management status for the object
3001 */
3002 void ClientSession::changeObjectMgmtStatus(NXCPMessage *pRequest)
3003 {
3004 NXCPMessage msg;
3005 UINT32 dwObjectId;
3006 NetObj *object;
3007
3008 // Prepare response message
3009 msg.setCode(CMD_REQUEST_COMPLETED);
3010 msg.setId(pRequest->getId());
3011
3012 // Get object id and check access rights
3013 dwObjectId = pRequest->getFieldAsUInt32(VID_OBJECT_ID);
3014 object = FindObjectById(dwObjectId);
3015 if (object != NULL)
3016 {
3017 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_MODIFY))
3018 {
3019 if ((object->getObjectClass() != OBJECT_TEMPLATE) &&
3020 (object->getObjectClass() != OBJECT_TEMPLATEGROUP) &&
3021 (object->getObjectClass() != OBJECT_TEMPLATEROOT))
3022 {
3023 BOOL bIsManaged = (BOOL)pRequest->getFieldAsUInt16(VID_MGMT_STATUS);
3024 object->setMgmtStatus(bIsManaged);
3025 msg.setField(VID_RCC, RCC_SUCCESS);
3026 WriteAuditLog(AUDIT_OBJECTS, TRUE, m_dwUserId, m_workstation, m_id, object->getId(),
3027 _T("Object %s set to %s state"), object->getName(), bIsManaged ? _T("managed") : _T("unmanaged"));
3028 }
3029 else
3030 {
3031 msg.setField(VID_RCC, RCC_INCOMPATIBLE_OPERATION);
3032 }
3033 }
3034 else
3035 {
3036 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3037 }
3038 }
3039 else
3040 {
3041 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3042 }
3043
3044 // Send response
3045 sendMessage(&msg);
3046 }
3047
3048 /**
3049 * Set user's password
3050 */
3051 void ClientSession::setPassword(NXCPMessage *pRequest)
3052 {
3053 NXCPMessage msg;
3054 UINT32 dwUserId;
3055
3056 // Prepare response message
3057 msg.setCode(CMD_REQUEST_COMPLETED);
3058 msg.setId(pRequest->getId());
3059
3060 dwUserId = pRequest->getFieldAsUInt32(VID_USER_ID);
3061
3062 if (((m_dwSystemAccess & SYSTEM_ACCESS_MANAGE_USERS) &&
3063 !((dwUserId == 0) && (m_dwUserId != 0))) || // Only administrator can change password for UID 0
3064 (dwUserId == m_dwUserId)) // User can change password for itself
3065 {
3066 UINT32 dwResult;
3067 TCHAR newPassword[1024], oldPassword[1024];
3068
3069 #ifdef UNICODE
3070 pRequest->getFieldAsString(VID_PASSWORD, newPassword, 256);
3071 if (pRequest->isFieldExist(VID_OLD_PASSWORD))
3072 pRequest->getFieldAsString(VID_OLD_PASSWORD, oldPassword, 256);
3073 #else
3074 pRequest->getFieldAsUtf8String(VID_PASSWORD, newPassword, 1024);
3075 if (pRequest->isFieldExist(VID_OLD_PASSWORD))
3076 pRequest->getFieldAsUtf8String(VID_OLD_PASSWORD, oldPassword, 1024);
3077 #endif
3078 else
3079 oldPassword[0] = 0;
3080 dwResult = SetUserPassword(dwUserId, newPassword, oldPassword, dwUserId == m_dwUserId);
3081 msg.setField(VID_RCC, dwResult);
3082
3083 if (dwResult == RCC_SUCCESS)
3084 {
3085 TCHAR userName[MAX_DB_STRING];
3086 ResolveUserId(dwUserId, userName, MAX_DB_STRING);
3087 WriteAuditLog(AUDIT_SECURITY, TRUE, m_dwUserId, m_workstation, m_id, 0, _T("Changed password for user %s"), userName);
3088 }
3089 }
3090 else
3091 {
3092 // Current user has no rights to change password for specific user
3093 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3094 }
3095
3096 // Send response
3097 sendMessage(&msg);
3098 }
3099
3100 /**
3101 * Send node's DCIs to client and lock data collection settings
3102 */
3103 void ClientSession::openNodeDCIList(NXCPMessage *pRequest)
3104 {
3105 NXCPMessage msg;
3106 UINT32 dwObjectId;
3107 NetObj *object;
3108 BOOL bSuccess = FALSE;
3109 TCHAR szLockInfo[MAX_SESSION_NAME];
3110
3111 // Prepare response message
3112 msg.setCode(CMD_REQUEST_COMPLETED);
3113 msg.setId(pRequest->getId());
3114
3115 // Get node id and check object class and access rights
3116 dwObjectId = pRequest->getFieldAsUInt32(VID_OBJECT_ID);
3117 object = FindObjectById(dwObjectId);
3118 if (object != NULL)
3119 {
3120 if ((object->getObjectClass() == OBJECT_NODE) ||
3121 (object->getObjectClass() == OBJECT_CLUSTER) ||
3122 (object->getObjectClass() == OBJECT_MOBILEDEVICE) ||
3123 (object->getObjectClass() == OBJECT_TEMPLATE))
3124 {
3125 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ))
3126 {
3127 // Try to lock DCI list
3128 if (((Template *)object)->lockDCIList(m_id, m_szUserName, szLockInfo))
3129 {
3130 bSuccess = TRUE;
3131 msg.setField(VID_RCC, RCC_SUCCESS);
3132
3133 // modify list of open nodes DCI lists
3134 m_pOpenDCIList = (UINT32 *)realloc(m_pOpenDCIList, sizeof(UINT32) * (m_dwOpenDCIListSize + 1));
3135 m_pOpenDCIList[m_dwOpenDCIListSize] = dwObjectId;
3136 m_dwOpenDCIListSize++;
3137 }
3138 else
3139 {
3140 msg.setField(VID_RCC, RCC_COMPONENT_LOCKED);
3141 msg.setField(VID_LOCKED_BY, szLockInfo);
3142 }
3143 }
3144 else
3145 {
3146 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3147 }
3148 }
3149 else
3150 {
3151 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3152 }
3153 }
3154 else
3155 {
3156 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3157 }
3158
3159 // Send response
3160 sendMessage(&msg);
3161
3162 // If DCI list was successfully locked, send it to client
3163 if (bSuccess)
3164 ((Template *)object)->sendItemsToClient(this, pRequest->getId());
3165 }
3166
3167 /**
3168 * Unlock node's data collection settings
3169 */
3170 void ClientSession::closeNodeDCIList(NXCPMessage *pRequest)
3171 {
3172 NXCPMessage msg;
3173 UINT32 dwObjectId;
3174 NetObj *object;
3175
3176 // Prepare response message
3177 msg.setCode(CMD_REQUEST_COMPLETED);
3178 msg.setId(pRequest->getId());
3179
3180 // Get node id and check object class and access rights
3181 dwObjectId = pRequest->getFieldAsUInt32(VID_OBJECT_ID);
3182 object = FindObjectById(dwObjectId);
3183 if (object != NULL)
3184 {
3185 if ((object->getObjectClass() == OBJECT_NODE) ||
3186 (object->getObjectClass() == OBJECT_CLUSTER) ||
3187 (object->getObjectClass() == OBJECT_MOBILEDEVICE) ||
3188 (object->getObjectClass() == OBJECT_TEMPLATE))
3189 {
3190 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ))
3191 {
3192 BOOL bSuccess;
3193
3194 // Try to unlock DCI list
3195 bSuccess = ((Template *)object)->unlockDCIList(m_id);
3196 msg.setField(VID_RCC, bSuccess ? RCC_SUCCESS : RCC_OUT_OF_STATE_REQUEST);
3197
3198 // modify list of open nodes DCI lists
3199 if (bSuccess)
3200 {
3201 UINT32 i;
3202
3203 for(i = 0; i < m_dwOpenDCIListSize; i++)
3204 if (m_pOpenDCIList[i] == dwObjectId)
3205 {
3206 m_dwOpenDCIListSize--;
3207 memmove(&m_pOpenDCIList[i], &m_pOpenDCIList[i + 1], sizeof(UINT32) * (m_dwOpenDCIListSize - i));
3208 break;
3209 }
3210 }
3211
3212 // Queue template update
3213 if ((object->getObjectClass() == OBJECT_TEMPLATE) || (object->getObjectClass() == OBJECT_CLUSTER))
3214 ((Template *)object)->queueUpdate();
3215 }
3216 else
3217 {
3218 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3219 }
3220 }
3221 else
3222 {
3223 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3224 }
3225 }
3226 else
3227 {
3228 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3229 }
3230
3231 // Send response
3232 sendMessage(&msg);
3233 }
3234
3235 /**
3236 * Create, modify, or delete data collection item for node
3237 */
3238 void ClientSession::modifyNodeDCI(NXCPMessage *pRequest)
3239 {
3240 NXCPMessage msg;
3241 UINT32 dwObjectId;
3242 NetObj *object;
3243
3244 // Prepare response message
3245 msg.setCode(CMD_REQUEST_COMPLETED);
3246 msg.setId(pRequest->getId());
3247
3248 // Get node id and check object class and access rights
3249 dwObjectId = pRequest->getFieldAsUInt32(VID_OBJECT_ID);
3250 object = FindObjectById(dwObjectId);
3251 if (object != NULL)
3252 {
3253 if ((object->getObjectClass() == OBJECT_NODE) ||
3254 (object->getObjectClass() == OBJECT_CLUSTER) ||
3255 (object->getObjectClass() == OBJECT_MOBILEDEVICE) ||
3256 (object->getObjectClass() == OBJECT_TEMPLATE))
3257 {
3258 if (((Template *)object)->isLockedBySession(m_id))
3259 {
3260 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_MODIFY))
3261 {
3262 UINT32 i, dwItemId, dwNumMaps, *pdwMapId, *pdwMapIndex;
3263 DCObject *dcObject;
3264 BOOL bSuccess = FALSE;
3265
3266 int dcObjectType = (int)pRequest->getFieldAsUInt16(VID_DCOBJECT_TYPE);
3267 switch(pRequest->getCode())
3268 {
3269 case CMD_CREATE_NEW_DCI:
3270 // Create dummy DCI
3271 switch(dcObjectType)
3272 {
3273 case DCO_TYPE_ITEM:
3274 dcObject = new DCItem(CreateUniqueId(IDG_ITEM), _T("no name"), DS_INTERNAL, DCI_DT_INT,
3275 ConfigReadInt(_T("DefaultDCIPollingInterval"), 60),
3276 ConfigReadInt(_T("DefaultDCIRetentionTime"), 30), (Node *)object);
3277 break;
3278 case DCO_TYPE_TABLE:
3279 dcObject = new DCTable(CreateUniqueId(IDG_ITEM), _T("no name"), DS_INTERNAL,
3280 ConfigReadInt(_T("DefaultDCIPollingInterval"), 60),
3281 ConfigReadInt(_T("DefaultDCIRetentionTime"), 30), (Node *)object);
3282 break;
3283 default:
3284 dcObject = NULL;
3285 break;
3286 }
3287 if (dcObject != NULL)
3288 {
3289 dcObject->setStatus(ITEM_STATUS_DISABLED, false);
3290 if ((bSuccess = ((Template *)object)->addDCObject(dcObject)))
3291 {
3292 msg.setField(VID_RCC, RCC_SUCCESS);
3293 // Return new item id to client
3294 msg.setField(VID_DCI_ID, dcObject->getId());
3295 }
3296 else // Unable to add item to node
3297 {
3298 delete dcObject;
3299 msg.setField(VID_RCC, RCC_DUPLICATE_DCI);
3300 }
3301 }
3302 else
3303 {
3304 msg.setField(VID_RCC, RCC_INVALID_ARGUMENT);
3305 }
3306 break;
3307 case CMD_MODIFY_NODE_DCI:
3308 dwItemId = pRequest->getFieldAsUInt32(VID_DCI_ID);
3309 bSuccess = ((Template *)object)->updateDCObject(dwItemId, pRequest, &dwNumMaps, &pdwMapIndex, &pdwMapId);
3310 if (bSuccess)
3311 {
3312 msg.setField(VID_RCC, RCC_SUCCESS);
3313
3314 // Send index to id mapping for newly created thresholds to client
3315 if (dcObjectType == DCO_TYPE_ITEM)
3316 {
3317 msg.setField(VID_DCI_NUM_MAPS, dwNumMaps);
3318 for(i = 0; i < dwNumMaps; i++)
3319 {
3320 pdwMapId[i] = htonl(pdwMapId[i]);
3321 pdwMapIndex[i] = htonl(pdwMapIndex[i]);
3322 }
3323 msg.setField(VID_DCI_MAP_IDS, (BYTE *)pdwMapId, sizeof(UINT32) * dwNumMaps);
3324 msg.setField(VID_DCI_MAP_INDEXES, (BYTE *)pdwMapIndex, sizeof(UINT32) * dwNumMaps);
3325 safe_free(pdwMapId);
3326 safe_free(pdwMapIndex);
3327 }
3328 }
3329 else
3330 {
3331 msg.setField(VID_RCC, RCC_INVALID_DCI_ID);
3332 }
3333 break;
3334 case CMD_DELETE_NODE_DCI:
3335 dwItemId = pRequest->getFieldAsUInt32(VID_DCI_ID);
3336 bSuccess = ((Template *)object)->deleteDCObject(dwItemId, true);
3337 msg.setField(VID_RCC, bSuccess ? RCC_SUCCESS : RCC_INVALID_DCI_ID);
3338 break;
3339 }
3340 if (bSuccess)
3341 ((Template *)object)->setDCIModificationFlag();
3342 }
3343 else // User doesn't have MODIFY rights on object
3344 {
3345 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3346 }
3347 }
3348 else // Nodes DCI list not locked by this session
3349 {
3350 msg.setField(VID_RCC, RCC_OUT_OF_STATE_REQUEST);
3351 }
3352 }
3353 else // Object is not a node
3354 {
3355 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3356 }
3357 }
3358 else // No object with given ID
3359 {
3360 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3361 }
3362
3363 // Send response
3364 sendMessage(&msg);
3365 }
3366
3367 /**
3368 * Change status for one or more DCIs
3369 */
3370 void ClientSession::changeDCIStatus(NXCPMessage *pRequest)
3371 {
3372 NXCPMessage msg;
3373 NetObj *object;
3374
3375 // Prepare response message
3376 msg.setCode(CMD_REQUEST_COMPLETED);
3377 msg.setId(pRequest->getId());
3378
3379 // Get node id and check object class and access rights
3380 object = FindObjectById(pRequest->getFieldAsUInt32(VID_OBJECT_ID));
3381 if (object != NULL)
3382 {
3383 if ((object->getObjectClass() == OBJECT_NODE) ||
3384 (object->getObjectClass() == OBJECT_CLUSTER) ||
3385 (object->getObjectClass() == OBJECT_MOBILEDEVICE) ||
3386 (object->getObjectClass() == OBJECT_TEMPLATE))
3387 {
3388 if (((Template *)object)->isLockedBySession(m_id))
3389 {
3390 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_MODIFY))
3391 {
3392 UINT32 dwNumItems, *pdwItemList;
3393 int iStatus;
3394
3395 iStatus = pRequest->getFieldAsUInt16(VID_DCI_STATUS);
3396 dwNumItems = pRequest->getFieldAsUInt32(VID_NUM_ITEMS);
3397 pdwItemList = (UINT32 *)malloc(sizeof(UINT32) * dwNumItems);
3398 pRequest->getFieldAsInt32Array(VID_ITEM_LIST, dwNumItems, pdwItemList);
3399 if (((Template *)object)->setItemStatus(dwNumItems, pdwItemList, iStatus))
3400 msg.setField(VID_RCC, RCC_SUCCESS);
3401 else
3402 msg.setField(VID_RCC, RCC_INVALID_DCI_ID);
3403 free(pdwItemList);
3404 }
3405 else // User doesn't have MODIFY rights on object
3406 {
3407 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3408 }
3409 }
3410 else // Nodes DCI list not locked by this session
3411 {
3412 msg.setField(VID_RCC, RCC_OUT_OF_STATE_REQUEST);
3413 }
3414 }
3415 else // Object is not a node
3416 {
3417 msg.setField(VID_RCC, RCC_INCOMPATIBLE_OPERATION);
3418 }
3419 }
3420 else // No object with given ID
3421 {
3422 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3423 }
3424
3425 // Send response
3426 sendMessage(&msg);
3427 }
3428
3429 /**
3430 * Clear all collected data for DCI
3431 */
3432 void ClientSession::clearDCIData(NXCPMessage *pRequest)
3433 {
3434 NXCPMessage msg;
3435 NetObj *object;
3436 UINT32 dwItemId;
3437
3438 // Prepare response message
3439 msg.setCode(CMD_REQUEST_COMPLETED);
3440 msg.setId(pRequest->getId());
3441
3442 // Get node id and check object class and access rights
3443 object = FindObjectById(pRequest->getFieldAsUInt32(VID_OBJECT_ID));
3444 if (object != NULL)
3445 {
3446 if ((object->getObjectClass() == OBJECT_NODE) || (object->getObjectClass() == OBJECT_MOBILEDEVICE) || (object->getObjectClass() == OBJECT_CLUSTER))
3447 {
3448 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_DELETE))
3449 {
3450 dwItemId = pRequest->getFieldAsUInt32(VID_DCI_ID);
3451 debugPrintf(4, _T("ClearDCIData: request for DCI %d at node %d"), dwItemId, object->getId());
3452 DCObject *dci = ((Template *)object)->getDCObjectById(dwItemId);
3453 if (dci != NULL)
3454 {
3455 msg.setField(VID_RCC, dci->deleteAllData() ? RCC_SUCCESS : RCC_DB_FAILURE);
3456 debugPrintf(4, _T("ClearDCIData: DCI %d at node %d"), dwItemId, object->getId());
3457 }
3458 else
3459 {
3460 msg.setField(VID_RCC, RCC_INVALID_DCI_ID);
3461 debugPrintf(4, _T("ClearDCIData: DCI %d at node %d not found"), dwItemId, object->getId());
3462 }
3463 }
3464 else // User doesn't have DELETE rights on object
3465 {
3466 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3467 }
3468 }
3469 else // Object is not a node
3470 {
3471 msg.setField(VID_RCC, RCC_INCOMPATIBLE_OPERATION);
3472 }
3473 }
3474 else // No object with given ID
3475 {
3476 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3477 }
3478
3479 // Send response
3480 sendMessage(&msg);
3481 }
3482
3483 /**
3484 * Force DCI data poll for given DCI
3485 */
3486 void ClientSession::forceDCIPoll(NXCPMessage *pRequest)
3487 {
3488 NXCPMessage msg;
3489 NetObj *object;
3490 UINT32 dwItemId;
3491
3492 // Prepare response message
3493 msg.setCode(CMD_REQUEST_COMPLETED);
3494 msg.setId(pRequest->getId());
3495
3496 // Get node id and check object class and access rights
3497 object = FindObjectById(pRequest->getFieldAsUInt32(VID_OBJECT_ID));
3498 if (object != NULL)
3499 {
3500 if ((object->getObjectClass() == OBJECT_NODE) || (object->getObjectClass() == OBJECT_MOBILEDEVICE) || (object->getObjectClass() == OBJECT_CLUSTER))
3501 {
3502 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ))
3503 {
3504 dwItemId = pRequest->getFieldAsUInt32(VID_DCI_ID);
3505 debugPrintf(4, _T("ForceDCIPoll: request for DCI %d at node %d"), dwItemId, object->getId());
3506 DCObject *dci = ((Template *)object)->getDCObjectById(dwItemId);
3507 if (dci != NULL)
3508 {
3509 dci->setLastPollTime(0);
3510 msg.setField(VID_RCC, RCC_SUCCESS);
3511 debugPrintf(4, _T("ForceDCIPoll: DCI %d at node %d"), dwItemId, object->getId());
3512 }
3513 else
3514 {
3515 msg.setField(VID_RCC, RCC_INVALID_DCI_ID);
3516 debugPrintf(4, _T("ForceDCIPoll: DCI %d at node %d not found"), dwItemId, object->getId());
3517 }
3518 }
3519 else // User doesn't have READ rights on object
3520 {
3521 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3522 }
3523 }
3524 else // Object is not a node
3525 {
3526 msg.setField(VID_RCC, RCC_INCOMPATIBLE_OPERATION);
3527 }
3528 }
3529 else // No object with given ID
3530 {
3531 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3532 }
3533
3534 // Send response
3535 sendMessage(&msg);
3536 }
3537
3538 /**
3539 * Copy or move DCI from one node or template to another
3540 */
3541 void ClientSession::copyDCI(NXCPMessage *pRequest)
3542 {
3543 NXCPMessage msg;
3544 NetObj *pSource, *pDestination;
3545 TCHAR szLockInfo[MAX_SESSION_NAME];
3546 BOOL bMove;
3547
3548 // Prepare response message
3549 msg.setCode(CMD_REQUEST_COMPLETED);
3550 msg.setId(pRequest->getId());
3551
3552 // Get source and destination
3553 pSource = FindObjectById(pRequest->getFieldAsUInt32(VID_SOURCE_OBJECT_ID));
3554 pDestination = FindObjectById(pRequest->getFieldAsUInt32(VID_DESTINATION_OBJECT_ID));
3555 if ((pSource != NULL) && (pDestination != NULL))
3556 {
3557 // Check object types
3558 if (((pSource->getObjectClass() == OBJECT_NODE) || (pSource->getObjectClass() == OBJECT_MOBILEDEVICE) || (pSource->getObjectClass() == OBJECT_TEMPLATE) || (pSource->getObjectClass() == OBJECT_CLUSTER)) &&
3559 ((pDestination->getObjectClass() == OBJECT_NODE) || (pDestination->getObjectClass() == OBJECT_MOBILEDEVICE) || (pDestination->getObjectClass() == OBJECT_TEMPLATE) || (pDestination->getObjectClass() == OBJECT_CLUSTER)))
3560 {
3561 if (((Template *)pSource)->isLockedBySession(m_id))
3562 {
3563 bMove = pRequest->getFieldAsUInt16(VID_MOVE_FLAG);
3564 // Check access rights
3565 if ((pSource->checkAccessRights(m_dwUserId, bMove ? (OBJECT_ACCESS_READ | OBJECT_ACCESS_MODIFY) : OBJECT_ACCESS_READ)) &&
3566 (pDestination->checkAccessRights(m_dwUserId, OBJECT_ACCESS_MODIFY)))
3567 {
3568 // Attempt to lock destination's DCI list
3569 if ((pDestination->getId() == pSource->getId()) ||
3570 (((Template *)pDestination)->lockDCIList(m_id, m_szUserName, szLockInfo)))
3571 {
3572 UINT32 i, *pdwItemList, dwNumItems;
3573 const DCObject *pSrcItem;
3574 DCObject *pDstItem;
3575 int iErrors = 0;
3576
3577 // Get list of items to be copied/moved
3578 dwNumItems = pRequest->getFieldAsUInt32(VID_NUM_ITEMS);
3579 pdwItemList = (UINT32 *)malloc(sizeof(UINT32) * dwNumItems);
3580 pRequest->getFieldAsInt32Array(VID_ITEM_LIST, dwNumItems, pdwItemList);
3581
3582 // Copy items
3583 for(i = 0; i < dwNumItems; i++)
3584 {
3585 pSrcItem = ((Template *)pSource)->getDCObjectById(pdwItemList[i]);
3586 if (pSrcItem != NULL)
3587 {
3588 switch(pSrcItem->getType())
3589 {
3590 case DCO_TYPE_ITEM:
3591 pDstItem = new DCItem((DCItem *)pSrcItem);
3592 break;
3593 case DCO_TYPE_TABLE:
3594 pDstItem = new DCTable((DCTable *)pSrcItem);
3595 break;
3596 default:
3597 pDstItem = NULL;
3598 break;
3599 }
3600 if (pDstItem != NULL)
3601 {
3602 pDstItem->setTemplateId(0, 0);
3603 pDstItem->changeBinding(CreateUniqueId(IDG_ITEM),
3604 (Template *)pDestination, FALSE);
3605 if (((Template *)pDestination)->addDCObject(pDstItem))
3606 {
3607 if (bMove)
3608 {
3609 // Delete original item
3610 if (!((Template *)pSource)->deleteDCObject(pdwItemList[i], TRUE))
3611 {
3612 iErrors++;
3613 }
3614 }
3615 }
3616 else
3617 {
3618 delete pDstItem;
3619 iErrors++;
3620 }
3621 }
3622 else
3623 {
3624 DbgPrintf(2, _T("INTERNAL ERROR: ClientSession::CopyDCI(): unknown DCO type %d"), pSrcItem->getType());
3625 iErrors++;
3626 }
3627 }
3628 else
3629 {
3630 iErrors++;
3631 }
3632 }
3633
3634 // Cleanup
3635 free(pdwItemList);
3636 if (pDestination->getId() != pSource->getId())
3637 ((Template *)pDestination)->unlockDCIList(m_id);
3638 msg.setField(VID_RCC, (iErrors == 0) ? RCC_SUCCESS : RCC_DCI_COPY_ERRORS);
3639
3640 // Queue template update
3641 if (pDestination->getObjectClass() == OBJECT_TEMPLATE)
3642 ((Template *)pDestination)->queueUpdate();
3643 }
3644 else // Destination's DCI list already locked by someone else
3645 {
3646 msg.setField(VID_RCC, RCC_COMPONENT_LOCKED);
3647 msg.setField(VID_LOCKED_BY, szLockInfo);
3648 }
3649 }
3650 else // User doesn't have enough rights on object(s)
3651 {
3652 msg.setField(VID_RCC, RCC_ACCESS_DENIED);
3653 }
3654 }
3655 else // Source node DCI list not locked by this session
3656 {
3657 msg.setField(VID_RCC, RCC_OUT_OF_STATE_REQUEST);
3658 }
3659 }
3660 else // Object(s) is not a node
3661 {
3662 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3663 }
3664 }
3665 else // No object(s) with given ID
3666 {
3667 msg.setField(VID_RCC, RCC_INVALID_OBJECT_ID);
3668 }
3669
3670 // Send response
3671 sendMessage(&msg);
3672 }
3673
3674 /**
3675 * Send list of thresholds for DCI
3676 */
3677 void ClientSession::sendDCIThresholds(NXCPMessage *request)
3678 {
3679 NXCPMessage msg;
3680 NetObj *object;
3681
3682 // Prepare response message
3683 msg.setCode(CMD_REQUEST_COMPLETED);
3684 msg.setId(request->getId());
3685
3686 // Get node id and check object class and access rights
3687 object = FindObjectById(request->getFieldAsUInt32(VID_OBJECT_ID));
3688 if (object != NULL)
3689 {
3690 if (object->checkAccessRights(m_dwUserId, OBJECT_ACCESS_READ))
3691 {
3692 if ((object->getObjectClass() == OBJECT_NODE) || (object->getObjectClass() == OBJECT_MOBILEDEVICE) || (object->getObjectClass() == OBJECT_CLUSTER))
3693 {
3694 DCObject *dci = ((Template *)object)->getDCObjectById(request->getFieldAsUInt32(VID_DCI_ID));
3695 if ((dci != NULL) && (dci->getType() == DCO_TYPE_ITEM))