Added option to export database

Fixed problem with native key transform
This commit is contained in:
Philipp Crocoll
2014-01-27 22:47:08 -08:00
parent d7109fc630
commit 2d53021f78
19 changed files with 5739 additions and 2177 deletions

View File

@@ -0,0 +1,119 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2013 Dominik Reichl <dominik.reichl@t-online.de>
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; either version 2 of the License, or
(at your option) any later version.
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using KeePassLib;
using KeePassLib.Interfaces;
namespace KeePass.DataExchange
{
public abstract class FileFormatProvider
{
public abstract bool SupportsImport { get; }
public abstract bool SupportsExport { get; }
public abstract string FormatName { get; }
public virtual string DisplayName
{
get { return this.FormatName; }
}
/// <summary>
/// Default file name extension, without leading dot.
/// If there are multiple default/equivalent extensions
/// (like e.g. "html" and "htm"), specify all of them
/// separated by a '|' (e.g. "html|htm").
/// </summary>
public virtual string DefaultExtension
{
get { return string.Empty; }
}
public virtual bool RequiresFile
{
get { return true; }
}
public virtual bool SupportsUuids
{
get { return false; }
}
public virtual bool RequiresKey
{
get { return false; }
}
/// <summary>
/// This property specifies if entries are only appended to the
/// end of the root group. This is true for example if the
/// file format doesn't support groups (i.e. no hierarchy).
/// </summary>
public virtual bool ImportAppendsToRootGroupOnly
{
get { return false; }
}
/// <summary>
/// Called before the <c>Export</c> method is invoked.
/// </summary>
/// <returns>Returns <c>true</c>, if the <c>Export</c> method
/// can be invoked. If it returns <c>false</c>, something has
/// failed and the export process should be aborted.</returns>
public virtual bool TryBeginExport()
{
return true;
}
/// <summary>
/// Import a stream into a database. Throws an exception if an error
/// occurs. Do not call the base class method when overriding it.
/// </summary>
/// <param name="pwStorage">Data storage into which the data will be imported.</param>
/// <param name="sInput">Input stream to read the data from.</param>
/// <param name="slLogger">Status logger. May be <c>null</c>.</param>
public abstract void Import(PwDatabase pwStorage, Stream sInput,
IStatusLogger slLogger);
/// <summary>
/// Export data into a stream. Throws an exception if an error
/// occurs (like writing to stream fails, etc.). Returns <c>true</c>,
/// if the export was successful.
/// </summary>
/// <param name="pwExportInfo">Contains the data source and detailed
/// information about which entries should be exported.</param>
/// <param name="sOutput">Output stream to write the data to.</param>
/// <param name="slLogger">Status logger. May be <c>null</c>.</param>
/// <returns>Returns <c>false</c>, if the user has aborted the export
/// process (like clicking Cancel in an additional export settings
/// dialog).</returns>
public virtual bool Export(PwExportInfo pwExportInfo, Stream sOutput,
IStatusLogger slLogger)
{
throw new NotSupportedException();
}
}
}

View File

@@ -0,0 +1,183 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2013 Dominik Reichl <dominik.reichl@t-online.de>
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; either version 2 of the License, or
(at your option) any later version.
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System.Diagnostics;
using System.IO;
using KeePassLib;
using KeePassLib.Delegates;
using KeePassLib.Interfaces;
using KeePassLib.Utility;
namespace KeePass.DataExchange.Formats
{
public sealed class KeePassCsv1x : FileFormatProvider
{
public override bool SupportsImport { get { return false; } }
public override bool SupportsExport { get { return true; } }
public override string FormatName { get { return "KeePass CSV (1.x)"; } }
public override string DefaultExtension { get { return "csv"; } }
// public override bool ImportAppendsToRootGroupOnly { get { return true; } }
/* public override void Import(PwDatabase pwStorage, Stream sInput,
IStatusLogger slLogger)
{
StreamReader sr = new StreamReader(sInput, Encoding.UTF8);
string strFileContents = sr.ReadToEnd();
sr.Close();
CharStream csSource = new CharStream(strFileContents);
while(true)
{
if(ReadEntry(pwStorage, csSource) == false)
break;
}
}
private static bool ReadEntry(PwDatabase pwStorage, CharStream csSource)
{
PwEntry pe = new PwEntry(true, true);
string strTitle = ReadCsvField(csSource);
if(strTitle == null) return false; // No entry available
string strUser = ReadCsvField(csSource);
if(strUser == null) throw new InvalidDataException();
string strPassword = ReadCsvField(csSource);
if(strPassword == null) throw new InvalidDataException();
string strUrl = ReadCsvField(csSource);
if(strUrl == null) throw new InvalidDataException();
string strNotes = ReadCsvField(csSource);
if(strNotes == null) throw new InvalidDataException();
if((strTitle == "Account") && (strUser == "Login Name") &&
(strPassword == "Password") && (strUrl == "Web Site") &&
(strNotes == "Comments"))
{
return true; // Ignore header entry
}
pe.Strings.Set(PwDefs.TitleField, new ProtectedString(
pwStorage.MemoryProtection.ProtectTitle, strTitle));
pe.Strings.Set(PwDefs.UserNameField, new ProtectedString(
pwStorage.MemoryProtection.ProtectUserName, strUser));
pe.Strings.Set(PwDefs.PasswordField, new ProtectedString(
pwStorage.MemoryProtection.ProtectPassword, strPassword));
pe.Strings.Set(PwDefs.UrlField, new ProtectedString(
pwStorage.MemoryProtection.ProtectUrl, strUrl));
pe.Strings.Set(PwDefs.NotesField, new ProtectedString(
pwStorage.MemoryProtection.ProtectNotes, strNotes));
pwStorage.RootGroup.AddEntry(pe, true);
return true;
}
private static string ReadCsvField(CharStream csSource)
{
StringBuilder sb = new StringBuilder();
bool bInField = false;
while(true)
{
char ch = csSource.ReadChar();
if(ch == char.MinValue)
return null;
if((ch == '\"') && !bInField)
bInField = true;
else if((ch == '\"') && bInField)
break;
else if(ch == '\\')
{
char chSub = csSource.ReadChar();
if(chSub == char.MinValue)
throw new InvalidDataException();
sb.Append(chSub);
}
else if(bInField)
sb.Append(ch);
}
return sb.ToString();
} */
public override void Import(PwDatabase pwStorage, Stream sInput, IStatusLogger slLogger)
{
throw new System.NotImplementedException();
}
public override bool Export(PwExportInfo pwExportInfo, Stream sOutput,
IStatusLogger slLogger)
{
PwGroup pg = (pwExportInfo.DataGroup ?? ((pwExportInfo.ContextDatabase !=
null) ? pwExportInfo.ContextDatabase.RootGroup : null));
StreamWriter sw = new StreamWriter(sOutput, StrUtil.Utf8);
sw.Write("\"Account\",\"Login Name\",\"Password\",\"Web Site\",\"Comments\"\r\n");
EntryHandler eh = delegate(PwEntry pe)
{
WriteCsvEntry(sw, pe);
return true;
};
if(pg != null) pg.TraverseTree(TraversalMethod.PreOrder, null, eh);
sw.Close();
return true;
}
private static void WriteCsvEntry(StreamWriter sw, PwEntry pe)
{
if(sw == null) { Debug.Assert(false); return; }
if(pe == null) { Debug.Assert(false); return; }
const string strSep = "\",\"";
sw.Write("\"");
WriteCsvString(sw, pe.Strings.ReadSafe(PwDefs.TitleField), strSep);
WriteCsvString(sw, pe.Strings.ReadSafe(PwDefs.UserNameField), strSep);
WriteCsvString(sw, pe.Strings.ReadSafe(PwDefs.PasswordField), strSep);
WriteCsvString(sw, pe.Strings.ReadSafe(PwDefs.UrlField), strSep);
WriteCsvString(sw, pe.Strings.ReadSafe(PwDefs.NotesField), "\"\r\n");
}
private static void WriteCsvString(StreamWriter sw, string strText,
string strAppend)
{
string str = strText;
if(!string.IsNullOrEmpty(str))
{
str = str.Replace("\\", "\\\\");
str = str.Replace("\"", "\\\"");
sw.Write(str);
}
if(!string.IsNullOrEmpty(strAppend)) sw.Write(strAppend);
}
}
}

View File

@@ -0,0 +1,60 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2013 Dominik Reichl <dominik.reichl@t-online.de>
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; either version 2 of the License, or
(at your option) any later version.
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using KeePassLib;
using KeePassLib.Interfaces;
using KeePassLib.Serialization;
namespace KeePass.DataExchange.Formats
{
public sealed class KeePassKdb2x : FileFormatProvider
{
public override bool SupportsImport { get { return true; } }
public override bool SupportsExport { get { return true; } }
public override string FormatName { get { return "KeePass KDBX (2.x)"; } }
public override string DefaultExtension { get { return "kdbx"; } }
public override bool SupportsUuids { get { return true; } }
public override bool RequiresKey { get { return true; } }
public override void Import(PwDatabase pwStorage, Stream sInput,
IStatusLogger slLogger)
{
KdbxFile kdbx = new KdbxFile(pwStorage);
kdbx.Load(sInput, KdbxFormat.Default, slLogger);
}
public override bool Export(PwExportInfo pwExportInfo, Stream sOutput,
IStatusLogger slLogger)
{
KdbxFile kdbx = new KdbxFile(pwExportInfo.ContextDatabase);
kdbx.Save(sOutput, pwExportInfo.DataGroup, KdbxFormat.Default, slLogger);
return true;
}
}
}

View File

@@ -0,0 +1,72 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2013 Dominik Reichl <dominik.reichl@t-online.de>
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; either version 2 of the License, or
(at your option) any later version.
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using KeePassLib;
using KeePassLib.Collections;
using KeePassLib.Interfaces;
using KeePassLib.Serialization;
namespace KeePass.DataExchange.Formats
{
public sealed class KeePassXml2x : FileFormatProvider
{
public override bool SupportsImport { get { return true; } }
public override bool SupportsExport { get { return true; } }
public override string FormatName { get { return "KeePass XML (2.x)"; } }
public override string DefaultExtension { get { return "xml"; } }
public override bool SupportsUuids { get { return true; } }
public override void Import(PwDatabase pwStorage, Stream sInput,
IStatusLogger slLogger)
{
KdbxFile kdbx = new KdbxFile(pwStorage);
kdbx.Load(sInput, KdbxFormat.PlainXml, slLogger);
}
public override bool Export(PwExportInfo pwExportInfo, Stream sOutput,
IStatusLogger slLogger)
{
PwDatabase pd = (pwExportInfo.ContextDatabase ?? new PwDatabase());
PwObjectList<PwDeletedObject> vDel = null;
if(pwExportInfo.ExportDeletedObjects == false)
{
vDel = pd.DeletedObjects.CloneShallow();
pd.DeletedObjects.Clear();
}
KdbxFile kdb = new KdbxFile(pd);
kdb.Save(sOutput, pwExportInfo.DataGroup, KdbxFormat.PlainXml, slLogger);
// Restore deleted objects list
if(vDel != null) pd.DeletedObjects.Add(vDel);
return true;
}
}
}

View File

@@ -0,0 +1,82 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2013 Dominik Reichl <dominik.reichl@t-online.de>
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; either version 2 of the License, or
(at your option) any later version.
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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using KeePassLib;
namespace KeePass.DataExchange
{
public sealed class PwExportInfo
{
private PwGroup m_pg;
/// <summary>
/// This group contains all entries and subgroups that should
/// be exported. Is never <c>null</c>.
/// </summary>
public PwGroup DataGroup
{
get { return m_pg; }
}
private PwDatabase m_pd;
/// <summary>
/// Optional context database reference. May be <c>null</c>.
/// </summary>
public PwDatabase ContextDatabase
{
get { return m_pd; }
}
private bool m_bExpDel = true;
/// <summary>
/// Indicates whether deleted objects should be exported, if
/// the data format supports it.
/// </summary>
public bool ExportDeletedObjects
{
get { return m_bExpDel; }
}
public PwExportInfo(PwGroup pgDataSource, PwDatabase pwContextInfo)
{
ConstructEx(pgDataSource, pwContextInfo, null);
}
public PwExportInfo(PwGroup pgDataSource, PwDatabase pwContextInfo,
bool bExportDeleted)
{
ConstructEx(pgDataSource, pwContextInfo, bExportDeleted);
}
private void ConstructEx(PwGroup pgDataSource, PwDatabase pwContextInfo,
bool? bExportDeleted)
{
if(pgDataSource == null) throw new ArgumentNullException("pgDataSource");
// pwContextInfo may be null.
m_pg = pgDataSource;
m_pd = pwContextInfo;
if(bExportDeleted.HasValue) m_bExpDel = bExportDeleted.Value;
}
}
}

View File

@@ -20,7 +20,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;EXCLUDE_TWOFISH;INCLUDE_KEYBOARD;EXCLUDE_KEYTRANSFORM;EXCLUDE_FILECHOOSER;EXCLUDE_JAVAFILESTORAGE</DefineConstants>
<DefineConstants>TRACE;DEBUG;EXCLUDE_TWOFISH;INCLUDE_KEYBOARD;INCLUDE_FILECHOOSER;EXCLUDE_JAVAFILESTORAGE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
@@ -59,6 +59,11 @@
<Compile Include="database\KdbDatabaseLoader.cs" />
<Compile Include="database\KdbxDatabaseLoader.cs" />
<Compile Include="database\SynchronizeCachedDatabase.cs" />
<Compile Include="DataExchange\FileFormatProvider.cs" />
<Compile Include="DataExchange\Formats\KeePassCsv1x.cs" />
<Compile Include="DataExchange\Formats\KeePassKdb2x.cs" />
<Compile Include="DataExchange\Formats\KeePassXml2x.cs" />
<Compile Include="DataExchange\PwExportInfo.cs" />
<Compile Include="Io\BuiltInFileStorage.cs" />
<Compile Include="Io\CachingFileStorage.cs" />
<Compile Include="Io\DropboxFileStorage.cs" />

View File

@@ -47,6 +47,7 @@ namespace keepass2android
ErrorOcurred,
SynchronizingOtpAuxFile,
SavingOtpAuxFile,
CertificateFailure
CertificateFailure,
exporting_database
}
}