示例#1
0
        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)
                    {
                        //
                        var indexOfSplit = line.IndexOf('=');
                        //
                        if (indexOfSplit == -1)
                        {
                            continue;
                        }
                        //
                        sb.Append("<input type=\"hidden\" name=\"").Append(line.Substring(0, indexOfSplit))
                        .Append("\" value=\"").Append(line.Substring(indexOfSplit + 1)).Append("\"></input>");
                    }

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

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

            return(config);
        }
示例#2
0
        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);
        }
示例#3
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);
        }
示例#4
0
        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);
        }
示例#5
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, 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 == "MsAccess2010" || item.Driver == "Excel" || item.Driver == "Excel2010" || 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();
            }
        }
示例#6
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 == "MsAccess2010" || item.Driver == "Excel" || item.Driver == "Excel2010" || 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 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, item.Id);

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

                item.Name                 = origItem.Name;
                item.FullName             = origItem.Name;
                item.Description          = "SolidCP 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();
            }
        }
        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          = "SolidCP 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();
            }
        }
示例#9
0
 public static void FireSuccessAuth(UserInfoInternal user)
 {
     DataProvider.SetUserOneTimePassword(user.UserId, CryptoUtils.Encrypt(user.Password), (int)OneTimePasswordStates.Expired);
 }