Skip to content

Commit

Permalink
bug fix: fix misconfigured database users in cPanel importer
Browse files Browse the repository at this point in the history
  • Loading branch information
usmannasir committed Sep 10, 2019
1 parent b7fec58 commit 52a2d54
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
16 changes: 15 additions & 1 deletion plogical/backupUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 50 additions & 2 deletions plogical/mysqlUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.