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 #ifdef HAVE_SYS_ERRNO_H
25 #include <sys/errno.h>
27 #ifdef HAVE_SYS_WAIT_H
35 #define WIN32_NO_STATUS
45 #include "wine/debug.h"
46 #include "wine/unicode.h"
47 #include "wine/list.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
51 /* NOTE: So far, this is implemented to support tests that require user logins,
52 * but not designed to handle real user databases. Those should probably
53 * be synced with either the host's user database or with Samba.
55 * FIXME: The user database should hold all the information the USER_INFO_4 struct
56 * needs, but for the first try, I will just implement the USER_INFO_1 fields.
62 WCHAR user_name[LM20_UNLEN+1];
63 WCHAR user_password[PWLEN + 1];
64 DWORD sec_since_passwd_change;
69 LPWSTR user_logon_script_path;
72 static struct list user_list = LIST_INIT( user_list );
74 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName);
76 /************************************************************
77 * NETAPI_ValidateServername
79 * Validates server name
81 static NET_API_STATUS NETAPI_ValidateServername(LPCWSTR ServerName)
85 if (ServerName[0] == 0)
86 return ERROR_BAD_NETPATH;
88 ((ServerName[0] == '\\') &&
89 (ServerName[1] != '\\'))
91 ((ServerName[0] == '\\') &&
92 (ServerName[1] == '\\') &&
95 return ERROR_INVALID_NAME;
100 /************************************************************
103 * Looks for a user in the user database.
104 * Returns a pointer to the entry in the user list when the user
105 * is found, NULL otherwise.
107 static struct sam_user* NETAPI_FindUser(LPCWSTR UserName)
109 struct sam_user *user;
111 LIST_FOR_EACH_ENTRY(user, &user_list, struct sam_user, entry)
113 if(lstrcmpW(user->user_name, UserName) == 0)
119 static BOOL NETAPI_IsCurrentUser(LPCWSTR username)
121 LPWSTR curr_user = NULL;
125 dwSize = LM20_UNLEN+1;
126 curr_user = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
129 ERR("Failed to allocate memory for user name.\n");
132 if(!GetUserNameW(curr_user, &dwSize))
134 ERR("Failed to get current user's user name.\n");
137 if (!lstrcmpW(curr_user, username))
143 HeapFree(GetProcessHeap(), 0, curr_user);
147 /************************************************************
148 * NetUserAdd (NETAPI32.@)
150 NET_API_STATUS WINAPI NetUserAdd(LPCWSTR servername,
151 DWORD level, LPBYTE bufptr, LPDWORD parm_err)
153 NET_API_STATUS status;
154 struct sam_user * su = NULL;
156 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_w(servername), level, bufptr, parm_err);
158 if((status = NETAPI_ValidateServername(servername)) != NERR_Success)
163 /* Level 3 and 4 are identical for the purposes of NetUserAdd */
166 FIXME("Level 3 and 4 not implemented.\n");
169 FIXME("Level 2 not implemented.\n");
173 PUSER_INFO_1 ui = (PUSER_INFO_1) bufptr;
174 su = HeapAlloc(GetProcessHeap(), 0, sizeof(struct sam_user));
177 status = NERR_InternalError;
181 if(lstrlenW(ui->usri1_name) > LM20_UNLEN)
183 status = NERR_BadUsername;
187 /*FIXME: do other checks for a valid username */
188 lstrcpyW(su->user_name, ui->usri1_name);
190 if(lstrlenW(ui->usri1_password) > PWLEN)
192 /* Always return PasswordTooShort on invalid passwords. */
193 status = NERR_PasswordTooShort;
196 lstrcpyW(su->user_password, ui->usri1_password);
198 su->sec_since_passwd_change = ui->usri1_password_age;
199 su->user_priv = ui->usri1_priv;
200 su->user_flags = ui->usri1_flags;
202 /*FIXME: set the other LPWSTRs to NULL for now */
204 su->user_comment = NULL;
205 su->user_logon_script_path = NULL;
207 list_add_head(&user_list, &su->entry);
211 TRACE("Invalid level %d specified.\n", level);
212 status = ERROR_INVALID_LEVEL;
216 HeapFree(GetProcessHeap(), 0, su);
221 /************************************************************
222 * NetUserDel (NETAPI32.@)
224 NET_API_STATUS WINAPI NetUserDel(LPCWSTR servername, LPCWSTR username)
226 NET_API_STATUS status;
227 struct sam_user *user;
229 TRACE("(%s, %s)\n", debugstr_w(servername), debugstr_w(username));
231 if((status = NETAPI_ValidateServername(servername))!= NERR_Success)
234 if ((user = NETAPI_FindUser(username)) == NULL)
235 return NERR_UserNotFound;
237 list_remove(&user->entry);
239 HeapFree(GetProcessHeap(), 0, user->home_dir);
240 HeapFree(GetProcessHeap(), 0, user->user_comment);
241 HeapFree(GetProcessHeap(), 0, user->user_logon_script_path);
242 HeapFree(GetProcessHeap(), 0, user);
247 /************************************************************
248 * NetUserGetInfo (NETAPI32.@)
250 NET_API_STATUS WINAPI
251 NetUserGetInfo(LPCWSTR servername, LPCWSTR username, DWORD level,
254 NET_API_STATUS status;
255 TRACE("(%s, %s, %d, %p)\n", debugstr_w(servername), debugstr_w(username),
257 status = NETAPI_ValidateServername(servername);
258 if (status != NERR_Success)
261 if(!NETAPI_IsLocalComputer(servername))
263 FIXME("Only implemented for local computer, but remote server"
264 "%s was requested.\n", debugstr_w(servername));
265 return NERR_InvalidComputer;
268 if(!NETAPI_FindUser(username) && !NETAPI_IsCurrentUser(username))
270 TRACE("User %s is unknown.\n", debugstr_w(username));
271 return NERR_UserNotFound;
281 name_sz = lstrlenW(username) + 1;
284 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
287 ui = (PUSER_INFO_0) *bufptr;
288 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
291 lstrcpyW(ui->usri0_name, username);
299 NET_API_STATUS status;
300 /* sizes of the field buffers in WCHARS */
301 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
308 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
309 if (status != NERR_Success)
311 NetApiBufferFree(ui0);
314 name_sz = lstrlenW(ui0->usri0_name) + 1;
317 NetApiBufferAllocate(sizeof(USER_INFO_10) +
318 (name_sz + comment_sz + usr_comment_sz +
319 full_name_sz) * sizeof(WCHAR),
321 ui = (PUSER_INFO_10) *bufptr;
322 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
323 ui->usri10_comment = (LPWSTR) (
324 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
325 ui->usri10_usr_comment = (LPWSTR) (
326 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
327 ui->usri10_full_name = (LPWSTR) (
328 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
331 lstrcpyW(ui->usri10_name, ui0->usri0_name);
332 NetApiBufferFree(ui0);
333 ui->usri10_comment[0] = 0;
334 ui->usri10_usr_comment[0] = 0;
335 ui->usri10_full_name[0] = 0;
341 static const WCHAR homedirW[] = {'H','O','M','E',0};
344 NET_API_STATUS status;
345 /* sizes of the field buffers in WCHARS */
346 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
348 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
353 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
354 if (status != NERR_Success)
356 NetApiBufferFree(ui0);
359 name_sz = lstrlenW(ui0->usri0_name) + 1;
360 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
362 NetApiBufferAllocate(sizeof(USER_INFO_1) +
363 (name_sz + password_sz + home_dir_sz +
364 comment_sz + script_path_sz) * sizeof(WCHAR),
367 ui = (PUSER_INFO_1) *bufptr;
368 ui->usri1_name = (LPWSTR) (ui + 1);
369 ui->usri1_password = ui->usri1_name + name_sz;
370 ui->usri1_home_dir = ui->usri1_password + password_sz;
371 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
372 ui->usri1_script_path = ui->usri1_comment + comment_sz;
374 lstrcpyW(ui->usri1_name, ui0->usri0_name);
375 NetApiBufferFree(ui0);
376 ui->usri1_password[0] = 0;
377 ui->usri1_password_age = 0;
379 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
380 ui->usri1_comment[0] = 0;
382 ui->usri1_script_path[0] = 0;
412 FIXME("Level %d is not implemented\n", level);
413 return NERR_InternalError;
416 TRACE("Invalid level %d is specified\n", level);
417 return ERROR_INVALID_LEVEL;
422 /************************************************************
423 * NetUserGetLocalGroups (NETAPI32.@)
425 NET_API_STATUS WINAPI
426 NetUserGetLocalGroups(LPCWSTR servername, LPCWSTR username, DWORD level,
427 DWORD flags, LPBYTE* bufptr, DWORD prefmaxlen,
428 LPDWORD entriesread, LPDWORD totalentries)
430 NET_API_STATUS status;
431 const WCHAR admins[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r','s',0};
433 LOCALGROUP_USERS_INFO_0* info;
436 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
437 debugstr_w(servername), debugstr_w(username), level, flags, bufptr,
438 prefmaxlen, entriesread, totalentries);
440 status = NETAPI_ValidateServername(servername);
441 if (status != NERR_Success)
445 NetApiBufferAllocate(size * sizeof(WCHAR), (LPVOID*)¤tuser);
446 if (!GetUserNameW(currentuser, &size)) {
447 NetApiBufferFree(currentuser);
448 return ERROR_NOT_ENOUGH_MEMORY;
451 if (lstrcmpiW(username, currentuser) && NETAPI_FindUser(username))
453 NetApiBufferFree(currentuser);
454 return NERR_UserNotFound;
457 NetApiBufferFree(currentuser);
459 size = sizeof(*info) + sizeof(admins);
461 if(prefmaxlen < size)
462 status = ERROR_MORE_DATA;
464 status = NetApiBufferAllocate(size, (LPVOID*)&info);
466 if(status != NERR_Success)
473 info->lgrui0_name = (LPWSTR)((LPBYTE)info + sizeof(*info));
474 lstrcpyW(info->lgrui0_name, admins);
476 *bufptr = (LPBYTE)info;
482 /************************************************************
483 * NetUserEnum (NETAPI32.@)
485 NET_API_STATUS WINAPI
486 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
487 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
488 LPDWORD resume_handle)
490 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername), level,
491 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
493 return ERROR_ACCESS_DENIED;
496 /************************************************************
497 * ACCESS_QueryAdminDisplayInformation
499 * Creates a buffer with information for the Admin User
501 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
503 static const WCHAR sAdminUserName[] = {
504 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
506 /* sizes of the field buffers in WCHARS */
507 int name_sz, comment_sz, full_name_sz;
508 PNET_DISPLAY_USER usr;
511 name_sz = lstrlenW(sAdminUserName) + 1;
515 *pdwSize = sizeof(NET_DISPLAY_USER);
516 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
517 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
520 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
521 usr->usri1_comment = (LPWSTR) (
522 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
523 usr->usri1_full_name = (LPWSTR) (
524 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
527 lstrcpyW(usr->usri1_name, sAdminUserName);
528 usr->usri1_comment[0] = 0;
529 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
530 usr->usri1_full_name[0] = 0;
531 usr->usri1_user_id = DOMAIN_USER_RID_ADMIN;
532 usr->usri1_next_index = 0;
535 /************************************************************
536 * ACCESS_QueryGuestDisplayInformation
538 * Creates a buffer with information for the Guest User
540 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
542 static const WCHAR sGuestUserName[] = {
543 'G','u','e','s','t',0 };
545 /* sizes of the field buffers in WCHARS */
546 int name_sz, comment_sz, full_name_sz;
547 PNET_DISPLAY_USER usr;
550 name_sz = lstrlenW(sGuestUserName) + 1;
554 *pdwSize = sizeof(NET_DISPLAY_USER);
555 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
556 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
559 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
560 usr->usri1_comment = (LPWSTR) (
561 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
562 usr->usri1_full_name = (LPWSTR) (
563 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
566 lstrcpyW(usr->usri1_name, sGuestUserName);
567 usr->usri1_comment[0] = 0;
568 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
569 UF_DONT_EXPIRE_PASSWD;
570 usr->usri1_full_name[0] = 0;
571 usr->usri1_user_id = DOMAIN_USER_RID_GUEST;
572 usr->usri1_next_index = 0;
575 /************************************************************
576 * Copies NET_DISPLAY_USER record.
578 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER *dest, LPWSTR *dest_buf,
579 PNET_DISPLAY_USER src)
581 LPWSTR str = *dest_buf;
583 src->usri1_name = str;
584 lstrcpyW(src->usri1_name, dest->usri1_name);
586 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
588 src->usri1_comment = str;
589 lstrcpyW(src->usri1_comment, dest->usri1_comment);
591 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
593 src->usri1_flags = dest->usri1_flags;
595 src->usri1_full_name = str;
596 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
598 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
600 src->usri1_user_id = dest->usri1_user_id;
601 src->usri1_next_index = dest->usri1_next_index;
605 /************************************************************
606 * NetQueryDisplayInformation (NETAPI32.@)
608 * The buffer structure:
609 * - array of fixed size record of the level type
610 * - strings, referenced by the record of the level type
612 NET_API_STATUS WINAPI
613 NetQueryDisplayInformation(
614 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
615 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
618 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
619 Level, Index, EntriesRequested, PreferredMaximumLength,
620 ReturnedEntryCount, SortedBuffer);
622 if(!NETAPI_IsLocalComputer(ServerName))
624 FIXME("Only implemented on local computer, but requested for "
625 "remote server %s\n", debugstr_w(ServerName));
626 return ERROR_ACCESS_DENIED;
634 PNET_DISPLAY_USER inf;
635 /* current available strings buffer */
637 PNET_DISPLAY_USER admin, guest;
638 DWORD admin_size, guest_size;
642 /* sizes of the field buffers in WCHARS */
643 int name_sz, comment_sz, full_name_sz;
645 /* number of the records, returned in SortedBuffer
646 3 - for current user, Administrator and Guest users
650 FIXME("Level %d partially implemented\n", Level);
651 *ReturnedEntryCount = records;
657 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &name);
658 if (!GetUserNameW(name, &dwSize))
660 NetApiBufferFree(name);
661 return ERROR_ACCESS_DENIED;
664 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
665 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
668 dwSize = sizeof(NET_DISPLAY_USER) * records;
669 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
671 NetApiBufferAllocate(dwSize +
672 admin_size - sizeof(NET_DISPLAY_USER) +
673 guest_size - sizeof(NET_DISPLAY_USER),
676 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
677 inf->usri1_name = str;
679 ((PBYTE) str) + name_sz * sizeof(WCHAR));
680 inf->usri1_comment = str;
682 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
683 inf->usri1_full_name = str;
685 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
688 lstrcpyW(inf->usri1_name, name);
689 NetApiBufferFree(name);
690 inf->usri1_comment[0] = 0;
692 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
693 inf->usri1_full_name[0] = 0;
694 inf->usri1_user_id = 0;
695 inf->usri1_next_index = 0;
698 ACCESS_CopyDisplayUser(admin, &str, inf);
699 NetApiBufferFree(admin);
702 ACCESS_CopyDisplayUser(guest, &str, inf);
703 NetApiBufferFree(guest);
710 FIXME("Level %d is not implemented\n", Level);
715 TRACE("Invalid level %d is specified\n", Level);
716 return ERROR_INVALID_LEVEL;
721 /************************************************************
722 * NetGetDCName (NETAPI32.@)
724 * Return the name of the primary domain controller (PDC)
727 NET_API_STATUS WINAPI
728 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
730 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
731 debugstr_w(domainname), bufptr);
732 return NERR_DCNotFound; /* say we can't find a domain controller */
735 /************************************************************
736 * NetGroupEnum (NETAPI32.@)
739 NET_API_STATUS WINAPI
740 NetGroupEnum(LPCWSTR servername, DWORD level, LPBYTE *bufptr, DWORD prefmaxlen,
741 LPDWORD entriesread, LPDWORD totalentries, LPDWORD resume_handle)
743 FIXME("(%s, %d, %p, %d, %p, %p, %p) stub!\n", debugstr_w(servername),
744 level, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
745 return ERROR_ACCESS_DENIED;
748 /************************************************************
749 * NetGroupGetInfo (NETAPI32.@)
752 NET_API_STATUS WINAPI NetGroupGetInfo(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE *bufptr)
754 FIXME("(%s, %s, %d, %p) stub!\n", debugstr_w(servername), debugstr_w(groupname), level, bufptr);
755 return ERROR_ACCESS_DENIED;
758 /******************************************************************************
759 * NetUserModalsGet (NETAPI32.@)
761 * Retrieves global information for all users and global groups in the security
765 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
766 * on which the function is to execute.
767 * level [I] Information level of the data.
768 * 0 Return global passwords parameters. bufptr points to a
769 * USER_MODALS_INFO_0 struct.
770 * 1 Return logon server and domain controller information. bufptr
771 * points to a USER_MODALS_INFO_1 struct.
772 * 2 Return domain name and identifier. bufptr points to a
773 * USER_MODALS_INFO_2 struct.
774 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
776 * pbuffer [I] Buffer that receives the data.
779 * Success: NERR_Success.
781 * ERROR_ACCESS_DENIED - the user does not have access to the info.
782 * NERR_InvalidComputer - computer name is invalid.
784 NET_API_STATUS WINAPI NetUserModalsGet(
785 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
787 TRACE("(%s %d %p)\n", debugstr_w(szServer), level, pbuffer);
792 /* return global passwords parameters */
793 FIXME("level 0 not implemented!\n");
795 return NERR_InternalError;
797 /* return logon server and domain controller info */
798 FIXME("level 1 not implemented!\n");
800 return NERR_InternalError;
803 /* return domain name and identifier */
804 PUSER_MODALS_INFO_2 umi;
805 LSA_HANDLE policyHandle;
806 LSA_OBJECT_ATTRIBUTES objectAttributes;
807 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
809 PSID domainIdentifier = NULL;
812 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
813 objectAttributes.Length = sizeof(objectAttributes);
815 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
816 POLICY_VIEW_LOCAL_INFORMATION,
818 if (ntStatus != STATUS_SUCCESS)
820 WARN("LsaOpenPolicy failed with NT status %x\n",
821 LsaNtStatusToWinError(ntStatus));
825 ntStatus = LsaQueryInformationPolicy(policyHandle,
826 PolicyAccountDomainInformation,
827 (PVOID *)&domainInfo);
828 if (ntStatus != STATUS_SUCCESS)
830 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
831 LsaNtStatusToWinError(ntStatus));
832 LsaClose(policyHandle);
836 domainIdentifier = domainInfo->DomainSid;
837 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
838 LsaClose(policyHandle);
840 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
841 GetLengthSid(domainIdentifier) +
842 domainNameLen * sizeof(WCHAR),
845 if (ntStatus != NERR_Success)
847 WARN("NetApiBufferAllocate() failed\n");
848 LsaFreeMemory(domainInfo);
852 umi = (USER_MODALS_INFO_2 *) *pbuffer;
853 umi->usrmod2_domain_id = *pbuffer + sizeof(USER_MODALS_INFO_2);
854 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
855 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
857 lstrcpynW(umi->usrmod2_domain_name,
858 domainInfo->DomainName.Buffer,
860 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
863 LsaFreeMemory(domainInfo);
868 /* return lockout information */
869 FIXME("level 3 not implemented!\n");
871 return NERR_InternalError;
873 TRACE("Invalid level %d is specified\n", level);
875 return ERROR_INVALID_LEVEL;
881 static int fork_smbpasswd( char * const argv[], pid_t *pid )
886 if (pipe( pipe_out ) == -1) return -1;
887 fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
888 fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
890 switch ((*pid = fork()))
893 close( pipe_out[0] );
894 close( pipe_out[1] );
897 dup2( pipe_out[0], 0 );
898 close( pipe_out[0] );
899 close( pipe_out[1] );
900 execvp( "smbpasswd", argv );
901 ERR( "can't execute smbpasswd, is it installed?\n" );
904 close( pipe_out[0] );
909 ERR( "no fork support on this platform\n" );
914 static char *strdup_unixcp( const WCHAR *str )
917 int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
918 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
919 WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
923 static NET_API_STATUS change_password_smb( LPCWSTR domainname, LPCWSTR username,
924 LPCWSTR oldpassword, LPCWSTR newpassword )
926 NET_API_STATUS ret = NERR_Success;
927 static char option_silent[] = "-s";
928 static char option_user[] = "-U";
929 static char option_remote[] = "-r";
930 static char smbpasswd[] = "smbpasswd";
933 char *server = NULL, *user, *argv[7], *old, *new = NULL;
935 if (domainname && !(server = strdup_unixcp( domainname ))) return ERROR_OUTOFMEMORY;
936 if (!(user = strdup_unixcp( username )))
938 HeapFree( GetProcessHeap(), 0, server );
939 return ERROR_OUTOFMEMORY;
942 argv[1] = option_silent;
943 argv[2] = option_user;
947 argv[4] = option_remote;
953 pipe_out = fork_smbpasswd( argv, &pid );
954 HeapFree( GetProcessHeap(), 0, server );
955 HeapFree( GetProcessHeap(), 0, user );
956 if (pipe_out == -1) return NERR_InternalError;
958 if (!(old = strdup_unixcp( oldpassword )))
960 ret = ERROR_OUTOFMEMORY;
963 if (!(new = strdup_unixcp( newpassword )))
965 ret = ERROR_OUTOFMEMORY;
968 write( pipe_out, old, strlen( old ) );
969 write( pipe_out, "\n", 1 );
970 write( pipe_out, new, strlen( new ) );
971 write( pipe_out, "\n", 1 );
972 write( pipe_out, new, strlen( new ) );
973 write( pipe_out, "\n", 1 );
984 wret = waitpid(pid, &status, 0);
985 } while (wret < 0 && errno == EINTR);
986 if (ret == NERR_Success &&
987 (wret < 0 || !WIFEXITED(status) || WEXITSTATUS(status)))
988 ret = NERR_InternalError;
992 HeapFree( GetProcessHeap(), 0, old );
993 HeapFree( GetProcessHeap(), 0, new );
997 /******************************************************************************
998 * NetUserChangePassword (NETAPI32.@)
1000 * domainname [I] Optional. Domain on which the user resides or the logon
1001 * domain of the current user if NULL.
1002 * username [I] Optional. Username to change the password for or the name
1003 * of the current user if NULL.
1004 * oldpassword [I] The user's current password.
1005 * newpassword [I] The password that the user will be changed to using.
1008 * Success: NERR_Success.
1009 * Failure: NERR_* failure code or win error code.
1012 NET_API_STATUS WINAPI NetUserChangePassword(LPCWSTR domainname, LPCWSTR username,
1013 LPCWSTR oldpassword, LPCWSTR newpassword)
1015 struct sam_user *user;
1017 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname), debugstr_w(username));
1019 if (!change_password_smb( domainname, username, oldpassword, newpassword ))
1020 return NERR_Success;
1023 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname));
1025 if((user = NETAPI_FindUser(username)) == NULL)
1026 return NERR_UserNotFound;
1028 if(lstrcmpW(user->user_password, oldpassword) != 0)
1029 return ERROR_INVALID_PASSWORD;
1031 if(lstrlenW(newpassword) > PWLEN)
1032 return ERROR_PASSWORD_RESTRICTION;
1034 lstrcpyW(user->user_password, newpassword);
1036 return NERR_Success;
1039 NET_API_STATUS WINAPI NetUseAdd(LMSTR servername, DWORD level, LPBYTE bufptr, LPDWORD parm_err)
1041 FIXME("%s %d %p %p stub\n", debugstr_w(servername), level, bufptr, parm_err);
1042 return NERR_Success;
1045 NET_API_STATUS WINAPI NetUseDel(LMSTR servername, LMSTR usename, DWORD forcecond)
1047 FIXME("%s %s %d stub\n", debugstr_w(servername), debugstr_w(usename), forcecond);
1048 return NERR_Success;