advapi32: Add a bunch of file access tests.
[wine.git] / dlls / netapi32 / wksta.c
blob122a6851ce554851782f46c6b1fda9fdbca04c1c
1 /* Copyright 2002 Andriy Palamarchuk
2 * Copyright (c) 2003 Juan Lang
4 * netapi32 user 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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winsock2.h"
31 #include "nb30.h"
32 #include "lmcons.h"
33 #include "lmapibuf.h"
34 #include "lmerr.h"
35 #include "lmwksta.h"
36 #include "iphlpapi.h"
37 #include "winerror.h"
38 #include "ntsecapi.h"
39 #include "netbios.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
44 /************************************************************
45 * NETAPI_IsLocalComputer
47 * Checks whether the server name indicates local machine.
49 BOOL NETAPI_IsLocalComputer(LMCSTR ServerName)
51 if (!ServerName)
53 return TRUE;
55 else if (ServerName[0] == '\0')
56 return TRUE;
57 else
59 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
60 BOOL Result;
61 LPWSTR buf;
63 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
64 Result = GetComputerNameW(buf, &dwSize);
65 if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
66 ServerName += 2;
67 Result = Result && !lstrcmpW(ServerName, buf);
68 NetApiBufferFree(buf);
70 return Result;
74 static void wprint_mac(WCHAR* buffer, int len, const MIB_IFROW *ifRow)
76 int i;
77 unsigned char val;
79 if (!buffer)
80 return;
81 if (len < 1)
82 return;
83 if (!ifRow)
85 *buffer = '\0';
86 return;
89 for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
91 val = ifRow->bPhysAddr[i];
92 if ((val >>4) >9)
93 buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10);
94 else
95 buffer[2*i] = (WCHAR)((val >>4) + '0');
96 if ((val & 0xf ) >9)
97 buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10);
98 else
99 buffer[2*i+1] = (WCHAR)((val & 0xf) + '0');
101 buffer[2*i]=0;
104 /* Theoretically this could be too short, except that MS defines
105 * MAX_ADAPTER_NAME as 128, and MAX_INTERFACE_NAME_LEN as 256, and both
106 * represent a count of WCHARs, so even with an extraordinarily long header
107 * this will be plenty
109 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN
110 #define MAX_TRANSPORT_ADDR 13
112 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_"
113 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_"
115 static void wprint_name(WCHAR *buffer, int len, ULONG transport,
116 PMIB_IFROW ifRow)
118 WCHAR *ptr1, *ptr2;
119 const char *name;
121 if (!buffer)
122 return;
123 if (!ifRow)
125 *buffer = '\0';
126 return;
129 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
130 name = NBT_TRANSPORT_NAME_HEADER;
131 else
132 name = UNKNOWN_TRANSPORT_NAME_HEADER;
134 for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++)
135 *ptr1 = *name;
136 for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++)
137 *ptr1 = *ptr2;
138 *ptr1 = '\0';
141 /***********************************************************************
142 * NetWkstaTransportEnum (NETAPI32.@)
145 struct WkstaTransportEnumData
147 UCHAR n_adapt;
148 UCHAR n_read;
149 DWORD prefmaxlen;
150 LPBYTE *pbuf;
151 NET_API_STATUS ret;
154 /**********************************************************************/
156 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
157 ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
159 BOOL ret;
160 struct WkstaTransportEnumData *enumData = closure;
162 if (enumData && enumData->pbuf)
164 if (lanaIndex == 0)
166 DWORD toAllocate;
168 enumData->n_adapt = totalLANAs;
169 enumData->n_read = 0;
171 toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0)
172 + MAX_TRANSPORT_NAME * sizeof(WCHAR) +
173 MAX_TRANSPORT_ADDR * sizeof(WCHAR));
174 if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH)
175 toAllocate = enumData->prefmaxlen;
176 NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf);
178 if (*(enumData->pbuf))
180 UCHAR spaceFor;
182 if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH)
183 spaceFor = totalLANAs;
184 else
185 spaceFor = enumData->prefmaxlen /
186 (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME +
187 MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
188 if (enumData->n_read < spaceFor)
190 PWKSTA_TRANSPORT_INFO_0 ti;
191 LMSTR transport_name, transport_addr;
192 MIB_IFROW ifRow;
194 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) +
195 enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0));
196 transport_name = (LMSTR)(*(enumData->pbuf) +
197 totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) +
198 enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR));
199 transport_addr = (LMSTR)(*(enumData->pbuf) +
200 totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) +
201 MAX_TRANSPORT_NAME * sizeof(WCHAR)) +
202 enumData->n_read * MAX_TRANSPORT_ADDR * sizeof(WCHAR));
204 ifRow.dwIndex = data->ifIndex;
205 GetIfEntry(&ifRow);
206 ti->wkti0_quality_of_service = 0;
207 ti->wkti0_number_of_vcs = 0;
208 ti->wkti0_transport_name = transport_name;
209 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME,
210 transport, &ifRow);
211 ti->wkti0_transport_address = transport_addr;
212 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR,
213 &ifRow);
214 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
215 ti->wkti0_wan_ish = TRUE;
216 else
217 ti->wkti0_wan_ish = FALSE;
218 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti);
219 TRACE("transport_name at %p %s\n",
220 ti->wkti0_transport_name,
221 debugstr_w(ti->wkti0_transport_name));
222 TRACE("transport_address at %p %s\n",
223 ti->wkti0_transport_address,
224 debugstr_w(ti->wkti0_transport_address));
225 enumData->n_read++;
226 enumData->ret = NERR_Success;
227 ret = TRUE;
229 else
231 enumData->ret = ERROR_MORE_DATA;
232 ret = FALSE;
235 else
237 enumData->ret = ERROR_OUTOFMEMORY;
238 ret = FALSE;
241 else
242 ret = FALSE;
243 return ret;
246 /**********************************************************************/
248 NET_API_STATUS WINAPI
249 NetWkstaTransportEnum(LMSTR ServerName, DWORD level, PBYTE* pbuf,
250 DWORD prefmaxlen, LPDWORD read_entries,
251 PDWORD total_entries, PDWORD hresume)
253 NET_API_STATUS ret;
255 TRACE(":%s, 0x%08x, %p, 0x%08x, %p, %p, %p\n", debugstr_w(ServerName),
256 level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
257 if (!NETAPI_IsLocalComputer(ServerName))
259 FIXME(":not implemented for non-local computers\n");
260 ret = ERROR_INVALID_LEVEL;
262 else
264 if (hresume && *hresume)
266 FIXME(":resume handle not implemented\n");
267 return ERROR_INVALID_LEVEL;
270 switch (level)
272 case 0: /* transport info */
274 ULONG allTransports;
275 struct WkstaTransportEnumData enumData;
277 if (NetBIOSNumAdapters() == 0)
278 return ERROR_NETWORK_UNREACHABLE;
279 if (!read_entries)
280 return STATUS_ACCESS_VIOLATION;
281 if (!total_entries || !pbuf)
282 return RPC_X_NULL_REF_POINTER;
284 enumData.prefmaxlen = prefmaxlen;
285 enumData.pbuf = pbuf;
286 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG));
287 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback,
288 &enumData);
289 *read_entries = enumData.n_read;
290 *total_entries = enumData.n_adapt;
291 if (hresume) *hresume= 0;
292 ret = enumData.ret;
293 break;
295 default:
296 TRACE("Invalid level %d is specified\n", level);
297 ret = ERROR_INVALID_LEVEL;
300 return ret;
304 /************************************************************
305 * NetWkstaUserGetInfo (NETAPI32.@)
307 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LMSTR reserved, DWORD level,
308 PBYTE* bufptr)
310 NET_API_STATUS nastatus;
312 TRACE("(%s, %d, %p)\n", debugstr_w(reserved), level, bufptr);
313 switch (level)
315 case 0:
317 PWKSTA_USER_INFO_0 ui;
318 DWORD dwSize = UNLEN + 1;
320 /* set up buffer */
321 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
322 (LPVOID *) bufptr);
323 if (nastatus != NERR_Success)
324 return ERROR_NOT_ENOUGH_MEMORY;
326 ui = (PWKSTA_USER_INFO_0) *bufptr;
327 ui->wkui0_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
329 /* get data */
330 if (!GetUserNameW(ui->wkui0_username, &dwSize))
332 NetApiBufferFree(ui);
333 return ERROR_NOT_ENOUGH_MEMORY;
335 else {
336 nastatus = NetApiBufferReallocate(
337 *bufptr, sizeof(WKSTA_USER_INFO_0) +
338 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
339 (LPVOID *) bufptr);
340 if (nastatus != NERR_Success)
341 return nastatus;
343 break;
346 case 1:
348 PWKSTA_USER_INFO_1 ui;
349 PWKSTA_USER_INFO_0 ui0;
350 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
351 LSA_HANDLE PolicyHandle;
352 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
353 NTSTATUS NtStatus;
355 /* sizes of the field buffers in WCHARS */
356 int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
358 FIXME("Level 1 processing is partially implemented\n");
359 oth_domains_sz = 1;
360 logon_server_sz = 1;
362 /* get some information first to estimate size of the buffer */
363 ui0 = NULL;
364 nastatus = NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
365 if (nastatus != NERR_Success)
366 return nastatus;
367 username_sz = lstrlenW(ui0->wkui0_username) + 1;
369 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
370 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
371 POLICY_VIEW_LOCAL_INFORMATION,
372 &PolicyHandle);
373 if (NtStatus != STATUS_SUCCESS)
375 TRACE("LsaOpenPolicyFailed with NT status %x\n",
376 LsaNtStatusToWinError(NtStatus));
377 NetApiBufferFree(ui0);
378 return ERROR_NOT_ENOUGH_MEMORY;
380 LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
381 (PVOID*) &DomainInfo);
382 logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
383 LsaClose(PolicyHandle);
385 /* set up buffer */
386 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
387 (username_sz + logon_domain_sz +
388 oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
389 (LPVOID *) bufptr);
390 if (nastatus != NERR_Success) {
391 NetApiBufferFree(ui0);
392 return nastatus;
394 ui = (WKSTA_USER_INFO_1 *) *bufptr;
395 ui->wkui1_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
396 ui->wkui1_logon_domain = (LMSTR) (
397 ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
398 ui->wkui1_oth_domains = (LMSTR) (
399 ((PBYTE) ui->wkui1_logon_domain) +
400 logon_domain_sz * sizeof(WCHAR));
401 ui->wkui1_logon_server = (LMSTR) (
402 ((PBYTE) ui->wkui1_oth_domains) +
403 oth_domains_sz * sizeof(WCHAR));
405 /* get data */
406 lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
407 NetApiBufferFree(ui0);
409 lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
410 logon_domain_sz);
411 LsaFreeMemory(DomainInfo);
413 /* FIXME. Not implemented. Populated with empty strings */
414 ui->wkui1_oth_domains[0] = 0;
415 ui->wkui1_logon_server[0] = 0;
416 break;
418 case 1101:
420 PWKSTA_USER_INFO_1101 ui;
421 DWORD dwSize = 1;
423 FIXME("Stub. Level 1101 processing is not implemented\n");
424 /* FIXME see also wkui1_oth_domains for level 1 */
426 /* set up buffer */
427 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
428 (LPVOID *) bufptr);
429 if (nastatus != NERR_Success)
430 return nastatus;
431 ui = (PWKSTA_USER_INFO_1101) *bufptr;
432 ui->wkui1101_oth_domains = (LMSTR)(ui + 1);
434 /* get data */
435 ui->wkui1101_oth_domains[0] = 0;
436 break;
438 default:
439 TRACE("Invalid level %d is specified\n", level);
440 return ERROR_INVALID_LEVEL;
442 return NERR_Success;
445 /************************************************************
446 * NetWkstaUserEnum (NETAPI32.@)
448 NET_API_STATUS WINAPI
449 NetWkstaUserEnum(LMSTR servername, DWORD level, LPBYTE* bufptr,
450 DWORD prefmaxlen, LPDWORD entriesread,
451 LPDWORD totalentries, LPDWORD resumehandle)
453 FIXME("(%s, %d, %p, %d, %p, %p, %p): stub!\n", debugstr_w(servername),
454 level, bufptr, prefmaxlen, entriesread, totalentries, resumehandle);
455 return ERROR_INVALID_PARAMETER;
458 /************************************************************
459 * NetpGetComputerName (NETAPI32.@)
461 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
463 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
465 TRACE("(%p)\n", Buffer);
466 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
467 if (GetComputerNameW(*Buffer, &dwSize))
469 return NetApiBufferReallocate(
470 *Buffer, (dwSize + 1) * sizeof(WCHAR),
471 (LPVOID *) Buffer);
473 else
475 NetApiBufferFree(*Buffer);
476 return ERROR_NOT_ENOUGH_MEMORY;
480 NET_API_STATUS WINAPI I_NetNameCompare(LPVOID p1, LPWSTR wkgrp, LPWSTR comp,
481 LPVOID p4, LPVOID p5)
483 FIXME("(%p %s %s %p %p): stub\n", p1, debugstr_w(wkgrp), debugstr_w(comp),
484 p4, p5);
485 return ERROR_INVALID_PARAMETER;
488 NET_API_STATUS WINAPI I_NetNameValidate(LPVOID p1, LPWSTR wkgrp, LPVOID p3,
489 LPVOID p4)
491 FIXME("(%p %s %p %p): stub\n", p1, debugstr_w(wkgrp), p3, p4);
492 return ERROR_INVALID_PARAMETER;
495 NET_API_STATUS WINAPI NetWkstaGetInfo( LMSTR servername, DWORD level,
496 LPBYTE* bufptr)
498 NET_API_STATUS ret;
500 TRACE("%s %d %p\n", debugstr_w( servername ), level, bufptr );
501 if (servername)
503 if (!NETAPI_IsLocalComputer(servername))
505 FIXME("remote computers not supported\n");
506 return ERROR_INVALID_LEVEL;
509 if (!bufptr) return ERROR_INVALID_PARAMETER;
511 switch (level)
513 case 100:
514 case 101:
515 case 102:
517 static const WCHAR lanroot[] = {'c',':','\\','l','a','n','m','a','n',0}; /* FIXME */
518 DWORD computerNameLen, domainNameLen, size;
519 WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
520 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
521 LSA_HANDLE PolicyHandle;
522 NTSTATUS NtStatus;
524 computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
525 GetComputerNameW(computerName, &computerNameLen);
526 computerNameLen++; /* include NULL terminator */
528 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
529 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
530 POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
531 if (NtStatus != STATUS_SUCCESS)
532 ret = LsaNtStatusToWinError(NtStatus);
533 else
535 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
537 LsaQueryInformationPolicy(PolicyHandle,
538 PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
539 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
540 size = sizeof(WKSTA_INFO_102) + computerNameLen * sizeof(WCHAR)
541 + domainNameLen * sizeof(WCHAR) + sizeof(lanroot);
542 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
543 if (ret == NERR_Success)
545 /* INFO_100 and INFO_101 structures are subsets of INFO_102 */
546 PWKSTA_INFO_102 info = (PWKSTA_INFO_102)*bufptr;
547 OSVERSIONINFOW verInfo;
549 info->wki102_platform_id = PLATFORM_ID_NT;
550 info->wki102_computername = (LMSTR)(*bufptr +
551 sizeof(WKSTA_INFO_102));
552 memcpy(info->wki102_computername, computerName,
553 computerNameLen * sizeof(WCHAR));
554 info->wki102_langroup = info->wki102_computername + computerNameLen;
555 memcpy(info->wki102_langroup, DomainInfo->DomainName.Buffer,
556 domainNameLen * sizeof(WCHAR));
557 info->wki102_lanroot = info->wki102_langroup + domainNameLen;
558 memcpy(info->wki102_lanroot, lanroot, sizeof(lanroot));
559 memset(&verInfo, 0, sizeof(verInfo));
560 verInfo.dwOSVersionInfoSize = sizeof(verInfo);
561 GetVersionExW(&verInfo);
562 info->wki102_ver_major = verInfo.dwMajorVersion;
563 info->wki102_ver_minor = verInfo.dwMinorVersion;
564 info->wki102_logged_on_users = 1;
566 LsaFreeMemory(DomainInfo);
567 LsaClose(PolicyHandle);
569 break;
572 default:
573 FIXME("level %d unimplemented\n", level);
574 ret = ERROR_INVALID_LEVEL;
576 return ret;
579 /************************************************************
580 * NetGetJoinInformation (NETAPI32.@)
582 NET_API_STATUS NET_API_FUNCTION NetGetJoinInformation(
583 LPCWSTR Server,
584 LPWSTR *Name,
585 PNETSETUP_JOIN_STATUS type)
587 FIXME("Stub %s %p %p\n", wine_dbgstr_w(Server), Name, type);
589 *Name = NULL;
590 *type = NetSetupUnknownStatus;
592 return NERR_Success;
595 /************************************************************
596 * NetUserGetGroups (NETAPI32.@)
598 NET_API_STATUS NET_API_FUNCTION NetUserGetGroups(
599 LPCWSTR servername,
600 LPCWSTR username,
601 DWORD level,
602 LPBYTE *bufptr,
603 DWORD prefixmaxlen,
604 LPDWORD entriesread,
605 LPDWORD totalentries)
607 FIXME("%s %s %d %p %d %p %p stub\n", debugstr_w(servername),
608 debugstr_w(username), level, bufptr, prefixmaxlen, entriesread,
609 totalentries);
611 *bufptr = NULL;
612 *entriesread = 0;
613 *totalentries = 0;
615 return ERROR_INVALID_LEVEL;