6db0c81acba60ebbf22562faf59809d82888e3aa
[reactos.git] / reactos / dll / win32 / netapi32 / user.c
1 /*
2 * Copyright 2002 Andriy Palamarchuk
3 *
4 * netapi32 user functions
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "lmcons.h"
29 #include "lmaccess.h"
30 #include "lmapibuf.h"
31 #include "lmerr.h"
32 #include "lmuse.h"
33 #include "ntsecapi.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
39
40 /* NOTE: So far, this is implemented to support tests that require user logins,
41 * but not designed to handle real user databases. Those should probably
42 * be synced with either the host's user database or with Samba.
43 *
44 * FIXME: The user database should hold all the information the USER_INFO_4 struct
45 * needs, but for the first try, I will just implement the USER_INFO_1 fields.
46 */
47
48 struct sam_user
49 {
50 struct list entry;
51 WCHAR user_name[LM20_UNLEN+1];
52 WCHAR user_password[PWLEN + 1];
53 DWORD sec_since_passwd_change;
54 DWORD user_priv;
55 LPWSTR home_dir;
56 LPWSTR user_comment;
57 DWORD user_flags;
58 LPWSTR user_logon_script_path;
59 };
60
61 static struct list user_list = LIST_INIT( user_list );
62
63 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName);
64
65 /************************************************************
66 * NETAPI_ValidateServername
67 *
68 * Validates server name
69 */
70 static NET_API_STATUS NETAPI_ValidateServername(LPCWSTR ServerName)
71 {
72 if (ServerName)
73 {
74 if (ServerName[0] == 0)
75 return ERROR_BAD_NETPATH;
76 else if (
77 ((ServerName[0] == '\\') &&
78 (ServerName[1] != '\\'))
79 ||
80 ((ServerName[0] == '\\') &&
81 (ServerName[1] == '\\') &&
82 (ServerName[2] == 0))
83 )
84 return ERROR_INVALID_NAME;
85 }
86 return NERR_Success;
87 }
88
89 /************************************************************
90 * NETAPI_FindUser
91 *
92 * Looks for a user in the user database.
93 * Returns a pointer to the entry in the user list when the user
94 * is found, NULL otherwise.
95 */
96 static struct sam_user* NETAPI_FindUser(LPCWSTR UserName)
97 {
98 struct sam_user *user;
99
100 LIST_FOR_EACH_ENTRY(user, &user_list, struct sam_user, entry)
101 {
102 if(lstrcmpW(user->user_name, UserName) == 0)
103 return user;
104 }
105 return NULL;
106 }
107
108 static BOOL NETAPI_IsCurrentUser(LPCWSTR username)
109 {
110 LPWSTR curr_user = NULL;
111 DWORD dwSize;
112 BOOL ret = FALSE;
113
114 dwSize = LM20_UNLEN+1;
115 curr_user = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
116 if(!curr_user)
117 {
118 ERR("Failed to allocate memory for user name.\n");
119 goto end;
120 }
121 if(!GetUserNameW(curr_user, &dwSize))
122 {
123 ERR("Failed to get current user's user name.\n");
124 goto end;
125 }
126 if (!lstrcmpW(curr_user, username))
127 {
128 ret = TRUE;
129 }
130
131 end:
132 HeapFree(GetProcessHeap(), 0, curr_user);
133 return ret;
134 }
135
136 /************************************************************
137 * NetUserAdd (NETAPI32.@)
138 */
139 NET_API_STATUS
140 WINAPI
141 NetUserAdd(LPCWSTR servername,
142 DWORD level,
143 LPBYTE bufptr,
144 LPDWORD parm_err)
145 {
146 NET_API_STATUS status;
147 struct sam_user * su = NULL;
148
149 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_w(servername), level, bufptr, parm_err);
150
151 if((status = NETAPI_ValidateServername(servername)) != NERR_Success)
152 return status;
153
154 switch(level)
155 {
156 /* Level 3 and 4 are identical for the purposes of NetUserAdd */
157 case 4:
158 case 3:
159 FIXME("Level 3 and 4 not implemented.\n");
160 /* Fall through */
161 case 2:
162 FIXME("Level 2 not implemented.\n");
163 /* Fall through */
164 case 1:
165 {
166 PUSER_INFO_1 ui = (PUSER_INFO_1) bufptr;
167 su = HeapAlloc(GetProcessHeap(), 0, sizeof(struct sam_user));
168 if(!su)
169 {
170 status = NERR_InternalError;
171 break;
172 }
173
174 if(lstrlenW(ui->usri1_name) > LM20_UNLEN)
175 {
176 status = NERR_BadUsername;
177 break;
178 }
179
180 /*FIXME: do other checks for a valid username */
181 lstrcpyW(su->user_name, ui->usri1_name);
182
183 if(lstrlenW(ui->usri1_password) > PWLEN)
184 {
185 /* Always return PasswordTooShort on invalid passwords. */
186 status = NERR_PasswordTooShort;
187 break;
188 }
189 lstrcpyW(su->user_password, ui->usri1_password);
190
191 su->sec_since_passwd_change = ui->usri1_password_age;
192 su->user_priv = ui->usri1_priv;
193 su->user_flags = ui->usri1_flags;
194
195 /*FIXME: set the other LPWSTRs to NULL for now */
196 su->home_dir = NULL;
197 su->user_comment = NULL;
198 su->user_logon_script_path = NULL;
199
200 list_add_head(&user_list, &su->entry);
201 return NERR_Success;
202 }
203 default:
204 TRACE("Invalid level %d specified.\n", level);
205 status = ERROR_INVALID_LEVEL;
206 break;
207 }
208
209 HeapFree(GetProcessHeap(), 0, su);
210
211 return status;
212 }
213
214
215 /******************************************************************************
216 * NetUserChangePassword (NETAPI32.@)
217 * PARAMS
218 * domainname [I] Optional. Domain on which the user resides or the logon
219 * domain of the current user if NULL.
220 * username [I] Optional. Username to change the password for or the name
221 * of the current user if NULL.
222 * oldpassword [I] The user's current password.
223 * newpassword [I] The password that the user will be changed to using.
224 *
225 * RETURNS
226 * Success: NERR_Success.
227 * Failure: NERR_* failure code or win error code.
228 *
229 */
230 NET_API_STATUS
231 WINAPI
232 NetUserChangePassword(LPCWSTR domainname,
233 LPCWSTR username,
234 LPCWSTR oldpassword,
235 LPCWSTR newpassword)
236 {
237 struct sam_user *user;
238
239 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname), debugstr_w(username));
240
241 if(domainname)
242 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname));
243
244 if((user = NETAPI_FindUser(username)) == NULL)
245 return NERR_UserNotFound;
246
247 if(lstrcmpW(user->user_password, oldpassword) != 0)
248 return ERROR_INVALID_PASSWORD;
249
250 if(lstrlenW(newpassword) > PWLEN)
251 return ERROR_PASSWORD_RESTRICTION;
252
253 lstrcpyW(user->user_password, newpassword);
254
255 return NERR_Success;
256 }
257
258
259 /************************************************************
260 * NetUserDel (NETAPI32.@)
261 */
262 NET_API_STATUS
263 WINAPI
264 NetUserDel(LPCWSTR servername,
265 LPCWSTR username)
266 {
267 NET_API_STATUS status;
268 struct sam_user *user;
269
270 TRACE("(%s, %s)\n", debugstr_w(servername), debugstr_w(username));
271
272 if((status = NETAPI_ValidateServername(servername))!= NERR_Success)
273 return status;
274
275 if ((user = NETAPI_FindUser(username)) == NULL)
276 return NERR_UserNotFound;
277
278 list_remove(&user->entry);
279
280 HeapFree(GetProcessHeap(), 0, user->home_dir);
281 HeapFree(GetProcessHeap(), 0, user->user_comment);
282 HeapFree(GetProcessHeap(), 0, user->user_logon_script_path);
283 HeapFree(GetProcessHeap(), 0, user);
284
285 return NERR_Success;
286 }
287
288
289 /************************************************************
290 * NetUserEnum (NETAPI32.@)
291 */
292 NET_API_STATUS
293 WINAPI
294 NetUserEnum(LPCWSTR servername,
295 DWORD level,
296 DWORD filter,
297 LPBYTE* bufptr,
298 DWORD prefmaxlen,
299 LPDWORD entriesread,
300 LPDWORD totalentries,
301 LPDWORD resume_handle)
302 {
303 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername), level,
304 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
305
306 return ERROR_ACCESS_DENIED;
307 }
308
309
310 /************************************************************
311 * NetUserGetGroups (NETAPI32.@)
312 */
313 NET_API_STATUS
314 WINAPI
315 NetUserGetGroups(LPCWSTR servername,
316 LPCWSTR username,
317 DWORD level,
318 LPBYTE *bufptr,
319 DWORD prefixmaxlen,
320 LPDWORD entriesread,
321 LPDWORD totalentries)
322 {
323 FIXME("%s %s %d %p %d %p %p stub\n", debugstr_w(servername),
324 debugstr_w(username), level, bufptr, prefixmaxlen, entriesread,
325 totalentries);
326
327 *bufptr = NULL;
328 *entriesread = 0;
329 *totalentries = 0;
330
331 return ERROR_INVALID_LEVEL;
332 }
333
334
335 /************************************************************
336 * NetUserGetInfo (NETAPI32.@)
337 */
338 NET_API_STATUS
339 WINAPI
340 NetUserGetInfo(LPCWSTR servername,
341 LPCWSTR username,
342 DWORD level,
343 LPBYTE* bufptr)
344 {
345 NET_API_STATUS status;
346 TRACE("(%s, %s, %d, %p)\n", debugstr_w(servername), debugstr_w(username),
347 level, bufptr);
348 status = NETAPI_ValidateServername(servername);
349 if (status != NERR_Success)
350 return status;
351
352 if(!NETAPI_IsLocalComputer(servername))
353 {
354 FIXME("Only implemented for local computer, but remote server"
355 "%s was requested.\n", debugstr_w(servername));
356 return NERR_InvalidComputer;
357 }
358
359 if(!NETAPI_FindUser(username) && !NETAPI_IsCurrentUser(username))
360 {
361 TRACE("User %s is unknown.\n", debugstr_w(username));
362 return NERR_UserNotFound;
363 }
364
365 switch (level)
366 {
367 case 0:
368 {
369 PUSER_INFO_0 ui;
370 int name_sz;
371
372 name_sz = lstrlenW(username) + 1;
373
374 /* set up buffer */
375 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
376 (LPVOID *) bufptr);
377
378 ui = (PUSER_INFO_0) *bufptr;
379 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
380
381 /* get data */
382 lstrcpyW(ui->usri0_name, username);
383 break;
384 }
385
386 case 10:
387 {
388 PUSER_INFO_10 ui;
389 PUSER_INFO_0 ui0;
390 NET_API_STATUS status;
391 /* sizes of the field buffers in WCHARS */
392 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
393
394 comment_sz = 1;
395 usr_comment_sz = 1;
396 full_name_sz = 1;
397
398 /* get data */
399 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
400 if (status != NERR_Success)
401 {
402 NetApiBufferFree(ui0);
403 return status;
404 }
405 name_sz = lstrlenW(ui0->usri0_name) + 1;
406
407 /* set up buffer */
408 NetApiBufferAllocate(sizeof(USER_INFO_10) +
409 (name_sz + comment_sz + usr_comment_sz +
410 full_name_sz) * sizeof(WCHAR),
411 (LPVOID *) bufptr);
412 ui = (PUSER_INFO_10) *bufptr;
413 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
414 ui->usri10_comment = (LPWSTR) (
415 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
416 ui->usri10_usr_comment = (LPWSTR) (
417 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
418 ui->usri10_full_name = (LPWSTR) (
419 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
420
421 /* set data */
422 lstrcpyW(ui->usri10_name, ui0->usri0_name);
423 NetApiBufferFree(ui0);
424 ui->usri10_comment[0] = 0;
425 ui->usri10_usr_comment[0] = 0;
426 ui->usri10_full_name[0] = 0;
427 break;
428 }
429
430 case 1:
431 {
432 static const WCHAR homedirW[] = {'H','O','M','E',0};
433 PUSER_INFO_1 ui;
434 PUSER_INFO_0 ui0;
435 NET_API_STATUS status;
436 /* sizes of the field buffers in WCHARS */
437 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
438
439 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
440 comment_sz = 1;
441 script_path_sz = 1;
442
443 /* get data */
444 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
445 if (status != NERR_Success)
446 {
447 NetApiBufferFree(ui0);
448 return status;
449 }
450 name_sz = lstrlenW(ui0->usri0_name) + 1;
451 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
452 /* set up buffer */
453 NetApiBufferAllocate(sizeof(USER_INFO_1) +
454 (name_sz + password_sz + home_dir_sz +
455 comment_sz + script_path_sz) * sizeof(WCHAR),
456 (LPVOID *) bufptr);
457
458 ui = (PUSER_INFO_1) *bufptr;
459 ui->usri1_name = (LPWSTR) (ui + 1);
460 ui->usri1_password = ui->usri1_name + name_sz;
461 ui->usri1_home_dir = ui->usri1_password + password_sz;
462 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
463 ui->usri1_script_path = ui->usri1_comment + comment_sz;
464 /* set data */
465 lstrcpyW(ui->usri1_name, ui0->usri0_name);
466 NetApiBufferFree(ui0);
467 ui->usri1_password[0] = 0;
468 ui->usri1_password_age = 0;
469 ui->usri1_priv = 0;
470 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
471 ui->usri1_comment[0] = 0;
472 ui->usri1_flags = 0;
473 ui->usri1_script_path[0] = 0;
474 break;
475 }
476 case 2:
477 case 3:
478 case 4:
479 case 11:
480 case 20:
481 case 23:
482 case 1003:
483 case 1005:
484 case 1006:
485 case 1007:
486 case 1008:
487 case 1009:
488 case 1010:
489 case 1011:
490 case 1012:
491 case 1013:
492 case 1014:
493 case 1017:
494 case 1018:
495 case 1020:
496 case 1023:
497 case 1024:
498 case 1025:
499 case 1051:
500 case 1052:
501 case 1053:
502 {
503 FIXME("Level %d is not implemented\n", level);
504 return NERR_InternalError;
505 }
506 default:
507 TRACE("Invalid level %d is specified\n", level);
508 return ERROR_INVALID_LEVEL;
509 }
510 return NERR_Success;
511 }
512
513
514 /************************************************************
515 * NetUserGetLocalGroups (NETAPI32.@)
516 */
517 NET_API_STATUS
518 WINAPI
519 NetUserGetLocalGroups(LPCWSTR servername,
520 LPCWSTR username,
521 DWORD level,
522 DWORD flags,
523 LPBYTE* bufptr,
524 DWORD prefmaxlen,
525 LPDWORD entriesread,
526 LPDWORD totalentries)
527 {
528 NET_API_STATUS status;
529 const WCHAR admins[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r','s',0};
530 LPWSTR currentuser;
531 LOCALGROUP_USERS_INFO_0* info;
532 DWORD size;
533
534 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
535 debugstr_w(servername), debugstr_w(username), level, flags, bufptr,
536 prefmaxlen, entriesread, totalentries);
537
538 status = NETAPI_ValidateServername(servername);
539 if (status != NERR_Success)
540 return status;
541
542 size = UNLEN + 1;
543 NetApiBufferAllocate(size * sizeof(WCHAR), (LPVOID*)&currentuser);
544 GetUserNameW(currentuser, &size);
545
546 if (lstrcmpiW(username, currentuser) && NETAPI_FindUser(username))
547 {
548 NetApiBufferFree(currentuser);
549 return NERR_UserNotFound;
550 }
551
552 NetApiBufferFree(currentuser);
553 *totalentries = 1;
554 size = sizeof(*info) + sizeof(admins);
555
556 if(prefmaxlen < size)
557 status = ERROR_MORE_DATA;
558 else
559 status = NetApiBufferAllocate(size, (LPVOID*)&info);
560
561 if(status != NERR_Success)
562 {
563 *bufptr = NULL;
564 *entriesread = 0;
565 return status;
566 }
567
568 info->lgrui0_name = (LPWSTR)((LPBYTE)info + sizeof(*info));
569 lstrcpyW(info->lgrui0_name, admins);
570
571 *bufptr = (LPBYTE)info;
572 *entriesread = 1;
573
574 return NERR_Success;
575 }
576
577
578 /******************************************************************************
579 * NetUserSetGroups (NETAPI32.@)
580 */
581 NET_API_STATUS
582 WINAPI
583 NetUserSetGroups(LPCWSTR servername,
584 LPCWSTR username,
585 DWORD level,
586 LPBYTE buf,
587 DWORD num_entries)
588 {
589 FIXME("(%s %s %lu %p %lu)\n",
590 debugstr_w(servername), debugstr_w(username), level, buf, num_entries);
591 return ERROR_ACCESS_DENIED;
592 }
593
594
595
596 /******************************************************************************
597 * NetUserSetInfo (NETAPI32.@)
598 */
599 NET_API_STATUS
600 WINAPI
601 NetUserSetInfo(LPCWSTR servername,
602 LPCWSTR username,
603 DWORD level,
604 LPBYTE buf,
605 LPDWORD parm_err)
606 {
607 FIXME("(%s %s %lu %p %p)\n",
608 debugstr_w(servername), debugstr_w(username), level, buf, parm_err);
609 return ERROR_ACCESS_DENIED;
610 }
611
612 /* EOF */