-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set permissions to the service
- Loading branch information
Showing
9 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
project('dummy-service', 'c', | ||
version : '0.1', | ||
default_options : ['warning_level=3']) | ||
|
||
executable('GalaxyCommunication', | ||
'communication.c') | ||
|
||
executable('update-permissions', 'permissions.c') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[properties] | ||
needs_exe_wrapper = true | ||
|
||
[binaries] | ||
c = 'x86_64-w64-mingw32-gcc' | ||
cpp = 'x86_64-w64-mingw32-g++' | ||
ar = 'x86_64-w64-mingw32-ar' | ||
strip = 'x86_64-w64-mingw32-strip' | ||
pkg-config = 'x86_64-w64-mingw32-pkg-config' | ||
windres = 'x86_64-w64-mingw32-windres' | ||
|
||
exe_wrapper = 'wine64' | ||
|
||
[host_machine] | ||
system = 'windows' | ||
cpu_family = 'x86_64' | ||
cpu = 'x86_64' | ||
endian = 'little' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#include <windows.h> | ||
#include <stdio.h> | ||
#include <aclapi.h> | ||
|
||
// based on https://learn.microsoft.com/en-en/windows/win32/services/svccontrol-cpp | ||
|
||
SC_HANDLE schSCManager; | ||
SC_HANDLE schService; | ||
|
||
EXPLICIT_ACCESS ea; | ||
SECURITY_DESCRIPTOR sd; | ||
PSECURITY_DESCRIPTOR psd = NULL; | ||
PACL pacl = NULL; | ||
PACL pNewAcl = NULL; | ||
BOOL bDaclPresent = FALSE; | ||
BOOL bDaclDefaulted = FALSE; | ||
DWORD dwError = 0; | ||
DWORD dwSize = 0; | ||
DWORD dwBytesNeeded = 0; | ||
|
||
int main(int argc, char** argv) { | ||
|
||
schSCManager = OpenSCManager( | ||
NULL, // local computer | ||
NULL, // ServicesActive database | ||
SC_MANAGER_ALL_ACCESS); // full access rights | ||
|
||
if (NULL == schSCManager) | ||
{ | ||
printf("OpenSCManager failed (%ld)\n", GetLastError()); | ||
return 1; | ||
} | ||
|
||
// Get a handle to the service | ||
|
||
schService = OpenService( | ||
schSCManager, // SCManager database | ||
"GalaxyCommunication", // name of service | ||
READ_CONTROL | WRITE_DAC); // access | ||
|
||
if (schService == NULL) | ||
{ | ||
printf("OpenService failed (%ld)\n", GetLastError()); | ||
CloseServiceHandle(schSCManager); | ||
return 1; | ||
} | ||
|
||
// Get the current security descriptor. | ||
|
||
if (!QueryServiceObjectSecurity(schService, | ||
DACL_SECURITY_INFORMATION, | ||
&psd, // using NULL does not work on all versions | ||
0, | ||
&dwBytesNeeded)) | ||
{ | ||
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) | ||
{ | ||
dwSize = dwBytesNeeded; | ||
psd = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(), | ||
HEAP_ZERO_MEMORY, dwSize); | ||
if (psd == NULL) | ||
{ | ||
// Note: HeapAlloc does not support GetLastError. | ||
printf("HeapAlloc failed\n"); | ||
goto dacl_cleanup; | ||
} | ||
|
||
if (!QueryServiceObjectSecurity(schService, | ||
DACL_SECURITY_INFORMATION, psd, dwSize, &dwBytesNeeded)) | ||
{ | ||
printf("QueryServiceObjectSecurity failed (%ld)\n", GetLastError()); | ||
goto dacl_cleanup; | ||
} | ||
} | ||
else | ||
{ | ||
printf("QueryServiceObjectSecurity failed (%ld)\n", GetLastError()); | ||
goto dacl_cleanup; | ||
} | ||
} | ||
|
||
// Get the DACL. | ||
|
||
if (!GetSecurityDescriptorDacl(psd, &bDaclPresent, &pacl, | ||
&bDaclDefaulted)) | ||
{ | ||
printf("GetSecurityDescriptorDacl failed(%ld)\n", GetLastError()); | ||
goto dacl_cleanup; | ||
} | ||
|
||
// Build the ACE. | ||
|
||
BuildExplicitAccessWithName(&ea, TEXT("EVERYONE"), | ||
SERVICE_START | SERVICE_STOP | READ_CONTROL, | ||
SET_ACCESS, NO_INHERITANCE); | ||
|
||
dwError = SetEntriesInAcl(1, &ea, pacl, &pNewAcl); | ||
if (dwError != ERROR_SUCCESS) | ||
{ | ||
printf("SetEntriesInAcl failed(%ld)\n", dwError); | ||
goto dacl_cleanup; | ||
} | ||
|
||
// Initialize a new security descriptor. | ||
|
||
if (!InitializeSecurityDescriptor(&sd, | ||
SECURITY_DESCRIPTOR_REVISION)) | ||
{ | ||
printf("InitializeSecurityDescriptor failed(%ld)\n", GetLastError()); | ||
goto dacl_cleanup; | ||
} | ||
|
||
// Set the new DACL in the security descriptor. | ||
|
||
if (!SetSecurityDescriptorDacl(&sd, TRUE, pNewAcl, FALSE)) | ||
{ | ||
printf("SetSecurityDescriptorDacl failed(%ld)\n", GetLastError()); | ||
goto dacl_cleanup; | ||
} | ||
|
||
// Set the new DACL for the service object. | ||
|
||
if (!SetServiceObjectSecurity(schService, | ||
DACL_SECURITY_INFORMATION, &sd)) | ||
{ | ||
printf("SetServiceObjectSecurity failed(%ld)\n", GetLastError()); | ||
goto dacl_cleanup; | ||
} | ||
else printf("Service DACL updated successfully\n"); | ||
|
||
dacl_cleanup: | ||
CloseServiceHandle(schSCManager); | ||
CloseServiceHandle(schService); | ||
|
||
if(NULL != pNewAcl) | ||
LocalFree((HLOCAL)pNewAcl); | ||
if(NULL != psd) | ||
HeapFree(GetProcessHeap(), 0, (LPVOID)psd); | ||
|
||
|
||
return 0; | ||
} |
Binary file not shown.