summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwmdocui.cpp
blob: 66a1b59c091c6bd3910df79ae63dd64a27a9b8e6 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/***************************************************************************
 *                                                                         *
 *   copyright (C) 2003, 2004 by Michael Buesch                            *
 *   email: mbuesch@freenet.de                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
 * copyright (C) 2004 by Ulf Schenk
 * This file is originaly based on version 1.0.1 of pwmanager
 * and was modified to run on embedded devices that run microkde
 *
 * $Id$
 **************************************************************************/  

#include "pwmdocui.h"
#include "setmasterpwwndimpl.h"
#include "getmasterpwwndimpl.h"
#include "pwmexception.h"
#include "getkeycardwnd.h"
#include "pwm.h"
#include "globalstuff.h"
#include "spinforsignal.h"

#include <qlineedit.h>
#include <qtabwidget.h>

#include <kmessagebox.h>
#include <kfiledialog.h>

#ifndef PWM_EMBEDDED
#include <kwin.h>
#else
#endif


#ifdef CONFIG_KEYCARD
# include "pwmkeycard.h"
#endif


PwMDocUi::PwMDocUi(QObject *parent, const char *name)
 : QObject(parent, name)
{
	currentView = 0;
	keyCard = 0;
}

PwMDocUi::~PwMDocUi()
{
}

QString PwMDocUi::requestMpw(bool chipcard)
{
	QString pw;

	if (chipcard) {
#ifdef CONFIG_KEYCARD
		PWM_ASSERT(keyCard);
		uint32_t id;
		string ret;
		SpinForSignal *spinner = keyCard->getSpinner();
		connect(keyCard, SIGNAL(keyAvailable(uint32_t, const string &)),
			spinner, SLOT(u32_str_slot(uint32_t, const string &)));
		keyCard->getKey();
		spinner->spin(&id, &ret);
		disconnect(keyCard, SIGNAL(keyAvailable(uint32_t, const string &)),
			   spinner, SLOT(u32_str_slot(uint32_t, const string &)));
		if (ret == "")
			return "";
		pw = ret.c_str();
#else // CONFIG_KEYCARD
		no_keycard_support_msg_box(currentView);
#endif // CONFIG_KEYCARD
	} else {
		GetMasterPwWndImpl pwWnd;
#ifndef PWM_EMBEDDED
		KWin::setState(pwWnd.winId(), NET::StaysOnTop);
#endif
		if (pwWnd.exec() != 1)
			return "";
		pw = pwWnd.pwLineEdit->text();
	}

	return pw;
}

QString PwMDocUi::requestNewMpw(bool *chipcard)
{
	QString pw;
	SetMasterPwWndImpl pwWnd(currentView);
	pwWnd.setPwMKeyCard(keyCard);
#ifndef PWM_EMBEDDED
	if (!chipcard) {
		pwWnd.mainTab->removePage(pwWnd.mainTab->page(1));
	}
#else
	qDebug("PwMDocUi::requestNewMpw must be implemented");
#endif

	if (pwWnd.exec() != 1)
		return "";
	pw = pwWnd.getPw(chipcard).c_str();

	return pw;
}

QString PwMDocUi::requestMpwChange(const QString *currentPw, bool *chipcard)
{
	QString pw(requestMpw(*chipcard));
	if (pw == "")
		return "";
	if (pw != *currentPw) {
		wrongMpwMsgBox(*chipcard);
		return "";
	}

	pw = requestNewMpw(chipcard);
	if (pw == "")
		return "";
	return pw;
}

void PwMDocUi::wrongMpwMsgBox(bool chipcard, QString prefix, QString postfix)
{
	QString msg;
	if (prefix != "") {
		msg += prefix;
		msg += "\n";
	}

	if (chipcard) {
		msg += i18n("Wrong key-card!\n"
			    "Please try again with the "
			    "correct key-card.");
	} else {
		msg += i18n("Wrong master-password!\n"
			    "Please try again.");
	}

	if (postfix != "") {
		msg += "\n";
		msg += postfix;
	}
	KMessageBox::error(currentView, msg,
			   (chipcard) ? (i18n("wrong chipcard"))
			   	      : (i18n("password error")));
}

void PwMDocUi::noMpwMsgBox(bool chipcard, QString prefix, QString postfix)
{
	QString msg;
	if (prefix != "") {
		msg += prefix;
		msg += "\n";
	}

	if (chipcard) {
		msg += i18n("No key-card found!\n"
			    "Please insert the "
			    "correct key-card.");
	} else {
		msg += i18n("No master-password given!");
	}

	if (postfix != "") {
		msg += "\n";
		msg += postfix;
	}
	KMessageBox::error(currentView, msg,
			   (chipcard) ? (i18n("no chipcard"))
			   	      : (i18n("password error")));
}

void PwMDocUi::rootAlertMsgBox()
{
	KMessageBox::error(currentView,
			   i18n("This feature is not available, "
				"if you execute PwM with \"root\" "
				"UID 0 privileges, for security reasons!"),
			   i18n("not allowed as root!"));
}

void PwMDocUi::cantDeeplock_notSavedMsgBox()
{
	KMessageBox::error(currentView,
			   i18n("Can't deep-lock, because the document "
				"hasn't been saved, yet. Please save "
				"to a file and try again."),
			   i18n("not saved, yet"));
}

void PwMDocUi::gpmPwLenErrMsgBox()
{
	KMessageBox::error(currentView,
			   i18n("GPasman does not support passwords "
				"shorter than 4 characters! Please try "
				"again with a longer password."),
			   i18n("password too short"));
}

int PwMDocUi::dirtyAskSave(const QString &docTitle)
{
	int ret;
#ifndef PWM_EMBEDDED
	ret = KMessageBox::questionYesNoCancel(currentView,
					       i18n("The list \"") +
					       docTitle +
					       i18n
					       ("\" has been modified.\n"
					        "Do you want to save it?"),
					       i18n("save?"));
	if (ret == KMessageBox::Yes) {
		return 0;
	} else if (ret == KMessageBox::No) {
		return 1;
	}
#else
	ret = KMessageBox::warningYesNoCancel(currentView,
					       i18n("The list \"") +
					       docTitle +
					       i18n
					       ("\" has been modified.\n"
					        "Do you want to save it?"),
					       i18n("save?"));
	if (ret == KMessageBox::Yes) {
		return 0;
	} else if (ret == KMessageBox::No) {
		return 1;
	}

#endif

	// cancel
	return -1;
}

bool PwMDocUi::saveDocUi(PwMDoc *doc)
{
	PWM_ASSERT(doc);
	doc->timer()->getLock(DocTimer::id_autoLockTimer);
	if (doc->isDocEmpty()) {
		KMessageBox::information(currentView,
					 i18n
					 ("Sorry, there's nothing to save.\n"
					  "Please first add some passwords."),
					 i18n("nothing to do"));
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return true;
	}
	PwMerror ret = doc->saveDoc(conf()->confGlobCompression());
	if (ret == e_filename) {
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return saveAsDocUi(doc);
	} else if (ret == e_weakPw) {
		KMessageBox::error(currentView,
				   i18n("Error: This is a weak password.\n"
					"Please select another password."),
				   i18n("weak password"));
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return false;
	} else if (ret == e_fileBackup) {
		KMessageBox::error(currentView,
				   i18n("Error: Couldn't make backup-file!"),
				   i18n("backup failed"));
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return false;
	} else if (ret != e_success) {
		KMessageBox::error(currentView,
				   i18n("Error: Couldn't write to file.\n"
					"Please check if you have permission to "
					"write to the file in that directory."),
				   i18n("error while writing"));
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return false;
	}
	doc->timer()->putLock(DocTimer::id_autoLockTimer);
	return true;
}

bool PwMDocUi::saveAsDocUi(PwMDoc *doc)
{
	PWM_ASSERT(doc);
	doc->timer()->getLock(DocTimer::id_autoLockTimer);
	if (doc->isDocEmpty()) {
		KMessageBox::information(currentView,
					 i18n
					 ("Sorry, there's nothing to save.\n"
					  "Please first add some passwords."),
					 i18n("nothing to do"));
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return true;
	}
	QString fn(KFileDialog::getSaveFileName(QString::null,
						i18n("*.pwm|PwManager Password file"),
						currentView));
	if (fn == "") {
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return false;
	}
	if (fn.right(4) != ".pwm")
		fn += ".pwm";

	PwMerror ret = doc->saveDoc(conf()->confGlobCompression(), &fn);
	if (ret != e_success) {
		KMessageBox::error(currentView,
				   i18n("Error: Couldn't write to file.\n"
					"Please check if you have permission to "
					"write to the file in that directory."),
				   i18n("error while writing"));
		doc->timer()->putLock(DocTimer::id_autoLockTimer);
		return false;
	}
	doc->timer()->putLock(DocTimer::id_autoLockTimer);
	return true;
}

bool PwMDocUi::openDocUi(PwMDoc *doc,
			 QString filename,
			 bool openDeepLocked)
{
	if (filename.isEmpty())
		filename = KFileDialog::getOpenFileName(QString::null,
							i18n("*.pwm|PwManager Password file\n"
							     "*|All files"), getCurrentView());
	if (filename.isEmpty())
		goto cancelOpen;
	PwMerror ret;
	while (true) {
		int lockStat = -1;
		if (openDeepLocked) {
			lockStat = 2;
		} else {
			if (conf()->confGlobUnlockOnOpen()) {
				lockStat = 0;
			} else {
				lockStat = 1;
			}
		}
		ret = doc->openDoc(&filename, lockStat);
		if (ret != e_success) {
			if (ret == e_readFile || ret == e_openFile) {
				KMessageBox::error(getCurrentView(),
						   i18n("Could not read file!")
						   + "\n"
						   + filename,
						   i18n("file error"));
				goto cancelOpen;
			}
			if (ret == e_alreadyOpen) {
				KMessageBox::error(getCurrentView(),
						   i18n("This file is already open."),
						   i18n("already open"));
				goto cancelOpen;
			}
			if (ret == e_fileVer) {
				KMessageBox::error(getCurrentView(),
						   i18n
						   ("File-version is not supported!\n"
						    "Did you create this file with an older or newer version of PwM?"),
						   i18n
						   ("incompatible version"));
				goto cancelOpen;
			}
			if (ret == e_wrongPw) {
				continue;
			}
			if (ret == e_noPw) {
				goto cancelOpen;
			}
			if (ret == e_fileFormat) {
				KMessageBox::error(getCurrentView(),
						   i18n
						   ("Sorry, this file has not been recognized "
						    "as a PwM Password file.\n"
						    "Probably you have selected the wrong file."),
						   i18n
						   ("no PwM password-file"));
				goto cancelOpen;
			}
			if (ret == e_fileCorrupt) {
				KMessageBox::error(getCurrentView(),
						   i18n
						   ("File corrupt!\n"
						    "Maybe the media, you stored this file on, "
						    "had bad sectors?"),
						   i18n
						   ("checksum error"));
				goto cancelOpen;
			}
		}
		break;
	}
	return true;

      cancelOpen:
	return false;
}

QString PwMDocUi::string_defaultCategory()
{
	return i18n("Default");
}

QString PwMDocUi::string_locked()
{
	return i18n("<LOCKED>");
}

QString PwMDocUi::string_deepLockedShort()
{
	return i18n("DEEP-LOCKED");
}

QString PwMDocUi::string_deepLockedLong()
{
	return i18n("This file is DEEP-LOCKED!\n"
		    "That means all data has been encrypted "
		    "and written out to the file. If you want "
		    "to see the entries, please UNLOCK the file. "
		    "While unlocking, you will be prompted for the "
		    "master-password or the key-card.");
}

QString PwMDocUi::string_defaultTitle()
{
	return i18n("Untitled");
}

#ifndef PWM_EMBEDDED
#include "pwmdocui.moc"
#endif