Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Bickerstaff committed Jul 9, 2024
1 parent aefb221 commit 2fe8c04
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 39 deletions.
106 changes: 67 additions & 39 deletions SuperLauncher/CredentialExpirationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ namespace SuperLauncher
{
public static class CredentialExpirationService
{
private static Timer Timer = new();
private static Timer CheckTimer = new();
private static Timer NotifyTimer = new();
public static DateTime PasswordLastSet = DateTime.MaxValue;
public static TimeSpan MaxPasswordAge = TimeSpan.MaxValue;
public static TimeSpan MaxPasswordAge = TimeSpan.Zero;
private static ExpStat Status = ExpStat.Unknown;
public static string PasswordExpirationMessage
{
get
{
return
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
"Password expires in " +
ExpirationTimeSpan.Days +
" day(s), " +
ExpirationTimeSpan.Hours +
" hour(s) and " +
ExpirationTimeSpan.Seconds +
" second(s).";
return "An un-known error occured when determining your password expiration date.";
}
}
public static DateTime PasswordExpirationDate
Expand All @@ -41,45 +46,68 @@ public static TimeSpan ExpirationTimeSpan
}
public static void Initialize()
{
Timer.Elapsed += CheckExpiration;
Timer.Enabled = true;
Timer.Interval = 10800000; //3 hours
Timer.Start();
CheckExpiration();
Task.Run(() => {
CheckTimer.Elapsed += CheckTimer_Elapsed;
CheckTimer.Enabled = true;
CheckTimer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds;
CheckTimer.Start();
CheckTimer_Elapsed(null, null);
if (Settings.Default.CredentialExpirationWarningIntervalMinutes <= 0) return;
NotifyTimer.Elapsed += NotifyTimer_Elapsed;
NotifyTimer.Enabled = true;
NotifyTimer.Interval = TimeSpan.FromMinutes(Settings.Default.CredentialExpirationWarningIntervalMinutes).TotalMilliseconds;
NotifyTimer.Start();
NotifyTimer_Elapsed(null, null);
});
}
public static void CheckExpiration(object s = null, object e = null)
private static void NotifyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Task.Run(() => {
try
{
DirectorySearcher ds = new();
ds.SearchScope = SearchScope.Base;
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("maxPwdAge");
ds.Filter = "";
SearchResult root = ds.FindOne();
ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("userAccountControl");
ds.PropertiesToLoad.Add("pwdLastSet");
ds.Filter = "(objectSid=" + WindowsIdentity.GetCurrent().User.Value + ")";
SearchResult user = ds.FindOne();
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
PasswordLastSet = DateTime.FromFileTime((long)user.Properties["pwdLastSet"][0]);
MaxPasswordAge = TimeSpan.FromMicroseconds((long)root.Properties["maxPwdAge"][0] / 10 * -1);
ModernLauncherNotifyIcon.Icon.BalloonTipTitle = RunAsHelper.GetCurrentDomainWithUserName();
ModernLauncherNotifyIcon.Icon.BalloonTipText = PasswordExpirationMessage;
ModernLauncherNotifyIcon.Icon.ShowBalloonTip(0);
}
catch
if (PasswordExpirationDate.CompareTo(DateTime.Now.AddDays(Settings.Default.CredentialExpirationWarningDays)) <= 0)
{
ModernLauncherNotifyIcon.Icon.BalloonTipTitle = RunAsHelper.GetCurrentDomainWithUserName();
ModernLauncherNotifyIcon.Icon.BalloonTipText = PasswordExpirationMessage;
ModernLauncherNotifyIcon.Icon.ShowBalloonTip(0);
}
}
private static void CheckTimer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
DirectorySearcher ds = new();
ds.SearchScope = SearchScope.Base;
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("maxPwdAge");
ds.Filter = "";
SearchResult root = ds.FindOne();
MaxPasswordAge = TimeSpan.FromMicroseconds((long)root.Properties["maxPwdAge"][0] / 10 * -1);
if (MaxPasswordAge == TimeSpan.Zero) { Status = ExpStat.NeverExpires; return; }
ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Clear();
ds.PropertiesToLoad.Add("userAccountControl");
ds.PropertiesToLoad.Add("pwdLastSet");
ds.Filter = "(objectSid=" + WindowsIdentity.GetCurrent().User.Value + ")";
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
if (pwdNeverExpires == true) { Status = ExpStat.NeverExpires; return; }
Status = ExpStat.Expires;
}
catch (Exception ex)
{
if (ex.Source == "System.DirectoryServices")
{
//Just give up
Status = ExpStat.DCNotResponding;
return;
}
});
Status = ExpStat.Unknown;
}
}
private enum ExpStat
{
Expires = 0,
NeverExpires = 1,
DCNotResponding = 2,
Unknown = -1
}
//private static datetime convertfromadtime()
//{

//}
}
}
}
12 changes: 12 additions & 0 deletions SuperLauncher/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SettingsDefault
" <Width>390</Width>" +
" <Height>230</Height>" +
" <CredentialExpirationWarningDays>7</CredentialExpirationWarningDays>" +
" <CredentialExpirationWarningIntervalMinutes>60</CredentialExpirationWarningIntervalMinutes>" +
"</SuperLauncher>";
public bool AutoElevate
{
Expand Down Expand Up @@ -117,6 +118,17 @@ public int CredentialExpirationWarningDays
Write("CredentialExpirationWarningDays", value);
}
}
public int CredentialExpirationWarningIntervalMinutes
{
get
{
return ReadInt("CredentialExpirationWarningIntervalMinutes");
}
set
{
Write("CredentialExpirationWarningIntervalMinutes", value);
}
}
public string Read(string NodeName)
{
XmlNode node = XDoc.SelectSingleNode("/SuperLauncher/" + NodeName);
Expand Down

0 comments on commit 2fe8c04

Please sign in to comment.