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