Skip to content

Commit 9de6243

Browse files
committed
bug fix: potential issue with sftp destination add #1210;
;
1 parent c7d3004 commit 9de6243

1 file changed

Lines changed: 193 additions & 112 deletions

File tree

‎plogical/backupUtilities.py‎

Lines changed: 193 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import sys
33

4+
import paramiko
5+
46
sys.path.append('/usr/local/CyberCP')
57
import django
68

@@ -9,7 +11,7 @@
911
django.setup()
1012
except:
1113
pass
12-
14+
import pysftp
1315
from plogical.randomPassword import generate_pass
1416
import pexpect
1517
from plogical import CyberCPLogFileWriter as logging
@@ -1147,143 +1149,222 @@ def initiateRestore(backupName, dir):
11471149
except BaseException as msg:
11481150
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [initiateRestore]")
11491151

1152+
# @staticmethod
1153+
# def sendKey(IPAddress, password, port='22', user='root'):
1154+
# try:
1155+
#
1156+
# expectation = []
1157+
# expectation.append("password:")
1158+
# expectation.append("Password:")
1159+
# expectation.append("Permission denied")
1160+
# expectation.append("100%")
1161+
#
1162+
# ## Temp changes
1163+
#
1164+
# command = 'chmod 600 %s' % ('/root/.ssh/cyberpanel.pub')
1165+
# ProcessUtilities.executioner(command)
1166+
#
1167+
# command = "scp -o StrictHostKeyChecking=no -P " + port + " /root/.ssh/cyberpanel.pub " + user + "@" + IPAddress + ":~/.ssh/authorized_keys"
1168+
# setupKeys = pexpect.spawn(command, timeout=3)
1169+
#
1170+
# if os.path.exists(ProcessUtilities.debugPath):
1171+
# logging.CyberCPLogFileWriter.writeToFile(command)
1172+
#
1173+
# index = setupKeys.expect(expectation)
1174+
#
1175+
# ## on first login attempt send password
1176+
#
1177+
# if index == 0:
1178+
# setupKeys.sendline(password)
1179+
# setupKeys.expect("100%")
1180+
# setupKeys.wait()
1181+
# elif index == 1:
1182+
# setupKeys.sendline(password)
1183+
# setupKeys.expect("100%")
1184+
# setupKeys.wait()
1185+
# elif index == 2:
1186+
# return [0, 'Please enable password authentication on your remote server.']
1187+
# elif index == 3:
1188+
# pass
1189+
# else:
1190+
# raise BaseException
1191+
#
1192+
# ## Temp changes
1193+
#
1194+
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1195+
# ProcessUtilities.executioner(command)
1196+
#
1197+
# return [1, "None"]
1198+
#
1199+
# except pexpect.TIMEOUT as msg:
1200+
#
1201+
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1202+
# ProcessUtilities.executioner(command)
1203+
#
1204+
# logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
1205+
# return [0, "TIMEOUT [sendKey]"]
1206+
# except pexpect.EOF as msg:
1207+
#
1208+
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1209+
# ProcessUtilities.executioner(command)
1210+
#
1211+
# logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
1212+
# return [0, "EOF [sendKey]"]
1213+
# except BaseException as msg:
1214+
#
1215+
# command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1216+
# ProcessUtilities.executioner(command)
1217+
#
1218+
# logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
1219+
# return [0, str(msg) + " [sendKey]"]
1220+
1221+
# @staticmethod
1222+
# def setupSSHKeys(IPAddress, password, port='22', user='root'):
1223+
# try:
1224+
# ## Checking for host verification
1225+
#
1226+
# backupUtilities.host_key_verification(IPAddress)
1227+
#
1228+
# if backupUtilities.checkIfHostIsUp(IPAddress) == 1:
1229+
# pass
1230+
# else:
1231+
# logging.CyberCPLogFileWriter.writeToFile("Host is Down.")
1232+
# # return [0,"Host is Down."]
1233+
#
1234+
# expectation = []
1235+
# expectation.append("password:")
1236+
# expectation.append("Password:")
1237+
# expectation.append("Permission denied")
1238+
# expectation.append("File exists")
1239+
#
1240+
# command = "ssh -o StrictHostKeyChecking=no -p " + port + ' ' + user + "@" + IPAddress + ' "mkdir ~/.ssh || rm -f ~/.ssh/temp && rm -f ~/.ssh/authorized_temp && cp ~/.ssh/authorized_keys ~/.ssh/temp || chmod 700 ~/.ssh || chmod g-w ~"'
1241+
# setupKeys = pexpect.spawn(command, timeout=3)
1242+
#
1243+
# if os.path.exists(ProcessUtilities.debugPath):
1244+
# logging.CyberCPLogFileWriter.writeToFile(command)
1245+
#
1246+
# index = setupKeys.expect(expectation)
1247+
#
1248+
# ## on first login attempt send password
1249+
#
1250+
# if index == 0:
1251+
# setupKeys.sendline(password)
1252+
# elif index == 1:
1253+
# setupKeys.sendline(password)
1254+
# elif index == 2:
1255+
# return [0, 'Please enable password authentication on your remote server.']
1256+
# elif index == 3:
1257+
# pass
1258+
# else:
1259+
# raise BaseException
1260+
#
1261+
# ## if it again give you password, than provided password is wrong
1262+
#
1263+
# expectation = []
1264+
# expectation.append("please try again.")
1265+
# expectation.append("Password:")
1266+
# expectation.append(pexpect.EOF)
1267+
#
1268+
# index = setupKeys.expect(expectation)
1269+
#
1270+
# if index == 0:
1271+
# return [0, "Wrong Password!"]
1272+
# elif index == 1:
1273+
# return [0, "Wrong Password!"]
1274+
# elif index == 2:
1275+
# setupKeys.wait()
1276+
#
1277+
# sendKey = backupUtilities.sendKey(IPAddress, password, port, user)
1278+
#
1279+
# if sendKey[0] == 1:
1280+
# return [1, "None"]
1281+
# else:
1282+
# return [0, sendKey[1]]
1283+
#
1284+
#
1285+
# except pexpect.TIMEOUT as msg:
1286+
# return [0, str(msg) + " [TIMEOUT setupSSHKeys]"]
1287+
# except BaseException as msg:
1288+
# return [0, str(msg) + " [setupSSHKeys]"]
1289+
11501290
@staticmethod
11511291
def sendKey(IPAddress, password, port='22', user='root'):
11521292
try:
1293+
ssh = paramiko.SSHClient()
1294+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
1295+
ssh.connect(IPAddress, port=int(port), username=user, password=password)
11531296

1154-
expectation = []
1155-
expectation.append("password:")
1156-
expectation.append("Password:")
1157-
expectation.append("Permission denied")
1158-
expectation.append("100%")
1159-
1160-
## Temp changes
1161-
1162-
command = 'chmod 600 %s' % ('/root/.ssh/cyberpanel.pub')
1163-
ProcessUtilities.executioner(command)
1164-
1165-
command = "scp -o StrictHostKeyChecking=no -P " + port + " /root/.ssh/cyberpanel.pub " + user + "@" + IPAddress + ":~/.ssh/authorized_keys"
1166-
setupKeys = pexpect.spawn(command, timeout=3)
1167-
1168-
if os.path.exists(ProcessUtilities.debugPath):
1169-
logging.CyberCPLogFileWriter.writeToFile(command)
1170-
1171-
index = setupKeys.expect(expectation)
1172-
1173-
## on first login attempt send password
1174-
1175-
if index == 0:
1176-
setupKeys.sendline(password)
1177-
setupKeys.expect("100%")
1178-
setupKeys.wait()
1179-
elif index == 1:
1180-
setupKeys.sendline(password)
1181-
setupKeys.expect("100%")
1182-
setupKeys.wait()
1183-
elif index == 2:
1184-
return [0, 'Please enable password authentication on your remote server.']
1185-
elif index == 3:
1297+
if os.path.exists('/root/.ssh/cyberpanel.pub'):
11861298
pass
11871299
else:
1188-
raise BaseException
1189-
1190-
## Temp changes
1300+
command = "ssh-keygen -f /root/.ssh/cyberpanel -t rsa -N ''"
1301+
ProcessUtilities.executioner(command, 'root', True)
11911302

1192-
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1303+
command = 'chmod 600 %s' % ('/root/.ssh/cyberpanel.pub')
11931304
ProcessUtilities.executioner(command)
11941305

1195-
return [1, "None"]
1196-
1197-
except pexpect.TIMEOUT as msg:
1306+
sftp = ssh.open_sftp()
1307+
sftp.put('/root/.ssh/cyberpanel.pub', '.ssh/authorized_keys')
1308+
sftp.close()
11981309

1199-
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1200-
ProcessUtilities.executioner(command)
1310+
ssh.exec_command('chmod 600 .ssh/authorized_keys')
12011311

1202-
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
1203-
return [0, "TIMEOUT [sendKey]"]
1204-
except pexpect.EOF as msg:
1312+
ssh.close()
12051313

12061314
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
12071315
ProcessUtilities.executioner(command)
12081316

1209-
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
1210-
return [0, "EOF [sendKey]"]
1211-
except BaseException as msg:
1212-
1213-
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1214-
ProcessUtilities.executioner(command)
1317+
return [1, "None"]
12151318

1216-
logging.CyberCPLogFileWriter.writeToFile(str(msg) + " [sendKey]")
1217-
return [0, str(msg) + " [sendKey]"]
1319+
except paramiko.AuthenticationException:
1320+
return [0, 'Authentication failed. [sendKey]']
1321+
except paramiko.SSHException as e:
1322+
return [0, f'SSH error: {str(e)} [sendKey]']
1323+
except Exception as e:
1324+
return [0, f'General Error: {str(e)} [sendKey]']
12181325

12191326
@staticmethod
12201327
def setupSSHKeys(IPAddress, password, port='22', user='root'):
12211328
try:
1222-
## Checking for host verification
1223-
1224-
backupUtilities.host_key_verification(IPAddress)
1225-
1226-
if backupUtilities.checkIfHostIsUp(IPAddress) == 1:
1227-
pass
1228-
else:
1229-
logging.CyberCPLogFileWriter.writeToFile("Host is Down.")
1230-
# return [0,"Host is Down."]
1231-
1232-
expectation = []
1233-
expectation.append("password:")
1234-
expectation.append("Password:")
1235-
expectation.append("Permission denied")
1236-
expectation.append("File exists")
1237-
1238-
command = "ssh -o StrictHostKeyChecking=no -p " + port + ' ' + user + "@" + IPAddress + ' "mkdir ~/.ssh || rm -f ~/.ssh/temp && rm -f ~/.ssh/authorized_temp && cp ~/.ssh/authorized_keys ~/.ssh/temp || chmod 700 ~/.ssh || chmod g-w ~"'
1239-
setupKeys = pexpect.spawn(command, timeout=3)
1240-
1241-
if os.path.exists(ProcessUtilities.debugPath):
1242-
logging.CyberCPLogFileWriter.writeToFile(command)
1329+
ssh = paramiko.SSHClient()
1330+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
1331+
ssh.connect(IPAddress, port=int(port), username=user, password=password)
1332+
1333+
commands = [
1334+
"mkdir -p .ssh",
1335+
"rm -f .ssh/temp",
1336+
"rm -f .ssh/authorized_temp",
1337+
"cp .ssh/authorized_keys .ssh/temp",
1338+
"chmod 700 .ssh",
1339+
"chmod g-w ~",
1340+
]
1341+
1342+
for command in commands:
1343+
try:
1344+
ssh.exec_command(command)
1345+
except BaseException as msg:
1346+
logging.CyberCPLogFileWriter.writeToFile(f'Error executing remote command {command}. Error {str(msg)}')
12431347

1244-
index = setupKeys.expect(expectation)
1348+
ssh.close()
12451349

1246-
## on first login attempt send password
1350+
sendKey = backupUtilities.sendKey(IPAddress, password, port, user)
12471351

1248-
if index == 0:
1249-
setupKeys.sendline(password)
1250-
elif index == 1:
1251-
setupKeys.sendline(password)
1252-
elif index == 2:
1253-
return [0, 'Please enable password authentication on your remote server.']
1254-
elif index == 3:
1255-
pass
1352+
if sendKey[0] == 1:
1353+
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1354+
ProcessUtilities.executioner(command)
1355+
return [1, "None"]
12561356
else:
1257-
raise BaseException
1258-
1259-
## if it again give you password, than provided password is wrong
1260-
1261-
expectation = []
1262-
expectation.append("please try again.")
1263-
expectation.append("Password:")
1264-
expectation.append(pexpect.EOF)
1265-
1266-
index = setupKeys.expect(expectation)
1267-
1268-
if index == 0:
1269-
return [0, "Wrong Password!"]
1270-
elif index == 1:
1271-
return [0, "Wrong Password!"]
1272-
elif index == 2:
1273-
setupKeys.wait()
1274-
1275-
sendKey = backupUtilities.sendKey(IPAddress, password, port, user)
1276-
1277-
if sendKey[0] == 1:
1278-
return [1, "None"]
1279-
else:
1280-
return [0, sendKey[1]]
1357+
command = 'chmod 644 %s' % ('/root/.ssh/cyberpanel.pub')
1358+
ProcessUtilities.executioner(command)
1359+
return [0, sendKey[1]]
12811360

1361+
except paramiko.AuthenticationException:
1362+
return [0, 'Authentication failed. [setupSSHKeys]']
1363+
except paramiko.SSHException as e:
1364+
return [0, f'SSH error: {str(e)} [setupSSHKeys]']
1365+
except Exception as e:
1366+
return [0, f'General Error: {str(e)} [setupSSHKeys]']
12821367

1283-
except pexpect.TIMEOUT as msg:
1284-
return [0, str(msg) + " [TIMEOUT setupSSHKeys]"]
1285-
except BaseException as msg:
1286-
return [0, str(msg) + " [setupSSHKeys]"]
12871368

12881369
@staticmethod
12891370
def checkIfHostIsUp(IPAddress):

0 commit comments

Comments
 (0)