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
)
56 else if (ServerName
[0] == '\0')
60 DWORD dwSize
= MAX_COMPUTERNAME_LENGTH
+ 1;
64 NetApiBufferAllocate(dwSize
* sizeof(WCHAR
), (LPVOID
*) &buf
);
65 Result
= GetComputerNameW(buf
, &dwSize
);
66 if (Result
&& (ServerName
[0] == '\\') && (ServerName
[1] == '\\'))
68 Result
= Result
&& !lstrcmpW(ServerName
, buf
);
69 NetApiBufferFree(buf
);
75 static void wprint_mac(WCHAR
* buffer
, int len
, PMIB_IFROW ifRow
)
90 for (i
= 0; i
< ifRow
->dwPhysAddrLen
&& 2 * i
< len
; i
++)
92 val
= ifRow
->bPhysAddr
[i
];
94 buffer
[2*i
] = (WCHAR
)((val
>>4) + 'A' - 10);
96 buffer
[2*i
] = (WCHAR
)((val
>>4) + '0');
98 buffer
[2*i
+1] = (WCHAR
)((val
& 0xf) + 'A' - 10);
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
,
130 if (!memcmp(&transport
, TRANSPORT_NBT
, sizeof(ULONG
)))
131 name
= NBT_TRANSPORT_NAME_HEADER
;
133 name
= UNKNOWN_TRANSPORT_NAME_HEADER
;
135 for (ptr1
= buffer
; *name
&& ptr1
< buffer
+ len
; ptr1
++, name
++)
137 for (ptr2
= ifRow
->wszName
; *ptr2
&& ptr1
< buffer
+ len
; ptr1
++, ptr2
++)
142 /***********************************************************************
143 * NetWkstaTransportEnum (NETAPI32.@)
146 struct WkstaTransportEnumData
155 /**********************************************************************/
157 static BOOL
WkstaEnumAdaptersCallback(UCHAR totalLANAs
, UCHAR lanaIndex
,
158 ULONG transport
, const NetBIOSAdapterImpl
*data
, void *closure
)
161 struct WkstaTransportEnumData
*enumData
= (struct WkstaTransportEnumData
*)
164 if (enumData
&& enumData
->pbuf
)
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
))
184 if (enumData
->prefmaxlen
== MAX_PREFERRED_LENGTH
)
185 spaceFor
= totalLANAs
;
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
;
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
;
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
,
213 ti
->wkti0_transport_address
= transport_addr
;
214 wprint_mac(ti
->wkti0_transport_address
, MAX_TRANSPORT_ADDR
,
216 if (!memcmp(&transport
, TRANSPORT_NBT
, sizeof(ULONG
)))
217 ti
->wkti0_wan_ish
= TRUE
;
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
));
228 enumData
->ret
= NERR_Success
;
233 enumData
->ret
= ERROR_MORE_DATA
;
239 enumData
->ret
= ERROR_OUTOFMEMORY
;
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
)
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
;
266 if (hresume
&& *hresume
)
268 FIXME(":resume handle not implemented\n");
269 return ERROR_INVALID_LEVEL
;
274 case 0: /* transport info */
277 struct WkstaTransportEnumData enumData
;
279 if (NetBIOSNumAdapters() == 0)
280 return ERROR_NETWORK_UNREACHABLE
;
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
,
291 *read_entries
= enumData
.n_read
;
292 *total_entries
= enumData
.n_adapt
;
293 if (hresume
) *hresume
= 0;
298 ERR("Invalid level %ld is specified\n", level
);
299 ret
= ERROR_INVALID_LEVEL
;
306 /************************************************************
307 * NetWkstaUserGetInfo (NETAPI32.@)
309 NET_API_STATUS WINAPI
NetWkstaUserGetInfo(LPWSTR reserved
, DWORD level
,
312 TRACE("(%s, %ld, %p)\n", debugstr_w(reserved
), level
, bufptr
);
317 PWKSTA_USER_INFO_0 ui
;
318 DWORD dwSize
= UNLEN
+ 1;
321 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0
) + dwSize
* sizeof(WCHAR
),
324 ui
= (PWKSTA_USER_INFO_0
) *bufptr
;
325 ui
->wkui0_username
= (LPWSTR
) (*bufptr
+ sizeof(WKSTA_USER_INFO_0
));
328 if (!GetUserNameW(ui
->wkui0_username
, &dwSize
))
330 NetApiBufferFree(ui
);
331 return ERROR_NOT_ENOUGH_MEMORY
;
334 NetApiBufferReallocate(
335 *bufptr
, sizeof(WKSTA_USER_INFO_0
) +
336 (lstrlenW(ui
->wkui0_username
) + 1) * sizeof(WCHAR
),
343 PWKSTA_USER_INFO_1 ui
;
344 PWKSTA_USER_INFO_0 ui0
;
346 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
347 LSA_HANDLE PolicyHandle
;
348 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo
;
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");
358 /* get some information first to estimate size of the buffer */
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
,
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
);
380 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1
) +
381 (username_sz
+ logon_domain_sz
+
382 oth_domains_sz
+ logon_server_sz
) * sizeof(WCHAR
),
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
));
396 dwSize
= username_sz
;
397 lstrcpyW(ui
->wkui1_username
, ui0
->wkui0_username
);
398 NetApiBufferFree(ui0
);
400 lstrcpynW(ui
->wkui1_logon_domain
, DomainInfo
->DomainName
.Buffer
,
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;
411 PWKSTA_USER_INFO_1101 ui
;
414 FIXME("Stub. Level 1101 processing is not implemented\n");
415 /* FIXME see also wkui1_oth_domains for level 1 */
418 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101
) + dwSize
* sizeof(WCHAR
),
421 ui
= (PWKSTA_USER_INFO_1101
) *bufptr
;
422 ui
->wkui1101_oth_domains
= (LPWSTR
)(ui
+ 1);
425 ui
->wkui1101_oth_domains
[0] = 0;
429 ERR("Invalid level %ld is specified\n", level
);
430 return ERROR_INVALID_LEVEL
;
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
),
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
),
463 return ERROR_INVALID_PARAMETER
;
466 NET_API_STATUS WINAPI
I_NetNameValidate(LPVOID p1
, LPWSTR wkgrp
, LPVOID p3
,
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
,
478 TRACE("%s %ld %p\n", debugstr_w( servername
), level
, bufptr
);
481 if (!NETAPI_IsLocalComputer(servername
))
483 FIXME("remote computers not supported\n");
484 return ERROR_INVALID_LEVEL
;
487 if (!bufptr
) return ERROR_INVALID_PARAMETER
;
493 DWORD computerNameLen
, domainNameLen
, size
;
494 WCHAR computerName
[MAX_COMPUTERNAME_LENGTH
+ 1];
495 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
496 LSA_HANDLE PolicyHandle
;
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
);
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
);
545 FIXME("level %ld unimplemented\n", level
);
546 ret
= ERROR_INVALID_LEVEL
;
551 /************************************************************
552 * NetGetJoinInformation (NETAPI32.@)
554 NET_API_STATUS NET_API_FUNCTION
NetGetJoinInformation(
557 PNETSETUP_JOIN_STATUS type
)
559 FIXME("Stub %s %p %p\n", wine_dbgstr_w(Server
), Name
, type
);
562 *type
= NetSetupUnknownStatus
;