license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxcl / users.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Client Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: users.cpp
21 **
22 **/
23
24 #include "libnxcl.h"
25
26
27 //
28 // Fill user record with data from message
29 //
30
31 void UpdateUserFromMessage(CSCPMessage *pMsg, NXC_USER *pUser)
32 {
33 // Process common fields
34 pUser->dwId = pMsg->GetVariableLong(VID_USER_ID);
35 pMsg->GetVariableStr(VID_USER_NAME, pUser->szName, MAX_USER_NAME);
36 pUser->wFlags = pMsg->GetVariableShort(VID_USER_FLAGS);
37 pUser->dwSystemRights = pMsg->GetVariableLong(VID_USER_SYS_RIGHTS);
38 pMsg->GetVariableStr(VID_USER_DESCRIPTION, pUser->szDescription, MAX_USER_DESCR);
39 pMsg->GetVariableBinary(VID_GUID, pUser->guid, UUID_LENGTH);
40
41 // Process group-specific fields
42 if (pUser->dwId & GROUP_FLAG)
43 {
44 DWORD i, dwId;
45
46 pUser->dwNumMembers = pMsg->GetVariableLong(VID_NUM_MEMBERS);
47 pUser->pdwMemberList = (DWORD *)realloc(pUser->pdwMemberList, sizeof(DWORD) * pUser->dwNumMembers);
48 for(i = 0, dwId = VID_GROUP_MEMBER_BASE; i < pUser->dwNumMembers; i++, dwId++)
49 pUser->pdwMemberList[i] = pMsg->GetVariableLong(dwId);
50 pUser->pszCertMappingData = NULL;
51 }
52 else // User-specific data
53 {
54 pUser->nAuthMethod = pMsg->GetVariableShort(VID_AUTH_METHOD);
55 pMsg->GetVariableStr(VID_USER_FULL_NAME, pUser->szFullName, MAX_USER_FULLNAME);
56 pUser->nCertMappingMethod = pMsg->GetVariableShort(VID_CERT_MAPPING_METHOD);
57 pUser->pszCertMappingData = pMsg->GetVariableStr(VID_CERT_MAPPING_DATA);
58 pUser->pdwMemberList = NULL;
59 }
60 }
61
62
63 //
64 // Load user database
65 // This function is NOT REENTRANT
66 //
67
68 DWORD LIBNXCL_EXPORTABLE NXCLoadUserDB(NXC_SESSION hSession)
69 {
70 return ((NXCL_Session *)hSession)->LoadUserDB();
71 }
72
73
74 //
75 // Find user in database by ID
76 //
77
78 NXC_USER LIBNXCL_EXPORTABLE *NXCFindUserById(NXC_SESSION hSession, DWORD dwId)
79 {
80 return ((NXCL_Session *)hSession)->FindUserById(dwId);
81 }
82
83
84 //
85 // Get pointer to user list and number of users
86 //
87
88 BOOL LIBNXCL_EXPORTABLE NXCGetUserDB(NXC_SESSION hSession, NXC_USER **ppUserList,
89 DWORD *pdwNumUsers)
90 {
91 return ((NXCL_Session *)hSession)->GetUserDB(ppUserList, pdwNumUsers);
92 }
93
94
95 //
96 // Create new user or group on server
97 //
98
99 DWORD LIBNXCL_EXPORTABLE NXCCreateUser(NXC_SESSION hSession, TCHAR *pszName,
100 BOOL bIsGroup, DWORD *pdwNewId)
101 {
102 CSCPMessage msg, *pResponse;
103 DWORD dwRetCode, dwRqId;
104
105 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
106
107 msg.SetCode(CMD_CREATE_USER);
108 msg.SetId(dwRqId);
109 msg.SetVariable(VID_USER_NAME, pszName);
110 msg.SetVariable(VID_IS_GROUP, (WORD)bIsGroup);
111 ((NXCL_Session *)hSession)->SendMsg(&msg);
112
113 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
114 if (pResponse != NULL)
115 {
116 dwRetCode = pResponse->GetVariableLong(VID_RCC);
117 if (dwRetCode == RCC_SUCCESS)
118 *pdwNewId = pResponse->GetVariableLong(VID_USER_ID);
119 delete pResponse;
120 }
121 else
122 {
123 dwRetCode = RCC_TIMEOUT;
124 }
125 return dwRetCode;
126 }
127
128
129 //
130 // Delete user or group
131 //
132
133 DWORD LIBNXCL_EXPORTABLE NXCDeleteUser(NXC_SESSION hSession, DWORD dwId)
134 {
135 CSCPMessage msg;
136 DWORD dwRqId;
137
138 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
139
140 msg.SetCode(CMD_DELETE_USER);
141 msg.SetId(dwRqId);
142 msg.SetVariable(VID_USER_ID, dwId);
143 ((NXCL_Session *)hSession)->SendMsg(&msg);
144
145 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
146 }
147
148
149 //
150 // Lock user database
151 //
152
153 DWORD LIBNXCL_EXPORTABLE NXCLockUserDB(NXC_SESSION hSession)
154 {
155 return ((NXCL_Session *)hSession)->SimpleCommand(CMD_LOCK_USER_DB);
156 }
157
158
159 //
160 // Unlock user database
161 //
162
163 DWORD LIBNXCL_EXPORTABLE NXCUnlockUserDB(NXC_SESSION hSession)
164 {
165 return ((NXCL_Session *)hSession)->SimpleCommand(CMD_UNLOCK_USER_DB);
166 }
167
168
169 //
170 // Modify user record
171 //
172
173 DWORD LIBNXCL_EXPORTABLE NXCModifyUser(NXC_SESSION hSession, NXC_USER *pUserInfo)
174 {
175 return NXCModifyUserEx(hSession, pUserInfo, 0xFFFFFFFF);
176 }
177
178 DWORD LIBNXCL_EXPORTABLE NXCModifyUserEx(NXC_SESSION hSession, NXC_USER *pUserInfo, DWORD dwFields)
179 {
180 CSCPMessage msg;
181 DWORD i, dwId, dwRqId;
182
183 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
184
185 // Fill in request
186 msg.SetCode(CMD_UPDATE_USER);
187 msg.SetId(dwRqId);
188 msg.SetVariable(VID_FIELDS, dwFields);
189 msg.SetVariable(VID_USER_ID, pUserInfo->dwId);
190 msg.SetVariable(VID_USER_NAME, pUserInfo->szName);
191 msg.SetVariable(VID_USER_DESCRIPTION, pUserInfo->szDescription);
192 msg.SetVariable(VID_USER_FLAGS, pUserInfo->wFlags);
193 msg.SetVariable(VID_USER_SYS_RIGHTS, pUserInfo->dwSystemRights);
194
195 // Group-specific fields
196 if (pUserInfo->dwId & GROUP_FLAG)
197 {
198 msg.SetVariable(VID_NUM_MEMBERS, pUserInfo->dwNumMembers);
199 for(i = 0, dwId = VID_GROUP_MEMBER_BASE; i < pUserInfo->dwNumMembers; i++, dwId++)
200 msg.SetVariable(dwId, pUserInfo->pdwMemberList[i]);
201 }
202 else // User-specific fields
203 {
204 msg.SetVariable(VID_USER_FULL_NAME, pUserInfo->szFullName);
205 msg.SetVariable(VID_AUTH_METHOD, (WORD)pUserInfo->nAuthMethod);
206 msg.SetVariable(VID_CERT_MAPPING_METHOD, (WORD)pUserInfo->nCertMappingMethod);
207 msg.SetVariable(VID_CERT_MAPPING_DATA, CHECK_NULL_EX(pUserInfo->pszCertMappingData));
208 }
209
210 ((NXCL_Session *)hSession)->SendMsg(&msg);
211
212 // Wait for response
213 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
214 }
215
216
217 //
218 // Set password for user
219 //
220
221 DWORD LIBNXCL_EXPORTABLE NXCSetPassword(NXC_SESSION hSession, DWORD userId,
222 const TCHAR *newPassword, const TCHAR *oldPassword)
223 {
224 CSCPMessage msg;
225 DWORD dwRqId;
226
227 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
228
229 msg.SetCode(CMD_SET_PASSWORD);
230 msg.SetId(dwRqId);
231 msg.SetVariable(VID_USER_ID, userId);
232 msg.SetVariable(VID_PASSWORD, newPassword);
233 if (oldPassword != NULL)
234 msg.SetVariable(VID_OLD_PASSWORD, oldPassword);
235
236 ((NXCL_Session *)hSession)->SendMsg(&msg);
237 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
238 }
239
240
241 //
242 // Get user variable
243 //
244
245 DWORD LIBNXCL_EXPORTABLE NXCGetUserVariable(NXC_SESSION hSession, DWORD dwUserId,
246 TCHAR *pszVarName, TCHAR *pszValue, DWORD dwSize)
247 {
248 CSCPMessage msg, *pResponse;
249 DWORD dwRqId, dwResult;
250
251 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
252
253 msg.SetCode(CMD_GET_USER_VARIABLE);
254 msg.SetId(dwRqId);
255 msg.SetVariable(VID_NAME, pszVarName);
256 if (dwUserId != CURRENT_USER)
257 msg.SetVariable(VID_USER_ID, dwUserId);
258 ((NXCL_Session *)hSession)->SendMsg(&msg);
259
260 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
261 if (pResponse != NULL)
262 {
263 dwResult = pResponse->GetVariableLong(VID_RCC);
264 if (dwResult == RCC_SUCCESS)
265 pResponse->GetVariableStr(VID_VALUE, pszValue, dwSize);
266 delete pResponse;
267 }
268 else
269 {
270 dwResult = RCC_TIMEOUT;
271 }
272
273 return dwResult;
274 }
275
276
277 //
278 // Set user variable
279 //
280
281 DWORD LIBNXCL_EXPORTABLE NXCSetUserVariable(NXC_SESSION hSession, DWORD dwUserId,
282 TCHAR *pszVarName, TCHAR *pszValue)
283 {
284 CSCPMessage msg;
285 DWORD dwRqId;
286
287 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
288
289 msg.SetCode(CMD_SET_USER_VARIABLE);
290 msg.SetId(dwRqId);
291 msg.SetVariable(VID_NAME, pszVarName);
292 msg.SetVariable(VID_VALUE, pszValue);
293 if (dwUserId != CURRENT_USER)
294 msg.SetVariable(VID_USER_ID, dwUserId);
295 ((NXCL_Session *)hSession)->SendMsg(&msg);
296
297 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
298 }
299
300
301 //
302 // Copy or move user variable
303 //
304
305 DWORD LIBNXCL_EXPORTABLE NXCCopyUserVariable(NXC_SESSION hSession, DWORD dwSrcUserId,
306 DWORD dwDstUserId, TCHAR *pszVarName,
307 BOOL bMove)
308 {
309 CSCPMessage msg;
310 DWORD dwRqId;
311
312 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
313
314 msg.SetCode(CMD_COPY_USER_VARIABLE);
315 msg.SetId(dwRqId);
316 msg.SetVariable(VID_NAME, pszVarName);
317 if (dwSrcUserId != CURRENT_USER)
318 msg.SetVariable(VID_USER_ID, dwSrcUserId);
319 msg.SetVariable(VID_DST_USER_ID, dwDstUserId);
320 msg.SetVariable(VID_MOVE_FLAG, (WORD)bMove);
321 ((NXCL_Session *)hSession)->SendMsg(&msg);
322
323 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
324 }
325
326
327 //
328 // Delete user variable
329 //
330
331 DWORD LIBNXCL_EXPORTABLE NXCDeleteUserVariable(NXC_SESSION hSession, DWORD dwUserId,
332 TCHAR *pszVarName)
333 {
334 CSCPMessage msg;
335 DWORD dwRqId;
336
337 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
338
339 msg.SetCode(CMD_DELETE_USER_VARIABLE);
340 msg.SetId(dwRqId);
341 msg.SetVariable(VID_NAME, pszVarName);
342 if (dwUserId != CURRENT_USER)
343 msg.SetVariable(VID_USER_ID, dwUserId);
344 ((NXCL_Session *)hSession)->SendMsg(&msg);
345
346 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
347 }
348
349
350 //
351 // Enumerate user variables
352 //
353
354 DWORD LIBNXCL_EXPORTABLE NXCEnumUserVariables(NXC_SESSION hSession, DWORD dwUserId,
355 TCHAR *pszPattern, DWORD *pdwNumVars,
356 TCHAR ***pppszVarList)
357 {
358 CSCPMessage msg, *pResponse;
359 DWORD i, dwId, dwRqId, dwResult;
360
361 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
362
363 msg.SetCode(CMD_ENUM_USER_VARIABLES);
364 msg.SetId(dwRqId);
365 msg.SetVariable(VID_SEARCH_PATTERN, pszPattern);
366 if (dwUserId != CURRENT_USER)
367 msg.SetVariable(VID_USER_ID, dwUserId);
368 ((NXCL_Session *)hSession)->SendMsg(&msg);
369
370 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
371 if (pResponse != NULL)
372 {
373 dwResult = pResponse->GetVariableLong(VID_RCC);
374 if (dwResult == RCC_SUCCESS)
375 {
376 *pdwNumVars = pResponse->GetVariableLong(VID_NUM_VARIABLES);
377 if (*pdwNumVars > 0)
378 {
379 *pppszVarList = (TCHAR **)malloc(sizeof(TCHAR *) * (*pdwNumVars));
380 for(i = 0, dwId = VID_VARLIST_BASE; i < *pdwNumVars; i++, dwId++)
381 (*pppszVarList)[i] = pResponse->GetVariableStr(dwId);
382 }
383 else
384 {
385 *pppszVarList = NULL;
386 }
387 }
388 delete pResponse;
389 }
390 else
391 {
392 dwResult = RCC_TIMEOUT;
393 }
394
395 return dwResult;
396 }
397
398
399 //
400 // Get session list
401 //
402
403 DWORD LIBNXCL_EXPORTABLE NXCGetSessionList(NXC_SESSION hSession, DWORD *pdwNumSessions,
404 NXC_CLIENT_SESSION_INFO **ppList)
405 {
406 CSCPMessage msg, *pResponse;
407 DWORD i, dwId, dwRqId, dwResult;
408
409 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
410
411 msg.SetCode(CMD_GET_SESSION_LIST);
412 msg.SetId(dwRqId);
413 ((NXCL_Session *)hSession)->SendMsg(&msg);
414
415 *pdwNumSessions = 0;
416 *ppList = NULL;
417
418 pResponse = ((NXCL_Session *)hSession)->WaitForMessage(CMD_REQUEST_COMPLETED, dwRqId);
419 if (pResponse != NULL)
420 {
421 dwResult = pResponse->GetVariableLong(VID_RCC);
422 if (dwResult == RCC_SUCCESS)
423 {
424 *pdwNumSessions = pResponse->GetVariableLong(VID_NUM_SESSIONS);
425 if (*pdwNumSessions > 0)
426 {
427 *ppList = (NXC_CLIENT_SESSION_INFO *)malloc(sizeof(NXC_CLIENT_SESSION_INFO) * (*pdwNumSessions));
428 for(i = 0; i < *pdwNumSessions; i++)
429 {
430 dwId = i * 100;
431 (*ppList)[i].dwSessionId = pResponse->GetVariableLong(dwId++);
432 (*ppList)[i].nCipher = pResponse->GetVariableShort(dwId++);
433 pResponse->GetVariableStr(dwId++, (*ppList)[i].szUserName, MAX_USER_NAME);
434 pResponse->GetVariableStr(dwId++, (*ppList)[i].szClientApp, MAX_DB_STRING);
435 }
436 }
437 }
438 delete pResponse;
439 }
440 else
441 {
442 dwResult = RCC_TIMEOUT;
443 }
444
445 return dwResult;
446 }
447
448
449 //
450 // Forcibly close client session
451 //
452
453 DWORD LIBNXCL_EXPORTABLE NXCKillSession(NXC_SESSION hSession, DWORD dwSessionId)
454 {
455 CSCPMessage msg;
456 DWORD dwRqId;
457
458 dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
459
460 msg.SetCode(CMD_KILL_SESSION);
461 msg.SetId(dwRqId);
462 msg.SetVariable(VID_SESSION_ID, dwSessionId);
463 ((NXCL_Session *)hSession)->SendMsg(&msg);
464
465 return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
466 }
467
468
469 //
470 // Get Id of currently logged in user
471 //
472
473 DWORD LIBNXCL_EXPORTABLE NXCGetCurrentUserId(NXC_SESSION hSession)
474 {
475 return ((NXCL_Session *)hSession)->GetCurrentUserId();
476 }
477
478
479 //
480 // Get system access rights of currently logged in user
481 //
482
483 DWORD LIBNXCL_EXPORTABLE NXCGetCurrentSystemAccess(NXC_SESSION hSession)
484 {
485 return ((NXCL_Session *)hSession)->GetCurrentSystemAccess();
486 }