summaryrefslogtreecommitdiffstats
path: root/pica-client/askpassword.cpp
blob: 6700220856b33275b24ad3c5164bb3f287e201c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
	(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, version 3.

	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 <https://www.gnu.org/licenses/>.
*/
#include "askpassword.h"
#include "globals.h"
#include <string.h>
#include <QInputDialog>
#include <QThread>
#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;
}