move IsRoot to posix identity

move checking root to CheckSystemCompatibility
use Syscall internal getuid
This commit is contained in:
ZephyrTFA
2024-08-18 13:26:09 -04:00
parent 3bcda563a0
commit 8da2424909
3 changed files with 33 additions and 29 deletions
@@ -675,6 +675,11 @@ namespace Tgstation.Server.Host.Components
{
if (!systemIdentity.CanCreateSymlinks)
throw new InvalidOperationException($"The user running {Constants.CanonicalPackageName} cannot create symlinks! Please try running as an administrative user!");
if (systemIdentity is PosixSystemIdentity posixIdentity && posixIdentity.IsRoot())
{
logger.LogWarning("TGS is being run as the root account. This is not recommended and may prevent launch in a future version.");
}
}
// This runs before the real socket is opened, ensures we don't perform reattaches unless we're fairly certain the bind won't fail
-29
View File
@@ -11,7 +11,6 @@ using Tgstation.Server.Host.Core;
using Tgstation.Server.Host.Properties;
using Tgstation.Server.Host.System;
using InteropServices = System.Runtime.InteropServices;
using Process = System.Diagnostics.Process;
namespace Tgstation.Server.Host
@@ -70,34 +69,6 @@ namespace Tgstation.Server.Host
args = listArgs.ToArray();
}
if (InteropServices.RuntimeInformation.IsOSPlatform(InteropServices.OSPlatform.Linux))
{
using var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "id",
Arguments = "-u",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
},
};
proc.Start();
await proc.WaitForExitAsync();
if (proc.ExitCode is not 0 || !int.TryParse(await proc.StandardOutput.ReadToEndAsync(), out var uid))
{
Console.Error.WriteLine("Failed to obtain user id.");
return 1;
}
if (uid is 0)
{
Console.Error.WriteLine("TGS is being run as root. This is not recommended and will prevent launching in a future version!");
}
}
var program = new Program();
return (int)await program.Main(args, updatePath);
}
@@ -2,6 +2,8 @@
using System.Threading;
using System.Threading.Tasks;
using Mono.Unix.Native;
namespace Tgstation.Server.Host.Security
{
/// <summary>
@@ -9,6 +11,32 @@ namespace Tgstation.Server.Host.Security
/// </summary>
sealed class PosixSystemIdentity : ISystemIdentity
{
/// <summary>
/// True if TGS is running under root.
/// </summary>
bool isRoot = false;
/// <summary>
/// True if <see cref="isRoot" /> is populated.
/// </summary>
bool isRootChecked = false;
/// <summary>
/// Checks whether TGS is running under the root user.
/// </summary>
/// <returns>True if running under root. False otherwise.</returns>
public bool IsRoot()
{
if (isRootChecked)
{
return isRoot;
}
isRoot = Syscall.getuid() == 0;
isRootChecked = true;
return isRoot;
}
/// <inheritdoc />
public string Uid => throw new NotImplementedException();