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