summaryrefslogtreecommitdiffabout
path: root/pwmanager
authorulf69 <ulf69>2004-10-19 18:19:48 (UTC)
committer ulf69 <ulf69>2004-10-19 18:19:48 (UTC)
commit3b4dc5931f729bd1385ec83f8c9b82a1eb42ef36 (patch) (side-by-side diff)
tree0c39138398640a7996ea4eea4c0f8196dbdc2bef /pwmanager
parent695c64501950e0503ed558fbe8d8c06993776eaa (diff)
downloadkdepimpi-3b4dc5931f729bd1385ec83f8c9b82a1eb42ef36.zip
kdepimpi-3b4dc5931f729bd1385ec83f8c9b82a1eb42ef36.tar.gz
kdepimpi-3b4dc5931f729bd1385ec83f8c9b82a1eb42ef36.tar.bz2
merging of two bugfixes provided by Michael:
1) files were getting stored always twice 2) "Save As..." returned a No permission dialogbox when executed right after starting the application
Diffstat (limited to 'pwmanager') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/pwmdoc.cpp65
-rw-r--r--pwmanager/pwmanager/pwmdoc.h2
-rw-r--r--pwmanager/pwmanager/pwmdocui.cpp12
3 files changed, 67 insertions, 12 deletions
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp
index e9906a4..f4a1636 100644
--- a/pwmanager/pwmanager/pwmdoc.cpp
+++ b/pwmanager/pwmanager/pwmdoc.cpp
@@ -2,25 +2,25 @@
* *
* 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 2.0 of pwmanager
+ * This file is originaly based on version 1.1 of pwmanager
* and was modified to run on embedded devices that run microkde
*
* $Id$
**************************************************************************/
#include "pwmdoc.h"
#include "pwmview.h"
#include "blowfish.h"
#include "sha1.h"
#include "globalstuff.h"
#include "gpasmanfile.h"
#include "serializer.h"
@@ -321,38 +321,74 @@ PwMDoc::PwMDoc(QObject *parent, const char *name)
}
PwMDoc::~PwMDoc()
{
emit docClosed(this);
getOpenDocList()->del(this);
delete _timer;
}
PwMerror PwMDoc::saveDoc(char compress, const QString *file)
{
PwMerror ret, e;
+ string serialized;
+ QFile f;
+ QString tmpFileMoved(QString::null);
+ bool wasDeepLocked;
+ QString savedFilename(filename);
+
if (!file) {
if (filename == "")
return e_filename;
- } else {
+ if (isDeepLocked()) {
+ /* We don't need to save any data.
+ * It's already all on disk, because
+ * we are deeplocked.
+ */
+ unsetDocStatFlag(DOC_STAT_DISK_DIRTY);
+ ret = e_success;
+ goto out;
+ }
+ } else {
if (*file == "" && filename == "")
return e_filename;
if (*file != "")
filename = *file;
}
- bool wasDeepLocked = isDeepLocked();
+ wasDeepLocked = isDeepLocked();
if (wasDeepLocked) {
- if (deepLock(false) != e_success)
- return e_noPw;
+ /* We are deeplocked. That means all data is already
+ * on disk. BUT we need to do saving procedure,
+ * because *file != savedFilename.
+ * Additionally we need to tempoarly restore
+ * the old "filename", because deepLock() references it.
+ */
+ QString newFilename(filename);
+ filename = savedFilename;
+ getDataChangedLock();
+ e = deepLock(false);
+ putDataChangedLock();
+ filename = newFilename;
+ switch (e) {
+ case e_success:
+ break;
+ case e_wrongPw:
+ case e_noPw:
+ emitDataChanged(this);
+ return e;
+ default:
+ emitDataChanged(this);
+ return e_openFile;
+ }
}
if (!isPwAvailable()) {
/* password is not available. This means, the
* document wasn't saved, yet.
*/
bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD);
QString pw(requestNewMpw(&useChipcard));
if (pw != "") {
currentPw = pw;
} else {
return e_noPw;
@@ -380,45 +416,43 @@ PwMerror PwMDoc::saveDoc(char compress, const QString *file)
printWarn("Invalid Hash-Algorithm selected! "
"Config-file seems to be corrupt. "
"Falling back to SHA1.");
_hashAlgo = PWM_HASH_SHA1;
}
char cryptAlgo = static_cast<char>(_cryptAlgo);
char hashAlgo = static_cast<char>(_hashAlgo);
if (conf()->confGlobMakeFileBackup()) {
if (!backupFile(filename))
return e_fileBackup;
}
- QString tmpFileMoved(QString::null);
if (QFile::exists(filename)) {
/* Move the existing file to some tmp file.
* When saving file succeeds, delete tmp file. Otherwise
* move tmp file back. See below.
*/
Randomizer *rnd = Randomizer::obj();
char rnd_buf[5];
sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF,
rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF);
tmpFileMoved = filename + "." + rnd_buf + ".mv";
if (!copyFile(filename, tmpFileMoved))
return e_openFile;
if (!QFile::remove(filename)) {
printWarn(string("removing orig file ")
+ filename.latin1()
+ " failed!");
}
}
- QFile f(filename);
- string serialized;
+ f.setName(filename);
if (!f.open(IO_ReadWrite)) {
ret = e_openFile;
goto out_moveback;
}
e = writeFileHeader(hashAlgo, hashAlgo,
cryptAlgo, compress,
&currentPw, &f);
if (e == e_hashNotImpl) {
printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl");
f.close();
ret = e_hashNotImpl;
goto out_moveback;
@@ -467,26 +501,34 @@ PwMerror PwMDoc::saveDoc(char compress, const QString *file)
printDebug("PwMDoc::saveDoc(): encrypt() failed");
f.close();
ret = e_enc;
goto out_moveback;
}
unsetDocStatFlag(DOC_STAT_DISK_DIRTY);
f.close();
if (chmod(filename.latin1(),
conf()->confGlobFilePermissions())) {
printWarn(string("chmod failed: ") + strerror(errno));
}
openDocList.edit(this, getTitle().latin1());
- if (wasDeepLocked)
- deepLock(true);
+ if (wasDeepLocked) {
+ /* Do _not_ save the data with the deepLock()
+ * call, because this will recurse
+ * into saveDoc()
+ */
+ deepLock(true, false);
+ /* We don't check return value here, because
+ * it won't fail. See NOTE in deepLock()
+ */
+ }
if (tmpFileMoved != QString::null) {
// now remove the moved file.
if (!QFile::remove(tmpFileMoved)) {
printWarn(string("removing file ")
+ tmpFileMoved.latin1()
+ " failed!");
}
}
ret = e_success;
printDebug(string("writing file { name: ")
+ filename.latin1() + " compress: "
+ tostr(static_cast<int>(compress)) + " cryptAlgo: "
@@ -1760,24 +1802,27 @@ bool PwMDoc::unlockAll_tempoary(bool revert)
}
++catI;
}
printDebug("tempoary unlocked dta.");
}
return true;
}
PwMerror PwMDoc::deepLock(bool lock, bool saveToFile)
{
PwMerror ret;
+ /* NOTE: saveDoc() depends on this function to return
+ * e_success if saveToFile == false
+ */
if (lock) {
if (isDeepLocked())
return e_lock;
if (saveToFile) {
if (isDocEmpty())
return e_docIsEmpty;
ret = saveDoc(conf()->confGlobCompression());
if (ret == e_filename) {
/* the doc wasn't saved to a file
* by the user, yet.
*/
diff --git a/pwmanager/pwmanager/pwmdoc.h b/pwmanager/pwmanager/pwmdoc.h
index 535fb92..a6e5f58 100644
--- a/pwmanager/pwmanager/pwmdoc.h
+++ b/pwmanager/pwmanager/pwmdoc.h
@@ -2,25 +2,25 @@
* *
* 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 2.0 of pwmanager
+ * This file is originaly based on version 1.1 of pwmanager
* and was modified to run on embedded devices that run microkde
*
* $Id$
**************************************************************************/
#ifndef __PWMDOC_H
#define __PWMDOC_H
#define PWM_FILE_VER (static_cast<char>(0x05))
#define PWM_HASH_SHA1 (static_cast<char>(0x01))
#define PWM_HASH_SHA256 (static_cast<char>(0x02))
diff --git a/pwmanager/pwmanager/pwmdocui.cpp b/pwmanager/pwmanager/pwmdocui.cpp
index 7b8e0ee..6ddb6f5 100644
--- a/pwmanager/pwmanager/pwmdocui.cpp
+++ b/pwmanager/pwmanager/pwmdocui.cpp
@@ -264,24 +264,29 @@ bool PwMDocUi::saveDocUi(PwMDoc *doc)
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_noPw ||
+ ret == e_wrongPw ||
+ ret == e_openFile) {
+ 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\n"
"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;
}
@@ -309,25 +314,30 @@ bool PwMDocUi::saveAsDocUi(PwMDoc *doc)
i18n("password filename(*.pwm)"),
currentView);
#endif
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) {
+ if (ret == e_noPw ||
+ ret == e_wrongPw ||
+ ret == e_openFile) {
+ 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\n"
"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;
}