mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2025-11-06 05:15:49 +01:00
Changes to backup engine and security improvments.
This commit is contained in:
@@ -49,46 +49,49 @@ class mysqlUtilities:
|
||||
data = f.read()
|
||||
password = data.split('\n', 1)[0]
|
||||
|
||||
expectation = "Enter password:"
|
||||
securemysql = pexpect.spawn("mysql -u root -p")
|
||||
securemysql.expect(expectation)
|
||||
securemysql.sendline(password)
|
||||
createDB = "CREATE DATABASE "+dbname
|
||||
|
||||
expectation = ["Access denied for user", "Welcome to the MariaDB monitor"]
|
||||
command = 'sudo mysql -u root -p' + password + ' -e "' + createDB + '"'
|
||||
cmd = shlex.split(command)
|
||||
res = subprocess.call(cmd)
|
||||
|
||||
index = securemysql.expect(expectation)
|
||||
if res == 1:
|
||||
logging.CyberCPLogFileWriter.writeToFile("Can not create Database: " +dbname)
|
||||
return 0
|
||||
|
||||
if index == 0:
|
||||
return "Wrong root Password"
|
||||
createUser = "CREATE USER '" +dbuser+ "'@'localhost' IDENTIFIED BY '"+dbpassword+"'"
|
||||
|
||||
command = 'sudo mysql -u root -p' + password + ' -e "' + createUser + '"'
|
||||
|
||||
cmd = shlex.split(command)
|
||||
res = subprocess.call(cmd)
|
||||
|
||||
if res == 1:
|
||||
logging.CyberCPLogFileWriter.writeToFile("Can not create Database User: " + dbuser)
|
||||
|
||||
## reverting the db creation which was created earlier
|
||||
|
||||
mysqlUtilities.deleteDatabase(dbname,dbuser)
|
||||
|
||||
return 0
|
||||
else:
|
||||
securemysql.sendline("CREATE DATABASE "+dbname+";")
|
||||
|
||||
dropDB = "GRANT ALL PRIVILEGES ON " +dbname+ ".* TO '" +dbuser+ "'@'localhost'"
|
||||
command = 'sudo mysql -u root -p' + password + ' -e "' + dropDB + '"'
|
||||
cmd = shlex.split(command)
|
||||
res = subprocess.call(cmd)
|
||||
|
||||
expectation = ["database exists","Query OK"]
|
||||
index = securemysql.expect(expectation)
|
||||
if res == 1:
|
||||
mysqlUtilities.deleteDatabase(dbname, dbuser)
|
||||
logging.CyberCPLogFileWriter.writeToFile("Can not grant privileges to user: " + dbuser)
|
||||
return 0
|
||||
|
||||
|
||||
if index == 0:
|
||||
return "This database already exists, please choose another name."
|
||||
elif index == 1:
|
||||
securemysql.sendline("CREATE USER '" +dbuser+ "'@'localhost' IDENTIFIED BY '"+dbpassword+"';")
|
||||
expectation = ["CREATE USER failed","Query OK"]
|
||||
|
||||
index = securemysql.expect(expectation)
|
||||
|
||||
if index == 0:
|
||||
securemysql.sendline("DROP DATABASE IF EXISTS "+dbname+";")
|
||||
return "This user already exists, please choose another user."
|
||||
else:
|
||||
securemysql.sendline("GRANT ALL PRIVILEGES ON " +dbname+ ".* TO '" +dbuser+ "'@'localhost';")
|
||||
expectation = "Query OK"
|
||||
securemysql.expect(expectation)
|
||||
securemysql.sendline("exit")
|
||||
securemysql.wait()
|
||||
return 1
|
||||
except pexpect.EOF, msg:
|
||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " Exception EOF [createDatabase]")
|
||||
|
||||
except BaseException, msg:
|
||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabase]")
|
||||
return 0
|
||||
|
||||
@staticmethod
|
||||
def deleteDatabase(dbname, dbuser):
|
||||
@@ -101,40 +104,29 @@ class mysqlUtilities:
|
||||
data = f.read()
|
||||
password = data.split('\n', 1)[0]
|
||||
|
||||
dropDB = "DROP DATABASE " + dbname
|
||||
command = 'sudo mysql -u root -p' + password + ' -e "' + dropDB + '"'
|
||||
cmd = shlex.split(command)
|
||||
res = subprocess.call(cmd)
|
||||
|
||||
expectation = "Enter password:"
|
||||
securemysql = pexpect.spawn("mysql -u root -p")
|
||||
securemysql.expect(expectation)
|
||||
securemysql.sendline(password)
|
||||
|
||||
expectation = ["Access denied for user", "Welcome to the MariaDB monitor"]
|
||||
|
||||
index = securemysql.expect(expectation)
|
||||
|
||||
if index == 0:
|
||||
return "Wrong root Password"
|
||||
if res == 1:
|
||||
logging.CyberCPLogFileWriter.writeToFile("Can not delete Database: " + dbname)
|
||||
return 0
|
||||
else:
|
||||
securemysql.sendline("DROP DATABASE IF EXISTS " + dbname + ";")
|
||||
dropUser = "DROP USER '"+dbuser+"'@'localhost'"
|
||||
command = 'sudo mysql -u root -p' + password + ' -e "' + dropUser + '"'
|
||||
cmd = shlex.split(command)
|
||||
res = subprocess.call(cmd)
|
||||
|
||||
expectation = ["Query OK",pexpect.EOF]
|
||||
index = securemysql.expect(expectation)
|
||||
if res == 1:
|
||||
logging.CyberCPLogFileWriter.writeToFile("Can not delete Database User: " + dbuser)
|
||||
return 0
|
||||
|
||||
if index == 0:
|
||||
return 1
|
||||
|
||||
securemysql.sendline("DROP USER '"+dbuser+"'@'localhost';")
|
||||
|
||||
securemysql.close()
|
||||
return 1
|
||||
else:
|
||||
securemysql.sendline("DROP USER '" + dbuser + "'@'localhost';")
|
||||
securemysql.close()
|
||||
return 1
|
||||
|
||||
except pexpect.EOF, msg:
|
||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " Exception EOF [deleteDatabase]")
|
||||
return str(msg)
|
||||
except BaseException, msg:
|
||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[createDatabase]")
|
||||
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[deleteDatabase]")
|
||||
return str(msg)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user