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 FIXME("(%s, %s, %d, %08x, %p %d, %p, %p) stub!\n",
314 debugstr_w(servername), debugstr_w(username), level, flags, bufptr,
315 prefmaxlen, entriesread, totalentries);
316 return NERR_InternalError;
319 /************************************************************
320 * NetUserEnum (NETAPI32.@)
322 NET_API_STATUS WINAPI
323 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
324 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
325 LPDWORD resume_handle)
327 FIXME("(%s,%d, 0x%d,%p,%d,%p,%p,%p) stub!\n", debugstr_w(servername), level,
328 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
330 return ERROR_ACCESS_DENIED;
333 /************************************************************
334 * ACCESS_QueryAdminDisplayInformation
336 * Creates a buffer with information for the Admin User
338 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
340 static const WCHAR sAdminUserName[] = {
341 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
343 /* sizes of the field buffers in WCHARS */
344 int name_sz, comment_sz, full_name_sz;
345 PNET_DISPLAY_USER usr;
348 name_sz = lstrlenW(sAdminUserName);
352 *pdwSize = sizeof(NET_DISPLAY_USER);
353 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
354 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
357 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
358 usr->usri1_comment = (LPWSTR) (
359 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
360 usr->usri1_full_name = (LPWSTR) (
361 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
364 lstrcpyW(usr->usri1_name, sAdminUserName);
365 usr->usri1_comment[0] = 0;
366 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
367 usr->usri1_full_name[0] = 0;
368 usr->usri1_user_id = 500;
369 usr->usri1_next_index = 0;
372 /************************************************************
373 * ACCESS_QueryGuestDisplayInformation
375 * Creates a buffer with information for the Guest User
377 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
379 static const WCHAR sGuestUserName[] = {
380 'G','u','e','s','t',0 };
382 /* sizes of the field buffers in WCHARS */
383 int name_sz, comment_sz, full_name_sz;
384 PNET_DISPLAY_USER usr;
387 name_sz = lstrlenW(sGuestUserName);
391 *pdwSize = sizeof(NET_DISPLAY_USER);
392 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
393 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
396 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
397 usr->usri1_comment = (LPWSTR) (
398 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
399 usr->usri1_full_name = (LPWSTR) (
400 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
403 lstrcpyW(usr->usri1_name, sGuestUserName);
404 usr->usri1_comment[0] = 0;
405 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
406 UF_DONT_EXPIRE_PASSWD;
407 usr->usri1_full_name[0] = 0;
408 usr->usri1_user_id = 500;
409 usr->usri1_next_index = 0;
412 /************************************************************
413 * NetQueryDisplayInformation (NETAPI32.@)
414 * Copies NET_DISPLAY_USER record.
416 static void ACCESS_CopyDisplayUser(PNET_DISPLAY_USER dest, LPWSTR *dest_buf,
417 PNET_DISPLAY_USER src)
419 LPWSTR str = *dest_buf;
421 src->usri1_name = str;
422 lstrcpyW(src->usri1_name, dest->usri1_name);
424 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
426 src->usri1_comment = str;
427 lstrcpyW(src->usri1_comment, dest->usri1_comment);
429 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
431 src->usri1_flags = dest->usri1_flags;
433 src->usri1_full_name = str;
434 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
436 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
438 src->usri1_user_id = dest->usri1_user_id;
439 src->usri1_next_index = dest->usri1_next_index;
443 /************************************************************
444 * NetQueryDisplayInformation (NETAPI32.@)
446 * The buffer structure:
447 * - array of fixed size record of the level type
448 * - strings, referenced by the record of the level type
450 NET_API_STATUS WINAPI
451 NetQueryDisplayInformation(
452 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
453 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
456 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
457 Level, Index, EntriesRequested, PreferredMaximumLength,
458 ReturnedEntryCount, SortedBuffer);
459 NETAPI_ForceLocalComputer(ServerName, ERROR_ACCESS_DENIED);
465 PNET_DISPLAY_USER inf;
466 /* current available strings buffer */
468 PNET_DISPLAY_USER admin, guest;
469 DWORD admin_size, guest_size;
473 /* sizes of the field buffers in WCHARS */
474 int name_sz, comment_sz, full_name_sz;
476 /* number of the records, returned in SortedBuffer
477 3 - for current user, Administrator and Guest users
481 FIXME("Level %d partially implemented\n", Level);
482 *ReturnedEntryCount = records;
488 NetApiBufferAllocate(dwSize, (LPVOID *) &name);
489 if (!GetUserNameW(name, &dwSize))
491 NetApiBufferFree(name);
492 return ERROR_ACCESS_DENIED;
495 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
496 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
499 dwSize = sizeof(NET_DISPLAY_USER) * records;
500 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
502 NetApiBufferAllocate(dwSize +
503 admin_size - sizeof(NET_DISPLAY_USER) +
504 guest_size - sizeof(NET_DISPLAY_USER),
505 (LPVOID *) SortedBuffer);
506 inf = (PNET_DISPLAY_USER) *SortedBuffer;
507 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
508 inf->usri1_name = str;
510 ((PBYTE) str) + name_sz * sizeof(WCHAR));
511 inf->usri1_comment = str;
513 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
514 inf->usri1_full_name = str;
516 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
519 lstrcpyW(inf->usri1_name, name);
520 NetApiBufferFree(name);
521 inf->usri1_comment[0] = 0;
523 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
524 inf->usri1_full_name[0] = 0;
525 inf->usri1_user_id = 0;
526 inf->usri1_next_index = 0;
529 ACCESS_CopyDisplayUser(admin, &str, inf);
530 NetApiBufferFree(admin);
533 ACCESS_CopyDisplayUser(guest, &str, inf);
534 NetApiBufferFree(guest);
541 FIXME("Level %d is not implemented\n", Level);
546 ERR("Invalid level %d is specified\n", Level);
547 return ERROR_INVALID_LEVEL;
552 /************************************************************
553 * NetGetDCName (NETAPI32.@)
555 * Return the name of the primary domain controller (PDC)
558 NET_API_STATUS WINAPI
559 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
561 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
562 debugstr_w(domainname), bufptr);
563 return NERR_DCNotFound; /* say we can't find a domain controller */
567 /******************************************************************************
568 * NetUserModalsGet (NETAPI32.@)
570 * Retrieves global information for all users and global groups in the security
574 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
575 * on which the function is to execute.
576 * level [I] Information level of the data.
577 * 0 Return global passwords parameters. bufptr points to a
578 * USER_MODALS_INFO_0 struct.
579 * 1 Return logon server and domain controller information. bufptr
580 * points to a USER_MODALS_INFO_1 struct.
581 * 2 Return domain name and identifier. bufptr points to a
582 * USER_MODALS_INFO_2 struct.
583 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
585 * pbuffer [I] Buffer that receives the data.
588 * Success: NERR_Success.
590 * ERROR_ACCESS_DENIED - the user does not have access to the info.
591 * NERR_InvalidComputer - computer name is invalid.
593 NET_API_STATUS WINAPI NetUserModalsGet(
594 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
596 TRACE("(%s %d %p)\n", debugstr_w(szServer), level, pbuffer);
601 /* return global passwords parameters */
602 FIXME("level 0 not implemented!\n");
604 return NERR_InternalError;
606 /* return logon server and domain controller info */
607 FIXME("level 1 not implemented!\n");
609 return NERR_InternalError;
612 /* return domain name and identifier */
613 PUSER_MODALS_INFO_2 umi;
614 LSA_HANDLE policyHandle;
615 LSA_OBJECT_ATTRIBUTES objectAttributes;
616 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
618 PSID domainIdentifier = NULL;
621 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
622 objectAttributes.Length = sizeof(objectAttributes);
624 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
625 POLICY_VIEW_LOCAL_INFORMATION,
627 if (ntStatus != STATUS_SUCCESS)
629 WARN("LsaOpenPolicy failed with NT status %x\n",
630 LsaNtStatusToWinError(ntStatus));
634 ntStatus = LsaQueryInformationPolicy(policyHandle,
635 PolicyAccountDomainInformation,
636 (PVOID *)&domainInfo);
637 if (ntStatus != STATUS_SUCCESS)
639 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
640 LsaNtStatusToWinError(ntStatus));
641 LsaClose(policyHandle);
645 domainIdentifier = domainInfo->DomainSid;
646 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
647 LsaClose(policyHandle);
649 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
650 GetLengthSid(domainIdentifier) +
651 domainNameLen * sizeof(WCHAR),
654 if (ntStatus != NERR_Success)
656 WARN("NetApiBufferAllocate() failed\n");
657 LsaFreeMemory(domainInfo);
661 umi = (USER_MODALS_INFO_2 *) *pbuffer;
662 umi->usrmod2_domain_id = (PSID)(*pbuffer +
663 sizeof(USER_MODALS_INFO_2));
664 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
665 sizeof(USER_MODALS_INFO_2) + GetLengthSid(domainIdentifier));
667 lstrcpynW(umi->usrmod2_domain_name,
668 domainInfo->DomainName.Buffer,
670 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
673 LsaFreeMemory(domainInfo);
678 /* return lockout information */
679 FIXME("level 3 not implemented!\n");
681 return NERR_InternalError;
683 WARN("Invalid level %d is specified\n", level);
685 return ERROR_INVALID_LEVEL;