rublon-ssh-old/RublonInstall/mainwindow.cpp

394 lines
13 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <QProcess>
#include <QFileInfo>
//#include <QSslSocket>
QString emailRegExp = QStringLiteral(".+@.+");
MainWindow::MainWindow(QWidget *parent)
: QWizard(parent)
{
setPage(Page_Intro, new IntroPage);
setPage(Page_Environments, new EnvironmentsPage);
setPage(Page_Scripts, new ScriptsPage);
setPage(Page_Summary, new SummaryPage);
setStartId(Page_Intro);
setOption(HaveHelpButton, true);
setPixmap(QWizard::LogoPixmap, QPixmap(":/img/logo2.png"));
setWindowTitle(tr("Rublon Instalator"));
}
void MainWindow::showHelp()
{
static QString lastHelpMessage;
QString message;
switch (currentId()) {
case Page_Intro:
message = tr("The decision you make here will affect which page you "
"get to see next.");
break;
case Page_Environments:
message = tr("Make sure to provide a valid email address, such as "
"thomas.gradgrind@example.co.uk.");
break;
case Page_Scripts:
message = tr("If you don't provide an upgrade key, you will be "
"asked to fill in your details.");
break;
case Page_Summary:
message = tr("You must accept the terms and conditions of the "
"license to proceed.");
break;
default:
message = tr("This help is likely not to be of any help.");
}
if (lastHelpMessage == message)
message = tr("Sorry, I already gave what help I could. "
"Maybe you should try asking a human?");
QMessageBox::information(this, tr("License Wizard Help"), message);
lastHelpMessage = message;
}
IntroPage::IntroPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Select server"));
setSubTitle(tr("Please select Rublon server type."));
localRadioButton = new QRadioButton(tr("&local"));
stagingRadioButton = new QRadioButton(tr("&staging"));
productionRadioButton = new QRadioButton(tr("&production"));
localRadioButton->setChecked(true);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(localRadioButton);
layout->addWidget(stagingRadioButton);
layout->addWidget(productionRadioButton);
setLayout(layout);
connect(localRadioButton,SIGNAL(clicked()), this, SLOT(setLocalUrlApi()));
connect(stagingRadioButton,SIGNAL(clicked()), this, SLOT(setStagingUrlApi()));
connect(productionRadioButton,SIGNAL(clicked()), this, SLOT(setproductionUrlApi()));
}
void IntroPage::initializePage()
{
QLineEdit *urlApi = new QLineEdit(this);
urlApi->setVisible(false);
registerField("URL_API",urlApi);
setField("URL_API",1);
}
int IntroPage::nextId() const
{
return MainWindow::Page_Environments;
}
void IntroPage::setUrlApi(int URL_API)
{
setField("URL_API",URL_API);
}
EnvironmentsPage::EnvironmentsPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Set User Environments"));
setSubTitle(tr("Please fill all fields."));
systemTokenLabel = new QLabel(tr("&System Token:"));
systemTokenLineEdit = new QLineEdit;
systemTokenLabel->setBuddy(systemTokenLineEdit);
secretKeyLabel = new QLabel(tr("&Secret Key:"));
secretKeyLineEdit = new QLineEdit;
secretKeyLabel->setBuddy(secretKeyLineEdit);
userEmailLabel = new QLabel(tr("&User eMail:"));
userEmailLineEdit = new QLineEdit;
userEmailLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression(emailRegExp), this));
userEmailLabel->setBuddy(userEmailLineEdit);
apiUrlLabel = new QLabel(tr("&API Url:"));
apiUrlLineEdit = new QLineEdit;
apiUrlLabel->setBuddy(apiUrlLineEdit);
QString SYSTEM_TOKEN = checkEnvironment("SYSTEM_TOKEN");
systemTokenLineEdit->setText(SYSTEM_TOKEN);
QString SECRET_KEY = checkEnvironment("SECRET_KEY");
secretKeyLineEdit->setText(SECRET_KEY);
QString USER_EMAIL = checkEnvironment("USER_EMAIL");
userEmailLineEdit->setText(USER_EMAIL);
QString URL_API = checkEnvironment("URL_API");
apiUrlLineEdit->setText(URL_API);
//registerField("details.systemToken*", systemTokenLineEdit);
//registerField("details.appUserId*", appUserIdLineEdit);
//registerField("details.userEmail*", userEmailLineEdit);
//registerField("details.rublonX*", rublonXLineEdit);
QGridLayout *layout = new QGridLayout;
layout->addWidget(systemTokenLabel, 0, 0);
layout->addWidget(systemTokenLineEdit, 0, 1);
layout->addWidget(secretKeyLabel, 1, 0);
layout->addWidget(secretKeyLineEdit, 1, 1);
layout->addWidget(userEmailLabel, 2, 0);
layout->addWidget(userEmailLineEdit, 2, 1);
layout->addWidget(apiUrlLabel, 3, 0);
layout->addWidget(apiUrlLineEdit, 3, 1);
setLayout(layout);
}
int EnvironmentsPage::nextId() const
{
apiUrlLineEdit->setEnabled(true);
if(field("URL_API").toInt() == 1) {
apiUrlLineEdit->setEnabled(true);
//apiUrlLineEdit->setText(qgetenv("URL_API"));
}
else if(field("URL_API").toInt() == 2) {
apiUrlLineEdit->setEnabled(false);
apiUrlLineEdit->setText("https://staging-core.rublon.net");
}
else if(field("URL_API").toInt() == 3) {
apiUrlLineEdit->setEnabled(false);
apiUrlLineEdit->setText("https://core.rublon.net");
}
QString name = qgetenv("USER");
if( name.isEmpty())
name = qgetenv("USERNAME");
if(name.isEmpty())
name = "";
QString userEnvironments ="SYSTEM_TOKEN="+systemTokenLineEdit->text()+
"\nUSER_EMAIL="+userEmailLineEdit->text()+
"\nSECRET_KEY="+secretKeyLineEdit->text()+
"\nURL_API="+apiUrlLineEdit->text()+
"\nSYSTEM_USER="+name;
QProcess process;
process.execute("rm /home/"+name+"/.pam_environment");
process.execute("touch /home/"+name+"/.pam_environment");
process.execute("/bin/bash -c \"echo '"+userEnvironments+"' > /home/"+name+"/.pam_environment\"");
process.execute("sync");
return MainWindow::Page_Scripts;
}
QString EnvironmentsPage::checkEnvironment(QString environment)
{
//qDebug() << " open file env " << environment;
QFile file("/home/"+qgetenv("USER")+"/.pam_environment");
//QFile file("/etc/security/pam_env.conf");
if(!file.open(QIODevice::ReadOnly))
return "";
environment = environment +"=";
QTextStream in(&file);
while(!in.atEnd()) {
QString line = in.readLine();
int pos = line.indexOf(environment);
file.close();
if (pos >= 0)
return line.remove(environment, Qt::CaseInsensitive);
//return line;
}
file.close();
return "";
}
ScriptsPage::ScriptsPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Install app and script"));
setSubTitle(tr("Enter user password (required for installation process)."));
sudoPassLabel = new QLabel(tr("&Password:"));
sudoPassLineEdit = new QLineEdit;
sudoPassLineEdit->setEchoMode((QLineEdit::Password));
sudoPassLabel->setBuddy(sudoPassLineEdit);
registerField("details.sudoPass*", sudoPassLineEdit);
sudoErrLabel = new QLabel("");
sudoErrLabel->setStyleSheet("QLabel { color : red}");
QGridLayout *layout = new QGridLayout;
layout->addWidget(sudoPassLabel);
layout->addWidget(sudoPassLineEdit);
layout->addWidget(sudoErrLabel);
setLayout(layout);
}
int ScriptsPage::nextId() const
{
QString sudoPass = sudoPassLineEdit->text();
QString error = "";
// Blokada wywołania przy inicjalizacja okna (opoznia dzialanie, przez probe uruchomienia skryptow)
if(!sudoPass.isEmpty()) {
QString name = qgetenv("USER");
if( name.isEmpty())
name = qgetenv("USERNAME");
if(name.isEmpty())
name = "";
QString sudoPass = sudoPassLineEdit->text();
QString dir = QDir::currentPath();
QProcess process;
process.start("sh -c \"echo "+sudoPass+" | sudo -S cp "+dir+"/rublonlogin.sh /usr/local/bin\""); //skrypt
process.waitForFinished();
//qDebug() << "Console output 1 : " << process.readAllStandardError(); //Reads standard error
error = process.readAllStandardError();
if(error.contains("exit code 2", Qt::CaseInsensitive)) {
sudoErrLabel->setText("Wrong password!");
return -1;
}
process.start("sh -c \"echo "+sudoPass+" | sudo -S cp -R "+dir+"/Rublon /usr/local/bin/"); //app
process.waitForFinished();
process.start("sh -c \"echo "+sudoPass+" | sudo -S rsync -r "+dir+"/lib/ /bin/Rublon/lib/"); //
process.waitForFinished();
process.start("sh -c \"echo "+sudoPass+" | sudo -S rsync -r "+dir+"/plugins/ /bin/Rublon/plugins/"); //
process.waitForFinished();
process.start("sh -c \"echo "+sudoPass+" | sudo -S rsync -r "+dir+"/translations/ /bin/Rublon/translations/"); //
process.waitForFinished();
process.start("sh -c \"echo "+sudoPass+" | sudo -S chmod o+u /etc/pam.d/common-session"); //
process.waitForFinished();
process.start("grep -R \"/usr/local/bin/rublonlogin.sh\" \"/etc/pam.d/common-session\""); //
process.waitForFinished();
QString output = process.readAllStandardOutput();
if(!output.contains("rublonlogin.sh")) {
process.start("sh -c \"echo "+sudoPass+" | sudo -S echo 'session optional pam_xauth.so' >> /etc/pam.d/common-session"); //
process.waitForFinished();
process.start("sh -c \"echo "+sudoPass+" | sudo -S echo 'session optional pam_env.so' >> /etc/pam.d/common-session"); //
process.waitForFinished();
process.start("sh -c \"echo "+sudoPass+" | sudo -S echo 'session optional pam_env.so user_readenv=1 envfile=/etc/default/locale' >> /etc/pam.d/common-session"); //
process.waitForFinished();
process.start("sh -c \"echo "+sudoPass+" | sudo -S echo 'session [success=1] pam_exec.so /usr/local/bin/rublonlogin.sh' >> /etc/pam.d/common-session"); //
process.waitForFinished();
}
process.start("sh -c \"echo "+sudoPass+" | sudo -S chmod 644 /etc/pam.d/common-session"); //app
process.waitForFinished();
}
return MainWindow::Page_Summary;
}
SummaryPage::SummaryPage(QWidget *parent)
: QWizardPage(parent)
{
setTitle(tr("Complete Your Registration"));
setSubTitle(tr("Confirm data and log out from the system."));
bottomLabel = new QLabel;
bottomLabel->setWordWrap(true);
agreeCheckBox = new QCheckBox(tr("Data entered correctly"));
registerField("conclusion.agree*", agreeCheckBox);
systemTokenLabel = new QLabel(checkEnvironment("SYSTEM_TOKEN"));
userEmailLabel = new QLabel(checkEnvironment("USER_EMAIL"));
secretKeyLabel = new QLabel(checkEnvironment("SECRET_KEY"));
apiUrlLabel = new QLabel(checkEnvironment("URL_API"));
startupScriptLabel = new QLabel("Startup Script : " + checkScripts("/usr/local/bin/rublonlogin.sh"));
loginAppLabel = new QLabel("Login App : " + checkScripts("/usr/local/bin/Rublon/rublonLogin"));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(bottomLabel);
layout->addWidget(systemTokenLabel);
layout->addWidget(secretKeyLabel);
layout->addWidget(userEmailLabel);
layout->addWidget(apiUrlLabel);
layout->addWidget(startupScriptLabel);
layout->addWidget(loginAppLabel);
layout->addWidget(agreeCheckBox);
setLayout(layout);
}
int SummaryPage::nextId() const
{
if(agreeCheckBox->checkState()==true) {
QProcess process;
process.execute("pkill -KILL -u " + qgetenv("USER"));
process.execute("dm-tool switch-to-greeter");
}
return -1;
}
void SummaryPage::initializePage()
{
systemTokenLabel->setText(checkEnvironment("SYSTEM_TOKEN"));
userEmailLabel->setText(checkEnvironment("USER_EMAIL"));
secretKeyLabel->setText(checkEnvironment("SECRET_KEY"));
apiUrlLabel->setText(checkEnvironment("URL_API"));
startupScriptLabel->setText("Startup Script : " + checkScripts("/usr/local/bin//rublonlogin.sh"));
loginAppLabel->setText("Login App : " + checkScripts("/usr/local/bin/Rublon/rublonLogin"));
}
void SummaryPage::setVisible(bool visible)
{
QWizardPage::setVisible(visible);
if (visible) {
//wizard()->setButtonText(QWizard::CustomButton1, tr("&Print"));
//wizard()->setOption(QWizard::HaveCustomButton1, true);
} else {
//wizard()->setOption(QWizard::HaveCustomButton1, false);
}
}
QString SummaryPage::checkEnvironment(QString environment)
{
//qDebug() << " open file summary " << environment;
QFile file("/home/"+qgetenv("USER")+"/.pam_environment");
//QFile file("/etc/security/pam_env.conf");
if(!file.open(QIODevice::ReadOnly))
return environment+"=";
QTextStream in(&file);
while(!in.atEnd()) {
QString line = in.readLine();
file.close();
int pos = line.indexOf(environment);
if (pos >= 0)
return line;
}
file.close();
}
QString SummaryPage::checkScripts(QString path)
{
QFileInfo check_file(path);
if(check_file.exists())
return " Installed";
else
return "---";
}