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