- call CertFreeCertificateContext from CertDeleteCertificateFromStore
[wine/multimedia.git] / dlls / netapi32 / wksta.c
blob5a3a318eaa31f3600c340a028491fea1b7c21054
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 "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winsock2.h"
29 #include "nb30.h"
30 #include "lmcons.h"
31 #include "lmapibuf.h"
32 #include "lmerr.h"
33 #include "lmwksta.h"
34 #include "iphlpapi.h"
35 #include "winerror.h"
36 #include "ntstatus.h"
37 #include "winreg.h"
38 #include "winternl.h"
39 #include "ntsecapi.h"
40 #include "netbios.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)
52 if (!ServerName)
54 return TRUE;
56 else if (ServerName[0] == '\0')
57 return TRUE;
58 else
60 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
61 BOOL Result;
62 LPWSTR buf;
64 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
65 Result = GetComputerNameW(buf, &dwSize);
66 if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
67 ServerName += 2;
68 Result = Result && !lstrcmpW(ServerName, buf);
69 NetApiBufferFree(buf);
71 return Result;
75 static void wprint_mac(WCHAR* buffer, int len, PMIB_IFROW ifRow)
77 int i;
78 unsigned char val;
80 if (!buffer)
81 return;
82 if (len < 1)
83 return;
84 if (!ifRow)
86 *buffer = '\0';
87 return;
90 for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
92 val = ifRow->bPhysAddr[i];
93 if ((val >>4) >9)
94 buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10);
95 else
96 buffer[2*i] = (WCHAR)((val >>4) + '0');
97 if ((val & 0xf ) >9)
98 buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10);
99 else
100 buffer[2*i+1] = (WCHAR)((val & 0xf) + '0');
102 buffer[2*i]=(WCHAR)0;
105 /* Theoretically this could be too short, except that MS defines
106 * MAX_ADAPTER_NAME as 128, and MAX_INTERFACE_NAME_LEN as 256, and both
107 * represent a count of WCHARs, so even with an extroardinarily long header
108 * this will be plenty
110 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN
111 #define MAX_TRANSPORT_ADDR 13
113 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_"
114 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_"
116 static void wprint_name(WCHAR *buffer, int len, ULONG transport,
117 PMIB_IFROW ifRow)
119 WCHAR *ptr1, *ptr2;
120 const char *name;
122 if (!buffer)
123 return;
124 if (!ifRow)
126 *buffer = '\0';
127 return;
130 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
131 name = NBT_TRANSPORT_NAME_HEADER;
132 else
133 name = UNKNOWN_TRANSPORT_NAME_HEADER;
135 for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++)
136 *ptr1 = *name;
137 for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++)
138 *ptr1 = *ptr2;
139 *ptr1 = '\0';
142 /***********************************************************************
143 * NetWkstaTransportEnum (NETAPI32.@)
146 struct WkstaTransportEnumData
148 UCHAR n_adapt;
149 UCHAR n_read;
150 DWORD prefmaxlen;
151 LPBYTE *pbuf;
152 NET_API_STATUS ret;
155 /**********************************************************************/
157 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
158 ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
160 BOOL ret;
161 struct WkstaTransportEnumData *enumData = (struct WkstaTransportEnumData *)
162 closure;
164 if (enumData && enumData->pbuf)
166 if (lanaIndex == 0)
168 DWORD toAllocate;
170 enumData->n_adapt = totalLANAs;
171 enumData->n_read = 0;
173 toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0)
174 + MAX_TRANSPORT_NAME * sizeof(WCHAR) +
175 MAX_TRANSPORT_ADDR * sizeof(WCHAR));
176 if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH)
177 toAllocate = enumData->prefmaxlen;
178 NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf);
180 if (*(enumData->pbuf))
182 UCHAR spaceFor;
184 if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH)
185 spaceFor = totalLANAs;
186 else
187 spaceFor = enumData->prefmaxlen /
188 (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME +
189 MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
190 if (enumData->n_read < spaceFor)
192 PWKSTA_TRANSPORT_INFO_0 ti;
193 LPWSTR transport_name, transport_addr;
194 MIB_IFROW ifRow;
196 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) +
197 enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0));
198 transport_name = (LPWSTR)(*(enumData->pbuf) +
199 totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) +
200 enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR));
201 transport_addr = (LPWSTR)(*(enumData->pbuf) +
202 totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) +
203 MAX_TRANSPORT_NAME * sizeof(WCHAR)) +
204 enumData->n_read * MAX_TRANSPORT_ADDR * sizeof(WCHAR));
206 ifRow.dwIndex = data->ifIndex;
207 GetIfEntry(&ifRow);
208 ti->wkti0_quality_of_service = 0;
209 ti->wkti0_number_of_vcs = 0;
210 ti->wkti0_transport_name = transport_name;
211 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME,
212 transport, &ifRow);
213 ti->wkti0_transport_address = transport_addr;
214 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR,
215 &ifRow);
216 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
217 ti->wkti0_wan_ish = TRUE;
218 else
219 ti->wkti0_wan_ish = FALSE;
220 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti);
221 TRACE("transport_name at %p %s\n",
222 ti->wkti0_transport_name,
223 debugstr_w(ti->wkti0_transport_name));
224 TRACE("transport_address at %p %s\n",
225 ti->wkti0_transport_address,
226 debugstr_w(ti->wkti0_transport_address));
227 enumData->n_read++;
228 enumData->ret = NERR_Success;
229 ret = TRUE;
231 else
233 enumData->ret = ERROR_MORE_DATA;
234 ret = FALSE;
237 else
239 enumData->ret = ERROR_OUTOFMEMORY;
240 ret = FALSE;
243 else
244 ret = FALSE;
245 return ret;
248 /**********************************************************************/
250 NET_API_STATUS WINAPI
251 NetWkstaTransportEnum(LPWSTR ServerName, DWORD level, PBYTE* pbuf,
252 DWORD prefmaxlen, LPDWORD read_entries,
253 PDWORD total_entries, PDWORD hresume)
255 NET_API_STATUS ret;
257 TRACE(":%s, 0x%08lx, %p, 0x%08lx, %p, %p, %p\n", debugstr_w(ServerName),
258 level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
259 if (!NETAPI_IsLocalComputer(ServerName))
261 FIXME(":not implemented for non-local computers\n");
262 ret = ERROR_INVALID_LEVEL;
264 else
266 if (hresume && *hresume)
268 FIXME(":resume handle not implemented\n");
269 return ERROR_INVALID_LEVEL;
272 switch (level)
274 case 0: /* transport info */
276 ULONG allTransports;
277 struct WkstaTransportEnumData enumData;
279 if (NetBIOSNumAdapters() == 0)
280 return ERROR_NETWORK_UNREACHABLE;
281 if (!read_entries)
282 return STATUS_ACCESS_VIOLATION;
283 if (!total_entries || !pbuf)
284 return RPC_X_NULL_REF_POINTER;
286 enumData.prefmaxlen = prefmaxlen;
287 enumData.pbuf = pbuf;
288 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG));
289 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback,
290 &enumData);
291 *read_entries = enumData.n_read;
292 *total_entries = enumData.n_adapt;
293 if (hresume) *hresume= 0;
294 ret = enumData.ret;
295 break;
297 default:
298 ERR("Invalid level %ld is specified\n", level);
299 ret = ERROR_INVALID_LEVEL;
302 return ret;
306 /************************************************************
307 * NetWkstaUserGetInfo (NETAPI32.@)
309 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LPWSTR reserved, DWORD level,
310 PBYTE* bufptr)
312 TRACE("(%s, %ld, %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 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
322 (LPVOID *) bufptr);
324 ui = (PWKSTA_USER_INFO_0) *bufptr;
325 ui->wkui0_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
327 /* get data */
328 if (!GetUserNameW(ui->wkui0_username, &dwSize))
330 NetApiBufferFree(ui);
331 return ERROR_NOT_ENOUGH_MEMORY;
333 else
334 NetApiBufferReallocate(
335 *bufptr, sizeof(WKSTA_USER_INFO_0) +
336 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
337 (LPVOID *) bufptr);
338 break;
341 case 1:
343 PWKSTA_USER_INFO_1 ui;
344 PWKSTA_USER_INFO_0 ui0;
345 DWORD dwSize;
346 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
347 LSA_HANDLE PolicyHandle;
348 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
349 NTSTATUS NtStatus;
351 /* sizes of the field buffers in WCHARS */
352 int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
354 FIXME("Level 1 processing is partially implemented\n");
355 oth_domains_sz = 1;
356 logon_server_sz = 1;
358 /* get some information first to estimate size of the buffer */
359 ui0 = NULL;
360 NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
361 username_sz = lstrlenW(ui0->wkui0_username) + 1;
363 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
364 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
365 POLICY_VIEW_LOCAL_INFORMATION,
366 &PolicyHandle);
367 if (NtStatus != STATUS_SUCCESS)
369 ERR("LsaOpenPolicyFailed with NT status %lx\n",
370 LsaNtStatusToWinError(NtStatus));
371 NetApiBufferFree(ui0);
372 return ERROR_NOT_ENOUGH_MEMORY;
374 LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
375 (PVOID*) &DomainInfo);
376 logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
377 LsaClose(PolicyHandle);
379 /* set up buffer */
380 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
381 (username_sz + logon_domain_sz +
382 oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
383 (LPVOID *) bufptr);
384 ui = (WKSTA_USER_INFO_1 *) *bufptr;
385 ui->wkui1_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
386 ui->wkui1_logon_domain = (LPWSTR) (
387 ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
388 ui->wkui1_oth_domains = (LPWSTR) (
389 ((PBYTE) ui->wkui1_logon_domain) +
390 logon_domain_sz * sizeof(WCHAR));
391 ui->wkui1_logon_server = (LPWSTR) (
392 ((PBYTE) ui->wkui1_oth_domains) +
393 oth_domains_sz * sizeof(WCHAR));
395 /* get data */
396 dwSize = username_sz;
397 lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
398 NetApiBufferFree(ui0);
400 lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
401 logon_domain_sz);
402 LsaFreeMemory(DomainInfo);
404 /* FIXME. Not implemented. Populated with empty strings */
405 ui->wkui1_oth_domains[0] = 0;
406 ui->wkui1_logon_server[0] = 0;
407 break;
409 case 1101:
411 PWKSTA_USER_INFO_1101 ui;
412 DWORD dwSize = 1;
414 FIXME("Stub. Level 1101 processing is not implemented\n");
415 /* FIXME see also wkui1_oth_domains for level 1 */
417 /* set up buffer */
418 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
419 (LPVOID *) bufptr);
421 ui = (PWKSTA_USER_INFO_1101) *bufptr;
422 ui->wkui1101_oth_domains = (LPWSTR)(ui + 1);
424 /* get data */
425 ui->wkui1101_oth_domains[0] = 0;
426 break;
428 default:
429 ERR("Invalid level %ld is specified\n", level);
430 return ERROR_INVALID_LEVEL;
432 return NERR_Success;
435 /************************************************************
436 * NetpGetComputerName (NETAPI32.@)
438 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
440 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
442 TRACE("(%p)\n", Buffer);
443 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
444 if (GetComputerNameW(*Buffer, &dwSize))
446 NetApiBufferReallocate(
447 *Buffer, dwSize * sizeof(WCHAR),
448 (LPVOID *) Buffer);
449 return NERR_Success;
451 else
453 NetApiBufferFree(*Buffer);
454 return ERROR_NOT_ENOUGH_MEMORY;
458 NET_API_STATUS WINAPI I_NetNameCompare(LPVOID p1, LPWSTR wkgrp, LPWSTR comp,
459 LPVOID p4, LPVOID p5)
461 FIXME("(%p %s %s %p %p): stub\n", p1, debugstr_w(wkgrp), debugstr_w(comp),
462 p4, p5);
463 return ERROR_INVALID_PARAMETER;
466 NET_API_STATUS WINAPI I_NetNameValidate(LPVOID p1, LPWSTR wkgrp, LPVOID p3,
467 LPVOID p4)
469 FIXME("(%p %s %p %p): stub\n", p1, debugstr_w(wkgrp), p3, p4);
470 return ERROR_INVALID_PARAMETER;
473 NET_API_STATUS WINAPI NetWkstaGetInfo( LPWSTR servername, DWORD level,
474 LPBYTE* bufptr)
476 NET_API_STATUS ret;
478 TRACE("%s %ld %p\n", debugstr_w( servername ), level, bufptr );
479 if (servername)
481 if (!NETAPI_IsLocalComputer(servername))
483 FIXME("remote computers not supported\n");
484 return ERROR_INVALID_LEVEL;
487 if (!bufptr) return ERROR_INVALID_PARAMETER;
489 switch (level)
491 case 100:
493 DWORD computerNameLen, domainNameLen, size;
494 WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
495 LSA_OBJECT_ATTRIBUTES ObjectAttributes;
496 LSA_HANDLE PolicyHandle;
497 NTSTATUS NtStatus;
499 computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
500 GetComputerNameW(computerName, &computerNameLen);
501 computerNameLen++; /* include NULL terminator */
503 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
504 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
505 POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
506 if (NtStatus != STATUS_SUCCESS)
507 ret = LsaNtStatusToWinError(NtStatus);
508 else
510 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
512 LsaQueryInformationPolicy(PolicyHandle,
513 PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
514 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
515 size = sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR)
516 + domainNameLen * sizeof(WCHAR);
517 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
518 if (ret == NERR_Success)
520 PWKSTA_INFO_100 info = (PWKSTA_INFO_100)*bufptr;
521 OSVERSIONINFOW verInfo;
523 info->wki100_platform_id = PLATFORM_ID_NT;
524 info->wki100_computername = (LPWSTR)(*bufptr +
525 sizeof(WKSTA_INFO_100));
526 memcpy(info->wki100_computername, computerName,
527 computerNameLen * sizeof(WCHAR));
528 info->wki100_langroup = (LPWSTR)(*bufptr +
529 sizeof(WKSTA_INFO_100) + computerNameLen * sizeof(WCHAR));
530 memcpy(info->wki100_langroup, DomainInfo->DomainName.Buffer,
531 domainNameLen * sizeof(WCHAR));
532 memset(&verInfo, 0, sizeof(verInfo));
533 verInfo.dwOSVersionInfoSize = sizeof(verInfo);
534 GetVersionExW(&verInfo);
535 info->wki100_ver_major = verInfo.dwMajorVersion;
536 info->wki100_ver_minor = verInfo.dwMinorVersion;
538 LsaFreeMemory(DomainInfo);
539 LsaClose(PolicyHandle);
541 break;
544 default:
545 FIXME("level %ld unimplemented\n", level);
546 ret = ERROR_INVALID_LEVEL;
548 return ret;
551 /************************************************************
552 * NetGetJoinInformation (NETAPI32.@)
554 NET_API_STATUS NET_API_FUNCTION NetGetJoinInformation(
555 LPCWSTR Server,
556 LPWSTR *Name,
557 PNETSETUP_JOIN_STATUS type)
559 FIXME("Stub %s %p %p\n", wine_dbgstr_w(Server), Name, type);
561 *Name = NULL;
562 *type = NetSetupUnknownStatus;
564 return NERR_Success;