This commit is contained in:
J-Jamet committed 2018-04-30 18:18:13 +02:00
1 parent 4164b5bd37
commit e8d71039d7
10 files changed
-856

No files matched your search

@@ -1,73 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwEntry;
public class AddEntry extends RunnableOnFinish {
protected Database mDb;
private PwEntry mEntry;
private Context ctx;
private boolean mDontSave;
public AddEntry(Context ctx, Database db, PwEntry entry, OnFinish finish) {
this(ctx, db, entry, finish, false);
}
public AddEntry(Context ctx, Database db, PwEntry entry, OnFinish finish, boolean dontSave) {
super(finish);
this.mDb = db;
this.mEntry = entry;
this.ctx = ctx;
this.mDontSave = dontSave;
this.mFinish = new AfterAdd(mFinish);
}
@Override
public void run() {
mDb.addEntryTo(mEntry, mEntry.getParent());
// Commit to disk
SaveDB save = new SaveDB(ctx, mDb, mFinish, mDontSave);
save.run();
}
private class AfterAdd extends OnFinish {
AfterAdd(OnFinish finish) {
super(finish);
}
@Override
public void run() {
if ( !mSuccess ) {
mDb.removeEntryFrom(mEntry, mEntry.getParent());
}
// TODO if add entry callback
super.run();
}
}
}
@@ -1,79 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwGroup;
public class AddGroup extends RunnableOnFinish {
protected Database mDb;
private PwGroup mNewGroup;
private Context ctx;
private boolean mDontSave;
public AddGroup(Context ctx, Database db, PwGroup newGroup, AfterActionNodeOnFinish afterAddNode) {
this(ctx, db, newGroup, afterAddNode, false);
}
public AddGroup(Context ctx, Database db, PwGroup newGroup, AfterActionNodeOnFinish afterAddNode,
boolean dontSave) {
super(afterAddNode);
this.mDb = db;
this.mNewGroup = newGroup;
this.mDontSave = dontSave;
this.ctx = ctx;
this.mFinish = new AfterAdd(mFinish);
}
@Override
public void run() {
mDb.addGroupTo(mNewGroup, mNewGroup.getParent());
// Commit to disk
SaveDB save = new SaveDB(ctx, mDb, mFinish, mDontSave);
save.run();
}
private class AfterAdd extends OnFinish {
AfterAdd(OnFinish finish) {
super(finish);
}
@Override
public void run() {
if ( !mSuccess ) {
mDb.removeGroupFrom(mNewGroup, mNewGroup.getParent());
}
// TODO Better callback
AfterActionNodeOnFinish afterAddNode =
(AfterActionNodeOnFinish) super.mOnFinish;
afterAddNode.mSuccess = mSuccess;
afterAddNode.mMessage = mMessage;
afterAddNode.run(null, mNewGroup);
}
}
}
@@ -1,63 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwDatabase;
import com.kunzisoft.keepass.utils.UriUtil;
public class CreateDB extends RunnableOnFinish {
private String mFilename;
private boolean mDontSave;
private Context ctx;
public CreateDB(Context ctx, String filename, OnFinish finish, boolean dontSave) {
super(finish);
mFilename = filename;
mDontSave = dontSave;
this.ctx = ctx;
}
@Override
public void run() {
// Create new database record
Database db = new Database();
App.setDB(db);
PwDatabase pm = PwDatabase.getNewDBInstance(mFilename);
pm.initNew(mFilename);
// Set Database state
db.setPwDatabase(pm);
db.setUri(UriUtil.parseDefaultFile(mFilename));
db.setLoaded();
App.clearShutdown();
// Commit changes
SaveDB save = new SaveDB(ctx, db, mFinish, mDontSave);
mFinish = null;
save.run();
}
}
@@ -1,103 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwEntry;
import com.kunzisoft.keepass.database.PwGroup;
/** Task to delete entries
* @author bpellin
*
*/
public class DeleteEntry extends RunnableOnFinish {
private Database mDb;
private PwEntry mEntry;
private boolean mDontSave;
private Context ctx;
public DeleteEntry(Context ctx, Database db, PwEntry entry, OnFinish finish) {
this(ctx, db, entry, finish, false);
}
public DeleteEntry(Context ctx, Database db, PwEntry entry, OnFinish finish, boolean dontSave) {
super(finish);
this.mDb = db;
this.mEntry = entry;
this.mDontSave = dontSave;
this.ctx = ctx;
}
@Override
public void run() {
PwGroup parent = mEntry.getParent();
// Remove Entry from parent
boolean recycle = mDb.canRecycle(mEntry);
if (recycle) {
mDb.recycle(mEntry);
}
else {
mDb.deleteEntry(mEntry);
}
// Save
mFinish = new AfterDelete(mFinish, parent, mEntry, recycle);
// Commit database
SaveDB save = new SaveDB(ctx, mDb, mFinish, mDontSave);
save.run();
}
private class AfterDelete extends OnFinish {
private PwGroup mParent;
private PwEntry mEntry;
private boolean recycled;
AfterDelete(OnFinish finish, PwGroup parent, PwEntry entry, boolean r) {
super(finish);
mParent = parent;
mEntry = entry;
recycled = r;
}
@Override
public void run() {
if ( !mSuccess ) {
if (recycled) {
mDb.undoRecycle(mEntry, mParent);
}
else {
mDb.undoDeleteEntry(mEntry, mParent);
}
}
// TODO Callback after delete entry
super.run();
}
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.net.Uri;
import java.io.Serializable;
public class FileOnFinish extends OnFinish implements Serializable {
private Uri mFilename = null;
protected FileOnFinish mOnFinish;
public FileOnFinish(FileOnFinish finish) {
super(finish);
mOnFinish = finish;
}
public void setFilename(Uri filename) {
mFilename = filename;
}
public Uri getFilename() {
return mFilename;
}
}
@@ -1,125 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.exception.ArcFourException;
import com.kunzisoft.keepass.database.exception.ContentFileNotFoundException;
import com.kunzisoft.keepass.database.exception.InvalidAlgorithmException;
import com.kunzisoft.keepass.database.exception.InvalidDBException;
import com.kunzisoft.keepass.database.exception.InvalidDBSignatureException;
import com.kunzisoft.keepass.database.exception.InvalidDBVersionException;
import com.kunzisoft.keepass.database.exception.InvalidKeyFileException;
import com.kunzisoft.keepass.database.exception.InvalidPasswordException;
import com.kunzisoft.keepass.database.exception.KeyFileEmptyException;
import java.io.FileNotFoundException;
import java.io.IOException;
public class LoadDB extends RunnableOnFinish {
private Uri mUri;
private String mPass;
private Uri mKey;
private Database mDb;
private Context mCtx;
private boolean mRememberKeyfile;
public LoadDB(Database db, Context ctx, Uri uri, String pass, Uri key, OnFinish finish) {
super(finish);
mDb = db;
mCtx = ctx;
mUri = uri;
mPass = pass;
mKey = key;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
mRememberKeyfile = prefs.getBoolean(ctx.getString(R.string.keyfile_key), ctx.getResources().getBoolean(R.bool.keyfile_default));
}
@Override
public void run() {
try {
mDb.loadData(mCtx, mUri, mPass, mKey, mStatus);
saveFileData(mUri, mKey);
} catch (ArcFourException e) {
finish(false, mCtx.getString(R.string.error_arc4));
return;
} catch (InvalidPasswordException e) {
finish(false, mCtx.getString(R.string.InvalidPassword));
return;
} catch (ContentFileNotFoundException e) {
finish(false, mCtx.getString(R.string.file_not_found_content));
return;
} catch (FileNotFoundException e) {
finish(false, mCtx.getString(R.string.file_not_found));
return;
} catch (IOException e) {
finish(false, e.getMessage());
return;
} catch (KeyFileEmptyException e) {
finish(false, mCtx.getString(R.string.keyfile_is_empty));
return;
} catch (InvalidAlgorithmException e) {
finish(false, mCtx.getString(R.string.invalid_algorithm));
return;
} catch (InvalidKeyFileException e) {
finish(false, mCtx.getString(R.string.keyfile_does_not_exist));
return;
} catch (InvalidDBSignatureException e) {
finish(false, mCtx.getString(R.string.invalid_db_sig));
return;
} catch (InvalidDBVersionException e) {
finish(false, mCtx.getString(R.string.unsupported_db_version));
return;
} catch (InvalidDBException e) {
finish(false, mCtx.getString(R.string.error_invalid_db));
return;
} catch (OutOfMemoryError e) {
finish(false, mCtx.getString(R.string.error_out_of_memory));
return;
} catch (Exception e) {
finish(false, e.getMessage());
return;
}
finish(true);
}
private void saveFileData(Uri uri, Uri key) {
if ( ! mRememberKeyfile ) {
key = null;
}
App.getFileHistory().createFile(uri, key);
}
}
@@ -1,69 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.exception.PwDbOutputException;
import java.io.IOException;
public class SaveDB extends RunnableOnFinish {
private Database mDb;
private boolean mDontSave;
private Context mCtx;
public SaveDB(Context ctx, Database db, OnFinish finish, boolean dontSave) {
super(finish);
mDb = db;
mDontSave = dontSave;
mCtx = ctx;
}
public SaveDB(Context ctx, Database db, OnFinish finish) {
super(finish);
mDb = db;
mDontSave = false;
mCtx = ctx;
}
@Override
public void run() {
if ( ! mDontSave ) {
try {
mDb.saveData(mCtx);
} catch (IOException e) {
finish(false, e.getMessage());
return;
} catch (PwDbOutputException e) {
// TODO: Restore
finish(false, e.getMessage());
return;
}
}
finish(true);
}
}
@@ -1,128 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwDatabase;
import com.kunzisoft.keepass.database.exception.InvalidKeyFileException;
import com.kunzisoft.keepass.dialogs.PasswordEncodingDialogHelper;
import com.kunzisoft.keepass.utils.UriUtil;
import java.io.IOException;
import java.io.InputStream;
public class SetPassword extends RunnableOnFinish {
private String mPassword;
private Uri mKeyfile;
private Database mDb;
private boolean mDontSave;
private Context ctx;
public SetPassword(Context ctx, Database db, String password, Uri keyfile, OnFinish finish) {
this(ctx, db, password, keyfile, finish, false);
}
public SetPassword(Context ctx, Database db, String password, Uri keyfile, OnFinish finish, boolean dontSave) {
super(finish);
mDb = db;
mPassword = password;
mKeyfile = keyfile;
mDontSave = dontSave;
this.ctx = ctx;
}
public boolean validatePassword(Context ctx, DialogInterface.OnClickListener onclick) {
if (!mDb.getPwDatabase().validatePasswordEncoding(mPassword)) {
PasswordEncodingDialogHelper dialog = new PasswordEncodingDialogHelper();
dialog.show(ctx, onclick, true);
return false;
}
return true;
}
@Override
public void run() {
PwDatabase pm = mDb.getPwDatabase();
byte[] backupKey = new byte[pm.getMasterKey().length];
System.arraycopy(pm.getMasterKey(), 0, backupKey, 0, backupKey.length);
// Set key
try {
InputStream is = UriUtil.getUriInputStream(ctx, mKeyfile);
pm.setMasterKey(mPassword, is);
} catch (InvalidKeyFileException e) {
erase(backupKey);
finish(false, e.getMessage());
return;
} catch (IOException e) {
erase(backupKey);
finish(false, e.getMessage());
return;
}
// Save Database
mFinish = new AfterSave(backupKey, mFinish);
SaveDB save = new SaveDB(ctx, mDb, mFinish, mDontSave);
save.run();
}
private class AfterSave extends OnFinish {
private byte[] mBackup;
public AfterSave(byte[] backup, OnFinish finish) {
super(finish);
mBackup = backup;
}
@Override
public void run() {
if ( ! mSuccess ) {
// Erase the current master key
erase(mDb.getPwDatabase().getMasterKey());
mDb.getPwDatabase().setMasterKey(mBackup);
}
super.run();
}
}
/** Overwrite the array as soon as we don't need it to avoid keeping the extra data in memory
* @param array
*/
private void erase(byte[] array) {
if ( array == null ) return;
for ( int i = 0; i < array.length; i++ ) {
array[i] = 0;
}
}
}
@@ -1,84 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwEntry;
public class UpdateEntry extends RunnableOnFinish {
private Database mDb;
private PwEntry mOldE;
private PwEntry mNewE;
private Context ctx;
private boolean mDontSave;
public UpdateEntry(Context ctx, Database db, PwEntry oldE, PwEntry newE, OnFinish finish) {
this(ctx, db, oldE, newE, finish, false);
}
public UpdateEntry(Context ctx, Database db, PwEntry oldE, PwEntry newE, OnFinish finish, boolean dontSave) {
super(finish);
this.mDb = db;
this.mOldE = oldE;
this.mNewE = newE;
this.ctx = ctx;
this.mDontSave = dontSave;
// Keep backup of original values in case save fails
PwEntry backup;
backup = mOldE.clone();
mFinish = new AfterUpdate(backup, finish);
}
@Override
public void run() {
// Update entry with new values
mDb.updateEntry(mOldE, mNewE);
mOldE.touch(true, true);
// Commit to disk
SaveDB save = new SaveDB(ctx, mDb, mFinish, mDontSave);
save.run();
}
private class AfterUpdate extends OnFinish {
private PwEntry mBackup;
AfterUpdate(PwEntry backup, OnFinish finish) {
super(finish);
mBackup = backup;
}
@Override
public void run() {
if ( !mSuccess ) {
// If we fail to save, back out changes to global structure
mDb.updateEntry(mOldE, mBackup);
}
// TODO Callback for update entry
super.run();
}
}
}
@@ -1,88 +0,0 @@
/*
* Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX 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.
*
* KeePass DX 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 KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.database.edit;
import android.content.Context;
import com.kunzisoft.keepass.database.Database;
import com.kunzisoft.keepass.database.PwGroup;
public class UpdateGroup extends RunnableOnFinish {
private Database mDb;
private PwGroup mOldGroup;
private PwGroup mNewGroup;
private Context ctx;
private boolean mDontSave;
public UpdateGroup(Context ctx, Database db, PwGroup oldGroup, PwGroup newGroup, AfterActionNodeOnFinish finish) {
this(ctx, db, oldGroup, newGroup, finish, false);
}
public UpdateGroup(Context ctx, Database db, PwGroup oldGroup, PwGroup newGroup, AfterActionNodeOnFinish finish, boolean dontSave) {
super(finish);
this.mDb = db;
this.mOldGroup = oldGroup;
this.mNewGroup = newGroup;
this.ctx = ctx;
this.mDontSave = dontSave;
// Keep backup of original values in case save fails
PwGroup backup;
backup = mOldGroup.clone();
this.mFinish = new AfterUpdate(backup, finish);
}
@Override
public void run() {
// Update group with new values
mDb.updateGroup(mOldGroup, mNewGroup);
mOldGroup.touch(true, true);
// Commit to disk
new SaveDB(ctx, mDb, mFinish, mDontSave).run();
}
private class AfterUpdate extends OnFinish {
private PwGroup mBackup;
AfterUpdate(PwGroup backup, OnFinish finish) {
super(finish);
mBackup = backup;
}
@Override
public void run() {
if ( !mSuccess ) {
// If we fail to save, back out changes to global structure
mDb.updateGroup(mOldGroup, mBackup);
}
// TODO Better callback
AfterActionNodeOnFinish afterActionNodeOnFinish =
(AfterActionNodeOnFinish) super.mOnFinish;
afterActionNodeOnFinish.mSuccess = mSuccess;
afterActionNodeOnFinish.mMessage = mMessage;
afterActionNodeOnFinish.run(mOldGroup, mNewGroup);
}
}
}