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, 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();
            }
        }
Пример #2
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);
        }
Пример #3
0
        public static UserInfo GetUser(string username)
        {
            // try to get user from database
            UserInfo user = ObjectUtils.FillObjectFromDataReader <UserInfo>(
                DataProvider.GetUserByUsername(SecurityContext.User.UserId, username));

            if (user != null)
            {
                user.Password = CryptoUtils.Decrypt(user.Password);
            }
            return(user);
        }
Пример #4
0
        internal static UserInfo GetUserInternally(string username)
        {
            // try to get user from database
            UserInfo user = ObjectUtils.FillObjectFromDataReader <UserInfo>(
                DataProvider.GetUserByUsernameInternally(username));

            if (user != null)
            {
                user.Password = CryptoUtils.Decrypt(user.Password);
            }
            return(user);
        }
Пример #5
0
        private static UserInfoInternal GetUser(IDataReader reader)
        {
            // try to get user from database
            UserInfoInternal user = ObjectUtils.FillObjectFromDataReader <UserInfoInternal>(reader);

            if (user != null)
            {
                user.Password = CryptoUtils.Decrypt(user.Password);
            }

            return(user);
        }
Пример #6
0
        private static FtpAccount ConvertItemToFtpAccount(ServiceProviderItem item)
        {
            FtpAccount account = (FtpAccount)item;

            string homeFolder = FilesController.GetHomeFolder(account.PackageId);

            if (!String.IsNullOrEmpty(account.Folder) && account.Folder.IndexOf(":") != -1)
            {
                account.Folder = account.Folder.Substring(homeFolder.Length);
            }
            account.Folder = (account.Folder == "") ? "\\" : account.Folder;

            // decode password
            account.Password = CryptoUtils.Decrypt(account.Password);

            return(account);
        }
        public static SqlUser GetSqlUser(int itemId)
        {
            // load meta item
            SqlUser item = (SqlUser)PackageController.GetPackageItem(itemId);

            // load service item
            DatabaseServer sql  = GetDatabaseServer(item.ServiceId);
            SqlUser        user = sql.GetUser(item.Name, GetSqlDatabasesArray(item.PackageId, item.GroupName));

            // add common properties
            user.Id        = item.Id;
            user.PackageId = item.PackageId;
            user.ServiceId = item.ServiceId;
            user.Password  = CryptoUtils.Decrypt(item.Password);
            user.GroupName = item.GroupName;

            return(user);
        }
Пример #8
0
        public static FtpAccount GetFtpAccount(int itemId)
        {
            // load meta item
            FtpAccount item = (FtpAccount)PackageController.GetPackageItem(itemId);

            // load service item
            FTPServer ftp = new FTPServer();

            ServiceProviderProxy.Init(ftp, item.ServiceId);
            FtpAccount account = ftp.GetAccount(item.Name);

            // truncate home folder
            account.Folder = FilesController.GetVirtualPackagePath(item.PackageId, account.Folder);

            account.Id        = item.Id;
            account.PackageId = item.PackageId;
            account.ServiceId = item.ServiceId;
            account.Password  = CryptoUtils.Decrypt(item.Password);

            return(account);
        }
Пример #9
0
        internal static SystemSettings GetSystemSettingsInternal(string settingsName, bool decryptPassword)
        {
            // create settings object
            SystemSettings settings = new SystemSettings();

            // get service settings
            IDataReader reader = null;

            try
            {
                // get service settings
                reader = DataProvider.GetSystemSettings(settingsName);

                while (reader.Read())
                {
                    string name = (string)reader["PropertyName"];
                    string val  = (string)reader["PropertyValue"];

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

                    settings[name] = val;
                }
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
            }

            return(settings);
        }
Пример #10
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);
        }
Пример #11
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();
            }
        }
        public static DatabaseBrowserConfiguration GetDatabaseBrowserLogonScript(int packageId,
                                                                                 string groupName, string username)
        {
            int serviceId = PackageController.GetPackageServiceId(packageId, groupName);

            if (serviceId == 0)
            {
                return(null);
            }

            StringDictionary settings = ServerController.GetServiceSettings(serviceId);
            string           url      = settings["BrowseURL"];
            string           method   = settings["BrowseMethod"];
            string           prms     = settings["BrowseParameters"];

            DatabaseBrowserConfiguration config = new DatabaseBrowserConfiguration();

            config.Enabled = !String.IsNullOrEmpty(url);
            config.Method  = method;

            prms = Utils.ReplaceStringVariable(prms, "server", settings["InternalAddress"]);

            // load database user
            SqlUser user = (SqlUser)PackageController.GetPackageItemByName(packageId, groupName, username, typeof(SqlUser));

            if (user != null)
            {
                prms = Utils.ReplaceStringVariable(prms, "user", username);
                prms = Utils.ReplaceStringVariable(prms, "password", CryptoUtils.Decrypt(user.Password));

                string[] lines = Utils.ParseDelimitedString(prms, '\n', '\r');

                StringBuilder sb = new StringBuilder();
                if (String.Compare(method, "get", true) == 0)
                {
                    // GET
                    sb.Append(url).Append("?");
                    foreach (string line in lines)
                    {
                        sb.Append(line).Append("&");
                    }

                    config.GetData = sb.ToString();
                }
                else
                {
                    // POST
                    sb.Append("<html><body>");
                    sb.Append("<form id=\"AspForm\" method=\"POST\" action=\"").Append(url).Append("\">");

                    foreach (string line in lines)
                    {
                        string[] pair = Utils.ParseDelimitedString(line, '=');
                        sb.Append("<input type=\"hidden\" name=\"").Append(pair[0])
                        .Append("\" value=\"").Append(pair[1]).Append("\"></input>");
                    }

                    sb.Append("</form><script language=\"javascript\">document.getElementById(\"AspForm\").submit();</script>");
                    sb.Append("</body></html>");

                    config.PostData = sb.ToString();
                }
            }

            return(config);
        }
        public int RestoreItem(string tempFolder, XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(SqlDatabase))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

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

                // create database if required
                if (!sql.DatabaseExists(itemName))
                {
                    sql.CreateDatabase(db);
                }

                // copy database backup to remote server
                XmlNode fileNode            = itemNode.SelectSingleNode("File[@name='DatabaseBackup']");
                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 = null;
                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
                    string tempPath = sql.AppendTempFileBinaryChunk(backupFileName, remoteBackupPath, buffer);
                    if (remoteBackupPath == null)
                    {
                        remoteBackupPath = tempPath;
                    }
                }while (readBytes == FILE_BUFFER_LENGTH);
                stream.Close();

                // restore database
                sql.RestoreDatabase(itemName, new string[] { remoteBackupPath });

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, group.GroupName,
                                                           itemName, typeof(SqlDatabase)) == null)
                {
                    db.PackageId = packageId;
                    db.ServiceId = serviceId;
                    db.GroupName = group.GroupName;
                    PackageController.AddPackageItem(db);
                }
            }
            else if (itemType == typeof(SqlUser))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

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

                // create user if required
                if (!sql.UserExists(itemName))
                {
                    sql.CreateUser(user, CryptoUtils.Decrypt(user.Password));
                }

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

            return(0);
        }