Implemented simple caching of client connections.
[wine] / dlls / netapi32 / wksta.c
1 /*
2  * Copyright 2002 Andriy Palamarchuk
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 "winbase.h"
22 #include "nb30.h"
23 #include "lmcons.h"
24 #include "lmapibuf.h"
25 #include "lmerr.h"
26 #include "lmwksta.h"
27 #include "winerror.h"
28 #include "winternl.h"
29 #include "ntsecapi.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
33
34 /************************************************************
35  *                NETAPI_IsLocalComputer
36  *
37  * Checks whether the server name indicates local machine.
38  */
39 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName)
40 {
41     if (!ServerName)
42     {
43         return TRUE;
44     }
45     else
46     {
47         DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
48         BOOL Result;
49         LPWSTR buf;
50
51         NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &buf);
52         Result = GetComputerNameW(buf,  &dwSize);
53         if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
54             ServerName += 2;
55         Result = Result && !lstrcmpW(ServerName, buf);
56         NetApiBufferFree(buf);
57
58         return Result;
59     }
60 }
61
62 int enum_hw(void);
63 void wprint_mac(WCHAR* buffer, int index);
64
65 NET_API_STATUS WINAPI 
66 NetWkstaTransportEnum(LPCWSTR ServerName, DWORD level, LPBYTE* pbuf,
67                       DWORD prefmaxlen, LPDWORD read_entries,
68                       LPDWORD total_entries, LPDWORD hresume)
69 {
70   FIXME(":%s, 0x%08lx, %p, 0x%08lx, %p, %p, %p\n", debugstr_w(ServerName), 
71         level, pbuf, prefmaxlen, read_entries, total_entries,hresume);
72   if (!NETAPI_IsLocalComputer(ServerName))
73     {
74       FIXME(":not implemented for non-local computers\n");
75       return ERROR_INVALID_LEVEL;
76     }
77   else
78     {
79       if (hresume && *hresume)
80         {
81           FIXME(":resume handle not implemented\n");
82           return ERROR_INVALID_LEVEL;
83         }
84         
85       switch (level)
86         {
87         case 0: /* transport info */
88           {
89             PWKSTA_TRANSPORT_INFO_0 ti;
90             int i,size_needed,n_adapt  = enum_hw();
91             
92             size_needed = n_adapt * (sizeof(WKSTA_TRANSPORT_INFO_0) 
93                                      * 13 * sizeof (WCHAR));
94             if (prefmaxlen == MAX_PREFERRED_LENGTH)
95               NetApiBufferAllocate( size_needed, (LPVOID *) pbuf);
96             else
97               {
98                 if (size_needed > prefmaxlen)
99                   return ERROR_MORE_DATA;
100                 NetApiBufferAllocate(prefmaxlen,
101                                      (LPVOID *) pbuf);
102               }
103             for (i = 0; i <n_adapt; i++)
104               {
105                 ti = (PWKSTA_TRANSPORT_INFO_0) 
106                   ((PBYTE) *pbuf + i * sizeof(WKSTA_TRANSPORT_INFO_0));
107                 ti->wkti0_quality_of_service=0;
108                 ti->wkti0_number_of_vcs=0;
109                 ti->wkti0_transport_name=NULL;
110                 ti->wkti0_transport_address= (LPWSTR)
111                   ((PBYTE )*pbuf + n_adapt* sizeof(WKSTA_TRANSPORT_INFO_0) 
112                    + i * 13 * sizeof (WCHAR));
113                 ti->wkti0_wan_ish=TRUE; /*TCPIP/NETBIOS Protocoll*/
114                 wprint_mac(ti->wkti0_transport_address,i);
115                 TRACE("%d of %d:ti at %p transport_address at %p %s\n",i,n_adapt,
116                       ti, ti->wkti0_transport_address, debugstr_w(ti->wkti0_transport_address));
117               }
118             if(read_entries)*read_entries = n_adapt;
119             if(total_entries)*total_entries = n_adapt;
120             if(hresume) *hresume= 0;
121             break;
122           }
123         default:
124           ERR("Invalid level %ld is specified\n", level);
125           return ERROR_INVALID_LEVEL;
126         }
127       return NERR_Success;
128     }
129 }
130                                             
131
132 /************************************************************
133  *                NetWkstaUserGetInfo  (NETAPI32.@)
134  */
135 NET_API_STATUS WINAPI NetWkstaUserGetInfo(LPWSTR reserved, DWORD level,
136                                           PBYTE* bufptr)
137 {
138     TRACE("(%s, %ld, %p)\n", debugstr_w(reserved), level, bufptr);
139     switch (level)
140     {
141     case 0:
142     {
143         PWKSTA_USER_INFO_0 ui;
144         DWORD dwSize = UNLEN + 1;
145
146         /* set up buffer */
147         NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_0) + dwSize * sizeof(WCHAR),
148                              (LPVOID *) bufptr);
149
150         ui = (PWKSTA_USER_INFO_0) *bufptr;
151         ui->wkui0_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_0));
152
153         /* get data */
154         if (!GetUserNameW(ui->wkui0_username, &dwSize))
155         {
156             NetApiBufferFree(ui);
157             return ERROR_NOT_ENOUGH_MEMORY;
158         }
159         else
160             NetApiBufferReallocate(
161                 *bufptr, sizeof(WKSTA_USER_INFO_0) +
162                 (lstrlenW(ui->wkui0_username) + 1) * sizeof(WCHAR),
163                 (LPVOID *) bufptr);
164         break;
165     }
166
167     case 1:
168     {
169         PWKSTA_USER_INFO_1 ui;
170         PWKSTA_USER_INFO_0 ui0;
171         DWORD dwSize;
172         LSA_OBJECT_ATTRIBUTES ObjectAttributes;
173         LSA_HANDLE PolicyHandle;
174         PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
175         NTSTATUS NtStatus;
176
177         /* sizes of the field buffers in WCHARS */
178         int username_sz, logon_domain_sz, oth_domains_sz, logon_server_sz;
179
180         FIXME("Level 1 processing is partially implemented\n");
181         oth_domains_sz = 1;
182         logon_server_sz = 1;
183
184         /* get some information first to estimate size of the buffer */
185         ui0 = NULL;
186         NetWkstaUserGetInfo(NULL, 0, (PBYTE *) &ui0);
187         username_sz = lstrlenW(ui0->wkui0_username) + 1;
188
189         ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
190         NtStatus = LsaOpenPolicy(NULL, &ObjectAttributes,
191                                  POLICY_VIEW_LOCAL_INFORMATION,
192                                  &PolicyHandle);
193         if (NtStatus != STATUS_SUCCESS)
194         {
195             ERR("LsaOpenPolicyFailed with NT status %lx\n",
196                 LsaNtStatusToWinError(NtStatus));
197             NetApiBufferFree(ui0);
198             return ERROR_NOT_ENOUGH_MEMORY;
199         }
200         LsaQueryInformationPolicy(PolicyHandle, PolicyAccountDomainInformation,
201                                   (PVOID*) &DomainInfo);
202         logon_domain_sz = lstrlenW(DomainInfo->DomainName.Buffer) + 1;
203         LsaClose(PolicyHandle);
204
205         /* set up buffer */
206         NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1) +
207                              (username_sz + logon_domain_sz +
208                               oth_domains_sz + logon_server_sz) * sizeof(WCHAR),
209                              (LPVOID *) bufptr);
210         ui = (WKSTA_USER_INFO_1 *) *bufptr;
211         ui->wkui1_username = (LPWSTR) (*bufptr + sizeof(WKSTA_USER_INFO_1));
212         ui->wkui1_logon_domain = (LPWSTR) (
213             ((PBYTE) ui->wkui1_username) + username_sz * sizeof(WCHAR));
214         ui->wkui1_oth_domains = (LPWSTR) (
215             ((PBYTE) ui->wkui1_logon_domain) +
216             logon_domain_sz * sizeof(WCHAR));
217         ui->wkui1_logon_server = (LPWSTR) (
218             ((PBYTE) ui->wkui1_oth_domains) +
219             oth_domains_sz * sizeof(WCHAR));
220
221         /* get data */
222         dwSize = username_sz;
223         lstrcpyW(ui->wkui1_username, ui0->wkui0_username);
224         NetApiBufferFree(ui0);
225
226         lstrcpynW(ui->wkui1_logon_domain, DomainInfo->DomainName.Buffer,
227                 logon_domain_sz);
228         LsaFreeMemory(DomainInfo);
229
230         /* FIXME. Not implemented. Populated with empty strings */
231         ui->wkui1_oth_domains[0] = 0;
232         ui->wkui1_logon_server[0] = 0;
233         break;
234     }
235     case 1101:
236     {
237         PWKSTA_USER_INFO_1101 ui;
238         DWORD dwSize = 1;
239
240         FIXME("Stub. Level 1101 processing is not implemented\n");
241         /* FIXME see also wkui1_oth_domains for level 1 */
242
243         /* set up buffer */
244         NetApiBufferAllocate(sizeof(WKSTA_USER_INFO_1101) + dwSize * sizeof(WCHAR),
245                              (LPVOID *) bufptr);
246
247         ui = (PWKSTA_USER_INFO_1101) *bufptr;
248         ui->wkui1101_oth_domains = (LPWSTR)(ui + 1);
249
250         /* get data */
251         ui->wkui1101_oth_domains[0] = 0;
252         break;
253     }
254     default:
255         ERR("Invalid level %ld is specified\n", level);
256         return ERROR_INVALID_LEVEL;
257     }
258     return NERR_Success;
259 }
260
261 /************************************************************
262  *                NetpGetComputerName  (NETAPI32.@)
263  */
264 NET_API_STATUS WINAPI NetpGetComputerName(LPWSTR *Buffer)
265 {
266     DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
267
268     TRACE("(%p)\n", Buffer);
269     NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) Buffer);
270     if (GetComputerNameW(*Buffer,  &dwSize))
271     {
272         NetApiBufferReallocate(
273             *Buffer, dwSize * sizeof(WCHAR),
274             (LPVOID *) Buffer);
275         return NERR_Success;
276     }
277     else
278     {
279         NetApiBufferFree(*Buffer);
280         return ERROR_NOT_ENOUGH_MEMORY;
281     }
282 }