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 const WCHAR sAdminUserName[] = {'A','d','m','i','n','i','s','t','r','a','t',
38 const WCHAR sGuestUserName[] = {'G','u','e','s','t',0};
40 /************************************************************
41 * NETAPI_ValidateServername
43 * Validates server name
45 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 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 * NetUserGetInfo (NETAPI32.@)
99 NetUserGetInfo(LPCWSTR servername, LPCWSTR username, DWORD level,
102 NET_API_STATUS status;
103 TRACE("(%s, %s, %ld, %p)\n", debugstr_w(servername), debugstr_w(username),
105 status = NETAPI_ValidateServername(servername);
106 if (status != NERR_Success)
108 NETAPI_ForceLocalComputer(servername, NERR_InvalidComputer);
109 NETAPI_ForceKnownUser(username, NERR_UserNotFound);
118 name_sz = lstrlenW(username) + 1;
121 NetApiBufferAllocate(sizeof(USER_INFO_0) + name_sz * sizeof(WCHAR),
124 ui = (PUSER_INFO_0) *bufptr;
125 ui->usri0_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_0));
128 lstrcpyW(ui->usri0_name, username);
136 NET_API_STATUS status;
137 /* sizes of the field buffers in WCHARS */
138 int name_sz, comment_sz, usr_comment_sz, full_name_sz;
145 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
146 if (status != NERR_Success)
148 NetApiBufferFree(ui0);
151 name_sz = lstrlenW(ui0->usri0_name) + 1;
154 NetApiBufferAllocate(sizeof(USER_INFO_10) +
155 (name_sz + comment_sz + usr_comment_sz +
156 full_name_sz) * sizeof(WCHAR),
158 ui = (PUSER_INFO_10) *bufptr;
159 ui->usri10_name = (LPWSTR) (*bufptr + sizeof(USER_INFO_10));
160 ui->usri10_comment = (LPWSTR) (
161 ((PBYTE) ui->usri10_name) + name_sz * sizeof(WCHAR));
162 ui->usri10_usr_comment = (LPWSTR) (
163 ((PBYTE) ui->usri10_comment) + comment_sz * sizeof(WCHAR));
164 ui->usri10_full_name = (LPWSTR) (
165 ((PBYTE) ui->usri10_usr_comment) + usr_comment_sz * sizeof(WCHAR));
168 lstrcpyW(ui->usri10_name, ui0->usri0_name);
169 NetApiBufferFree(ui0);
170 ui->usri10_comment[0] = 0;
171 ui->usri10_usr_comment[0] = 0;
172 ui->usri10_full_name[0] = 0;
178 static const WCHAR homedirW[] = {'H','O','M','E',0};
181 NET_API_STATUS status;
182 /* sizes of the field buffers in WCHARS */
183 int name_sz, password_sz, home_dir_sz, comment_sz, script_path_sz;
185 password_sz = 1; /* not filled out for security reasons for NetUserGetInfo*/
190 status = NetUserGetInfo(servername, username, 0, (LPBYTE *) &ui0);
191 if (status != NERR_Success)
193 NetApiBufferFree(ui0);
196 name_sz = lstrlenW(ui0->usri0_name) + 1;
197 home_dir_sz = GetEnvironmentVariableW(homedirW, NULL,0);
199 NetApiBufferAllocate(sizeof(USER_INFO_1) +
200 (name_sz + password_sz + home_dir_sz +
201 comment_sz + script_path_sz) * sizeof(WCHAR),
204 ui = (PUSER_INFO_1) *bufptr;
205 ui->usri1_name = (LPWSTR) (ui + 1);
206 ui->usri1_password = ui->usri1_name + name_sz;
207 ui->usri1_home_dir = ui->usri1_password + password_sz;
208 ui->usri1_comment = ui->usri1_home_dir + home_dir_sz;
209 ui->usri1_script_path = ui->usri1_comment + comment_sz;
211 lstrcpyW(ui->usri1_name, ui0->usri0_name);
212 NetApiBufferFree(ui0);
213 ui->usri1_password[0] = 0;
214 ui->usri1_password_age = 0;
216 GetEnvironmentVariableW(homedirW, ui->usri1_home_dir,home_dir_sz);
217 ui->usri1_comment[0] = 0;
219 ui->usri1_script_path[0] = 0;
249 FIXME("Level %ld is not implemented\n", level);
253 ERR("Invalid level %ld is specified\n", level);
254 return ERROR_INVALID_LEVEL;
259 /************************************************************
260 * ACCESS_QueryAdminDisplayInformation
262 * Creates a buffer with information for the Admin User
264 void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
266 const WCHAR sAdminUserName[] = {
267 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
269 /* sizes of the field buffers in WCHARS */
270 int name_sz, comment_sz, full_name_sz;
271 PNET_DISPLAY_USER usr;
274 name_sz = lstrlenW(sAdminUserName);
278 *pdwSize = sizeof(NET_DISPLAY_USER);
279 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
280 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
283 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
284 usr->usri1_comment = (LPWSTR) (
285 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
286 usr->usri1_full_name = (LPWSTR) (
287 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
290 lstrcpyW(usr->usri1_name, sAdminUserName);
291 usr->usri1_comment[0] = 0;
292 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
293 usr->usri1_full_name[0] = 0;
294 usr->usri1_user_id = 500;
295 usr->usri1_next_index = 0;
298 /************************************************************
299 * ACCESS_QueryGuestDisplayInformation
301 * Creates a buffer with information for the Guest User
303 void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
305 const WCHAR sGuestUserName[] = {
306 'G','u','e','s','t',0 };
308 /* sizes of the field buffers in WCHARS */
309 int name_sz, comment_sz, full_name_sz;
310 PNET_DISPLAY_USER usr;
313 name_sz = lstrlenW(sGuestUserName);
317 *pdwSize = sizeof(NET_DISPLAY_USER);
318 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
319 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
322 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
323 usr->usri1_comment = (LPWSTR) (
324 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
325 usr->usri1_full_name = (LPWSTR) (
326 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
329 lstrcpyW(usr->usri1_name, sGuestUserName);
330 usr->usri1_comment[0] = 0;
331 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
332 UF_DONT_EXPIRE_PASSWD;
333 usr->usri1_full_name[0] = 0;
334 usr->usri1_user_id = 500;
335 usr->usri1_next_index = 0;
338 /************************************************************
339 * NetQueryDisplayInformation (NETAPI32.@)
340 * Copies NET_DISPLAY_USER record.
342 void ACCESS_CopyDisplayUser(PNET_DISPLAY_USER dest, LPWSTR *dest_buf,
343 PNET_DISPLAY_USER src)
345 LPWSTR str = *dest_buf;
347 src->usri1_name = str;
348 lstrcpyW(src->usri1_name, dest->usri1_name);
350 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
352 src->usri1_comment = str;
353 lstrcpyW(src->usri1_comment, dest->usri1_comment);
355 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
357 src->usri1_flags = dest->usri1_flags;
359 src->usri1_full_name = str;
360 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
362 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
364 src->usri1_user_id = dest->usri1_user_id;
365 src->usri1_next_index = dest->usri1_next_index;
369 /************************************************************
370 * NetQueryDisplayInformation (NETAPI32.@)
372 * The buffer structure:
373 * - array of fixed size record of the level type
374 * - strings, referenced by the record of the level type
376 NET_API_STATUS WINAPI
377 NetQueryDisplayInformation(
378 LPWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
379 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
382 TRACE("(%s, %ld, %ld, %ld, %ld, %p, %p)\n", debugstr_w(ServerName),
383 Level, Index, EntriesRequested, PreferredMaximumLength,
384 ReturnedEntryCount, SortedBuffer);
385 NETAPI_ForceLocalComputer(ServerName, ERROR_ACCESS_DENIED);
391 PNET_DISPLAY_USER inf;
392 /* current available strings buffer */
394 PNET_DISPLAY_USER admin, guest;
395 DWORD admin_size, guest_size;
399 /* sizes of the field buffers in WCHARS */
400 int name_sz, comment_sz, full_name_sz;
402 /* number of the records, returned in SortedBuffer
403 3 - for current user, Administrator and Guest users
407 FIXME("Level %ld partially implemented\n", Level);
408 *ReturnedEntryCount = records;
414 NetApiBufferAllocate(dwSize, (LPVOID *) &name);
415 if (!GetUserNameW(name, &dwSize))
417 NetApiBufferFree(name);
418 return ERROR_ACCESS_DENIED;
421 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
422 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
425 dwSize = sizeof(NET_DISPLAY_USER) * records;
426 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
428 NetApiBufferAllocate(dwSize +
429 admin_size - sizeof(NET_DISPLAY_USER) +
430 guest_size - sizeof(NET_DISPLAY_USER),
431 (LPVOID *) SortedBuffer);
432 inf = (PNET_DISPLAY_USER) *SortedBuffer;
433 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
434 inf->usri1_name = str;
436 ((PBYTE) str) + name_sz * sizeof(WCHAR));
437 inf->usri1_comment = str;
439 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
440 inf->usri1_full_name = str;
442 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
445 lstrcpyW(inf->usri1_name, name);
446 NetApiBufferFree(name);
447 inf->usri1_comment[0] = 0;
449 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
450 inf->usri1_full_name[0] = 0;
451 inf->usri1_user_id = 0;
452 inf->usri1_next_index = 0;
455 ACCESS_CopyDisplayUser(admin, &str, inf);
456 NetApiBufferFree(admin);
459 ACCESS_CopyDisplayUser(guest, &str, inf);
460 NetApiBufferFree(guest);
467 FIXME("Level %ld is not implemented\n", Level);
472 ERR("Invalid level %ld is specified\n", Level);
473 return ERROR_INVALID_LEVEL;
478 /************************************************************
479 * NetGetDCName (NETAPI32.@)
481 * Return the name of the primary domain controller (PDC)
484 NET_API_STATUS WINAPI
485 NetGetDCName(LPWSTR servername, LPWSTR domainname, LPBYTE bufptr)
488 return NERR_DCNotFound; /* say we can't find a domain controller */