#include #include #include #include #include #include "debug.h" #define TESTFILENAME "PermissionsTest.txt" int main( int argc, char **argv ) { char userName[ UNLEN+1 ]; char sid[ 400 ]; char *stringSID; char domainName[ 256 ]; SID_NAME_USE sidType; char pswd[] = "The quick brown fox"; char ssdTemplate[] = "O:%sD:P(A;;FA;;;%s)"; char ssd[1024]; SECURITY_DESCRIPTOR *psd = NULL; SECURITY_ATTRIBUTES sa = { sizeof( SECURITY_ATTRIBUTES ), NULL, 0 }; ULONG sdSize, unSize = sizeof( userName ), sidSize = sizeof( sid ), dnSize = sizeof( domainName ), written; HANDLE h; DIEIF( !GetUserName( userName, &unSize ), NULL ); DIEIF( !LookupAccountName( NULL, userName, sid, &sidSize, domainName, &dnSize, &sidType ), NULL ); printf( "Got sid\n" ); DIEIF( !ConvertSidToStringSid( sid, &stringSID ), NULL ); printf( "SID (as string): '%s'\n", stringSID ); sprintf_s( ssd, sizeof(ssd), ssdTemplate, stringSID, stringSID ); printf( "SSD: '%s'\n", ssd ); DIEIF( !ConvertStringSecurityDescriptorToSecurityDescriptor( ssd, SDDL_REVISION_1, &psd, &sdSize ), NULL ); printf( "psd:%x sdSize: %d\n", psd, sdSize ); sa.lpSecurityDescriptor = psd; DIEIF( ( h = CreateFile( TESTFILENAME, GENERIC_WRITE, FILE_SHARE_READ, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL )) == INVALID_HANDLE_VALUE, NULL ); printf("File created\n" ); DIEIF( !WriteFile( h, pswd, sizeof( pswd ), &written, NULL ), NULL ); printf( "File written '%s'\n", pswd ); while( !_kbhit() ) Sleep( 1 ); printf( "Closing file\n" ); DIEIF( !CloseHandle( h ), NULL ); DIEIF( !DeleteFile( TESTFILENAME ), NULL ); return 0; }