1 /* Copyright 2002 Andriy Palamarchuk 2 * Copyright (c) 2003 Juan Lang 3 * 4 * netapi32 user functions 5 * 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. 10 * 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. 15 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 19 */ 20 21 #include "netapi32.h" 22 23 #include <lmwksta.h> 24 #include <lmjoin.h> 25 26 WINE_DEFAULT_DEBUG_CHANNEL(netapi32); 27 28 /************************************************************ 29 * NETAPI_IsLocalComputer 30 * 31 * Checks whether the server name indicates local machine. 32 */ 33 DECLSPEC_HIDDEN BOOL NETAPI_IsLocalComputer( LMCSTR name ) 34 { 35 WCHAR buf[MAX_COMPUTERNAME_LENGTH + 1]; 36 DWORD size = sizeof(buf) / sizeof(buf[0]); 37 BOOL ret; 38 39 if (!name || !name[0]) return TRUE; 40 41 ret = GetComputerNameW( buf, &size ); 42 if (ret && name[0] == '\\' && name[1] == '\\') name += 2; 43 return ret && !strcmpiW( name, buf ); 44 } 45 46 static void wprint_mac(WCHAR* buffer, int len, const MIB_IFROW *ifRow) 47 { 48 int i; 49 unsigned char val; 50 51 if (!buffer) 52 return; 53 if (len < 1) 54 return; 55 if (!ifRow) 56 { 57 *buffer = '\0'; 58 return; 59 } 60 61 for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++) 62 { 63 val = ifRow->bPhysAddr[i]; 64 if ((val >>4) >9) 65 buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10); 66 else 67 buffer[2*i] = (WCHAR)((val >>4) + '0'); 68 if ((val & 0xf ) >9) 69 buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10); 70 else 71 buffer[2*i+1] = (WCHAR)((val & 0xf) + '0'); 72 } 73 buffer[2*i]=0; 74 } 75 76 /* Theoretically this could be too short, except that MS defines 77 * MAX_ADAPTER_NAME as 128, and MAX_INTERFACE_NAME_LEN as 256, and both 78 * represent a count of WCHARs, so even with an extraordinarily long header 79 * this will be plenty 80 */ 81 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN 82 #define MAX_TRANSPORT_ADDR 13 83 84 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_" 85 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_" 86 87 static void wprint_name(WCHAR *buffer, int len, ULONG transport, 88 PMIB_IFROW ifRow) 89 { 90 WCHAR *ptr1, *ptr2; 91 const char *name; 92 93 if (!buffer) 94 return; 95 if (!ifRow) 96 { 97 *buffer = '\0'; 98 return; 99 } 100 101 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG))) 102 name = NBT_TRANSPORT_NAME_HEADER; 103 else 104 name = UNKNOWN_TRANSPORT_NAME_HEADER; 105 106 for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++) 107 *ptr1 = *name; 108 for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++) 109 *ptr1 = *ptr2; 110 *ptr1 = '\0'; 111 } 112 113 /*********************************************************************** 114 * NetWkstaTransportEnum (NETAPI32.@) 115 */ 116 117 struct WkstaTransportEnumData 118 { 119 UCHAR n_adapt; 120 UCHAR n_read; 121 DWORD prefmaxlen; 122 LPBYTE *pbuf; 123 NET_API_STATUS ret; 124 }; 125 126 /**********************************************************************/ 127 128 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex, 129 ULONG transport, const NetBIOSAdapterImpl *data, void *closure) 130 { 131 BOOL ret; 132 struct WkstaTransportEnumData *enumData = closure; 133 134 if (enumData && enumData->pbuf) 135 { 136 if (lanaIndex == 0) 137 { 138 DWORD toAllocate; 139 140 enumData->n_adapt = totalLANAs; 141 enumData->n_read = 0; 142 143 toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) 144 + MAX_TRANSPORT_NAME * sizeof(WCHAR) + 145 MAX_TRANSPORT_ADDR * sizeof(WCHAR)); 146 if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH) 147 toAllocate = enumData->prefmaxlen; 148 NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf); 149 } 150 if (*(enumData->pbuf)) 151 { 152 UCHAR spaceFor; 153 154 if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH) 155 spaceFor = totalLANAs; 156 else 157 spaceFor = enumData->prefmaxlen / 158 (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME + 159 MAX_TRANSPORT_ADDR) * sizeof(WCHAR)); 160 if (enumData->n_read < spaceFor) 161 { 162 PWKSTA_TRANSPORT_INFO_0 ti; 163 LMSTR transport_name, transport_addr; 164 MIB_IFROW ifRow; 165 166 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) + 167 enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0)); 168 transport_name = (LMSTR)(*(enumData->pbuf) + 169 totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) + 170 enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR)); 171 transport_addr = (LMSTR)(*(enumData->pbuf) + 172 totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) + 173 MAX_TRANSPORT_NAME * sizeof(WCHAR)) + 174 enumData->n_read * MAX_TRANSPORT_ADDR * sizeof(WCHAR)); 175 176 ifRow.dwIndex = data->ifIndex; 177 GetIfEntry(&ifRow); 178 ti->wkti0_quality_of_service = 0; 179 ti->wkti0_number_of_vcs = 0; 180 ti->wkti0_transport_name = transport_name; 181 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME, 182 transport, &ifRow); 183 ti->wkti0_transport_address = transport_addr; 184 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR, 185 &ifRow); 186 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG))) 187 ti->wkti0_wan_ish = TRUE; 188 else 189 ti->wkti0_wan_ish = FALSE; 190 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti); 191 TRACE("transport_name at %p %s\n", 192 ti->wkti0_transport_name, 193 debugstr_w(ti->wkti0_transport_name)); 194 TRACE("transport_address at %p %s\n", 195 ti->wkti0_transport_address, 196 debugstr_w(ti->wkti0_transport_address)); 197 enumData->n_read++; 198 enumData->ret = NERR_Success; 199 ret = TRUE; 200 } 201 else 202 { 203 enumData->ret = ERROR_MORE_DATA; 204 ret = FALSE; 205 } 206 } 207 else 208 { 209 enumData->ret = ERROR_OUTOFMEMORY; 210 ret = FALSE; 211 } 212 } 213 else 214 ret = FALSE; 215 return ret; 216 } 217 218 /**********************************************************************/ 219 220 NET_API_STATUS WINAPI 221 NetWkstaTransportEnum(LMSTR ServerName, DWORD level, PBYTE* pbuf, 222 DWORD prefmaxlen, LPDWORD read_entries, 223 PDWORD total_entries, PDWORD hresume) 224 { 225 NET_API_STATUS ret; 226 227 TRACE(":%s, 0x%08x, %p, 0x%08x, %p, %p, %p\n", debugstr_w(ServerName), 228 level, pbuf, prefmaxlen, read_entries, total_entries,hresume); 229 if (!NETAPI_IsLocalComputer(ServerName)) 230 { 231 FIXME(":not implemented for non-local computers\n"); 232 ret = ERROR_INVALID_LEVEL; 233 } 234 else 235 { 236 if (hresume && *hresume) 237 { 238 FIXME(":resume handle not implemented\n"); 239 return ERROR_INVALID_LEVEL; 240 } 241 242 switch (level) 243 { 244 case 0: /* transport info */ 245 { 246 ULONG allTransports; 247 struct WkstaTransportEnumData enumData; 248 249 if (NetBIOSNumAdapters() == 0) 250 return ERROR_NETWORK_UNREACHABLE; 251 if (!read_entries) 252 return STATUS_ACCESS_VIOLATION; 253 if (!total_entries || !pbuf) 254 return RPC_X_NULL_REF_POINTER; 255 256 enumData.prefmaxlen = prefmaxlen; 257 enumData.pbuf = pbuf; 258 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG)); 259 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback, 260 &enumData); 261 *read_entries = enumData.n_read; 262 *total_entries = enumData.n_adapt; 263 if (hresume) *hresume= 0; 264 ret = enumData.ret; 265 break; 266 } 267 default: 268 TRACE("Invalid level %d is specified\n", level); 269 ret = ERROR_INVALID_LEVEL; 270 } 271 } 272 return ret; 273 } 274 275 276 /************************************************************ 277 * NetWkstaUserGetInfo (NETAPI32.@) 278 */ 279 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LMSTR reserved, DWORD level, 280 PBYTE* bufptr) 281 { 282 NET_API_STATUS nastatus; 283 284 TRACE("(%s, %d, %p)\n", debugstr_w(reserved), level, bufptr); 285 switch (level) 286 { 287 case 0: 288 { 289 PWKSTA_USER_INFO_0 ui; 290 DWORD dwSize = UNLEN + 1; 291 292 /* set up buffer */ 293 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR), 294 (LPVOID *) bufptr); 295 if (nastatus != NERR_Success) 296 return ERROR_NOT_ENOUGH_MEMORY; 297 298 ui = (PWKSTA_USER_INFO_0) *bufptr; 299 ui->wkui0_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0)); 300 301 /* get data */ 302 if (!GetUserNameW(ui->wkui0_username, &dwSize)) 303 { 304 NetApiBufferFree(ui); 305 return ERROR_NOT_ENOUGH_MEMORY; 306 } 307 else { 308 nastatus = NetApiBufferReallocate( 309 *bufptr, sizeof(WKSTA_USER_INFO_0) + 310 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR), 311 (LPVOID *) bufptr); 312 if (nastatus != NERR_Success) 313 { 314 NetApiBufferFree(ui); 315 return nastatus; 316 } 317 ui = (PWKSTA_USER_INFO_0) *bufptr; 318 ui->wkui0_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0)); 319 } 320 break; 321 } 322 323 case 1: 324 { 325 PWKSTA_USER_INFO_1 ui; 326 PWKSTA_USER_INFO_0 ui0; 327 LSA_OBJECT_ATTRIBUTES ObjectAttributes; 328 LSA_HANDLE PolicyHandle; 329 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo; 330 NTSTATUS NtStatus; 331 332 /* sizes of the field buffers in WCHARS */ 333 int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz; 334 335 FIXME("Level 1 processing is partially implemented\n"); 336 oth_domains_sz = 1; 337 logon_server_sz = 1; 338 339 /* get some information first to estimate size of the buffer */ 340 ui0 = NULL; 341 nastatus = NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0); 342 if (nastatus != NERR_Success) 343 return nastatus; 344 username_sz = lstrlenW(ui0->wkui0_username) + 1; 345 346 ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes)); 347 NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes, 348 POLICY_VIEW_LOCAL_INFORMATION, 349 &PolicyHandle); 350 if (NtStatus != STATUS_SUCCESS) 351 { 352 TRACE("LsaOpenPolicyFailed with NT status %x\n", 353 LsaNtStatusToWinError(NtStatus)); 354 NetApiBufferFree(ui0); 355 return ERROR_NOT_ENOUGH_MEMORY; 356 } 357 LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation, 358 (PVOID*) &DomainInfo); 359 logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1; 360 LsaClose(PolicyHandle); 361 362 /* set up buffer */ 363 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) + 364 (username_sz + logon_domain_sz + 365 oth_domains_sz + logon_server_sz) * sizeof(WCHAR), 366 (LPVOID *) bufptr); 367 if (nastatus != NERR_Success) { 368 NetApiBufferFree(ui0); 369 return nastatus; 370 } 371 ui = (WKSTA_USER_INFO_1 *) *bufptr; 372 ui->wkui1_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1)); 373 ui->wkui1_logon_domain = (LMSTR) ( 374 ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR)); 375 ui->wkui1_oth_domains = (LMSTR) ( 376 ((PBYTE) ui->wkui1_logon_domain) + 377 logon_domain_sz * sizeof(WCHAR)); 378 ui->wkui1_logon_server = (LMSTR) ( 379 ((PBYTE) ui->wkui1_oth_domains) + 380 oth_domains_sz * sizeof(WCHAR)); 381 382 /* get data */ 383 lstrcpyW(ui->wkui1_username, ui0->wkui0_username); 384 NetApiBufferFree(ui0); 385 386 lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer, 387 logon_domain_sz); 388 LsaFreeMemory(DomainInfo); 389 390 /* FIXME. Not implemented. Populated with empty strings */ 391 ui->wkui1_oth_domains[0] = 0; 392 ui->wkui1_logon_server[0] = 0; 393 break; 394 } 395 case 1101: 396 { 397 PWKSTA_USER_INFO_1101 ui; 398 DWORD dwSize = 1; 399 400 FIXME("Stub. Level 1101 processing is not implemented\n"); 401 /* FIXME see also wkui1_oth_domains for level 1 */ 402 403 /* set up buffer */ 404 nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR), 405 (LPVOID *) bufptr); 406 if (nastatus != NERR_Success) 407 return nastatus; 408 ui = (PWKSTA_USER_INFO_1101) *bufptr; 409 ui->wkui1101_oth_domains = (LMSTR)(ui + 1); 410 411 /* get data */ 412 ui->wkui1101_oth_domains[0] = 0; 413 break; 414 } 415 default: 416 TRACE("Invalid level %d is specified\n", level); 417 return ERROR_INVALID_LEVEL; 418 } 419 return NERR_Success; 420 } 421 422 /************************************************************ 423 * NetpGetComputerName (NETAPI32.@) 424 */ 425 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer) 426 { 427 DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1; 428 429 TRACE("(%p)\n", Buffer); 430 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer); 431 if (GetComputerNameW(*Buffer, &dwSize)) 432 { 433 return NetApiBufferReallocate( 434 *Buffer, (dwSize + 1) * sizeof(WCHAR), 435 (LPVOID *) Buffer); 436 } 437 else 438 { 439 NetApiBufferFree(*Buffer); 440 return ERROR_NOT_ENOUGH_MEMORY; 441 } 442 } 443 444 NET_API_STATUS WINAPI I_NetNameCompare(LPVOID p1, LPWSTR wkgrp, LPWSTR comp, 445 LPVOID p4, LPVOID p5) 446 { 447 FIXME("(%p %s %s %p %p): stub\n", p1, debugstr_w(wkgrp), debugstr_w(comp), 448 p4, p5); 449 return ERROR_INVALID_PARAMETER; 450 } 451 452 NET_API_STATUS WINAPI I_NetNameValidate(LPVOID p1, LPWSTR wkgrp, LPVOID p3, 453 LPVOID p4) 454 { 455 FIXME("(%p %s %p %p): stub\n", p1, debugstr_w(wkgrp), p3, p4); 456 return ERROR_INVALID_PARAMETER; 457 } 458