27 lines
988 B
C#
27 lines
988 B
C#
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);
|
|
}
|
|
}
|