mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-21 20:17:22 +01:00
move IsRoot to posix identity
move checking root to CheckSystemCompatibility use Syscall internal getuid
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user