496f40580ed31a60248261fee300b08c0d2d7671
[reactos.git] / reactos / dll / win32 / netapi32 / access.c
1 /*
2 * Copyright 2002 Andriy Palamarchuk
3 *
4 * netapi32 access 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 <stdarg.h>
22
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "lmcons.h"
29 #include "lmaccess.h"
30 #include "lmapibuf.h"
31 #include "lmerr.h"
32 #include "lmuse.h"
33 #include "ntsecapi.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36 #include "wine/list.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(netapi32);
39
40 BOOL NETAPI_IsLocalComputer(LPCWSTR ServerName);
41
42 /************************************************************
43 * ACCESS_QueryAdminDisplayInformation
44 *
45 * Creates a buffer with information for the Admin User
46 */
47 static void ACCESS_QueryAdminDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
48 {
49 static const WCHAR sAdminUserName[] = {
50 'A','d','m','i','n','i','s','t','r','a','t','o','r',0};
51
52 /* sizes of the field buffers in WCHARS */
53 int name_sz, comment_sz, full_name_sz;
54 PNET_DISPLAY_USER usr;
55
56 /* set up buffer */
57 name_sz = lstrlenW(sAdminUserName) + 1;
58 comment_sz = 1;
59 full_name_sz = 1;
60
61 *pdwSize = sizeof(NET_DISPLAY_USER);
62 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
63 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
64
65 usr = *buf;
66 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
67 usr->usri1_comment = (LPWSTR) (
68 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
69 usr->usri1_full_name = (LPWSTR) (
70 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
71
72 /* set data */
73 lstrcpyW(usr->usri1_name, sAdminUserName);
74 usr->usri1_comment[0] = 0;
75 usr->usri1_flags = UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
76 usr->usri1_full_name[0] = 0;
77 usr->usri1_user_id = DOMAIN_USER_RID_ADMIN;
78 usr->usri1_next_index = 0;
79 }
80
81 /************************************************************
82 * ACCESS_QueryGuestDisplayInformation
83 *
84 * Creates a buffer with information for the Guest User
85 */
86 static void ACCESS_QueryGuestDisplayInformation(PNET_DISPLAY_USER *buf, PDWORD pdwSize)
87 {
88 static const WCHAR sGuestUserName[] = {
89 'G','u','e','s','t',0 };
90
91 /* sizes of the field buffers in WCHARS */
92 int name_sz, comment_sz, full_name_sz;
93 PNET_DISPLAY_USER usr;
94
95 /* set up buffer */
96 name_sz = lstrlenW(sGuestUserName) + 1;
97 comment_sz = 1;
98 full_name_sz = 1;
99
100 *pdwSize = sizeof(NET_DISPLAY_USER);
101 *pdwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
102 NetApiBufferAllocate(*pdwSize, (LPVOID *) buf);
103
104 usr = *buf;
105 usr->usri1_name = (LPWSTR) ((PBYTE) usr + sizeof(NET_DISPLAY_USER));
106 usr->usri1_comment = (LPWSTR) (
107 ((PBYTE) usr->usri1_name) + name_sz * sizeof(WCHAR));
108 usr->usri1_full_name = (LPWSTR) (
109 ((PBYTE) usr->usri1_comment) + comment_sz * sizeof(WCHAR));
110
111 /* set data */
112 lstrcpyW(usr->usri1_name, sGuestUserName);
113 usr->usri1_comment[0] = 0;
114 usr->usri1_flags = UF_ACCOUNTDISABLE | UF_SCRIPT | UF_NORMAL_ACCOUNT |
115 UF_DONT_EXPIRE_PASSWD;
116 usr->usri1_full_name[0] = 0;
117 usr->usri1_user_id = DOMAIN_USER_RID_GUEST;
118 usr->usri1_next_index = 0;
119 }
120
121 /************************************************************
122 * Copies NET_DISPLAY_USER record.
123 */
124 static void ACCESS_CopyDisplayUser(const NET_DISPLAY_USER *dest, LPWSTR *dest_buf,
125 PNET_DISPLAY_USER src)
126 {
127 LPWSTR str = *dest_buf;
128
129 src->usri1_name = str;
130 lstrcpyW(src->usri1_name, dest->usri1_name);
131 str = (LPWSTR) (
132 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
133
134 src->usri1_comment = str;
135 lstrcpyW(src->usri1_comment, dest->usri1_comment);
136 str = (LPWSTR) (
137 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
138
139 src->usri1_flags = dest->usri1_flags;
140
141 src->usri1_full_name = str;
142 lstrcpyW(src->usri1_full_name, dest->usri1_full_name);
143 str = (LPWSTR) (
144 ((PBYTE) str) + (lstrlenW(str) + 1) * sizeof(WCHAR));
145
146 src->usri1_user_id = dest->usri1_user_id;
147 src->usri1_next_index = dest->usri1_next_index;
148 *dest_buf = str;
149 }
150
151 /************************************************************
152 * NetQueryDisplayInformation (NETAPI32.@)
153 *
154 * The buffer structure:
155 * - array of fixed size record of the level type
156 * - strings, referenced by the record of the level type
157 */
158 NET_API_STATUS WINAPI
159 NetQueryDisplayInformation(
160 LPCWSTR ServerName, DWORD Level, DWORD Index, DWORD EntriesRequested,
161 DWORD PreferredMaximumLength, LPDWORD ReturnedEntryCount,
162 PVOID *SortedBuffer)
163 {
164 TRACE("(%s, %d, %d, %d, %d, %p, %p)\n", debugstr_w(ServerName),
165 Level, Index, EntriesRequested, PreferredMaximumLength,
166 ReturnedEntryCount, SortedBuffer);
167
168 if(!NETAPI_IsLocalComputer(ServerName))
169 {
170 FIXME("Only implemented on local computer, but requested for "
171 "remote server %s\n", debugstr_w(ServerName));
172 return ERROR_ACCESS_DENIED;
173 }
174
175 switch (Level)
176 {
177 case 1:
178 {
179 /* current record */
180 PNET_DISPLAY_USER inf;
181 /* current available strings buffer */
182 LPWSTR str;
183 PNET_DISPLAY_USER admin, guest;
184 DWORD admin_size, guest_size;
185 LPWSTR name = NULL;
186 DWORD dwSize;
187
188 /* sizes of the field buffers in WCHARS */
189 int name_sz, comment_sz, full_name_sz;
190
191 /* number of the records, returned in SortedBuffer
192 3 - for current user, Administrator and Guest users
193 */
194 int records = 3;
195
196 FIXME("Level %d partially implemented\n", Level);
197 *ReturnedEntryCount = records;
198 comment_sz = 1;
199 full_name_sz = 1;
200
201 /* get data */
202 dwSize = UNLEN + 1;
203 NetApiBufferAllocate(dwSize * sizeof(WCHAR), (LPVOID *) &name);
204 if (!GetUserNameW(name, &dwSize))
205 {
206 NetApiBufferFree(name);
207 return ERROR_ACCESS_DENIED;
208 }
209 name_sz = dwSize;
210 ACCESS_QueryAdminDisplayInformation(&admin, &admin_size);
211 ACCESS_QueryGuestDisplayInformation(&guest, &guest_size);
212
213 /* set up buffer */
214 dwSize = sizeof(NET_DISPLAY_USER) * records;
215 dwSize += (name_sz + comment_sz + full_name_sz) * sizeof(WCHAR);
216
217 NetApiBufferAllocate(dwSize +
218 admin_size - sizeof(NET_DISPLAY_USER) +
219 guest_size - sizeof(NET_DISPLAY_USER),
220 SortedBuffer);
221 inf = *SortedBuffer;
222 str = (LPWSTR) ((PBYTE) inf + sizeof(NET_DISPLAY_USER) * records);
223 inf->usri1_name = str;
224 str = (LPWSTR) (
225 ((PBYTE) str) + name_sz * sizeof(WCHAR));
226 inf->usri1_comment = str;
227 str = (LPWSTR) (
228 ((PBYTE) str) + comment_sz * sizeof(WCHAR));
229 inf->usri1_full_name = str;
230 str = (LPWSTR) (
231 ((PBYTE) str) + full_name_sz * sizeof(WCHAR));
232
233 /* set data */
234 lstrcpyW(inf->usri1_name, name);
235 NetApiBufferFree(name);
236 inf->usri1_comment[0] = 0;
237 inf->usri1_flags =
238 UF_SCRIPT | UF_NORMAL_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
239 inf->usri1_full_name[0] = 0;
240 inf->usri1_user_id = 0;
241 inf->usri1_next_index = 0;
242
243 inf++;
244 ACCESS_CopyDisplayUser(admin, &str, inf);
245 NetApiBufferFree(admin);
246
247 inf++;
248 ACCESS_CopyDisplayUser(guest, &str, inf);
249 NetApiBufferFree(guest);
250 break;
251 }
252
253 case 2:
254 case 3:
255 {
256 FIXME("Level %d is not implemented\n", Level);
257 break;
258 }
259
260 default:
261 TRACE("Invalid level %d is specified\n", Level);
262 return ERROR_INVALID_LEVEL;
263 }
264 return NERR_Success;
265 }
266
267 /************************************************************
268 * NetGetDCName (NETAPI32.@)
269 *
270 * Return the name of the primary domain controller (PDC)
271 */
272
273 NET_API_STATUS WINAPI
274 NetGetDCName(LPCWSTR servername, LPCWSTR domainname, LPBYTE *bufptr)
275 {
276 FIXME("(%s, %s, %p) stub!\n", debugstr_w(servername),
277 debugstr_w(domainname), bufptr);
278 return NERR_DCNotFound; /* say we can't find a domain controller */
279 }
280
281 /******************************************************************************
282 * NetUserModalsGet (NETAPI32.@)
283 *
284 * Retrieves global information for all users and global groups in the security
285 * database.
286 *
287 * PARAMS
288 * szServer [I] Specifies the DNS or the NetBIOS name of the remote server
289 * on which the function is to execute.
290 * level [I] Information level of the data.
291 * 0 Return global passwords parameters. bufptr points to a
292 * USER_MODALS_INFO_0 struct.
293 * 1 Return logon server and domain controller information. bufptr
294 * points to a USER_MODALS_INFO_1 struct.
295 * 2 Return domain name and identifier. bufptr points to a
296 * USER_MODALS_INFO_2 struct.
297 * 3 Return lockout information. bufptr points to a USER_MODALS_INFO_3
298 * struct.
299 * pbuffer [I] Buffer that receives the data.
300 *
301 * RETURNS
302 * Success: NERR_Success.
303 * Failure:
304 * ERROR_ACCESS_DENIED - the user does not have access to the info.
305 * NERR_InvalidComputer - computer name is invalid.
306 */
307 NET_API_STATUS WINAPI NetUserModalsGet(
308 LPCWSTR szServer, DWORD level, LPBYTE *pbuffer)
309 {
310 TRACE("(%s %d %p)\n", debugstr_w(szServer), level, pbuffer);
311
312 switch (level)
313 {
314 case 0:
315 /* return global passwords parameters */
316 FIXME("level 0 not implemented!\n");
317 *pbuffer = NULL;
318 return NERR_InternalError;
319 case 1:
320 /* return logon server and domain controller info */
321 FIXME("level 1 not implemented!\n");
322 *pbuffer = NULL;
323 return NERR_InternalError;
324 case 2:
325 {
326 /* return domain name and identifier */
327 PUSER_MODALS_INFO_2 umi;
328 LSA_HANDLE policyHandle;
329 LSA_OBJECT_ATTRIBUTES objectAttributes;
330 PPOLICY_ACCOUNT_DOMAIN_INFO domainInfo;
331 NTSTATUS ntStatus;
332 PSID domainIdentifier = NULL;
333 int domainNameLen, domainIdLen;
334
335 ZeroMemory(&objectAttributes, sizeof(objectAttributes));
336 objectAttributes.Length = sizeof(objectAttributes);
337
338 ntStatus = LsaOpenPolicy(NULL, &objectAttributes,
339 POLICY_VIEW_LOCAL_INFORMATION,
340 &policyHandle);
341 if (ntStatus != STATUS_SUCCESS)
342 {
343 WARN("LsaOpenPolicy failed with NT status %x\n",
344 LsaNtStatusToWinError(ntStatus));
345 return ntStatus;
346 }
347
348 ntStatus = LsaQueryInformationPolicy(policyHandle,
349 PolicyAccountDomainInformation,
350 (PVOID *)&domainInfo);
351 if (ntStatus != STATUS_SUCCESS)
352 {
353 WARN("LsaQueryInformationPolicy failed with NT status %x\n",
354 LsaNtStatusToWinError(ntStatus));
355 LsaClose(policyHandle);
356 return ntStatus;
357 }
358
359 domainIdentifier = domainInfo->DomainSid;
360 domainIdLen = (domainIdentifier) ? GetLengthSid(domainIdentifier) : 0;
361 domainNameLen = lstrlenW(domainInfo->DomainName.Buffer) + 1;
362 LsaClose(policyHandle);
363
364 ntStatus = NetApiBufferAllocate(sizeof(USER_MODALS_INFO_2) +
365 domainIdLen +
366 domainNameLen * sizeof(WCHAR),
367 (LPVOID *)pbuffer);
368
369 if (ntStatus != NERR_Success)
370 {
371 WARN("NetApiBufferAllocate() failed\n");
372 LsaFreeMemory(domainInfo);
373 return ntStatus;
374 }
375
376 umi = (USER_MODALS_INFO_2 *) *pbuffer;
377 umi->usrmod2_domain_id = (domainIdLen > 0) ? (*pbuffer + sizeof(USER_MODALS_INFO_2)) : NULL;
378 umi->usrmod2_domain_name = (LPWSTR)(*pbuffer +
379 sizeof(USER_MODALS_INFO_2) + domainIdLen);
380
381 lstrcpynW(umi->usrmod2_domain_name,
382 domainInfo->DomainName.Buffer,
383 domainNameLen);
384 if (domainIdLen > 0)
385 CopySid(GetLengthSid(domainIdentifier), umi->usrmod2_domain_id,
386 domainIdentifier);
387
388 LsaFreeMemory(domainInfo);
389
390 break;
391 }
392 case 3:
393 /* return lockout information */
394 FIXME("level 3 not implemented!\n");
395 *pbuffer = NULL;
396 return NERR_InternalError;
397 default:
398 TRACE("Invalid level %d is specified\n", level);
399 *pbuffer = NULL;
400 return ERROR_INVALID_LEVEL;
401 }
402
403 return NERR_Success;
404 }
405
406 NET_API_STATUS WINAPI NetUseAdd(LMSTR servername, DWORD level, LPBYTE bufptr, LPDWORD parm_err)
407 {
408 FIXME("%s %d %p %p stub\n", debugstr_w(servername), level, bufptr, parm_err);
409 return NERR_Success;
410 }