netapi32: Downgrade more ERRs to TRACEs.
[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 "winreg.h"
39 #include "ntsecapi.h"
40 #include "netbios.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
44
45 /************************************************************
46  *                NETAPI_IsLocalComputer
47  *
48  * Checks whether the server name indicates local machine.
49  */
50 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName)
51 {
52     if (!ServerName)
53     {
54         return TRUE;
55     }
56     else if (ServerName[0] == '\0')
57         return TRUE;
58     else
59     {
60         DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
61         BOOL Result;
62         LPWSTR buf;
63
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);
70
71         return Result;
72     }
73 }
74
75 static void wprint_mac(WCHAR* buffer, int len, PMIB_IFROW ifRow)
76 {
77     int i;
78     unsigned char val;
79
80     if (!buffer)
81         return;
82     if (len < 1)
83         return;
84     if (!ifRow)
85     {
86         *buffer = '\0';
87         return;
88     }
89
90     for (i = 0; i < ifRow->dwPhysAddrLen && 2 * i < len; i++)
91     {
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');
101     }
102     buffer[2*i]=(WCHAR)0;
103 }
104
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
109  */
110 #define MAX_TRANSPORT_NAME MAX_INTERFACE_NAME_LEN
111 #define MAX_TRANSPORT_ADDR 13
112
113 #define NBT_TRANSPORT_NAME_HEADER "\\Device\\NetBT_Tcpip_"
114 #define UNKNOWN_TRANSPORT_NAME_HEADER "\\Device\\UnknownTransport_"
115
116 static void wprint_name(WCHAR *buffer, int len, ULONG transport,
117  PMIB_IFROW ifRow)
118 {
119     WCHAR *ptr1, *ptr2;
120     const char *name;
121
122     if (!buffer)
123         return;
124     if (!ifRow)
125     {
126         *buffer = '\0';
127         return;
128     }
129
130     if (!memcmp(&transport, TRANSPORT_NBT, sizeof(ULONG)))
131         name = NBT_TRANSPORT_NAME_HEADER;
132     else
133         name = UNKNOWN_TRANSPORT_NAME_HEADER;
134
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';
140 }
141
142 /***********************************************************************
143  *                NetWkstaTransportEnum  (NETAPI32.@)
144  */
145  
146 struct WkstaTransportEnumData
147 {
148     UCHAR          n_adapt;
149     UCHAR          n_read;
150     DWORD          prefmaxlen;
151     LPBYTE        *pbuf;
152     NET_API_STATUS ret;
153 };
154
155 /**********************************************************************/
156
157 static BOOL WkstaEnumAdaptersCallback(UCHAR totalLANAs, UCHAR lanaIndex,
158  ULONG transport, const NetBIOSAdapterImpl *data, void *closure)
159 {
160     BOOL ret;
161     struct WkstaTransportEnumData *enumData = (struct WkstaTransportEnumData *)
162      closure;
163
164     if (enumData && enumData->pbuf)
165     {
166         if (lanaIndex == 0)
167         {
168             DWORD toAllocate;
169
170             enumData->n_adapt = totalLANAs;
171             enumData->n_read = 0;
172
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);
179         }
180         if (*(enumData->pbuf))
181         {
182             UCHAR spaceFor;
183
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)
191             {
192                 PWKSTA_TRANSPORT_INFO_0 ti;
193                 LPWSTR transport_name, transport_addr;
194                 MIB_IFROW ifRow;
195
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));
205
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;
230             }
231             else
232             {
233                 enumData->ret = ERROR_MORE_DATA;
234                 ret = FALSE;
235             }
236         }
237         else
238         {
239             enumData->ret = ERROR_OUTOFMEMORY;
240             ret = FALSE;
241         }
242     }
243     else
244         ret = FALSE;
245     return ret;
246 }
247
248 /**********************************************************************/
249
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)
254 {
255     NET_API_STATUS ret;
256
257     TRACE(":%s, 0x%08x, %p, 0x%08x, %p, %p, %p\n", debugstr_w(ServerName), 
258      level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
259     if (!NETAPI_IsLocalComputer(ServerName))
260     {
261         FIXME(":not implemented for non-local computers\n");
262         ret = ERROR_INVALID_LEVEL;
263     }
264     else
265     {
266         if (hresume && *hresume)
267         {
268           FIXME(":resume handle not implemented\n");
269           return ERROR_INVALID_LEVEL;
270         }
271
272         switch (level)
273         {
274             case 0: /* transport info */
275             {
276                 ULONG allTransports;
277                 struct WkstaTransportEnumData enumData;
278
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;
285
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;
296             }
297             default:
298                 TRACE("Invalid level %d is specified\n", level);
299                 ret = ERROR_INVALID_LEVEL;
300         }
301     }
302     return ret;
303 }
304
305
306 /************************************************************
307  *                NetWkstaUserGetInfo  (NETAPI32.@)
308  */
309 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LPWSTR reserved, DWORD level,
310                                           PBYTE* bufptr)
311 {
312     TRACE("(%s, %d, %p)\n", debugstr_w(reserved), level, bufptr);
313     switch (level)
314     {
315     case 0:
316     {
317         PWKSTA_USER_INFO_0 ui;
318         DWORD dwSize = UNLEN + 1;
319
320         /* set up buffer */
321         NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
322                              (LPVOID *) bufptr);
323
324         ui = (PWKSTA_USER_INFO_0) *bufptr;
325         ui->wkui0_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
326
327         /* get data */
328         if (!GetUserNameW(ui->wkui0_username, &dwSize))
329         {
330             NetApiBufferFree(ui);
331             return ERROR_NOT_ENOUGH_MEMORY;
332         }
333         else
334             NetApiBufferReallocate(
335                 *bufptr, sizeof(WKSTA_USER_INFO_0) +
336                 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
337                 (LPVOID *) bufptr);
338         break;
339     }
340
341     case 1:
342     {
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;
350
351         /* sizes of the field buffers in WCHARS */
352         int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
353
354         FIXME("Level 1 processing is partially implemented\n");
355         oth_domains_sz = 1;
356         logon_server_sz = 1;
357
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;
362
363         ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
364         NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
365                                  POLICY_VIEW_LOCAL_INFORMATION,
366                                  &PolicyHandle);
367         if (NtStatus != STATUS_SUCCESS)
368         {
369             TRACE("LsaOpenPolicyFailed with NT status %x\n",
370                 LsaNtStatusToWinError(NtStatus));
371             NetApiBufferFree(ui0);
372             return ERROR_NOT_ENOUGH_MEMORY;
373         }
374         LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
375                                   (PVOID*) &DomainInfo);
376         logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
377         LsaClose(PolicyHandle);
378
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));
394
395         /* get data */
396         dwSize = username_sz;
397         lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
398         NetApiBufferFree(ui0);
399
400         lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
401                 logon_domain_sz);
402         LsaFreeMemory(DomainInfo);
403
404         /* FIXME. Not implemented. Populated with empty strings */
405         ui->wkui1_oth_domains[0] = 0;
406         ui->wkui1_logon_server[0] = 0;
407         break;
408     }
409     case 1101:
410     {
411         PWKSTA_USER_INFO_1101 ui;
412         DWORD dwSize = 1;
413
414         FIXME("Stub. Level 1101 processing is not implemented\n");
415         /* FIXME see also wkui1_oth_domains for level 1 */
416
417         /* set up buffer */
418         NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
419                              (LPVOID *) bufptr);
420
421         ui = (PWKSTA_USER_INFO_1101) *bufptr;
422         ui->wkui1101_oth_domains = (LPWSTR)(ui + 1);
423
424         /* get data */
425         ui->wkui1101_oth_domains[0] = 0;
426         break;
427     }
428     default:
429         TRACE("Invalid level %d is specified\n", level);
430         return ERROR_INVALID_LEVEL;
431     }
432     return NERR_Success;
433 }
434
435 /************************************************************
436  *                NetpGetComputerName  (NETAPI32.@)
437  */
438 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
439 {
440     DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
441
442     TRACE("(%p)\n", Buffer);
443     NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
444     if (GetComputerNameW(*Buffer,  &dwSize))
445     {
446         NetApiBufferReallocate(
447             *Buffer, (dwSize + 1) * sizeof(WCHAR),
448             (LPVOID *) Buffer);
449         return NERR_Success;
450     }
451     else
452     {
453         NetApiBufferFree(*Buffer);
454         return ERROR_NOT_ENOUGH_MEMORY;
455     }
456 }
457
458 NET_API_STATUS WINAPI I_NetNameCompare(LPVOID p1, LPWSTR wkgrp, LPWSTR comp,
459  LPVOID p4, LPVOID p5)
460 {
461     FIXME("(%p %s %s %p %p): stub\n", p1, debugstr_w(wkgrp), debugstr_w(comp),
462      p4, p5);
463     return ERROR_INVALID_PARAMETER;
464 }
465
466 NET_API_STATUS WINAPI I_NetNameValidate(LPVOID p1, LPWSTR wkgrp, LPVOID p3,
467  LPVOID p4)
468 {
469     FIXME("(%p %s %p %p): stub\n", p1, debugstr_w(wkgrp), p3, p4);
470     return ERROR_INVALID_PARAMETER;
471 }
472
473 NET_API_STATUS WINAPI NetWkstaGetInfo( LPWSTR servername, DWORD level,
474                                        LPBYTE* bufptr)
475 {
476     NET_API_STATUS ret;
477
478     TRACE("%s %d %p\n", debugstr_w( servername ), level, bufptr );
479     if (servername)
480     {
481         if (!NETAPI_IsLocalComputer(servername))
482         {
483             FIXME("remote computers not supported\n");
484             return ERROR_INVALID_LEVEL;
485         }
486     }
487     if (!bufptr) return ERROR_INVALID_PARAMETER;
488
489     switch (level)
490     {
491         case 100:
492         case 101:
493         case 102:
494         {
495             static const WCHAR lanroot[] = {'c',':','\\','l','a','n','m','a','n',0};  /* FIXME */
496             DWORD computerNameLen, domainNameLen, size;
497             WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
498             LSA_OBJECT_ATTRIBUTES ObjectAttributes;
499             LSA_HANDLE PolicyHandle;
500             NTSTATUS NtStatus;
501            
502             computerNameLen = MAX_COMPUTERNAME_LENGTH + 1;
503             GetComputerNameW(computerName, &computerNameLen);
504             computerNameLen++; /* include NULL terminator */
505
506             ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
507             NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
508              POLICY_VIEW_LOCAL_INFORMATION, &PolicyHandle);
509             if (NtStatus != STATUS_SUCCESS)
510                 ret = LsaNtStatusToWinError(NtStatus);
511             else
512             {
513                 PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
514
515                 LsaQueryInformationPolicy(PolicyHandle,
516                  PolicyAccountDomainInformation, (PVOID*)&DomainInfo);
517                 domainNameLen = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
518                 size = sizeof(WKSTA_INFO_102) + computerNameLen * sizeof(WCHAR)
519                     + domainNameLen * sizeof(WCHAR) + sizeof(lanroot);
520                 ret = NetApiBufferAllocate(size, (LPVOID *)bufptr);
521                 if (ret == NERR_Success)
522                 {
523                     /* INFO_100 and INFO_101 structures are subsets of INFO_102 */
524                     PWKSTA_INFO_102 info = (PWKSTA_INFO_102)*bufptr;
525                     OSVERSIONINFOW verInfo;
526
527                     info->wki102_platform_id = PLATFORM_ID_NT;
528                     info->wki102_computername = (LPWSTR)(*bufptr +
529                      sizeof(WKSTA_INFO_102));
530                     memcpy(info->wki102_computername, computerName,
531                      computerNameLen * sizeof(WCHAR));
532                     info->wki102_langroup = info->wki102_computername + computerNameLen;
533                     memcpy(info->wki102_langroup, DomainInfo->DomainName.Buffer,
534                      domainNameLen * sizeof(WCHAR));
535                     info->wki102_lanroot = info->wki102_langroup + domainNameLen;
536                     memcpy(info->wki102_lanroot, lanroot, sizeof(lanroot));
537                     memset(&verInfo, 0, sizeof(verInfo));
538                     verInfo.dwOSVersionInfoSize = sizeof(verInfo);
539                     GetVersionExW(&verInfo);
540                     info->wki102_ver_major = verInfo.dwMajorVersion;
541                     info->wki102_ver_minor = verInfo.dwMinorVersion;
542                     info->wki102_logged_on_users = 1;
543                 }
544                 LsaFreeMemory(DomainInfo);
545                 LsaClose(PolicyHandle);
546             }
547             break;
548         }
549
550         default:
551             FIXME("level %d unimplemented\n", level);
552             ret = ERROR_INVALID_LEVEL;
553     }
554     return ret;
555 }
556
557 /************************************************************
558  *                NetGetJoinInformation (NETAPI32.@)
559  */
560 NET_API_STATUS NET_API_FUNCTION NetGetJoinInformation(
561     LPCWSTR Server,
562     LPWSTR *Name,
563     PNETSETUP_JOIN_STATUS type)
564 {
565     FIXME("Stub %s %p %p\n", wine_dbgstr_w(Server), Name, type);
566
567     *Name = NULL;
568     *type = NetSetupUnknownStatus;
569
570     return NERR_Success;
571 }