- Add some stubs needed by native Windows DLLs.
[reactos.git] / reactos / lib / advapi32 / sec / misc.c
1 /* $Id: misc.c,v 1.15 2004/05/13 20:42:28 navaraf Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/advapi32/sec/misc.c
6 * PURPOSE: Miscellaneous security functions
7 */
8
9 #define NTOS_MODE_USER
10 #include <ntos.h>
11 #include <windows.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16
17 /*
18 * @implemented
19 */
20 BOOL STDCALL
21 AreAllAccessesGranted(DWORD GrantedAccess,
22 DWORD DesiredAccess)
23 {
24 return((BOOL)RtlAreAllAccessesGranted(GrantedAccess,
25 DesiredAccess));
26 }
27
28
29 /*
30 * @implemented
31 */
32 BOOL STDCALL
33 AreAnyAccessesGranted(DWORD GrantedAccess,
34 DWORD DesiredAccess)
35 {
36 return((BOOL)RtlAreAnyAccessesGranted(GrantedAccess,
37 DesiredAccess));
38 }
39
40
41 /******************************************************************************
42 * GetFileSecurityA [ADVAPI32.@]
43 *
44 * Obtains Specified information about the security of a file or directory.
45 *
46 * PARAMS
47 * lpFileName [I] Name of the file to get info for
48 * RequestedInformation [I] SE_ flags from "winnt.h"
49 * pSecurityDescriptor [O] Destination for security information
50 * nLength [I] Length of pSecurityDescriptor
51 * lpnLengthNeeded [O] Destination for length of returned security information
52 *
53 * RETURNS
54 * Success: TRUE. pSecurityDescriptor contains the requested information.
55 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
56 *
57 * NOTES
58 * The information returned is constrained by the callers access rights and
59 * privileges.
60 *
61 * @unimplemented
62 */
63 BOOL WINAPI
64 GetFileSecurityA (LPCSTR lpFileName,
65 SECURITY_INFORMATION RequestedInformation,
66 PSECURITY_DESCRIPTOR pSecurityDescriptor,
67 DWORD nLength,
68 LPDWORD lpnLengthNeeded)
69 {
70 DPRINT("GetFileSecurityA: stub\n");
71 return TRUE;
72 }
73
74 /*
75 * @unimplemented
76 */
77 BOOL WINAPI
78 GetFileSecurityW (LPCWSTR lpFileName,
79 SECURITY_INFORMATION RequestedInformation,
80 PSECURITY_DESCRIPTOR pSecurityDescriptor,
81 DWORD nLength, LPDWORD lpnLengthNeeded)
82 {
83 DPRINT("GetFileSecurityW: stub\n");
84 return TRUE;
85 }
86
87 /*
88 * @implemented
89 */
90 BOOL STDCALL
91 GetKernelObjectSecurity(HANDLE Handle,
92 SECURITY_INFORMATION RequestedInformation,
93 PSECURITY_DESCRIPTOR pSecurityDescriptor,
94 DWORD nLength,
95 LPDWORD lpnLengthNeeded)
96 {
97 NTSTATUS Status;
98
99 Status = NtQuerySecurityObject(Handle,
100 RequestedInformation,
101 pSecurityDescriptor,
102 nLength,
103 lpnLengthNeeded);
104 if (!NT_SUCCESS(Status))
105 {
106 SetLastError(RtlNtStatusToDosError(Status));
107 return(FALSE);
108 }
109 return(TRUE);
110 }
111
112
113 /******************************************************************************
114 * SetFileSecurityA [ADVAPI32.@]
115 * Sets the security of a file or directory
116 *
117 * @unimplemented
118 */
119 BOOL STDCALL
120 SetFileSecurityA (LPCSTR lpFileName,
121 SECURITY_INFORMATION RequestedInformation,
122 PSECURITY_DESCRIPTOR pSecurityDescriptor)
123 {
124 DPRINT("SetFileSecurityA : stub\n");
125 return TRUE;
126 }
127
128
129 /*
130 * @implemented
131 */
132 BOOL STDCALL
133 SetKernelObjectSecurity(HANDLE Handle,
134 SECURITY_INFORMATION SecurityInformation,
135 PSECURITY_DESCRIPTOR SecurityDescriptor)
136 {
137 NTSTATUS Status;
138
139 Status = NtSetSecurityObject(Handle,
140 SecurityInformation,
141 SecurityDescriptor);
142 if (!NT_SUCCESS(Status))
143 {
144 SetLastError(RtlNtStatusToDosError(Status));
145 return FALSE;
146 }
147 return TRUE;
148 }
149
150
151 /*
152 * @implemented
153 */
154 VOID STDCALL
155 MapGenericMask(PDWORD AccessMask,
156 PGENERIC_MAPPING GenericMapping)
157 {
158 RtlMapGenericMask(AccessMask,
159 GenericMapping);
160 }
161
162
163 /*
164 * @implemented
165 */
166 BOOL STDCALL
167 ImpersonateLoggedOnUser(HANDLE hToken)
168 {
169 SECURITY_QUALITY_OF_SERVICE Qos;
170 OBJECT_ATTRIBUTES ObjectAttributes;
171 HANDLE NewToken;
172 TOKEN_TYPE Type;
173 ULONG ReturnLength;
174 BOOL Duplicated;
175 NTSTATUS Status;
176
177 /* Get the token type */
178 Status = NtQueryInformationToken (hToken,
179 TokenType,
180 &Type,
181 sizeof(TOKEN_TYPE),
182 &ReturnLength);
183 if (!NT_SUCCESS(Status))
184 {
185 SetLastError (RtlNtStatusToDosError (Status));
186 return FALSE;
187 }
188
189 if (Type == TokenPrimary)
190 {
191 /* Create a duplicate impersonation token */
192 Qos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
193 Qos.ImpersonationLevel = SecurityImpersonation;
194 Qos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
195 Qos.EffectiveOnly = FALSE;
196
197 ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
198 ObjectAttributes.RootDirectory = NULL;
199 ObjectAttributes.ObjectName = NULL;
200 ObjectAttributes.Attributes = 0;
201 ObjectAttributes.SecurityDescriptor = NULL;
202 ObjectAttributes.SecurityQualityOfService = &Qos;
203
204 Status = NtDuplicateToken (hToken,
205 TOKEN_IMPERSONATE | TOKEN_QUERY,
206 &ObjectAttributes,
207 FALSE,
208 TokenImpersonation,
209 &NewToken);
210 if (!NT_SUCCESS(Status))
211 {
212 SetLastError (RtlNtStatusToDosError (Status));
213 return FALSE;
214 }
215
216 Duplicated = TRUE;
217 }
218 else
219 {
220 /* User the original impersonation token */
221 NewToken = hToken;
222 Duplicated = FALSE;
223 }
224
225 /* Impersonate the the current thread */
226 Status = NtSetInformationThread (NtCurrentThread (),
227 ThreadImpersonationToken,
228 NewToken,
229 sizeof(HANDLE));
230
231 if (Duplicated == TRUE)
232 {
233 NtClose (NewToken);
234 }
235
236 if (!NT_SUCCESS(Status))
237 {
238 SetLastError (RtlNtStatusToDosError (Status));
239 return FALSE;
240 }
241
242 return TRUE;
243 }
244
245
246 /*
247 * @implemented
248 */
249 BOOL STDCALL
250 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel)
251 {
252 NTSTATUS Status;
253
254 Status = RtlImpersonateSelf(ImpersonationLevel);
255 if (!NT_SUCCESS(Status))
256 {
257 SetLastError(RtlNtStatusToDosError(Status));
258 return FALSE;
259 }
260 return TRUE;
261 }
262
263
264 /*
265 * @implemented
266 */
267 BOOL STDCALL
268 RevertToSelf(VOID)
269 {
270 NTSTATUS Status;
271 HANDLE Token = NULL;
272
273 Status = NtSetInformationThread(NtCurrentThread(),
274 ThreadImpersonationToken,
275 &Token,
276 sizeof(HANDLE));
277 if (!NT_SUCCESS(Status))
278 {
279 SetLastError(RtlNtStatusToDosError(Status));
280 return FALSE;
281 }
282 return TRUE;
283 }
284
285
286 /******************************************************************************
287 * GetUserNameA [ADVAPI32.@]
288 *
289 * Get the current user name.
290 *
291 * PARAMS
292 * lpszName [O] Destination for the user name.
293 * lpSize [I/O] Size of lpszName.
294 *
295 * RETURNS
296 * Success: The length of the user name, including terminating NUL.
297 * Failure: ERROR_MORE_DATA if *lpSize is too small.
298 *
299 * @unimplemented
300 */
301 BOOL WINAPI
302 GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
303 {
304 // size_t len;
305 // char name[] = { "Administrator" };
306
307 /* We need to include the null character when determining the size of the buffer. */
308 // len = strlen(name) + 1;
309 // if (len > *lpSize)
310 // {
311 // SetLastError(ERROR_MORE_DATA);
312 // *lpSize = len;
313 // return 0;
314 // }
315
316 // *lpSize = len;
317 // strcpy(lpszName, name);
318 return TRUE;
319 }
320
321 /******************************************************************************
322 * GetUserNameW [ADVAPI32.@]
323 *
324 * See GetUserNameA.
325 *
326 * @unimplemented
327 */
328 BOOL WINAPI
329 GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
330 {
331 // char name[] = { "Administrator" };
332
333 // DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
334
335 // if (len > *lpSize)
336 // {
337 // SetLastError(ERROR_MORE_DATA);
338 // *lpSize = len;
339 // return FALSE;
340 // }
341
342 // *lpSize = len;
343 // MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, len );
344 return TRUE;
345 }
346
347
348 /******************************************************************************
349 * LookupAccountSidA [ADVAPI32.@]
350 *
351 * @unimplemented
352 */
353 BOOL STDCALL
354 LookupAccountSidA (LPCSTR lpSystemName,
355 PSID lpSid,
356 LPSTR lpName,
357 LPDWORD cchName,
358 LPSTR lpReferencedDomainName,
359 LPDWORD cchReferencedDomainName,
360 PSID_NAME_USE peUse)
361 {
362 DPRINT1("LookupAccountSidA is unimplemented, but returns success\n");
363 return TRUE;
364 }
365
366
367 /******************************************************************************
368 * LookupAccountSidW [ADVAPI32.@]
369 *
370 * @unimplemented
371 */
372 BOOL STDCALL
373 LookupAccountSidW (LPCWSTR lpSystemName,
374 PSID lpSid,
375 LPWSTR lpName,
376 LPDWORD cchName,
377 LPWSTR lpReferencedDomainName,
378 LPDWORD cchReferencedDomainName,
379 PSID_NAME_USE peUse)
380 {
381 DPRINT1("LookupAccountSidW is unimplemented, but returns success\n");
382 return TRUE;
383 }
384
385
386 /**********************************************************************
387 * LookupPrivilegeValueA EXPORTED
388 *
389 * @implemented
390 */
391 BOOL STDCALL
392 LookupPrivilegeValueA (LPCSTR lpSystemName,
393 LPCSTR lpName,
394 PLUID lpLuid)
395 {
396 UNICODE_STRING SystemName;
397 UNICODE_STRING Name;
398 BOOL Result;
399
400 /* Remote system? */
401 if (lpSystemName != NULL)
402 {
403 RtlCreateUnicodeStringFromAsciiz (&SystemName,
404 (LPSTR)lpSystemName);
405 }
406
407 /* Check the privilege name is not NULL */
408 if (lpName == NULL)
409 {
410 SetLastError (ERROR_INVALID_PARAMETER);
411 return FALSE;
412 }
413
414 RtlCreateUnicodeStringFromAsciiz (&Name,
415 (LPSTR)lpName);
416
417 Result = LookupPrivilegeValueW ((lpSystemName != NULL) ? SystemName.Buffer : NULL,
418 Name.Buffer,
419 lpLuid);
420
421 RtlFreeUnicodeString (&Name);
422
423 /* Remote system? */
424 if (lpSystemName != NULL)
425 {
426 RtlFreeUnicodeString (&SystemName);
427 }
428
429 return Result;
430 }
431
432
433 /**********************************************************************
434 * LookupPrivilegeValueW EXPORTED
435 *
436 * @unimplemented
437 */
438 BOOL STDCALL
439 LookupPrivilegeValueW (LPCWSTR lpSystemName,
440 LPCWSTR lpName,
441 PLUID lpLuid)
442 {
443 return FALSE;
444 }
445
446
447 /**********************************************************************
448 * LookupPrivilegeDisplayNameA EXPORTED
449 *
450 * @unimplemented
451 */
452 BOOL STDCALL
453 LookupPrivilegeDisplayNameA (LPCSTR lpSystemName,
454 LPCSTR lpName,
455 LPSTR lpDisplayName,
456 LPDWORD cbDisplayName,
457 LPDWORD lpLanguageId)
458 {
459 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
460 return FALSE;
461 }
462
463
464 /**********************************************************************
465 * LookupPrivilegeDisplayNameW EXPORTED
466 *
467 * @unimplemented
468 */
469 BOOL STDCALL
470 LookupPrivilegeDisplayNameW (LPCWSTR lpSystemName,
471 LPCWSTR lpName,
472 LPWSTR lpDisplayName,
473 LPDWORD cbDisplayName,
474 LPDWORD lpLanguageId)
475 {
476 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
477 return FALSE;
478 }
479
480
481 /**********************************************************************
482 * LookupPrivilegeNameA EXPORTED
483 *
484 * @unimplemented
485 */
486 BOOL STDCALL
487 LookupPrivilegeNameA (LPCSTR lpSystemName,
488 PLUID lpLuid,
489 LPSTR lpName,
490 LPDWORD cbName)
491 {
492 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
493 return FALSE;
494 }
495
496
497 /**********************************************************************
498 * LookupPrivilegeNameW EXPORTED
499 *
500 * @unimplemented
501 */
502 BOOL STDCALL
503 LookupPrivilegeNameW (LPCWSTR lpSystemName,
504 PLUID lpLuid,
505 LPWSTR lpName,
506 LPDWORD cbName)
507 {
508 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
509 return FALSE;
510 }
511
512 /* EOF */