first commit
This commit is contained in:
26
Services/SuUserSwitcher.cs
Normal file
26
Services/SuUserSwitcher.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using HanaToolbox.Services.Interfaces;
|
||||
|
||||
namespace HanaToolbox.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a shell command to run as <sid>adm using `su - <sid>adm -c '...'`.
|
||||
/// If the current OS user is already <sid>adm, the command is executed directly.
|
||||
/// </summary>
|
||||
public sealed class SuUserSwitcher(IProcessRunner runner) : IUserSwitcher
|
||||
{
|
||||
public async Task<ProcessResult> RunAsAsync(
|
||||
string sid, string shellCommand,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var sidAdm = $"{sid.ToLowerInvariant()}adm";
|
||||
|
||||
if (string.Equals(Environment.UserName, sidAdm, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Already running as the correct user — run directly via bash
|
||||
return await runner.RunAsync("/bin/bash", ["-c", shellCommand], ct);
|
||||
}
|
||||
|
||||
// Use `su -` to inherit the user's environment (HOME, PATH, etc.)
|
||||
return await runner.RunAsync("/bin/su", ["-", sidAdm, "-c", shellCommand], ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user