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);
297 return NERR_InternalError;
300 ERR("Invalid level %ld is specified\n", level);
301 return ERROR_INVALID_LEVEL;
306 /************************************************************
307 * NetUserGetLocalGroups (NETAPI32.@)
309 NET_API_STATUS WINAPI
310 NetUserGetLocalGroups(LPCWSTR servername, LPCWSTR username, DWORD level,
311 DWORD flags, LPBYTE* bufptr, DWORD prefmaxlen,
312 LPDWORD entriesread, LPDWORD totalentries)
314 FIXME("(%s, %s, %ld, %08lx, %p %ld, %p, %p) stub!\n",
315 debugstr_w(servername), debugstr_w(username), level, flags, bufptr,
316 prefmaxlen, entriesread, totalentries);
317 return NERR_InternalError;
320 /************************************************************
321 * NetUserEnum (NETAPI32.@)
323 NET_API_STATUS WINAPI
324 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
325 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
326 LPDWORD resume_handle)
328 FIXME("(%s,%ld, 0x%ld,%p,%ld,%p,%p,%p) stub!\n", debugstr_w(servername), level,
329 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
331 return ERROR_ACCESS_DENIED;
334 /************************************************************
335 * ACCESS_QueryAdminDisplayInformation
337 * Creates a buffer with information for the Admin User
339 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
341 static const WCHAR sAdminUserName[] = {
342 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
344 /* sizes of the field buffers in WCHARS */
345 int name_sz, comment_sz, full_name_sz;
346 PNET_DISPLAY_USER usr;
349 name_sz = lstrlenW(sAdminUserName);
353 *pdwSize = sizeof(NET_DISPLAY_USER);
354 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
355 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
358 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
359 usr->usri1_comment = (LPWSTR) (
360 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
361 usr->usri1_full_name = (LPWSTR) (
362 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
365 lstrcpyW(usr->usri1_name, sAdminUserName);
366 usr->usri1_comment[0] = 0;
367 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
368 usr->usri1_full_name[0] = 0;
369 usr->usri1_user_id = 500;
370 usr->usri1_next_index = 0;
373 /************************************************************
374 * ACCESS_QueryGuestDisplayInformation
376 * Creates a buffer with information for the Guest User
378 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
380 static const WCHAR sGuestUserName[] = {
381 'G','u','e','s','t',0 };
383 /* sizes of the field buffers in WCHARS */
384 int name_sz, comment_sz, full_name_sz;
385 PNET_DISPLAY_USER usr;
388 name_sz = lstrlenW(sGuestUserName);
392 *pdwSize = sizeof(NET_DISPLAY_USER);
393 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
394 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
397 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
398 usr->usri1_comment = (LPWSTR) (
399 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
400 usr->usri1_full_name = (LPWSTR) (
401 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
404 lstrcpyW(usr->usri1_name, sGuestUserName);
405 usr->usri1_comment[0] = 0;
406 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
407 UF_DONT_EXPIRE_PASSWD;
408 usr->usri1_full_name[0] = 0;
409 usr->usri1_user_id = 500;
410 usr->usri1_next_index = 0;
413 /************************************************************
414 * NetQueryDisplayInformation (NETAPI32.@)
415 * Copies NET_DISPLAY_USER record.
417 static void ACCESS_CopyDisplayUser(PNET_DISPLAY_USER dest, LPWSTR *dest_buf,
418 PNET_DISPLAY_USER src)
420 LPWSTR str = *dest_buf;
422 src->usri1_name = str;
423 lstrcpyW(src->usri1_name, dest->usri1_name);
425 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
427 src->usri1_comment = str;
428 lstrcpyW(src->usri1_comment, dest->usri1_comment);
430 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
432 src->usri1_flags = dest->usri1_flags;
434 src->usri1_full_name = str;
435 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
437 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
439 src->usri1_user_id = dest->usri1_user_id;
440 src->usri1_next_index = dest->usri1_next_index;
444 /************************************************************
445 * NetQueryDisplayInformation (NETAPI32.@)
447 * The buffer structure:
448 * - array of fixed size record of the level type
449 * - strings, referenced by the record of the level type
451 NET_API_STATUS WINAPI
452 NetQueryDisplayInformation(
453 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
454 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
457 TRACE("(%s, %ld, %ld, %ld, %ld, %p, %p)\n", debugstr_w(ServerName),
458 Level, Index, EntriesRequested, PreferredMaximumLength,
459 ReturnedEntryCount, SortedBuffer);
460 NETAPI_ForceLocalComputer(ServerName, ERROR_ACCESS_DENIED);
466 PNET_DISPLAY_USER inf;
467 /* current available strings buffer */
469 PNET_DISPLAY_USER admin, guest;
470 DWORD admin_size, guest_size;
474 /* sizes of the field buffers in WCHARS */
475 int name_sz, comment_sz, full_name_sz;
477 /* number of the records, returned in SortedBuffer
478 3 - for current user, Administrator and Guest users
482 FIXME("Level %ld partially implemented\n", Level);
483 *ReturnedEntryCount = records;
489 NetApiBufferAllocate(dwSize, (LPVOID *) &name);
490 if (!GetUserNameW(name, &dwSize))
492 NetApiBufferFree(name);
493 return ERROR_ACCESS_DENIED;
496 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
497 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
500 dwSize = sizeof(NET_DISPLAY_USER) * records;
501 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
503 NetApiBufferAllocate(dwSize +
504 admin_size - sizeof(NET_DISPLAY_USER) +
505 guest_size - sizeof(NET_DISPLAY_USER),
506 (LPVOID *) SortedBuffer);
507 inf = (PNET_DISPLAY_USER) *SortedBuffer;
508 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
509 inf->usri1_name = str;
511 ((PBYTE) str) + name_sz * sizeof(WCHAR));
512 inf->usri1_comment = str;
514 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
515 inf->usri1_full_name = str;
517 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
520 lstrcpyW(inf->usri1_name, name);
521 NetApiBufferFree(name);
522 inf->usri1_comment[0] = 0;
524 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
525 inf->usri1_full_name[0] = 0;
526 inf->usri1_user_id = 0;
527 inf->usri1_next_index = 0;
530 ACCESS_CopyDisplayUser(admin, &str, inf);
531 NetApiBufferFree(admin);
534 ACCESS_CopyDisplayUser(guest, &str, inf);
535 NetApiBufferFree(guest);
542 FIXME("Level %ld is not implemented\n", Level);
547 ERR("Invalid level %ld is specified\n", Level);
548 return ERROR_INVALID_LEVEL;
553 /************************************************************
554 * NetGetDCName (NETAPI32.@)
556 * Return the name of the primary domain controller (PDC)
559 NET_API_STATUS WINAPI
560 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
562 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
563 debugstr_w(domainname), bufptr);
564 return NERR_DCNotFound; /* say we can't find a domain controller */
568 /******************************************************************************
569 * NetUserModalsGet (NETAPI32.@)
571 * Retrieves global information for all users and global groups in the security
575 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
576 * on which the function is to execute.
577 * level [I] Information level of the data.
578 * 0 Return global passwords parameters. bufptr points to a
579 * USER_MODALS_INFO_0 struct.
580 * 1 Return logon server and domain controller information. bufptr
581 * points to a USER_MODALS_INFO_1 struct.
582 * 2 Return domain name and identifier. bufptr points to a
583 * USER_MODALS_INFO_2 struct.
584 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
586 * pbuffer [I] Buffer that receives the data.
589 * Success: NERR_Success.
591 * ERROR_ACCESS_DENIED - the user does not have access to the info.
592 * NERR_InvalidComputer - computer name is invalid.
594 NET_API_STATUS WINAPI NetUserModalsGet(
595 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
597 TRACE("(%s %ld %p)\n", debugstr_w(szServer), level, pbuffer);
602 /* return global passwords parameters */
603 FIXME("level 0 not implemented!\n");
605 return NERR_InternalError;
607 /* return logon server and domain controller info */
608 FIXME("level 1 not implemented!\n");
610 return NERR_InternalError;
613 /* return domain name and identifier */
614 PUSER_MODALS_INFO_2 umi;
615 LSA_HANDLE policyHandle;
616 LSA_OBJECT_ATTRIBUTES objectAttributes;
617 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
619 PSID domainIdentifier = NULL;
622 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
623 objectAttributes.Length = sizeof(objectAttributes);
625 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
626 POLICY_VIEW_LOCAL_INFORMATION,
628 if (ntStatus != STATUS_SUCCESS)
630 WARN("LsaOpenPolicy failed with NT status %lx\n",
631 LsaNtStatusToWinError(ntStatus));
635 ntStatus = LsaQueryInformationPolicy(policyHandle,
636 PolicyAccountDomainInformation,
637 (PVOID *)&domainInfo);
638 if (ntStatus != STATUS_SUCCESS)
640 WARN("LsaQueryInformationPolicy failed with NT status %lx\n",
641 LsaNtStatusToWinError(ntStatus));
642 LsaClose(policyHandle);
646 domainIdentifier = domainInfo->DomainSid;
647 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
648 LsaClose(policyHandle);
650 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
651 GetLengthSid(domainIdentifier) +
652 domainNameLen * sizeof(WCHAR),
655 if (ntStatus != NERR_Success)
657 WARN("NetApiBufferAllocate() failed\n");
658 LsaFreeMemory(domainInfo);
662 umi = (USER_MODALS_INFO_2 *) *pbuffer;
663 umi->usrmod2_domain_id = (PSID)(*pbuffer +
664 sizeof(USER_MODALS_INFO_2));
665 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
666 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
668 lstrcpynW(umi->usrmod2_domain_name,
669 domainInfo->DomainName.Buffer,
671 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
674 LsaFreeMemory(domainInfo);
679 /* return lockout information */
680 FIXME("level 3 not implemented!\n");
682 return NERR_InternalError;
684 WARN("Invalid level %ld is specified\n", level);
686 return ERROR_INVALID_LEVEL;