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
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
)
54 DWORD dwSize
= MAX_COMPUTERNAME_LENGTH
+ 1;
58 NetApiBufferAllocate(dwSize
* sizeof(WCHAR
), (LPVOID
*) &buf
);
59 Result
= GetComputerNameW(buf
, &dwSize
);
60 if (Result
&& (ServerName
[0] == '\\') && (ServerName
[1] == '\\'))
62 Result
= Result
&& !lstrcmpW(ServerName
, buf
);
63 NetApiBufferFree(buf
);
69 static void wprint_mac(WCHAR
* buffer
, int len
, PMIB_IFROW ifRow
)
84 for (i
= 0; i
< ifRow
->dwPhysAddrLen
&& 2 * i
< len
; i
++)
86 val
= ifRow
->bPhysAddr
[i
];
88 buffer
[2*i
] = (WCHAR
)((val
>>4) + 'A' - 10);
90 buffer
[2*i
] = (WCHAR
)((val
>>4) + '0');
92 buffer
[2*i
+1] = (WCHAR
)((val
& 0xf) + 'A' - 10);
94 buffer
[2*i
+1] = (WCHAR
)((val
& 0xf) + '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
,
124 if (!memcmp(&transport
, TRANSPORT_NBT
, sizeof(ULONG
)))
125 name
= NBT_TRANSPORT_NAME_HEADER
;
127 name
= UNKNOWN_TRANSPORT_NAME_HEADER
;
129 for (ptr1
= buffer
; *name
&& ptr1
< buffer
+ len
; ptr1
++, name
++)
131 for (ptr2
= ifRow
->wszName
; *ptr2
&& ptr1
< buffer
+ len
; ptr1
++, ptr2
++)
136 struct WkstaTransportEnumData
145 static BOOL
WkstaEnumAdaptersCallback(UCHAR totalLANAs
, UCHAR lanaIndex
,
146 ULONG transport
, const NetBIOSAdapterImpl
*data
, void *closure
)
149 struct WkstaTransportEnumData
*enumData
= (struct WkstaTransportEnumData
*)
152 if (enumData
&& enumData
->pbuf
)
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
))
172 if (enumData
->prefmaxlen
== MAX_PREFERRED_LENGTH
)
173 spaceFor
= totalLANAs
;
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
;
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
;
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
,
201 ti
->wkti0_transport_address
= transport_addr
;
202 wprint_mac(ti
->wkti0_transport_address
, MAX_TRANSPORT_ADDR
,
204 if (!memcmp(&transport
, TRANSPORT_NBT
, sizeof(ULONG
)))
205 ti
->wkti0_wan_ish
= TRUE
;
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
));
216 enumData
->ret
= NERR_Success
;
221 enumData
->ret
= ERROR_MORE_DATA
;
227 enumData
->ret
= ERROR_OUTOFMEMORY
;
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
)
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
;
252 if (hresume
&& *hresume
)
254 FIXME(":resume handle not implemented\n");
255 return ERROR_INVALID_LEVEL
;
260 case 0: /* transport info */
263 struct WkstaTransportEnumData enumData
;
265 if (NetBIOSNumAdapters() == 0)
266 return ERROR_NETWORK_UNREACHABLE
;
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
,
277 *read_entries
= enumData
.n_read
;
278 *total_entries
= enumData
.n_adapt
;
279 if (hresume
) *hresume
= 0;
284 ERR("Invalid level %ld is specified\n", level
);
285 ret
= ERROR_INVALID_LEVEL
;
292 /************************************************************
293 * NetWkstaUserGetInfo (NETAPI32.@)
295 NET_API_STATUS WINAPI
NetWkstaUserGetInfo(LPWSTR reserved
, DWORD level
,
298 TRACE("(%s, %ld, %p)\n", debugstr_w(reserved
), level
, bufptr
);
303 PWKSTA_USER_INFO_0 ui
;
304 DWORD dwSize
= UNLEN
+ 1;
307 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0
) + dwSize
* sizeof(WCHAR
),
310 ui
= (PWKSTA_USER_INFO_0
) *bufptr
;
311 ui
->wkui0_username
= (LPWSTR
) (*bufptr
+ sizeof(WKSTA_USER_INFO_0
));
314 if (!GetUserNameW(ui
->wkui0_username
, &dwSize
))
316 NetApiBufferFree(ui
);
317 return ERROR_NOT_ENOUGH_MEMORY
;
320 NetApiBufferReallocate(
321 *bufptr
, sizeof(WKSTA_USER_INFO_0
) +
322 (lstrlenW(ui
->wkui0_username
) + 1) * sizeof(WCHAR
),
329 PWKSTA_USER_INFO_1 ui
;
330 PWKSTA_USER_INFO_0 ui0
;
332 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
333 LSA_HANDLE PolicyHandle
;
334 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo
;
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");
344 /* get some information first to estimate size of the buffer */
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
,
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
);
366 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1
) +
367 (username_sz
+ logon_domain_sz
+
368 oth_domains_sz
+ logon_server_sz
) * sizeof(WCHAR
),
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
));
382 dwSize
= username_sz
;
383 lstrcpyW(ui
->wkui1_username
, ui0
->wkui0_username
);
384 NetApiBufferFree(ui0
);
386 lstrcpynW(ui
->wkui1_logon_domain
, DomainInfo
->DomainName
.Buffer
,
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;
397 PWKSTA_USER_INFO_1101 ui
;
400 FIXME("Stub. Level 1101 processing is not implemented\n");
401 /* FIXME see also wkui1_oth_domains for level 1 */
404 NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101
) + dwSize
* sizeof(WCHAR
),
407 ui
= (PWKSTA_USER_INFO_1101
) *bufptr
;
408 ui
->wkui1101_oth_domains
= (LPWSTR
)(ui
+ 1);
411 ui
->wkui1101_oth_domains
[0] = 0;
415 ERR("Invalid level %ld is specified\n", level
);
416 return ERROR_INVALID_LEVEL
;
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
),
439 NetApiBufferFree(*Buffer
);
440 return ERROR_NOT_ENOUGH_MEMORY
;
444 NET_API_STATUS WINAPI
NetWkstaGetInfo( LPWSTR servername
, DWORD level
,
449 TRACE("%p %ld %p\n", debugstr_w( servername
), level
, bufptr
);
452 FIXME("remote computers not supported\n");
453 return ERROR_INVALID_LEVEL
;
455 if (!bufptr
) return ERROR_INVALID_PARAMETER
;
461 DWORD computerNameLen
, domainNameLen
, size
;
462 WCHAR computerName
[MAX_COMPUTERNAME_LENGTH
+ 1];
463 LSA_OBJECT_ATTRIBUTES ObjectAttributes
;
464 LSA_HANDLE PolicyHandle
;
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
);
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
);
513 FIXME("level %ld unimplemented\n", level
);
514 ret
= ERROR_INVALID_LEVEL
;