diff options
| -rw-r--r-- | pica-client/askpassword.cpp | 73 | ||||
| -rw-r--r-- | pica-client/askpassword.h | 8 | ||||
| -rw-r--r-- | pica-client/nodethread.cpp | 7 | ||||
| -rw-r--r-- | pica-client/nodethread.h | 4 | ||||
| -rw-r--r-- | pica-client/skynet.cpp | 31 | ||||
| -rw-r--r-- | pica-client/skynet.h | 1 |
6 files changed, 98 insertions, 26 deletions
diff --git a/pica-client/askpassword.cpp b/pica-client/askpassword.cpp index a171217..70f8644 100644 --- a/pica-client/askpassword.cpp +++ b/pica-client/askpassword.cpp @@ -2,38 +2,54 @@ #include "globals.h" #include <string.h> #include <QInputDialog> +#include <QThread> #include "accounts.h" QMutex AskPassword::passwd_mutex; -QMap<QByteArray,QString> AskPassword::idpasswd; +char AskPassword::current_password[256]; +int AskPassword::is_invalid = 0; +int AskPassword::is_cancelled = 0; +int AskPassword::is_set = 0; AskPassword::AskPassword(QObject *parent) : QObject(parent) { - connect(this,SIGNAL(password_requested()),this,SLOT(password_dialog()),Qt::BlockingQueuedConnection); + connect(this,SIGNAL(password_requested()),this,SLOT(password_dialog()), Qt::BlockingQueuedConnection); +} + +AskPassword::~AskPassword() +{ + memset(current_password, 0, sizeof(current_password)); } int AskPassword::ask_password_cb(char *buf, int size, int rwflag, void *userdata) { - QString pswd; - QByteArray id = QByteArray((const char*) userdata, PICA_ID_SIZE); + //QString pswd; + //QByteArray id = QByteArray((const char*) userdata, PICA_ID_SIZE); + + if (is_cancelled) + return 0; passwd_mutex.lock(); - if (idpasswd.contains(id)) + if (!is_set || is_invalid) { - pswd = idpasswd[id]; + if (askpassword->thread() != QThread::currentThread()) + askpassword->emit_sig(); + else + askpassword->password_dialog(); } - else + + if (is_set) { - askpassword->emit_sig(); - pswd = idpasswd[id]; + strncpy(buf, current_password, size); + buf[size - 1] = 0; } + passwd_mutex.unlock(); - strncpy(buf, pswd.toUtf8().constData(), size); - buf[size - 1] = 0; - return(strlen(buf)); + + return is_set ? (strlen(buf)) : 0; } void AskPassword::emit_sig() @@ -41,15 +57,42 @@ void AskPassword::emit_sig() emit password_requested(); } +void AskPassword::clear() +{ + passwd_mutex.lock(); + is_invalid = 0; + is_set = 0; + is_cancelled = 0; + memset(current_password, 0, sizeof(current_password)); + passwd_mutex.unlock(); + +} + +void AskPassword::setInvalidPassword() +{ + passwd_mutex.lock(); + is_invalid = 1; + memset(current_password, 0, sizeof(current_password)); + passwd_mutex.unlock(); +} + void AskPassword::password_dialog() { - bool ok; - QString title = QObject::tr("Enter private key passphrase"); + bool ok = false; + QString title = is_invalid ? QObject::tr("Invalid passphrase, try again") : QObject::tr("Enter private key passphrase"); QString text = QObject::tr(QString("Passphrase for account %1 \n (%2)") .arg(Accounts::GetCurrentAccount().name) .arg(Accounts::GetCurrentAccount().id.toBase64().constData()).toUtf8().constData()); QString psw = QInputDialog::getText((QWidget*)mainwindow,title,text,QLineEdit::Password,QString(),&ok); + if (ok) - idpasswd[Accounts::GetCurrentAccount().id] = psw; + { + strncpy(current_password, psw.toUtf8().constData(), sizeof(current_password)); + current_password[sizeof(current_password) - 1] = 0; + is_set = 1; + is_invalid = 0; + } + else + is_cancelled = 1; } diff --git a/pica-client/askpassword.h b/pica-client/askpassword.h index 6abc009..9deb691 100644 --- a/pica-client/askpassword.h +++ b/pica-client/askpassword.h @@ -10,12 +10,18 @@ class AskPassword : public QObject Q_OBJECT public: explicit AskPassword(QObject *parent = 0); + ~AskPassword(); static int ask_password_cb(char *buf, int size, int rwflag, void *userdata); + static void setInvalidPassword(); + static void clear(); private: void emit_sig(); - static QMap<QByteArray,QString> idpasswd; static QMutex passwd_mutex; + static char current_password[256]; + static int is_cancelled; + static int is_invalid; + static int is_set; signals: void password_requested(); diff --git a/pica-client/nodethread.cpp b/pica-client/nodethread.cpp index 3850397..579b84b 100644 --- a/pica-client/nodethread.cpp +++ b/pica-client/nodethread.cpp @@ -6,7 +6,7 @@ QMutex NodeThread::finished_mutex; -NodeThread::NodeThread(Nodes::NodeRecord &addr, bool *completed, Accounts::AccountRecord &acc, PICA_c2n **pica_conn, QMutex *wm) : +NodeThread::NodeThread(Nodes::NodeRecord &addr, bool *completed, PICA_acc *acc, PICA_c2n **pica_conn, QMutex *wm) : QThread(0) { node_addr = addr; @@ -23,14 +23,15 @@ void NodeThread::run() int ret; exit = 0; - ret = PICA_new_connection(node_addr.address.toUtf8().constData(), + /*ret = PICA_new_connection(node_addr.address.toUtf8().constData(), node_addr.port, user_account.CA_file.toUtf8().constData(), user_account.cert_file.toUtf8().constData(), user_account.pkey_file.toUtf8().constData(), DHParam::GetDHParamFilename().toUtf8().constData(), AskPassword::ask_password_cb, - &ci); + &ci);*/ + ret = PICA_new_connection(user_account, node_addr.address.toUtf8().constData(), node_addr.port, &ci); switch(ret) { diff --git a/pica-client/nodethread.h b/pica-client/nodethread.h index 524688e..8974255 100644 --- a/pica-client/nodethread.h +++ b/pica-client/nodethread.h @@ -12,7 +12,7 @@ class NodeThread : public QThread { Q_OBJECT public: - explicit NodeThread( Nodes::NodeRecord &addr, bool *completed,Accounts::AccountRecord &acc,struct PICA_c2n **pica_conn, QMutex *wm); + explicit NodeThread( Nodes::NodeRecord &addr, bool *completed, PICA_acc *acc,struct PICA_c2n **pica_conn, QMutex *wm); void CloseThread(); private: void run(); @@ -23,7 +23,7 @@ private: bool *finished_flag; Nodes::NodeRecord node_addr; struct PICA_c2n *ci; - Accounts::AccountRecord user_account; + struct PICA_acc *user_account; struct PICA_c2n **connection; int exit; diff --git a/pica-client/skynet.cpp b/pica-client/skynet.cpp index fda9a31..122df47 100644 --- a/pica-client/skynet.cpp +++ b/pica-client/skynet.cpp @@ -8,6 +8,8 @@ //#include "dialogs/forgedcertdialog.h" #include <QDebug> #include "history.h" +#include "dhparam.h" +#include "askpassword.h" SkyNet::SkyNet() : nodes(config_dbname), QObject(0) @@ -91,7 +93,7 @@ void SkyNet::nodethread_connected(QString addr, quint16 port, NodeThread *thread write_mutex.lock(); struct PICA_c2c *chan = NULL; - ret = PICA_create_channel(nodelink, (const unsigned char*)c2c_peer_ids[i].constData(), &chan); + ret = PICA_create_channel(nodelink, (const unsigned char*)c2c_peer_ids[i].constData(), NULL, &chan); qDebug()<<"restoring channel to "<<c2c_peer_ids[i]<<" ret ="<<ret<<"\n"; write_mutex.unlock(); @@ -183,7 +185,7 @@ void SkyNet::timerEvent(QTimerEvent *e) struct PICA_c2c *chan = NULL; - ret = PICA_create_channel(nodelink, (const unsigned char*)c2c_peer_ids[i].constData(), &chan); + ret = PICA_create_channel(nodelink, (const unsigned char*)c2c_peer_ids[i].constData(), NULL, &chan); qDebug()<<"restoring channel to "<<c2c_peer_ids[i]<<" ret ="<<ret<<" in timer event\n"; @@ -204,6 +206,25 @@ void SkyNet::Join(Accounts::AccountRecord &accrec) QList<Nodes::NodeRecord> nodelist; skynet_account = accrec; nodelist = nodes.GetNodes(); + int ret; + + AskPassword::clear(); + do + { + ret = PICA_open_acc(skynet_account.cert_file.toUtf8().constData(), + skynet_account.pkey_file.toUtf8().constData(), + DHParam::GetDHParamFilename().toUtf8().constData(), + AskPassword::ask_password_cb, + &acc); + + if (ret == PICA_ERRINVPKEYPASSPHRASE) + AskPassword::setInvalidPassword(); + } while (ret == PICA_ERRINVPKEYPASSPHRASE); + + qDebug() << "PICA_open_acc() returned " << ret; + + if (ret != PICA_OK) + return; if (nodelist.count()==0) { @@ -213,7 +234,7 @@ void SkyNet::Join(Accounts::AccountRecord &accrec) for (int i=0;i<nodelist.count() && !self_aware;i++) {//TODO FIXME сделать запуск потоков порциями, а не все сразу - threads.append(new NodeThread(nodelist[i],&self_aware,skynet_account,&nodelink, &write_mutex)); + threads.append(new NodeThread(nodelist[i],&self_aware, acc,&nodelink, &write_mutex)); connect(threads.last(), SIGNAL(finished()),this,SLOT(nodethread_finished())); connect(threads.last(), SIGNAL(NodeStatusChanged(QString,quint16,bool)), this, SLOT(node_status_changed(QString,quint16,bool))); connect(threads.last(), SIGNAL(ConnectedToNode(QString,quint16,NodeThread*)), this, SLOT(nodethread_connected(QString,quint16,NodeThread*))); @@ -265,7 +286,7 @@ if ( (iptr = find_active_chan(to)) ) { struct PICA_c2c *chan = NULL; - ret = PICA_create_channel(nodelink, (const unsigned char*)to.constData(), &chan); + ret = PICA_create_channel(nodelink, (const unsigned char*)to.constData(), NULL, &chan); QList<QString> l; l.append(filepath); @@ -401,7 +422,7 @@ if ( (iptr = find_active_chan(to)) ) { struct PICA_c2c *chan = NULL; - ret = PICA_create_channel(nodelink, (const unsigned char*)to.constData(), &chan); + ret = PICA_create_channel(nodelink, (const unsigned char*)to.constData(), NULL, &chan); QList<QString> l; l.append(msg); diff --git a/pica-client/skynet.h b/pica-client/skynet.h index a0f974b..41aac0d 100644 --- a/pica-client/skynet.h +++ b/pica-client/skynet.h @@ -63,6 +63,7 @@ private: bool self_aware; QString status; struct PICA_c2n *nodelink; + struct PICA_acc *acc; Accounts::AccountRecord skynet_account; QMutex write_mutex; QMap<QByteArray, QList<QString> > msgqueues; |
