File: AppConfigSerializer.cs

package info (click to toggle)
keepass2 2.57%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,516 kB
  • sloc: cs: 120,930; xml: 6,271; cpp: 322; sh: 53; makefile: 49
file content (475 lines) | stat: -rw-r--r-- 13,387 bytes parent folder | download | duplicates (3)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2024 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.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;

using KeePass.UI;
using KeePass.Util;
using KeePass.Util.XmlSerialization;

using KeePassLib;
using KeePassLib.Serialization;
using KeePassLib.Utility;

namespace KeePass.App.Configuration
{
	public static class AppConfigSerializer
	{
		private static string g_strBaseName = null; // Null allowed

		private static string g_strAppDataDir = null;
		private static string g_strAppDataLocalDir = null;
		private static string g_strEnforcedConfigFile = null;
		private static string g_strGlobalConfigFile = null;
		private static string g_strUserConfigFile = null;

		private const string g_strFileSuffix = ".config.xml";
		private const string g_strFileEnfSuffix = ".config.enforced.xml";

		/// <summary>
		/// Get our folder in the application data directory.
		/// </summary>
		public static string AppDataDirectory
		{
			get { GetConfigPaths(); return g_strAppDataDir; }
		}

		/// <summary>
		/// Get our folder in the local application data directory.
		/// </summary>
		public static string LocalAppDataDirectory
		{
			get { GetConfigPaths(); return g_strAppDataLocalDir; }
		}

		internal static string EnforcedConfigFile
		{
			get { GetConfigPaths(); return g_strEnforcedConfigFile; }
		}

		/// <summary>
		/// Get/set the base name for the configuration. If this property is
		/// <c>null</c>, the class constructs names based on the current
		/// assembly and the product name.
		/// </summary>
		public static string BaseName
		{
			get { return g_strBaseName; }

			set
			{
				g_strBaseName = value;

				// Invalidate paths
				g_strAppDataDir = null;
				g_strAppDataLocalDir = null;
				g_strEnforcedConfigFile = null;
				g_strGlobalConfigFile = null;
				g_strUserConfigFile = null;
			}
		}

		private static XmlDocument g_xdEnforced = null;
		public static XmlDocument EnforcedConfigXml
		{
			get { return g_xdEnforced; }
			internal set { g_xdEnforced = value; }
		}

		private static void GetConfigPaths()
		{
			if(g_strGlobalConfigFile == null)
			{
				Assembly asm = Assembly.GetExecutingAssembly();
				if(asm == null) { Debug.Assert(false); return; }

#if !KeePassLibSD
				string strFile = null;

				try { strFile = asm.Location; }
				catch(Exception) { }

				if(string.IsNullOrEmpty(strFile))
					strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
#else
				string strFile = UrlUtil.FileUrlToPath(asm.GetName().CodeBase);
#endif
				if(string.IsNullOrEmpty(strFile)) { Debug.Assert(false); strFile = string.Empty; }

				if(!string.IsNullOrEmpty(g_strBaseName))
					strFile = UrlUtil.GetFileDirectory(strFile, true, false) + g_strBaseName;
				else
				{
					if(strFile.EndsWith(".exe", StrUtil.CaseIgnoreCmp) ||
						strFile.EndsWith(".dll", StrUtil.CaseIgnoreCmp))
						strFile = strFile.Substring(0, strFile.Length - 4);
				}

				g_strGlobalConfigFile = strFile + g_strFileSuffix;
				g_strEnforcedConfigFile = strFile + g_strFileEnfSuffix;
			}

			if(g_strUserConfigFile == null)
			{
				string strBaseName = (!string.IsNullOrEmpty(g_strBaseName) ?
					g_strBaseName : PwDefs.ShortProductName);

				string strUserDir;
				try
				{
					strUserDir = Environment.GetFolderPath(
						Environment.SpecialFolder.ApplicationData);
				}
				catch(Exception)
				{
					strUserDir = UrlUtil.GetFileDirectory(UrlUtil.FileUrlToPath(
						Assembly.GetExecutingAssembly().GetName().CodeBase), true, false);
				}
				strUserDir = UrlUtil.EnsureTerminatingSeparator(strUserDir, false);

				string strUserDirLocal;
				try
				{
					strUserDirLocal = Environment.GetFolderPath(
						Environment.SpecialFolder.LocalApplicationData);
				}
				catch(Exception) { strUserDirLocal = strUserDir; }
				strUserDirLocal = UrlUtil.EnsureTerminatingSeparator(strUserDirLocal, false);

				g_strAppDataDir = strUserDir + strBaseName;
				g_strAppDataLocalDir = strUserDirLocal + strBaseName;
				g_strUserConfigFile = UrlUtil.EnsureTerminatingSeparator(
					g_strAppDataDir, false) + strBaseName + g_strFileSuffix;

				string strLocalOvr = Program.CommandLineArgs[
					AppDefs.CommandLineOptions.ConfigPathLocal];
				if(!string.IsNullOrEmpty(strLocalOvr))
				{
					string strWD = UrlUtil.EnsureTerminatingSeparator(
						WinUtil.GetWorkingDirectory(), false);
					g_strUserConfigFile = UrlUtil.MakeAbsolutePath(strWD +
						"Sentinel.txt", strLocalOvr);
					// Do not change g_strAppDataDir, as it's returned from
					// the AppDataDirectory property
				}

				Debug.Assert(!string.IsNullOrEmpty(g_strAppDataDir));
			}
		}

		private static void EnsureDirOfFileExists(string strFile)
		{
			if(string.IsNullOrEmpty(strFile)) { Debug.Assert(false); return; }

			try
			{
				string strDir = UrlUtil.GetFileDirectory(strFile, false, true);
				if(!Directory.Exists(strDir))
					Directory.CreateDirectory(strDir);
			}
			catch(Exception) { Debug.Assert(false); }
		}

		internal static XmlDocument LoadEnforced(bool bSetPrimary)
		{
			GetConfigPaths();

			if(bSetPrimary)
			{
				Debug.Assert(g_xdEnforced == null);
				g_xdEnforced = null;
			}

#if DEBUG
			Stopwatch sw = Stopwatch.StartNew();
#endif
			try
			{
				if(!File.Exists(g_strEnforcedConfigFile)) return null;

				XmlDocument xd = XmlUtilEx.CreateXmlDocument();
				xd.Load(g_strEnforcedConfigFile);

				if(bSetPrimary) g_xdEnforced = xd;
				return xd;
			}
			catch(Exception ex)
			{
				FileDialogsEx.ShowConfigError(g_strEnforcedConfigFile, ex, false, false);
			}
#if DEBUG
			finally
			{
				sw.Stop();
			}
#endif

			return null;
		}

		private static AppConfigEx Load(string strFilePath, XmlDocument xdEnforced)
		{
			if(string.IsNullOrEmpty(strFilePath)) { Debug.Assert(false); return null; }

			AppConfigEx cfg = null;
			try
			{
				if(!File.Exists(strFilePath)) return null;

				XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx));

				if(xdEnforced == null)
				{
					using(FileStream fs = new FileStream(strFilePath,
						FileMode.Open, FileAccess.Read, FileShare.Read))
					{
						cfg = (AppConfigEx)xs.Deserialize(fs);
					}
				}
				else // Enforced configuration
				{
					XmlDocument xd = XmlUtilEx.CreateXmlDocument();
					xd.Load(strFilePath);

					XmContext ctx = new XmContext(xd, AppConfigEx.GetNodeOptions,
						AppConfigEx.GetNodeKey);
					XmlUtil.MergeElements(xd.DocumentElement, xdEnforced.DocumentElement,
						"/" + xd.DocumentElement.Name, null, ctx);

					using(MemoryStream msW = new MemoryStream())
					{
						xd.Save(msW);

						using(MemoryStream msR = new MemoryStream(msW.ToArray(), false))
						{
							cfg = (AppConfigEx)xs.Deserialize(msR);
						}
					}
				}
			}
			catch(Exception ex)
			{
				FileDialogsEx.ShowConfigError(strFilePath, ex, false, true);
			}

			if(cfg != null) cfg.OnLoad();
			return cfg;
		}

		public static AppConfigEx Load()
		{
			GetConfigPaths();

			// AppConfigEx cfgEnf = Load(g_strEnforcedConfigFile);
			// if(cfgEnf != null)
			// {
			//	cfgEnf.Meta.IsEnforcedConfiguration = true;
			//	return cfgEnf;
			// }
			XmlDocument xdEnforced = LoadEnforced(true);

			AppConfigEx cfgGlobal = Load(g_strGlobalConfigFile, xdEnforced);
			if((cfgGlobal != null) && !cfgGlobal.Meta.PreferUserConfiguration)
				return cfgGlobal; // Do not show error for corrupted local configuration

			AppConfigEx cfgUser = Load(g_strUserConfigFile, xdEnforced);

			if((cfgGlobal == null) && (cfgUser == null))
			{
				if(xdEnforced != null)
				{
					// Create an empty configuration file and merge it with
					// the enforced configuration; this ensures that merge
					// behaviors (like the node mode 'Remove') work as intended
					try
					{
						string strFile = Program.TempFilesPool.GetTempFileName("xml");
						File.WriteAllText(strFile, AppConfigEx.GetEmptyXml(),
							StrUtil.Utf8);

						AppConfigEx cfg = Load(strFile, xdEnforced);
						if(cfg != null) return cfg;
						Debug.Assert(false);
					}
					catch(Exception) { Debug.Assert(false); }
				}

				AppConfigEx cfgNew = new AppConfigEx();
				cfgNew.OnLoad(); // Create defaults
				return cfgNew;
			}
			if((cfgGlobal != null) && (cfgUser == null)) return cfgGlobal;
			if((cfgGlobal == null) && (cfgUser != null)) return cfgUser;

			cfgUser.Meta.PreferUserConfiguration = cfgGlobal.Meta.PreferUserConfiguration;
			return (cfgGlobal.Meta.PreferUserConfiguration ? cfgUser : cfgGlobal);
		}

		internal static void SaveEx(AppConfigEx cfg, Stream s)
		{
			if(cfg == null) throw new ArgumentNullException("cfg");
			if(s == null) throw new ArgumentNullException("s");

			cfg.OnSavePre();

			// Temporarily remove user file preference (restore after saving)
			bool bPrefUser = cfg.Meta.PreferUserConfiguration;
			cfg.Meta.PreferUserConfiguration = false;

			try
			{
				using(XmlWriter xw = XmlUtilEx.CreateXmlWriter(s))
				{
					XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx));
					xs.Serialize(xw, cfg);
				}
			}
			finally
			{
				cfg.Meta.PreferUserConfiguration = bPrefUser;
				cfg.OnSavePost();
			}
		}

		private static bool Save(AppConfigEx cfg, string strFilePath)
		{
			if(string.IsNullOrEmpty(strFilePath)) { Debug.Assert(false); return false; }

			try
			{
				IOConnectionInfo iocPath = IOConnectionInfo.FromPath(strFilePath);

				using(FileTransactionEx ft = new FileTransactionEx(iocPath,
					cfg.Application.UseTransactedConfigWrites))
				{
					using(Stream s = ft.OpenWrite())
					{
						SaveEx(cfg, s);
					}

					ft.CommitWrite();
				}

				return true;
			}
			catch(Exception ex)
			{
				FileDialogsEx.ShowConfigError(strFilePath, ex, true, false);
			}

			return false;
		}

		public static bool Save(AppConfigEx cfg)
		{
			if(cfg == null) { Debug.Assert(false); return false; }
			if(!cfg.Application.ConfigSave) return false;

			GetConfigPaths();

			if(cfg.Meta.PreferUserConfiguration)
			{
				EnsureDirOfFileExists(g_strUserConfigFile);
				if(Save(cfg, g_strUserConfigFile)) return true;

				if(Save(cfg, g_strGlobalConfigFile)) return true;
			}
			else // Prefer global
			{
				if(Save(cfg, g_strGlobalConfigFile)) return true;

				EnsureDirOfFileExists(g_strUserConfigFile);
				if(Save(cfg, g_strUserConfigFile)) return true;
			}

			return false;
		}

		internal static bool Save()
		{
			return Save(Program.Config);
		}

		internal static string TryCreateBackup(string strFilePath)
		{
			if(string.IsNullOrEmpty(strFilePath)) { Debug.Assert(false); return null; }

			try
			{
				if(!File.Exists(strFilePath)) return null;

				string strToS = UrlUtil.EnsureTerminatingSeparator(
					UrlUtil.GetTempPath(), false) + PwDefs.ShortProductName +
					"_" + DateTime.Now.ToString(@"yyMMdd'_'HHmmss");
				NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;

				for(int i = 1; i < 100; ++i)
				{
					string strTo = strToS + ((i == 1) ? string.Empty :
						("_" + i.ToString(nfi))) + g_strFileSuffix;

					if(File.Exists(strTo)) continue;

					File.Copy(strFilePath, strTo, true);
					return strTo;
				}

				Debug.Assert(false);
			}
			catch(Exception) { Debug.Assert(false); }

			return null;
		}

		internal static void CreateBackupIfNecessary()
		{
			AppConfigEx cfg = Program.Config;
			ulong uVersion = cfg.Meta.GetVersion();
			bool b = false;

			// if(!string.IsNullOrEmpty(cfg.Application.HelpUrl) &&
			//	!AppConfigEx.IsOptionEnforced(cfg.Application, "HelpUrl"))
			//	b = true; // Help URL might be deleted by auto-disable
			// else
			if(uVersion < 0x0002003600000000UL) // < 2.54
			{
				// The default content mode of the following elements has been
				// changed in KeePass 2.54
				if(AppConfigEx.IsOptionEnforced(cfg.Application, "TriggerSystem") ||
					AppConfigEx.IsOptionEnforced(cfg.Integration, "UrlSchemeOverrides") ||
					AppConfigEx.IsOptionEnforced(cfg.PasswordGenerator, "UserProfiles"))
					b = true;
			}

			if(b)
			{
				GetConfigPaths();
				TryCreateBackup(cfg.Meta.PreferUserConfiguration ?
					g_strUserConfigFile : g_strGlobalConfigFile);
			}
		}
	}
}