Skip to content
Permalink
Browse files

bug fix: fix misconfigured database users in cPanel importer

  • Loading branch information...
usmannasir committed Sep 10, 2019
1 parent b7fec58 commit 52a2d54168aeef6fc37f2a5a0f6f9b8ae86dc1ee
Showing with 65 additions and 3 deletions.
  1. +15 −1 plogical/backupUtilities.py
  2. +50 −2 plogical/mysqlUtilities.py
@@ -104,18 +104,32 @@ def prepareBackupMeta(backupDomain, backupName, tempStoragePath, backupPath):
for items in databases:
try:
dbuser = DBUsers.objects.get(user=items.dbUser)
userToTry = items.dbUser
except:
dbusers = DBUsers.objects.all().filter(user=items.dbUser)
userToTry = items.dbUser
for it in dbusers:
dbuser = it
break

userToTry = mysqlUtilities.mysqlUtilities.fetchuser(items.dbUser)

try:
dbuser = DBUsers.objects.get(user=userToTry)
except:
dbusers = DBUsers.objects.all().filter(user=userToTry)
for it in dbusers:
dbuser = it
break



databaseXML = Element('database')

child = SubElement(databaseXML, 'dbName')
child.text = items.dbName
child = SubElement(databaseXML, 'dbUser')
child.text = items.dbUser
child.text = userToTry
child = SubElement(databaseXML, 'password')
child.text = dbuser.password

@@ -12,6 +12,7 @@
try:
from websiteFunctions.models import Websites
from databases.models import Databases
from backup.models import DBUsers
except:
pass
import MySQLdb as mysql
@@ -704,13 +705,60 @@ def changePassword(userName, dbPassword):

if connection == 0:
return 0

cursor.execute("use mysql")
cursor.execute("SET PASSWORD FOR '" + userName + "'@'localhost' = PASSWORD('" + dbPassword + "')")


try:
dbuser = DBUsers.objects.get(user=userName)
cursor.execute("SET PASSWORD FOR '" + userName + "'@'localhost' = PASSWORD('" + dbPassword + "')")
except:
userName = mysqlUtilities.fetchuser(userName)
cursor.execute("SET PASSWORD FOR '" + userName + "'@'localhost' = PASSWORD('" + dbPassword + "')")

connection.close()

return 1

except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[mysqlUtilities.changePassword]")
return 0

@staticmethod
def fetchuser(userName):
try:
connection, cursor = mysqlUtilities.setupConnection()
cursor.execute("use mysql")

database = Databases.objects.get(dbUser=userName)
databaseName = database.dbName
databaseName = databaseName.replace('_', '\_')
query = "select user from db where db = '%s'" % (databaseName)

if connection == 0:
return 0

cursor.execute(query)
rows = cursor.fetchall()
counter = 0

for row in rows:
if row[0].find('_') > -1:
database.dbUser = row[0]
database.save()
try:
connection.close()
except:
pass
message = 'Detected databaser user is %s for database %s.' % (row[0], databaseName)
logging.CyberCPLogFileWriter.writeToFile(message)
return row[0]
else:
counter = counter + 1

connection.close()

return 1

except BaseException, msg:
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[mysqlUtilities.fetchuser]")
return 0

0 comments on commit 52a2d54

Please sign in to comment.
You can’t perform that action at this time.