/* (c) Copyright 2012 - 2018 Anton Sviridenko https://picapica.im This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "askpassword.h" #include "globals.h" #include #include #include #include "accounts.h" QMutex AskPassword::passwd_mutex; 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); } 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); if (is_cancelled) return 0; passwd_mutex.lock(); if (!is_set || is_invalid) { if (askpassword->thread() != QThread::currentThread()) askpassword->emit_sig(); else askpassword->password_dialog(); } if (is_set) { strncpy(buf, current_password, size); buf[size - 1] = 0; } passwd_mutex.unlock(); return is_set ? (strlen(buf)) : 0; } 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 = 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) { 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; }