File: MultipleValuesEntryContext.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 (531 lines) | stat: -rw-r--r-- 13,952 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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
  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.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using KeePass.UI;

using KeePassLib;
using KeePassLib.Collections;
using KeePassLib.Security;

namespace KeePass.Util.MultipleValues
{
	public sealed class MultipleValuesEntryContext : MultipleValuesContext
	{
		private readonly PwEntry[] m_v;
		private readonly PwDatabase m_pd; // Impl. detail; maybe entries from diff. dbs. in the future
		private readonly PwEntry m_peM;

		private readonly PwUuid m_puCueIcon = new PwUuid(true);

		private readonly bool[] m_vModified;

		internal readonly Dictionary<string, bool> MultiStringProt =
			new Dictionary<string, bool>();
		internal bool MultiExpiry = false;
		internal bool MultiFgColor = false;
		internal bool MultiBgColor = false;
		internal bool MultiAutoTypeEnabled = false;
		internal bool MultiAutoTypeObf = false;

		// For plugins
		public PwEntry[] Entries
		{
			get { return m_v; }
		}

		internal MultipleValuesEntryContext(PwEntry[] v, PwDatabase pd, out PwEntry peMulti)
		{
			if((v == null) || (v.Length < 1)) throw new ArgumentOutOfRangeException("v");
			if(Array.IndexOf(v, null) >= 0) throw new ArgumentOutOfRangeException("v");
			if(pd == null) throw new ArgumentNullException("pd");
			if(!pd.IsOpen) throw new ArgumentOutOfRangeException("pd");

			PwEntry peM = new PwEntry(true, true);
			peM.ParentGroup = pd.RootGroup;

			m_v = new PwEntry[v.Length];
			Array.Copy(v, m_v, v.Length);
			m_pd = pd;
			m_peM = peM;

			m_vModified = new bool[v.Length];

			MultiInitStrings();
			MultiInitIcon();
			MultiInitProperties();
			MultiInitCustomData();

			if(peM.CustomIconUuid.Equals(m_puCueIcon))
			{
				using(Image img = MultipleValuesEx.CreateMultiImage(null))
				{
					using(MemoryStream ms = new MemoryStream())
					{
						img.Save(ms, ImageFormat.Png);

						PwCustomIcon ico = new PwCustomIcon(m_puCueIcon, ms.ToArray());
						pd.CustomIcons.Add(ico);
						pd.UINeedsIconUpdate = true;
					}
				}
			}

			peMulti = peM;
		}

		protected override void Dispose(bool disposing)
		{
			if(!disposing) { Debug.Assert(false); return; }
			if(!m_pd.IsOpen) { Debug.Assert(false); return; }

			int i = m_pd.GetCustomIconIndex(m_puCueIcon);
			if(i >= 0)
			{
				m_pd.CustomIcons.RemoveAt(i);
				m_pd.UINeedsIconUpdate = true;
			}
		}

		internal override bool ApplyChanges()
		{
			if(!m_pd.IsOpen) { Debug.Assert(false); return false; }

			MultiApplyStrings();
			MultiApplyIcon();
			MultiApplyProperties();
			MultiApplyCustomData();

			bool bModAny = false;
			for(int i = 0; i < m_v.Length; ++i)
			{
				if(m_vModified[i])
				{
					m_v[i].Touch(true, false);

					m_vModified[i] = false;
					bModAny = true;
				}
			}

			return bModAny;
		}

		private void PrepareMod(int iEntry)
		{
			if(m_vModified[iEntry]) return;
			m_vModified[iEntry] = true;

			m_v[iEntry].CreateBackup(m_pd);
		}

		private void EnsureStandardStrings()
		{
			foreach(string strKey in PwDefs.GetStandardFields())
			{
				if(!m_peM.Strings.Exists(strKey))
					m_peM.Strings.Set(strKey, (m_pd.MemoryProtection.GetProtection(
						strKey) ? ProtectedString.EmptyEx : ProtectedString.Empty));

				// Standard string protections are normalized while
				// loading/saving the database file
				this.MultiStringProt[strKey] = true;
			}
		}

		private void MultiInitStrings()
		{
			ProtectedString psCue = MultipleValuesEx.CueProtectedString;
			ProtectedString psCueEx = MultipleValuesEx.CueProtectedStringEx;

			EnsureStandardStrings();

			for(int i = 0; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				foreach(KeyValuePair<string, ProtectedString> kvp in pe.Strings)
				{
					if(i == 0) m_peM.Strings.Set(kvp.Key, kvp.Value);
					else
					{
						bool bProt = kvp.Value.IsProtected;
						ProtectedString psM = m_peM.Strings.Get(kvp.Key);

						if(psM == null)
							m_peM.Strings.Set(kvp.Key, (bProt ? psCueEx : psCue));
						else
						{
							bool bProtM = psM.IsProtected;

							if(!psM.Equals(kvp.Value, false))
								m_peM.Strings.Set(kvp.Key, ((bProtM && bProt) ?
									psCueEx : psCue));

							if(bProtM != bProt)
							{
								this.MultiStringProt[kvp.Key] = true;
								if(bProtM)
									m_peM.Strings.Set(kvp.Key, m_peM.Strings.Get(
										kvp.Key).WithProtection(false)); // May be set to cue above
							}
						}
					}
				}

				List<KeyValuePair<string, ProtectedString>> lM =
					new List<KeyValuePair<string, ProtectedString>>(m_peM.Strings);
				foreach(KeyValuePair<string, ProtectedString> kvpM in lM)
				{
					if(pe.Strings.Exists(kvpM.Key)) continue;

					if(!PwDefs.IsStandardField(kvpM.Key) || !kvpM.Value.IsEmpty)
						m_peM.Strings.Set(kvpM.Key, (kvpM.Value.IsProtected ?
							psCueEx : psCue));
				}
			}
		}

		private void MultiApplyStrings()
		{
			ProtectedString psCue = MultipleValuesEx.CueProtectedString;

			EnsureStandardStrings();

			for(int i = 0; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				foreach(KeyValuePair<string, ProtectedString> kvpM in m_peM.Strings)
				{
					ProtectedString ps = pe.Strings.Get(kvpM.Key);

					bool bProt = kvpM.Value.IsProtected;
					bool bMultiProt;
					this.MultiStringProt.TryGetValue(kvpM.Key, out bMultiProt);
					if(bMultiProt && (ps != null))
						bProt = ps.IsProtected;

					if(kvpM.Value.Equals(psCue, false))
					{
						if((ps != null) && (ps.IsProtected != bProt))
						{
							PrepareMod(i);
							pe.Strings.Set(kvpM.Key, ps.WithProtection(bProt));
						}
					}
					else if(kvpM.Value.IsEmpty && (ps == null) &&
						PwDefs.IsStandardField(kvpM.Key))
					{
						// Do not create the string
					}
					else
					{
						if((ps == null) || !ps.Equals(kvpM.Value, false) ||
							(ps.IsProtected != bProt))
						{
							PrepareMod(i);
							pe.Strings.Set(kvpM.Key, kvpM.Value.WithProtection(bProt));
						}
					}
				}

				List<string> lKeys = pe.Strings.GetKeys();
				foreach(string strKey in lKeys)
				{
					if(!m_peM.Strings.Exists(strKey))
					{
						PrepareMod(i);
						pe.Strings.Remove(strKey);
					}
				}
			}
		}

		private void MultiInitIcon()
		{
			m_peM.IconId = m_v[0].IconId;
			m_peM.CustomIconUuid = m_v[0].CustomIconUuid;

			bool bCustomZ = m_peM.CustomIconUuid.Equals(PwUuid.Zero);

			for(int i = 1; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				bool bCustomE = pe.CustomIconUuid.Equals(m_peM.CustomIconUuid);
				if(!bCustomE || (bCustomZ && (pe.IconId != m_peM.IconId)))
				{
					m_peM.IconId = PwIcon.Key;
					m_peM.CustomIconUuid = m_puCueIcon;
					break;
				}
			}
		}

		private void MultiApplyIcon()
		{
			if(m_peM.CustomIconUuid.Equals(m_puCueIcon)) return;

			bool bCustomZ = m_peM.CustomIconUuid.Equals(PwUuid.Zero);

			for(int i = 0; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				bool bCustomE = pe.CustomIconUuid.Equals(m_peM.CustomIconUuid);
				if(!bCustomE)
				{
					PrepareMod(i);
					pe.CustomIconUuid = m_peM.CustomIconUuid;

					if(bCustomZ) pe.IconId = m_peM.IconId;
				}
				else if(bCustomZ && (pe.IconId != m_peM.IconId))
				{
					PrepareMod(i);
					pe.IconId = m_peM.IconId;
				}
			}
		}

		private static bool ListsEqual(List<string> lA, List<string> lB)
		{
			if(lA.Count != lB.Count) return false;

			for(int i = 0; i < lA.Count; ++i)
			{
				if(lA[i] != lB[i]) return false;
			}

			return true;
		}

		private void MultiInitProperties()
		{
			string strCue = MultipleValuesEx.CueString;

			bool bExpM = m_v[0].Expires;
			m_peM.Expires = bExpM;
			m_peM.ExpiryTime = m_v[0].ExpiryTime;

			m_peM.ForegroundColor = m_v[0].ForegroundColor;
			m_peM.BackgroundColor = m_v[0].BackgroundColor;

			bool bTagsEq = true;
			m_peM.Tags = new List<string>(m_v[0].Tags);

			m_peM.OverrideUrl = m_v[0].OverrideUrl;

			m_peM.AutoType.Enabled = m_v[0].AutoType.Enabled;
			m_peM.AutoType.DefaultSequence = m_v[0].AutoType.DefaultSequence;
			m_peM.AutoType.ObfuscationOptions = m_v[0].AutoType.ObfuscationOptions;

			for(int i = 1; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				if((pe.Expires != bExpM) || (bExpM && (pe.ExpiryTime != m_peM.ExpiryTime)))
				{
					this.MultiExpiry = true;
					m_peM.Expires = false;
					bExpM = false;
				}

				if(!UIUtil.ColorsEqual(pe.ForegroundColor, m_peM.ForegroundColor))
				{
					this.MultiFgColor = true;
					m_peM.ForegroundColor = Color.Empty;
				}

				if(!UIUtil.ColorsEqual(pe.BackgroundColor, m_peM.BackgroundColor))
				{
					this.MultiBgColor = true;
					m_peM.BackgroundColor = Color.Empty;
				}

				if(bTagsEq && !ListsEqual(pe.Tags, m_peM.Tags))
				{
					m_peM.Tags.Clear(); // We own it, see above
					m_peM.Tags.Add(strCue);
					bTagsEq = false;
				}

				if(pe.OverrideUrl != m_peM.OverrideUrl)
					m_peM.OverrideUrl = strCue;

				if(pe.AutoType.Enabled != m_peM.AutoType.Enabled)
				{
					this.MultiAutoTypeEnabled = true;
					m_peM.AutoType.Enabled = true;
				}

				if(pe.AutoType.DefaultSequence != m_peM.AutoType.DefaultSequence)
					m_peM.AutoType.DefaultSequence = strCue;

				if(pe.AutoType.ObfuscationOptions != m_peM.AutoType.ObfuscationOptions)
				{
					this.MultiAutoTypeObf = true;
					m_peM.AutoType.ObfuscationOptions = AutoTypeObfuscationOptions.None;
				}
			}
		}

		private void MultiApplyProperties()
		{
			string strCue = MultipleValuesEx.CueString;

			bool bExpM = m_peM.Expires;

			bool bSetTags = ((m_peM.Tags.Count != 1) || (m_peM.Tags[0] != strCue));
			bool bSetOvUrl = (m_peM.OverrideUrl != strCue);
			bool bSetAtSeq = (m_peM.AutoType.DefaultSequence != strCue);

			for(int i = 0; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				if(!this.MultiExpiry && ((pe.Expires != bExpM) ||
					(bExpM && (pe.ExpiryTime != m_peM.ExpiryTime))))
				{
					PrepareMod(i);
					pe.Expires = bExpM;

					if(bExpM) pe.ExpiryTime = m_peM.ExpiryTime;
				}

				if(!this.MultiFgColor && !UIUtil.ColorsEqual(pe.ForegroundColor,
					m_peM.ForegroundColor))
				{
					PrepareMod(i);
					pe.ForegroundColor = m_peM.ForegroundColor;
				}

				if(!this.MultiBgColor && !UIUtil.ColorsEqual(pe.BackgroundColor,
					m_peM.BackgroundColor))
				{
					PrepareMod(i);
					pe.BackgroundColor = m_peM.BackgroundColor;
				}

				if(bSetTags && !ListsEqual(pe.Tags, m_peM.Tags))
				{
					PrepareMod(i);
					pe.Tags = new List<string>(m_peM.Tags);
				}

				if(bSetOvUrl && (pe.OverrideUrl != m_peM.OverrideUrl))
				{
					PrepareMod(i);
					pe.OverrideUrl = m_peM.OverrideUrl;
				}

				if(!this.MultiAutoTypeEnabled && (pe.AutoType.Enabled !=
					m_peM.AutoType.Enabled))
				{
					PrepareMod(i);
					pe.AutoType.Enabled = m_peM.AutoType.Enabled;
				}

				if(bSetAtSeq && (pe.AutoType.DefaultSequence !=
					m_peM.AutoType.DefaultSequence))
				{
					PrepareMod(i);
					pe.AutoType.DefaultSequence = m_peM.AutoType.DefaultSequence;
				}

				if(!this.MultiAutoTypeObf && (pe.AutoType.ObfuscationOptions !=
					m_peM.AutoType.ObfuscationOptions))
				{
					PrepareMod(i);
					pe.AutoType.ObfuscationOptions = m_peM.AutoType.ObfuscationOptions;
				}
			}
		}

		private void MultiInitCustomData()
		{
			string strCue = MultipleValuesEx.CueString;

			for(int i = 0; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				foreach(KeyValuePair<string, string> kvp in pe.CustomData)
				{
					if(i == 0) m_peM.CustomData.Set(kvp.Key, kvp.Value);
					else
					{
						string strM = m_peM.CustomData.Get(kvp.Key);
						if((strM == null) || (strM != kvp.Value))
							m_peM.CustomData.Set(kvp.Key, strCue);
					}
				}

				List<KeyValuePair<string, string>> lM =
					new List<KeyValuePair<string, string>>(m_peM.CustomData);
				foreach(KeyValuePair<string, string> kvpM in lM)
				{
					if(!pe.CustomData.Exists(kvpM.Key))
						m_peM.CustomData.Set(kvpM.Key, strCue);
				}
			}
		}

		private void MultiApplyCustomData()
		{
			string strCue = MultipleValuesEx.CueString;

			for(int i = 0; i < m_v.Length; ++i)
			{
				PwEntry pe = m_v[i];

				foreach(KeyValuePair<string, string> kvpM in m_peM.CustomData)
				{
					if(kvpM.Value == strCue) continue;

					string str = pe.CustomData.Get(kvpM.Key);
					if((str == null) || (str != kvpM.Value))
					{
						PrepareMod(i);
						pe.CustomData.Set(kvpM.Key, kvpM.Value);
					}
				}

				List<KeyValuePair<string, string>> l =
					new List<KeyValuePair<string, string>>(pe.CustomData);
				foreach(KeyValuePair<string, string> kvp in l)
				{
					if(!m_peM.CustomData.Exists(kvp.Key))
					{
						PrepareMod(i);
						pe.CustomData.Remove(kvp.Key);
					}
				}
			}
		}
	}
}