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
22 #include "wine/port.h"
27 #define WIN32_NO_STATUS
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)
55 else if (ServerName[0] == '\0')
59 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
63 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
64 Result = GetComputerNameW(buf, &dwSize);
65 if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
67 Result = Result && !lstrcmpW(ServerName, buf);
68 NetApiBufferFree(buf);
74 static void wprint_mac(WCHAR* buffer, int len, const MIB_IFROW *ifRow)
89 for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
91 val = ifRow->bPhysAddr[i];
93 buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10);
95 buffer[2*i] = (WCHAR)((val >>4) + '0');
97 buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10);
99 buffer[2*i+1] = (WCHAR)((val & 0xf) + '0');
101 buffer[2*i]=(WCHAR)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,
129 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
130 name = NBT_TRANSPORT_NAME_HEADER;
132 name = UNKNOWN_TRANSPORT_NAME_HEADER;
134 for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++)
136 for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++)
141 /***********************************************************************
142 * NetWkstaTransportEnum (NETAPI32.@)
145 struct WkstaTransportEnumData
154 /**********************************************************************/
156 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
157 ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
160 struct WkstaTransportEnumData *enumData = (struct WkstaTransportEnumData *)
163 if (enumData && enumData->pbuf)
169 enumData->n_adapt = totalLANAs;
170 enumData->n_read = 0;
172 toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0)
173 + MAX_TRANSPORT_NAME * sizeof(WCHAR) +
174 MAX_TRANSPORT_ADDR * sizeof(WCHAR));
175 if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH)
176 toAllocate = enumData->prefmaxlen;
177 NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf);
179 if (*(enumData->pbuf))
183 if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH)
184 spaceFor = totalLANAs;
186 spaceFor = enumData->prefmaxlen /
187 (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME +
188 MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
189 if (enumData->n_read < spaceFor)
191 PWKSTA_TRANSPORT_INFO_0 ti;
192 LMSTR transport_name, transport_addr;
195 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) +
196 enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0));
197 transport_name = (LMSTR)(*(enumData->pbuf) +
198 totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) +
199 enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR));
200 transport_addr = (LMSTR)(*(enumData->pbuf) +
201 totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) +
202 MAX_TRANSPORT_NAME * sizeof(WCHAR)) +
203 enumData->n_read * MAX_TRANSPORT_ADDR * sizeof(WCHAR));
205 ifRow.dwIndex = data->ifIndex;
207 ti->wkti0_quality_of_service = 0;
208 ti->wkti0_number_of_vcs = 0;
209 ti->wkti0_transport_name = transport_name;
210 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME,
212 ti->wkti0_transport_address = transport_addr;
213 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR,
215 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
216 ti->wkti0_wan_ish = TRUE;
218 ti->wkti0_wan_ish = FALSE;
219 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti);
220 TRACE("transport_name at %p %s\n",
221 ti->wkti0_transport_name,
222 debugstr_w(ti->wkti0_transport_name));
223 TRACE("transport_address at %p %s\n",
224 ti->wkti0_transport_address,
225 debugstr_w(ti->wkti0_transport_address));
227 enumData->ret = NERR_Success;
232 enumData->ret = ERROR_MORE_DATA;
238 enumData->ret = ERROR_OUTOFMEMORY;
247 /**********************************************************************/
249 NET_API_STATUS WINAPI
250 NetWkstaTransportEnum(LMSTR ServerName, DWORD level, PBYTE* pbuf,
251 DWORD prefmaxlen, LPDWORD read_entries,
252 PDWORD total_entries, PDWORD hresume)
256 TRACE(":%s, 0x%08x, %p, 0x%08x, %p, %p, %p\n", debugstr_w(ServerName),
257 level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
258 if (!NETAPI_IsLocalComputer(ServerName))
260 FIXME(":not implemented for non-local computers\n");
261 ret = ERROR_INVALID_LEVEL;
265 if (hresume && *hresume)
267 FIXME(":resume handle not implemented\n");
268 return ERROR_INVALID_LEVEL;
273 case 0: /* transport info */
276 struct WkstaTransportEnumData enumData;
278 if (NetBIOSNumAdapters() == 0)
279 return ERROR_NETWORK_UNREACHABLE;
281 return STATUS_ACCESS_VIOLATION;
282 if (!total_entries || !pbuf)
283 return RPC_X_NULL_REF_POINTER;
285 enumData.prefmaxlen = prefmaxlen;
286 enumData.pbuf = pbuf;
287 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG));
288 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback,
290 *read_entries = enumData.n_read;
291 *total_entries = enumData.n_adapt;
292 if (hresume) *hresume= 0;
297 TRACE("Invalid level %d is specified\n", level);
298 ret = ERROR_INVALID_LEVEL;
305 /************************************************************
306 * NetWkstaUserGetInfo (NETAPI32.@)
308 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LMSTR reserved, DWORD level,
311 NET_API_STATUS nastatus;
313 TRACE("(%s, %d, %p)\n", debugstr_w(reserved), level, bufptr);
318 PWKSTA_USER_INFO_0 ui;
319 DWORD dwSize = UNLEN + 1;
322 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
324 if (nastatus != NERR_Success)
325 return ERROR_NOT_ENOUGH_MEMORY;
327 ui = (PWKSTA_USER_INFO_0) *bufptr;
328 ui->wkui0_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
331 if (!GetUserNameW(ui->wkui0_username, &dwSize))
333 NetApiBufferFree(ui);
334 return ERROR_NOT_ENOUGH_MEMORY;
337 nastatus = NetApiBufferReallocate(
338 *bufptr, sizeof(WKSTA_USER_INFO_0) +
339 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
341 if (nastatus != NERR_Success)
349 PWKSTA_USER_INFO_1 ui;
350 PWKSTA_USER_INFO_0 ui0;
351 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
352 LSA_HANDLE PolicyHandle;
353 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
356 /* sizes of the field buffers in WCHARS */
357 int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
359 FIXME("Level 1 processing is partially implemented\n");
363 /* get some information first to estimate size of the buffer */
365 nastatus = NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
366 if (nastatus != NERR_Success)
368 username_sz = lstrlenW(ui0->wkui0_username) + 1;
370 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
371 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
372 POLICY_VIEW_LOCAL_INFORMATION,
374 if (NtStatus != STATUS_SUCCESS)
376 TRACE("LsaOpenPolicyFailed with NT status %x\n",
377 LsaNtStatusToWinError(NtStatus));
378 NetApiBufferFree(ui0);
379 return ERROR_NOT_ENOUGH_MEMORY;
381 LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
382 (PVOID*) &DomainInfo);
383 logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
384 LsaClose(PolicyHandle);
387 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
388 (username_sz + logon_domain_sz +
389 oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
391 if (nastatus != NERR_Success) {
392 NetApiBufferFree(ui0);
395 ui = (WKSTA_USER_INFO_1 *) *bufptr;
396 ui->wkui1_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
397 ui->wkui1_logon_domain = (LMSTR) (
398 ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
399 ui->wkui1_oth_domains = (LMSTR) (
400 ((PBYTE) ui->wkui1_logon_domain) +
401 logon_domain_sz * sizeof(WCHAR));
402 ui->wkui1_logon_server = (LMSTR) (
403 ((PBYTE) ui->wkui1_oth_domains) +
404 oth_domains_sz * sizeof(WCHAR));
407 lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
408 NetApiBufferFree(ui0);
410 lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
412 LsaFreeMemory(DomainInfo);
414 /* FIXME. Not implemented. Populated with empty strings */
415 ui->wkui1_oth_domains[0] = 0;
416 ui->wkui1_logon_server[0] = 0;
421 PWKSTA_USER_INFO_1101 ui;
424 FIXME("Stub. Level 1101 processing is not implemented\n");
425 /* FIXME see also wkui1_oth_domains for level 1 */
428 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
430 if (nastatus != NERR_Success)
432 ui = (PWKSTA_USER_INFO_1101) *bufptr;
433 ui->wkui1101_oth_domains = (LMSTR)(ui + 1);
436 ui->wkui1101_oth_domains[0] = 0;
440 TRACE("Invalid level %d is specified\n", level);
441 return ERROR_INVALID_LEVEL;
446 /************************************************************
447 * NetWkstaUserEnum (NETAPI32.@)
449 NET_API_STATUS WINAPI
450 NetWkstaUserEnum(LMSTR servername, DWORD level, LPBYTE* bufptr,
451 DWORD prefmaxlen, LPDWORD entriesread,
452 LPDWORD totalentries, LPDWORD resumehandle)
454 FIXME("(%s, %d, %p, %d, %p, %p, %p): stub!\n", debugstr_w(servername),
455 level, bufptr, prefmaxlen, entriesread, totalentries, resumehandle);
456 return ERROR_INVALID_PARAMETER;
459 /************************************************************
460 * NetpGetComputerName (NETAPI32.@)
462 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
464 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
466 TRACE("(%p)\n", Buffer);
467 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
468 if (GetComputerNameW(*Buffer, &dwSize))
470 return NetApiBufferReallocate(
471 *Buffer, (dwSize + 1) * sizeof(WCHAR),
476 NetApiBufferFree(*Buffer);
477 return ERROR_NOT_ENOUGH_MEMORY;
481 NET_API_STATUS WINAPI I_NetNameCompare(LPVOID p1, LPWSTR wkgrp, LPWSTR comp,
482 LPVOID p4, LPVOID p5)
484 FIXME("(%p %s %s %p %p): stub\n", p1, debugstr_w(wkgrp), debugstr_w(comp),
486 return ERROR_INVALID_PARAMETER;
489 NET_API_STATUS WINAPI I_NetNameValidate(LPVOID p1, LPWSTR wkgrp, LPVOID p3,
492 FIXME("(%p %s %p %p): stub\n", p1, debugstr_w(wkgrp), p3, p4);
493 return ERROR_INVALID_PARAMETER;
496 NET_API_STATUS WINAPI NetWkstaGetInfo( LMSTR servername, DWORD level,
501 TRACE("%s %d %p\n", debugstr_w( servername ), level, bufptr );
504 if (!NETAPI_IsLocalComputer(servername))
506 FIXME("remote computers not supported\n");
507 return ERROR_INVALID_LEVEL;
510 if (!bufptr) return ERROR_INVALID_PARAMETER;
518 static const WCHAR lanroot[] = {'c',':','\\','l','a','n','m','a','n',0}; /* FIXME */
519 DWORD computerNameLen, domainNameLen, size;
520 WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
521 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
522 LSA_HANDLE PolicyHandle;
525 computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
526 GetComputerNameW(computerName, &computerNameLen);
527 computerNameLen++; /* include NULL terminator */
529 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
530 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
531 POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
532 if (NtStatus != STATUS_SUCCESS)
533 ret = LsaNtStatusToWinError(NtStatus);
536 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
538 LsaQueryInformationPolicy(PolicyHandle,
539 PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
540 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
541 size = sizeof(WKSTA_INFO_102) + computerNameLen * sizeof(WCHAR)
542 + domainNameLen * sizeof(WCHAR) + sizeof(lanroot);
543 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
544 if (ret == NERR_Success)
546 /* INFO_100 and INFO_101 structures are subsets of INFO_102 */
547 PWKSTA_INFO_102 info = (PWKSTA_INFO_102)*bufptr;
548 OSVERSIONINFOW verInfo;
550 info->wki102_platform_id = PLATFORM_ID_NT;
551 info->wki102_computername = (LMSTR)(*bufptr +
552 sizeof(WKSTA_INFO_102));
553 memcpy(info->wki102_computername, computerName,
554 computerNameLen * sizeof(WCHAR));
555 info->wki102_langroup = info->wki102_computername + computerNameLen;
556 memcpy(info->wki102_langroup, DomainInfo->DomainName.Buffer,
557 domainNameLen * sizeof(WCHAR));
558 info->wki102_lanroot = info->wki102_langroup + domainNameLen;
559 memcpy(info->wki102_lanroot, lanroot, sizeof(lanroot));
560 memset(&verInfo, 0, sizeof(verInfo));
561 verInfo.dwOSVersionInfoSize = sizeof(verInfo);
562 GetVersionExW(&verInfo);
563 info->wki102_ver_major = verInfo.dwMajorVersion;
564 info->wki102_ver_minor = verInfo.dwMinorVersion;
565 info->wki102_logged_on_users = 1;
567 LsaFreeMemory(DomainInfo);
568 LsaClose(PolicyHandle);
574 FIXME("level %d unimplemented\n", level);
575 ret = ERROR_INVALID_LEVEL;
580 /************************************************************
581 * NetGetJoinInformation (NETAPI32.@)
583 NET_API_STATUS NET_API_FUNCTION NetGetJoinInformation(
586 PNETSETUP_JOIN_STATUS type)
588 FIXME("Stub %s %p %p\n", wine_dbgstr_w(Server), Name, type);
591 *type = NetSetupUnknownStatus;