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 "netapi32_misc.h"
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(netapi32
);
40 static const WCHAR sAdminUserName
[] = {'A','d','m','i','n','i','s','t','r','a','t',
42 static const WCHAR sGuestUserName
[] = {'G','u','e','s','t',0};
44 /************************************************************
45 * NETAPI_ValidateServername
47 * Validates server name
49 static NET_API_STATUS
NETAPI_ValidateServername(LPCWSTR ServerName
)
53 if (ServerName
[0] == 0)
54 return ERROR_BAD_NETPATH
;
56 ((ServerName
[0] == '\\') &&
57 (ServerName
[1] != '\\'))
59 ((ServerName
[0] == '\\') &&
60 (ServerName
[1] == '\\') &&
63 return ERROR_INVALID_NAME
;
68 /************************************************************
71 * Checks whether the user name indicates current user.
73 static BOOL
NETAPI_IsKnownUser(LPCWSTR UserName
)
75 DWORD dwSize
= UNLEN
+ 1;
79 if (!lstrcmpW(UserName
, sAdminUserName
) ||
80 !lstrcmpW(UserName
, sGuestUserName
))
82 NetApiBufferAllocate(dwSize
* sizeof(WCHAR
), (LPVOID
*) &buf
);
83 Result
= GetUserNameW(buf
, &dwSize
);
85 Result
= Result
&& !lstrcmpW(UserName
, buf
);
86 NetApiBufferFree(buf
);
91 #define NETAPI_ForceKnownUser(UserName, FailureCode) \
92 if (!NETAPI_IsKnownUser(UserName)) \
94 FIXME("Can't find information for user %s\n", \
95 debugstr_w(UserName)); \
99 /************************************************************
100 * NetUserAdd (NETAPI32.@)
102 NET_API_STATUS WINAPI
NetUserAdd(LPCWSTR servername
,
103 DWORD level
, LPBYTE bufptr
, LPDWORD parm_err
)
105 NET_API_STATUS status
;
106 FIXME("(%s, %d, %p, %p) stub!\n", debugstr_w(servername
), level
, bufptr
, parm_err
);
108 status
= NETAPI_ValidateServername(servername
);
109 if (status
!= NERR_Success
)
112 if ((bufptr
!= NULL
) && (level
> 0) && (level
<= 4))
114 PUSER_INFO_1 ui
= (PUSER_INFO_1
) bufptr
;
115 TRACE("usri%d_name: %s\n", level
, debugstr_w(ui
->usri1_name
));
116 TRACE("usri%d_password: %s\n", level
, debugstr_w(ui
->usri1_password
));
117 TRACE("usri%d_comment: %s\n", level
, debugstr_w(ui
->usri1_comment
));
122 /************************************************************
123 * NetUserDel (NETAPI32.@)
125 NET_API_STATUS WINAPI
NetUserDel(LPCWSTR servername
, LPCWSTR username
)
127 NET_API_STATUS status
;
128 FIXME("(%s, %s) stub!\n", debugstr_w(servername
), debugstr_w(username
));
130 status
= NETAPI_ValidateServername(servername
);
131 if (status
!= NERR_Success
)
134 if (!NETAPI_IsKnownUser(username
))
135 return NERR_UserNotFound
;
137 /* Delete the user here */
141 /************************************************************
142 * NetUserGetInfo (NETAPI32.@)
144 NET_API_STATUS WINAPI
145 NetUserGetInfo(LPCWSTR servername
, LPCWSTR username
, DWORD level
,
148 NET_API_STATUS status
;
149 TRACE("(%s, %s, %d, %p)\n", debugstr_w(servername
), debugstr_w(username
),
151 status
= NETAPI_ValidateServername(servername
);
152 if (status
!= NERR_Success
)
154 NETAPI_ForceLocalComputer(servername
, NERR_InvalidComputer
);
155 NETAPI_ForceKnownUser(username
, NERR_UserNotFound
);
164 name_sz
= lstrlenW(username
) + 1;
167 NetApiBufferAllocate(sizeof(USER_INFO_0
) + name_sz
* sizeof(WCHAR
),
170 ui
= (PUSER_INFO_0
) *bufptr
;
171 ui
->usri0_name
= (LPWSTR
) (*bufptr
+ sizeof(USER_INFO_0
));
174 lstrcpyW(ui
->usri0_name
, username
);
182 NET_API_STATUS status
;
183 /* sizes of the field buffers in WCHARS */
184 int name_sz
, comment_sz
, usr_comment_sz
, full_name_sz
;
191 status
= NetUserGetInfo(servername
, username
, 0, (LPBYTE
*) &ui0
);
192 if (status
!= NERR_Success
)
194 NetApiBufferFree(ui0
);
197 name_sz
= lstrlenW(ui0
->usri0_name
) + 1;
200 NetApiBufferAllocate(sizeof(USER_INFO_10
) +
201 (name_sz
+ comment_sz
+ usr_comment_sz
+
202 full_name_sz
) * sizeof(WCHAR
),
204 ui
= (PUSER_INFO_10
) *bufptr
;
205 ui
->usri10_name
= (LPWSTR
) (*bufptr
+ sizeof(USER_INFO_10
));
206 ui
->usri10_comment
= (LPWSTR
) (
207 ((PBYTE
) ui
->usri10_name
) + name_sz
* sizeof(WCHAR
));
208 ui
->usri10_usr_comment
= (LPWSTR
) (
209 ((PBYTE
) ui
->usri10_comment
) + comment_sz
* sizeof(WCHAR
));
210 ui
->usri10_full_name
= (LPWSTR
) (
211 ((PBYTE
) ui
->usri10_usr_comment
) + usr_comment_sz
* sizeof(WCHAR
));
214 lstrcpyW(ui
->usri10_name
, ui0
->usri0_name
);
215 NetApiBufferFree(ui0
);
216 ui
->usri10_comment
[0] = 0;
217 ui
->usri10_usr_comment
[0] = 0;
218 ui
->usri10_full_name
[0] = 0;
224 static const WCHAR homedirW
[] = {'H','O','M','E',0};
227 NET_API_STATUS status
;
228 /* sizes of the field buffers in WCHARS */
229 int name_sz
, password_sz
, home_dir_sz
, comment_sz
, script_path_sz
;
231 password_sz
= 1; /* not filled out for security reasons for NetUserGetInfo*/
236 status
= NetUserGetInfo(servername
, username
, 0, (LPBYTE
*) &ui0
);
237 if (status
!= NERR_Success
)
239 NetApiBufferFree(ui0
);
242 name_sz
= lstrlenW(ui0
->usri0_name
) + 1;
243 home_dir_sz
= GetEnvironmentVariableW(homedirW
, NULL
,0);
245 NetApiBufferAllocate(sizeof(USER_INFO_1
) +
246 (name_sz
+ password_sz
+ home_dir_sz
+
247 comment_sz
+ script_path_sz
) * sizeof(WCHAR
),
250 ui
= (PUSER_INFO_1
) *bufptr
;
251 ui
->usri1_name
= (LPWSTR
) (ui
+ 1);
252 ui
->usri1_password
= ui
->usri1_name
+ name_sz
;
253 ui
->usri1_home_dir
= ui
->usri1_password
+ password_sz
;
254 ui
->usri1_comment
= ui
->usri1_home_dir
+ home_dir_sz
;
255 ui
->usri1_script_path
= ui
->usri1_comment
+ comment_sz
;
257 lstrcpyW(ui
->usri1_name
, ui0
->usri0_name
);
258 NetApiBufferFree(ui0
);
259 ui
->usri1_password
[0] = 0;
260 ui
->usri1_password_age
= 0;
262 GetEnvironmentVariableW(homedirW
, ui
->usri1_home_dir
,home_dir_sz
);
263 ui
->usri1_comment
[0] = 0;
265 ui
->usri1_script_path
[0] = 0;
295 FIXME("Level %d is not implemented\n", level
);
296 return NERR_InternalError
;
299 ERR("Invalid level %d is specified\n", level
);
300 return ERROR_INVALID_LEVEL
;
305 /************************************************************
306 * NetUserGetLocalGroups (NETAPI32.@)
308 NET_API_STATUS WINAPI
309 NetUserGetLocalGroups(LPCWSTR servername
, LPCWSTR username
, DWORD level
,
310 DWORD flags
, LPBYTE
* bufptr
, DWORD prefmaxlen
,
311 LPDWORD entriesread
, LPDWORD totalentries
)
313 NET_API_STATUS status
;
315 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
316 debugstr_w(servername
), debugstr_w(username
), level
, flags
, bufptr
,
317 prefmaxlen
, entriesread
, totalentries
);
319 status
= NETAPI_ValidateServername(servername
);
320 if (status
!= NERR_Success
)
323 if (!NETAPI_IsKnownUser(username
))
324 return NERR_UserNotFound
;
326 if (bufptr
) *bufptr
= NULL
;
327 if (entriesread
) *entriesread
= 0;
328 if (totalentries
) *totalentries
= 0;
333 /************************************************************
334 * NetUserEnum (NETAPI32.@)
336 NET_API_STATUS WINAPI
337 NetUserEnum(LPCWSTR servername
, DWORD level
, DWORD filter
, LPBYTE
* bufptr
,
338 DWORD prefmaxlen
, LPDWORD entriesread
, LPDWORD totalentries
,
339 LPDWORD resume_handle
)
341 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername
), level
,
342 filter
, bufptr
, prefmaxlen
, entriesread
, totalentries
, resume_handle
);
344 return ERROR_ACCESS_DENIED
;
347 /************************************************************
348 * ACCESS_QueryAdminDisplayInformation
350 * Creates a buffer with information for the Admin User
352 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER
*buf
, PDWORD pdwSize
)
354 static const WCHAR sAdminUserName
[] = {
355 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
357 /* sizes of the field buffers in WCHARS */
358 int name_sz
, comment_sz
, full_name_sz
;
359 PNET_DISPLAY_USER usr
;
362 name_sz
= lstrlenW(sAdminUserName
);
366 *pdwSize
= sizeof(NET_DISPLAY_USER
);
367 *pdwSize
+= (name_sz
+ comment_sz
+ full_name_sz
) * sizeof(WCHAR
);
368 NetApiBufferAllocate(*pdwSize
, (LPVOID
*) buf
);
371 usr
->usri1_name
= (LPWSTR
) ((PBYTE
) usr
+ sizeof(NET_DISPLAY_USER
));
372 usr
->usri1_comment
= (LPWSTR
) (
373 ((PBYTE
) usr
->usri1_name
) + name_sz
* sizeof(WCHAR
));
374 usr
->usri1_full_name
= (LPWSTR
) (
375 ((PBYTE
) usr
->usri1_comment
) + comment_sz
* sizeof(WCHAR
));
378 lstrcpyW(usr
->usri1_name
, sAdminUserName
);
379 usr
->usri1_comment
[0] = 0;
380 usr
->usri1_flags
= UF_SCRIPT
| UF_NORMAL_ACCOUNT
| UF_DONT_EXPIRE_PASSWD
;
381 usr
->usri1_full_name
[0] = 0;
382 usr
->usri1_user_id
= 500;
383 usr
->usri1_next_index
= 0;
386 /************************************************************
387 * ACCESS_QueryGuestDisplayInformation
389 * Creates a buffer with information for the Guest User
391 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER
*buf
, PDWORD pdwSize
)
393 static const WCHAR sGuestUserName
[] = {
394 'G','u','e','s','t',0 };
396 /* sizes of the field buffers in WCHARS */
397 int name_sz
, comment_sz
, full_name_sz
;
398 PNET_DISPLAY_USER usr
;
401 name_sz
= lstrlenW(sGuestUserName
);
405 *pdwSize
= sizeof(NET_DISPLAY_USER
);
406 *pdwSize
+= (name_sz
+ comment_sz
+ full_name_sz
) * sizeof(WCHAR
);
407 NetApiBufferAllocate(*pdwSize
, (LPVOID
*) buf
);
410 usr
->usri1_name
= (LPWSTR
) ((PBYTE
) usr
+ sizeof(NET_DISPLAY_USER
));
411 usr
->usri1_comment
= (LPWSTR
) (
412 ((PBYTE
) usr
->usri1_name
) + name_sz
* sizeof(WCHAR
));
413 usr
->usri1_full_name
= (LPWSTR
) (
414 ((PBYTE
) usr
->usri1_comment
) + comment_sz
* sizeof(WCHAR
));
417 lstrcpyW(usr
->usri1_name
, sGuestUserName
);
418 usr
->usri1_comment
[0] = 0;
419 usr
->usri1_flags
= UF_ACCOUNTDISABLE
| UF_SCRIPT
| UF_NORMAL_ACCOUNT
|
420 UF_DONT_EXPIRE_PASSWD
;
421 usr
->usri1_full_name
[0] = 0;
422 usr
->usri1_user_id
= 500;
423 usr
->usri1_next_index
= 0;
426 /************************************************************
427 * NetQueryDisplayInformation (NETAPI32.@)
428 * Copies NET_DISPLAY_USER record.
430 static void ACCESS_CopyDisplayUser(PNET_DISPLAY_USER dest
, LPWSTR
*dest_buf
,
431 PNET_DISPLAY_USER src
)
433 LPWSTR str
= *dest_buf
;
435 src
->usri1_name
= str
;
436 lstrcpyW(src
->usri1_name
, dest
->usri1_name
);
438 ((PBYTE
) str
) + (lstrlenW(str
) + 1) * sizeof(WCHAR
));
440 src
->usri1_comment
= str
;
441 lstrcpyW(src
->usri1_comment
, dest
->usri1_comment
);
443 ((PBYTE
) str
) + (lstrlenW(str
) + 1) * sizeof(WCHAR
));
445 src
->usri1_flags
= dest
->usri1_flags
;
447 src
->usri1_full_name
= str
;
448 lstrcpyW(src
->usri1_full_name
, dest
->usri1_full_name
);
450 ((PBYTE
) str
) + (lstrlenW(str
) + 1) * sizeof(WCHAR
));
452 src
->usri1_user_id
= dest
->usri1_user_id
;
453 src
->usri1_next_index
= dest
->usri1_next_index
;
457 /************************************************************
458 * NetQueryDisplayInformation (NETAPI32.@)
460 * The buffer structure:
461 * - array of fixed size record of the level type
462 * - strings, referenced by the record of the level type
464 NET_API_STATUS WINAPI
465 NetQueryDisplayInformation(
466 LPCWSTR ServerName
, DWORD Level
, DWORD Index
, DWORD EntriesRequested
,
467 DWORD PreferredMaximumLength
, LPDWORD ReturnedEntryCount
,
470 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName
),
471 Level
, Index
, EntriesRequested
, PreferredMaximumLength
,
472 ReturnedEntryCount
, SortedBuffer
);
473 NETAPI_ForceLocalComputer(ServerName
, ERROR_ACCESS_DENIED
);
479 PNET_DISPLAY_USER inf
;
480 /* current available strings buffer */
482 PNET_DISPLAY_USER admin
, guest
;
483 DWORD admin_size
, guest_size
;
487 /* sizes of the field buffers in WCHARS */
488 int name_sz
, comment_sz
, full_name_sz
;
490 /* number of the records, returned in SortedBuffer
491 3 - for current user, Administrator and Guest users
495 FIXME("Level %d partially implemented\n", Level
);
496 *ReturnedEntryCount
= records
;
502 NetApiBufferAllocate(dwSize
, (LPVOID
*) &name
);
503 if (!GetUserNameW(name
, &dwSize
))
505 NetApiBufferFree(name
);
506 return ERROR_ACCESS_DENIED
;
509 ACCESS_QueryAdminDisplayInformation(&admin
, &admin_size
);
510 ACCESS_QueryGuestDisplayInformation(&guest
, &guest_size
);
513 dwSize
= sizeof(NET_DISPLAY_USER
) * records
;
514 dwSize
+= (name_sz
+ comment_sz
+ full_name_sz
) * sizeof(WCHAR
);
516 NetApiBufferAllocate(dwSize
+
517 admin_size
- sizeof(NET_DISPLAY_USER
) +
518 guest_size
- sizeof(NET_DISPLAY_USER
),
519 (LPVOID
*) SortedBuffer
);
520 inf
= (PNET_DISPLAY_USER
) *SortedBuffer
;
521 str
= (LPWSTR
) ((PBYTE
) inf
+ sizeof(NET_DISPLAY_USER
) * records
);
522 inf
->usri1_name
= str
;
524 ((PBYTE
) str
) + name_sz
* sizeof(WCHAR
));
525 inf
->usri1_comment
= str
;
527 ((PBYTE
) str
) + comment_sz
* sizeof(WCHAR
));
528 inf
->usri1_full_name
= str
;
530 ((PBYTE
) str
) + full_name_sz
* sizeof(WCHAR
));
533 lstrcpyW(inf
->usri1_name
, name
);
534 NetApiBufferFree(name
);
535 inf
->usri1_comment
[0] = 0;
537 UF_SCRIPT
| UF_NORMAL_ACCOUNT
| UF_DONT_EXPIRE_PASSWD
;
538 inf
->usri1_full_name
[0] = 0;
539 inf
->usri1_user_id
= 0;
540 inf
->usri1_next_index
= 0;
543 ACCESS_CopyDisplayUser(admin
, &str
, inf
);
544 NetApiBufferFree(admin
);
547 ACCESS_CopyDisplayUser(guest
, &str
, inf
);
548 NetApiBufferFree(guest
);
555 FIXME("Level %d is not implemented\n", Level
);
560 ERR("Invalid level %d is specified\n", Level
);
561 return ERROR_INVALID_LEVEL
;
566 /************************************************************
567 * NetGetDCName (NETAPI32.@)
569 * Return the name of the primary domain controller (PDC)
572 NET_API_STATUS WINAPI
573 NetGetDCName(LPCWSTR servername
, LPCWSTR domainname
, LPBYTE
*bufptr
)
575 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername
),
576 debugstr_w(domainname
), bufptr
);
577 return NERR_DCNotFound
; /* say we can't find a domain controller */
581 /******************************************************************************
582 * NetUserModalsGet (NETAPI32.@)
584 * Retrieves global information for all users and global groups in the security
588 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
589 * on which the function is to execute.
590 * level [I] Information level of the data.
591 * 0 Return global passwords parameters. bufptr points to a
592 * USER_MODALS_INFO_0 struct.
593 * 1 Return logon server and domain controller information. bufptr
594 * points to a USER_MODALS_INFO_1 struct.
595 * 2 Return domain name and identifier. bufptr points to a
596 * USER_MODALS_INFO_2 struct.
597 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
599 * pbuffer [I] Buffer that receives the data.
602 * Success: NERR_Success.
604 * ERROR_ACCESS_DENIED - the user does not have access to the info.
605 * NERR_InvalidComputer - computer name is invalid.
607 NET_API_STATUS WINAPI
NetUserModalsGet(
608 LPCWSTR szServer
, DWORD level
, LPBYTE
*pbuffer
)
610 TRACE("(%s %d %p)\n", debugstr_w(szServer
), level
, pbuffer
);
615 /* return global passwords parameters */
616 FIXME("level 0 not implemented!\n");
618 return NERR_InternalError
;
620 /* return logon server and domain controller info */
621 FIXME("level 1 not implemented!\n");
623 return NERR_InternalError
;
626 /* return domain name and identifier */
627 PUSER_MODALS_INFO_2 umi
;
628 LSA_HANDLE policyHandle
;
629 LSA_OBJECT_ATTRIBUTES objectAttributes
;
630 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo
;
632 PSID domainIdentifier
= NULL
;
635 ZeroMemory(&objectAttributes
, sizeof(objectAttributes
));
636 objectAttributes
.Length
= sizeof(objectAttributes
);
638 ntStatus
= LsaOpenPolicy(NULL
, &objectAttributes
,
639 POLICY_VIEW_LOCAL_INFORMATION
,
641 if (ntStatus
!= STATUS_SUCCESS
)
643 WARN("LsaOpenPolicy failed with NT status %x\n",
644 LsaNtStatusToWinError(ntStatus
));
648 ntStatus
= LsaQueryInformationPolicy(policyHandle
,
649 PolicyAccountDomainInformation
,
650 (PVOID
*)&domainInfo
);
651 if (ntStatus
!= STATUS_SUCCESS
)
653 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
654 LsaNtStatusToWinError(ntStatus
));
655 LsaClose(policyHandle
);
659 domainIdentifier
= domainInfo
->DomainSid
;
660 domainNameLen
= lstrlenW(domainInfo
->DomainName
.Buffer
) + 1;
661 LsaClose(policyHandle
);
663 ntStatus
= NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2
) +
664 GetLengthSid(domainIdentifier
) +
665 domainNameLen
* sizeof(WCHAR
),
668 if (ntStatus
!= NERR_Success
)
670 WARN("NetApiBufferAllocate() failed\n");
671 LsaFreeMemory(domainInfo
);
675 umi
= (USER_MODALS_INFO_2
*) *pbuffer
;
676 umi
->usrmod2_domain_id
= (PSID
)(*pbuffer
+
677 sizeof(USER_MODALS_INFO_2
));
678 umi
->usrmod2_domain_name
= (LPWSTR
)(*pbuffer
+
679 sizeof(USER_MODALS_INFO_2
) + GetLengthSid(domainIdentifier
));
681 lstrcpynW(umi
->usrmod2_domain_name
,
682 domainInfo
->DomainName
.Buffer
,
684 CopySid(GetLengthSid(domainIdentifier
), umi
->usrmod2_domain_id
,
687 LsaFreeMemory(domainInfo
);
692 /* return lockout information */
693 FIXME("level 3 not implemented!\n");
695 return NERR_InternalError
;
697 WARN("Invalid level %d is specified\n", level
);
699 return ERROR_INVALID_LEVEL
;
705 /******************************************************************************
706 * NetUserChangePassword (NETAPI32.@)
708 * domainname [I] Optional. Domain on which the user resides or the logon
709 * domain of the current user if NULL.
710 * username [I] Optional. Username to change the password for or the name
711 * of the current user if NULL.
712 * oldpassword [I] The user's current password.
713 * newpassword [I] The password that the user will be changed to using.
716 * Success: NERR_Success.
717 * Failure: NERR_* failure code or win error code.
720 NET_API_STATUS WINAPI
NetUserChangePassword(LPCWSTR domainname
, LPCWSTR username
,
721 LPCWSTR oldpassword
, LPCWSTR newpassword
)
723 FIXME("(%s, %s, ..., ...)\n", debugstr_w(domainname
), debugstr_w(username
));
724 return NERR_InternalError
;