Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Bickerstaff committed Jul 11, 2024
1 parent f05eca3 commit 50eeba6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 30 deletions.
2 changes: 1 addition & 1 deletion CommonProperties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PlatformTarget>x64</PlatformTarget>
<Platforms>x64</Platforms>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<NoWarn>CS0108;WFAC010</NoWarn>
<NoWarn>CS0108;WFAC010;CA1416</NoWarn>
</PropertyGroup>
<!--<ItemGroup>
<AssemblyAttribute Include="System.Reflection.AssemblyTitleAttribute">
Expand Down
47 changes: 26 additions & 21 deletions SuperLauncher/CredentialExpirationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

namespace SuperLauncher
{
public static class CredentialExpirationService
public class CredentialExpirationService
{
private static Timer CheckTimer = new();
private static Timer NotifyTimer = new();
public static DateTime PasswordLastSet = DateTime.MaxValue;
public static TimeSpan MaxPasswordAge = TimeSpan.MaxValue;
private static ExpStat Status = ExpStat.Unknown;
public static string PasswordExpirationMessage
private readonly Timer CheckTimer = new();
private readonly Timer NotifyTimer = new();
private DateTime PasswordLastSet = DateTime.MaxValue;
private TimeSpan MaxPasswordAge = TimeSpan.MaxValue;
private ExpStat Status = ExpStat.Unknown;
private readonly SecurityIdentifier SID;
public string PasswordExpirationMessage
{
get
{
if (Status == ExpStat.NeverExpires) return "Password never expires.";
if (Status == ExpStat.DCNotResponding) return "Could not determine password expiration date, Active Directory is offline.";
if (Status == ExpStat.Expires) return
if (Status == ExpStat.Expires) return
"Password expires in " +
ExpirationTimeSpan.Days +
" day(s), " +
Expand All @@ -30,24 +31,26 @@ public static string PasswordExpirationMessage
return "An un-known error occured when determining your password expiration date.";
}
}
public static DateTime PasswordExpirationDate
public DateTime PasswordExpirationDate
{
get
{
if (MaxPasswordAge == TimeSpan.MaxValue) return DateTime.MaxValue;
return PasswordLastSet.Add(MaxPasswordAge);
}
}
public static TimeSpan ExpirationTimeSpan
public TimeSpan ExpirationTimeSpan
{
get
{
return PasswordExpirationDate.Subtract(DateTime.Now);
}
}
public static void Initialize()
public CredentialExpirationService(SecurityIdentifier SID)
{
Task.Run(() => {
this.SID = SID;
Task.Run(() =>
{
CheckTimer.Elapsed += CheckTimer_Elapsed;
CheckTimer.Enabled = true;
CheckTimer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds;
Expand All @@ -61,7 +64,7 @@ public static void Initialize()
NotifyTimer_Elapsed(null, null);
});
}
private static void NotifyTimer_Elapsed(object sender, ElapsedEventArgs e)
private void NotifyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (PasswordExpirationDate.CompareTo(DateTime.Now.AddDays(Settings.Default.CredentialExpirationWarningDays)) <= 0)
{
Expand All @@ -70,12 +73,14 @@ private static void NotifyTimer_Elapsed(object sender, ElapsedEventArgs e)
ModernLauncherNotifyIcon.Icon.ShowBalloonTip(0);
}
}
private static void CheckTimer_Elapsed(object sender, ElapsedEventArgs e)
private void CheckTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
DirectorySearcher ds = new();
ds.SearchScope = SearchScope.Base;
DirectorySearcher ds = new()
{
SearchScope = SearchScope.Base
};
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("maxPwdAge");
ds.Filter = "";
Expand All @@ -86,7 +91,7 @@ private static void CheckTimer_Elapsed(object sender, ElapsedEventArgs e)
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("userAccountControl");
ds.PropertiesToLoad.Add("pwdLastSet");
ds.Filter = "(objectSid=" + WindowsIdentity.GetCurrent().User.Value + ")";
ds.Filter = "(objectSid=" + SID + ")";
SearchResult user = ds.FindOne();
PasswordLastSet = DateTime.FromFileTime((long)user.Properties["pwdLastSet"][0]);
bool pwdNeverExpires = (((int)user.Properties["userAccountControl"][0]) & 0x00010000) == 0x00010000; //https://learn.microsoft.com/en-us/windows/win32/api/iads/ne-iads-ads_user_flag_enum
Expand All @@ -105,10 +110,10 @@ private static void CheckTimer_Elapsed(object sender, ElapsedEventArgs e)
}
private enum ExpStat
{
Expires = 0,
NeverExpires = 1,
DCNotResponding = 2,
Unknown = -1
Expires = 0,
NeverExpires = 1,
DCNotResponding = 2,
Unknown = -1
}
}
}
35 changes: 29 additions & 6 deletions SuperLauncher/ModernLauncher.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -22,9 +23,10 @@ public static DpiScale DPI
private bool IsClosing = false;
private static DpiScale rDPI;
private WindowInteropHelper WIH;
private uint ShowSuperLauncherMessage = Win32Interop.RegisterWindowMessage("ShowSuperLauncher");
private readonly uint ShowSuperLauncherMessage = Win32Interop.RegisterWindowMessage("ShowSuperLauncher");
private HwndSource HWND;
private ModernLauncherBadge ExpirationBadge = null;
private readonly Dictionary<string, CredentialExpirationService> CredentialExpirationServices = [];
private readonly DoubleAnimation RenderBoostAnimation = new()
{
Duration = TimeSpan.FromSeconds(0.5),
Expand Down Expand Up @@ -93,8 +95,10 @@ private void Icon_Click(object sender, System.Windows.Forms.MouseEventArgs e)
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
Win32Interop.GetCursorPos(out Win32Interop.POINT point);
ModernLauncherContextMenu menu = new();
menu.Topmost = true;
ModernLauncherContextMenu menu = new()
{
Topmost = true
};
menu.Frame.Content = new ModernLauncherContextMenuMain();
menu.Top = DPI.ScalePixelsDown(point.y) - 240;
menu.Left = DPI.ScalePixelsDown(point.x) - 80;
Expand Down Expand Up @@ -149,13 +153,23 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
OpenTopAnimation.Completed += OpenTopAnimation_Completed;
CloseTopAnimation.Completed += CloseTopAnimation_Completed;
InitializeNotifyIcon();
CredentialExpirationService.Initialize();
Win32Interop.RegisterHotKey(WIH.Handle, 0, 0x1 | 0x4000, 0x53); //Register Hot Key ALT + S
Win32Interop.RegisterHotKey(WIH.Handle, 1, 0x1 | 0x4000, 0x45); //Register Hot Key ALT + E
Win32Interop.RegisterHotKey(WIH.Handle, 2, 0x1 | 0x4000, 0x52); //Register Hot Key ALT + R
HWND.AddHook(HwndSourceHook);
_ = Win32Interop.SetWindowLong(WIH.Handle, Win32Interop.SetWindowLongIndex.GWL_EXSTYLE, Win32Interop.ExtendedWindowStyles.WS_EX_TOOLWINDOW);
SetElevateLabels();
if (RunAsHelper.GetOriginalInvokerDomainWithUserName() != RunAsHelper.GetCurrentDomainWithUserName())
{
CredentialExpirationServices.Add(
RunAsHelper.GetOriginalInvokerDomainWithUserName(),
new(RunAsHelper.GetOriginalInvokerSID())
);
}
CredentialExpirationServices.Add(
RunAsHelper.GetCurrentDomainWithUserName(),
new(RunAsHelper.GetCurrentSID())
);
}
public void OpenWindow(bool Center = false)
{
Expand Down Expand Up @@ -286,12 +300,21 @@ private void BtnAdd_Click(object sender, RoutedEventArgs e)
}
private void ElevateUser_MouseEnter(object sender, MouseEventArgs e)
{
ExpirationBadge = new(CredentialExpirationService.PasswordExpirationMessage);
List<string> lines = [];
foreach (KeyValuePair<string, CredentialExpirationService> kvp in CredentialExpirationServices)
{
string message = string.Empty;
message += kvp.Key;
message += ": ";
message += kvp.Value.PasswordExpirationMessage;
lines.Add(message);
}
ExpirationBadge = new(string.Join('\n', lines));
ExpirationBadge.Show();
}
private void ElevateUser_MouseLeave(object sender, MouseEventArgs e)
{
if (ExpirationBadge != null) ExpirationBadge.Close();
ExpirationBadge?.Close();
}
}
}
1 change: 0 additions & 1 deletion SuperLauncher/ModernLauncherBadge.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Windows;
using static System.Net.Mime.MediaTypeNames;

namespace SuperLauncher
{
Expand Down
4 changes: 4 additions & 0 deletions SuperLauncher/RunAsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public static SecurityIdentifier GetOriginalInvokerSID()
{
return new WindowsIdentity(GetOriginalInvokerUPN()).User;
}
public static SecurityIdentifier GetCurrentSID()
{
return WindowsIdentity.GetCurrent().User;
}
public static void Exit()
{
if (Program.ModernApplication != null)
Expand Down
2 changes: 1 addition & 1 deletion SuperLauncher/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SettingsDefault
public string configPath = Path.Combine(@"C:\Users\Public\Documents\Below Average\Super Launcher\", RunAsHelper.GetOriginalInvokerDomain(), RunAsHelper.GetOriginalInvokerUserName(), "SuperLauncherConfig.xml");
public XmlDocument XDoc = new();
public XmlDocument XDocDefault = new();
private string DefaultXML =
private string DefaultXML =
"<!-- Super Launcher Config File -->" +
"<SuperLauncher>" +
" <AutoElevate>false</AutoElevate>" +
Expand Down

0 comments on commit 50eeba6

Please sign in to comment.