correction of password managment

This commit is contained in:
Daniel Tartavel 2023-03-08 16:03:24 +01:00
parent 63bcc965b5
commit 5f5277199a
7 changed files with 161 additions and 249 deletions

View File

@ -31,7 +31,6 @@ HEADERS += \
FORMS += \ FORMS += \
about.ui \ about.ui \
configuration.ui \ configuration.ui \
login.ui \
mainwindow.ui mainwindow.ui
TRANSLATIONS += \ TRANSLATIONS += \

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.14.2, 2023-03-05T14:17:52. --> <!-- Written by QtCreator 4.14.2, 2023-03-08T15:56:52. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -125,26 +125,3 @@ void MainWindow::readRsyncOutput()
} }
} }
} }
/*void MainWindow::downloadProcessError(QProcess::ProcessError error)
{
QMessageBox::warning(
this,
"RsyncUI",
downloadProcessErrorString[error].toStdString().c_str()
);
}*/
// process raise error
void MainWindow::downloadProcessStderr()
{
QByteArray errorLine;
errorLine = this->downloading.process->readAllStandardError();
QMessageBox::warning(
this,
"RsyncUI",
errorLine
);
}

144
login.ui
View File

@ -1,144 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LoginDialog</class>
<widget class="QDialog" name="LoginDialog">
<property name="windowModality">
<enum>Qt::ApplicationModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>194</height>
</rect>
</property>
<property name="windowTitle">
<string>RsyncUI request</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<widget class="QDialogButtonBox" name="loginBox">
<property name="geometry">
<rect>
<x>50</x>
<y>150</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>381</width>
<height>121</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Login name</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="textInteractionFlags">
<set>Qt::NoTextInteraction</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="loginEdit">
<property name="mouseTracking">
<bool>false</bool>
</property>
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="text">
<string/>
</property>
<property name="maxLength">
<number>32767</number>
</property>
<property name="placeholderText">
<string>Enter login</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="passwordEdit">
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
<property name="placeholderText">
<string>Enter password</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>loginBox</sender>
<signal>accepted()</signal>
<receiver>LoginDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>loginBox</sender>
<signal>rejected()</signal>
<receiver>LoginDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -68,7 +68,8 @@ MainWindow::MainWindow(QWidget *parent)
connect(this, &MainWindow::stopDownloading, this, &MainWindow::cancelled); connect(this, &MainWindow::stopDownloading, this, &MainWindow::cancelled);
connect(config.buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted())); connect(config.buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
connect(config.comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::on_comboBox_currentIndexChanged); connect(config.comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::on_comboBox_currentIndexChanged);
connect(loginD.loginBox, SIGNAL(accepted()), this, SLOT(on_loginBox_accepted())); // connect(loginD.loginBox, SIGNAL(accepted()), this, SLOT(on_loginBox_accepted()));
// connect(this, SIGNAL(passwordReady()), this, SLOT(waitPasswword()));
loadSettings(); loadSettings();
@ -292,7 +293,7 @@ void MainWindow::populateTree()
{ {
// server is validated, scanning directory // server is validated, scanning directory
path = this->connexion.service + "/"; path = this->connexion.service + "/";
scanDir(this->connexion.server, this->connexion.port, nullptr, path); scanDir(this->connexion.server, this->connexion.port, nullptr, path);
} }
// Restoring cursor // Restoring cursor
QGuiApplication::restoreOverrideCursor(); QGuiApplication::restoreOverrideCursor();
@ -303,6 +304,7 @@ void MainWindow::populateTree()
void MainWindow::populateList() void MainWindow::populateList()
{ {
QString server; QString server;
QString service;
QStringList hidden; QStringList hidden;
int port; int port;
int i; int i;
@ -353,18 +355,70 @@ void MainWindow::populateList()
} }
} }
this->settings.endGroup(); this->settings.endGroup();
this->settings.beginGroup("Hidden/"); this->settings.beginGroup("Hidden/" + server);
hidden = this->settings.allKeys(); hidden = this->settings.allKeys();
this->settings.endGroup();
for (i = 0; i < hidden.size(); i++) for (i = 0; i < hidden.size(); i++)
{ {
service = hidden.at(i);
//TODO detect if service is already present //TODO detect if service is already present
ui->listWidget->addItem(this->settings.value(hidden.at(i)).toString() + "\n\t"); if (testServicePresence(service, false))
{
ui->listWidget->addItem(service + "\n\t");
}
} }
QGuiApplication::restoreOverrideCursor(); //setting cursor to default QGuiApplication::restoreOverrideCursor(); //setting cursor to default
this->settings.endGroup();
} }
} }
// Test if service is already present on the server
bool MainWindow::testServicePresence(QString service, bool askPassword)
{
QString cmd;
QStringList param;
QString line;
QString errorRsync;
QStringList v;
QProcess *myProcess;
bool returnValue = false;
QEventLoop loop;
cmd = "/usr/bin/rsync";
param << "--contimeout=10" << "-nq" << "--port=" + QString::number(this->connexion.port) << this->connexion.server + "::" + service;
myProcess = new QProcess(this);
myProcess->setProcessChannelMode(QProcess::MergedChannels);
myProcess->start(cmd, param);
myProcess->waitForStarted();
myProcess->write("\n");
//myProcess->waitForFinished(12000);
while(myProcess->waitForReadyRead(10000))
{
while(1)
{
// line empty then buffer is empty so returning to wait new datas
line = QString::fromUtf8(myProcess->readLine());
if (line.isEmpty())
{
break;
}
if (line.contains("auth failed"))
{
if (askPassword)
{
getUserPassword(&this->connexion);
}
returnValue = true;
}
}
}
if (myProcess->exitCode() == 0)
{
returnValue = true;
}
myProcess->close();
return returnValue;
}
//list services of the rsync server //list services of the rsync server
void MainWindow::listServices() void MainWindow::listServices()
{ {
@ -425,13 +479,15 @@ bool MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
QString fileType; QString fileType;
QString date; QString date;
QProcess * myProcess; QProcess * myProcess;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
bool isDir = false; bool isDir = false;
bool flag = false; bool flag = false;
bool readOk = false; bool readOk = false;
bool passwdOk = false;
int nChild = 0; int nChild = 0;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
myProcess = new QProcess(this); myProcess = new QProcess(this);
myProcess->setProcessChannelMode(QProcess::MergedChannels);
if (parent != nullptr) if (parent != nullptr)
{ {
@ -470,12 +526,19 @@ bool MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
flag = true; flag = true;
break; break;
} }
if (line.contains("auth failed"))
{
myProcess->readAllStandardOutput();
getUserPassword(&this->connexion);
this->rescan = true;
return 0;
}
// extracting name, size and if is dir/file // extracting name, size and if is dir/file
line = line.simplified(); line = line.simplified();
filename = line.section(" ", 4); filename = line.section(" ", 4);
if (filename != '.') if (filename != '.')
{ {
size = line.section(" ", 1, 1); size = line.section(" ", 1, 1);
fullsize = size; fullsize = size;
fullsize.remove(","); fullsize.remove(",");
@ -484,7 +547,8 @@ bool MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
{ {
size = sizeA.at(0) + " " + UnitText[0] + " "; size = sizeA.at(0) + " " + UnitText[0] + " ";
}else }else
{ { myProcess->setProcessEnvironment(env);
size = sizeA.at(0) + "," + sizeA.at(1).left(2) + " " + UnitText[sizeA.count()-1] + " "; size = sizeA.at(0) + "," + sizeA.at(1).left(2) + " " + UnitText[sizeA.count()-1] + " ";
} }
@ -498,6 +562,11 @@ bool MainWindow::scanDir(QString server, int portN, QTreeWidgetItem *parent, QSt
date = line.section(' ', 2, 2); date = line.section(' ', 2, 2);
fileType = getFileType(filename); fileType = getFileType(filename);
addTreeItem(filename, size, fullsize, fileType, date, isDir, parent); addTreeItem(filename, size, fullsize, fileType, date, isDir, parent);
if (passwdOk == false and !this->connexion.password.isEmpty())
{
this->settings.setValue("Passwords/" + this->connexion.server + "/" + this->connexion.service + "/" + this->connexion.user, this->connexion.password);
this->settings.sync();
}
} }
} }
flag = false; flag = false;
@ -655,6 +724,7 @@ void MainWindow::addTreeItem(QString name, QString fileSize, QString fullsize, Q
void MainWindow::on_listWidget_clicked() void MainWindow::on_listWidget_clicked()
{ {
this->connexion.service = ui->listWidget->currentItem()->text().section("\n", 0 ,0); this->connexion.service = ui->listWidget->currentItem()->text().section("\n", 0 ,0);
ui->treeWidget->clear();
preparePopulateTree(); preparePopulateTree();
} }
@ -674,65 +744,72 @@ void MainWindow::preparePopulateTree()
this->downloading.savePath = this->settings.value(str).toString(); this->downloading.savePath = this->settings.value(str).toString();
} }
getUserPassword(false); getUserPassword(&this->connexion);
//this->settings.beginGroup("Passwords/" + this->connexion.server + "/" + this->connexion.service);
//logins = this->settings.allKeys();
//if (logins.count() != 0)
//{
// this->connexion.user = logins[0];
// this->connexion.password = this->settings.value(logins[0]).toString();
//}
this->settings.endGroup();
populateTree(); populateTree();
} }
// get password and user login // get password and user login
// if object = false ==> searching from connexion object // if object = false ==> searching from connexion object
// else searching from downloading object // else searching from downloading object
bool MainWindow::getUserPassword(bool object = false) bool MainWindow::getUserPassword(Connexion * object)
{ {
QStringList logins; QStringList logins;
QString login; QString login;
bool returnValue; QString password;
QString server;
QString service;
bool returnValue = false;
bool ok = false; bool ok = false;
if (object == false) server = object->server;
{ service = object->service;
this->settings.beginGroup("Passwords/" + this->connexion.server + "/" + this->connexion.service);
}else
{
this->settings.beginGroup("Passwords/" + this->downloading.server + "/" + this->downloading.service);
}
logins = this->settings.allKeys();
//TODO choose login in case of multiples logins object->user = "";
if (logins.count() != 0) object->password = "";
this->settings.beginGroup("Passwords/" + server + "/" + service);
logins = this->settings.allKeys();
if (logins.count() != 1)
{ {
//choose login in case of multiples logins
loginD.loginEdit->setHistoryItems(logins);
login = QInputDialog::getItem(this, login = QInputDialog::getItem(this,
"RsincUI", "RsincUI",
tr("There is many users for this service.\nSelect user you want to connect with."), tr("Select the user you want to connect with or enter a new one"),
logins, logins,
0, 0,
true, true,
&ok, &ok,
Qt::Popup, Qt::Popup,
Qt::ImhNoPredictiveText Qt::ImhNoPredictiveText
); );
if (object == false) if (ok and !login.isEmpty())
{ {
this->connexion.user = logins[0]; if (!logins.contains(login))
this->connexion.password = this->settings.value(logins[0]).toString(); {
}else password = QInputDialog::getText(this,
{ tr("RsyncUI Request"),
this->downloading.user = logins[0]; tr("Enter password"), QLineEdit::Password,
this->downloading.password = this->settings.value(logins[0]).toString(); "", &ok, Qt::Popup,
Qt::ImhNoPredictiveText);
if (!ok or password.isEmpty())
{
password = "";
}
}else
{
password = this->settings.value(login).toString();
}
object->user = login;
object->password = password;
} }
returnValue = true;
}else }else
{ {
this->downloading.user = nullptr; object->user = logins.at(0);
this->downloading.password = nullptr; object->password = this->settings.value(object->user).toString();
returnValue = false; returnValue = true;
} }
this->settings.endGroup(); this->settings.endGroup();
return returnValue; return returnValue;
@ -866,7 +943,7 @@ void MainWindow::startDownloading()
ui->progressBar->setValue(0); ui->progressBar->setValue(0);
ui->progressBar->show(); ui->progressBar->show();
getUserPassword(false); //getUserPassword();
//QtConcurrent::run(&this->downloadO, &downloadFile::download, this); //QtConcurrent::run(&this->downloadO, &downloadFile::download, this);
this->download(); this->download();
@ -905,7 +982,7 @@ void MainWindow::downloadFinished(int exitCode, QProcess::ExitStatus exitStatus)
aborted = tr("stopped by user"); aborted = tr("stopped by user");
}else if (exitCode == 5) // password asked }else if (exitCode == 5) // password asked
{ {
loginDialog.show(); getUserPassword(&this->downloading);
retry = true; retry = true;
} }
@ -927,7 +1004,7 @@ void MainWindow::downloadFinished(int exitCode, QProcess::ExitStatus exitStatus)
// disconnecting signals to slots // disconnecting signals to slots
disconnect(this->downloading.process, 0, 0, 0); disconnect(this->downloading.process, 0, 0, 0);
// reset variables and window // reset variables and window, close process
this->downloading.process->close(); this->downloading.process->close();
ui->progressBar->hide(); ui->progressBar->hide();
@ -949,7 +1026,9 @@ void MainWindow::downloadFinished(int exitCode, QProcess::ExitStatus exitStatus)
this->downloading.server = path.midRef(pos+4).toString(); this->downloading.server = path.midRef(pos+4).toString();
path.resize(pos); path.resize(pos);
this->downloading.path = path; this->downloading.path = path;
getUserPassword(true);
//getUserPassword(true);
testServicePresence(this->downloading.service, true);
// savepath exists in settings ? // savepath exists in settings ?
str = "Folder/" + this->downloading.server + "/" + this->downloading.service; str = "Folder/" + this->downloading.server + "/" + this->downloading.service;
@ -1202,7 +1281,7 @@ void MainWindow::loadDownloadList()
} }
// clear object downloading // clear object downloading
void Downloading::clear() void Connexion::clear()
{ {
this->path.clear(); this->path.clear();
this->server.clear(); this->server.clear();
@ -1248,21 +1327,30 @@ void MainWindow::on_actionExit_triggered()
void MainWindow::on_loginBox_accepted() void MainWindow::on_loginBox_accepted()
{ {
if (!loginD.loginEdit->text().isEmpty()) /*Connexion * conn = this->passwordConnexion;
if (!loginD.loginEdit->currentText().isEmpty())
{ {
this->connexion.user = loginD.loginEdit->text(); QString path;
conn->user = loginD.loginEdit->currentText();
path = "Passwords/" + conn->server + "/" + conn->service + "/" + conn->user;
if (!loginD.passwordEdit->text().isEmpty()) if (!loginD.passwordEdit->text().isEmpty())
{ {
this->connexion.password = loginD.passwordEdit->text(); conn->password = loginD.passwordEdit->text();
this->settings.setValue("Passwords/" + this->connexion.server + "/" + this->connexion.service + "/" + this->connexion.user, this->connexion.password); this->settings.setValue("Passwords/" + conn->server + "/" + conn->service + "/" + conn->user, conn->password);
this->settings.sync(); this->settings.sync();
if (this->rescan == true) if (this->rescan == true)
{ {
this->rescan = false; this->rescan = false;
populateTree(); populateTree();
} }
}else if (this->settings.contains(path))
{
conn->password = this->settings.value(path).toString();
} }
} }
emit passwordReady();*/
} }
void MainWindow::setDlSpeed(QString speed) void MainWindow::setDlSpeed(QString speed)
@ -1281,7 +1369,7 @@ void MainWindow::on_actionHiddenService_triggered()
if (ok && !text.isEmpty()) if (ok && !text.isEmpty())
{ {
this->connexion.service = text; this->connexion.service = text;
this->settings.setValue("Hidden/" + this->connexion.server, text); this->settings.setValue("Hidden/" + this->connexion.server + "/" + text, true);
preparePopulateTree(); preparePopulateTree();
} }
} }

View File

@ -46,6 +46,7 @@
#include <QMenu> #include <QMenu>
#include <QTranslator> #include <QTranslator>
#include <QInputDialog> #include <QInputDialog>
#include <QSignalSpy>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
@ -61,10 +62,15 @@ class Connexion
int contimeout = 20; int contimeout = 20;
QString server; QString server;
QString service; QString service;
QString path;
QString savePath;
QString user; QString user;
QString password; QString password;
QProcess * process = nullptr;
int port = 873; int port = 873;
bool comboboxChanged; bool comboboxChanged;
bool quit = false;
void clear();
}; };
class Downloading class Downloading
@ -104,7 +110,7 @@ class MainWindow : public QMainWindow
~MainWindow(); ~MainWindow();
QProgressDialog *progress; QProgressDialog *progress;
Connexion connexion; Connexion connexion;
Downloading downloading; Connexion downloading;
QSettings settings; QSettings settings;
About about; About about;
QDialog Configuration; QDialog Configuration;
@ -117,6 +123,7 @@ class MainWindow : public QMainWindow
QSystemTrayIcon * trayIcon; QSystemTrayIcon * trayIcon;
QString icon = "/usr/share/icons/RsyncUI.png"; QString icon = "/usr/share/icons/RsyncUI.png";
bool rescan = false; bool rescan = false;
bool passwordReturned;
QList<QString> UnitText { QList<QString> UnitText {
tr("B"), tr("B"),
@ -151,8 +158,6 @@ class MainWindow : public QMainWindow
bool validateServer(QString server); bool validateServer(QString server);
bool isIpAddress(QString server); bool isIpAddress(QString server);
void addTreeItem(QString name, QString fileSize, QString fullsize, QString type, QString date, bool isDir, QTreeWidgetItem *parent); void addTreeItem(QString name, QString fileSize, QString fullsize, QString type, QString date, bool isDir, QTreeWidgetItem *parent);
//QTreeWidgetItem * addTreeRoot(QString name, QString size, QString fullsize, bool isDir);
//QTreeWidgetItem * addTreeChild(QTreeWidgetItem *parent, QString name, QString size, QString fullsize, bool isDir);
bool scanDir(QString server, int portN, QTreeWidgetItem *parent = NULL, QString path = "" ); bool scanDir(QString server, int portN, QTreeWidgetItem *parent = NULL, QString path = "" );
void startDownloading(); void startDownloading();
void loadSettings(); void loadSettings();
@ -165,8 +170,9 @@ class MainWindow : public QMainWindow
void hideWindow(); void hideWindow();
void showWindow(); void showWindow();
void init(); void init();
bool getUserPassword(bool); bool getUserPassword(Connexion *);
void preparePopulateTree(); void preparePopulateTree();
bool testServicePresence(QString, bool=false);
private slots: private slots:
@ -176,10 +182,6 @@ class MainWindow : public QMainWindow
void downloadFinished(int exitCode, QProcess::ExitStatus exitStatus); void downloadFinished(int exitCode, QProcess::ExitStatus exitStatus);
// void downloadProcessError(QProcess::ProcessError error);
void downloadProcessStderr();
void readRsyncOutput(); void readRsyncOutput();
void stoppingDownload(); void stoppingDownload();
@ -226,7 +228,7 @@ class MainWindow : public QMainWindow
void speed(QString); void speed(QString);
void finishedSignal(bool = true); void finishedSignal(bool = true);
void fileName(QString); void fileName(QString);
//void errorSignal(QString); void passwordReady();
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -44,29 +44,18 @@ const vector<string> explode(const string& s, const char& c, int n = 0)
} }
// test return code of rsync // test return code of rsync
// return true in case of error
bool testRsyncReturn(MainWindow * w, QProcess * myProcess) bool testRsyncReturn(MainWindow * w, QProcess * myProcess)
{ {
if (myProcess->exitStatus() != 0) if (myProcess->exitStatus() != 0)
{ {
QMessageBox::warning( QMessageBox::warning(
NULL, w,
"RsyncUI", "RsyncUI",
myProcess->errorString(), myProcess->errorString(),
QMessageBox::Ok, QMessageBox::Ok,
QMessageBox::Ok); QMessageBox::Ok);
return true; return true;
}else if (myProcess->exitCode() == 5)
{
w->loginDialog.show();
}else if (myProcess->exitCode() != 0)
{
QMessageBox::warning(
NULL,
"RsyncUI",
rsyncErrorStrings[myProcess->exitCode()],
QMessageBox::Ok,
QMessageBox::Ok);
return true;
} }
return false; return false;
} }
@ -78,3 +67,4 @@ QString getFileType(QString filename)
QString returnValue = mime.name().section('/',0 ,0); QString returnValue = mime.name().section('/',0 ,0);
return returnValue; return returnValue;
} }