public static string SetOneTimePassword(int userId)
        {
            int passwordLength = 12; // default length

            // load password policy
            UserSettings userSettings   = UserController.GetUserSettings(userId, UserSettings.WEBSITEPANEL_POLICY);
            string       passwordPolicy = userSettings["PasswordPolicy"];

            if (!String.IsNullOrEmpty(passwordPolicy))
            {
                // get third parameter - max length
                try
                {
                    passwordLength = Utils.ParseInt(passwordPolicy.Split(';')[2].Trim(), passwordLength);
                }
                catch { /* skip */ }
            }

            // generate password
            var password = Utils.GetRandomString(passwordLength);

            DataProvider.SetUserOneTimePassword(userId, CryptoUtils.Encrypt(password), (int)OneTimePasswordStates.Active);

            return(password);
        }
        public static int ChangeUserPassword(int userId, string password)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // get user details
            UserInfo user = GetUserInternally(userId);

            // place log record
            TaskManager.StartTask("USER", "CHANGE_PASSWORD", user.Username);
            TaskManager.ItemId = user.UserId;

            try
            {
                DataProvider.ChangeUserPassword(SecurityContext.User.UserId, userId,
                                                CryptoUtils.Encrypt(password));

                return(0);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int ChangeUserPassword(string username, string oldPassword, string newPassword, string ip)
        {
            // place log record
            TaskManager.StartTask("USER", "CHANGE_PASSWORD_BY_USERNAME_PASSWORD", username);
            TaskManager.WriteParameter("IP", ip);

            try
            {
                UserInfo user = GetUserByUsernamePassword(username, oldPassword, ip);
                if (user == null)
                {
                    TaskManager.WriteWarning("Account not found");
                    return(BusinessErrorCodes.ERROR_USER_NOT_FOUND);
                }

                // change password
                DataProvider.ChangeUserPassword(-1, user.UserId,
                                                CryptoUtils.Encrypt(newPassword));

                return(0);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemple #4
0
        public int RestoreItem(string tempFolder, System.Xml.XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(FtpAccount))
            {
                FTPServer ftp = GetFTPServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(FtpAccount));
                FtpAccount    account    = (FtpAccount)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("FtpAccount")));

                // create DSN if required
                if (!ftp.AccountExists(itemName))
                {
                    account.Password = CryptoUtils.Decrypt(account.Password);
                    ftp.CreateAccount(account);

                    // restore password
                    account.Password = CryptoUtils.Encrypt(account.Password);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(FtpAccount)) == null)
                {
                    account.PackageId = packageId;
                    account.ServiceId = serviceId;
                    PackageController.AddPackageItem(account);
                }
            }

            return(0);
        }
        public static int UpdateSqlUser(SqlUser item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // load original meta item
            SqlUser origItem = (SqlUser)PackageController.GetPackageItem(item.Id);

            if (origItem == null)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_USERS_PACKAGE_ITEM_NOT_FOUND);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(origItem.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // place log record
            TaskManager.StartTask("SQL_USER", "UPDATE", origItem.Name);
            TaskManager.ItemId = item.Id;

            try
            {
                // get service
                DatabaseServer sql = GetDatabaseServer(origItem.ServiceId);

                // update service item
                sql.UpdateUser(item, GetSqlDatabasesArray(origItem.PackageId, origItem.GroupName));

                // update meta item
                if (item.Password == "")
                {
                    item.Password = CryptoUtils.Decrypt(origItem.Password);
                }

                item.Password = CryptoUtils.Encrypt(item.Password);
                PackageController.UpdatePackageItem(item);
                return(0);
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);

                // Return a generic error instead of re-throwing an exception
                return(BusinessErrorCodes.FAILED_EXECUTE_SERVICE_OPERATION);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemple #6
0
        public int RestoreItem(string tempFolder, System.Xml.XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(SystemUser))
            {
                SharePointServer sps = GetSharePoint(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SystemUser));
                SystemUser    user       = (SystemUser)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SystemUser")));

                // create user if required
                if (!sps.UserExists(itemName))
                {
                    user.Password = CryptoUtils.Decrypt(user.Password);
                    sps.CreateUser(user);

                    // restore password
                    user.Password = CryptoUtils.Encrypt(user.Password);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(SystemUser)) == null)
                {
                    user.PackageId = packageId;
                    user.ServiceId = serviceId;
                    PackageController.AddPackageItem(user);
                }
            }
            else if (itemType == typeof(SystemGroup))
            {
                SharePointServer sps = GetSharePoint(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SystemGroup));
                SystemGroup   sysGroup   = (SystemGroup)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SystemGroup")));

                // create user if required
                if (!sps.GroupExists(itemName))
                {
                    sps.CreateGroup(sysGroup);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(SystemGroup)) == null)
                {
                    sysGroup.PackageId = packageId;
                    sysGroup.ServiceId = serviceId;
                    PackageController.AddPackageItem(sysGroup);
                }
            }

            return(0);
        }
        public static int SetSystemSettings(string settingsName, SystemSettings settings)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsAdmin
                                                            | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            XmlDocument xmldoc = new XmlDocument();
            XmlElement  root   = xmldoc.CreateElement("properties");

            foreach (string[] pair in settings.SettingsArray)
            {
                string name = pair[0];
                string val  = pair[1];

                if (name.ToLower().IndexOf("password") != -1)
                {
                    val = CryptoUtils.Encrypt(val);
                }

                XmlElement property = xmldoc.CreateElement("property");

                property.SetAttribute("name", name);
                property.SetAttribute("value", val);

                root.AppendChild(property);
            }

            DataProvider.SetSystemSettings(settingsName, root.OuterXml);

            return(0);
        }
Exemple #8
0
        public static int AddOdbcSource(SystemDSN item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // check quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.OS_ODBC);

            if (quota.QuotaExhausted)
            {
                return(BusinessErrorCodes.ERROR_OS_DSN_RESOURCE_QUOTA_LIMIT);
            }

            // check if mail resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Os);

            if (serviceId == 0)
            {
                return(BusinessErrorCodes.ERROR_OS_RESOURCE_UNAVAILABLE);
            }

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(SystemDSN)) != null)
            {
                return(BusinessErrorCodes.ERROR_OS_DSN_PACKAGE_ITEM_EXISTS);
            }

            // place log record
            TaskManager.StartTask("ODBC_DSN", "ADD", item.Name);

            try
            {
                // check service items
                OS.OperatingSystem os = GetOS(serviceId);
                if (os.GetDSN(item.Name) != null)
                {
                    return(BusinessErrorCodes.ERROR_OS_DSN_SERVICE_ITEM_EXISTS);
                }

                string[] dbNameParts = item.DatabaseName.Split('|');
                string   groupName   = null;
                if (dbNameParts.Length > 1)
                {
                    item.DatabaseName = dbNameParts[0];
                    groupName         = dbNameParts[1];
                }

                // get database server address
                item.DatabaseServer = GetDatabaseServerName(groupName, item.PackageId);

                if (item.Driver == "MsAccess" || item.Driver == "Excel" || item.Driver == "Text")
                {
                    item.DatabaseName = FilesController.GetFullPackagePath(item.PackageId, item.DatabaseName);
                }

                // add service item
                os.CreateDSN(item);

                // save item
                item.DatabasePassword = CryptoUtils.Encrypt(item.DatabasePassword);
                item.ServiceId        = serviceId;
                int itemId = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;

                return(itemId);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int AddUser(UserInfo user, bool sendLetter)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            string pattern = @"[\\/:*?<>|""]+";

            if (String.IsNullOrEmpty(user.Username))
            {
                return(BusinessErrorCodes.ERROR_INVALID_USER_NAME);
            }

            if (Regex.IsMatch(user.Username, pattern))
            {
                return(BusinessErrorCodes.ERROR_INVALID_USER_NAME);
            }

            if (UserExists(user.Username))
            {
                return(BusinessErrorCodes.ERROR_USER_ALREADY_EXISTS);
            }

            // only administrators can set admin role
            if (!SecurityContext.User.IsInRole(SecurityContext.ROLE_ADMINISTRATOR) &&
                user.Role == UserRole.Administrator)
            {
                user.Role = UserRole.User;
            }

            // check integrity when creating a user account
            if (user.Role == UserRole.User)
            {
                user.EcommerceEnabled = false;
            }

            // place log record
            TaskManager.StartTask("USER", "ADD", user.Username);

            try
            {
                // add user to database
                int userId = DataProvider.AddUser(
                    SecurityContext.User.UserId,
                    user.OwnerId,
                    user.RoleId,
                    user.StatusId,
                    user.IsDemo,
                    user.IsPeer,
                    user.Comments,
                    user.Username.Trim(),
                    CryptoUtils.Encrypt(user.Password),
                    user.FirstName,
                    user.LastName,
                    user.Email,
                    user.SecondaryEmail,
                    user.Address,
                    user.City,
                    user.Country,
                    user.State,
                    user.Zip,
                    user.PrimaryPhone,
                    user.SecondaryPhone,
                    user.Fax,
                    user.InstantMessenger,
                    user.HtmlMail,
                    user.CompanyName,
                    user.EcommerceEnabled);

                if (userId == -1)
                {
                    TaskManager.WriteWarning("Account with such username already exists");
                    return(BusinessErrorCodes.ERROR_USER_ALREADY_EXISTS);
                }

                TaskManager.ItemId = userId;
                TaskManager.TaskParameters["SendLetter"] = sendLetter;

                return(userId);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemple #10
0
        public static int AddSharePointUser(SystemUser item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // check quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.SHAREPOINT_USERS);

            if (quota.QuotaExhausted)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_USERS_RESOURCE_QUOTA_LIMIT);
            }

            // check if mail resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.SharePoint);

            if (serviceId == 0)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_UNAVAILABLE);
            }

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, ResourceGroups.SharePoint, item.Name, typeof(SystemUser)) != null)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_USERS_PACKAGE_ITEM_EXISTS);
            }

            // place log record
            TaskManager.StartTask("SHAREPOINT", "ADD_USER", item.Name);

            try
            {
                // check service items
                SharePointServer sps = GetSharePoint(serviceId);
                if (sps.UserExists(item.Name))
                {
                    return(BusinessErrorCodes.ERROR_SHAREPOINT_USERS_SERVICE_ITEM_EXISTS);
                }

                // create service item
                item.FullName             = item.Name;
                item.Description          = "WebsitePanel System Account";
                item.AccountDisabled      = false;
                item.PasswordCantChange   = true;
                item.PasswordNeverExpires = true;

                // add service item
                sps.CreateUser(item);

                // save item
                item.Password  = CryptoUtils.Encrypt(item.Password);
                item.ServiceId = serviceId;
                int itemId = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;

                return(itemId);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemple #11
0
        public static int UpdateFtpAccount(FtpAccount item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // load original meta item
            FtpAccount origItem = (FtpAccount)PackageController.GetPackageItem(item.Id);

            if (origItem == null)
            {
                return(BusinessErrorCodes.ERROR_FTP_PACKAGE_ITEM_NOT_FOUND);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(origItem.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // place log record
            TaskManager.StartTask("FTP_ACCOUNT", "UPDATE", origItem.Name, item.Id);

            TaskManager.WriteParameter("Folder", item.Folder);
            TaskManager.WriteParameter("CanRead", item.CanRead);
            TaskManager.WriteParameter("CanWrite", item.CanWrite);

            try
            {
                // get service
                FTPServer ftp = new FTPServer();
                ServiceProviderProxy.Init(ftp, origItem.ServiceId);

                // store original folder
                string origFolder = item.Folder;

                // convert folder
                StringDictionary ftpSettings = ServerController.GetServiceSettings(origItem.ServiceId);
                if (Utils.ParseBool(ftpSettings["BuildUncFilesPath"], false))
                {
                    // UNC
                    // get OS service
                    int osServiceId = PackageController.GetPackageServiceId(origItem.PackageId, ResourceGroups.Os);
                    item.Folder = FilesController.GetFullUncPackagePath(origItem.PackageId, osServiceId, item.Folder);
                }
                else
                {
                    // Absolute
                    item.Folder = FilesController.GetFullPackagePath(origItem.PackageId, item.Folder);
                }

                item.Enabled = true;

                // add service item
                ftp.UpdateAccount(item);

                // save item
                item.Password = String.IsNullOrEmpty(item.Password) ? origItem.Password : CryptoUtils.Encrypt(item.Password);
                item.Folder   = (origFolder == "") ? "\\" : origFolder;
                PackageController.UpdatePackageItem(item);

                return(0);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemple #12
0
        public static int AddFtpAccount(FtpAccount item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // check quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.FTP_ACCOUNTS);

            if (quota.QuotaExhausted)
            {
                return(BusinessErrorCodes.ERROR_FTP_RESOURCE_QUOTA_LIMIT);
            }

            // check if FTP account already exists
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Ftp);

            if (serviceId == 0)
            {
                return(BusinessErrorCodes.ERROR_FTP_RESOURCE_UNAVAILABLE);
            }

            // place log record
            TaskManager.StartTask("FTP_ACCOUNT", "ADD", item.Name);
            TaskManager.WriteParameter("Folder", item.Folder);
            TaskManager.WriteParameter("CanRead", item.CanRead);
            TaskManager.WriteParameter("CanWrite", item.CanWrite);


            try
            {
                // check package items
                if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(FtpAccount)) != null)
                {
                    return(BusinessErrorCodes.ERROR_FTP_PACKAGE_ITEM_EXISTS);
                }

                // check service items
                FTPServer ftp = new FTPServer();
                ServiceProviderProxy.Init(ftp, serviceId);
                if (ftp.AccountExists(item.Name))
                {
                    return(BusinessErrorCodes.ERROR_FTP_SERVICE_ITEM_EXISTS);
                }

                // store original path
                string origFolder = item.Folder;

                // convert folder
                StringDictionary ftpSettings = ServerController.GetServiceSettings(serviceId);
                if (Utils.ParseBool(ftpSettings["BuildUncFilesPath"], false))
                {
                    // UNC
                    // get OS service
                    int osServiceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.Os);
                    item.Folder = FilesController.GetFullUncPackagePath(item.PackageId, osServiceId, item.Folder);
                }
                else
                {
                    // Absolute
                    item.Folder = FilesController.GetFullPackagePath(item.PackageId, item.Folder);
                }

                item.Enabled = true;

                // add service item
                ftp.CreateAccount(item);

                // save item
                item.Password  = CryptoUtils.Encrypt(item.Password);
                item.ServiceId = serviceId;
                item.Folder    = (origFolder == "") ? "\\" : origFolder;
                int itemId = PackageController.AddPackageItem(item);
                TaskManager.ItemId = itemId;
                return(itemId);
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
                throw;
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int AddSqlUser(SqlUser item, string groupName)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // check quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, groupName + ".Users");

            if (quota.QuotaExhausted)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_USERS_RESOURCE_QUOTA_LIMIT);
            }

            // check if mail resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, groupName);

            if (serviceId == 0)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_RESOURCE_UNAVAILABLE);
            }

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, groupName, item.Name, typeof(SqlUser)) != null)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_USERS_PACKAGE_ITEM_EXISTS);
            }

            // place log record
            TaskManager.StartTask("SQL_USER", "ADD", item.Name);
            TaskManager.WriteParameter("Provider", groupName);

            int itemId = default(int);

            try
            {
                // check service items
                DatabaseServer sql = GetDatabaseServer(serviceId);
                if (sql.UserExists(item.Name))
                {
                    return(BusinessErrorCodes.ERROR_MSSQL_USERS_SERVICE_ITEM_EXISTS);
                }

                // add service item
                sql.CreateUser(item, item.Password);

                // save item
                item.Password  = CryptoUtils.Encrypt(item.Password);
                item.ServiceId = serviceId;
                itemId         = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
                //
                if (ex.Message.Contains("INVALID_USERNAME"))
                {
                    return(BusinessErrorCodes.ERROR_MYSQL_INVALID_USER_NAME);
                }
                // Return a generic error instead of default(int)
                itemId = BusinessErrorCodes.FAILED_EXECUTE_SERVICE_OPERATION;
            }
            finally
            {
                TaskManager.CompleteTask();
            }
            return(itemId);
        }
Exemple #14
0
        public static int UpdateSharePointUser(SystemUser item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // load original meta item
            SystemUser origItem = (SystemUser)PackageController.GetPackageItem(item.Id);

            if (origItem == null)
            {
                return(BusinessErrorCodes.ERROR_SHAREPOINT_USERS_PACKAGE_ITEM_NOT_FOUND);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(origItem.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // place log record
            TaskManager.StartTask("SHAREPOINT", "UPDATE_USER", origItem.Name);
            TaskManager.ItemId = item.Id;

            try
            {
                // get service
                SharePointServer sps = GetSharePoint(origItem.ServiceId);

                item.Name                 = origItem.Name;
                item.FullName             = origItem.Name;
                item.Description          = "WebsitePanel System Account";
                item.AccountDisabled      = false;
                item.PasswordCantChange   = true;
                item.PasswordNeverExpires = true;

                // update service item
                sps.UpdateUser(item);

                // update meta item
                if (item.Password != "")
                {
                    item.Password = CryptoUtils.Encrypt(item.Password);
                    PackageController.UpdatePackageItem(item);
                }

                return(0);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemple #15
0
        public static int UpdateOdbcSource(SystemDSN item)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // load original meta item
            SystemDSN origItem = (SystemDSN)PackageController.GetPackageItem(item.Id);

            if (origItem == null)
            {
                return(BusinessErrorCodes.ERROR_OS_DSN_PACKAGE_ITEM_NOT_FOUND);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(origItem.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // place log record
            TaskManager.StartTask("ODBC_DSN", "UPDATE", origItem.Name);
            TaskManager.ItemId = item.Id;

            try
            {
                // get service
                OS.OperatingSystem os = GetOS(origItem.ServiceId);

                // password
                item.Driver = origItem.Driver;
                item.Name   = origItem.Name;

                if (item.DatabasePassword == "")
                {
                    item.DatabasePassword = CryptoUtils.Decrypt(origItem.DatabasePassword);
                }

                string[] dbNameParts = item.DatabaseName.Split('|');
                string   groupName   = null;
                if (dbNameParts.Length > 1)
                {
                    item.DatabaseName = dbNameParts[0];
                    groupName         = dbNameParts[1];
                }

                // get database server address
                item.DatabaseServer = GetDatabaseServerName(groupName, item.PackageId);

                if (item.Driver == "MsAccess" || item.Driver == "Excel" || item.Driver == "Text")
                {
                    item.DatabaseName = FilesController.GetFullPackagePath(origItem.PackageId, item.DatabaseName);
                }

                // update service item
                os.UpdateDSN(item);

                // update meta item
                if (item.DatabasePassword != "")
                {
                    item.DatabasePassword = CryptoUtils.Encrypt(item.DatabasePassword);
                    PackageController.UpdatePackageItem(item);
                }

                return(0);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemple #16
0
        public int RestoreItem(string tempFolder, System.Xml.XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(HomeFolder))
            {
                OS.OperatingSystem os = GetOS(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(HomeFolder));
                HomeFolder    homeFolder = (HomeFolder)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("HomeFolder")));

                // create home folder if required
                if (!os.DirectoryExists(homeFolder.Name))
                {
                    os.CreatePackageFolder(homeFolder.Name);
                }

                // copy database backup to remote server
                XmlNode fileNode            = itemNode.SelectSingleNode("File[@name='SpaceFiles']");
                string  backupFileName      = fileNode.Attributes["path"].Value;
                long    backupFileLength    = Int64.Parse(fileNode.Attributes["size"].Value);
                string  localBackupFilePath = Path.Combine(tempFolder, backupFileName);

                if (new FileInfo(localBackupFilePath).Length != backupFileLength)
                {
                    return(-3);
                }

                FileStream stream = new FileStream(localBackupFilePath, FileMode.Open, FileAccess.Read);
                byte[]     buffer = new byte[FILE_BUFFER_LENGTH];

                int    readBytes        = 0;
                long   length           = 0;
                string remoteBackupPath = Path.Combine(homeFolder.Name, backupFileName);
                do
                {
                    // read package file
                    readBytes = stream.Read(buffer, 0, FILE_BUFFER_LENGTH);
                    length   += readBytes;

                    if (readBytes < FILE_BUFFER_LENGTH)
                    {
                        // resize buffer
                        Array.Resize <byte>(ref buffer, readBytes);
                    }

                    // write remote backup file
                    os.AppendFileBinaryContent(remoteBackupPath, buffer);
                }while (readBytes == FILE_BUFFER_LENGTH);
                stream.Close();

                // unzip files
                os.UnzipFiles(remoteBackupPath, homeFolder.Name);

                // delete archive
                if (os.FileExists(remoteBackupPath))
                {
                    os.DeleteFile(remoteBackupPath);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(HomeFolder)) == null)
                {
                    homeFolder.PackageId = packageId;
                    homeFolder.ServiceId = serviceId;
                    PackageController.AddPackageItem(homeFolder);
                }
            }
            else if (itemType == typeof(SystemDSN))
            {
                OS.OperatingSystem os = GetOS(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SystemDSN));
                SystemDSN     dsn        = (SystemDSN)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SystemDSN")));

                // create DSN if required
                if (os.GetDSN(itemName) == null)
                {
                    dsn.DatabasePassword = CryptoUtils.Decrypt(dsn.DatabasePassword);
                    os.CreateDSN(dsn);

                    // restore password
                    dsn.DatabasePassword = CryptoUtils.Encrypt(dsn.DatabasePassword);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(SystemDSN)) == null)
                {
                    dsn.PackageId = packageId;
                    dsn.ServiceId = serviceId;
                    PackageController.AddPackageItem(dsn);
                }
            }

            return(0);
        }
 public static void FireSuccessAuth(UserInfoInternal user)
 {
     DataProvider.SetUserOneTimePassword(user.UserId, CryptoUtils.Encrypt(user.Password), (int)OneTimePasswordStates.Expired);
 }