From c7b65ef3853bfd06bbf2de927777c2a79126c989 Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 23 Sep 2007 13:33:19 +0000 Subject: [PATCH] host key config git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@31 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- src/mainwindow.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mainwindow.h | 6 ++++ 2 files changed, 89 insertions(+), 0 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6b642a0..33455d4 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -33,6 +33,8 @@ #include #include #include +#include +#include MMainWindow::MMainWindow() { @@ -46,6 +48,10 @@ MMainWindow::MMainWindow() m->addAction(tr("&Close Window"),this,SLOT(close())); m=mb->addMenu(tr("&Configure")); m->addAction(tr("&Language..."),this,SLOT(changeLang())); + m->addSeparator(); + m->addAction(tr("&Export Host Key..."),this,SLOT(exportKey())); + m->addAction(tr("&Import Host Key..."),this,SLOT(importKey())); + m->addAction(tr("&Generate Host Key..."),this,SLOT(generateKey())); //create central widget QWidget *loginwidget; @@ -217,3 +223,80 @@ void MMainWindow::changeLang() { choseLanguage(); } + +void MMainWindow::exportKey() +{ + QString key=QSettings().value("hostkey").toString().trimmed(); + saveKey(key); +} + +void MMainWindow::generateKey() +{ + MKeyGen mkg; + if(mkg.exec()==QDialog::Accepted) + saveKey(mkg.getKey()); +} + +void MMainWindow::saveKey(QString key) +{ + QStringList fn; + QFileDialog fdlg(this,tr("Export Key to File"),QString(),"Magic Smoke Host Key (*.mshk)"); + fdlg.setDefaultSuffix("mshk"); + fdlg.setAcceptMode(QFileDialog::AcceptSave); + fdlg.setFileMode(QFileDialog::AnyFile); + if(!fdlg.exec())return; + fn=fdlg.selectedFiles(); + if(fn.size()!=1)return; + QFile fd(fn[0]); + if(!fd.open(QIODevice::WriteOnly|QIODevice::Truncate)){ + QMessageBox::warning(this,tr("Warning"),tr("Unable to open file %1 for writing: %2").arg(fn[0]).arg(fd.errorString())); + return; + } + QString chk=QCryptographicHash::hash(key.toAscii(),QCryptographicHash::Md5).toHex(); + QString out="MagicSmokeHostKey\n"+key+"\n"+chk; + fd.write(out.toAscii()); + fd.close(); +} + +void MMainWindow::importKey() +{ + if(QMessageBox::warning(this,tr("Warning"),tr("Importing a key overwrites the host key that is currently used by this program. This may disable your accounts. Do you still want to continue?"),QMessageBox::Yes|QMessageBox::Abort,QMessageBox::Abort)!=QMessageBox::Yes) + return; + QStringList fn; + QFileDialog fdlg(this,tr("Import Key from File"),QString(),"Magic Smoke Host Key (*.mshk)"); + fdlg.setDefaultSuffix("mshk"); + fdlg.setAcceptMode(QFileDialog::AcceptOpen); + fdlg.setFileMode(QFileDialog::ExistingFile); + if(!fdlg.exec())return; + fn=fdlg.selectedFiles(); + if(fn.size()!=1)return; + QFile fd(fn[0]); + if(!fd.open(QIODevice::ReadOnly)){ + QMessageBox::warning(this,tr("Warning"),tr("Unable to open file %1 for reading: %2").arg(fn[0]).arg(fd.errorString())); + return; + } + //read content (max: 10k to limit potential damage) + QStringList fc=QString::fromAscii(fd.read(10240)).split("\n",QString::SkipEmptyParts); + fd.close(); + //check content + if(fc.size()<3){ + QMessageBox::warning(this,tr("Warning"),tr("This is not a host key file.")); + return; + } + if(fc[0].trimmed()!="MagicSmokeHostKey"){ + QMessageBox::warning(this,tr("Warning"),tr("This is not a host key file.")); + return; + } + QString key=fc[1].trimmed(); + if(!QRegExp("[0-9a-fA-F]+").exactMatch(key) || key.size()<40){ + QMessageBox::warning(this,tr("Warning"),tr("This host key file does not contain a valid key.")); + return; + } + QString chk=QCryptographicHash::hash(key.toAscii(),QCryptographicHash::Md5).toHex(); + if(chk!=fc[2].trimmed()){ + QMessageBox::warning(this,tr("Warning"),tr("The key check sum did not match. Please get a clean copy of the host key file.")); + return; + } + //save + QSettings().setValue("hostkey",key); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 3560096..2d5b675 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -38,6 +38,9 @@ class MMainWindow:public QMainWindow QSpinBox*proxyport; QComboBox*profiles; + //helper for exportKey and generateKey + void saveKey(QString); + private slots: //handling of login/profile screen void initProfiles(); @@ -47,6 +50,9 @@ class MMainWindow:public QMainWindow void startLogin(); //settings void changeLang(); + void exportKey(); + void importKey(); + void generateKey(); }; #endif -- 1.7.2.5