/// <summary>
        /// GetUserCertificate method implmentation
        /// </summary>
        public override X509Certificate2 GetUserCertificate(string upn)
        {
            string        request = "SELECT CERTIFICATE FROM KEYS WHERE UPN=@UPN";
            SqlConnection con     = new SqlConnection(_connectionstring);
            SqlCommand    sql     = new SqlCommand(request, con);

            SqlParameter prm = new SqlParameter("@UPN", SqlDbType.VarChar);

            sql.Parameters.Add(prm);
            prm.Value = upn.ToLower();

            con.Open();
            try
            {
                SqlDataReader rd = sql.ExecuteReader();
                if (rd.Read())
                {
                    string strcert = rd.GetString(0);
                    return(new X509Certificate2(Convert.FromBase64String(strcert), "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
        /// <summary>
        /// RemoveUserKey method implmentation
        /// </summary>
        public override bool RemoveUserKey(string upn)
        {
            string request = "DELETE FROM KEYS WHERE UPN=@UPN";

            SqlConnection con = new SqlConnection(_connectionstring);
            SqlCommand    sql = new SqlCommand(request, con);

            SqlParameter pupn = new SqlParameter("@UPN", SqlDbType.VarChar);

            sql.Parameters.Add(pupn);
            pupn.Value = upn.ToLower();

            con.Open();
            try
            {
                int res = sql.ExecuteNonQuery();
                return(res > 0);
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
        /// <summary>
        /// HasStoredKey method implementation
        /// </summary>
        public override bool HasStoredKey(string upn)
        {
            string request = "SELECT ID, UPN FROM KEYS WHERE UPN=@UPN";

            SqlConnection con = new SqlConnection(_connectionstring);
            SqlCommand    sql = new SqlCommand(request, con);

            SqlParameter prm = new SqlParameter("@UPN", SqlDbType.VarChar);

            sql.Parameters.Add(prm);
            prm.Value = upn.ToLower();
            con.Open();
            try
            {
                SqlDataReader rd = sql.ExecuteReader();
                return(rd.Read());
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
Example #4
0
        private void PerformLogWrite()
        {
            DateTime timeNow = DateTime.Now;

            tbLastLogTime.Text = timeNow.ToString("HH:mm:ss.f");
            tbNextLogTime.Text = timeNow.Add(loggingTimer.Interval).ToString("HH:mm:ss.f");

            btnLog.IsEnabled = false;

            for (int i = 0; i < aiFilters.Length; i++)
            {
                logToFile.BufferEntry(aiFilters[i].output.ToString("F3"));
            }

            foreach (Sensor s in daqSim.di)
            {
                logToFile.BufferEntry(s.valStr);
            }

            if (logToFile.WriteEntry())
            {
                tbLogEntryCount.Text = logToFile.NumEntries.ToString();
                loggingTimer.Go();
            }
            else
            {
                tbLogEntryCount.Text = logToFile.NumEntries.ToString() + " !--ERR--!";
            }
        }
Example #5
0
        /// <summary>
        /// SetUserCredential method implementation
        /// </summary>
        public bool SetUserCredential(MFAWebAuthNUser user, MFAUserCredential credential)
        {
            bool result = false;

            try
            {
                credential.UserId = user.Id;
                List <MFAUserCredential> _lst = _mfacredusers.GetData();
                _lst.Where(s => s.UserId.SequenceEqual(user.Id) && (s.Descriptor.Id.SequenceEqual(credential.Descriptor.Id))).ToList()
                .ForEach(s =>
                {
                    s.AaGuid                = credential.AaGuid;
                    s.CredType              = credential.CredType;
                    s.Descriptor            = credential.Descriptor;
                    s.Descriptor.Id         = credential.Descriptor.Id;
                    s.Descriptor.Transports = credential.Descriptor.Transports;
                    s.Descriptor.Type       = credential.Descriptor.Type;
                    s.PublicKey             = credential.PublicKey;
                    s.RegDate               = credential.RegDate;
                    s.SignatureCounter      = credential.SignatureCounter;
                    s.UserHandle            = credential.UserHandle;
                    s.UserId                = credential.UserId;
                    result = true;
                });
                _mfacredusers.SetData(_lst);
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            return(result);
        }
Example #6
0
        /// <summary>
        /// CheckMFAUser method implmentation
        /// </summary>
        private bool CheckMFAUser(UsersADDSRecord Parameters, string identity)
        {
            try
            {
                using (DirectoryEntry rootdir = ADDSUtils.GetDirectoryEntry(Parameters.DomainName, Parameters.UserName, Parameters.Password))
                {
                    string qryldap = "(&(objectCategory=person)(objectClass=user)(" + ADDSClaimsUtilities.GetADDSSearchAttribute() + "=" + identity + "))";
                    using (DirectorySearcher dsusr = new DirectorySearcher(rootdir, qryldap))
                    {
                        dsusr.PropertiesToLoad.Clear();
                        dsusr.PropertiesToLoad.Add("objectGUID");
                        dsusr.PropertiesToLoad.Add("userPrincipalName");
                        dsusr.PropertiesToLoad.Add("sAMAccountName");
                        dsusr.PropertiesToLoad.Add("msDS-PrincipalName");
                        dsusr.ReferralChasing = ReferralChasingOption.All;

                        SearchResult sr = dsusr.FindOne();
                        if (sr != null)
                        {
                            return(sr.Properties["objectGUID"][0] != null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                // throw new Exception(ex.Message);
            }
            return(false);
        }
Example #7
0
 /// <summary>
 /// IsMFAUserRegistered method implementation
 /// </summary>
 public override bool IsMFAUserRegistered(string upn)
 {
     try
     {
         return(GetMFAUser(upn) != null);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         return(false);
     }
 }
Example #8
0
 /// <summary>
 /// GetCredentialByCredentialId method implementation
 /// </summary>
 public MFAUserCredential GetCredentialByCredentialId(MFAWebAuthNUser user, string credentialId)
 {
     try
     {
         List <MFAUserCredential> _creds = _mfacredusers.GetData();
         return(_creds.FirstOrDefault(s => s.UserId.SequenceEqual(user.Id) && (HexaEncoding.GetHexStringFromByteArray(s.Descriptor.Id)).Equals(credentialId)));
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
 }
Example #9
0
 /// <summary>
 /// GetCredentialsByUser method implementation
 /// </summary>
 public List <MFAUserCredential> GetCredentialsByUser(MFAWebAuthNUser user)
 {
     try
     {
         List <MFAUserCredential> _creds = _mfacredusers.GetData();
         return(_creds.Where(s => s.UserId.SequenceEqual(user.Id)).ToList());
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
 }
Example #10
0
 /// <summary>
 /// RemoveUserCredential method implementation
 /// </summary>
 public bool RemoveUserCredential(MFAWebAuthNUser user, string credentialId)
 {
     try
     {
         List <MFAUserCredential> _lst = _mfacredusers.GetData();
         int res = _lst.RemoveAll(s => s.UserId.SequenceEqual(user.Id) && (HexaEncoding.GetHexStringFromByteArray(s.Descriptor.Id)).Equals(credentialId));
         _mfacredusers.SetData(_lst);
         return(res > 0);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
 }
Example #11
0
 /// <summary>
 /// RemoveUserKey method implementation
 /// </summary>
 public override bool RemoveUserKey(string upn)
 {
     try
     {
         List <MFAUserKeys> _lst = _mfakeysusers.GetData();
         int res = _lst.RemoveAll(s => s.UserName.ToLower().Equals(upn.ToLower()));
         _mfakeysusers.SetData(_lst);
         return(res > 0);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
 }
Example #12
0
 /// <summary>
 /// AddCredential method implementation
 /// </summary>
 public bool AddUserCredential(MFAWebAuthNUser user, MFAUserCredential credential)
 {
     try
     {
         credential.UserId = user.Id;
         List <MFAUserCredential> _lst = _mfacredusers.GetData();
         _lst.Add(credential);
         _mfacredusers.SetData(_lst);
         return(true);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
 }
Example #13
0
        /// <summary>
        /// HasStoredKey method implementation
        /// </summary>
        public override bool HasStoredKey(string upn)
        {
            bool result = false;

            try
            {
                List <MFAUserKeys> _lst = _mfakeysusers.GetData();
                result = (_lst.FirstOrDefault(s => s.UserName.ToLower().Equals(upn.ToLower()) && (!string.IsNullOrEmpty(s.UserKey))) != null);
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            return(result);
        }
Example #14
0
 /// <summary>
 /// IsMFAUserRegistered method implementation
 /// </summary>
 private bool IsMFAUserRegistered(string upn)
 {
     try
     {
         if (string.IsNullOrEmpty(upn))
         {
             return(false);
         }
         List <MFAUser> _lst = _mfausers.GetData();
         return(_lst.FirstOrDefault(s => s.UPN.ToLower().Equals(upn.ToLower())) != null);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         return(false);
     }
 }
Example #15
0
 /// <summary>
 /// DoUpdateUserKey method implementation
 /// </summary>
 private void DoUpdateUserKey(string upn, string secretkey)
 {
     try
     {
         List <MFAUserKeys> _lst = _mfakeysusers.GetData();
         _lst.Where(s => s.UserName.ToLower().Equals(upn.ToLower())).ToList()
         .ForEach(s =>
         {
             s.UserKey = secretkey;
         });
         _mfakeysusers.SetData(_lst);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
     return;
 }
Example #16
0
        /// <summary>
        /// GetUserKey method implementation
        /// </summary>
        public override string GetUserKey(string upn)
        {
            string result = string.Empty;

            try
            {
                List <MFAUserKeys> _lst = _mfakeysusers.GetData();
                MFAUserKeys        _itm = _lst.FirstOrDefault(s => s.UserName.ToLower().Equals(upn.ToLower()) && (!string.IsNullOrEmpty(s.UserKey)));
                if (_itm != null)
                {
                    result = _itm.UserKey;
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            return(result);
        }
Example #17
0
 /// <summary>
 /// DoUpdateUserCertificate method implementation
 /// </summary>
 private void DoUpdateUserCertificate(string upn, X509Certificate2 cert)
 {
     try
     {
         List <MFAUserKeys> _lst = _mfakeysusers.GetData();
         _lst.Where(s => s.UserName.ToLower().Equals(upn.ToLower())).ToList()
         .ForEach(s =>
         {
             s.UserCertificate = Convert.ToBase64String(cert.Export(X509ContentType.Pfx, CheckSumEncoding.CheckSumAsString(upn)));
             cert.Reset();
         });
         _mfakeysusers.SetData(_lst);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
     return;
 }
Example #18
0
        //////////////////////////////////////////////////////
        // Datalog functions
        private void LogInitialize()
        {
            logToFile      = new DataLog(Config.Charkey("dataLogDelim", ','));
            tbLogPath.Text = logToFile.FilePath;

            tbLogEntryCount.Text = logToFile.NumEntries.ToString();

            logToFile.BufferEntry("Timestamp");

            foreach (Sensor s in daqSim.ai)
            {
                logToFile.BufferEntry(s.name);
            }

            foreach (Sensor s in daqSim.di)
            {
                logToFile.BufferEntry(s.name);
            }

            logToFile.WriteEntry(tStamp: false, incrCtr: false);
        }
        /// <summary>
        /// GetUserCertificate method implementation
        /// </summary>
        public override X509Certificate2 GetUserCertificate(string upn, string password)
        {
            X509Certificate2 result = null;

            try
            {
                List <MFAUserKeys> _lst = _mfakeysusers.GetData();
                MFAUserKeys        _itm = _lst.FirstOrDefault(s => s.UserName.ToLower().Equals(upn.ToLower()) && (!string.IsNullOrEmpty(s.UserCertificate)));
                if (_itm != null)
                {
                    X509Certificate2 cert = new X509Certificate2(Convert.FromBase64String(_itm.UserCertificate), password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.EphemeralKeySet);
                    result = cert;
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            return(result);
        }
Example #20
0
 /// <summary>
 /// DisableMFAUser method implementation
 /// </summary>
 public override MFAUser DisableMFAUser(MFAUser reg)
 {
     if (!IsMFAUserRegistered(reg.UPN))
     {
         throw new Exception("The user " + reg.UPN + " cannot be updated ! \r User not found !");
     }
     try
     {
         reg.Enabled      = false;
         reg.IsRegistered = true;
         List <MFAUser> _lst = _mfausers.GetData();
         _lst.Where(s => s.UPN.ToLower().Equals(reg.UPN.ToLower())).ToList().ForEach(s =>
                                                                                     { s.IsRegistered = reg.IsRegistered; s.Enabled = reg.Enabled; });
         _mfausers.SetData(_lst);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
     return(reg);
 }
Example #21
0
        /// <summary>
        /// UpdateStoredKey method implementation
        /// </summary>
        private string DoUpdateUserKey(string upn, string secretkey, X509Certificate2 certificate)
        {
            string request = "UPDATE KEYS SET SECRETKEY = @SECRETKEY, CERTIFICATE = @CERTIFICATE WHERE UPN=@UPN";

            SqlConnection con = new SqlConnection(_connectionstring);
            SqlCommand    sql = new SqlCommand(request, con);

            SqlParameter pupn = new SqlParameter("@UPN", SqlDbType.VarChar);

            sql.Parameters.Add(pupn);
            pupn.Value = upn.ToLower();

            SqlParameter psecret = new SqlParameter("@SECRETKEY", SqlDbType.VarChar, 8000);

            sql.Parameters.Add(psecret);
            psecret.Value = secretkey;

            SqlParameter pcert = new SqlParameter("@CERTIFICATE", SqlDbType.VarChar, 8000);

            sql.Parameters.Add(pcert);
            pcert.Value = Convert.ToBase64String(certificate.Export(X509ContentType.Pfx, CheckSumEncoding.CheckSumAsString(upn)));

            con.Open();
            try
            {
                int res = sql.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            finally
            {
                certificate.Reset();
                con.Close();
            }
            return(secretkey);
        }
Example #22
0
        /// <summary>
        /// DoInsertUserCertificate method implementation
        /// </summary>
        private void DoInsertUserCertificate(string upn, X509Certificate2 cert)
        {
            List <MFAUserKeys> _lst = _mfakeysusers.GetData();

            try
            {
                MFAUserKeys _itm = new MFAUserKeys()
                {
                    UserName        = upn.ToLower(),
                    UserKey         = string.Empty,
                    UserCertificate = Convert.ToBase64String(cert.Export(X509ContentType.Pfx, CheckSumEncoding.CheckSumAsString(upn)))
                };
                cert.Reset();
                _lst.Add(_itm);
                _mfakeysusers.SetData(_lst);
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// InsertStoredKey method implementation
        /// </summary>
        private string DoInsertUserKey(string upn, string secretkey, string certificate)
        {
            string request = "INSERT INTO KEYS (UPN, SECRETKEY, CERTIFICATE) VALUES (@UPN, @SECRETKEY, @CERTIFICATE)";

            SqlConnection con = new SqlConnection(_connectionstring);
            SqlCommand    sql = new SqlCommand(request, con);

            SqlParameter pupn = new SqlParameter("@UPN", SqlDbType.VarChar);

            sql.Parameters.Add(pupn);
            pupn.Value = upn.ToLower();

            SqlParameter psecret = new SqlParameter("@SECRETKEY", SqlDbType.VarChar);

            sql.Parameters.Add(psecret);
            psecret.Value = secretkey;

            SqlParameter pcert = new SqlParameter("@CERTIFICATE", SqlDbType.VarChar);

            sql.Parameters.Add(pcert);
            pcert.Value = certificate;

            con.Open();
            try
            {
                int res = sql.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
            return(secretkey);
        }
Example #24
0
 /// <summary>
 /// DeleteMFAUser method implementation
 /// </summary>
 public override bool DeleteMFAUser(MFAUser reg, bool dropkey = true)
 {
     if (!IsMFAUserRegistered(reg.UPN))
     {
         throw new Exception("The user " + reg.UPN + " cannot be deleted ! \r User not found !");
     }
     try
     {
         if (dropkey)
         {
             this.OnKeyDataEvent(reg.UPN, KeysDataManagerEventKind.Remove);
         }
         List <MFAUser> _lst = _mfausers.GetData();
         _lst.RemoveAll(s => s.UPN.ToLower().Equals(reg.UPN.ToLower()));
         _mfausers.SetData(_lst);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
     return(true);
 }
Example #25
0
 /// <summary>
 /// AddMFAUser method implementation
 /// </summary>
 public override MFAUser AddMFAUser(MFAUser reg, bool resetkey = false, bool canupdate = true, bool disableoninsert = false)
 {
     if (IsMFAUserRegistered(reg.UPN))
     {
         if (canupdate)
         {
             return(SetMFAUser(reg, resetkey, false));
         }
         else
         {
             return(GetMFAUser(reg.UPN));
         }
     }
     try
     {
         MFAUser usr = GetMFAUser(reg.UPN);
         if (disableoninsert) // disable change if not explicitely done
         {
             reg.Enabled = false;
         }
         reg.IsRegistered = true;
         if (resetkey)
         {
             this.OnKeyDataEvent(reg.UPN, KeysDataManagerEventKind.add);
         }
         List <MFAUser> _lst = _mfausers.GetData();
         _lst.Add(reg);
         _mfausers.SetData(_lst);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
     return(GetMFAUser(reg.UPN));
 }
Example #26
0
        /// <summary>
        /// GetUserKey method implmentation
        /// </summary>
        public override string GetUserKey(string upn)
        {
            string        request = "SELECT SECRETKEY FROM KEYS WHERE UPN=@UPN";
            SqlConnection con     = new SqlConnection(_connectionstring);
            SqlCommand    sql     = new SqlCommand(request, con);

            SqlParameter prm = new SqlParameter("@UPN", SqlDbType.VarChar);

            sql.Parameters.Add(prm);
            prm.Value = upn.ToLower();

            Registration reg = new Registration();

            con.Open();
            try
            {
                SqlDataReader rd = sql.ExecuteReader();
                if (rd.Read())
                {
                    return(rd.GetString(0));
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
Example #27
0
        /// <summary>
        /// ImportMFAUsers method implementation
        /// </summary>
        public virtual MFAUserList ImportMFAUsers(UsersADDSRecord Parameters, bool disableall = false)
        {
            if (!string.IsNullOrEmpty(Parameters.LDAPPath))
            {
                Parameters.LDAPPath = Parameters.LDAPPath.Replace("ldap://", "");
                Parameters.LDAPPath = Parameters.LDAPPath.Replace("ldaps://", "");
                Parameters.LDAPPath = Parameters.LDAPPath.Replace("LDAP://", "");
                Parameters.LDAPPath = Parameters.LDAPPath.Replace("LDAPS://", "");
            }
            MFAUniqueUserList registrations = new MFAUniqueUserList();

            try
            {
                using (DirectoryEntry rootdir = ADDSUtils.GetDirectoryEntry(Parameters.DomainName, Parameters.UserName, Parameters.Password, Parameters.LDAPPath))
                {
                    string qryldap = string.Empty;
                    string subldap = string.Empty;
                    bool   hasval1 = false;
                    bool   hasval2 = false;
                    qryldap = "(|(&(objectCategory=group)(objectClass=group))(&(objectCategory=user)(objectClass=user)";
                    if (Parameters.CreatedSince.HasValue)
                    {
                        subldap += "(whenCreated>=" + Parameters.CreatedSince.Value.ToString("yyyyMMddHHmmss.0Z") + ")";
                        hasval1  = true;
                    }
                    if (Parameters.ModifiedSince.HasValue)
                    {
                        subldap += "(whenChanged>=" + Parameters.ModifiedSince.Value.ToString("yyyyMMddHHmmss.0Z") + ")";
                        hasval2  = true;
                    }
                    if (hasval1 && hasval2)
                    {
                        qryldap += "(|" + subldap + ")";
                    }
                    else if (hasval1 || hasval2)
                    {
                        qryldap += subldap;
                    }
                    qryldap += "))";

                    using (DirectorySearcher dsusr = new DirectorySearcher(rootdir, qryldap))
                    {
                        AddPropertiesToLoadForSearcher(dsusr, Parameters.MailAttribute, Parameters.PhoneAttribute);
                        dsusr.SizeLimit = 100000; // Set maxrows
                        dsusr.PageSize  = 5000;

                        SearchResultCollection src = dsusr.FindAll();
                        if (src != null)
                        {
                            foreach (SearchResult sr in src)
                            {
                                using (DirectoryEntry DirEntry = ADDSUtils.GetDirectoryEntry(Parameters.DomainName, Parameters.UserName, Parameters.Password, sr))
                                {
                                    int k = IsImportUser(DirEntry.Properties["objectClass"].Value);
                                    switch (k)
                                    {
                                    case 1:
                                        DoImportUser(DirEntry, registrations, Parameters, disableall);
                                        break;

                                    case 2:
                                        if (!Parameters.NoRecurse)
                                        {
                                            DoImportGroup(DirEntry, registrations, Parameters, disableall);
                                        }
                                        break;

                                    default:
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry("Root : " + ex.Message, System.Diagnostics.EventLogEntryType.Error, 5100);
                // throw new Exception(ex.Message);
            }
            return(registrations);
        }
Example #28
0
        /// <summary>
        /// DoImportGroup method implementation
        /// </summary>
        private void DoImportGroup(DirectoryEntry DirEntry, MFAUniqueUserList users, UsersADDSRecord Parameters, bool disableall)
        {
            string distinguishedName = string.Empty;
            string sidstr            = string.Empty;

            try
            {
                distinguishedName = DirEntry.Properties["distinguishedName"].Value.ToString();
                byte[] SD  = (byte[])DirEntry.Properties["objectSID"].Value;
                string sid = new SecurityIdentifier(SD, 0).ToString();
                sidstr = sid.Substring(sid.LastIndexOf("-") + 1);
                using (DirectoryEntry rootdir = ADDSUtils.GetDirectoryEntry(Parameters.DomainName, Parameters.UserName, Parameters.Password)) // Binding Root
                {
                    string qryldap = string.Empty;
                    string subldap = string.Empty;
                    bool   hasval1 = false;
                    bool   hasval2 = false;
                    qryldap = "(| (&(objectCategory=group)(objectClass=group)(memberof=" + distinguishedName + ")) (&(objectCategory=user)(objectClass=user)(|(memberof=" + distinguishedName + ")(primaryGroupID=" + sidstr + "))";
                    if (Parameters.CreatedSince.HasValue)
                    {
                        subldap += "(whenCreated>=" + Parameters.CreatedSince.Value.ToString("yyyyMMddHHmmss.0Z") + ")";
                        hasval1  = true;
                    }
                    if (Parameters.ModifiedSince.HasValue)
                    {
                        subldap += "(whenChanged>=" + Parameters.ModifiedSince.Value.ToString("yyyyMMddHHmmss.0Z") + ")";
                        hasval2  = true;
                    }
                    if (hasval1 && hasval2)
                    {
                        qryldap += "(|" + subldap + ")";
                    }
                    else if (hasval1 || hasval2)
                    {
                        qryldap += subldap;
                    }
                    qryldap += "))";

                    using (DirectorySearcher dsusr = new DirectorySearcher(rootdir, qryldap))
                    {
                        AddPropertiesToLoadForSearcher(dsusr, Parameters.MailAttribute, Parameters.PhoneAttribute);
                        dsusr.SizeLimit = 100000; // Set maxrows
                        dsusr.PageSize  = 5000;

                        SearchResultCollection src = dsusr.FindAll();
                        if (src != null)
                        {
                            foreach (SearchResult sr in src)
                            {
                                using (DirectoryEntry SubDirEntry = ADDSUtils.GetDirectoryEntry(Parameters.DomainName, Parameters.UserName, Parameters.Password, sr))
                                {
                                    int k = IsImportUser(SubDirEntry.Properties["objectClass"].Value);
                                    switch (k)
                                    {
                                    case 1:
                                        DoImportUser(SubDirEntry, users, Parameters, disableall);
                                        break;

                                    case 2:
                                        if (!Parameters.NoRecurse)
                                        {
                                            DoImportGroup(SubDirEntry, users, Parameters, disableall);
                                        }
                                        break;

                                    default:
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DataLog.WriteEntry("DN : " + distinguishedName + "     SID : " + sidstr + "     Error : " + ex.Message, System.Diagnostics.EventLogEntryType.Error, 5100);
                // throw new Exception(ex.Message);
            }
        }
Example #29
0
 /// <summary>
 /// SetMFAUser method implementation
 /// </summary>
 public override MFAUser SetMFAUser(MFAUser reg, bool resetkey = false, bool caninsert = true, bool disableoninsert = false)
 {
     if (!IsMFAUserRegistered(reg.UPN))
     {
         if (caninsert)
         {
             return(AddMFAUser(reg, resetkey, false));
         }
         else
         {
             return(GetMFAUser(reg.UPN));
         }
     }
     try
     {
         MFAUser usr = GetMFAUser(reg.UPN);
         if (!disableoninsert) // disable change if not explicitely done
         {
             if (reg.Enabled)
             {
                 usr.Enabled = true;
             }
             else
             {
                 usr.Enabled = false;
             }
         }
         else
         {
             usr.Enabled = false;
         }
         usr.IsRegistered    = true;
         usr.MailAddress     = reg.MailAddress;
         usr.PhoneNumber     = reg.PhoneNumber;
         usr.PreferredMethod = reg.PreferredMethod;
         usr.PIN             = reg.PIN;
         if (string.IsNullOrEmpty(reg.OverrideMethod))
         {
             usr.OverrideMethod = string.Empty;
         }
         else
         {
             usr.OverrideMethod = reg.OverrideMethod;
         }
         if (resetkey)
         {
             this.OnKeyDataEvent(reg.UPN, KeysDataManagerEventKind.add);
         }
         List <MFAUser> _lst = _mfausers.GetData();
         _lst.Where(s => s.UPN.ToLower().Equals(reg.UPN.ToLower())).ToList().ForEach(s =>
         {
             s.Enabled         = usr.Enabled;
             s.MailAddress     = usr.MailAddress;
             s.PhoneNumber     = usr.PhoneNumber;
             s.PreferredMethod = usr.PreferredMethod;
             s.OverrideMethod  = usr.OverrideMethod;
             s.PIN             = usr.PIN;
         });
         _mfausers.SetData(_lst);
     }
     catch (Exception ex)
     {
         DataLog.WriteEntry(ex.Message, System.Diagnostics.EventLogEntryType.Error, 5000);
         throw new Exception(ex.Message);
     }
     return(GetMFAUser(reg.UPN));
 }
Example #30
0
        /// <summary>
        /// DoImportUser method implementation
        /// </summary>
        private void DoImportUser(DirectoryEntry DirEntry, MFAUniqueUserList users, UsersADDSRecord Parameters, bool disableall = false)
        {
            if (DirEntry.Properties["objectGUID"].Value != null)
            {
                MFAUser reg = new MFAUser();
                try
                {
                    reg.ID = new Guid((byte[])DirEntry.Properties["objectGUID"].Value).ToString();
                    if (DirEntry.Properties[ADDSClaimsUtilities.GetADDSUserAttribute()] != null)
                    {
                        if (DirEntry.Properties[ADDSClaimsUtilities.GetADDSUserAttribute()].Count > 0)
                        {
                            reg.UPN = DirEntry.Properties[ADDSClaimsUtilities.GetADDSUserAttribute()][0].ToString();
                            if (!string.IsNullOrEmpty(Parameters.MailAttribute))
                            {
                                if (DirEntry.Properties[Parameters.MailAttribute].Value != null)
                                {
                                    reg.MailAddress = DirEntry.Properties[Parameters.MailAttribute].Value.ToString();
                                }
                            }
                            else
                            {
                                if (DirEntry.Properties["otherMailbox"].Value != null)
                                {
                                    reg.MailAddress = DirEntry.Properties["otherMailbox"].Value.ToString();
                                }
                                else if (DirEntry.Properties["mail"].Value != null)
                                {
                                    reg.MailAddress = DirEntry.Properties["mail"].Value.ToString();
                                }
                            }

                            if (!string.IsNullOrEmpty(Parameters.PhoneAttribute))
                            {
                                if (DirEntry.Properties[Parameters.PhoneAttribute].Value != null)
                                {
                                    reg.PhoneNumber = DirEntry.Properties[Parameters.PhoneAttribute].Value.ToString();
                                }
                            }
                            else
                            {
                                if (DirEntry.Properties["mobile"].Value != null)
                                {
                                    reg.PhoneNumber = DirEntry.Properties["mobile"].Value.ToString();
                                }
                                else if (DirEntry.Properties["otherMobile"].Value != null)
                                {
                                    reg.PhoneNumber = DirEntry.Properties["otherMobile"].Value.ToString();
                                }
                                else if (DirEntry.Properties["telephoneNumber"].Value != null)
                                {
                                    reg.PhoneNumber = DirEntry.Properties["telephoneNumber"].Value.ToString();
                                }
                            }
                            reg.PreferredMethod = Parameters.Method;
                            reg.OverrideMethod  = string.Empty;
                            if (disableall)
                            {
                                reg.Enabled = false;
                            }
                            else if (DirEntry.Properties["userAccountControl"] != null)
                            {
                                int v = Convert.ToInt32(DirEntry.Properties["userAccountControl"].Value);
                                reg.Enabled = ((v & 2) == 0);
                            }
                            else
                            {
                                reg.Enabled = true;
                            }
                            users.AddOrUpdate(reg);
                        }
                    }
                }
                catch (Exception ex)
                {
                    DataLog.WriteEntry("User ID : " + reg.ID + "    Error : " + ex.Message, System.Diagnostics.EventLogEntryType.Error, 20104);
                }
            }
        }