1 /* $Id: misc.c,v 1.19 2004/07/06 22:08:48 gvg Exp $
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
22 AreAllAccessesGranted(DWORD GrantedAccess
,
25 return((BOOL
)RtlAreAllAccessesGranted(GrantedAccess
,
34 AreAnyAccessesGranted(DWORD GrantedAccess
,
37 return((BOOL
)RtlAreAnyAccessesGranted(GrantedAccess
,
42 /******************************************************************************
43 * GetFileSecurityA [ADVAPI32.@]
45 * Obtains Specified information about the security of a file or directory.
48 * lpFileName [I] Name of the file to get info for
49 * RequestedInformation [I] SE_ flags from "winnt.h"
50 * pSecurityDescriptor [O] Destination for security information
51 * nLength [I] Length of pSecurityDescriptor
52 * lpnLengthNeeded [O] Destination for length of returned security information
55 * Success: TRUE. pSecurityDescriptor contains the requested information.
56 * Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
59 * The information returned is constrained by the callers access rights and
65 GetFileSecurityA (LPCSTR lpFileName
,
66 SECURITY_INFORMATION RequestedInformation
,
67 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
69 LPDWORD lpnLengthNeeded
)
71 DPRINT1("GetFileSecurityA: stub\n");
79 GetFileSecurityW (LPCWSTR lpFileName
,
80 SECURITY_INFORMATION RequestedInformation
,
81 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
82 DWORD nLength
, LPDWORD lpnLengthNeeded
)
84 DPRINT1("GetFileSecurityW: stub\n");
92 GetKernelObjectSecurity(HANDLE Handle
,
93 SECURITY_INFORMATION RequestedInformation
,
94 PSECURITY_DESCRIPTOR pSecurityDescriptor
,
96 LPDWORD lpnLengthNeeded
)
100 Status
= NtQuerySecurityObject(Handle
,
101 RequestedInformation
,
105 if (!NT_SUCCESS(Status
))
107 SetLastError(RtlNtStatusToDosError(Status
));
114 /******************************************************************************
115 * SetFileSecurityW [ADVAPI32.@]
116 * Sets the security of a file or directory
121 SetFileSecurityW (LPCWSTR lpFileName
,
122 SECURITY_INFORMATION RequestedInformation
,
123 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
125 DPRINT1("SetFileSecurityW : stub\n");
130 /******************************************************************************
131 * SetFileSecurityA [ADVAPI32.@]
132 * Sets the security of a file or directory
137 SetFileSecurityA (LPCSTR lpFileName
,
138 SECURITY_INFORMATION RequestedInformation
,
139 PSECURITY_DESCRIPTOR pSecurityDescriptor
)
141 DPRINT("SetFileSecurityA : stub\n");
150 SetKernelObjectSecurity(HANDLE Handle
,
151 SECURITY_INFORMATION SecurityInformation
,
152 PSECURITY_DESCRIPTOR SecurityDescriptor
)
156 Status
= NtSetSecurityObject(Handle
,
159 if (!NT_SUCCESS(Status
))
161 SetLastError(RtlNtStatusToDosError(Status
));
172 MapGenericMask(PDWORD AccessMask
,
173 PGENERIC_MAPPING GenericMapping
)
175 RtlMapGenericMask(AccessMask
,
184 ImpersonateLoggedOnUser(HANDLE hToken
)
186 SECURITY_QUALITY_OF_SERVICE Qos
;
187 OBJECT_ATTRIBUTES ObjectAttributes
;
194 /* Get the token type */
195 Status
= NtQueryInformationToken (hToken
,
200 if (!NT_SUCCESS(Status
))
202 SetLastError (RtlNtStatusToDosError (Status
));
206 if (Type
== TokenPrimary
)
208 /* Create a duplicate impersonation token */
209 Qos
.Length
= sizeof(SECURITY_QUALITY_OF_SERVICE
);
210 Qos
.ImpersonationLevel
= SecurityImpersonation
;
211 Qos
.ContextTrackingMode
= SECURITY_DYNAMIC_TRACKING
;
212 Qos
.EffectiveOnly
= FALSE
;
214 ObjectAttributes
.Length
= sizeof(OBJECT_ATTRIBUTES
);
215 ObjectAttributes
.RootDirectory
= NULL
;
216 ObjectAttributes
.ObjectName
= NULL
;
217 ObjectAttributes
.Attributes
= 0;
218 ObjectAttributes
.SecurityDescriptor
= NULL
;
219 ObjectAttributes
.SecurityQualityOfService
= &Qos
;
221 Status
= NtDuplicateToken (hToken
,
222 TOKEN_IMPERSONATE
| TOKEN_QUERY
,
227 if (!NT_SUCCESS(Status
))
229 SetLastError (RtlNtStatusToDosError (Status
));
237 /* User the original impersonation token */
242 /* Impersonate the the current thread */
243 Status
= NtSetInformationThread (NtCurrentThread (),
244 ThreadImpersonationToken
,
248 if (Duplicated
== TRUE
)
253 if (!NT_SUCCESS(Status
))
255 SetLastError (RtlNtStatusToDosError (Status
));
267 ImpersonateSelf(SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
)
271 Status
= RtlImpersonateSelf(ImpersonationLevel
);
272 if (!NT_SUCCESS(Status
))
274 SetLastError(RtlNtStatusToDosError(Status
));
290 Status
= NtSetInformationThread(NtCurrentThread(),
291 ThreadImpersonationToken
,
294 if (!NT_SUCCESS(Status
))
296 SetLastError(RtlNtStatusToDosError(Status
));
303 /******************************************************************************
304 * GetUserNameA [ADVAPI32.@]
306 * Get the current user name.
309 * lpszName [O] Destination for the user name.
310 * lpSize [I/O] Size of lpszName.
313 * Success: The length of the user name, including terminating NUL.
314 * Failure: ERROR_MORE_DATA if *lpSize is too small.
319 GetUserNameA( LPSTR lpszName
, LPDWORD lpSize
)
322 // char name[] = { "Administrator" };
324 /* We need to include the null character when determining the size of the buffer. */
325 // len = strlen(name) + 1;
326 // if (len > *lpSize)
328 // SetLastError(ERROR_MORE_DATA);
334 // strcpy(lpszName, name);
335 DPRINT1("GetUserNameA: stub\n");
339 /******************************************************************************
340 * GetUserNameW [ADVAPI32.@]
347 GetUserNameW( LPWSTR lpszName
, LPDWORD lpSize
)
349 // char name[] = { "Administrator" };
351 // DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
353 // if (len > *lpSize)
355 // SetLastError(ERROR_MORE_DATA);
361 // MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, len );
362 DPRINT1("GetUserNameW: stub\n");
367 /******************************************************************************
368 * LookupAccountSidA [ADVAPI32.@]
373 LookupAccountSidA (LPCSTR lpSystemName
,
377 LPSTR lpReferencedDomainName
,
378 LPDWORD cchReferencedDomainName
,
381 DPRINT1("LookupAccountSidA is unimplemented, but returns success\n");
386 /******************************************************************************
387 * LookupAccountSidW [ADVAPI32.@]
392 LookupAccountSidW (LPCWSTR lpSystemName
,
396 LPWSTR lpReferencedDomainName
,
397 LPDWORD cchReferencedDomainName
,
400 DPRINT1("LookupAccountSidW is unimplemented, but returns success\n");
405 /**********************************************************************
406 * LookupPrivilegeValueA EXPORTED
411 LookupPrivilegeValueA (LPCSTR lpSystemName
,
415 UNICODE_STRING SystemName
;
420 if (lpSystemName
!= NULL
)
422 RtlCreateUnicodeStringFromAsciiz (&SystemName
,
423 (LPSTR
)lpSystemName
);
426 /* Check the privilege name is not NULL */
429 SetLastError (ERROR_INVALID_PARAMETER
);
433 RtlCreateUnicodeStringFromAsciiz (&Name
,
436 Result
= LookupPrivilegeValueW ((lpSystemName
!= NULL
) ? SystemName
.Buffer
: NULL
,
440 RtlFreeUnicodeString (&Name
);
443 if (lpSystemName
!= NULL
)
445 RtlFreeUnicodeString (&SystemName
);
452 /**********************************************************************
453 * LookupPrivilegeValueW EXPORTED
458 LookupPrivilegeValueW (LPCWSTR SystemName
,
462 static const WCHAR
* const DefaultPrivNames
[] =
464 L
"SeCreateTokenPrivilege",
465 L
"SeAssignPrimaryTokenPrivilege",
466 L
"SeLockMemoryPrivilege",
467 L
"SeIncreaseQuotaPrivilege",
468 L
"SeUnsolicitedInputPrivilege",
469 L
"SeMachineAccountPrivilege",
471 L
"SeSecurityPrivilege",
472 L
"SeTakeOwnershipPrivilege",
473 L
"SeLoadDriverPrivilege",
474 L
"SeSystemProfilePrivilege",
475 L
"SeSystemtimePrivilege",
476 L
"SeProfileSingleProcessPrivilege",
477 L
"SeIncreaseBasePriorityPrivilege",
478 L
"SeCreatePagefilePrivilege",
479 L
"SeCreatePermanentPrivilege",
480 L
"SeBackupPrivilege",
481 L
"SeRestorePrivilege",
482 L
"SeShutdownPrivilege",
485 L
"SeSystemEnvironmentPrivilege",
486 L
"SeChangeNotifyPrivilege",
487 L
"SeRemoteShutdownPrivilege",
488 L
"SeUndockPrivilege",
489 L
"SeSyncAgentPrivilege",
490 L
"SeEnableDelegationPrivilege",
491 L
"SeManageVolumePrivilege",
492 L
"SeImpersonatePrivilege",
493 L
"SeCreateGlobalPrivilege"
497 if (NULL
!= SystemName
&& L
'\0' != *SystemName
)
499 DPRINT1("LookupPrivilegeValueW: not implemented for remote system\n");
500 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
504 for (Priv
= 0; Priv
< sizeof(DefaultPrivNames
) / sizeof(DefaultPrivNames
[0]); Priv
++)
506 if (0 == wcscmp(PrivName
, DefaultPrivNames
[Priv
]))
508 Luid
->LowPart
= Priv
+ 1;
514 DPRINT1("LookupPrivilegeValueW: no such privilege %S\n", PrivName
);
515 SetLastError(ERROR_NO_SUCH_PRIVILEGE
);
520 /**********************************************************************
521 * LookupPrivilegeDisplayNameA EXPORTED
526 LookupPrivilegeDisplayNameA (LPCSTR lpSystemName
,
529 LPDWORD cbDisplayName
,
530 LPDWORD lpLanguageId
)
532 DPRINT1("LookupPrivilegeDisplayNameA: stub\n");
533 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
538 /**********************************************************************
539 * LookupPrivilegeDisplayNameW EXPORTED
544 LookupPrivilegeDisplayNameW (LPCWSTR lpSystemName
,
546 LPWSTR lpDisplayName
,
547 LPDWORD cbDisplayName
,
548 LPDWORD lpLanguageId
)
550 DPRINT1("LookupPrivilegeDisplayNameW: stub\n");
551 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
556 /**********************************************************************
557 * LookupPrivilegeNameA EXPORTED
562 LookupPrivilegeNameA (LPCSTR lpSystemName
,
567 DPRINT1("LookupPrivilegeNameA: stub\n");
568 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
573 /**********************************************************************
574 * LookupPrivilegeNameW EXPORTED
579 LookupPrivilegeNameW (LPCWSTR lpSystemName
,
584 DPRINT1("LookupPrivilegeNameW: stub\n");
585 SetLastError (ERROR_CALL_NOT_IMPLEMENTED
);
590 /**********************************************************************
591 * GetNamedSecurityInfoW EXPORTED
596 GetNamedSecurityInfoW(LPWSTR pObjectName
,
597 SE_OBJECT_TYPE ObjectType
,
598 SECURITY_INFORMATION SecurityInfo
,
603 PSECURITY_DESCRIPTOR
*ppSecurityDescriptor
)
605 DPRINT1("GetNamedSecurityInfoW: stub\n");
606 return ERROR_CALL_NOT_IMPLEMENTED
;
610 /**********************************************************************
611 * GetNamedSecurityInfoA EXPORTED
616 GetNamedSecurityInfoA(LPSTR pObjectName
,
617 SE_OBJECT_TYPE ObjectType
,
618 SECURITY_INFORMATION SecurityInfo
,
623 PSECURITY_DESCRIPTOR
*ppSecurityDescriptor
)
625 DPRINT1("GetNamedSecurityInfoA: stub\n");
626 return ERROR_CALL_NOT_IMPLEMENTED
;
630 /**********************************************************************
631 * SetNamedSecurityInfoW EXPORTED
636 SetNamedSecurityInfoW(LPWSTR pObjectName
,
637 SE_OBJECT_TYPE ObjectType
,
638 SECURITY_INFORMATION SecurityInfo
,
644 DPRINT1("SetNamedSecurityInfoW: stub\n");
645 return ERROR_CALL_NOT_IMPLEMENTED
;
649 /**********************************************************************
650 * SetNamedSecurityInfoA EXPORTED
655 SetNamedSecurityInfoA(LPSTR pObjectName
,
656 SE_OBJECT_TYPE ObjectType
,
657 SECURITY_INFORMATION SecurityInfo
,
663 DPRINT1("SetNamedSecurityInfoA: stub\n");
664 return ERROR_CALL_NOT_IMPLEMENTED
;