-
Notifications
You must be signed in to change notification settings - Fork 808
Expand file tree
/
Copy pathSecureSettings.cs
More file actions
157 lines (133 loc) · 4.66 KB
/
SecureSettings.cs
File metadata and controls
157 lines (133 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Collections.Concurrent;
using System.Diagnostics;
using UniGetUI.Core.Data;
using UniGetUI.Core.Tools;
namespace UniGetUI.Core.SettingsEngine.SecureSettings;
public static class SecureSettings
{
public static string? TEST_SecureSettingsRootOverride { private get; set; }
// Various predefined secure settings keys
public enum K
{
AllowCLIArguments,
AllowImportingCLIArguments,
AllowPrePostOpCommand,
AllowImportPrePostOpCommands,
ForceUserGSudo,
AllowCustomManagerPaths,
Unset,
};
public static string ResolveKey(K key)
{
return key switch
{
K.AllowCLIArguments => "AllowCLIArguments",
K.AllowImportingCLIArguments => "AllowImportingCLIArguments",
K.AllowPrePostOpCommand => "AllowPrePostInstallCommands",
K.AllowImportPrePostOpCommands => "AllowImportingPrePostInstallCommands",
K.ForceUserGSudo => "ForceUserGSudo",
K.AllowCustomManagerPaths => "AllowCustomManagerPaths",
K.Unset => throw new InvalidDataException("SecureSettings key was unset!"),
_ => throw new KeyNotFoundException(
$"The SecureSettings key {key} was not found on the ResolveKey map"
),
};
}
private static readonly ConcurrentDictionary<string, bool> _cache = new();
public static class Args
{
public const string ENABLE_FOR_USER = "--enable-secure-setting-for-user";
public const string DISABLE_FOR_USER = "--disable-secure-setting-for-user";
}
public static bool Get(K key)
{
string purifiedSetting = CoreTools.MakeValidFileName(ResolveKey(key));
return _cache.GetOrAdd(purifiedSetting, ResolveSettingValue);
}
public static async Task<bool> TrySet(K key, bool enabled)
{
string purifiedSetting = CoreTools.MakeValidFileName(ResolveKey(key));
_cache.TryRemove(purifiedSetting, out _);
string purifiedUser = CoreTools.MakeValidFileName(Environment.UserName);
if (!OperatingSystem.IsWindows())
{
return ApplyForUser(purifiedUser, purifiedSetting, enabled) is 0;
}
using Process p = new Process();
p.StartInfo = new()
{
UseShellExecute = true,
CreateNoWindow = true,
FileName = CoreData.UniGetUIExecutableFile,
Verb = "runas",
ArgumentList =
{
enabled ? Args.ENABLE_FOR_USER : Args.DISABLE_FOR_USER,
purifiedUser,
purifiedSetting,
},
};
p.Start();
await p.WaitForExitAsync();
return p.ExitCode is 0;
}
public static int ApplyForUser(string username, string setting, bool enable)
{
try
{
string purifiedSetting = CoreTools.MakeValidFileName(setting);
_cache.TryRemove(purifiedSetting, out _);
string purifiedUser = CoreTools.MakeValidFileName(username);
var settingsLocation = Path.Join(GetSecureSettingsRoot(), purifiedUser);
var settingFile = Path.Join(settingsLocation, purifiedSetting);
if (!Directory.Exists(settingsLocation))
{
Directory.CreateDirectory(settingsLocation);
}
if (enable)
{
File.WriteAllText(settingFile, "");
}
else
{
if (File.Exists(settingFile))
{
File.Delete(settingFile);
}
}
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return -1;
}
}
private static string GetSecureSettingsRoot()
{
if (TEST_SecureSettingsRootOverride is not null)
{
return TEST_SecureSettingsRootOverride;
}
if (OperatingSystem.IsWindows())
{
return Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
"UniGetUI",
"SecureSettings"
);
}
return Path.Join(CoreData.UniGetUIDataDirectory, "SecureSettings");
}
private static bool ResolveSettingValue(string purifiedSetting)
{
string purifiedUser = CoreTools.MakeValidFileName(Environment.UserName);
var settingsLocation = Path.Join(GetSecureSettingsRoot(), purifiedUser);
if (!Directory.Exists(settingsLocation))
{
return false;
}
var settingFile = Path.Join(settingsLocation, purifiedSetting);
return File.Exists(settingFile);
}
}