Skip to content

Commit

Permalink
Fixing issue with BinaryFormatter.
Browse files Browse the repository at this point in the history
Updated README.md
  • Loading branch information
Gerardo Grignoli committed Dec 18, 2019
1 parent 97bbc3c commit 3d1daff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ Read or write a user setting

- Why `gsudo` instead of just `sudo`?

When I created `gsudo`, there were other `sudo` packages on most Windows popular package managers such as `Chocolatey` and `Scoop`, all of them doing the elevation on a new console. In my opinion, that context switch is improductive, and also makes such tools less usefull for scripting. I could name the app `sudo` and the package as `gsudo`, but I fear people will not remember the package name for further installations. I will add the option to bind `sudo` command to the `gsudo` app in future versions of the installer.
When I created `gsudo`, there were other `sudo` packages on most Windows popular package managers such as `Chocolatey` and `Scoop`. I could name the app `sudo` and the package as `gsudo`, but I fear people will not remember the package name for further installations. I will add the option to bind `sudo` command to the `gsudo` app in future versions of the installer.
6 changes: 4 additions & 2 deletions backlog.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# gsudo Backlog

- Check for grandparents on Authorization()
- raw mode shouls merge StdErr and StdOut in one file, to fix random ordering of stderr/stdout.
- Chocolatey package / Scoop package / Release on github.
- gsudo --nocache (service will quit immediately after process ends and will not elevate other commands with cached credentials) (find better --syntax)
- Allow to specify other username. (RunAsUser verb)
- console resize on vt mode.

## Other not so likely ideas

- Third consecutive Ctrl-C should ask if the child process must be kept running or killed.
- Remote sudo. Run process on another machine / as in PSExec. (security?)
- Make gsudo chocolatey package link sudo to gsudo (conflict with chocolatey sudo package)
- Spend 500 USD in a code-signing certificate so I can sign the builds. I need to setup an https web site for gsudo or myself first as a prerequisit to get the certificate.
- gsudo Chocolatey Package to genereate a self-signed cert, install, and sign the exe on install, then delete the cert. (better uac prompt without $$ buying a certificate)
- Low level console access (https://docs.microsoft.com/en-us/windows/console/console-functions)

## Completed

- Third consecutive Ctrl-C or client disconnect kills the elevated process.
- VT console extended keys (F1-F12, CTRL+?, HOME,PAGEUP)
- WinPty/VT100 support: When in VT mode, processes are spawn using a PseudoConsole. Rendering could be done using Windows Console ENABLE_VIRTUAL_TERMINAL processing flag but it is pretty [unstable](https://github.com/microsoft/terminal/issues/3765). So it is disabled by default unless you are running inside ConEmu/Cmder which are VT100 ready terminals.
VT Mode is enabled automatically if you run inside a ConEmu/Cmder or if you use `--vt` flag.
Expand Down
37 changes: 26 additions & 11 deletions src/gsudo/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ public async Task<int> Execute()

if (elevationRequest.Mode == ElevationRequest.ConsoleMode.VT)
{
elevationRequest.ConsoleWidth = Console.WindowWidth;
elevationRequest.ConsoleWidth = Console.WindowWidth;
elevationRequest.ConsoleHeight = Console.WindowHeight;

if (TerminalHelper.IsConEmu())
elevationRequest.ConsoleWidth--; // weird ConEmu/Cmder fix

Environment.SetEnvironmentVariable("PROMPT", GlobalSettings.VTPrompt.Value);
}
else
{
Environment.SetEnvironmentVariable("PROMPT", GlobalSettings.Prompt.Value);
}

if (ProcessExtensions.IsAdministrator() && !GlobalSettings.NewWindow)
Expand All @@ -66,6 +60,15 @@ public async Task<int> Execute()

// No need to escalate. Run in-process

if (elevationRequest.Mode == ElevationRequest.ConsoleMode.Raw)
{
Environment.SetEnvironmentVariable("PROMPT", GlobalSettings.Prompt.Value);
}
else
{
Environment.SetEnvironmentVariable("PROMPT", GlobalSettings.VTPrompt.Value);
}

if (GlobalSettings.NewWindow)
{
using (Process process = ProcessFactory.StartDetached(exeName, GetArguments(), Environment.CurrentDirectory, false))
Expand Down Expand Up @@ -137,10 +140,7 @@ public async Task<int> Execute()
Logger.Instance.Log("Unable to connect to the elevated service.", LogLevel.Error);
return Constants.GSUDO_ERROR_EXITCODE;
}

new BinaryFormatter()
// { TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways, Binder = new MySerializationBinder() }
.Serialize(connection.ControlStream, elevationRequest);
WriteElevationRequest(elevationRequest, connection);

await connection.ControlStream.FlushAsync().ConfigureAwait(false);
ConnectionKeepAliveThread.Start(connection);
Expand All @@ -157,6 +157,21 @@ public async Task<int> Execute()

}

private async Task WriteElevationRequest(ElevationRequest elevationRequest, Connection connection)
{
var ms = new System.IO.MemoryStream();
new BinaryFormatter()
{ TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways, Binder = new MySerializationBinder() }
.Serialize(ms, elevationRequest);
ms.Seek(0, System.IO.SeekOrigin.Begin);

byte[] lengthArray = BitConverter.GetBytes(ms.Length);
Logger.Instance.Log($"ElevationRequest length {ms.Length}", LogLevel.Debug);

await connection.ControlStream.WriteAsync(lengthArray, 0, sizeof(int)).ConfigureAwait(false);
await connection.ControlStream.WriteAsync(ms.ToArray(), 0, (int)ms.Length).ConfigureAwait(false);
}

/// <summary>
/// Decide wheter we will use raw piped I/O screen communication,
/// or enhanced, colorfull VT mode with nice TAB auto-complete.
Expand Down
13 changes: 12 additions & 1 deletion src/gsudo/Commands/ServiceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,20 @@ private IRpcServer CreateServer()

private static async Task<ElevationRequest> ReadElevationRequest(Stream dataPipe)
{
byte[] dataSize = new byte[sizeof(int)];
dataPipe.Read(dataSize, 0, sizeof(int));
int dataSizeInt = BitConverter.ToInt32(dataSize, 0);
byte[] inBuffer = new byte[dataSizeInt];

var bytesRemaining = dataSizeInt;
while (bytesRemaining > 0 )
bytesRemaining -= dataPipe.Read(inBuffer, 0, bytesRemaining);

Logger.Instance.Log($"ElevationRequest length {dataSizeInt}", LogLevel.Debug);

return (ElevationRequest) new BinaryFormatter()
// { TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesAlways, Binder = new MySerializationBinder() }
.Deserialize(dataPipe);
.Deserialize(new MemoryStream(inBuffer));
}
}
}

0 comments on commit 3d1daff

Please sign in to comment.