Example #1
0
        public static bool CreateDatabaseFromRoot(string mysqlHostName, string rootPassword, string serverName,
                                                  string ipAddress, string random)
        {
            if (!(VerifyHostName(serverName) && VerifyHostAddress(ipAddress)))
            {
                if (!Debugger.IsAttached)
                {
                    return(true); // Probable hack attempt - fail silently
                }
            }

            try
            {
                random = random.Trim();

                if (random.Length > 5)               // if UI-enforced maxlength beaten somehow, limit here
                {
                    random = random.Substring(0, 5); // MySQL will hit a maxlength otherwise
                }

                if (string.IsNullOrEmpty(random))
                {
                    random = Authentication.CreateWeakSecret(5);
                }

                SwarmDb.Credentials rootCredentials = new SwarmDb.Credentials("mysql",
                                                                              new SwarmDb.ServerSet(mysqlHostName), "root", rootPassword);

                string readPass  = GenerateLongPassword();
                string writePass = GenerateLongPassword();
                string adminPass = GenerateLongPassword();

                string[] initInstructions =
                    DbCreateScript.Replace("[random]", random)
                    .Replace("[readpass]", readPass)
                    .Replace("[writepass]", writePass)
                    .Replace("[adminpass]", adminPass).Split('#');

                SwarmDb.GetTestDatabase(rootCredentials).ExecuteAdminCommands(initInstructions);

                PermissionsAnalysis permissionsResult = FirstCredentialsTest(
                    "Swarmops-" + random, mysqlHostName, "Swarmops-R-" + random, readPass,
                    "Swarmops-" + random, mysqlHostName, "Swarmops-W-" + random, writePass,
                    "Swarmops-" + random, mysqlHostName, "Swarmops-A-" + random, adminPass,
                    serverName, ipAddress);

                if (!permissionsResult.AllPermissionsOk)
                {
                    throw new InvalidOperationException("waaaaaah");
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #2
0
        public static AjaxCallResult CreateDatabaseFromRoot(string mysqlHostName, string rootPassword, string serverName,
                                                            string ipAddress, string random)
        {
            if (!(VerifyHostName(serverName) && VerifyHostAddress(ipAddress)))
            {
                if (!Debugger.IsAttached)
                {
                    return(new AjaxCallResult {
                        Success = true
                    });                                         // Probable hack attempt - fail silently
                }
            }

            try
            {
                random = random.Trim();

                if (random.Length > 5)               // if UI-enforced maxlength beaten somehow, limit here
                {
                    random = random.Substring(0, 5); // MySQL will hit a maxlength otherwise
                }

                if (string.IsNullOrEmpty(random))
                {
                    random = Authentication.CreateWeakSecret(5);
                }

                SwarmDb.Credentials rootCredentials = new SwarmDb.Credentials("mysql",
                                                                              new SwarmDb.ServerSet(mysqlHostName), "root", rootPassword);

                string readPass  = GenerateLongPassword();
                string writePass = GenerateLongPassword();
                string adminPass = GenerateLongPassword();

                string[] initInstructions =
                    DbCreateScript.Replace("[random]", random)
                    .Replace("[readpass]", readPass)
                    .Replace("[writepass]", writePass)
                    .Replace("[adminpass]", adminPass).Split('#');

                try
                {
                    SwarmDb.GetTestDatabase(rootCredentials).ExecuteAdminCommands(initInstructions);
                }
                catch (DatabaseExecuteException sqlException)
                {
                    return(new AjaxCallResult
                    {
                        Success = false,
                        DisplayMessage = sqlException.AttemptedCommand
                    });
                }

                PermissionsAnalysis permissionsResult = FirstCredentialsTest(
                    "Swarmops-" + random, mysqlHostName, "Swarmops-R-" + random, readPass,
                    "Swarmops-" + random, mysqlHostName, "Swarmops-W-" + random, writePass,
                    "Swarmops-" + random, mysqlHostName, "Swarmops-A-" + random, adminPass,
                    serverName, ipAddress);

                if (!permissionsResult.AllPermissionsOk)
                {
                    // TODO: Return a better exccption detailing exactly what permission isn't set as required

                    return(new AjaxCallResult {
                        Success = false
                    });
                }

                return(new AjaxCallResult {
                    Success = true
                });
            }
            catch (Exception)
            {
                return(new AjaxCallResult {
                    Success = false
                });
            }
        }