summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwmdoc.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/pwmdoc.cpp') (more/less context) (show whitespace changes)
-rw-r--r--pwmanager/pwmanager/pwmdoc.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp
index 61f2616..36b0df5 100644
--- a/pwmanager/pwmanager/pwmdoc.cpp
+++ b/pwmanager/pwmanager/pwmdoc.cpp
@@ -1,85 +1,87 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * copyright (C) 2003, 2004 by Michael Buesch * 3 * copyright (C) 2003, 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de * 4 * email: mbuesch@freenet.de *
5 * * 5 * *
6 * This program is free software; you can redistribute it and/or modify * 6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 * 7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. * 8 * as published by the Free Software Foundation. *
9 * * 9 * *
10 ***************************************************************************/ 10 ***************************************************************************/
11 11
12/*************************************************************************** 12/***************************************************************************
13 * copyright (C) 2004 by Ulf Schenk 13 * copyright (C) 2004 by Ulf Schenk
14 * This file is originaly based on version 1.1 of pwmanager 14 * This file is originaly based on version 1.1 of pwmanager
15 * and was modified to run on embedded devices that run microkde 15 * and was modified to run on embedded devices that run microkde
16 * 16 *
17 * $Id$ 17 * $Id$
18 **************************************************************************/ 18 **************************************************************************/
19 19
20#include "pwmdoc.h" 20#include "pwmdoc.h"
21#include "pwmview.h" 21#include "pwmview.h"
22#include "blowfish.h" 22#include "blowfish.h"
23#include "sha1.h" 23#include "sha1.h"
24#include "globalstuff.h" 24#include "globalstuff.h"
25#include "gpasmanfile.h" 25#include "gpasmanfile.h"
26#include "serializer.h" 26#include "serializer.h"
27#include "compressgzip.h" 27#include "compressgzip.h"
28//US#include "compressbzip2.h" 28//US#include "compressbzip2.h"
29#include "randomizer.h" 29#include "randomizer.h"
30#include "pwminit.h" 30#include "pwminit.h"
31#include "libgcryptif.h" 31#include "libgcryptif.h"
32#ifdef PWM_EMBEDDED 32#ifdef PWM_EMBEDDED
33#include "pwmprefs.h" 33#include "pwmprefs.h"
34#include "kglobal.h" 34#include "kglobal.h"
35#endif 35#endif
36 36
37#include <kmessagebox.h> 37#include <kmessagebox.h>
38#include <kconfig.h>
39#include <kglobalsettings.h>
38#include <libkcal/syncdefines.h> 40#include <libkcal/syncdefines.h>
39 41
40 42
41#ifdef CONFIG_KWALLETIF 43#ifdef CONFIG_KWALLETIF
42# include "kwalletemu.h" 44# include "kwalletemu.h"
43#endif // CONFIG_KWALLETIF 45#endif // CONFIG_KWALLETIF
44 46
45#include <qdatetime.h> 47#include <qdatetime.h>
46#include <qsize.h> 48#include <qsize.h>
47#include <qfileinfo.h> 49#include <qfileinfo.h>
48#include <qfile.h> 50#include <qfile.h>
49 51
50#include <stdio.h> 52#include <stdio.h>
51#include <stdlib.h> 53#include <stdlib.h>
52#include <errno.h> 54#include <errno.h>
53#include <string.h> 55#include <string.h>
54//US#include <iostream> 56//US#include <iostream>
55#include <algorithm> 57#include <algorithm>
56#include <sys/types.h> 58#include <sys/types.h>
57#include <sys/stat.h> 59#include <sys/stat.h>
58#ifndef _WIN32_ 60#ifndef _WIN32_
59#include <unistd.h> 61#include <unistd.h>
60#include <stdint.h> 62#include <stdint.h>
61#endif 63#endif
62 64
63#ifdef PWM_EMBEDDED 65#ifdef PWM_EMBEDDED
64#ifndef Q_LONG 66#ifndef Q_LONG
65#define Q_LONG long 67#define Q_LONG long
66#endif 68#endif
67 69
68#ifndef Q_ULONG 70#ifndef Q_ULONG
69#define Q_ULONG unsigned long 71#define Q_ULONG unsigned long
70#endif 72#endif
71#endif //PWM_EMBEDDED 73#endif //PWM_EMBEDDED
72 74
73 75
74//TODO: reset to its normal value. 76//TODO: reset to its normal value.
75//LR set to 5 min 77//LR set to 5 min
76 #define META_CHECK_TIMER_INTERVAL300 /* 10 300*/ /* sek */ 78 #define META_CHECK_TIMER_INTERVAL300 /* 10 300*/ /* sek */
77 79
78using namespace std; 80using namespace std;
79 81
80 82
81void PwMDocList::add(PwMDoc *doc, const string &id) 83void PwMDocList::add(PwMDoc *doc, const string &id)
82{ 84{
83#ifdef PWM_DEBUG 85#ifdef PWM_DEBUG
84 // check for existance of object in debug mode only. 86 // check for existance of object in debug mode only.
85 vector<listItem>::iterator begin = docList.begin(), 87 vector<listItem>::iterator begin = docList.begin(),
@@ -382,96 +384,139 @@ PwMerror PwMDoc::saveDoc(char compress, const QString *file)
382 emitDataChanged(this); 384 emitDataChanged(this);
383 return e_openFile; 385 return e_openFile;
384 } 386 }
385 } 387 }
386 388
387 if (!isPwAvailable()) { 389 if (!isPwAvailable()) {
388 /* password is not available. This means, the 390 /* password is not available. This means, the
389 * document wasn't saved, yet. 391 * document wasn't saved, yet.
390 */ 392 */
391 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); 393 bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD);
392 QString pw(requestNewMpw(&useChipcard)); 394 QString pw(requestNewMpw(&useChipcard));
393 if (pw != "") { 395 if (pw != "") {
394 currentPw = pw; 396 currentPw = pw;
395 } else { 397 } else {
396 return e_noPw; 398 return e_noPw;
397 } 399 }
398 if (useChipcard) { 400 if (useChipcard) {
399 setDocStatFlag(DOC_STAT_USE_CHIPCARD); 401 setDocStatFlag(DOC_STAT_USE_CHIPCARD);
400 } else { 402 } else {
401 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); 403 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD);
402 } 404 }
403 } 405 }
404 406
405 int _cryptAlgo = conf()->confGlobCryptAlgo(); 407 int _cryptAlgo = conf()->confGlobCryptAlgo();
406 int _hashAlgo = conf()->confGlobHashAlgo(); 408 int _hashAlgo = conf()->confGlobHashAlgo();
407 409
408 // sanity check for the selected algorithms 410 // sanity check for the selected algorithms
409 if (_cryptAlgo < PWM_CRYPT_BLOWFISH || 411 if (_cryptAlgo < PWM_CRYPT_BLOWFISH ||
410 _cryptAlgo > PWM_CRYPT_TWOFISH128) { 412 _cryptAlgo > PWM_CRYPT_TWOFISH128) {
411 printWarn("Invalid Crypto-Algorithm selected! " 413 printWarn("Invalid Crypto-Algorithm selected! "
412 "Config-file seems to be corrupt. " 414 "Config-file seems to be corrupt. "
413 "Falling back to Blowfish."); 415 "Falling back to Blowfish.");
414 _cryptAlgo = PWM_CRYPT_BLOWFISH; 416 _cryptAlgo = PWM_CRYPT_BLOWFISH;
415 } 417 }
416 if (_hashAlgo < PWM_HASH_SHA1 || 418 if (_hashAlgo < PWM_HASH_SHA1 ||
417 _hashAlgo > PWM_HASH_TIGER) { 419 _hashAlgo > PWM_HASH_TIGER) {
418 printWarn("Invalid Hash-Algorithm selected! " 420 printWarn("Invalid Hash-Algorithm selected! "
419 "Config-file seems to be corrupt. " 421 "Config-file seems to be corrupt. "
420 "Falling back to SHA1."); 422 "Falling back to SHA1.");
421 _hashAlgo = PWM_HASH_SHA1; 423 _hashAlgo = PWM_HASH_SHA1;
422 } 424 }
423 char cryptAlgo = static_cast<char>(_cryptAlgo); 425 char cryptAlgo = static_cast<char>(_cryptAlgo);
424 char hashAlgo = static_cast<char>(_hashAlgo); 426 char hashAlgo = static_cast<char>(_hashAlgo);
425 427
426 if (conf()->confGlobMakeFileBackup()) { 428 if (conf()->confGlobMakeFileBackup()) {
427 if (!backupFile(filename)) 429 if (!backupFile(filename))
428 return e_fileBackup; 430 return e_fileBackup;
429 } 431 }
432 int mLastBackupDate = 0;
433 KConfig configGlobal (locateLocal("config","pwmanagerbuprc"));
434 QFileInfo fileInfo ( filename );
435 mLastBackupDate = configGlobal.readNumEntry( "LastBackupDate-"+ fileInfo.fileName (), 0 );
436 KConfig config (locateLocal("config","microkdeglobalrc"));
437 config.setGroup( "BackupSettings" );
438 bool b_enabled = config.readBoolEntry( "BackupEnabled" );
439 if ( b_enabled ) {
440 int num = config.readNumEntry( "BackupNumbers" );
441 int d_count = config.readNumEntry( "BackupDayCount" );
442 bool stdDir = config.readBoolEntry( "BackupUseDefaultDir" );
443 QString bupDir = config.readEntry( "BackupDatadir" );
444 QDate reference ( 2000,1,1 );
445 int daysTo = reference.daysTo ( QDate::currentDate() );
446 bool saveDate = false;
447 if ( daysTo - d_count >= mLastBackupDate ) {
448 qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate );
449 if ( stdDir )
450 bupDir = KGlobalSettings::backupDataDir();
451 int retval = KApplication::createBackup( filename, bupDir, num );
452 if ( retval == 0 ) {
453 qDebug("KO: Backup cancelled. Will try again tomorrow ");
454 // retval == 0 : backup skipped for today, try again tomorrow
455 mLastBackupDate = daysTo - d_count+1;
456 saveDate = true;
457 } else if ( retval == 1 ){
458 qDebug("KO: Backup created.");
459 // backup ok
460 mLastBackupDate = daysTo;
461 saveDate = true;
462 } else if ( retval == 2 ){
463 qDebug("KO: Backup globally cancelled.");
464 // backup globally cancelled
465 b_enabled = false;
466 }
467 if ( !b_enabled ) {
468 config.writeEntry( "mBackupEnabled", false );
469 }
470 if ( saveDate ) {
471 configGlobal.writeEntry( "LastBackupDate-"+ fileInfo.fileName (), mLastBackupDate );
472 }
473 }
474 }
430 if (QFile::exists(filename)) { 475 if (QFile::exists(filename)) {
431 /* Move the existing file to some tmp file. 476 /* Move the existing file to some tmp file.
432 * When saving file succeeds, delete tmp file. Otherwise 477 * When saving file succeeds, delete tmp file. Otherwise
433 * move tmp file back. See below. 478 * move tmp file back. See below.
434 */ 479 */
435 Randomizer *rnd = Randomizer::obj(); 480 Randomizer *rnd = Randomizer::obj();
436 char rnd_buf[5]; 481 char rnd_buf[5];
437 sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF, 482 sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF,
438 rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF); 483 rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF);
439 tmpFileMoved = filename + "." + rnd_buf + ".mv"; 484 tmpFileMoved = filename + "." + rnd_buf + ".mv";
440 if (!copyFile(filename, tmpFileMoved)) 485 if (!copyFile(filename, tmpFileMoved))
441 return e_openFile; 486 return e_openFile;
442 if (!QFile::remove(filename)) { 487 if (!QFile::remove(filename)) {
443 printWarn(string("removing orig file ") 488 printWarn(string("removing orig file ")
444 + filename.latin1() 489 + filename.latin1()
445 + " failed!"); 490 + " failed!");
446 } 491 }
447 } 492 }
448 f.setName(filename); 493 f.setName(filename);
449 if (!f.open(IO_ReadWrite)) { 494 if (!f.open(IO_ReadWrite)) {
450 ret = e_openFile; 495 ret = e_openFile;
451 goto out_moveback; 496 goto out_moveback;
452 } 497 }
453 e = writeFileHeader(hashAlgo, hashAlgo, 498 e = writeFileHeader(hashAlgo, hashAlgo,
454 cryptAlgo, compress, 499 cryptAlgo, compress,
455 &currentPw, &f); 500 &currentPw, &f);
456 if (e == e_hashNotImpl) { 501 if (e == e_hashNotImpl) {
457 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl"); 502 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl");
458 f.close(); 503 f.close();
459 ret = e_hashNotImpl; 504 ret = e_hashNotImpl;
460 goto out_moveback; 505 goto out_moveback;
461 } else if (e != e_success) { 506 } else if (e != e_success) {
462 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed"); 507 printDebug("PwMDoc::saveDoc(): writeFileHeader() failed");
463 f.close(); 508 f.close();
464 ret = e_writeHeader; 509 ret = e_writeHeader;
465 goto out_moveback; 510 goto out_moveback;
466 } 511 }
467 if (!serializeDta(&serialized)) { 512 if (!serializeDta(&serialized)) {
468 printDebug("PwMDoc::saveDoc(): serializeDta() failed"); 513 printDebug("PwMDoc::saveDoc(): serializeDta() failed");
469 f.close(); 514 f.close();
470 ret = e_serializeDta; 515 ret = e_serializeDta;
471 goto out_moveback; 516 goto out_moveback;
472 } 517 }
473 e = writeDataHash(hashAlgo, &serialized, &f); 518 e = writeDataHash(hashAlgo, &serialized, &f);
474 if (e == e_hashNotImpl) { 519 if (e == e_hashNotImpl) {
475 printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl"); 520 printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl");
476 f.close(); 521 f.close();
477 ret = e_hashNotImpl; 522 ret = e_hashNotImpl;