Esempio n. 1
0
        /// <summary>
        /// DoImport() method implmentation
        /// </summary>
        public override bool DoImport()
        {
            char          sep      = Path.DirectorySeparatorChar;
            string        filename = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + sep + "MFA" + sep + "adimport-" + DateTime.Now.ToFileTime().ToString() + ".log";
            TraceListener listen   = InitializeTrace(filename);

            try
            {
                ADDSHost adht = Config.Hosts.ActiveDirectoryHost;
                if (string.IsNullOrEmpty(DomainName))
                {
                    DomainName = adht.DomainName;
                }
                if (string.IsNullOrEmpty(UserName))
                {
                    UserName = adht.Account;
                }
                if (string.IsNullOrEmpty(Password))
                {
                    Password = adht.Password;
                }

                DataRepositoryService client = null;
                switch (Config.StoreMode)
                {
                case DataRepositoryKind.ADDS:
                    client = new ADDSDataRepositoryService(Config.Hosts.ActiveDirectoryHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.SQL:
                    client = new SQLDataRepositoryService(Config.Hosts.SQLServerHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.Custom:
                    client = CustomDataRepositoryActivator.CreateInstance(Config.Hosts.CustomStoreHost, Config.DeliveryWindow);
                    break;
                }

                Trace.WriteLine("");
                Trace.WriteLine(string.Format("Importing for AD : {0}", LDAPPath));
                Trace.Indent();
                Trace.WriteLine("Querying users from AD");
                MFAUserList lst = client.ImportMFAUsers(DomainName, UserName, Password, LDAPPath, CreatedSince, ModifiedSince, MailAttribute, PhoneAttribute, Method, Config.Hosts.ActiveDirectoryHost.UseSSL, DisableAll);
                Trace.WriteLine(string.Format("Querying return {0} users from AD", lst.Count.ToString()));

                DataRepositoryService client2 = null;
                switch (Config.StoreMode)
                {
                case DataRepositoryKind.ADDS:
                    Trace.WriteLine("");
                    Trace.WriteLine("Importing ADDS Mode");
                    Trace.Indent();
                    client2 = new ADDSDataRepositoryService(Config.Hosts.ActiveDirectoryHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.SQL:
                    Trace.WriteLine("");
                    Trace.WriteLine("Importing SQL Mode");
                    Trace.Indent();
                    client2 = new SQLDataRepositoryService(Config.Hosts.SQLServerHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.Custom:

                    Trace.WriteLine("");
                    Trace.WriteLine("Importing Custom Store Mode");
                    Trace.Indent();
                    client2 = CustomDataRepositoryActivator.CreateInstance(Config.Hosts.CustomStoreHost, Config.DeliveryWindow);
                    break;
                }
                client2.OnKeyDataEvent += KeyDataEvent;
                foreach (MFAUser reg in lst)
                {
                    Trace.TraceInformation(string.Format("Importing user {0} from AD", reg.UPN));
                    try
                    {
                        MFAUser ext = client2.GetMFAUser(reg.UPN);
                        if (ext == null)
                        {
                            reg.PIN = Config.DefaultPin;
                            client2.AddMFAUser(reg, ForceNewKey, false);
                            Trace.TraceInformation(string.Format("User {0} Imported in MFA", reg.UPN));
                            if (!string.IsNullOrEmpty(reg.MailAddress))
                            {
                                if (SendEmail)
                                {
                                    string      qrcode = KeysManager.EncodedKey(reg.UPN);
                                    CultureInfo info   = null;
                                    try
                                    {
                                        info = CultureInfo.CurrentUICulture;
                                    }
                                    catch
                                    {
                                        info = new CultureInfo(Config.DefaultCountryCode);
                                    }
                                    MailUtilities.SendKeyByEmail(reg.MailAddress, reg.UPN, qrcode, Config.MailProvider, Config, info);
                                    Trace.TraceInformation(string.Format("Sending Sensitive mail for User {0} Imported in MFA", reg.UPN));
                                }
                            }
                            RecordsCount++;
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorsCount++;
                        Trace.TraceError("Error importing Record N° {0} \r\r {1}", (RecordsCount + 1).ToString(), ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(string.Format("Error importing from AD \r\r {0}", ex.Message));
                return(false);
            }
            finally
            {
                Trace.Unindent();
                FinalizeTrace(listen);
            }
            return(true);
        }
        /// <summary>
        /// DoImport() method implmentation
        /// </summary>
        public override bool DoImport()
        {
            char          sep      = Path.DirectorySeparatorChar;
            string        filename = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + sep + "MFA" + sep + "adimport-" + DateTime.Now.ToFileTime().ToString() + ".log";
            TraceListener listen   = InitializeTrace(filename);

            try
            {
                ADDSHost adht = Config.Hosts.ActiveDirectoryHost;
                if (string.IsNullOrEmpty(Parameters.DomainName))
                {
                    Parameters.DomainName = adht.DomainName;
                }
                if (string.IsNullOrEmpty(Parameters.UserName))
                {
                    Parameters.UserName = adht.Account;
                }
                if (string.IsNullOrEmpty(Parameters.Password))
                {
                    Parameters.Password = adht.Password;
                }

                DataRepositoryService client = null;
                switch (Config.StoreMode)
                {
                case DataRepositoryKind.ADDS:
                    client = new ADDSDataRepositoryService(Config.Hosts.ActiveDirectoryHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.SQL:
                    client = new SQLDataRepositoryService(Config.Hosts.SQLServerHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.Custom:
                    client = CustomDataRepositoryActivator.CreateInstance(Config.Hosts.CustomStoreHost, Config.DeliveryWindow);
                    break;
                }

                Trace.WriteLine("");
                Trace.WriteLine(string.Format("Importing for AD : {0}", Parameters.LDAPPath));
                Trace.Indent();
                Trace.WriteLine("Query users from AD");
                MFAUserList lst = client.ImportMFAUsers(Parameters, DisableAll);
                Trace.WriteLine(string.Format("Query returns {0} user(s) from AD", lst.Count.ToString()));

                DataRepositoryService client2 = null;
                switch (Config.StoreMode)
                {
                case DataRepositoryKind.ADDS:
                    Trace.WriteLine("");
                    Trace.WriteLine("Importing ADDS Mode");
                    Trace.Indent();
                    client2 = new ADDSDataRepositoryService(Config.Hosts.ActiveDirectoryHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.SQL:
                    Trace.WriteLine("");
                    Trace.WriteLine("Importing SQL Mode");
                    Trace.Indent();
                    client2 = new SQLDataRepositoryService(Config.Hosts.SQLServerHost, Config.DeliveryWindow);
                    break;

                case DataRepositoryKind.Custom:

                    Trace.WriteLine("");
                    Trace.WriteLine("Importing Custom Store Mode");
                    Trace.Indent();
                    client2 = CustomDataRepositoryActivator.CreateInstance(Config.Hosts.CustomStoreHost, Config.DeliveryWindow);
                    break;
                }
                client2.OnKeyDataEvent += KeyDataEvent;
                foreach (MFAUser reg in lst)
                {
                    try
                    {
                        MFAUser ext = client2.GetMFAUser(reg.UPN);
                        if (ext == null)
                        {
                            reg.PIN             = Convert.ToInt32(Config.DefaultPin);
                            reg.PreferredMethod = Config.DefaultProviderMethod;
                        }
                        else
                        {
                            if (this.Parameters.Method != PreferredMethod.None)
                            {
                                reg.PreferredMethod = this.Parameters.Method;
                            }
                            else
                            {
                                reg.PreferredMethod = ext.PreferredMethod;
                            }
                        }
                        if (reg.PIN <= 0)
                        {
                            reg.PIN = Convert.ToInt32(Config.DefaultPin);
                        }

                        if (!Utilities.ValidateEmail(reg.MailAddress, (Config.MailProvider.Enabled && Config.MailProvider.IsRequired)))
                        {
                            throw new Exception(string.Format("invalid mail address for user : {0}", reg.UPN));
                        }
                        if (!Utilities.ValidatePhoneNumber(reg.PhoneNumber, (Config.ExternalProvider.Enabled && Config.ExternalProvider.IsRequired)))
                        {
                            throw new Exception(string.Format("invalid phone number for user : {0}", reg.UPN));
                        }

                        client2.AddMFAUser(reg, ForceNewKey, true);
                        Trace.TraceInformation(string.Format("User {0} Imported in MFA", reg.UPN));
                        if (!string.IsNullOrEmpty(reg.MailAddress))
                        {
                            if (SendEmail)
                            {
                                string      qrcode = KeysManager.EncodedKey(reg.UPN);
                                CultureInfo info   = null;
                                try
                                {
                                    info = CultureInfo.CurrentUICulture;
                                }
                                catch
                                {
                                    info = new CultureInfo(Config.DefaultCountryCode);
                                }
                                MailUtilities.SendKeyByEmail(reg.MailAddress, reg.UPN, qrcode, Config.MailProvider, Config, info);
                                Trace.TraceInformation(string.Format("Sending Sensitive mail for User {0}", reg.UPN));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrorsCount++;
                        Trace.TraceError("Error importing User {0} : {1}", reg.UPN, ex.Message);
                    }
                    finally
                    {
                        RecordsCount++;
                    }
                }
                Trace.Unindent();
                Trace.WriteLine("");
                Trace.WriteLine(string.Format("Imported {0} User(s) from AD Source", RecordsCount));
            }
            catch (Exception ex)
            {
                Trace.Unindent();
                Trace.TraceError(string.Format("Error importing from AD : {0}", ex.Message));
                Log.WriteEntry(string.Format("Error importing from AD : {0}", ex.Message), EventLogEntryType.Error, 20000);
                return(false);
            }
            finally
            {
                Trace.Unindent();
                FinalizeTrace(listen);
            }
            return(true);
        }