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 if (!GetUserNameW(currentuser
, &size
)) {
441 NetApiBufferFree(currentuser
);
442 return ERROR_NOT_ENOUGH_MEMORY
;
445 if (lstrcmpiW(username
, currentuser
) && NETAPI_FindUser(username
))
447 NetApiBufferFree(currentuser
);
448 return NERR_UserNotFound
;
451 NetApiBufferFree(currentuser
);
453 size
= sizeof(*info
) + sizeof(admins
);
455 if(prefmaxlen
< size
)
456 status
= ERROR_MORE_DATA
;
458 status
= NetApiBufferAllocate(size
, (LPVOID
*)&info
);
460 if(status
!= NERR_Success
)
467 info
->lgrui0_name
= (LPWSTR
)((LPBYTE
)info
+ sizeof(*info
));
468 lstrcpyW(info
->lgrui0_name
, admins
);
470 *bufptr
= (LPBYTE
)info
;
476 /************************************************************
477 * NetUserEnum (NETAPI32.@)
479 NET_API_STATUS WINAPI
480 NetUserEnum(LPCWSTR servername
, DWORD level
, DWORD filter
, LPBYTE
* bufptr
,
481 DWORD prefmaxlen
, LPDWORD entriesread
, LPDWORD totalentries
,
482 LPDWORD resume_handle
)
484 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername
), level
,
485 filter
, bufptr
, prefmaxlen
, entriesread
, totalentries
, resume_handle
);
487 return ERROR_ACCESS_DENIED
;
490 /************************************************************
491 * ACCESS_QueryAdminDisplayInformation
493 * Creates a buffer with information for the Admin User
495 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER
*buf
, PDWORD pdwSize
)
497 static const WCHAR sAdminUserName
[] = {
498 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
500 /* sizes of the field buffers in WCHARS */
501 int name_sz
, comment_sz
, full_name_sz
;
502 PNET_DISPLAY_USER usr
;
505 name_sz
= lstrlenW(sAdminUserName
) + 1;
509 *pdwSize
= sizeof(NET_DISPLAY_USER
);
510 *pdwSize
+= (name_sz
+ comment_sz
+ full_name_sz
) * sizeof(WCHAR
);
511 NetApiBufferAllocate(*pdwSize
, (LPVOID
*) buf
);
514 usr
->usri1_name
= (LPWSTR
) ((PBYTE
) usr
+ sizeof(NET_DISPLAY_USER
));
515 usr
->usri1_comment
= (LPWSTR
) (
516 ((PBYTE
) usr
->usri1_name
) + name_sz
* sizeof(WCHAR
));
517 usr
->usri1_full_name
= (LPWSTR
) (
518 ((PBYTE
) usr
->usri1_comment
) + comment_sz
* sizeof(WCHAR
));
521 lstrcpyW(usr
->usri1_name
, sAdminUserName
);
522 usr
->usri1_comment
[0] = 0;
523 usr
->usri1_flags
= UF_SCRIPT
| UF_NORMAL_ACCOUNT
| UF_DONT_EXPIRE_PASSWD
;
524 usr
->usri1_full_name
[0] = 0;
525 usr
->usri1_user_id
= DOMAIN_USER_RID_ADMIN
;
526 usr
->usri1_next_index
= 0;
529 /************************************************************
530 * ACCESS_QueryGuestDisplayInformation
532 * Creates a buffer with information for the Guest User
534 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER
*buf
, PDWORD pdwSize
)
536 static const WCHAR sGuestUserName
[] = {
537 'G','u','e','s','t',0 };
539 /* sizes of the field buffers in WCHARS */
540 int name_sz
, comment_sz
, full_name_sz
;
541 PNET_DISPLAY_USER usr
;
544 name_sz
= lstrlenW(sGuestUserName
) + 1;
548 *pdwSize
= sizeof(NET_DISPLAY_USER
);
549 *pdwSize
+= (name_sz
+ comment_sz
+ full_name_sz
) * sizeof(WCHAR
);
550 NetApiBufferAllocate(*pdwSize
, (LPVOID
*) buf
);
553 usr
->usri1_name
= (LPWSTR
) ((PBYTE
) usr
+ sizeof(NET_DISPLAY_USER
));
554 usr
->usri1_comment
= (LPWSTR
) (
555 ((PBYTE
) usr
->usri1_name
) + name_sz
* sizeof(WCHAR
));
556 usr
->usri1_full_name
= (LPWSTR
) (
557 ((PBYTE
) usr
->usri1_comment
) + comment_sz
* sizeof(WCHAR
));
560 lstrcpyW(usr
->usri1_name
, sGuestUserName
);
561 usr
->usri1_comment
[0] = 0;
562 usr
->usri1_flags
= UF_ACCOUNTDISABLE
| UF_SCRIPT
| UF_NORMAL_ACCOUNT
|
563 UF_DONT_EXPIRE_PASSWD
;
564 usr
->usri1_full_name
[0] = 0;
565 usr
->usri1_user_id
= DOMAIN_USER_RID_GUEST
;
566 usr
->usri1_next_index
= 0;
569 /************************************************************
570 * Copies NET_DISPLAY_USER record.
572 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER
*dest
, LPWSTR
*dest_buf
,
573 PNET_DISPLAY_USER src
)
575 LPWSTR str
= *dest_buf
;
577 src
->usri1_name
= str
;
578 lstrcpyW(src
->usri1_name
, dest
->usri1_name
);
580 ((PBYTE
) str
) + (lstrlenW(str
) + 1) * sizeof(WCHAR
));
582 src
->usri1_comment
= str
;
583 lstrcpyW(src
->usri1_comment
, dest
->usri1_comment
);
585 ((PBYTE
) str
) + (lstrlenW(str
) + 1) * sizeof(WCHAR
));
587 src
->usri1_flags
= dest
->usri1_flags
;
589 src
->usri1_full_name
= str
;
590 lstrcpyW(src
->usri1_full_name
, dest
->usri1_full_name
);
592 ((PBYTE
) str
) + (lstrlenW(str
) + 1) * sizeof(WCHAR
));
594 src
->usri1_user_id
= dest
->usri1_user_id
;
595 src
->usri1_next_index
= dest
->usri1_next_index
;
599 /************************************************************
600 * NetQueryDisplayInformation (NETAPI32.@)
602 * The buffer structure:
603 * - array of fixed size record of the level type
604 * - strings, referenced by the record of the level type
606 NET_API_STATUS WINAPI
607 NetQueryDisplayInformation(
608 LPCWSTR ServerName
, DWORD Level
, DWORD Index
, DWORD EntriesRequested
,
609 DWORD PreferredMaximumLength
, LPDWORD ReturnedEntryCount
,
612 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName
),
613 Level
, Index
, EntriesRequested
, PreferredMaximumLength
,
614 ReturnedEntryCount
, SortedBuffer
);
616 if(!NETAPI_IsLocalComputer(ServerName
))
618 FIXME("Only implemented on local computer, but requested for "
619 "remote server %s\n", debugstr_w(ServerName
));
620 return ERROR_ACCESS_DENIED
;
628 PNET_DISPLAY_USER inf
;
629 /* current available strings buffer */
631 PNET_DISPLAY_USER admin
, guest
;
632 DWORD admin_size
, guest_size
;
636 /* sizes of the field buffers in WCHARS */
637 int name_sz
, comment_sz
, full_name_sz
;
639 /* number of the records, returned in SortedBuffer
640 3 - for current user, Administrator and Guest users
644 FIXME("Level %d partially implemented\n", Level
);
645 *ReturnedEntryCount
= records
;
651 NetApiBufferAllocate(dwSize
* sizeof(WCHAR
), (LPVOID
*) &name
);
652 if (!GetUserNameW(name
, &dwSize
))
654 NetApiBufferFree(name
);
655 return ERROR_ACCESS_DENIED
;
658 ACCESS_QueryAdminDisplayInformation(&admin
, &admin_size
);
659 ACCESS_QueryGuestDisplayInformation(&guest
, &guest_size
);
662 dwSize
= sizeof(NET_DISPLAY_USER
) * records
;
663 dwSize
+= (name_sz
+ comment_sz
+ full_name_sz
) * sizeof(WCHAR
);
665 NetApiBufferAllocate(dwSize
+
666 admin_size
- sizeof(NET_DISPLAY_USER
) +
667 guest_size
- sizeof(NET_DISPLAY_USER
),
670 str
= (LPWSTR
) ((PBYTE
) inf
+ sizeof(NET_DISPLAY_USER
) * records
);
671 inf
->usri1_name
= str
;
673 ((PBYTE
) str
) + name_sz
* sizeof(WCHAR
));
674 inf
->usri1_comment
= str
;
676 ((PBYTE
) str
) + comment_sz
* sizeof(WCHAR
));
677 inf
->usri1_full_name
= str
;
679 ((PBYTE
) str
) + full_name_sz
* sizeof(WCHAR
));
682 lstrcpyW(inf
->usri1_name
, name
);
683 NetApiBufferFree(name
);
684 inf
->usri1_comment
[0] = 0;
686 UF_SCRIPT
| UF_NORMAL_ACCOUNT
| UF_DONT_EXPIRE_PASSWD
;
687 inf
->usri1_full_name
[0] = 0;
688 inf
->usri1_user_id
= 0;
689 inf
->usri1_next_index
= 0;
692 ACCESS_CopyDisplayUser(admin
, &str
, inf
);
693 NetApiBufferFree(admin
);
696 ACCESS_CopyDisplayUser(guest
, &str
, inf
);
697 NetApiBufferFree(guest
);
704 FIXME("Level %d is not implemented\n", Level
);
709 TRACE("Invalid level %d is specified\n", Level
);
710 return ERROR_INVALID_LEVEL
;
715 /************************************************************
716 * NetGetDCName (NETAPI32.@)
718 * Return the name of the primary domain controller (PDC)
721 NET_API_STATUS WINAPI
722 NetGetDCName(LPCWSTR servername
, LPCWSTR domainname
, LPBYTE
*bufptr
)
724 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername
),
725 debugstr_w(domainname
), bufptr
);
726 return NERR_DCNotFound
; /* say we can't find a domain controller */
729 /************************************************************
730 * NetGroupEnum (NETAPI32.@)
733 NET_API_STATUS WINAPI
734 NetGroupEnum(LPCWSTR servername
, DWORD level
, LPBYTE
*bufptr
, DWORD prefmaxlen
,
735 LPDWORD entriesread
, LPDWORD totalentries
, LPDWORD resume_handle
)
737 FIXME("(%s, %d, %p, %d, %p, %p, %p) stub!\n", debugstr_w(servername
),
738 level
, bufptr
, prefmaxlen
, entriesread
, totalentries
, resume_handle
);
739 return ERROR_ACCESS_DENIED
;
742 /************************************************************
743 * NetGroupGetInfo (NETAPI32.@)
746 NET_API_STATUS WINAPI
NetGroupGetInfo(LPCWSTR servername
, LPCWSTR groupname
, DWORD level
, LPBYTE
*bufptr
)
748 FIXME("(%s, %s, %d, %p) stub!\n", debugstr_w(servername
), debugstr_w(groupname
), level
, bufptr
);
749 return ERROR_ACCESS_DENIED
;
752 /******************************************************************************
753 * NetUserModalsGet (NETAPI32.@)
755 * Retrieves global information for all users and global groups in the security
759 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
760 * on which the function is to execute.
761 * level [I] Information level of the data.
762 * 0 Return global passwords parameters. bufptr points to a
763 * USER_MODALS_INFO_0 struct.
764 * 1 Return logon server and domain controller information. bufptr
765 * points to a USER_MODALS_INFO_1 struct.
766 * 2 Return domain name and identifier. bufptr points to a
767 * USER_MODALS_INFO_2 struct.
768 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
770 * pbuffer [I] Buffer that receives the data.
773 * Success: NERR_Success.
775 * ERROR_ACCESS_DENIED - the user does not have access to the info.
776 * NERR_InvalidComputer - computer name is invalid.
778 NET_API_STATUS WINAPI
NetUserModalsGet(
779 LPCWSTR szServer
, DWORD level
, LPBYTE
*pbuffer
)
781 TRACE("(%s %d %p)\n", debugstr_w(szServer
), level
, pbuffer
);
786 /* return global passwords parameters */
787 FIXME("level 0 not implemented!\n");
789 return NERR_InternalError
;
791 /* return logon server and domain controller info */
792 FIXME("level 1 not implemented!\n");
794 return NERR_InternalError
;
797 /* return domain name and identifier */
798 PUSER_MODALS_INFO_2 umi
;
799 LSA_HANDLE policyHandle
;
800 LSA_OBJECT_ATTRIBUTES objectAttributes
;
801 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo
;
803 PSID domainIdentifier
= NULL
;
806 ZeroMemory(&objectAttributes
, sizeof(objectAttributes
));
807 objectAttributes
.Length
= sizeof(objectAttributes
);
809 ntStatus
= LsaOpenPolicy(NULL
, &objectAttributes
,
810 POLICY_VIEW_LOCAL_INFORMATION
,
812 if (ntStatus
!= STATUS_SUCCESS
)
814 WARN("LsaOpenPolicy failed with NT status %x\n",
815 LsaNtStatusToWinError(ntStatus
));
819 ntStatus
= LsaQueryInformationPolicy(policyHandle
,
820 PolicyAccountDomainInformation
,
821 (PVOID
*)&domainInfo
);
822 if (ntStatus
!= STATUS_SUCCESS
)
824 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
825 LsaNtStatusToWinError(ntStatus
));
826 LsaClose(policyHandle
);
830 domainIdentifier
= domainInfo
->DomainSid
;
831 domainNameLen
= lstrlenW(domainInfo
->DomainName
.Buffer
) + 1;
832 LsaClose(policyHandle
);
834 ntStatus
= NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2
) +
835 GetLengthSid(domainIdentifier
) +
836 domainNameLen
* sizeof(WCHAR
),
839 if (ntStatus
!= NERR_Success
)
841 WARN("NetApiBufferAllocate() failed\n");
842 LsaFreeMemory(domainInfo
);
846 umi
= (USER_MODALS_INFO_2
*) *pbuffer
;
847 umi
->usrmod2_domain_id
= *pbuffer
+ sizeof(USER_MODALS_INFO_2
);
848 umi
->usrmod2_domain_name
= (LPWSTR
)(*pbuffer
+
849 sizeof(USER_MODALS_INFO_2
) + GetLengthSid(domainIdentifier
));
851 lstrcpynW(umi
->usrmod2_domain_name
,
852 domainInfo
->DomainName
.Buffer
,
854 CopySid(GetLengthSid(domainIdentifier
), umi
->usrmod2_domain_id
,
857 LsaFreeMemory(domainInfo
);
862 /* return lockout information */
863 FIXME("level 3 not implemented!\n");
865 return NERR_InternalError
;
867 TRACE("Invalid level %d is specified\n", level
);
869 return ERROR_INVALID_LEVEL
;
875 static int fork_smbpasswd( char * const argv
[] )
880 if (pipe( pipe_out
) == -1) return -1;
881 fcntl( pipe_out
[0], F_SETFD
, FD_CLOEXEC
);
882 fcntl( pipe_out
[1], F_SETFD
, FD_CLOEXEC
);
887 close( pipe_out
[0] );
888 close( pipe_out
[1] );
891 dup2( pipe_out
[0], 0 );
892 close( pipe_out
[0] );
893 close( pipe_out
[1] );
894 execvp( "smbpasswd", argv
);
895 ERR( "can't execute smbpasswd, is it installed?\n" );
898 close( pipe_out
[0] );
903 ERR( "no fork support on this platform\n" );
908 static char *strdup_unixcp( const WCHAR
*str
)
911 int len
= WideCharToMultiByte( CP_UNIXCP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
912 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
913 WideCharToMultiByte( CP_UNIXCP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
917 static NET_API_STATUS
change_password_smb( LPCWSTR domainname
, LPCWSTR username
,
918 LPCWSTR oldpassword
, LPCWSTR newpassword
)
920 static char option_silent
[] = "-s";
921 static char option_user
[] = "-U";
922 static char option_remote
[] = "-r";
923 static char smbpasswd
[] = "smbpasswd";
925 char *server
= NULL
, *user
, *argv
[7], *old
, *new;
927 if (domainname
&& !(server
= strdup_unixcp( domainname
))) return ERROR_OUTOFMEMORY
;
928 if (!(user
= strdup_unixcp( username
)))
930 HeapFree( GetProcessHeap(), 0, server
);
931 return ERROR_OUTOFMEMORY
;
934 argv
[1] = option_silent
;
935 argv
[2] = option_user
;
939 argv
[4] = option_remote
;
945 pipe_out
= fork_smbpasswd( argv
);
946 HeapFree( GetProcessHeap(), 0, server
);
947 HeapFree( GetProcessHeap(), 0, user
);
948 if (pipe_out
== -1) return NERR_InternalError
;
950 if (!(old
= strdup_unixcp( oldpassword
)))
953 return ERROR_OUTOFMEMORY
;
955 if (!(new = strdup_unixcp( newpassword
)))
958 HeapFree( GetProcessHeap(), 0, old
);
959 return ERROR_OUTOFMEMORY
;
961 write( pipe_out
, old
, strlen( old
) );
962 write( pipe_out
, "\n", 1 );
963 write( pipe_out
, new, strlen( new ) );
964 write( pipe_out
, "\n", 1 );
965 write( pipe_out
, new, strlen( new ) );
966 write( pipe_out
, "\n", 1 );
969 HeapFree( GetProcessHeap(), 0, old
);
970 HeapFree( GetProcessHeap(), 0, new );
974 /******************************************************************************
975 * NetUserChangePassword (NETAPI32.@)
977 * domainname [I] Optional. Domain on which the user resides or the logon
978 * domain of the current user if NULL.
979 * username [I] Optional. Username to change the password for or the name
980 * of the current user if NULL.
981 * oldpassword [I] The user's current password.
982 * newpassword [I] The password that the user will be changed to using.
985 * Success: NERR_Success.
986 * Failure: NERR_* failure code or win error code.
989 NET_API_STATUS WINAPI
NetUserChangePassword(LPCWSTR domainname
, LPCWSTR username
,
990 LPCWSTR oldpassword
, LPCWSTR newpassword
)
992 struct sam_user
*user
;
994 TRACE("(%s, %s, ..., ...)\n", debugstr_w(domainname
), debugstr_w(username
));
996 if (!change_password_smb( domainname
, username
, oldpassword
, newpassword
))
1000 FIXME("Ignoring domainname %s.\n", debugstr_w(domainname
));
1002 if((user
= NETAPI_FindUser(username
)) == NULL
)
1003 return NERR_UserNotFound
;
1005 if(lstrcmpW(user
->user_password
, oldpassword
) != 0)
1006 return ERROR_INVALID_PASSWORD
;
1008 if(lstrlenW(newpassword
) > PWLEN
)
1009 return ERROR_PASSWORD_RESTRICTION
;
1011 lstrcpyW(user
->user_password
, newpassword
);
1013 return NERR_Success
;
1016 NET_API_STATUS WINAPI
NetUseAdd(LMSTR servername
, DWORD level
, LPBYTE bufptr
, LPDWORD parm_err
)
1018 FIXME("%s %d %p %p stub\n", debugstr_w(servername
), level
, bufptr
, parm_err
);
1019 return NERR_Success
;
1022 NET_API_STATUS WINAPI
NetUseDel(LMSTR servername
, LMSTR usename
, DWORD forcecond
)
1024 FIXME("%s %s %d stub\n", debugstr_w(servername
), debugstr_w(usename
), forcecond
);
1025 return NERR_Success
;