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