wininet: Use LIST_FOR_EACH_ENTRY in URLCacheContainers_FindContainerW instead of...
[wine] / dlls / netapi32 / wksta.c
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 "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winsock2.h"
31 #include "nb30.h"
32 #include "lmcons.h"
33 #include "lmapibuf.h"
34 #include "lmerr.h"
35 #include "lmwksta.h"
36 #include "iphlpapi.h"
37 #include "winerror.h"
38 #include "ntsecapi.h"
39 #include "netbios.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
43
44 /************************************************************
45  *                NETAPI_IsLocalComputer
46  *
47  * Checks whether the server name indicates local machine.
48  */
49 BOOL NETAPI_IsLocalComputer(LMCSTR ServerName)
50 {
51     if (!ServerName)
52     {
53         return TRUE;
54     }
55     else if (ServerName[0] == '\0')
56         return TRUE;
57     else
58     {
59         DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
60         BOOL Result;
61         LPWSTR buf;
62
63         NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
64         Result = GetComputerNameW(buf,  &dwSize);
65         if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
66             ServerName += 2;
67         Result = Result && !lstrcmpW(ServerName, buf);
68         NetApiBufferFree(buf);
69
70         return Result;
71     }
72 }
73
74 static void wprint_mac(WCHAR* buffer, int len, const MIB_IFROW *ifRow)
75 {
76     int i;
77     unsigned char val;
78
79     if (!buffer)
80         return;
81     if (len < 1)
82         return;
83     if (!ifRow)
84     {
85         *buffer = '\0';
86         return;
87     }
88
89     for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
90     {
91         val = ifRow->bPhysAddr[i];
92         if ((val >>4) >9)
93             buffer[2*i] = (WCHAR)((val >>4) + 'A' - 10);
94         else
95             buffer[2*i] = (WCHAR)((val >>4) + '0');
96         if ((val & 0xf ) >9)
97             buffer[2*i+1] = (WCHAR)((val & 0xf) + 'A' - 10);
98         else
99             buffer[2*i+1] = (WCHAR)((val & 0xf) + '0');
100     }
101     buffer[2*i]=(WCHAR)0;
102 }
103
104 /* Theoretically this could be too short, except that MS defines
105  * MAX_ADAPTER_NAME as 128, and MAX_INTERFACE_NAME_LEN as 256, and both
106  * represent a count of WCHARs, so even with an extroardinarily long header
107  * this will be plenty
108  */
109 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN
110 #define MAX_TRANSPORT_ADDR 13
111
112 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_"
113 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_"
114
115 static void wprint_name(WCHAR *buffer, int len, ULONG transport,
116  PMIB_IFROW ifRow)
117 {
118     WCHAR *ptr1, *ptr2;
119     const char *name;
120
121     if (!buffer)
122         return;
123     if (!ifRow)
124     {
125         *buffer = '\0';
126         return;
127     }
128
129     if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
130         name = NBT_TRANSPORT_NAME_HEADER;
131     else
132         name = UNKNOWN_TRANSPORT_NAME_HEADER;
133
134     for (ptr1 = buffer; *name && ptr1 < buffer + len; ptr1++, name++)
135         *ptr1 = *name;
136     for (ptr2 = ifRow->wszName; *ptr2 && ptr1 < buffer + len; ptr1++, ptr2++)
137         *ptr1 = *ptr2;
138     *ptr1 = '\0';
139 }
140
141 /***********************************************************************
142  *                NetWkstaTransportEnum  (NETAPI32.@)
143  */
144  
145 struct WkstaTransportEnumData
146 {
147     UCHAR          n_adapt;
148     UCHAR          n_read;
149     DWORD          prefmaxlen;
150     LPBYTE        *pbuf;
151     NET_API_STATUS ret;
152 };
153
154 /**********************************************************************/
155
156 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
157  ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
158 {
159     BOOL ret;
160     struct WkstaTransportEnumData *enumData = (struct WkstaTransportEnumData *)
161      closure;
162
163     if (enumData && enumData->pbuf)
164     {
165         if (lanaIndex == 0)
166         {
167             DWORD toAllocate;
168
169             enumData->n_adapt = totalLANAs;
170             enumData->n_read = 0;
171
172             toAllocate = totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0)
173              + MAX_TRANSPORT_NAME * sizeof(WCHAR) +
174              MAX_TRANSPORT_ADDR * sizeof(WCHAR));
175             if (enumData->prefmaxlen != MAX_PREFERRED_LENGTH)
176                 toAllocate = enumData->prefmaxlen;
177             NetApiBufferAllocate(toAllocate, (LPVOID *)enumData->pbuf);
178         }
179         if (*(enumData->pbuf))
180         {
181             UCHAR spaceFor;
182
183             if (enumData->prefmaxlen == MAX_PREFERRED_LENGTH)
184                 spaceFor = totalLANAs;
185             else
186                 spaceFor = enumData->prefmaxlen /
187                  (sizeof(WKSTA_TRANSPORT_INFO_0) + (MAX_TRANSPORT_NAME +
188                  MAX_TRANSPORT_ADDR) * sizeof(WCHAR));
189             if (enumData->n_read < spaceFor)
190             {
191                 PWKSTA_TRANSPORT_INFO_0 ti;
192                 LMSTR transport_name, transport_addr;
193                 MIB_IFROW ifRow;
194
195                 ti = (PWKSTA_TRANSPORT_INFO_0)(*(enumData->pbuf) +
196                  enumData->n_read * sizeof(WKSTA_TRANSPORT_INFO_0));
197                 transport_name = (LMSTR)(*(enumData->pbuf) +
198                  totalLANAs * sizeof(WKSTA_TRANSPORT_INFO_0) +
199                  enumData->n_read * MAX_TRANSPORT_NAME * sizeof(WCHAR));
200                 transport_addr = (LMSTR)(*(enumData->pbuf) +
201                  totalLANAs * (sizeof(WKSTA_TRANSPORT_INFO_0) +
202                  MAX_TRANSPORT_NAME * sizeof(WCHAR)) +
203                  enumData->n_read * MAX_TRANSPORT_ADDR * sizeof(WCHAR));
204
205                 ifRow.dwIndex = data->ifIndex;
206                 GetIfEntry(&ifRow);
207                 ti->wkti0_quality_of_service = 0;
208                 ti->wkti0_number_of_vcs = 0;
209                 ti->wkti0_transport_name = transport_name;
210                 wprint_name(ti->wkti0_transport_name, MAX_TRANSPORT_NAME,
211                  transport, &ifRow);
212                 ti->wkti0_transport_address = transport_addr;
213                 wprint_mac(ti->wkti0_transport_address, MAX_TRANSPORT_ADDR,
214                  &ifRow);
215                 if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
216                     ti->wkti0_wan_ish = TRUE;
217                 else
218                     ti->wkti0_wan_ish = FALSE;
219                 TRACE("%d of %d:ti at %p\n", lanaIndex, totalLANAs, ti);
220                 TRACE("transport_name at %p %s\n",
221                  ti->wkti0_transport_name,
222                  debugstr_w(ti->wkti0_transport_name));
223                 TRACE("transport_address at %p %s\n",
224                  ti->wkti0_transport_address,
225                  debugstr_w(ti->wkti0_transport_address));
226                 enumData->n_read++;
227                 enumData->ret = NERR_Success;
228                 ret = TRUE;
229             }
230             else
231             {
232                 enumData->ret = ERROR_MORE_DATA;
233                 ret = FALSE;
234             }
235         }
236         else
237         {
238             enumData->ret = ERROR_OUTOFMEMORY;
239             ret = FALSE;
240         }
241     }
242     else
243         ret = FALSE;
244     return ret;
245 }
246
247 /**********************************************************************/
248
249 NET_API_STATUS WINAPI 
250 NetWkstaTransportEnum(LMSTR ServerName, DWORD level, PBYTE* pbuf,
251       DWORD prefmaxlen, LPDWORD read_entries,
252       PDWORD total_entries, PDWORD hresume)
253 {
254     NET_API_STATUS ret;
255
256     TRACE(":%s, 0x%08x, %p, 0x%08x, %p, %p, %p\n", debugstr_w(ServerName), 
257      level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
258     if (!NETAPI_IsLocalComputer(ServerName))
259     {
260         FIXME(":not implemented for non-local computers\n");
261         ret = ERROR_INVALID_LEVEL;
262     }
263     else
264     {
265         if (hresume && *hresume)
266         {
267           FIXME(":resume handle not implemented\n");
268           return ERROR_INVALID_LEVEL;
269         }
270
271         switch (level)
272         {
273             case 0: /* transport info */
274             {
275                 ULONG allTransports;
276                 struct WkstaTransportEnumData enumData;
277
278                 if (NetBIOSNumAdapters() == 0)
279                   return ERROR_NETWORK_UNREACHABLE;
280                 if (!read_entries)
281                   return STATUS_ACCESS_VIOLATION;
282                 if (!total_entries || !pbuf)
283                   return RPC_X_NULL_REF_POINTER;
284
285                 enumData.prefmaxlen = prefmaxlen;
286                 enumData.pbuf = pbuf;
287                 memcpy(&allTransports, ALL_TRANSPORTS, sizeof(ULONG));
288                 NetBIOSEnumAdapters(allTransports, WkstaEnumAdaptersCallback,
289                  &enumData);
290                 *read_entries = enumData.n_read;
291                 *total_entries = enumData.n_adapt;
292                 if (hresume) *hresume= 0;
293                 ret = enumData.ret;
294                 break;
295             }
296             default:
297                 TRACE("Invalid level %d is specified\n", level);
298                 ret = ERROR_INVALID_LEVEL;
299         }
300     }
301     return ret;
302 }
303
304
305 /************************************************************
306  *                NetWkstaUserGetInfo  (NETAPI32.@)
307  */
308 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LMSTR reserved, DWORD level,
309                                           PBYTE* bufptr)
310 {
311     NET_API_STATUS nastatus;
312
313     TRACE("(%s, %d, %p)\n", debugstr_w(reserved), level, bufptr);
314     switch (level)
315     {
316     case 0:
317     {
318         PWKSTA_USER_INFO_0 ui;
319         DWORD dwSize = UNLEN + 1;
320
321         /* set up buffer */
322         nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
323                              (LPVOID *) bufptr);
324         if (nastatus != NERR_Success)
325             return ERROR_NOT_ENOUGH_MEMORY;
326
327         ui = (PWKSTA_USER_INFO_0) *bufptr;
328         ui->wkui0_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
329
330         /* get data */
331         if (!GetUserNameW(ui->wkui0_username, &dwSize))
332         {
333             NetApiBufferFree(ui);
334             return ERROR_NOT_ENOUGH_MEMORY;
335         }
336         else {
337             nastatus = NetApiBufferReallocate(
338                 *bufptr, sizeof(WKSTA_USER_INFO_0) +
339                 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
340                 (LPVOID *) bufptr);
341             if (nastatus != NERR_Success)
342                 return nastatus;
343         }
344         break;
345     }
346
347     case 1:
348     {
349         PWKSTA_USER_INFO_1 ui;
350         PWKSTA_USER_INFO_0 ui0;
351         DWORD dwSize;
352         LSA_OBJECT_ATTRIBUTES ObjectAttributes;
353         LSA_HANDLE PolicyHandle;
354         PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
355         NTSTATUS NtStatus;
356
357         /* sizes of the field buffers in WCHARS */
358         int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
359
360         FIXME("Level 1 processing is partially implemented\n");
361         oth_domains_sz = 1;
362         logon_server_sz = 1;
363
364         /* get some information first to estimate size of the buffer */
365         ui0 = NULL;
366         nastatus = NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
367         if (nastatus != NERR_Success)
368             return nastatus;
369         username_sz = lstrlenW(ui0->wkui0_username) + 1;
370
371         ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
372         NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
373                                  POLICY_VIEW_LOCAL_INFORMATION,
374                                  &PolicyHandle);
375         if (NtStatus != STATUS_SUCCESS)
376         {
377             TRACE("LsaOpenPolicyFailed with NT status %x\n",
378                 LsaNtStatusToWinError(NtStatus));
379             NetApiBufferFree(ui0);
380             return ERROR_NOT_ENOUGH_MEMORY;
381         }
382         LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
383                                   (PVOID*) &DomainInfo);
384         logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
385         LsaClose(PolicyHandle);
386
387         /* set up buffer */
388         nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
389                              (username_sz + logon_domain_sz +
390                               oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
391                              (LPVOID *) bufptr);
392         if (nastatus != NERR_Success) {
393             NetApiBufferFree(ui0);
394             return nastatus;
395         }
396         ui = (WKSTA_USER_INFO_1 *) *bufptr;
397         ui->wkui1_username = (LMSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
398         ui->wkui1_logon_domain = (LMSTR) (
399             ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
400         ui->wkui1_oth_domains = (LMSTR) (
401             ((PBYTE) ui->wkui1_logon_domain) +
402             logon_domain_sz * sizeof(WCHAR));
403         ui->wkui1_logon_server = (LMSTR) (
404             ((PBYTE) ui->wkui1_oth_domains) +
405             oth_domains_sz * sizeof(WCHAR));
406
407         /* get data */
408         dwSize = username_sz;
409         lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
410         NetApiBufferFree(ui0);
411
412         lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
413                 logon_domain_sz);
414         LsaFreeMemory(DomainInfo);
415
416         /* FIXME. Not implemented. Populated with empty strings */
417         ui->wkui1_oth_domains[0] = 0;
418         ui->wkui1_logon_server[0] = 0;
419         break;
420     }
421     case 1101:
422     {
423         PWKSTA_USER_INFO_1101 ui;
424         DWORD dwSize = 1;
425
426         FIXME("Stub. Level 1101 processing is not implemented\n");
427         /* FIXME see also wkui1_oth_domains for level 1 */
428
429         /* set up buffer */
430         nastatus = NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
431                              (LPVOID *) bufptr);
432         if (nastatus != NERR_Success)
433             return nastatus;
434         ui = (PWKSTA_USER_INFO_1101) *bufptr;
435         ui->wkui1101_oth_domains = (LMSTR)(ui + 1);
436
437         /* get data */
438         ui->wkui1101_oth_domains[0] = 0;
439         break;
440     }
441     default:
442         TRACE("Invalid level %d is specified\n", level);
443         return ERROR_INVALID_LEVEL;
444     }
445     return NERR_Success;
446 }
447
448 /************************************************************
449  *                NetpGetComputerName  (NETAPI32.@)
450  */
451 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
452 {
453     DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
454
455     TRACE("(%p)\n", Buffer);
456     NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
457     if (GetComputerNameW(*Buffer,  &dwSize))
458     {
459         return NetApiBufferReallocate(
460             *Buffer, (dwSize + 1) * sizeof(WCHAR),
461             (LPVOID *) Buffer);
462     }
463     else
464     {
465         NetApiBufferFree(*Buffer);
466         return ERROR_NOT_ENOUGH_MEMORY;
467     }
468 }
469
470 NET_API_STATUS WINAPI I_NetNameCompare(LPVOID p1, LPWSTR wkgrp, LPWSTR comp,
471  LPVOID p4, LPVOID p5)
472 {
473     FIXME("(%p %s %s %p %p): stub\n", p1, debugstr_w(wkgrp), debugstr_w(comp),
474      p4, p5);
475     return ERROR_INVALID_PARAMETER;
476 }
477
478 NET_API_STATUS WINAPI I_NetNameValidate(LPVOID p1, LPWSTR wkgrp, LPVOID p3,
479  LPVOID p4)
480 {
481     FIXME("(%p %s %p %p): stub\n", p1, debugstr_w(wkgrp), p3, p4);
482     return ERROR_INVALID_PARAMETER;
483 }
484
485 NET_API_STATUS WINAPI NetWkstaGetInfo( LMSTR servername, DWORD level,
486                                        LPBYTE* bufptr)
487 {
488     NET_API_STATUS ret;
489
490     TRACE("%s %d %p\n", debugstr_w( servername ), level, bufptr );
491     if (servername)
492     {
493         if (!NETAPI_IsLocalComputer(servername))
494         {
495             FIXME("remote computers not supported\n");
496             return ERROR_INVALID_LEVEL;
497         }
498     }
499     if (!bufptr) return ERROR_INVALID_PARAMETER;
500
501     switch (level)
502     {
503         case 100:
504         case 101:
505         case 102:
506         {
507             static const WCHAR lanroot[] = {'c',':','\\','l','a','n','m','a','n',0};  /* FIXME */
508             DWORD computerNameLen, domainNameLen, size;
509             WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
510             LSA_OBJECT_ATTRIBUTES ObjectAttributes;
511             LSA_HANDLE PolicyHandle;
512             NTSTATUS NtStatus;
513            
514             computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
515             GetComputerNameW(computerName, &computerNameLen);
516             computerNameLen++; /* include NULL terminator */
517
518             ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
519             NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
520              POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
521             if (NtStatus != STATUS_SUCCESS)
522                 ret = LsaNtStatusToWinError(NtStatus);
523             else
524             {
525                 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
526
527                 LsaQueryInformationPolicy(PolicyHandle,
528                  PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
529                 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
530                 size = sizeof(WKSTA_INFO_102) + computerNameLen * sizeof(WCHAR)
531                     + domainNameLen * sizeof(WCHAR) + sizeof(lanroot);
532                 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
533                 if (ret == NERR_Success)
534                 {
535                     /* INFO_100 and INFO_101 structures are subsets of INFO_102 */
536                     PWKSTA_INFO_102 info = (PWKSTA_INFO_102)*bufptr;
537                     OSVERSIONINFOW verInfo;
538
539                     info->wki102_platform_id = PLATFORM_ID_NT;
540                     info->wki102_computername = (LMSTR)(*bufptr +
541                      sizeof(WKSTA_INFO_102));
542                     memcpy(info->wki102_computername, computerName,
543                      computerNameLen * sizeof(WCHAR));
544                     info->wki102_langroup = info->wki102_computername + computerNameLen;
545                     memcpy(info->wki102_langroup, DomainInfo->DomainName.Buffer,
546                      domainNameLen * sizeof(WCHAR));
547                     info->wki102_lanroot = info->wki102_langroup + domainNameLen;
548                     memcpy(info->wki102_lanroot, lanroot, sizeof(lanroot));
549                     memset(&verInfo, 0, sizeof(verInfo));
550                     verInfo.dwOSVersionInfoSize = sizeof(verInfo);
551                     GetVersionExW(&verInfo);
552                     info->wki102_ver_major = verInfo.dwMajorVersion;
553                     info->wki102_ver_minor = verInfo.dwMinorVersion;
554                     info->wki102_logged_on_users = 1;
555                 }
556                 LsaFreeMemory(DomainInfo);
557                 LsaClose(PolicyHandle);
558             }
559             break;
560         }
561
562         default:
563             FIXME("level %d unimplemented\n", level);
564             ret = ERROR_INVALID_LEVEL;
565     }
566     return ret;
567 }
568
569 /************************************************************
570  *                NetGetJoinInformation (NETAPI32.@)
571  */
572 NET_API_STATUS NET_API_FUNCTION NetGetJoinInformation(
573     LPCWSTR Server,
574     LPWSTR *Name,
575     PNETSETUP_JOIN_STATUS type)
576 {
577     FIXME("Stub %s %p %p\n", wine_dbgstr_w(Server), Name, type);
578
579     *Name = NULL;
580     *type = NetSetupUnknownStatus;
581
582     return NERR_Success;
583 }