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