2021-04-26 20:41:26 +00: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-26 21:41:04 +00:00
using System ;
using System.Security.Principal ;
2021-04-26 20:41:26 +00:00
using osu.Framework ;
using osu.Framework.Allocation ;
2021-04-27 01:05:18 +00:00
using osu.Framework.Graphics ;
2021-04-26 20:41:26 +00:00
using osu.Framework.Graphics.Sprites ;
using osu.Game.Graphics ;
using osu.Game.Overlays ;
using osu.Game.Overlays.Notifications ;
2021-04-26 21:41:04 +00:00
namespace osu.Desktop.Admin
2021-04-26 20:41:26 +00: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 01:05:18 +00:00
public class ElevatedPrivilegesChecker : Component
2021-04-26 20:41:26 +00:00
{
[Resolved]
protected NotificationOverlay Notifications { get ; private set ; }
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2021-04-27 01:05:18 +00:00
bool elevated = false ;
if ( OperatingSystem . IsWindows ( ) )
{
var windowsIdentity = WindowsIdentity . GetCurrent ( ) ;
var windowsPrincipal = new WindowsPrincipal ( windowsIdentity ) ;
elevated = windowsPrincipal . IsInRole ( WindowsBuiltInRole . Administrator ) ;
}
else if ( RuntimeInfo . IsUnix )
{
elevated = Mono . Unix . Native . Syscall . geteuid ( ) = = 0 ;
}
if ( ! elevated )
return ;
Notifications . Post ( new ElevatedPrivilegesNotification ( ) ) ;
}
2021-04-26 20:41:26 +00:00
2021-04-27 01:05:18 +00:00
private class ElevatedPrivilegesNotification : SimpleNotification
2021-04-26 20:41:26 +00:00
{
public override bool IsImportant = > true ;
2021-04-27 01:05:18 +00:00
public ElevatedPrivilegesNotification ( )
2021-04-26 20:41:26 +00:00
{
2021-04-26 21:41:04 +00:00
Text = $"Running osu! as {(RuntimeInfo.IsUnix ? " root " : " administrator ")} does not improve performance and poses a security risk. Please run the game normally." ;
2021-04-26 20:41:26 +00:00
}
[BackgroundDependencyLoader]
private void load ( OsuColour colours , NotificationOverlay notificationOverlay )
{
Icon = FontAwesome . Solid . ShieldAlt ;
IconBackgound . Colour = colours . YellowDark ;
Activated = delegate
{
notificationOverlay . Hide ( ) ;
return true ;
} ;
}
}
}
}