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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
45 /************************************************************
46 * NETAPI_IsLocalComputer
48 * Checks whether the server name indicates local machine.
50 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName)
58 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
62 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
63 Result = GetComputerNameW(buf, &dwSize);
64 if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
66 Result = Result && !lstrcmpW(ServerName, buf);
67 NetApiBufferFree(buf);
73 static void wprint_mac(WCHAR* buffer, int len, PMIB_IFROW ifRow)
88 for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
90 val = ifRow->bPhysAddr[i];
92 buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10);
94 buffer[2*i] = (WCHAR)((val >>4) + '0');
96 buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10);
98 buffer[2*i+1] = (WCHAR)((val & 0xf) + '0');
100 buffer[2*i]=(WCHAR)0;
103 /* Theoretically this could be too short, except that MS defines
104 * MAX_ADAPTER_NAME as 128, and MAX_INTERFACE_NAME_LEN as 256, and both
105 * represent a count of WCHARs, so even with an extroardinarily long header
106 * this will be plenty
108 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN
109 #define MAX_TRANSPORT_ADDR 13
111 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_"
112 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_"
114 static void wprint_name(WCHAR *buffer, int len, ULONG transport,
128 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
129 name = NBT_TRANSPORT_NAME_HEADER;
131 name = UNKNOWN_TRANSPORT_NAME_HEADER;
133 for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++)
135 for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++)
140 struct WkstaTransportEnumData
149 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
150 ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
153 struct WkstaTransportEnumData *enumData = (struct WkstaTransportEnumData *)
156 if (enumData && enumData->pbuf)
162 enumData->n_adapt = totalLANAs;
163 enumData->n_read = 0;
165 toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0)
166 + MAX_TRANSPORT_NAME * sizeof(WCHAR) +
167 MAX_TRANSPORT_ADDR * sizeof(WCHAR));
168 if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH)
169 toAllocate = enumData->prefmaxlen;
170 NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf);
172 if (*(enumData->pbuf))
176 if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH)
177 spaceFor = totalLANAs;
179 spaceFor = enumData->prefmaxlen /
180 (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME +
181 MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
182 if (enumData->n_read < spaceFor)
184 PWKSTA_TRANSPORT_INFO_0 ti;
185 LPWSTR transport_name, transport_addr;
188 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) +
189 enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0));
190 transport_name = (LPWSTR)(*(enumData->pbuf) +
191 totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) +
192 enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR));
193 transport_addr = (LPWSTR)(*(enumData->pbuf) +
194 totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) +
195 MAX_TRANSPORT_NAME * sizeof(WCHAR)) +
196 enumData->n_read * MAX_TRANSPORT_ADDR * sizeof(WCHAR));
198 ifRow.dwIndex = data->ifIndex;
200 ti->wkti0_quality_of_service = 0;
201 ti->wkti0_number_of_vcs = 0;
202 ti->wkti0_transport_name = transport_name;
203 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME,
205 ti->wkti0_transport_address = transport_addr;
206 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR,
208 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
209 ti->wkti0_wan_ish = TRUE;
211 ti->wkti0_wan_ish = FALSE;
212 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti);
213 TRACE("transport_name at %p %s\n",
214 ti->wkti0_transport_name,
215 debugstr_w(ti->wkti0_transport_name));
216 TRACE("transport_address at %p %s\n",
217 ti->wkti0_transport_address,
218 debugstr_w(ti->wkti0_transport_address));
220 enumData->ret = NERR_Success;
225 enumData->ret = ERROR_MORE_DATA;
231 enumData->ret = ERROR_OUTOFMEMORY;
240 NET_API_STATUS WINAPI
241 NetWkstaTransportEnum(LPWSTR ServerName, DWORD level, PBYTE* pbuf,
242 DWORD prefmaxlen, LPDWORD read_entries,
243 PDWORD total_entries, PDWORD hresume)
247 TRACE(":%s, 0x%08lx, %p, 0x%08lx, %p, %p, %p\n", debugstr_w(ServerName),
248 level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
249 if (!NETAPI_IsLocalComputer(ServerName))
251 FIXME(":not implemented for non-local computers\n");
252 ret = ERROR_INVALID_LEVEL;
256 if (hresume && *hresume)
258 FIXME(":resume handle not implemented\n");
259 return ERROR_INVALID_LEVEL;
264 case 0: /* transport info */
267 struct WkstaTransportEnumData enumData;
269 if (NetBIOSNumAdapters() == 0)
270 return ERROR_NETWORK_UNREACHABLE;
272 return STATUS_ACCESS_VIOLATION;
273 if (!total_entries || !pbuf)
274 return RPC_X_NULL_REF_POINTER;
276 enumData.prefmaxlen = prefmaxlen;
277 enumData.pbuf = pbuf;
278 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG));
279 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback,
281 *read_entries = enumData.n_read;
282 *total_entries = enumData.n_adapt;
283 if (hresume) *hresume= 0;
288 ERR("Invalid level %ld is specified\n", level);
289 ret = ERROR_INVALID_LEVEL;
296 /************************************************************
297 * NetWkstaUserGetInfo (NETAPI32.@)
299 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LPWSTR reserved, DWORD level,
302 TRACE("(%s, %ld, %p)\n", debugstr_w(reserved), level, bufptr);
307 PWKSTA_USER_INFO_0 ui;
308 DWORD dwSize = UNLEN + 1;
311 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
314 ui = (PWKSTA_USER_INFO_0) *bufptr;
315 ui->wkui0_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
318 if (!GetUserNameW(ui->wkui0_username, &dwSize))
320 NetApiBufferFree(ui);
321 return ERROR_NOT_ENOUGH_MEMORY;
324 NetApiBufferReallocate(
325 *bufptr, sizeof(WKSTA_USER_INFO_0) +
326 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
333 PWKSTA_USER_INFO_1 ui;
334 PWKSTA_USER_INFO_0 ui0;
336 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
337 LSA_HANDLE PolicyHandle;
338 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
341 /* sizes of the field buffers in WCHARS */
342 int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
344 FIXME("Level 1 processing is partially implemented\n");
348 /* get some information first to estimate size of the buffer */
350 NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
351 username_sz = lstrlenW(ui0->wkui0_username) + 1;
353 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
354 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
355 POLICY_VIEW_LOCAL_INFORMATION,
357 if (NtStatus != STATUS_SUCCESS)
359 ERR("LsaOpenPolicyFailed with NT status %lx\n",
360 LsaNtStatusToWinError(NtStatus));
361 NetApiBufferFree(ui0);
362 return ERROR_NOT_ENOUGH_MEMORY;
364 LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
365 (PVOID*) &DomainInfo);
366 logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
367 LsaClose(PolicyHandle);
370 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
371 (username_sz + logon_domain_sz +
372 oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
374 ui = (WKSTA_USER_INFO_1 *) *bufptr;
375 ui->wkui1_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
376 ui->wkui1_logon_domain = (LPWSTR) (
377 ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
378 ui->wkui1_oth_domains = (LPWSTR) (
379 ((PBYTE) ui->wkui1_logon_domain) +
380 logon_domain_sz * sizeof(WCHAR));
381 ui->wkui1_logon_server = (LPWSTR) (
382 ((PBYTE) ui->wkui1_oth_domains) +
383 oth_domains_sz * sizeof(WCHAR));
386 dwSize = username_sz;
387 lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
388 NetApiBufferFree(ui0);
390 lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
392 LsaFreeMemory(DomainInfo);
394 /* FIXME. Not implemented. Populated with empty strings */
395 ui->wkui1_oth_domains[0] = 0;
396 ui->wkui1_logon_server[0] = 0;
401 PWKSTA_USER_INFO_1101 ui;
404 FIXME("Stub. Level 1101 processing is not implemented\n");
405 /* FIXME see also wkui1_oth_domains for level 1 */
408 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
411 ui = (PWKSTA_USER_INFO_1101) *bufptr;
412 ui->wkui1101_oth_domains = (LPWSTR)(ui + 1);
415 ui->wkui1101_oth_domains[0] = 0;
419 ERR("Invalid level %ld is specified\n", level);
420 return ERROR_INVALID_LEVEL;
425 /************************************************************
426 * NetpGetComputerName (NETAPI32.@)
428 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
430 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
432 TRACE("(%p)\n", Buffer);
433 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
434 if (GetComputerNameW(*Buffer, &dwSize))
436 NetApiBufferReallocate(
437 *Buffer, dwSize * sizeof(WCHAR),
443 NetApiBufferFree(*Buffer);
444 return ERROR_NOT_ENOUGH_MEMORY;
448 NET_API_STATUS WINAPI NetWkstaGetInfo( LPWSTR servername, DWORD level,
453 TRACE("%p %ld %p\n", debugstr_w( servername ), level, bufptr );
456 FIXME("remote computers not supported\n");
457 return ERROR_INVALID_LEVEL;
459 if (!bufptr) return ERROR_INVALID_PARAMETER;
465 DWORD computerNameLen, domainNameLen, size;
466 WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
467 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
468 LSA_HANDLE PolicyHandle;
471 computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
472 GetComputerNameW(computerName, &computerNameLen);
473 computerNameLen++; /* include NULL terminator */
475 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
476 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
477 POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
478 if (NtStatus != STATUS_SUCCESS)
479 ret = LsaNtStatusToWinError(NtStatus);
482 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
484 LsaQueryInformationPolicy(PolicyHandle,
485 PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
486 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
487 size = sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR)
488 + domainNameLen * sizeof(WCHAR);
489 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
490 if (ret == NERR_Success)
492 PWKSTA_INFO_100 info = (PWKSTA_INFO_100)*bufptr;
493 OSVERSIONINFOW verInfo;
495 info->wki100_platform_id = PLATFORM_ID_NT;
496 info->wki100_computername = (LPWSTR)(*bufptr +
497 sizeof(WKSTA_INFO_100));
498 memcpy(info->wki100_computername, computerName,
499 computerNameLen * sizeof(WCHAR));
500 info->wki100_langroup = (LPWSTR)(*bufptr +
501 sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR));
502 memcpy(info->wki100_langroup, DomainInfo->DomainName.Buffer,
503 domainNameLen * sizeof(WCHAR));
504 memset(&verInfo, 0, sizeof(verInfo));
505 verInfo.dwOSVersionInfoSize = sizeof(verInfo);
506 GetVersionExW(&verInfo);
507 info->wki100_ver_major = verInfo.dwMajorVersion;
508 info->wki100_ver_minor = verInfo.dwMinorVersion;
510 LsaFreeMemory(DomainInfo);
511 LsaClose(PolicyHandle);
517 FIXME("level %ld unimplemented\n", level);
518 ret = ERROR_INVALID_LEVEL;