Zwraca wartość różną od zera w przypadku sukcesu. W przeciwnym wypadku funkcja zwraca wartość 0.
Funkcja konwertuje identyfikator bezpieczeństwa (SID) z postaci tekstowej do prawidłowego funkcjonalnie SID-a.
Windows 2000 wspiera tylko i wyłącznie standardowy zapis identyfikatora SID, tj. identyfikator bezpieczeństwa musi być zapisany w postaci S-R-I-S-S.
#include <windows.h>
#include <cstdio>
#include <string>
#include <tchar.h>
#include <aclapi.h>
#include <sddl.h>
DWORD AddAceToObjectsSecurityDescriptor(
LPTSTR pszObjName,
SE_OBJECT_TYPE ObjectType,
LPTSTR pszTrustee,
TRUSTEE_FORM TrusteeForm,
DWORD dwAccessRights,
ACCESS_MODE AccessMode,
DWORD dwInheritance
)
{
DWORD dwRes = 0;
PACL pOldDACL = NULL, pNewDACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS ea;
if( NULL == pszObjName )
return ERROR_INVALID_PARAMETER;
dwRes = GetNamedSecurityInfo( pszObjName, ObjectType,
DACL_SECURITY_INFORMATION,
NULL, NULL, & pOldDACL, NULL, & pSD );
if( ERROR_SUCCESS != dwRes ) {
printf( "GetNamedSecurityInfo Error %u\n", dwRes );
goto Cleanup;
}
ZeroMemory( & ea, sizeof( EXPLICIT_ACCESS ) );
ea.grfAccessPermissions = dwAccessRights;
ea.grfAccessMode = AccessMode;
ea.grfInheritance = dwInheritance;
ea.Trustee.TrusteeForm = TrusteeForm;
ea.Trustee.ptstrName = pszTrustee;
dwRes = SetEntriesInAcl( 1, & ea, pOldDACL, & pNewDACL );
if( ERROR_SUCCESS != dwRes ) {
printf( "SetEntriesInAcl Error %u\n", dwRes );
goto Cleanup;
}
dwRes = SetNamedSecurityInfo( pszObjName, ObjectType,
DACL_SECURITY_INFORMATION,
NULL, NULL, pNewDACL, NULL );
if( ERROR_SUCCESS != dwRes ) {
printf( "SetNamedSecurityInfo Error %u\n", dwRes );
goto Cleanup;
}
Cleanup:
if( pSD != NULL )
LocalFree(( HLOCAL ) pSD );
if( pNewDACL != NULL )
LocalFree(( HLOCAL ) pNewDACL );
return dwRes;
}
BOOL SetPrivilege(
HANDLE hToken,
LPCTSTR lpszPrivilege,
BOOL bEnablePrivilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if( !LookupPrivilegeValue(
NULL,
lpszPrivilege,
& luid ) )
{
printf( "LookupPrivilegeValue error: %u\n", GetLastError() );
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[ 0 ].Luid = luid;
if( bEnablePrivilege )
tp.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[ 0 ].Attributes = 0;
if( !AdjustTokenPrivileges(
hToken,
FALSE,
& tp,
sizeof( TOKEN_PRIVILEGES ),
( PTOKEN_PRIVILEGES ) NULL,
( PDWORD ) NULL ) )
{
printf( "AdjustTokenPrivileges error: %u\n", GetLastError() );
return FALSE;
}
if( GetLastError() == ERROR_NOT_ALL_ASSIGNED )
{
printf( "The token does not have the specified privilege. \n" );
return FALSE;
}
return TRUE;
}
int main()
{
DWORD dwError;
dwError = AddAceToObjectsSecurityDescriptor( _T( "D:\\bumtralala" ),
SE_FILE_OBJECT,
_T( "Wszyscy" ),
TRUSTEE_IS_NAME,
GENERIC_ALL,
SET_ACCESS,
SUB_CONTAINERS_AND_OBJECTS_INHERIT
);
PSID psid;
::ConvertStringSidToSid( _T( "S-1-1-0" ), & psid );
dwError = AddAceToObjectsSecurityDescriptor( _T( "MACHINE\\Software\\ablabla" ),
SE_REGISTRY_KEY,
( LPTSTR ) psid,
TRUSTEE_IS_SID,
STANDARD_RIGHTS_ALL,
GRANT_ACCESS,
CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE
);
::LocalFree( psid );
return 0;
}