Partially implement NetWkstaGetInfo.
[wine.git] / dlls / netapi32 / wksta.c
blobf0c5565d7d1f720190499821876c00566fd8742e
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
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "nb30.h"
26 #include "lmcons.h"
27 #include "lmapibuf.h"
28 #include "lmerr.h"
29 #include "lmwksta.h"
30 #include "iphlpapi.h"
31 #include "winerror.h"
32 #include "ntstatus.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "ntsecapi.h"
36 #include "netbios.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
41 /************************************************************
42 * NETAPI_IsLocalComputer
44 * Checks whether the server name indicates local machine.
46 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName)
48 if (!ServerName)
50 return TRUE;
52 else
54 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
55 BOOL Result;
56 LPWSTR buf;
58 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
59 Result = GetComputerNameW(buf, &dwSize);
60 if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
61 ServerName += 2;
62 Result = Result && !lstrcmpW(ServerName, buf);
63 NetApiBufferFree(buf);
65 return Result;
69 static void wprint_mac(WCHAR* buffer, int len, PMIB_IFROW ifRow)
71 int i;
72 unsigned char val;
74 if (!buffer)
75 return;
76 if (len < 1)
77 return;
78 if (!ifRow)
80 *buffer = '\0';
81 return;
84 for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
86 val = ifRow->bPhysAddr[i];
87 if ((val >>4) >9)
88 buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10);
89 else
90 buffer[2*i] = (WCHAR)((val >>4) + '0');
91 if ((val & 0xf ) >9)
92 buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10);
93 else
94 buffer[2*i+1] = (WCHAR)((val & 0xf) + '0');
96 buffer[2*i]=(WCHAR)0;
99 /* Theoretically this could be too short, except that MS defines
100 * MAX_ADAPTER_NAME as 128, and MAX_INTERFACE_NAME_LEN as 256, and both
101 * represent a count of WCHARs, so even with an extroardinarily long header
102 * this will be plenty
104 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN
105 #define MAX_TRANSPORT_ADDR 13
107 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_"
108 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_"
110 static void wprint_name(WCHAR *buffer, int len, ULONG transport,
111 PMIB_IFROW ifRow)
113 WCHAR *ptr1, *ptr2;
114 const char *name;
116 if (!buffer)
117 return;
118 if (!ifRow)
120 *buffer = '\0';
121 return;
124 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
125 name = NBT_TRANSPORT_NAME_HEADER;
126 else
127 name = UNKNOWN_TRANSPORT_NAME_HEADER;
129 for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++)
130 *ptr1 = *name;
131 for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++)
132 *ptr1 = *ptr2;
133 *ptr1 = '\0';
136 struct WkstaTransportEnumData
138 UCHAR n_adapt;
139 UCHAR n_read;
140 DWORD prefmaxlen;
141 LPBYTE *pbuf;
142 NET_API_STATUS ret;
145 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
146 ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
148 BOOL ret;
149 struct WkstaTransportEnumData *enumData = (struct WkstaTransportEnumData *)
150 closure;
152 if (enumData && enumData->pbuf)
154 if (lanaIndex == 0)
156 DWORD toAllocate;
158 enumData->n_adapt = totalLANAs;
159 enumData->n_read = 0;
161 toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0)
162 + MAX_TRANSPORT_NAME * sizeof(WCHAR) +
163 MAX_TRANSPORT_ADDR * sizeof(WCHAR));
164 if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH)
165 toAllocate = enumData->prefmaxlen;
166 NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf);
168 if (*(enumData->pbuf))
170 UCHAR spaceFor;
172 if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH)
173 spaceFor = totalLANAs;
174 else
175 spaceFor = enumData->prefmaxlen /
176 (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME +
177 MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
178 if (enumData->n_read < spaceFor)
180 PWKSTA_TRANSPORT_INFO_0 ti;
181 LPWSTR transport_name, transport_addr;
182 MIB_IFROW ifRow;
184 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) +
185 enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0));
186 transport_name = (LPWSTR)(*(enumData->pbuf) +
187 totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) +
188 enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR));
189 transport_addr = (LPWSTR)(*(enumData->pbuf) +
190 totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) +
191 MAX_TRANSPORT_NAME * sizeof(WCHAR)) +
192 (enumData->n_read + MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
194 ifRow.dwIndex = data->ifIndex;
195 GetIfEntry(&ifRow);
196 ti->wkti0_quality_of_service = 0;
197 ti->wkti0_number_of_vcs = 0;
198 ti->wkti0_transport_name = transport_name;
199 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME,
200 transport, &ifRow);
201 ti->wkti0_transport_address = transport_addr;
202 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR,
203 &ifRow);
204 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
205 ti->wkti0_wan_ish = TRUE;
206 else
207 ti->wkti0_wan_ish = FALSE;
208 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti);
209 TRACE("transport_name at %p %s\n",
210 ti->wkti0_transport_name,
211 debugstr_w(ti->wkti0_transport_name));
212 TRACE("transport_address at %p %s\n",
213 ti->wkti0_transport_address,
214 debugstr_w(ti->wkti0_transport_address));
215 enumData->n_read++;
216 enumData->ret = NERR_Success;
217 ret = TRUE;
219 else
221 enumData->ret = ERROR_MORE_DATA;
222 ret = FALSE;
225 else
227 enumData->ret = ERROR_OUTOFMEMORY;
228 ret = FALSE;
231 else
232 ret = FALSE;
233 return ret;
236 NET_API_STATUS WINAPI
237 NetWkstaTransportEnum(LPCWSTR ServerName, DWORD level, LPBYTE* pbuf,
238 DWORD prefmaxlen, LPDWORD read_entries,
239 LPDWORD total_entries, LPDWORD hresume)
241 NET_API_STATUS ret;
243 TRACE(":%s, 0x%08lx, %p, 0x%08lx, %p, %p, %p\n", debugstr_w(ServerName),
244 level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
245 if (!NETAPI_IsLocalComputer(ServerName))
247 FIXME(":not implemented for non-local computers\n");
248 ret = ERROR_INVALID_LEVEL;
250 else
252 if (hresume && *hresume)
254 FIXME(":resume handle not implemented\n");
255 return ERROR_INVALID_LEVEL;
258 switch (level)
260 case 0: /* transport info */
262 ULONG allTransports;
263 struct WkstaTransportEnumData enumData;
265 if (NetBIOSNumAdapters() == 0)
266 return ERROR_NETWORK_UNREACHABLE;
267 if (!read_entries)
268 return STATUS_ACCESS_VIOLATION;
269 if (!total_entries || !pbuf)
270 return RPC_X_NULL_REF_POINTER;
272 enumData.prefmaxlen = prefmaxlen;
273 enumData.pbuf = pbuf;
274 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG));
275 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback,
276 &enumData);
277 *read_entries = enumData.n_read;
278 *total_entries = enumData.n_adapt;
279 if (hresume) *hresume= 0;
280 ret = enumData.ret;
281 break;
283 default:
284 ERR("Invalid level %ld is specified\n", level);
285 ret = ERROR_INVALID_LEVEL;
288 return ret;
292 /************************************************************
293 * NetWkstaUserGetInfo (NETAPI32.@)
295 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LPWSTR reserved, DWORD level,
296 PBYTE* bufptr)
298 TRACE("(%s, %ld, %p)\n", debugstr_w(reserved), level, bufptr);
299 switch (level)
301 case 0:
303 PWKSTA_USER_INFO_0 ui;
304 DWORD dwSize = UNLEN + 1;
306 /* set up buffer */
307 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
308 (LPVOID *) bufptr);
310 ui = (PWKSTA_USER_INFO_0) *bufptr;
311 ui->wkui0_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
313 /* get data */
314 if (!GetUserNameW(ui->wkui0_username, &dwSize))
316 NetApiBufferFree(ui);
317 return ERROR_NOT_ENOUGH_MEMORY;
319 else
320 NetApiBufferReallocate(
321 *bufptr, sizeof(WKSTA_USER_INFO_0) +
322 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
323 (LPVOID *) bufptr);
324 break;
327 case 1:
329 PWKSTA_USER_INFO_1 ui;
330 PWKSTA_USER_INFO_0 ui0;
331 DWORD dwSize;
332 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
333 LSA_HANDLE PolicyHandle;
334 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
335 NTSTATUS NtStatus;
337 /* sizes of the field buffers in WCHARS */
338 int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
340 FIXME("Level 1 processing is partially implemented\n");
341 oth_domains_sz = 1;
342 logon_server_sz = 1;
344 /* get some information first to estimate size of the buffer */
345 ui0 = NULL;
346 NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
347 username_sz = lstrlenW(ui0->wkui0_username) + 1;
349 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
350 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
351 POLICY_VIEW_LOCAL_INFORMATION,
352 &PolicyHandle);
353 if (NtStatus != STATUS_SUCCESS)
355 ERR("LsaOpenPolicyFailed with NT status %lx\n",
356 LsaNtStatusToWinError(NtStatus));
357 NetApiBufferFree(ui0);
358 return ERROR_NOT_ENOUGH_MEMORY;
360 LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
361 (PVOID*) &DomainInfo);
362 logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
363 LsaClose(PolicyHandle);
365 /* set up buffer */
366 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
367 (username_sz + logon_domain_sz +
368 oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
369 (LPVOID *) bufptr);
370 ui = (WKSTA_USER_INFO_1 *) *bufptr;
371 ui->wkui1_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
372 ui->wkui1_logon_domain = (LPWSTR) (
373 ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
374 ui->wkui1_oth_domains = (LPWSTR) (
375 ((PBYTE) ui->wkui1_logon_domain) +
376 logon_domain_sz * sizeof(WCHAR));
377 ui->wkui1_logon_server = (LPWSTR) (
378 ((PBYTE) ui->wkui1_oth_domains) +
379 oth_domains_sz * sizeof(WCHAR));
381 /* get data */
382 dwSize = username_sz;
383 lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
384 NetApiBufferFree(ui0);
386 lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
387 logon_domain_sz);
388 LsaFreeMemory(DomainInfo);
390 /* FIXME. Not implemented. Populated with empty strings */
391 ui->wkui1_oth_domains[0] = 0;
392 ui->wkui1_logon_server[0] = 0;
393 break;
395 case 1101:
397 PWKSTA_USER_INFO_1101 ui;
398 DWORD dwSize = 1;
400 FIXME("Stub. Level 1101 processing is not implemented\n");
401 /* FIXME see also wkui1_oth_domains for level 1 */
403 /* set up buffer */
404 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
405 (LPVOID *) bufptr);
407 ui = (PWKSTA_USER_INFO_1101) *bufptr;
408 ui->wkui1101_oth_domains = (LPWSTR)(ui + 1);
410 /* get data */
411 ui->wkui1101_oth_domains[0] = 0;
412 break;
414 default:
415 ERR("Invalid level %ld is specified\n", level);
416 return ERROR_INVALID_LEVEL;
418 return NERR_Success;
421 /************************************************************
422 * NetpGetComputerName (NETAPI32.@)
424 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
426 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
428 TRACE("(%p)\n", Buffer);
429 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
430 if (GetComputerNameW(*Buffer, &dwSize))
432 NetApiBufferReallocate(
433 *Buffer, dwSize * sizeof(WCHAR),
434 (LPVOID *) Buffer);
435 return NERR_Success;
437 else
439 NetApiBufferFree(*Buffer);
440 return ERROR_NOT_ENOUGH_MEMORY;
444 NET_API_STATUS WINAPI NetWkstaGetInfo( LPWSTR servername, DWORD level,
445 LPBYTE* bufptr)
447 NET_API_STATUS ret;
449 TRACE("%p %ld %p\n", debugstr_w( servername ), level, bufptr );
450 if (servername)
452 FIXME("remote computers not supported\n");
453 return ERROR_INVALID_LEVEL;
455 if (!bufptr) return ERROR_INVALID_PARAMETER;
457 switch (level)
459 case 100:
461 DWORD computerNameLen, domainNameLen, size;
462 WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
463 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
464 LSA_HANDLE PolicyHandle;
465 NTSTATUS NtStatus;
467 computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
468 GetComputerNameW(computerName, &computerNameLen);
469 computerNameLen++; /* include NULL terminator */
471 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
472 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
473 POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
474 if (NtStatus != STATUS_SUCCESS)
475 ret = LsaNtStatusToWinError(NtStatus);
476 else
478 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
480 LsaQueryInformationPolicy(PolicyHandle,
481 PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
482 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
483 size = sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR)
484 + domainNameLen * sizeof(WCHAR);
485 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
486 if (ret == NERR_Success)
488 PWKSTA_INFO_100 info = (PWKSTA_INFO_100)*bufptr;
489 OSVERSIONINFOW verInfo;
491 info->wki100_platform_id = PLATFORM_ID_NT;
492 info->wki100_computername = (LPWSTR)(*bufptr +
493 sizeof(WKSTA_INFO_100));
494 memcpy(info->wki100_computername, computerName,
495 computerNameLen * sizeof(WCHAR));
496 info->wki100_langroup = (LPWSTR)(*bufptr +
497 sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR));
498 memcpy(info->wki100_langroup, DomainInfo->DomainName.Buffer,
499 domainNameLen * sizeof(WCHAR));
500 memset(&verInfo, 0, sizeof(verInfo));
501 verInfo.dwOSVersionInfoSize = sizeof(verInfo);
502 GetVersionExW(&verInfo);
503 info->wki100_ver_major = verInfo.dwMajorVersion;
504 info->wki100_ver_minor = verInfo.dwMinorVersion;
506 LsaFreeMemory(DomainInfo);
507 LsaClose(PolicyHandle);
509 break;
512 default:
513 FIXME("level %ld unimplemented\n", level);
514 ret = ERROR_INVALID_LEVEL;
516 return ret;