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"
27 #define WIN32_NO_STATUS
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
46 /************************************************************
47 * NETAPI_IsLocalComputer
49 * Checks whether the server name indicates local machine.
51 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName)
57 else if (ServerName[0] == '\0')
61 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
65 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
66 Result = GetComputerNameW(buf, &dwSize);
67 if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
69 Result = Result && !lstrcmpW(ServerName, buf);
70 NetApiBufferFree(buf);
76 static void wprint_mac(WCHAR* buffer, int len, PMIB_IFROW ifRow)
91 for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
93 val = ifRow->bPhysAddr[i];
95 buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10);
97 buffer[2*i] = (WCHAR)((val >>4) + '0');
99 buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10);
101 buffer[2*i+1] = (WCHAR)((val & 0xf) + '0');
103 buffer[2*i]=(WCHAR)0;
106 /* Theoretically this could be too short, except that MS defines
107 * MAX_ADAPTER_NAME as 128, and MAX_INTERFACE_NAME_LEN as 256, and both
108 * represent a count of WCHARs, so even with an extroardinarily long header
109 * this will be plenty
111 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN
112 #define MAX_TRANSPORT_ADDR 13
114 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_"
115 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_"
117 static void wprint_name(WCHAR *buffer, int len, ULONG transport,
131 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
132 name = NBT_TRANSPORT_NAME_HEADER;
134 name = UNKNOWN_TRANSPORT_NAME_HEADER;
136 for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++)
138 for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++)
143 /***********************************************************************
144 * NetWkstaTransportEnum (NETAPI32.@)
147 struct WkstaTransportEnumData
156 /**********************************************************************/
158 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
159 ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
162 struct WkstaTransportEnumData *enumData = (struct WkstaTransportEnumData *)
165 if (enumData && enumData->pbuf)
171 enumData->n_adapt = totalLANAs;
172 enumData->n_read = 0;
174 toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0)
175 + MAX_TRANSPORT_NAME * sizeof(WCHAR) +
176 MAX_TRANSPORT_ADDR * sizeof(WCHAR));
177 if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH)
178 toAllocate = enumData->prefmaxlen;
179 NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf);
181 if (*(enumData->pbuf))
185 if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH)
186 spaceFor = totalLANAs;
188 spaceFor = enumData->prefmaxlen /
189 (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME +
190 MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
191 if (enumData->n_read < spaceFor)
193 PWKSTA_TRANSPORT_INFO_0 ti;
194 LPWSTR transport_name, transport_addr;
197 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) +
198 enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0));
199 transport_name = (LPWSTR)(*(enumData->pbuf) +
200 totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) +
201 enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR));
202 transport_addr = (LPWSTR)(*(enumData->pbuf) +
203 totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) +
204 MAX_TRANSPORT_NAME * sizeof(WCHAR)) +
205 enumData->n_read * MAX_TRANSPORT_ADDR * sizeof(WCHAR));
207 ifRow.dwIndex = data->ifIndex;
209 ti->wkti0_quality_of_service = 0;
210 ti->wkti0_number_of_vcs = 0;
211 ti->wkti0_transport_name = transport_name;
212 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME,
214 ti->wkti0_transport_address = transport_addr;
215 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR,
217 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
218 ti->wkti0_wan_ish = TRUE;
220 ti->wkti0_wan_ish = FALSE;
221 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti);
222 TRACE("transport_name at %p %s\n",
223 ti->wkti0_transport_name,
224 debugstr_w(ti->wkti0_transport_name));
225 TRACE("transport_address at %p %s\n",
226 ti->wkti0_transport_address,
227 debugstr_w(ti->wkti0_transport_address));
229 enumData->ret = NERR_Success;
234 enumData->ret = ERROR_MORE_DATA;
240 enumData->ret = ERROR_OUTOFMEMORY;
249 /**********************************************************************/
251 NET_API_STATUS WINAPI
252 NetWkstaTransportEnum(LPWSTR ServerName, DWORD level, PBYTE* pbuf,
253 DWORD prefmaxlen, LPDWORD read_entries,
254 PDWORD total_entries, PDWORD hresume)
258 TRACE(":%s, 0x%08lx, %p, 0x%08lx, %p, %p, %p\n", debugstr_w(ServerName),
259 level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
260 if (!NETAPI_IsLocalComputer(ServerName))
262 FIXME(":not implemented for non-local computers\n");
263 ret = ERROR_INVALID_LEVEL;
267 if (hresume && *hresume)
269 FIXME(":resume handle not implemented\n");
270 return ERROR_INVALID_LEVEL;
275 case 0: /* transport info */
278 struct WkstaTransportEnumData enumData;
280 if (NetBIOSNumAdapters() == 0)
281 return ERROR_NETWORK_UNREACHABLE;
283 return STATUS_ACCESS_VIOLATION;
284 if (!total_entries || !pbuf)
285 return RPC_X_NULL_REF_POINTER;
287 enumData.prefmaxlen = prefmaxlen;
288 enumData.pbuf = pbuf;
289 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG));
290 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback,
292 *read_entries = enumData.n_read;
293 *total_entries = enumData.n_adapt;
294 if (hresume) *hresume= 0;
299 ERR("Invalid level %ld is specified\n", level);
300 ret = ERROR_INVALID_LEVEL;
307 /************************************************************
308 * NetWkstaUserGetInfo (NETAPI32.@)
310 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LPWSTR reserved, DWORD level,
313 TRACE("(%s, %ld, %p)\n", debugstr_w(reserved), level, bufptr);
318 PWKSTA_USER_INFO_0 ui;
319 DWORD dwSize = UNLEN + 1;
322 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
325 ui = (PWKSTA_USER_INFO_0) *bufptr;
326 ui->wkui0_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
329 if (!GetUserNameW(ui->wkui0_username, &dwSize))
331 NetApiBufferFree(ui);
332 return ERROR_NOT_ENOUGH_MEMORY;
335 NetApiBufferReallocate(
336 *bufptr, sizeof(WKSTA_USER_INFO_0) +
337 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
344 PWKSTA_USER_INFO_1 ui;
345 PWKSTA_USER_INFO_0 ui0;
347 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
348 LSA_HANDLE PolicyHandle;
349 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
352 /* sizes of the field buffers in WCHARS */
353 int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
355 FIXME("Level 1 processing is partially implemented\n");
359 /* get some information first to estimate size of the buffer */
361 NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
362 username_sz = lstrlenW(ui0->wkui0_username) + 1;
364 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
365 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
366 POLICY_VIEW_LOCAL_INFORMATION,
368 if (NtStatus != STATUS_SUCCESS)
370 ERR("LsaOpenPolicyFailed with NT status %lx\n",
371 LsaNtStatusToWinError(NtStatus));
372 NetApiBufferFree(ui0);
373 return ERROR_NOT_ENOUGH_MEMORY;
375 LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
376 (PVOID*) &DomainInfo);
377 logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
378 LsaClose(PolicyHandle);
381 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
382 (username_sz + logon_domain_sz +
383 oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
385 ui = (WKSTA_USER_INFO_1 *) *bufptr;
386 ui->wkui1_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
387 ui->wkui1_logon_domain = (LPWSTR) (
388 ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
389 ui->wkui1_oth_domains = (LPWSTR) (
390 ((PBYTE) ui->wkui1_logon_domain) +
391 logon_domain_sz * sizeof(WCHAR));
392 ui->wkui1_logon_server = (LPWSTR) (
393 ((PBYTE) ui->wkui1_oth_domains) +
394 oth_domains_sz * sizeof(WCHAR));
397 dwSize = username_sz;
398 lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
399 NetApiBufferFree(ui0);
401 lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
403 LsaFreeMemory(DomainInfo);
405 /* FIXME. Not implemented. Populated with empty strings */
406 ui->wkui1_oth_domains[0] = 0;
407 ui->wkui1_logon_server[0] = 0;
412 PWKSTA_USER_INFO_1101 ui;
415 FIXME("Stub. Level 1101 processing is not implemented\n");
416 /* FIXME see also wkui1_oth_domains for level 1 */
419 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
422 ui = (PWKSTA_USER_INFO_1101) *bufptr;
423 ui->wkui1101_oth_domains = (LPWSTR)(ui + 1);
426 ui->wkui1101_oth_domains[0] = 0;
430 ERR("Invalid level %ld is specified\n", level);
431 return ERROR_INVALID_LEVEL;
436 /************************************************************
437 * NetpGetComputerName (NETAPI32.@)
439 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
441 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
443 TRACE("(%p)\n", Buffer);
444 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
445 if (GetComputerNameW(*Buffer, &dwSize))
447 NetApiBufferReallocate(
448 *Buffer, dwSize * sizeof(WCHAR),
454 NetApiBufferFree(*Buffer);
455 return ERROR_NOT_ENOUGH_MEMORY;
459 NET_API_STATUS WINAPI I_NetNameCompare(LPVOID p1, LPWSTR wkgrp, LPWSTR comp,
460 LPVOID p4, LPVOID p5)
462 FIXME("(%p %s %s %p %p): stub\n", p1, debugstr_w(wkgrp), debugstr_w(comp),
464 return ERROR_INVALID_PARAMETER;
467 NET_API_STATUS WINAPI I_NetNameValidate(LPVOID p1, LPWSTR wkgrp, LPVOID p3,
470 FIXME("(%p %s %p %p): stub\n", p1, debugstr_w(wkgrp), p3, p4);
471 return ERROR_INVALID_PARAMETER;
474 NET_API_STATUS WINAPI NetWkstaGetInfo( LPWSTR servername, DWORD level,
479 TRACE("%s %ld %p\n", debugstr_w( servername ), level, bufptr );
482 if (!NETAPI_IsLocalComputer(servername))
484 FIXME("remote computers not supported\n");
485 return ERROR_INVALID_LEVEL;
488 if (!bufptr) return ERROR_INVALID_PARAMETER;
494 DWORD computerNameLen, domainNameLen, size;
495 WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
496 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
497 LSA_HANDLE PolicyHandle;
500 computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
501 GetComputerNameW(computerName, &computerNameLen);
502 computerNameLen++; /* include NULL terminator */
504 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
505 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
506 POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
507 if (NtStatus != STATUS_SUCCESS)
508 ret = LsaNtStatusToWinError(NtStatus);
511 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
513 LsaQueryInformationPolicy(PolicyHandle,
514 PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
515 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
516 size = sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR)
517 + domainNameLen * sizeof(WCHAR);
518 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
519 if (ret == NERR_Success)
521 PWKSTA_INFO_100 info = (PWKSTA_INFO_100)*bufptr;
522 OSVERSIONINFOW verInfo;
524 info->wki100_platform_id = PLATFORM_ID_NT;
525 info->wki100_computername = (LPWSTR)(*bufptr +
526 sizeof(WKSTA_INFO_100));
527 memcpy(info->wki100_computername, computerName,
528 computerNameLen * sizeof(WCHAR));
529 info->wki100_langroup = (LPWSTR)(*bufptr +
530 sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR));
531 memcpy(info->wki100_langroup, DomainInfo->DomainName.Buffer,
532 domainNameLen * sizeof(WCHAR));
533 memset(&verInfo, 0, sizeof(verInfo));
534 verInfo.dwOSVersionInfoSize = sizeof(verInfo);
535 GetVersionExW(&verInfo);
536 info->wki100_ver_major = verInfo.dwMajorVersion;
537 info->wki100_ver_minor = verInfo.dwMinorVersion;
539 LsaFreeMemory(DomainInfo);
540 LsaClose(PolicyHandle);
546 FIXME("level %ld unimplemented\n", level);
547 ret = ERROR_INVALID_LEVEL;
552 /************************************************************
553 * NetGetJoinInformation (NETAPI32.@)
555 NET_API_STATUS NET_API_FUNCTION NetGetJoinInformation(
558 PNETSETUP_JOIN_STATUS type)
560 FIXME("Stub %s %p %p\n", wine_dbgstr_w(Server), Name, type);
563 *type = NetSetupUnknownStatus;