2 * Copyright 2002 Andriy Palamarchuk
4 * netapi32 access functions
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.
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.
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
24 #define WIN32_NO_STATUS
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
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.
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.
51 WCHAR user_name[LM20_UNLEN+1];
52 WCHAR user_password[PWLEN + 1];
53 DWORD sec_since_passwd_change;
58 LPWSTR user_logon_script_path;
61 static struct list user_list = LIST_INIT( user_list );
63 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName);
65 /************************************************************
66 * NETAPI_ValidateServername
68 * Validates server name
70 static NET_API_STATUS NETAPI_ValidateServername(LPCWSTR ServerName)
74 if (ServerName[0] == 0)
75 return ERROR_BAD_NETPATH;
77 ((ServerName[0] == '\\') &&
78 (ServerName[1] != '\\'))
80 ((ServerName[0] == '\\') &&
81 (ServerName[1] == '\\') &&
84 return ERROR_INVALID_NAME;
89 /************************************************************
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.
96 static struct sam_user* NETAPI_FindUser(LPCWSTR UserName)
98 struct sam_user *user;
100 LIST_FOR_EACH_ENTRY(user, &user_list, struct sam_user, entry)
102 if(lstrcmpW(user->user_name, UserName) == 0)
108 /************************************************************
109 * NetUserAdd (NETAPI32.@)
111 NET_API_STATUS WINAPI NetUserAdd(LPCWSTR servername,
112 DWORD level, LPBYTE bufptr, LPDWORD parm_err)
114 NET_API_STATUS status;
115 struct sam_user * su = NULL;
117 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_w(servername), level, bufptr, parm_err);
119 if((status = NETAPI_ValidateServername(servername)) != NERR_Success)
124 /* Level 3 and 4 are identical for the purposes of NetUserAdd */
127 FIXME("Level 3 and 4 not implemented.\n");
130 FIXME("Level 2 not implemented.\n");
134 PUSER_INFO_1 ui = (PUSER_INFO_1) bufptr;
135 su = HeapAlloc(GetProcessHeap(), 0, sizeof(struct sam_user));
138 status = NERR_InternalError;
142 if(lstrlenW(ui->usri1_name) > LM20_UNLEN)
144 status = NERR_BadUsername;
148 /*FIXME: do other checks for a valid username */
149 lstrcpyW(su->user_name, ui->usri1_name);
151 if(lstrlenW(ui->usri1_password) > PWLEN)
153 /* Always return PasswordTooShort on invalid passwords. */
154 status = NERR_PasswordTooShort;
157 lstrcpyW(su->user_password, ui->usri1_password);
159 su->sec_since_passwd_change = ui->usri1_password_age;
160 su->user_priv = ui->usri1_priv;
161 su->user_flags = ui->usri1_flags;
163 /*FIXME: set the other LPWSTRs to NULL for now */
165 su->user_comment = NULL;
166 su->user_logon_script_path = NULL;
168 list_add_head(&user_list, &su->entry);
172 TRACE("Invalid level %d specified.\n", level);
173 status = ERROR_INVALID_LEVEL;
177 HeapFree(GetProcessHeap(), 0, su);
182 /************************************************************
183 * NetUserDel (NETAPI32.@)
185 NET_API_STATUS WINAPI NetUserDel(LPCWSTR servername, LPCWSTR username)
187 NET_API_STATUS status;
188 struct sam_user *user;
190 TRACE("(%s, %s)\n", debugstr_w(servername), debugstr_w(username));
192 if((status = NETAPI_ValidateServername(servername))!= NERR_Success)
195 if ((user = NETAPI_FindUser(username)) == NULL)
196 return NERR_UserNotFound;
198 list_remove(&user->entry);
200 HeapFree(GetProcessHeap(), 0, user->home_dir);
201 HeapFree(GetProcessHeap(), 0, user->user_comment);
202 HeapFree(GetProcessHeap(), 0, user->user_logon_script_path);
203 HeapFree(GetProcessHeap(), 0, user);
208 /************************************************************
209 * NetUserGetInfo (NETAPI32.@)
211 NET_API_STATUS WINAPI
212 NetUserGetInfo(LPCWSTR servername, LPCWSTR username, DWORD level,
215 NET_API_STATUS status;
216 TRACE("(%s, %s, %d, %p)\n", debugstr_w(servername), debugstr_w(username),
218 status = NETAPI_ValidateServername(servername);
219 if (status != NERR_Success)
222 if(!NETAPI_IsLocalComputer(servername))
224 FIXME("Only implemented for local computer, but remote server"
225 "%s was requested.\n", debugstr_w(servername));
226 return NERR_InvalidComputer;
229 if(!NETAPI_FindUser(username))
231 TRACE("User %s is unknown.\n", debugstr_w(username));
232 return NERR_UserNotFound;
242 name_sz = lstrlenW(username) + 1;
245 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
248 ui = (PUSER_INFO_0) *bufptr;
249 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
252 lstrcpyW(ui->usri0_name, username);
260 NET_API_STATUS status;
261 /* sizes of the field buffers in WCHARS */
262 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
269 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
270 if (status != NERR_Success)
272 NetApiBufferFree(ui0);
275 name_sz = lstrlenW(ui0->usri0_name) + 1;
278 NetApiBufferAllocate(sizeof(USER_INFO_10) +
279 (name_sz + comment_sz + usr_comment_sz +
280 full_name_sz) * sizeof(WCHAR),
282 ui = (PUSER_INFO_10) *bufptr;
283 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
284 ui->usri10_comment = (LPWSTR) (
285 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
286 ui->usri10_usr_comment = (LPWSTR) (
287 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
288 ui->usri10_full_name = (LPWSTR) (
289 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
292 lstrcpyW(ui->usri10_name, ui0->usri0_name);
293 NetApiBufferFree(ui0);
294 ui->usri10_comment[0] = 0;
295 ui->usri10_usr_comment[0] = 0;
296 ui->usri10_full_name[0] = 0;
302 static const WCHAR homedirW[] = {'H','O','M','E',0};
305 NET_API_STATUS status;
306 /* sizes of the field buffers in WCHARS */
307 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
309 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
314 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
315 if (status != NERR_Success)
317 NetApiBufferFree(ui0);
320 name_sz = lstrlenW(ui0->usri0_name) + 1;
321 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
323 NetApiBufferAllocate(sizeof(USER_INFO_1) +
324 (name_sz + password_sz + home_dir_sz +
325 comment_sz + script_path_sz) * sizeof(WCHAR),
328 ui = (PUSER_INFO_1) *bufptr;
329 ui->usri1_name = (LPWSTR) (ui + 1);
330 ui->usri1_password = ui->usri1_name + name_sz;
331 ui->usri1_home_dir = ui->usri1_password + password_sz;
332 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
333 ui->usri1_script_path = ui->usri1_comment + comment_sz;
335 lstrcpyW(ui->usri1_name, ui0->usri0_name);
336 NetApiBufferFree(ui0);
337 ui->usri1_password[0] = 0;
338 ui->usri1_password_age = 0;
340 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
341 ui->usri1_comment[0] = 0;
343 ui->usri1_script_path[0] = 0;
373 FIXME("Level %d is not implemented\n", level);
374 return NERR_InternalError;
377 TRACE("Invalid level %d is specified\n", level);
378 return ERROR_INVALID_LEVEL;
383 /************************************************************
384 * NetUserGetLocalGroups (NETAPI32.@)
386 NET_API_STATUS WINAPI
387 NetUserGetLocalGroups(LPCWSTR servername, LPCWSTR username, DWORD level,
388 DWORD flags, LPBYTE* bufptr, DWORD prefmaxlen,
389 LPDWORD entriesread, LPDWORD totalentries)
391 NET_API_STATUS status;
393 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
394 debugstr_w(servername), debugstr_w(username), level, flags, bufptr,
395 prefmaxlen, entriesread, totalentries);
397 status = NETAPI_ValidateServername(servername);
398 if (status != NERR_Success)
401 if (!NETAPI_FindUser(username))
402 return NERR_UserNotFound;
404 if (bufptr) *bufptr = NULL;
405 if (entriesread) *entriesread = 0;
406 if (totalentries) *totalentries = 0;
411 /************************************************************
412 * NetUserEnum (NETAPI32.@)
414 NET_API_STATUS WINAPI
415 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
416 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
417 LPDWORD resume_handle)
419 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername), level,
420 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
422 return ERROR_ACCESS_DENIED;
425 /************************************************************
426 * ACCESS_QueryAdminDisplayInformation
428 * Creates a buffer with information for the Admin User
430 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
432 static const WCHAR sAdminUserName[] = {
433 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
435 /* sizes of the field buffers in WCHARS */
436 int name_sz, comment_sz, full_name_sz;
437 PNET_DISPLAY_USER usr;
440 name_sz = lstrlenW(sAdminUserName);
444 *pdwSize = sizeof(NET_DISPLAY_USER);
445 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
446 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
449 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
450 usr->usri1_comment = (LPWSTR) (
451 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
452 usr->usri1_full_name = (LPWSTR) (
453 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
456 lstrcpyW(usr->usri1_name, sAdminUserName);
457 usr->usri1_comment[0] = 0;
458 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
459 usr->usri1_full_name[0] = 0;
460 usr->usri1_user_id = 500;
461 usr->usri1_next_index = 0;
464 /************************************************************
465 * ACCESS_QueryGuestDisplayInformation
467 * Creates a buffer with information for the Guest User
469 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
471 static const WCHAR sGuestUserName[] = {
472 'G','u','e','s','t',0 };
474 /* sizes of the field buffers in WCHARS */
475 int name_sz, comment_sz, full_name_sz;
476 PNET_DISPLAY_USER usr;
479 name_sz = lstrlenW(sGuestUserName);
483 *pdwSize = sizeof(NET_DISPLAY_USER);
484 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
485 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
488 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
489 usr->usri1_comment = (LPWSTR) (
490 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
491 usr->usri1_full_name = (LPWSTR) (
492 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
495 lstrcpyW(usr->usri1_name, sGuestUserName);
496 usr->usri1_comment[0] = 0;
497 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
498 UF_DONT_EXPIRE_PASSWD;
499 usr->usri1_full_name[0] = 0;
500 usr->usri1_user_id = 500;
501 usr->usri1_next_index = 0;
504 /************************************************************
505 * Copies NET_DISPLAY_USER record.
507 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER *dest, LPWSTR *dest_buf,
508 PNET_DISPLAY_USER src)
510 LPWSTR str = *dest_buf;
512 src->usri1_name = str;
513 lstrcpyW(src->usri1_name, dest->usri1_name);
515 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
517 src->usri1_comment = str;
518 lstrcpyW(src->usri1_comment, dest->usri1_comment);
520 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
522 src->usri1_flags = dest->usri1_flags;
524 src->usri1_full_name = str;
525 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
527 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
529 src->usri1_user_id = dest->usri1_user_id;
530 src->usri1_next_index = dest->usri1_next_index;
534 /************************************************************
535 * NetQueryDisplayInformation (NETAPI32.@)
537 * The buffer structure:
538 * - array of fixed size record of the level type
539 * - strings, referenced by the record of the level type
541 NET_API_STATUS WINAPI
542 NetQueryDisplayInformation(
543 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
544 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
547 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
548 Level, Index, EntriesRequested, PreferredMaximumLength,
549 ReturnedEntryCount, SortedBuffer);
551 if(!NETAPI_IsLocalComputer(ServerName))
553 FIXME("Only implemented on local computer, but requested for "
554 "remote server %s\n", debugstr_w(ServerName));
555 return ERROR_ACCESS_DENIED;
563 PNET_DISPLAY_USER inf;
564 /* current available strings buffer */
566 PNET_DISPLAY_USER admin, guest;
567 DWORD admin_size, guest_size;
571 /* sizes of the field buffers in WCHARS */
572 int name_sz, comment_sz, full_name_sz;
574 /* number of the records, returned in SortedBuffer
575 3 - for current user, Administrator and Guest users
579 FIXME("Level %d partially implemented\n", Level);
580 *ReturnedEntryCount = records;
586 NetApiBufferAllocate(dwSize, (LPVOID *) &name);
587 if (!GetUserNameW(name, &dwSize))
589 NetApiBufferFree(name);
590 return ERROR_ACCESS_DENIED;
593 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
594 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
597 dwSize = sizeof(NET_DISPLAY_USER) * records;
598 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
600 NetApiBufferAllocate(dwSize +
601 admin_size - sizeof(NET_DISPLAY_USER) +
602 guest_size - sizeof(NET_DISPLAY_USER),
604 inf = (PNET_DISPLAY_USER) *SortedBuffer;
605 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
606 inf->usri1_name = str;
608 ((PBYTE) str) + name_sz * sizeof(WCHAR));
609 inf->usri1_comment = str;
611 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
612 inf->usri1_full_name = str;
614 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
617 lstrcpyW(inf->usri1_name, name);
618 NetApiBufferFree(name);
619 inf->usri1_comment[0] = 0;
621 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
622 inf->usri1_full_name[0] = 0;
623 inf->usri1_user_id = 0;
624 inf->usri1_next_index = 0;
627 ACCESS_CopyDisplayUser(admin, &str, inf);
628 NetApiBufferFree(admin);
631 ACCESS_CopyDisplayUser(guest, &str, inf);
632 NetApiBufferFree(guest);
639 FIXME("Level %d is not implemented\n", Level);
644 TRACE("Invalid level %d is specified\n", Level);
645 return ERROR_INVALID_LEVEL;
650 /************************************************************
651 * NetGetDCName (NETAPI32.@)
653 * Return the name of the primary domain controller (PDC)
656 NET_API_STATUS WINAPI
657 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
659 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
660 debugstr_w(domainname), bufptr);
661 return NERR_DCNotFound; /* say we can't find a domain controller */
665 /******************************************************************************
666 * NetUserModalsGet (NETAPI32.@)
668 * Retrieves global information for all users and global groups in the security
672 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
673 * on which the function is to execute.
674 * level [I] Information level of the data.
675 * 0 Return global passwords parameters. bufptr points to a
676 * USER_MODALS_INFO_0 struct.
677 * 1 Return logon server and domain controller information. bufptr
678 * points to a USER_MODALS_INFO_1 struct.
679 * 2 Return domain name and identifier. bufptr points to a
680 * USER_MODALS_INFO_2 struct.
681 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
683 * pbuffer [I] Buffer that receives the data.
686 * Success: NERR_Success.
688 * ERROR_ACCESS_DENIED - the user does not have access to the info.
689 * NERR_InvalidComputer - computer name is invalid.
691 NET_API_STATUS WINAPI NetUserModalsGet(
692 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
694 TRACE("(%s %d %p)\n", debugstr_w(szServer), level, pbuffer);
699 /* return global passwords parameters */
700 FIXME("level 0 not implemented!\n");
702 return NERR_InternalError;
704 /* return logon server and domain controller info */
705 FIXME("level 1 not implemented!\n");
707 return NERR_InternalError;
710 /* return domain name and identifier */
711 PUSER_MODALS_INFO_2 umi;
712 LSA_HANDLE policyHandle;
713 LSA_OBJECT_ATTRIBUTES objectAttributes;
714 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
716 PSID domainIdentifier = NULL;
719 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
720 objectAttributes.Length = sizeof(objectAttributes);
722 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
723 POLICY_VIEW_LOCAL_INFORMATION,
725 if (ntStatus != STATUS_SUCCESS)
727 WARN("LsaOpenPolicy failed with NT status %x\n",
728 LsaNtStatusToWinError(ntStatus));
732 ntStatus = LsaQueryInformationPolicy(policyHandle,
733 PolicyAccountDomainInformation,
734 (PVOID *)&domainInfo);
735 if (ntStatus != STATUS_SUCCESS)
737 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
738 LsaNtStatusToWinError(ntStatus));
739 LsaClose(policyHandle);
743 domainIdentifier = domainInfo->DomainSid;
744 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
745 LsaClose(policyHandle);
747 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
748 GetLengthSid(domainIdentifier) +
749 domainNameLen * sizeof(WCHAR),
752 if (ntStatus != NERR_Success)
754 WARN("NetApiBufferAllocate() failed\n");
755 LsaFreeMemory(domainInfo);
759 umi = (USER_MODALS_INFO_2 *) *pbuffer;
760 umi->usrmod2_domain_id = (PSID)(*pbuffer +
761 sizeof(USER_MODALS_INFO_2));
762 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
763 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
765 lstrcpynW(umi->usrmod2_domain_name,
766 domainInfo->DomainName.Buffer,
768 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
771 LsaFreeMemory(domainInfo);
776 /* return lockout information */
777 FIXME("level 3 not implemented!\n");
779 return NERR_InternalError;
781 TRACE("Invalid level %d is specified\n", level);
783 return ERROR_INVALID_LEVEL;
789 /******************************************************************************
790 * NetUserChangePassword (NETAPI32.@)
792 * domainname [I] Optional. Domain on which the user resides or the logon
793 * domain of the current user if NULL.
794 * username [I] Optional. Username to change the password for or the name
795 * of the current user if NULL.
796 * oldpassword [I] The user's current password.
797 * newpassword [I] The password that the user will be changed to using.
800 * Success: NERR_Success.
801 * Failure: NERR_* failure code or win error code.
804 NET_API_STATUS WINAPI NetUserChangePassword(LPCWSTR domainname, LPCWSTR username,
805 LPCWSTR oldpassword, LPCWSTR newpassword)
807 struct sam_user *user;
809 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname), debugstr_w(username));
812 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname));
814 if((user = NETAPI_FindUser(username)) == NULL)
815 return NERR_UserNotFound;
817 if(lstrcmpW(user->user_password, oldpassword) != 0)
818 return ERROR_INVALID_PASSWORD;
820 if(lstrlenW(newpassword) > PWLEN)
821 return ERROR_PASSWORD_RESTRICTION;
823 lstrcpyW(user->user_password, newpassword);
828 NET_API_STATUS WINAPI NetUseAdd(LMSTR servername, DWORD level, LPBYTE bufptr, LPDWORD parm_err)
830 FIXME("%s %d %p %p stub\n", debugstr_w(servername), level, bufptr, parm_err);