-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
ArgumentsHelper.cs
352 lines (306 loc) · 14.6 KB
/
ArgumentsHelper.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using gsudo.Commands;
using gsudo.Native;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace gsudo.Helpers
{
public static class ArgumentsHelper
{
static readonly HashSet<string> CmdCommands = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "ASSOC", "ATTRIB", "BREAK", "BCDEDIT", "CACLS", "CALL", "CD", "CHCP", "CHDIR", "CHKDSK", "CHKNTFS", "CLS", /*"CMD",*/ "COLOR", "COMP", "COMPACT", "CONVERT", "COPY", "DATE", "DEL", "DIR", "DISKPART", "DOSKEY", "DRIVERQUERY", "ECHO", "ENDLOCAL", "ERASE", "EXIT", "FC", "FIND", "FINDSTR", "FOR", "FORMAT", "FSUTIL", "FTYPE", "GOTO", "GPRESULT", "GRAFTABL", "HELP", "ICACLS", "IF", "LABEL", "MD", "MKDIR", "MKLINK", "MODE", "MORE", "MOVE", "OPENFILES", "PATH", "PAUSE", "POPD", "PRINT", "PROMPT", "PUSHD", "RD", "RECOVER", "REM", "REN", "RENAME", "REPLACE", "RMDIR", "ROBOCOPY", "SET", "SETLOCAL", "SC", "SCHTASKS", "SHIFT", "SHUTDOWN", "SORT", "START", "SUBST", "SYSTEMINFO", "TASKLIST", "TASKKILL", "TIME", "TITLE", "TREE", "TYPE", "VER", "VERIFY", "VOL", "XCOPY", "WMIC" };
static readonly HashSet<string> CreateProcessSupportedExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".CMD", ".EXE", ".BAT", ".COM" };
internal static string[] AugmentCommand(string[] args)
{
string currentShellExeName;
Shell currentShell = ShellHelper.DetectInvokingShell(out currentShellExeName);
if (currentShell.In(Shell.PowerShell, Shell.PowerShellCore, Shell.PowerShellCore623BuggedGlobalInstall))
{
// PowerShell Core 6.0.0 to 6.2.3 does not supports command line arguments.
// See:
// https://github.com/PowerShell/PowerShell/pull/10461#event-2959890147
// https://github.com/gerardog/gsudo/issues/10
/*
Running ./gsudo from powershell should elevate the current shell, which means:
=> On PowerShell, run => powershell -NoLogo
=> On PowerShellCore => pwsh -NoLogo
=> On PowerShellCore623BuggedGlobalInstall => pwsh
Running ./gsudo {command} should elevate the powershell command.
=> On PowerShell => powershell -NoLogo -NoProfile -Command {command}
=> On PowerShellCore => pwsh -NoLogo -NoProfile -Command {command}
=> On PowerShellCore623BuggedGlobalInstall => pwsh {command}
*/
var newArgs = new List<string>();
newArgs.Add($"\"{currentShellExeName}\"");
if (currentShell == Shell.PowerShellCore623BuggedGlobalInstall)
{
Logger.Instance.Log("Please update to PowerShell Core >= 6.2.4 to avoid profile loading.", LogLevel.Warning);
}
else
{
newArgs.Add("-NoLogo");
if (args.Length > 0)
{
newArgs.Add("-NoProfile");
newArgs.Add("-Command");
}
}
if (args.Length > 0)
newArgs.AddMany(args);
return newArgs.ToArray();
}
if (currentShell == Shell.Yori)
{
if (args.Length == 0)
return new[] {currentShellExeName};
else
return new[] {currentShellExeName, "-c"}
.Concat(args).ToArray();
}
// Not Powershell, or Powershell Core, assume CMD.
if (args.Length == 0)
{
return new string[]
{ Environment.GetEnvironmentVariable("COMSPEC"), "/k" };
}
else
{
if (CmdCommands.Contains(args[0]))
return new string[]
{ Environment.GetEnvironmentVariable("COMSPEC"), "/c" }
.Concat(args).ToArray();
var exename = ProcessFactory.FindExecutableInPath(UnQuote(args[0]));
if (exename == null || !CreateProcessSupportedExtensions.Contains(Path.GetExtension(exename)))
{
// We don't know what command are we executing. It may be an invalid program...
// Or a non-executable file with a valid file association..
// Let CMD decide that... Invoke using "CMD /C" prefix ..
return new string[]
{ Environment.GetEnvironmentVariable("COMSPEC"), "/c" }
.Concat(args).ToArray();
}
else
{
args[0] = $"\"{exename}\""; // replace command name with full+absolute+quoted path to resolve many issues, like "Batch files not started by create process if no extension is specified."
return args;
}
}
}
public static IEnumerable<string> SplitArgs(string args)
{
args = args.Trim();
var results = new List<string>();
int pushed = 0;
int curr = 0;
bool insideQuotes = false;
while (curr < args.Length)
{
if (args[curr] == '"')
insideQuotes = !insideQuotes;
else if (args[curr] == ' ' && !insideQuotes)
{
if ((curr - pushed) > 0)
results.Add(args.Substring(pushed, curr - pushed));
pushed = curr + 1;
}
curr++;
}
if (pushed < curr)
results.Add(args.Substring(pushed, curr - pushed));
return results;
}
internal static ICommand ParseCommand(IEnumerable<string> argsEnumerable)
{
// Parse Options
var args = new LinkedList<string>(argsEnumerable);
Func<string> dequeue = () =>
{
if (args.Count == 0) throw new ApplicationException("Missing argument");
var ret = args.First.Value;
args.RemoveFirst();
return ret;
};
string arg;
while (args.Count>0)
{
arg = dequeue();
if (
SetTrueIf(arg, () => InputArguments.NewWindow = true, "-n", "--new") ||
SetTrueIf(arg, () => InputArguments.Wait = true, "-w", "--wait") ||
// Legacy, now undocumented features.
SetTrueIf(arg, () => Settings.ForcePipedConsole.Value = true, "--piped", "--raw" /*--raw for backward compat*/) ||
SetTrueIf(arg, () => Settings.ForceAttachedConsole.Value = true, "--attached") ||
SetTrueIf(arg, () => Settings.ForceVTConsole.Value = true, "--vt") ||
SetTrueIf(arg, () => Settings.CopyEnvironmentVariables.Value = true, "--copyEV") ||
SetTrueIf(arg, () => Settings.CopyNetworkShares.Value = true, "--copyNS") ||
SetTrueIf(arg, () => InputArguments.RunAsSystem = true, "-s", "--system") ||
SetTrueIf(arg, () => InputArguments.Global = true, "--global") ||
SetTrueIf(arg, () => InputArguments.KillCache = true, "-k", "--reset-timestamp")
)
{ }
else if (arg.In("-v", "--version"))
return new ShowVersionHelpCommand();
else if (arg.In("-h", "--help", "help", "/?", "/h"))
return new HelpCommand();
else if (arg.In("--debug"))
{
InputArguments.Debug = true;
Settings.LogLevel.Value = LogLevel.All;
}
else if (arg.In("--loglevel"))
{
arg = dequeue();
if (!Enum.TryParse<LogLevel>(arg, true, out var val))
throw new ApplicationException($"\"{arg}\" is not a valid LogLevel. Valid values are: All, Debug, Info, Warning, Error, None");
Settings.LogLevel.Value = val;
}
else if (arg.In("-i", "--integrity"))
{
arg = dequeue();
if (!Enum.TryParse<IntegrityLevel>(arg, true, out var val))
throw new ApplicationException($"\"{arg}\" is not a valid IntegrityLevel. Valid values are: \n" +
$"Untrusted, Low, Medium, MediuPlus, High, System, Protected (unsupported), Secure (unsupported)");
InputArguments.IntegrityLevel = val;
}
else if (arg.StartsWith("-", StringComparison.Ordinal))
{
throw new ApplicationException($"Invalid option: {arg}");
}
else
{
// arg is not an option, requeue and parse as a command.
args.AddFirst(arg);
break;
}
}
if (args.Count == 0)
{
if (InputArguments.KillCache)
{
// support for "-k" as command
// return a verbose command, instead of a silent argument.
// this is overly complicated because it supports a sudo-like experience like 'sudo -k' (kill as a verb/command)
// or 'sudo -k command' (kill as an argument)
InputArguments.KillCache = false;
return new KillCacheCommand(verbose: true);
}
return new RunCommand() { CommandToRun = Array.Empty<string>() };
}
// Parse Command
arg = dequeue();
if (arg.Equals("help", StringComparison.OrdinalIgnoreCase))
return new HelpCommand();
if (arg.In("gsudoservice"))
{
return new ServiceCommand()
{
AllowedPid = int.Parse(dequeue(), CultureInfo.InvariantCulture),
AllowedSid = dequeue(),
LogLvl = ExtensionMethods.ParseEnum<LogLevel>(dequeue()),
CacheDuration = Settings.TimeSpanParseWithInfinite(dequeue())
};
}
/*
if (arg.In("gsudoservicehop"))
{
return new ServiceHopCommand()
{
allowedPid = int.Parse(dequeue(), CultureInfo.InvariantCulture),
allowedSid = dequeue(),
LogLvl = ExtensionMethods.ParseEnum<LogLevel>(dequeue()),
};
}
*/
if (arg.In("gsudoctrlc"))
return new CtrlCCommand()
{
Pid = int.Parse(dequeue(), CultureInfo.InvariantCulture),
sendSigBreak = bool.Parse(dequeue())
};
if (arg.In("config"))
return new ConfigCommand() { key = args.FirstOrDefault(), value = args.Skip(1) };
if (arg.In("status"))
return new StatusCommand();
/* Obsolete since TokenSwitch Mode
if (arg.In("AttachRun"))
return new AttachRun() { CommandToRun = args };
*/
if (arg.In("Cache"))
{
var cmd = new CacheCommand();
while (args.Count > 0)
{
arg = dequeue();
if (arg.In("ON"))
cmd.Action = CacheCommandAction.On;
else if (arg.In("OFF"))
cmd.Action = CacheCommandAction.Off;
else if (arg.In("-h", "/h", "--help", "help"))
cmd.Action = CacheCommandAction.Help;
else if (arg.In("-p", "--pid"))
cmd.AllowedPid = int.Parse(dequeue(), CultureInfo.InvariantCulture);
else if (arg.In("-d", "--duration"))
cmd.CacheDuration = Settings.TimeSpanParseWithInfinite(dequeue());
else if (arg.In("-k"))
InputArguments.KillCache = true;
else
throw new ApplicationException($"Unknown argument: {arg}");
}
return cmd;
}
if (arg.In("gsudoelevate"))
{
var cmd = new ElevateCommand();
while (args.Count > 0)
{
arg = dequeue();
if (arg.In("--pid"))
cmd.ProcessId = int.Parse(dequeue(), CultureInfo.InvariantCulture);
}
return cmd;
}
if (arg.In("run"))
return new RunCommand() { CommandToRun = args };
args.AddFirst(arg);
if (arg == "!!" || arg.StartsWith("!", StringComparison.InvariantCulture))
return new BangBangCommand() { Pattern = string.Join(" ", args) };
return new RunCommand() { CommandToRun = args };
}
private static bool SetTrueIf(string arg, Action setting, params string[] v)
{
if (arg.In(v))
{
setting();
return true;
}
return false;
}
internal static string GetRealCommandLine()
{
System.IntPtr ptr = ConsoleApi.GetCommandLine();
string commandLine = Marshal.PtrToStringAuto(ptr).TrimStart();
Logger.Instance.Log($"Command Line: {commandLine}", LogLevel.Debug);
if (commandLine[0] == '"')
return commandLine.Substring(commandLine.IndexOf('"', 1) + 1).TrimStart(' ');
else if (commandLine.IndexOf(' ', 1) >= 0)
return commandLine.Substring(commandLine.IndexOf(' ', 1) + 1).TrimStart(' ');
else
return string.Empty;
}
public static string UnQuote(string v)
{
if (string.IsNullOrEmpty(v))
return v;
if (v[0] == '"' && v[v.Length - 1] == '"')
return v.Substring(1, v.Length - 2);
if (v[0] == '"' && v.Trim().EndsWith("\"", StringComparison.Ordinal))
return UnQuote(v.Trim());
if (v[0] == '"')
return v.Substring(1);
else
return v;
}
}
}