File: ExportUtil.cs

package info (click to toggle)
keepass2 2.47%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,992 kB
  • sloc: cs: 111,053; xml: 5,956; cpp: 308; makefile: 48; sh: 48
file content (277 lines) | stat: -rw-r--r-- 8,267 bytes parent folder | download
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
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2021 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.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

using KeePass.App;
using KeePass.Forms;
using KeePass.Resources;
using KeePass.UI;
using KeePass.Util;

using KeePassLib;
using KeePassLib.Delegates;
using KeePassLib.Interfaces;
using KeePassLib.Keys;
using KeePassLib.Serialization;
using KeePassLib.Utility;

using NativeLib = KeePassLib.Native.NativeLib;

namespace KeePass.DataExchange
{
	public static class ExportUtil
	{
		public static bool Export(PwExportInfo pwExportInfo, IStatusLogger slLogger)
		{
			if(pwExportInfo == null) throw new ArgumentNullException("pwExportInfo");
			if(pwExportInfo.DataGroup == null) throw new ArgumentException();

			if(!AppPolicy.Try(AppPolicyId.Export)) return false;

			ExchangeDataForm dlg = new ExchangeDataForm();
			dlg.InitEx(true, pwExportInfo.ContextDatabase, pwExportInfo.DataGroup);
			dlg.ExportInfo = pwExportInfo;

			bool bStatusActive = false;

			try
			{
				if(dlg.ShowDialog() == DialogResult.OK)
				{
					FileFormatProvider ffp = dlg.ResultFormat;
					if(ffp == null) { Debug.Assert(false); return false; }

					IOConnectionInfo ioc = null;
					if(ffp.RequiresFile)
					{
						string[] vFiles = dlg.ResultFiles;
						if(vFiles == null) { Debug.Assert(false); return false; }
						if(vFiles.Length == 0) { Debug.Assert(false); return false; }
						Debug.Assert(vFiles.Length == 1);

						string strFile = vFiles[0];
						if(string.IsNullOrEmpty(strFile)) { Debug.Assert(false); return false; }

						ioc = IOConnectionInfo.FromPath(strFile);
					}

					if(slLogger != null)
					{
						slLogger.StartLogging(KPRes.ExportingStatusMsg, true);
						bStatusActive = true;
					}

					Application.DoEvents(); // Redraw parent window
					return Export(pwExportInfo, ffp, ioc, slLogger);
				}
			}
			catch(Exception ex) { MessageService.ShowWarning(ex); }
			finally
			{
				UIUtil.DestroyForm(dlg);
				if(bStatusActive) slLogger.EndLogging();
			}

			return false;
		}

		public static bool Export(PwExportInfo pwExportInfo, string strFormatName,
			IOConnectionInfo iocOutput)
		{
			if(strFormatName == null) throw new ArgumentNullException("strFormatName");
			// iocOutput may be null

			FileFormatProvider ffp = Program.FileFormatPool.Find(strFormatName);
			if(ffp == null) return false;

			NullStatusLogger slLogger = new NullStatusLogger();
			return Export(pwExportInfo, ffp, iocOutput, slLogger);
		}

		public static bool Export(PwExportInfo pwExportInfo, FileFormatProvider fileFormat,
			IOConnectionInfo iocOutput, IStatusLogger slLogger)
		{
			if(pwExportInfo == null) throw new ArgumentNullException("pwExportInfo");
			if(pwExportInfo.DataGroup == null) throw new ArgumentException();
			if(fileFormat == null) throw new ArgumentNullException("fileFormat");

			bool bFileReq = fileFormat.RequiresFile;
			if(bFileReq && (iocOutput == null))
				throw new ArgumentNullException("iocOutput");
			if(bFileReq && (iocOutput.Path.Length == 0))
				throw new ArgumentException();

			PwDatabase pd = pwExportInfo.ContextDatabase;
			Debug.Assert(pd != null);

			if(!AppPolicy.Try(AppPolicyId.Export)) return false;
			if(!AppPolicy.Current.ExportNoKey && (pd != null))
			{
				if(!KeyUtil.ReAskKey(pd, true)) return false;
			}

			if(!fileFormat.SupportsExport) return false;
			if(!fileFormat.TryBeginExport()) return false;

			CompositeKey ckOrgMasterKey = null;
			DateTime dtOrgMasterKey = PwDefs.DtDefaultNow;

			PwGroup pgOrgData = pwExportInfo.DataGroup;
			PwGroup pgOrgRoot = ((pd != null) ? pd.RootGroup : null);
			bool bParentGroups = (pwExportInfo.ExportParentGroups && (pd != null) &&
				(pgOrgData != pgOrgRoot));

			bool bExistedAlready = true; // No deletion by default
			bool bResult = false;

			try
			{
				if(pwExportInfo.ExportMasterKeySpec && fileFormat.RequiresKey &&
					(pd != null))
				{
					KeyCreationForm kcf = new KeyCreationForm();
					kcf.InitEx((iocOutput ?? new IOConnectionInfo()), true);

					if(UIUtil.ShowDialogNotValue(kcf, DialogResult.OK)) return false;

					ckOrgMasterKey = pd.MasterKey;
					dtOrgMasterKey = pd.MasterKeyChanged;

					pd.MasterKey = kcf.CompositeKey;
					pd.MasterKeyChanged = DateTime.UtcNow;

					UIUtil.DestroyForm(kcf);
				}

				if(bParentGroups)
				{
					PwGroup pgNew = WithParentGroups(pgOrgData, pd);
					pwExportInfo.DataGroup = pgNew;
					pd.RootGroup = pgNew;
				}

				if(bFileReq) bExistedAlready = IOConnection.FileExists(iocOutput);

				Stream s = (bFileReq ? IOConnection.OpenWrite(iocOutput) : null);
				try { bResult = fileFormat.Export(pwExportInfo, s, slLogger); }
				finally { if(s != null) s.Close(); }

				if(bFileReq && bResult)
				{
					if(pwExportInfo.ExportPostOpen)
						NativeLib.StartProcess(iocOutput.Path);
					if(pwExportInfo.ExportPostShow)
						WinUtil.ShowFileInFileManager(iocOutput.Path, true);
				}
			}
			catch(Exception ex) { MessageService.ShowWarning(ex); }
			finally
			{
				if(ckOrgMasterKey != null)
				{
					pd.MasterKey = ckOrgMasterKey;
					pd.MasterKeyChanged = dtOrgMasterKey;
				}

				if(bParentGroups)
				{
					pwExportInfo.DataGroup = pgOrgData;
					pd.RootGroup = pgOrgRoot;
				}
			}

			if(bFileReq && !bResult && !bExistedAlready)
			{
				try { IOConnection.DeleteFile(iocOutput); }
				catch(Exception) { }
			}

			return bResult;
		}

		private static PwGroup WithParentGroups(PwGroup pg, PwDatabase pd)
		{
			if(pg == null) { Debug.Assert(false); return null; }
			if(pd == null) { Debug.Assert(false); return pg; }

			Dictionary<PwUuid, bool> dUuids = CollectUuids(pg);

			PwGroup pgNew = FilterCloneGroup(pd.RootGroup, dUuids);
			Debug.Assert(pgNew.GetEntriesCount(true) == pg.GetEntriesCount(true));
			return pgNew;
		}

		private static Dictionary<PwUuid, bool> CollectUuids(PwGroup pg)
		{
			Dictionary<PwUuid, bool> d = new Dictionary<PwUuid, bool>();

			Action<IStructureItem> fAdd = delegate(IStructureItem it)
			{
				if(it == null) { Debug.Assert(false); return; }

				Debug.Assert(!d.ContainsKey(it.Uuid));
				d[it.Uuid] = true;

				PwGroup pgParent = it.ParentGroup;
				while(pgParent != null)
				{
					d[pgParent.Uuid] = true;
					pgParent = pgParent.ParentGroup;
				}
			};

			GroupHandler gh = delegate(PwGroup pgCur) { fAdd(pgCur); return true; };
			EntryHandler eh = delegate(PwEntry peCur) { fAdd(peCur); return true; };

			fAdd(pg);
			pg.TraverseTree(TraversalMethod.PreOrder, gh, eh);

			return d;
		}

		private static PwGroup FilterCloneGroup(PwGroup pg, Dictionary<PwUuid, bool> dUuids)
		{
			PwGroup pgNew = new PwGroup();
			pgNew.Uuid = pg.Uuid;
			pgNew.AssignProperties(pg, false, true);
			Debug.Assert(pgNew.EqualsGroup(pg, (PwCompareOptions.IgnoreParentGroup |
				PwCompareOptions.PropertiesOnly), MemProtCmpMode.Full));

			foreach(PwEntry pe in pg.Entries)
			{
				if(dUuids.ContainsKey(pe.Uuid))
					pgNew.AddEntry(pe.CloneDeep(), true, false);
			}

			foreach(PwGroup pgSub in pg.Groups)
			{
				if(dUuids.ContainsKey(pgSub.Uuid))
					pgNew.AddGroup(FilterCloneGroup(pgSub, dUuids), true, false);
			}

			return pgNew;
		}
	}
}