Esempio n. 1
0
        public static bool TryGetLdapUserInfo(string login, string password, out UserInfo userInfo)
        {
            userInfo = ASC.Core.Users.Constants.LostUser;

            if (!SetupInfo.IsVisibleSettings(ManagementType.LdapSettings.ToString()) ||
                CoreContext.Configuration.Standalone && !CoreContext.TenantManager.GetTenantQuota(TenantProvider.CurrentTenantID).Ldap)
            {
                return(false);
            }

            var settings = SettingsManager.Instance.LoadSettings <LDAPSupportSettings>(TenantProvider.CurrentTenantID);

            if (!settings.EnableLdapAuthentication)
            {
                return(false);
            }
            try
            {
                var importer = new LDAPUserImporter();
                try
                {
                    LdapSettingsChecker ldapSettingsChecker;
                    string currentLogin;
                    if (!WorkContext.IsMono)
                    {
                        ldapSettingsChecker = new SystemLdapSettingsChecker();
                        currentLogin        = login;
                    }
                    else
                    {
                        currentLogin = GetEntryDN(settings, login);
                        if (currentLogin == null)
                        {
                            return(false);
                        }
                        ldapSettingsChecker = new NovellLdapSettingsChecker();
                    }
                    ldapSettingsChecker.CheckCredentials(currentLogin, password, settings.Server, settings.PortNumber, settings.StartTls);
                }
                catch (Exception)
                {
                    return(false);
                }

                if (login.Contains("\\"))
                {
                    login = login.Split('\\')[1];
                }
                var sid = importer.GetSidOfCurrentUser(login, settings);
                if (sid == null)
                {
                    return(false);
                }
                List <GroupInfo> existingGroups;
                importer.GetDiscoveredGroupsByAttributes(settings, out existingGroups);
                if (importer.GetDiscoveredUser(settings, sid).Equals(ASC.Core.Users.Constants.LostUser))
                {
                    return(false);
                }

                userInfo = CoreContext.UserManager.GetUserBySid("l" + sid);
                if (userInfo.Equals(ASC.Core.Users.Constants.LostUser))
                {
                    userInfo = CoreContext.UserManager.GetUserBySid(sid);
                    if (userInfo.Equals(ASC.Core.Users.Constants.LostUser))
                    {
                        userInfo = importer.GetDiscoveredUser(settings, sid);
                        if (userInfo.Equals(ASC.Core.Users.Constants.LostUser))
                        {
                            return(false);
                        }
                        if (userInfo.FirstName == string.Empty)
                        {
                            userInfo.FirstName = Resource.FirstName;
                        }
                        if (userInfo.LastName == string.Empty)
                        {
                            userInfo.LastName = Resource.LastName;
                        }
                        try
                        {
                            SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);

                            var asVisitor = TenantStatisticsProvider.GetUsersCount() >= TenantExtra.GetTenantQuota().ActiveUsers;
                            userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, asVisitor);

                            importer.AddUserIntoGroups(userInfo, settings);
                            importer.AddUserInCacheGroups(userInfo);
                        }
                        finally
                        {
                            SecurityContext.Logout();
                        }
                    }
                }
                else
                {
                    userInfo.Sid = sid;
                    try
                    {
                        SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);

                        var asVisitor = TenantStatisticsProvider.GetUsersCount() >= TenantExtra.GetTenantQuota().ActiveUsers;

                        userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, asVisitor, false, false);
                    }
                    finally
                    {
                        SecurityContext.Logout();
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                log.ErrorFormat("Unexpected error: {0}", e);
                userInfo = ASC.Core.Users.Constants.LostUser;
                return(false);
            }
        }
Esempio n. 2
0
        public void RunJob(DistributedTask _, CancellationToken cancellationToken)
        {
            try
            {
                CancellationToken = cancellationToken;

                CoreContext.TenantManager.SetCurrentTenant(CurrentTenant);

                SecurityContext.AuthenticateMe(Core.Configuration.Constants.CoreSystem);

                Thread.CurrentThread.CurrentCulture   = CultureInfo.GetCultureInfo(_culture);
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(_culture);

                Logger = LogManager.GetLogger("ASC");

                if (LDAPSettings == null)
                {
                    Error = Resource.LdapSettingsErrorCantGetLdapSettings;
                    Logger.Error("Can't save default LDAP settings.");
                    return;
                }

                switch (OperationType)
                {
                case LdapOperationType.Save:
                case LdapOperationType.SaveTest:

                    Logger.InfoFormat("Start '{0}' operation",
                                      Enum.GetName(typeof(LdapOperationType), OperationType));

                    SetProgress(1, Resource.LdapSettingsStatusCheckingLdapSettings);

                    Logger.Debug("PrepareSettings()");

                    PrepareSettings(LDAPSettings);

                    if (!string.IsNullOrEmpty(Error))
                    {
                        Logger.DebugFormat("PrepareSettings() Error: {0}", Error);
                        return;
                    }

                    Importer = new NovellLdapUserImporter(LDAPSettings, Resource);

                    if (LDAPSettings.EnableLdapAuthentication)
                    {
                        var ldapSettingsChecker = new NovellLdapSettingsChecker(Importer);

                        SetProgress(5, Resource.LdapSettingsStatusLoadingBaseInfo);

                        var result = ldapSettingsChecker.CheckSettings();

                        if (result != LdapSettingsStatus.Ok)
                        {
                            if (result == LdapSettingsStatus.CertificateRequest)
                            {
                                TaskInfo.SetProperty(CERT_REQUEST,
                                                     ldapSettingsChecker.CertificateConfirmRequest);
                            }

                            Error = GetError(result);

                            Logger.DebugFormat("ldapSettingsChecker.CheckSettings() Error: {0}", Error);

                            return;
                        }
                    }

                    break;

                case LdapOperationType.Sync:
                case LdapOperationType.SyncTest:
                    Logger.InfoFormat("Start '{0}' operation",
                                      Enum.GetName(typeof(LdapOperationType), OperationType));

                    Importer = new NovellLdapUserImporter(LDAPSettings, Resource);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                Do();
            }
            catch (AuthorizingException authError)
            {
                Error = Resource.ErrorAccessDenied;
                Logger.Error(Error, new SecurityException(Error, authError));
            }
            catch (AggregateException ae)
            {
                ae.Flatten().Handle(e => e is TaskCanceledException || e is OperationCanceledException);
            }
            catch (TenantQuotaException e)
            {
                Error = Resource.LdapSettingsTenantQuotaSettled;
                Logger.ErrorFormat("TenantQuotaException. {0}", e);
            }
            catch (FormatException e)
            {
                Error = Resource.LdapSettingsErrorCantCreateUsers;
                Logger.ErrorFormat("FormatException error. {0}", e);
            }
            catch (Exception e)
            {
                Error = Resource.LdapSettingsInternalServerError;
                Logger.ErrorFormat("Internal server error. {0}", e);
            }
            finally
            {
                try
                {
                    TaskInfo.SetProperty(FINISHED, true);
                    PublishTaskInfo();
                    Dispose();
                    SecurityContext.Logout();
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("LdapOperation finalization problem. {0}", ex);
                }
            }
        }