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
35 #include "netapi32_misc.h"
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
41 static const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
43 static const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
45 /************************************************************
46 * NETAPI_ValidateServername
48 * Validates server name
50 static NET_API_STATUS NETAPI_ValidateServername(LPCWSTR ServerName)
54 if (ServerName[0] == 0)
55 return ERROR_BAD_NETPATH;
57 ((ServerName[0] == '\\') &&
58 (ServerName[1] != '\\'))
60 ((ServerName[0] == '\\') &&
61 (ServerName[1] == '\\') &&
64 return ERROR_INVALID_NAME;
69 /************************************************************
72 * Checks whether the user name indicates current user.
74 static BOOL NETAPI_IsKnownUser(LPCWSTR UserName)
76 DWORD dwSize = UNLEN + 1;
80 if (!lstrcmpW(UserName, sAdminUserName) ||
81 !lstrcmpW(UserName, sGuestUserName))
83 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
84 Result = GetUserNameW(buf, &dwSize);
86 Result = Result && !lstrcmpW(UserName, buf);
87 NetApiBufferFree(buf);
92 #define NETAPI_ForceKnownUser(UserName, FailureCode) \
93 if (!NETAPI_IsKnownUser(UserName)) \
95 FIXME("Can't find information for user %s\n", \
96 debugstr_w(UserName)); \
100 /************************************************************
101 * NetUserAdd (NETAPI32.@)
103 NET_API_STATUS WINAPI NetUserAdd(LPCWSTR servername,
104 DWORD level, LPBYTE bufptr, LPDWORD parm_err)
106 NET_API_STATUS status;
107 FIXME("(%s, %ld, %p, %p) stub!\n", debugstr_w(servername), level, bufptr, parm_err);
109 status = NETAPI_ValidateServername(servername);
110 if (status != NERR_Success)
113 if ((bufptr != NULL) && (level > 0) && (level <= 4))
115 PUSER_INFO_1 ui = (PUSER_INFO_1) bufptr;
116 TRACE("usri%ld_name: %s\n", level, debugstr_w(ui->usri1_name));
117 TRACE("usri%ld_password: %s\n", level, debugstr_w(ui->usri1_password));
118 TRACE("usri%ld_comment: %s\n", level, debugstr_w(ui->usri1_comment));
123 /************************************************************
124 * NetUserDel (NETAPI32.@)
126 NET_API_STATUS WINAPI NetUserDel(LPCWSTR servername, LPCWSTR username)
128 NET_API_STATUS status;
129 FIXME("(%s, %s) stub!\n", debugstr_w(servername), debugstr_w(username));
131 status = NETAPI_ValidateServername(servername);
132 if (status != NERR_Success)
135 if (!NETAPI_IsKnownUser(username))
136 return NERR_UserNotFound;
138 /* Delete the user here */
142 /************************************************************
143 * NetUserGetInfo (NETAPI32.@)
145 NET_API_STATUS WINAPI
146 NetUserGetInfo(LPCWSTR servername, LPCWSTR username, DWORD level,
149 NET_API_STATUS status;
150 TRACE("(%s, %s, %ld, %p)\n", debugstr_w(servername), debugstr_w(username),
152 status = NETAPI_ValidateServername(servername);
153 if (status != NERR_Success)
155 NETAPI_ForceLocalComputer(servername, NERR_InvalidComputer);
156 NETAPI_ForceKnownUser(username, NERR_UserNotFound);
165 name_sz = lstrlenW(username) + 1;
168 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
171 ui = (PUSER_INFO_0) *bufptr;
172 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
175 lstrcpyW(ui->usri0_name, username);
183 NET_API_STATUS status;
184 /* sizes of the field buffers in WCHARS */
185 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
192 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
193 if (status != NERR_Success)
195 NetApiBufferFree(ui0);
198 name_sz = lstrlenW(ui0->usri0_name) + 1;
201 NetApiBufferAllocate(sizeof(USER_INFO_10) +
202 (name_sz + comment_sz + usr_comment_sz +
203 full_name_sz) * sizeof(WCHAR),
205 ui = (PUSER_INFO_10) *bufptr;
206 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
207 ui->usri10_comment = (LPWSTR) (
208 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
209 ui->usri10_usr_comment = (LPWSTR) (
210 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
211 ui->usri10_full_name = (LPWSTR) (
212 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
215 lstrcpyW(ui->usri10_name, ui0->usri0_name);
216 NetApiBufferFree(ui0);
217 ui->usri10_comment[0] = 0;
218 ui->usri10_usr_comment[0] = 0;
219 ui->usri10_full_name[0] = 0;
225 static const WCHAR homedirW[] = {'H','O','M','E',0};
228 NET_API_STATUS status;
229 /* sizes of the field buffers in WCHARS */
230 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
232 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
237 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
238 if (status != NERR_Success)
240 NetApiBufferFree(ui0);
243 name_sz = lstrlenW(ui0->usri0_name) + 1;
244 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
246 NetApiBufferAllocate(sizeof(USER_INFO_1) +
247 (name_sz + password_sz + home_dir_sz +
248 comment_sz + script_path_sz) * sizeof(WCHAR),
251 ui = (PUSER_INFO_1) *bufptr;
252 ui->usri1_name = (LPWSTR) (ui + 1);
253 ui->usri1_password = ui->usri1_name + name_sz;
254 ui->usri1_home_dir = ui->usri1_password + password_sz;
255 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
256 ui->usri1_script_path = ui->usri1_comment + comment_sz;
258 lstrcpyW(ui->usri1_name, ui0->usri0_name);
259 NetApiBufferFree(ui0);
260 ui->usri1_password[0] = 0;
261 ui->usri1_password_age = 0;
263 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
264 ui->usri1_comment[0] = 0;
266 ui->usri1_script_path[0] = 0;
296 FIXME("Level %ld is not implemented\n", level);
300 ERR("Invalid level %ld is specified\n", level);
301 return ERROR_INVALID_LEVEL;
308 /************************************************************
309 * NetUserEnum (NETAPI32.@)
311 NET_API_STATUS WINAPI
312 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
313 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
314 LPDWORD resume_handle)
316 FIXME("(%s,%ld, 0x%ld,%p,%ld,%p,%p,%p) stub!\n", debugstr_w(servername), level,
317 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
319 return ERROR_ACCESS_DENIED;
322 /************************************************************
323 * ACCESS_QueryAdminDisplayInformation
325 * Creates a buffer with information for the Admin User
327 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
329 static const WCHAR sAdminUserName[] = {
330 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
332 /* sizes of the field buffers in WCHARS */
333 int name_sz, comment_sz, full_name_sz;
334 PNET_DISPLAY_USER usr;
337 name_sz = lstrlenW(sAdminUserName);
341 *pdwSize = sizeof(NET_DISPLAY_USER);
342 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
343 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
346 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
347 usr->usri1_comment = (LPWSTR) (
348 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
349 usr->usri1_full_name = (LPWSTR) (
350 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
353 lstrcpyW(usr->usri1_name, sAdminUserName);
354 usr->usri1_comment[0] = 0;
355 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
356 usr->usri1_full_name[0] = 0;
357 usr->usri1_user_id = 500;
358 usr->usri1_next_index = 0;
361 /************************************************************
362 * ACCESS_QueryGuestDisplayInformation
364 * Creates a buffer with information for the Guest User
366 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
368 static const WCHAR sGuestUserName[] = {
369 'G','u','e','s','t',0 };
371 /* sizes of the field buffers in WCHARS */
372 int name_sz, comment_sz, full_name_sz;
373 PNET_DISPLAY_USER usr;
376 name_sz = lstrlenW(sGuestUserName);
380 *pdwSize = sizeof(NET_DISPLAY_USER);
381 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
382 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
385 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
386 usr->usri1_comment = (LPWSTR) (
387 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
388 usr->usri1_full_name = (LPWSTR) (
389 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
392 lstrcpyW(usr->usri1_name, sGuestUserName);
393 usr->usri1_comment[0] = 0;
394 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
395 UF_DONT_EXPIRE_PASSWD;
396 usr->usri1_full_name[0] = 0;
397 usr->usri1_user_id = 500;
398 usr->usri1_next_index = 0;
401 /************************************************************
402 * NetQueryDisplayInformation (NETAPI32.@)
403 * Copies NET_DISPLAY_USER record.
405 static void ACCESS_CopyDisplayUser(PNET_DISPLAY_USER dest, LPWSTR *dest_buf,
406 PNET_DISPLAY_USER src)
408 LPWSTR str = *dest_buf;
410 src->usri1_name = str;
411 lstrcpyW(src->usri1_name, dest->usri1_name);
413 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
415 src->usri1_comment = str;
416 lstrcpyW(src->usri1_comment, dest->usri1_comment);
418 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
420 src->usri1_flags = dest->usri1_flags;
422 src->usri1_full_name = str;
423 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
425 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
427 src->usri1_user_id = dest->usri1_user_id;
428 src->usri1_next_index = dest->usri1_next_index;
432 /************************************************************
433 * NetQueryDisplayInformation (NETAPI32.@)
435 * The buffer structure:
436 * - array of fixed size record of the level type
437 * - strings, referenced by the record of the level type
439 NET_API_STATUS WINAPI
440 NetQueryDisplayInformation(
441 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
442 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
445 TRACE("(%s, %ld, %ld, %ld, %ld, %p, %p)\n", debugstr_w(ServerName),
446 Level, Index, EntriesRequested, PreferredMaximumLength,
447 ReturnedEntryCount, SortedBuffer);
448 NETAPI_ForceLocalComputer(ServerName, ERROR_ACCESS_DENIED);
454 PNET_DISPLAY_USER inf;
455 /* current available strings buffer */
457 PNET_DISPLAY_USER admin, guest;
458 DWORD admin_size, guest_size;
462 /* sizes of the field buffers in WCHARS */
463 int name_sz, comment_sz, full_name_sz;
465 /* number of the records, returned in SortedBuffer
466 3 - for current user, Administrator and Guest users
470 FIXME("Level %ld partially implemented\n", Level);
471 *ReturnedEntryCount = records;
477 NetApiBufferAllocate(dwSize, (LPVOID *) &name);
478 if (!GetUserNameW(name, &dwSize))
480 NetApiBufferFree(name);
481 return ERROR_ACCESS_DENIED;
484 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
485 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
488 dwSize = sizeof(NET_DISPLAY_USER) * records;
489 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
491 NetApiBufferAllocate(dwSize +
492 admin_size - sizeof(NET_DISPLAY_USER) +
493 guest_size - sizeof(NET_DISPLAY_USER),
494 (LPVOID *) SortedBuffer);
495 inf = (PNET_DISPLAY_USER) *SortedBuffer;
496 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
497 inf->usri1_name = str;
499 ((PBYTE) str) + name_sz * sizeof(WCHAR));
500 inf->usri1_comment = str;
502 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
503 inf->usri1_full_name = str;
505 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
508 lstrcpyW(inf->usri1_name, name);
509 NetApiBufferFree(name);
510 inf->usri1_comment[0] = 0;
512 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
513 inf->usri1_full_name[0] = 0;
514 inf->usri1_user_id = 0;
515 inf->usri1_next_index = 0;
518 ACCESS_CopyDisplayUser(admin, &str, inf);
519 NetApiBufferFree(admin);
522 ACCESS_CopyDisplayUser(guest, &str, inf);
523 NetApiBufferFree(guest);
530 FIXME("Level %ld is not implemented\n", Level);
535 ERR("Invalid level %ld is specified\n", Level);
536 return ERROR_INVALID_LEVEL;
541 /************************************************************
542 * NetGetDCName (NETAPI32.@)
544 * Return the name of the primary domain controller (PDC)
547 NET_API_STATUS WINAPI
548 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
550 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
551 debugstr_w(domainname), bufptr);
552 return NERR_DCNotFound; /* say we can't find a domain controller */
556 /******************************************************************************
557 * NetUserModalsGet (NETAPI32.@)
559 * Retrieves global information for all users and global groups in the security
563 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
564 * on which the function is to execute.
565 * level [I] Information level of the data.
566 * 0 Return global passwords parameters. bufptr points to a
567 * USER_MODALS_INFO_0 struct.
568 * 1 Return logon server and domain controller information. bufptr
569 * points to a USER_MODALS_INFO_1 struct.
570 * 2 Return domain name and identifier. bufptr points to a
571 * USER_MODALS_INFO_2 struct.
572 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
574 * pbuffer [I] Buffer that receives the data.
577 * Success: NERR_Success.
579 * ERROR_ACCESS_DENIED - the user does not have access to the info.
580 * NERR_InvalidComputer - computer name is invalid.
582 NET_API_STATUS WINAPI NetUserModalsGet(
583 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
585 TRACE("(%s %ld %p)\n", debugstr_w(szServer), level, pbuffer);
590 /* return global passwords parameters */
591 FIXME("level 0 not implemented!\n");
593 return NERR_InternalError;
595 /* return logon server and domain controller info */
596 FIXME("level 1 not implemented!\n");
598 return NERR_InternalError;
601 /* return domain name and identifier */
602 PUSER_MODALS_INFO_2 umi;
603 LSA_HANDLE policyHandle;
604 LSA_OBJECT_ATTRIBUTES objectAttributes;
605 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
607 PSID domainIdentifier = NULL;
610 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
612 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
613 POLICY_VIEW_LOCAL_INFORMATION,
615 if (ntStatus != STATUS_SUCCESS)
617 WARN("LsaOpenPolicy failed with NT status %lx\n",
618 LsaNtStatusToWinError(ntStatus));
622 ntStatus = LsaQueryInformationPolicy(policyHandle,
623 PolicyAccountDomainInformation,
624 (PVOID *)&domainInfo);
625 if (ntStatus != STATUS_SUCCESS)
627 WARN("LsaQueryInformationPolicy failed with NT status %lx\n",
628 LsaNtStatusToWinError(ntStatus));
632 domainIdentifier = domainInfo->DomainSid;
633 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
634 LsaClose(policyHandle);
636 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
637 GetLengthSid(domainIdentifier) +
638 domainNameLen * sizeof(WCHAR),
641 if (ntStatus != NERR_Success)
643 WARN("NetApiBufferAllocate() failed\n");
644 LsaFreeMemory(domainInfo);
648 umi = (USER_MODALS_INFO_2 *) *pbuffer;
649 umi->usrmod2_domain_id = (PSID)(*pbuffer +
650 sizeof(USER_MODALS_INFO_2));
651 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
652 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
654 lstrcpynW(umi->usrmod2_domain_name,
655 domainInfo->DomainName.Buffer,
657 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
660 LsaFreeMemory(domainInfo);
665 /* return lockout information */
666 FIXME("level 3 not implemented!\n");
668 return NERR_InternalError;
670 WARN("Invalid level %ld is specified\n", level);
672 return ERROR_INVALID_LEVEL;
678 /************************************************************
679 * NetLocalGroupAdd (NETAPI32.@)
681 NET_API_STATUS WINAPI NetLocalGroupAdd(LPCWSTR servername, DWORD level,
682 LPBYTE buf, LPDWORD parm_err)
684 FIXME("(%s %ld %p %p) stub!\n", debugstr_w(servername), level, buf, parm_err);
688 /************************************************************
689 * NetLocalGroupSetMember (NETAPI32.@)
692 NET_API_STATUS WINAPI NetLocalGroupSetMembers(LPCWSTR servername,
693 LPCWSTR groupname, DWORD level, LPBYTE buf, DWORD totalentries)
695 FIXME("(%s %s %ld %p %ld) stub!\n", debugstr_w(servername),
696 debugstr_w(groupname),level, buf, totalentries);