// Bit-size/platform inter-mediator for getting user password information private static int GetLocalUserPasswordInformation(string userName, out UserPasswordInformation userPasswordInfo, out AccountStatus accountStatus) { userPasswordInfo = new UserPasswordInformation(); if (Common.GetOSPlatformID() == PlatformID.MacOSX) { int lastChangeDate, maxDaysForChange, accountExpirationDate; accountStatus = AccountStatus.Normal; // Mac OS X call if (GetLocalUserPasswordInformationMac(userName, out lastChangeDate, out maxDaysForChange, out accountExpirationDate) == 0) { userPasswordInfo.lastChangeDate = lastChangeDate; userPasswordInfo.maxDaysForChange = maxDaysForChange; userPasswordInfo.accountExpirationDate = accountExpirationDate; return 0; } } else { if (IntPtr.Size == 4) { // 32-bit OS call UserPasswordInformation32 userPasswordInfo32 = new UserPasswordInformation32(); if (GetLocalUserPasswordInformation32(userName, ref userPasswordInfo32, out accountStatus) == 0) { userPasswordInfo.lastChangeDate = userPasswordInfo32.lastChangeDate; userPasswordInfo.minDaysForChange = userPasswordInfo32.minDaysForChange; userPasswordInfo.maxDaysForChange = userPasswordInfo32.maxDaysForChange; userPasswordInfo.warningDays = userPasswordInfo32.warningDays; userPasswordInfo.inactivityDays = userPasswordInfo32.inactivityDays; userPasswordInfo.accountExpirationDate = userPasswordInfo32.accountExpirationDate; return 0; } } else { // 64-bit OS call return GetLocalUserPasswordInformation64(userName, ref userPasswordInfo, out accountStatus); } } return 1; }
[DllImport(ImportFileName, EntryPoint = "GetLocalUserPasswordInformation")] // 64-bit version private static extern int GetLocalUserPasswordInformation64(string userName, ref UserPasswordInformation userPasswordInfo, out AccountStatus status);
private static int GetCachedLocalUserPasswordInformation(string userName, out UserPasswordInformation userPasswordInfo, out AccountStatus accountStatus) { // Attempt to retrieve Unix user principal identity in case shadow information has already been parsed, in most // cases we will have already reduced rights needed to read this information so we pick up pre-parsed info WindowsPrincipal principal = Thread.CurrentPrincipal as WindowsPrincipal; if ((object)principal != null) { UnixIdentity identity = principal.Identity as UnixIdentity; // If user has already been authenticated, we can load pre-parsed shadow information if ((object)identity != null && identity.LoadedUserPasswordInformation && identity.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase)) { userPasswordInfo = identity.UserPasswordInformation; accountStatus = identity.AccountStatus; return 0; } } return GetLocalUserPasswordInformation(userName, out userPasswordInfo, out accountStatus); }