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