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
29 #define WIN32_NO_STATUS
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
45 /* NOTE: So far, this is implemented to support tests that require user logins,
46 * but not designed to handle real user databases. Those should probably
47 * be synced with either the host's user database or with Samba.
49 * FIXME: The user database should hold all the information the USER_INFO_4 struct
50 * needs, but for the first try, I will just implement the USER_INFO_1 fields.
56 WCHAR user_name[LM20_UNLEN+1];
57 WCHAR user_password[PWLEN + 1];
58 DWORD sec_since_passwd_change;
63 LPWSTR user_logon_script_path;
66 static struct list user_list = LIST_INIT( user_list );
68 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName);
70 /************************************************************
71 * NETAPI_ValidateServername
73 * Validates server name
75 static NET_API_STATUS NETAPI_ValidateServername(LPCWSTR ServerName)
79 if (ServerName[0] == 0)
80 return ERROR_BAD_NETPATH;
82 ((ServerName[0] == '\\') &&
83 (ServerName[1] != '\\'))
85 ((ServerName[0] == '\\') &&
86 (ServerName[1] == '\\') &&
89 return ERROR_INVALID_NAME;
94 /************************************************************
97 * Looks for a user in the user database.
98 * Returns a pointer to the entry in the user list when the user
99 * is found, NULL otherwise.
101 static struct sam_user* NETAPI_FindUser(LPCWSTR UserName)
103 struct sam_user *user;
105 LIST_FOR_EACH_ENTRY(user, &user_list, struct sam_user, entry)
107 if(lstrcmpW(user->user_name, UserName) == 0)
113 static BOOL NETAPI_IsCurrentUser(LPCWSTR username)
115 LPWSTR curr_user = NULL;
119 dwSize = LM20_UNLEN+1;
120 curr_user = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
123 ERR("Failed to allocate memory for user name.\n");
126 if(!GetUserNameW(curr_user, &dwSize))
128 ERR("Failed to get current user's user name.\n");
131 if (!lstrcmpW(curr_user, username))
137 HeapFree(GetProcessHeap(), 0, curr_user);
141 /************************************************************
142 * NetUserAdd (NETAPI32.@)
144 NET_API_STATUS WINAPI NetUserAdd(LPCWSTR servername,
145 DWORD level, LPBYTE bufptr, LPDWORD parm_err)
147 NET_API_STATUS status;
148 struct sam_user * su = NULL;
150 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_w(servername), level, bufptr, parm_err);
152 if((status = NETAPI_ValidateServername(servername)) != NERR_Success)
157 /* Level 3 and 4 are identical for the purposes of NetUserAdd */
160 FIXME("Level 3 and 4 not implemented.\n");
163 FIXME("Level 2 not implemented.\n");
167 PUSER_INFO_1 ui = (PUSER_INFO_1) bufptr;
168 su = HeapAlloc(GetProcessHeap(), 0, sizeof(struct sam_user));
171 status = NERR_InternalError;
175 if(lstrlenW(ui->usri1_name) > LM20_UNLEN)
177 status = NERR_BadUsername;
181 /*FIXME: do other checks for a valid username */
182 lstrcpyW(su->user_name, ui->usri1_name);
184 if(lstrlenW(ui->usri1_password) > PWLEN)
186 /* Always return PasswordTooShort on invalid passwords. */
187 status = NERR_PasswordTooShort;
190 lstrcpyW(su->user_password, ui->usri1_password);
192 su->sec_since_passwd_change = ui->usri1_password_age;
193 su->user_priv = ui->usri1_priv;
194 su->user_flags = ui->usri1_flags;
196 /*FIXME: set the other LPWSTRs to NULL for now */
198 su->user_comment = NULL;
199 su->user_logon_script_path = NULL;
201 list_add_head(&user_list, &su->entry);
205 TRACE("Invalid level %d specified.\n", level);
206 status = ERROR_INVALID_LEVEL;
210 HeapFree(GetProcessHeap(), 0, su);
215 /************************************************************
216 * NetUserDel (NETAPI32.@)
218 NET_API_STATUS WINAPI NetUserDel(LPCWSTR servername, LPCWSTR username)
220 NET_API_STATUS status;
221 struct sam_user *user;
223 TRACE("(%s, %s)\n", debugstr_w(servername), debugstr_w(username));
225 if((status = NETAPI_ValidateServername(servername))!= NERR_Success)
228 if ((user = NETAPI_FindUser(username)) == NULL)
229 return NERR_UserNotFound;
231 list_remove(&user->entry);
233 HeapFree(GetProcessHeap(), 0, user->home_dir);
234 HeapFree(GetProcessHeap(), 0, user->user_comment);
235 HeapFree(GetProcessHeap(), 0, user->user_logon_script_path);
236 HeapFree(GetProcessHeap(), 0, user);
241 /************************************************************
242 * NetUserGetInfo (NETAPI32.@)
244 NET_API_STATUS WINAPI
245 NetUserGetInfo(LPCWSTR servername, LPCWSTR username, DWORD level,
248 NET_API_STATUS status;
249 TRACE("(%s, %s, %d, %p)\n", debugstr_w(servername), debugstr_w(username),
251 status = NETAPI_ValidateServername(servername);
252 if (status != NERR_Success)
255 if(!NETAPI_IsLocalComputer(servername))
257 FIXME("Only implemented for local computer, but remote server"
258 "%s was requested.\n", debugstr_w(servername));
259 return NERR_InvalidComputer;
262 if(!NETAPI_FindUser(username) && !NETAPI_IsCurrentUser(username))
264 TRACE("User %s is unknown.\n", debugstr_w(username));
265 return NERR_UserNotFound;
275 name_sz = lstrlenW(username) + 1;
278 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
281 ui = (PUSER_INFO_0) *bufptr;
282 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
285 lstrcpyW(ui->usri0_name, username);
293 NET_API_STATUS status;
294 /* sizes of the field buffers in WCHARS */
295 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
302 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
303 if (status != NERR_Success)
305 NetApiBufferFree(ui0);
308 name_sz = lstrlenW(ui0->usri0_name) + 1;
311 NetApiBufferAllocate(sizeof(USER_INFO_10) +
312 (name_sz + comment_sz + usr_comment_sz +
313 full_name_sz) * sizeof(WCHAR),
315 ui = (PUSER_INFO_10) *bufptr;
316 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
317 ui->usri10_comment = (LPWSTR) (
318 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
319 ui->usri10_usr_comment = (LPWSTR) (
320 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
321 ui->usri10_full_name = (LPWSTR) (
322 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
325 lstrcpyW(ui->usri10_name, ui0->usri0_name);
326 NetApiBufferFree(ui0);
327 ui->usri10_comment[0] = 0;
328 ui->usri10_usr_comment[0] = 0;
329 ui->usri10_full_name[0] = 0;
335 static const WCHAR homedirW[] = {'H','O','M','E',0};
338 NET_API_STATUS status;
339 /* sizes of the field buffers in WCHARS */
340 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
342 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
347 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
348 if (status != NERR_Success)
350 NetApiBufferFree(ui0);
353 name_sz = lstrlenW(ui0->usri0_name) + 1;
354 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
356 NetApiBufferAllocate(sizeof(USER_INFO_1) +
357 (name_sz + password_sz + home_dir_sz +
358 comment_sz + script_path_sz) * sizeof(WCHAR),
361 ui = (PUSER_INFO_1) *bufptr;
362 ui->usri1_name = (LPWSTR) (ui + 1);
363 ui->usri1_password = ui->usri1_name + name_sz;
364 ui->usri1_home_dir = ui->usri1_password + password_sz;
365 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
366 ui->usri1_script_path = ui->usri1_comment + comment_sz;
368 lstrcpyW(ui->usri1_name, ui0->usri0_name);
369 NetApiBufferFree(ui0);
370 ui->usri1_password[0] = 0;
371 ui->usri1_password_age = 0;
373 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
374 ui->usri1_comment[0] = 0;
376 ui->usri1_script_path[0] = 0;
406 FIXME("Level %d is not implemented\n", level);
407 return NERR_InternalError;
410 TRACE("Invalid level %d is specified\n", level);
411 return ERROR_INVALID_LEVEL;
416 /************************************************************
417 * NetUserGetLocalGroups (NETAPI32.@)
419 NET_API_STATUS WINAPI
420 NetUserGetLocalGroups(LPCWSTR servername, LPCWSTR username, DWORD level,
421 DWORD flags, LPBYTE* bufptr, DWORD prefmaxlen,
422 LPDWORD entriesread, LPDWORD totalentries)
424 NET_API_STATUS status;
425 const WCHAR admins[] = {'A','d','m','i','n','i','s','t','r','a','t','o','r','s',0};
427 LOCALGROUP_USERS_INFO_0* info;
430 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
431 debugstr_w(servername), debugstr_w(username), level, flags, bufptr,
432 prefmaxlen, entriesread, totalentries);
434 status = NETAPI_ValidateServername(servername);
435 if (status != NERR_Success)
439 NetApiBufferAllocate(size * sizeof(WCHAR), (LPVOID*)¤tuser);
440 GetUserNameW(currentuser, &size);
442 if (lstrcmpiW(username, currentuser) && NETAPI_FindUser(username))
444 NetApiBufferFree(currentuser);
445 return NERR_UserNotFound;
448 NetApiBufferFree(currentuser);
450 size = sizeof(*info) + sizeof(admins);
452 if(prefmaxlen < size)
453 status = ERROR_MORE_DATA;
455 status = NetApiBufferAllocate(size, (LPVOID*)&info);
457 if(status != NERR_Success)
464 info->lgrui0_name = (LPWSTR)((LPBYTE)info + sizeof(*info));
465 lstrcpyW(info->lgrui0_name, admins);
467 *bufptr = (LPBYTE)info;
473 /************************************************************
474 * NetUserEnum (NETAPI32.@)
476 NET_API_STATUS WINAPI
477 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
478 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
479 LPDWORD resume_handle)
481 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername), level,
482 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
484 return ERROR_ACCESS_DENIED;
487 /************************************************************
488 * ACCESS_QueryAdminDisplayInformation
490 * Creates a buffer with information for the Admin User
492 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
494 static const WCHAR sAdminUserName[] = {
495 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
497 /* sizes of the field buffers in WCHARS */
498 int name_sz, comment_sz, full_name_sz;
499 PNET_DISPLAY_USER usr;
502 name_sz = lstrlenW(sAdminUserName) + 1;
506 *pdwSize = sizeof(NET_DISPLAY_USER);
507 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
508 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
511 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
512 usr->usri1_comment = (LPWSTR) (
513 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
514 usr->usri1_full_name = (LPWSTR) (
515 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
518 lstrcpyW(usr->usri1_name, sAdminUserName);
519 usr->usri1_comment[0] = 0;
520 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
521 usr->usri1_full_name[0] = 0;
522 usr->usri1_user_id = DOMAIN_USER_RID_ADMIN;
523 usr->usri1_next_index = 0;
526 /************************************************************
527 * ACCESS_QueryGuestDisplayInformation
529 * Creates a buffer with information for the Guest User
531 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
533 static const WCHAR sGuestUserName[] = {
534 'G','u','e','s','t',0 };
536 /* sizes of the field buffers in WCHARS */
537 int name_sz, comment_sz, full_name_sz;
538 PNET_DISPLAY_USER usr;
541 name_sz = lstrlenW(sGuestUserName) + 1;
545 *pdwSize = sizeof(NET_DISPLAY_USER);
546 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
547 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
550 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
551 usr->usri1_comment = (LPWSTR) (
552 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
553 usr->usri1_full_name = (LPWSTR) (
554 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
557 lstrcpyW(usr->usri1_name, sGuestUserName);
558 usr->usri1_comment[0] = 0;
559 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
560 UF_DONT_EXPIRE_PASSWD;
561 usr->usri1_full_name[0] = 0;
562 usr->usri1_user_id = DOMAIN_USER_RID_GUEST;
563 usr->usri1_next_index = 0;
566 /************************************************************
567 * Copies NET_DISPLAY_USER record.
569 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER *dest, LPWSTR *dest_buf,
570 PNET_DISPLAY_USER src)
572 LPWSTR str = *dest_buf;
574 src->usri1_name = str;
575 lstrcpyW(src->usri1_name, dest->usri1_name);
577 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
579 src->usri1_comment = str;
580 lstrcpyW(src->usri1_comment, dest->usri1_comment);
582 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
584 src->usri1_flags = dest->usri1_flags;
586 src->usri1_full_name = str;
587 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
589 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
591 src->usri1_user_id = dest->usri1_user_id;
592 src->usri1_next_index = dest->usri1_next_index;
596 /************************************************************
597 * NetQueryDisplayInformation (NETAPI32.@)
599 * The buffer structure:
600 * - array of fixed size record of the level type
601 * - strings, referenced by the record of the level type
603 NET_API_STATUS WINAPI
604 NetQueryDisplayInformation(
605 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
606 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
609 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
610 Level, Index, EntriesRequested, PreferredMaximumLength,
611 ReturnedEntryCount, SortedBuffer);
613 if(!NETAPI_IsLocalComputer(ServerName))
615 FIXME("Only implemented on local computer, but requested for "
616 "remote server %s\n", debugstr_w(ServerName));
617 return ERROR_ACCESS_DENIED;
625 PNET_DISPLAY_USER inf;
626 /* current available strings buffer */
628 PNET_DISPLAY_USER admin, guest;
629 DWORD admin_size, guest_size;
633 /* sizes of the field buffers in WCHARS */
634 int name_sz, comment_sz, full_name_sz;
636 /* number of the records, returned in SortedBuffer
637 3 - for current user, Administrator and Guest users
641 FIXME("Level %d partially implemented\n", Level);
642 *ReturnedEntryCount = records;
648 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &name);
649 if (!GetUserNameW(name, &dwSize))
651 NetApiBufferFree(name);
652 return ERROR_ACCESS_DENIED;
655 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
656 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
659 dwSize = sizeof(NET_DISPLAY_USER) * records;
660 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
662 NetApiBufferAllocate(dwSize +
663 admin_size - sizeof(NET_DISPLAY_USER) +
664 guest_size - sizeof(NET_DISPLAY_USER),
667 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
668 inf->usri1_name = str;
670 ((PBYTE) str) + name_sz * sizeof(WCHAR));
671 inf->usri1_comment = str;
673 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
674 inf->usri1_full_name = str;
676 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
679 lstrcpyW(inf->usri1_name, name);
680 NetApiBufferFree(name);
681 inf->usri1_comment[0] = 0;
683 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
684 inf->usri1_full_name[0] = 0;
685 inf->usri1_user_id = 0;
686 inf->usri1_next_index = 0;
689 ACCESS_CopyDisplayUser(admin, &str, inf);
690 NetApiBufferFree(admin);
693 ACCESS_CopyDisplayUser(guest, &str, inf);
694 NetApiBufferFree(guest);
701 FIXME("Level %d is not implemented\n", Level);
706 TRACE("Invalid level %d is specified\n", Level);
707 return ERROR_INVALID_LEVEL;
712 /************************************************************
713 * NetGetDCName (NETAPI32.@)
715 * Return the name of the primary domain controller (PDC)
718 NET_API_STATUS WINAPI
719 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
721 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
722 debugstr_w(domainname), bufptr);
723 return NERR_DCNotFound; /* say we can't find a domain controller */
726 /************************************************************
727 * NetGroupEnum (NETAPI32.@)
730 NET_API_STATUS WINAPI
731 NetGroupEnum(LPCWSTR servername, DWORD level, LPBYTE *bufptr, DWORD prefmaxlen,
732 LPDWORD entriesread, LPDWORD totalentries, LPDWORD resume_handle)
734 FIXME("(%s, %d, %p, %d, %p, %p, %p) stub!\n", debugstr_w(servername),
735 level, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
736 return ERROR_ACCESS_DENIED;
739 /************************************************************
740 * NetGroupGetInfo (NETAPI32.@)
743 NET_API_STATUS WINAPI NetGroupGetInfo(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE *bufptr)
745 FIXME("(%s, %s, %d, %p) stub!\n", debugstr_w(servername), debugstr_w(groupname), level, bufptr);
746 return ERROR_ACCESS_DENIED;
749 /******************************************************************************
750 * NetUserModalsGet (NETAPI32.@)
752 * Retrieves global information for all users and global groups in the security
756 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
757 * on which the function is to execute.
758 * level [I] Information level of the data.
759 * 0 Return global passwords parameters. bufptr points to a
760 * USER_MODALS_INFO_0 struct.
761 * 1 Return logon server and domain controller information. bufptr
762 * points to a USER_MODALS_INFO_1 struct.
763 * 2 Return domain name and identifier. bufptr points to a
764 * USER_MODALS_INFO_2 struct.
765 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
767 * pbuffer [I] Buffer that receives the data.
770 * Success: NERR_Success.
772 * ERROR_ACCESS_DENIED - the user does not have access to the info.
773 * NERR_InvalidComputer - computer name is invalid.
775 NET_API_STATUS WINAPI NetUserModalsGet(
776 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
778 TRACE("(%s %d %p)\n", debugstr_w(szServer), level, pbuffer);
783 /* return global passwords parameters */
784 FIXME("level 0 not implemented!\n");
786 return NERR_InternalError;
788 /* return logon server and domain controller info */
789 FIXME("level 1 not implemented!\n");
791 return NERR_InternalError;
794 /* return domain name and identifier */
795 PUSER_MODALS_INFO_2 umi;
796 LSA_HANDLE policyHandle;
797 LSA_OBJECT_ATTRIBUTES objectAttributes;
798 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
800 PSID domainIdentifier = NULL;
803 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
804 objectAttributes.Length = sizeof(objectAttributes);
806 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
807 POLICY_VIEW_LOCAL_INFORMATION,
809 if (ntStatus != STATUS_SUCCESS)
811 WARN("LsaOpenPolicy failed with NT status %x\n",
812 LsaNtStatusToWinError(ntStatus));
816 ntStatus = LsaQueryInformationPolicy(policyHandle,
817 PolicyAccountDomainInformation,
818 (PVOID *)&domainInfo);
819 if (ntStatus != STATUS_SUCCESS)
821 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
822 LsaNtStatusToWinError(ntStatus));
823 LsaClose(policyHandle);
827 domainIdentifier = domainInfo->DomainSid;
828 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
829 LsaClose(policyHandle);
831 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
832 GetLengthSid(domainIdentifier) +
833 domainNameLen * sizeof(WCHAR),
836 if (ntStatus != NERR_Success)
838 WARN("NetApiBufferAllocate() failed\n");
839 LsaFreeMemory(domainInfo);
843 umi = (USER_MODALS_INFO_2 *) *pbuffer;
844 umi->usrmod2_domain_id = *pbuffer + sizeof(USER_MODALS_INFO_2);
845 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
846 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
848 lstrcpynW(umi->usrmod2_domain_name,
849 domainInfo->DomainName.Buffer,
851 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
854 LsaFreeMemory(domainInfo);
859 /* return lockout information */
860 FIXME("level 3 not implemented!\n");
862 return NERR_InternalError;
864 TRACE("Invalid level %d is specified\n", level);
866 return ERROR_INVALID_LEVEL;
872 static int fork_smbpasswd( char * const argv[] )
877 if (pipe( pipe_out ) == -1) return -1;
878 fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
879 fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
884 close( pipe_out[0] );
885 close( pipe_out[1] );
888 dup2( pipe_out[0], 0 );
889 close( pipe_out[0] );
890 close( pipe_out[1] );
891 execvp( "smbpasswd", argv );
892 ERR( "can't execute smbpasswd, is it installed?\n" );
895 close( pipe_out[0] );
900 ERR( "no fork support on this platform\n" );
905 static char *strdup_unixcp( const WCHAR *str )
908 int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
909 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
910 WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
914 static NET_API_STATUS change_password_smb( LPCWSTR domainname, LPCWSTR username,
915 LPCWSTR oldpassword, LPCWSTR newpassword )
917 static char option_silent[] = "-s";
918 static char option_user[] = "-U";
919 static char option_remote[] = "-r";
920 static char smbpasswd[] = "smbpasswd";
922 char *server = NULL, *user, *argv[7], *old, *new;
924 if (domainname && !(server = strdup_unixcp( domainname ))) return ERROR_OUTOFMEMORY;
925 if (!(user = strdup_unixcp( username )))
927 HeapFree( GetProcessHeap(), 0, server );
928 return ERROR_OUTOFMEMORY;
931 argv[1] = option_silent;
932 argv[2] = option_user;
936 argv[4] = option_remote;
942 pipe_out = fork_smbpasswd( argv );
943 HeapFree( GetProcessHeap(), 0, server );
944 HeapFree( GetProcessHeap(), 0, user );
945 if (pipe_out == -1) return NERR_InternalError;
947 if (!(old = strdup_unixcp( oldpassword )))
950 return ERROR_OUTOFMEMORY;
952 if (!(new = strdup_unixcp( newpassword )))
955 HeapFree( GetProcessHeap(), 0, old );
956 return ERROR_OUTOFMEMORY;
958 write( pipe_out, old, strlen( old ) );
959 write( pipe_out, "\n", 1 );
960 write( pipe_out, new, strlen( new ) );
961 write( pipe_out, "\n", 1 );
962 write( pipe_out, new, strlen( new ) );
963 write( pipe_out, "\n", 1 );
966 HeapFree( GetProcessHeap(), 0, old );
967 HeapFree( GetProcessHeap(), 0, new );
971 /******************************************************************************
972 * NetUserChangePassword (NETAPI32.@)
974 * domainname [I] Optional. Domain on which the user resides or the logon
975 * domain of the current user if NULL.
976 * username [I] Optional. Username to change the password for or the name
977 * of the current user if NULL.
978 * oldpassword [I] The user's current password.
979 * newpassword [I] The password that the user will be changed to using.
982 * Success: NERR_Success.
983 * Failure: NERR_* failure code or win error code.
986 NET_API_STATUS WINAPI NetUserChangePassword(LPCWSTR domainname, LPCWSTR username,
987 LPCWSTR oldpassword, LPCWSTR newpassword)
989 struct sam_user *user;
991 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname), debugstr_w(username));
993 if (!change_password_smb( domainname, username, oldpassword, newpassword ))
997 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname));
999 if((user = NETAPI_FindUser(username)) == NULL)
1000 return NERR_UserNotFound;
1002 if(lstrcmpW(user->user_password, oldpassword) != 0)
1003 return ERROR_INVALID_PASSWORD;
1005 if(lstrlenW(newpassword) > PWLEN)
1006 return ERROR_PASSWORD_RESTRICTION;
1008 lstrcpyW(user->user_password, newpassword);
1010 return NERR_Success;
1013 NET_API_STATUS WINAPI NetUseAdd(LMSTR servername, DWORD level, LPBYTE bufptr, LPDWORD parm_err)
1015 FIXME("%s %d %p %p stub\n", debugstr_w(servername), level, bufptr, parm_err);
1016 return NERR_Success;
1019 NET_API_STATUS WINAPI NetUseDel(LMSTR servername, LMSTR usename, DWORD forcecond)
1021 FIXME("%s %s %d stub\n", debugstr_w(servername), debugstr_w(usename), forcecond);
1022 return NERR_Success;