2021-04-27 04:41:26 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2021-04-27 05:41:04 +08:00
using System ;
using System.Security.Principal ;
2021-04-27 04:41:26 +08:00
using osu.Framework ;
using osu.Framework.Allocation ;
2021-04-27 09:05:18 +08:00
using osu.Framework.Graphics ;
2021-04-27 04:41:26 +08:00
using osu.Framework.Graphics.Sprites ;
using osu.Game.Graphics ;
using osu.Game.Overlays ;
using osu.Game.Overlays.Notifications ;
2021-04-27 10:37:08 +08:00
namespace osu.Desktop.Security
2021-04-27 04:41:26 +08:00
{
/// <summary>
/// Checks if the game is running with elevated privileges (as admin in Windows, root in Unix) and displays a warning notification if so.
/// </summary>
2021-04-27 09:05:18 +08:00
public partial class ElevatedPrivilegesChecker : Component
2021-04-27 04:41:26 +08:00
{
[Resolved]
2022-08-02 22:23:54 +08:00
private INotificationOverlay notifications { get ; set ; } = null ! ;
2021-04-27 04:41:26 +08:00
2021-04-27 10:37:08 +08:00
private bool elevated ;
2021-04-27 10:51:03 +08:00
[BackgroundDependencyLoader]
private void load ( )
{
2021-04-27 13:35:57 +08:00
elevated = checkElevated ( ) ;
2021-04-27 10:51:03 +08:00
}
2021-04-27 04:41:26 +08:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2021-04-27 12:23:08 +08:00
if ( elevated )
notifications . Post ( new ElevatedPrivilegesNotification ( ) ) ;
2021-04-27 09:05:18 +08:00
}
2021-04-27 04:41:26 +08:00
2021-04-27 13:35:57 +08:00
private bool checkElevated ( )
2021-04-27 10:37:08 +08:00
{
2021-04-27 12:23:08 +08:00
try
2021-04-27 10:37:08 +08:00
{
2021-04-27 12:23:08 +08:00
switch ( RuntimeInfo . OS )
2021-04-27 10:37:08 +08:00
{
2021-04-27 12:23:08 +08:00
case RuntimeInfo . Platform . Windows :
if ( ! OperatingSystem . IsWindows ( ) ) return false ;
2021-04-27 10:37:08 +08:00
2021-04-27 12:23:08 +08:00
var windowsIdentity = WindowsIdentity . GetCurrent ( ) ;
var windowsPrincipal = new WindowsPrincipal ( windowsIdentity ) ;
2021-04-27 10:37:08 +08:00
2021-04-27 12:23:08 +08:00
return windowsPrincipal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
2021-04-27 10:37:08 +08:00
2021-04-27 12:23:08 +08:00
case RuntimeInfo . Platform . macOS :
case RuntimeInfo . Platform . Linux :
return Mono . Unix . Native . Syscall . geteuid ( ) = = 0 ;
}
}
catch
{
2021-04-27 10:37:08 +08:00
}
2021-04-27 13:35:14 +08:00
return false ;
2021-04-27 10:37:08 +08:00
}
2021-04-27 09:05:18 +08:00
private partial class ElevatedPrivilegesNotification : SimpleNotification
2021-04-27 04:41:26 +08:00
{
public override bool IsImportant = > true ;
2021-04-27 09:05:18 +08:00
public ElevatedPrivilegesNotification ( )
2021-04-27 04:41:26 +08:00
{
2021-04-27 13:38:19 +08:00
Text = $"Running osu! as {(RuntimeInfo.IsUnix ? " root " : " administrator ")} does not improve performance, may break integrations and poses a security risk. Please run the game as a normal user." ;
2021-04-27 04:41:26 +08:00
}
[BackgroundDependencyLoader]
2022-01-15 08:06:39 +08:00
private void load ( OsuColour colours )
2021-04-27 04:41:26 +08:00
{
Icon = FontAwesome . Solid . ShieldAlt ;
2022-08-30 16:40:35 +08:00
IconContent . Colour = colours . YellowDark ;
2021-04-27 04:41:26 +08:00
}
}
}
}