Skip to content

Commit

Permalink
Rearrange solution directory structure
Browse files Browse the repository at this point in the history
Create seperate premission elevator for improved logging when trying to take ownership and permission
  • Loading branch information
Leapward-Koex committed Nov 7, 2024
1 parent fc4f927 commit c973452
Show file tree
Hide file tree
Showing 57 changed files with 173 additions and 45 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 6 additions & 1 deletion LockscreenGif.csproj → LockScreenGif/LockscreenGif.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.3.230502000" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="7.0.0" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="8.0.1" />
<PackageReference Include="WindowsDisplayAPI" Version="1.3.0.13" />
<PackageReference Include="WinUIEx" Version="2.2" />
</ItemGroup>
Expand All @@ -43,6 +43,11 @@
<ItemGroup>
<Folder Include="Properties\PublishProfiles\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Logger\Logger.csproj" />
<ProjectReference Include="..\PermissionElevator\PermissionElevator.csproj" />
</ItemGroup>

<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public async Task<bool> ApplyGifAsLockscreenAsync()
Logger.Info($"User SID: {sid}. CurrentImage: {CurrentImage?.Path}");
if (CurrentImage != null && sid != null)
{
Logger.Info($"Calling default Windows API to set lockscreen image");
await LockScreen.SetImageFileAsync(CurrentImage);
//Logger.Info($"Calling default Windows API to set lockscreen image");
//await LockScreen.SetImageFileAsync(CurrentImage);
var lockscreenDirectory = $@"C:\ProgramData\Microsoft\Windows\SystemData\{sid}\ReadOnly";
Logger.Info($"Trying to take ownership of {lockscreenDirectory}");
await TakeOwnershipOfLockscreenFolderAsync(lockscreenDirectory);
Expand Down Expand Up @@ -157,32 +157,47 @@ private DeleteFilesResult DeleteDimmedFiles(string lockscreenDirectory)

private async Task TakeOwnershipOfLockscreenFolderAsync(string lockscreenDirectory)
{
var startInfo = new ProcessStartInfo
Logger.Info("Checking permissions pre ownership");
try
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "takeown",
Arguments = $"/f \"{lockscreenDirectory}\" /r /d y",
Verb = "runas",
UseShellExecute = true
};
var process = Process.Start(startInfo);
if (process != null)
var icalsListPermissionStartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "icacls",
Arguments = $"* /t",
WorkingDirectory = lockscreenDirectory,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
};

using var icalsListPermissionProcess = Process.Start(icalsListPermissionStartInfo);
if (icalsListPermissionProcess != null)
{
// Read output and error in separate tasks to avoid deadlocks
var outputTask = ReadStreamAsync(icalsListPermissionProcess.StandardOutput, Logger.Info);
var errorTask = ReadStreamAsync(icalsListPermissionProcess.StandardError, Logger.Error);

await Task.WhenAll(outputTask, errorTask);
await icalsListPermissionProcess.WaitForExitAsync();
}
}
catch (Exception ex)
{
await process.WaitForExitAsync();
Logger.Error("Failed to check permissions", ex);
}

var icalsStartInfo = new ProcessStartInfo
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "icacls",
Arguments = $"\"{lockscreenDirectory}\" /grant *S-1-1-0:(F) /T /C",
FileName = "PermissionElevator",
Verb = "runas",
UseShellExecute = true
};
var icaslProcess = Process.Start(icalsStartInfo);
if (icaslProcess != null)
var process = Process.Start(startInfo);
if (process != null)
{
await icaslProcess.WaitForExitAsync();
await process.WaitForExitAsync();
}

Logger.Info("Checking permissions post ownership");
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 31 additions & 5 deletions LockscreenGif.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,44 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LockscreenGif", "LockscreenGif.csproj", "{D980ADB0-8A26-463F-9FCA-65326FD5DDF4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Logger", "Logger\Logger.csproj", "{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PermissionElevator", "PermissionElevator\PermissionElevator.csproj", "{11E2B137-30FC-49F2-9596-0AF6743B0C29}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LockscreenGif", "LockScreenGif\LockscreenGif.csproj", "{2B02598D-AE15-4590-A2C6-47487D4FF769}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D980ADB0-8A26-463F-9FCA-65326FD5DDF4}.Debug|x64.ActiveCfg = Debug|x64
{D980ADB0-8A26-463F-9FCA-65326FD5DDF4}.Debug|x64.Build.0 = Debug|x64
{D980ADB0-8A26-463F-9FCA-65326FD5DDF4}.Release|x64.ActiveCfg = Release|x64
{D980ADB0-8A26-463F-9FCA-65326FD5DDF4}.Release|x64.Build.0 = Release|x64
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Debug|x64.ActiveCfg = Debug|Any CPU
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Debug|x64.Build.0 = Debug|Any CPU
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Release|Any CPU.Build.0 = Release|Any CPU
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Release|x64.ActiveCfg = Release|Any CPU
{E6F2A3CA-BE5E-44A6-83BF-F5D16FED461B}.Release|x64.Build.0 = Release|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Debug|x64.ActiveCfg = Debug|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Debug|x64.Build.0 = Debug|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Release|Any CPU.Build.0 = Release|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Release|x64.ActiveCfg = Release|Any CPU
{11E2B137-30FC-49F2-9596-0AF6743B0C29}.Release|x64.Build.0 = Release|Any CPU
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Debug|Any CPU.ActiveCfg = Debug|x64
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Debug|Any CPU.Build.0 = Debug|x64
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Debug|x64.ActiveCfg = Debug|x64
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Debug|x64.Build.0 = Debug|x64
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Release|Any CPU.ActiveCfg = Release|x64
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Release|Any CPU.Build.0 = Release|x64
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Release|x64.ActiveCfg = Release|x64
{2B02598D-AE15-4590-A2C6-47487D4FF769}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 10 additions & 4 deletions Services/Logger.cs → Logger/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,25 @@ public static void CleanupOldLogFiles()
}


public static void Info(string message)
public static void Info(string? message)
{
Log("INFO", message);
if (message != null)
{
Log("INFO", message);
}
}

public static void Warn(string message)
{
Log("WARN", message);
}

public static void Error(string message)
public static void Error(string? message)
{
Error(message, null);
if (message != null)
{
Error(message, null);
}
}

public static void Error(string message, Exception? ex = null)
Expand Down
9 changes: 9 additions & 0 deletions Logger/Logger.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
17 changes: 0 additions & 17 deletions Package.appinstaller

This file was deleted.

19 changes: 19 additions & 0 deletions PermissionElevator/PermissionElevator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows10.0.17763.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Logger\Logger.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions PermissionElevator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;

var lockscreenDirectory = $@"C:\ProgramData\Microsoft\Windows\SystemData\{UserPrincipal.Current.Sid}\ReadOnly";

var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false,
FileName = "takeown",
Arguments = $"/f \"{lockscreenDirectory}\" /r /d y",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
Logger.Info($"Starting takeown of {lockscreenDirectory}");
var process = new Process();
process.OutputDataReceived += (sender, args) => Logger.Info(args.Data);
process.ErrorDataReceived += (sender, args) => Logger.Error(args.Data);
process.StartInfo = startInfo;
process.Start();
Logger.Info("Waiting for takeown to finish");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync();

var icaclsStartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = false,
FileName = "icacls",
Arguments = $"\"{lockscreenDirectory}\" /grant *S-1-1-0:(F) /T /C",
RedirectStandardOutput = true,
RedirectStandardError = true,
};
Logger.Info($"Starting icals of {lockscreenDirectory}");
var icaclsProcess = new Process();
icaclsProcess.OutputDataReceived += (sender, args) => Logger.Info(args.Data);
icaclsProcess.ErrorDataReceived += (sender, args) => Logger.Error(args.Data);
icaclsProcess.StartInfo = icaclsStartInfo;
icaclsProcess.Start();
Logger.Info("Waiting for icals to finish");
icaclsProcess.BeginOutputReadLine();
icaclsProcess.BeginErrorReadLine();
await icaclsProcess.WaitForExitAsync();

Logger.Info("Finished");
return 0;
17 changes: 17 additions & 0 deletions PermissionElevator/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
</application>
</compatibility>

</assembly>

0 comments on commit c973452

Please sign in to comment.