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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "netapi32_misc.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
36 static const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
38 static const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
40 /************************************************************
41 * NETAPI_ValidateServername
43 * Validates server name
45 static NET_API_STATUS NETAPI_ValidateServername(LPCWSTR ServerName)
49 if (ServerName[0] == 0)
50 return ERROR_BAD_NETPATH;
52 ((ServerName[0] == '\\') &&
53 (ServerName[1] != '\\'))
55 ((ServerName[0] == '\\') &&
56 (ServerName[1] == '\\') &&
59 return ERROR_INVALID_NAME;
64 /************************************************************
67 * Checks whether the user name indicates current user.
69 static BOOL NETAPI_IsKnownUser(LPCWSTR UserName)
71 DWORD dwSize = UNLEN + 1;
75 if (!lstrcmpW(UserName, sAdminUserName) ||
76 !lstrcmpW(UserName, sGuestUserName))
78 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
79 Result = GetUserNameW(buf, &dwSize);
81 Result = Result && !lstrcmpW(UserName, buf);
82 NetApiBufferFree(buf);
87 #define NETAPI_ForceKnownUser(UserName, FailureCode) \
88 if (!NETAPI_IsKnownUser(UserName)) \
90 FIXME("Can't find information for user %s\n", \
91 debugstr_w(UserName)); \
95 /************************************************************
96 * NetUserAdd (NETAPI32.@)
98 NET_API_STATUS WINAPI NetUserAdd(LPCWSTR servername,
99 DWORD level, LPBYTE bufptr, LPDWORD parm_err)
101 NET_API_STATUS status;
102 FIXME("(%s, %ld, %p, %p) stub!\n", debugstr_w(servername), level, bufptr, parm_err);
104 status = NETAPI_ValidateServername(servername);
105 if (status != NERR_Success)
108 if ((bufptr != NULL) && (level > 0) && (level <= 4))
110 PUSER_INFO_1 ui = (PUSER_INFO_1) bufptr;
111 TRACE("usri%ld_name: %s\n", level, debugstr_w(ui->usri1_name));
112 TRACE("usri%ld_password: %s\n", level, debugstr_w(ui->usri1_password));
113 TRACE("usri%ld_comment: %s\n", level, debugstr_w(ui->usri1_comment));
118 /************************************************************
119 * NetUserDel (NETAPI32.@)
121 NET_API_STATUS WINAPI NetUserDel(LPCWSTR servername, LPCWSTR username)
123 NET_API_STATUS status;
124 FIXME("(%s, %s) stub!\n", debugstr_w(servername), debugstr_w(username));
126 status = NETAPI_ValidateServername(servername);
127 if (status != NERR_Success)
130 if (!NETAPI_IsKnownUser(username))
131 return NERR_UserNotFound;
133 /* Delete the user here */
137 /************************************************************
138 * NetUserGetInfo (NETAPI32.@)
140 NET_API_STATUS WINAPI
141 NetUserGetInfo(LPCWSTR servername, LPCWSTR username, DWORD level,
144 NET_API_STATUS status;
145 TRACE("(%s, %s, %ld, %p)\n", debugstr_w(servername), debugstr_w(username),
147 status = NETAPI_ValidateServername(servername);
148 if (status != NERR_Success)
150 NETAPI_ForceLocalComputer(servername, NERR_InvalidComputer);
151 NETAPI_ForceKnownUser(username, NERR_UserNotFound);
160 name_sz = lstrlenW(username) + 1;
163 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
166 ui = (PUSER_INFO_0) *bufptr;
167 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
170 lstrcpyW(ui->usri0_name, username);
178 NET_API_STATUS status;
179 /* sizes of the field buffers in WCHARS */
180 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
187 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
188 if (status != NERR_Success)
190 NetApiBufferFree(ui0);
193 name_sz = lstrlenW(ui0->usri0_name) + 1;
196 NetApiBufferAllocate(sizeof(USER_INFO_10) +
197 (name_sz + comment_sz + usr_comment_sz +
198 full_name_sz) * sizeof(WCHAR),
200 ui = (PUSER_INFO_10) *bufptr;
201 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
202 ui->usri10_comment = (LPWSTR) (
203 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
204 ui->usri10_usr_comment = (LPWSTR) (
205 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
206 ui->usri10_full_name = (LPWSTR) (
207 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
210 lstrcpyW(ui->usri10_name, ui0->usri0_name);
211 NetApiBufferFree(ui0);
212 ui->usri10_comment[0] = 0;
213 ui->usri10_usr_comment[0] = 0;
214 ui->usri10_full_name[0] = 0;
220 static const WCHAR homedirW[] = {'H','O','M','E',0};
223 NET_API_STATUS status;
224 /* sizes of the field buffers in WCHARS */
225 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
227 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
232 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
233 if (status != NERR_Success)
235 NetApiBufferFree(ui0);
238 name_sz = lstrlenW(ui0->usri0_name) + 1;
239 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
241 NetApiBufferAllocate(sizeof(USER_INFO_1) +
242 (name_sz + password_sz + home_dir_sz +
243 comment_sz + script_path_sz) * sizeof(WCHAR),
246 ui = (PUSER_INFO_1) *bufptr;
247 ui->usri1_name = (LPWSTR) (ui + 1);
248 ui->usri1_password = ui->usri1_name + name_sz;
249 ui->usri1_home_dir = ui->usri1_password + password_sz;
250 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
251 ui->usri1_script_path = ui->usri1_comment + comment_sz;
253 lstrcpyW(ui->usri1_name, ui0->usri0_name);
254 NetApiBufferFree(ui0);
255 ui->usri1_password[0] = 0;
256 ui->usri1_password_age = 0;
258 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
259 ui->usri1_comment[0] = 0;
261 ui->usri1_script_path[0] = 0;
291 FIXME("Level %ld is not implemented\n", level);
295 ERR("Invalid level %ld is specified\n", level);
296 return ERROR_INVALID_LEVEL;
303 /************************************************************
304 * NetUserEnum (NETAPI32.@)
306 NET_API_STATUS WINAPI
307 NetUserEnum(LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr,
308 DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries,
309 LPDWORD resume_handle)
311 FIXME("(%s,%ld, 0x%ld,%p,%ld,%p,%p,%p) stub!\n", debugstr_w(servername), level,
312 filter, bufptr, prefmaxlen, entriesread, totalentries, resume_handle);
314 return ERROR_ACCESS_DENIED;
317 /************************************************************
318 * ACCESS_QueryAdminDisplayInformation
320 * Creates a buffer with information for the Admin User
322 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
324 static const WCHAR sAdminUserName[] = {
325 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
327 /* sizes of the field buffers in WCHARS */
328 int name_sz, comment_sz, full_name_sz;
329 PNET_DISPLAY_USER usr;
332 name_sz = lstrlenW(sAdminUserName);
336 *pdwSize = sizeof(NET_DISPLAY_USER);
337 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
338 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
341 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
342 usr->usri1_comment = (LPWSTR) (
343 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
344 usr->usri1_full_name = (LPWSTR) (
345 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
348 lstrcpyW(usr->usri1_name, sAdminUserName);
349 usr->usri1_comment[0] = 0;
350 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
351 usr->usri1_full_name[0] = 0;
352 usr->usri1_user_id = 500;
353 usr->usri1_next_index = 0;
356 /************************************************************
357 * ACCESS_QueryGuestDisplayInformation
359 * Creates a buffer with information for the Guest User
361 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
363 static const WCHAR sGuestUserName[] = {
364 'G','u','e','s','t',0 };
366 /* sizes of the field buffers in WCHARS */
367 int name_sz, comment_sz, full_name_sz;
368 PNET_DISPLAY_USER usr;
371 name_sz = lstrlenW(sGuestUserName);
375 *pdwSize = sizeof(NET_DISPLAY_USER);
376 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
377 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
380 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
381 usr->usri1_comment = (LPWSTR) (
382 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
383 usr->usri1_full_name = (LPWSTR) (
384 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
387 lstrcpyW(usr->usri1_name, sGuestUserName);
388 usr->usri1_comment[0] = 0;
389 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
390 UF_DONT_EXPIRE_PASSWD;
391 usr->usri1_full_name[0] = 0;
392 usr->usri1_user_id = 500;
393 usr->usri1_next_index = 0;
396 /************************************************************
397 * NetQueryDisplayInformation (NETAPI32.@)
398 * Copies NET_DISPLAY_USER record.
400 static void ACCESS_CopyDisplayUser(PNET_DISPLAY_USER dest, LPWSTR *dest_buf,
401 PNET_DISPLAY_USER src)
403 LPWSTR str = *dest_buf;
405 src->usri1_name = str;
406 lstrcpyW(src->usri1_name, dest->usri1_name);
408 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
410 src->usri1_comment = str;
411 lstrcpyW(src->usri1_comment, dest->usri1_comment);
413 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
415 src->usri1_flags = dest->usri1_flags;
417 src->usri1_full_name = str;
418 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
420 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
422 src->usri1_user_id = dest->usri1_user_id;
423 src->usri1_next_index = dest->usri1_next_index;
427 /************************************************************
428 * NetQueryDisplayInformation (NETAPI32.@)
430 * The buffer structure:
431 * - array of fixed size record of the level type
432 * - strings, referenced by the record of the level type
434 NET_API_STATUS WINAPI
435 NetQueryDisplayInformation(
436 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
437 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
440 TRACE("(%s, %ld, %ld, %ld, %ld, %p, %p)\n", debugstr_w(ServerName),
441 Level, Index, EntriesRequested, PreferredMaximumLength,
442 ReturnedEntryCount, SortedBuffer);
443 NETAPI_ForceLocalComputer(ServerName, ERROR_ACCESS_DENIED);
449 PNET_DISPLAY_USER inf;
450 /* current available strings buffer */
452 PNET_DISPLAY_USER admin, guest;
453 DWORD admin_size, guest_size;
457 /* sizes of the field buffers in WCHARS */
458 int name_sz, comment_sz, full_name_sz;
460 /* number of the records, returned in SortedBuffer
461 3 - for current user, Administrator and Guest users
465 FIXME("Level %ld partially implemented\n", Level);
466 *ReturnedEntryCount = records;
472 NetApiBufferAllocate(dwSize, (LPVOID *) &name);
473 if (!GetUserNameW(name, &dwSize))
475 NetApiBufferFree(name);
476 return ERROR_ACCESS_DENIED;
479 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
480 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
483 dwSize = sizeof(NET_DISPLAY_USER) * records;
484 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
486 NetApiBufferAllocate(dwSize +
487 admin_size - sizeof(NET_DISPLAY_USER) +
488 guest_size - sizeof(NET_DISPLAY_USER),
489 (LPVOID *) SortedBuffer);
490 inf = (PNET_DISPLAY_USER) *SortedBuffer;
491 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
492 inf->usri1_name = str;
494 ((PBYTE) str) + name_sz * sizeof(WCHAR));
495 inf->usri1_comment = str;
497 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
498 inf->usri1_full_name = str;
500 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
503 lstrcpyW(inf->usri1_name, name);
504 NetApiBufferFree(name);
505 inf->usri1_comment[0] = 0;
507 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
508 inf->usri1_full_name[0] = 0;
509 inf->usri1_user_id = 0;
510 inf->usri1_next_index = 0;
513 ACCESS_CopyDisplayUser(admin, &str, inf);
514 NetApiBufferFree(admin);
517 ACCESS_CopyDisplayUser(guest, &str, inf);
518 NetApiBufferFree(guest);
525 FIXME("Level %ld is not implemented\n", Level);
530 ERR("Invalid level %ld is specified\n", Level);
531 return ERROR_INVALID_LEVEL;
536 /************************************************************
537 * NetGetDCName (NETAPI32.@)
539 * Return the name of the primary domain controller (PDC)
542 NET_API_STATUS WINAPI
543 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
545 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
546 debugstr_w(domainname), bufptr);
547 return NERR_DCNotFound; /* say we can't find a domain controller */
551 /************************************************************
552 * NetUserModalsGet (NETAPI32.@)
554 NET_API_STATUS WINAPI NetUserModalsGet(LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
556 FIXME("(%s %ld %p) stub!\n", debugstr_w(szServer), level, pbuffer);
557 return NERR_InternalError;
560 /************************************************************
561 * NetLocalGroupAdd (NETAPI32.@)
563 NET_API_STATUS WINAPI NetLocalGroupAdd(LPCWSTR servername, DWORD level,
564 LPBYTE buf, LPDWORD parm_err)
566 FIXME("(%s %ld %p %p) stub!\n", debugstr_w(servername), level, buf, parm_err);
570 /************************************************************
571 * NetLocalGroupSetMember (NETAPI32.@)
574 NET_API_STATUS WINAPI NetLocalGroupSetMembers(LPCWSTR servername,
575 LPCWSTR groupname, DWORD level, LPBYTE buf, DWORD totalentries)
577 FIXME("(%s %s %ld %p %ld) stub!\n", debugstr_w(servername),
578 debugstr_w(groupname),level, buf, totalentries);