File: PluginManager.cs

package info (click to toggle)
keepass2 2.41%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,892 kB
  • sloc: cs: 103,600; xml: 5,869; cpp: 308; sh: 48; makefile: 46
file content (574 lines) | stat: -rw-r--r-- 16,654 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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2019 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Remoting;
using System.Text;
using System.Windows.Forms;

using KeePass.App;
using KeePass.App.Configuration;
using KeePass.Plugins;
using KeePass.Resources;
using KeePass.UI;
using KeePass.Util;

using KeePassLib;
using KeePassLib.Cryptography;
using KeePassLib.Delegates;
using KeePassLib.Interfaces;
using KeePassLib.Native;
using KeePassLib.Utility;

namespace KeePass.Plugins
{
	internal sealed class PluginManager : IEnumerable<PluginInfo>
	{
		private List<PluginInfo> m_vPlugins = new List<PluginInfo>();
		private IPluginHost m_host = null;

		private static string g_strUserDir = string.Empty;
		internal static string UserDirectory
		{
			get { return g_strUserDir; }
		}

		public void Initialize(IPluginHost host)
		{
			Debug.Assert(host != null);
			m_host = host;
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return m_vPlugins.GetEnumerator();
		}
		
		public IEnumerator<PluginInfo> GetEnumerator()
		{
			return m_vPlugins.GetEnumerator();
		}

		internal void LoadAllPlugins()
		{
			string[] vExclNames = new string[] {
				AppDefs.FileNames.Program, AppDefs.FileNames.XmlSerializers,
				AppDefs.FileNames.NativeLib32, AppDefs.FileNames.NativeLib64,
				AppDefs.FileNames.ShInstUtil
			};

			string strAppDir = UrlUtil.GetFileDirectory(WinUtil.GetExecutable(),
				false, true);
			LoadAllPlugins(strAppDir, SearchOption.TopDirectoryOnly, vExclNames);
			g_strUserDir = strAppDir; // Preliminary, see below

			if(WinUtil.IsAppX)
			{
				string str = UrlUtil.EnsureTerminatingSeparator(
					AppConfigSerializer.AppDataDirectory, false) + AppDefs.PluginsDir;
				LoadAllPlugins(str, SearchOption.AllDirectories, vExclNames);

				g_strUserDir = str;
			}
			else if(!NativeLib.IsUnix())
			{
				string str = UrlUtil.EnsureTerminatingSeparator(strAppDir,
					false) + AppDefs.PluginsDir;
				LoadAllPlugins(str, SearchOption.AllDirectories, vExclNames);

				g_strUserDir = str;
			}
			else // Unix
			{
				try
				{
					DirectoryInfo diPlgRoot = new DirectoryInfo(strAppDir);
					foreach(DirectoryInfo diSub in diPlgRoot.GetDirectories())
					{
						if(diSub == null) { Debug.Assert(false); continue; }

						if(string.Equals(diSub.Name, AppDefs.PluginsDir,
							StrUtil.CaseIgnoreCmp))
						{
							LoadAllPlugins(diSub.FullName, SearchOption.AllDirectories,
								vExclNames);

							g_strUserDir = diSub.FullName;
						}
					}
				}
				catch(Exception) { Debug.Assert(false); }
			}
		}

		public void LoadAllPlugins(string strDir, SearchOption so, string[] vExclNames)
		{
			Debug.Assert(m_host != null);

			try
			{
				if(!Directory.Exists(strDir)) return; // No assert

				List<string> lDlls = UrlUtil.GetFilePaths(strDir, "*.dll", so);
				FilterList(lDlls, vExclNames);

				List<string> lExes = UrlUtil.GetFilePaths(strDir, "*.exe", so);
				FilterList(lExes, vExclNames);

				List<string> lPlgxs = UrlUtil.GetFilePaths(strDir, "*." +
					PlgxPlugin.PlgxExtension, so);
				FilterList(lPlgxs, vExclNames);

				FilterLists(lDlls, lExes, lPlgxs);

				LoadPlugins(lDlls, null, null, true);
				LoadPlugins(lExes, null, null, true);

				if(lPlgxs.Count != 0)
				{
					OnDemandStatusDialog dlgStatus = new OnDemandStatusDialog(true, null);
					dlgStatus.StartLogging(PwDefs.ShortProductName, false);

					try
					{
						foreach(string strFile in lPlgxs)
							PlgxPlugin.Load(strFile, dlgStatus);
					}
					finally { dlgStatus.EndLogging(); }
				}
			}
			catch(Exception) { Debug.Assert(false); } // Path access violation
		}

		public void LoadPlugin(string strFilePath, string strTypeName,
			string strDisplayFilePath, bool bSkipCacheFile)
		{
			if(strFilePath == null) throw new ArgumentNullException("strFilePath");

			List<string> l = new List<string>();
			l.Add(strFilePath);

			LoadPlugins(l, strTypeName, strDisplayFilePath, bSkipCacheFile);
		}

		private void LoadPlugins(List<string> lFiles, string strTypeName,
			string strDisplayFilePath, bool bSkipCacheFiles)
		{
			string strCacheRoot = UrlUtil.EnsureTerminatingSeparator(
				PlgxCache.GetCacheRoot(), false);

			foreach(string strFile in lFiles)
			{
				if(bSkipCacheFiles && strFile.StartsWith(strCacheRoot,
					StrUtil.CaseIgnoreCmp))
					continue;

				FileVersionInfo fvi = null;
				try
				{
					fvi = FileVersionInfo.GetVersionInfo(strFile);

					if((fvi == null) || (fvi.ProductName == null) ||
						(fvi.ProductName != AppDefs.PluginProductName))
					{
						continue;
					}
				}
				catch(Exception) { continue; }

				Exception exShowStd = null;
				try
				{
					string strHash = Convert.ToBase64String(CryptoUtil.HashSha256(
						strFile), Base64FormattingOptions.None);

					PluginInfo pi = new PluginInfo(strFile, fvi, strDisplayFilePath);
					pi.Interface = CreatePluginInstance(pi.FilePath, strTypeName);

					CheckCompatibility(strHash, pi.Interface);
					// CheckCompatibilityRefl(strFile);

					if(!pi.Interface.Initialize(m_host))
						continue; // Fail without error

					m_vPlugins.Add(pi);
				}
				catch(BadImageFormatException exBif)
				{
					if(Is1xPlugin(strFile))
						MessageService.ShowWarning(KPRes.PluginIncompatible +
							MessageService.NewLine + strFile + MessageService.NewParagraph +
							KPRes.Plugin1x + MessageService.NewParagraph + KPRes.Plugin1xHint);
					else exShowStd = exBif;
				}
				catch(Exception exLoad)
				{
					if(Program.CommandLineArgs[AppDefs.CommandLineOptions.Debug] != null)
						MessageService.ShowWarningExcp(strFile, exLoad);
					else exShowStd = exLoad;
				}

				if(exShowStd != null)
					ShowLoadError(strFile, exShowStd, null);
			}
		}

		internal static void ShowLoadError(string strPath, Exception ex,
			IStatusLogger slStatus)
		{
			if(string.IsNullOrEmpty(strPath)) { Debug.Assert(false); return; }

			if(slStatus != null)
				slStatus.SetText(KPRes.PluginLoadFailed, LogStatusType.Info);

			bool bShowExcp = (Program.CommandLineArgs[
				AppDefs.CommandLineOptions.Debug] != null);

			string strMsg = KPRes.PluginIncompatible + MessageService.NewLine +
				strPath + MessageService.NewParagraph + KPRes.PluginUpdateHint;
			string strExcp = ((ex != null) ? StrUtil.FormatException(ex).Trim() : null);

			VistaTaskDialog vtd = new VistaTaskDialog();
			vtd.Content = strMsg;
			vtd.ExpandedByDefault = ((strExcp != null) && bShowExcp);
			vtd.ExpandedInformation = strExcp;
			vtd.WindowTitle = PwDefs.ShortProductName;
			vtd.SetIcon(VtdIcon.Warning);

			if(!vtd.ShowDialog())
			{
				if(!bShowExcp) MessageService.ShowWarning(strMsg);
				else MessageService.ShowWarningExcp(strPath, ex);
			}
		}

		public void UnloadAllPlugins()
		{
			foreach(PluginInfo plugin in m_vPlugins)
			{
				Debug.Assert(plugin.Interface != null);
				if(plugin.Interface != null)
				{
					try { plugin.Interface.Terminate(); }
					catch(Exception) { Debug.Assert(false); }
				}
			}

			m_vPlugins.Clear();
		}

		private static Plugin CreatePluginInstance(string strFilePath,
			string strTypeName)
		{
			Debug.Assert(strFilePath != null);
			if(strFilePath == null) throw new ArgumentNullException("strFilePath");

			string strType;
			if(string.IsNullOrEmpty(strTypeName))
			{
				strType = UrlUtil.GetFileName(strFilePath);
				strType = UrlUtil.StripExtension(strType) + "." +
					UrlUtil.StripExtension(strType) + "Ext";
			}
			else strType = strTypeName + "." + strTypeName + "Ext";

			ObjectHandle oh = Activator.CreateInstanceFrom(strFilePath, strType);

			Plugin plugin = (oh.Unwrap() as Plugin);
			if(plugin == null) throw new FileLoadException();
			return plugin;
		}

		private static bool Is1xPlugin(string strFile)
		{
			try
			{
				byte[] pbFile = File.ReadAllBytes(strFile);
				byte[] pbSig = StrUtil.Utf8.GetBytes("KpCreateInstance");
				string strData = MemUtil.ByteArrayToHexString(pbFile);
				string strSig = MemUtil.ByteArrayToHexString(pbSig);

				return (strData.IndexOf(strSig) >= 0);
			}
			catch(Exception) { Debug.Assert(false); }

			return false;
		}

		private static void FilterList(List<string> l, string[] vExclNames)
		{
			if((l == null) || (vExclNames == null)) { Debug.Assert(false); return; }

			for(int i = l.Count - 1; i >= 0; --i)
			{
				string strName = UrlUtil.GetFileName(l[i]);
				if(string.IsNullOrEmpty(strName))
				{
					Debug.Assert(false);
					l.RemoveAt(i);
					continue;
				}

				// Ignore satellite assemblies
				if(strName.EndsWith(".resources.dll", StrUtil.CaseIgnoreCmp))
				{
					l.RemoveAt(i);
					continue;
				}

				foreach(string strExcl in vExclNames)
				{
					if(string.IsNullOrEmpty(strExcl)) { Debug.Assert(false); continue; }

					if(strName.Equals(strExcl, StrUtil.CaseIgnoreCmp))
					{
						l.RemoveAt(i);
						break;
					}
				}
			}
		}

		private static void FilterLists(List<string> lDlls, List<string> lExes,
			List<string> lPlgxs)
		{
			bool bPreferDll = Program.IsStableAssembly();

			for(int i = lDlls.Count - 1; i >= 0; --i)
			{
				string strDllPre = UrlUtil.StripExtension(lDlls[i]);

				for(int j = lPlgxs.Count - 1; j >= 0; --j)
				{
					string strPlgxPre = UrlUtil.StripExtension(lPlgxs[j]);

					if(string.Equals(strDllPre, strPlgxPre, StrUtil.CaseIgnoreCmp))
					{
						if(bPreferDll) lPlgxs.RemoveAt(j);
						else lDlls.RemoveAt(i);

						break;
					}
				}
			}
		}

		private static void CheckRefs(Module m, int iMdTokenType,
			GAction<Module, int> fCheck)
		{
			if((m == null) || (fCheck == null)) { Debug.Assert(false); return; }
			if((iMdTokenType & 0x00FFFFFF) != 0)
			{
				Debug.Assert(false); // Not a valid MetadataTokenType
				return;
			}
			if((iMdTokenType < 0) || (iMdTokenType == 0x7F000000))
			{
				Debug.Assert(false); // Loop below would need to be adjusted
				return;
			}

			try
			{
				// https://msdn.microsoft.com/en-us/library/ms404456(v=vs.100).aspx
				// https://docs.microsoft.com/en-us/dotnet/standard/metadata-and-self-describing-components
				int s = iMdTokenType | 1; // RID = 0 <=> 'nil token'
				int e = iMdTokenType | 0x00FFFFFF;

				for(int i = s; i <= e; ++i) fCheck(m, i);
			}
			catch(ArgumentOutOfRangeException) { } // End of metadata table
			catch(ArgumentException) { Debug.Assert(false); }
			// Other exceptions indicate an unresolved reference
		}

		private static void CheckTypeRef(Module m, int iMdToken)
		{
			// ResolveType should throw exception for unresolvable token
			// if(m.ResolveType(iMdToken) == null) { Debug.Assert(false); }

			// ResolveType should throw exception for unresolvable token
			Type t = m.ResolveType(iMdToken);
			if(t == null) { Debug.Assert(false); return; }

			if(t.Assembly == typeof(PluginManager).Assembly)
			{
				if(t.IsNotPublic || t.IsNestedPrivate || t.IsNestedAssembly ||
					t.IsNestedFamANDAssem)
					throw new UnauthorizedAccessException("Ref.: " + t.ToString() + ".");
			}
		}

		private static void CheckMemberRef(Module m, int iMdToken)
		{
			// ResolveMember should throw exception for unresolvable token
			// if(m.ResolveMember(iMdToken) == null) { Debug.Assert(false); }

			// ResolveMember should throw exception for unresolvable token
			MemberInfo mi = m.ResolveMember(iMdToken);
			if(mi == null) { Debug.Assert(false); return; }

			if(mi.Module == typeof(PluginManager).Module)
			{
				MethodBase mb = (mi as MethodBase);
				if(mb != null)
				{
					if(mb.IsPrivate || mb.IsAssembly || mb.IsFamilyAndAssembly)
						ThrowRefAccessExcp(mb);
					return;
				}

				FieldInfo fi = (mi as FieldInfo);
				if(fi != null)
				{
					if(fi.IsPrivate || fi.IsAssembly || fi.IsFamilyAndAssembly)
						ThrowRefAccessExcp(fi);
					return;
				}

				Debug.Assert(false); // Unknown member reference type
			}
		}

		private static void ThrowRefAccessExcp(MemberInfo mi)
		{
			string str = "Ref.: ";

			try
			{
				Type t = mi.DeclaringType;
				if(t != null) str += t.ToString() + " -> ";
			}
			catch(Exception) { Debug.Assert(false); }

			throw new MemberAccessException(str + mi.ToString() + ".");
		}

		private static void CheckCompatibilityPriv(Plugin p)
		{
			// When trying to resolve a non-existing token, Mono
			// terminates the whole process with a SIGABRT instead
			// of just throwing an ArgumentOutOfRangeException
			if(MonoWorkarounds.IsRequired(9604)) return;

			Assembly asm = p.GetType().Assembly;
			if(asm == typeof(PluginManager).Assembly) { Debug.Assert(false); return; }

			foreach(Module m in asm.GetModules())
			{
				// MetadataTokenType.TypeRef = 0x01000000
				CheckRefs(m, 0x01000000, CheckTypeRef);

				// MetadataTokenType.MemberRef = 0x0A000000
				CheckRefs(m, 0x0A000000, CheckMemberRef);
			}
		}

		private static void CheckCompatibility(string strHash, Plugin p)
		{
			AceApplication aceApp = Program.Config.Application;
			// bool? ob = aceApp.GetPluginCompat(strHash);
			// if(ob.HasValue) return ob.Value;
			if(aceApp.IsPluginCompat(strHash)) return;

			CheckCompatibilityPriv(p);

			aceApp.SetPluginCompat(strHash);
		}

		/* private static void CheckCompatibilityRefl(string strFile)
		{
			ResolveEventHandler eh = delegate(object sender, ResolveEventArgs e)
			{
				string strName = e.Name;
				if(strName.Equals("KeePass", StrUtil.CaseIgnoreCmp) ||
					strName.StartsWith("KeePass,", StrUtil.CaseIgnoreCmp))
					return Assembly.ReflectionOnlyLoadFrom(WinUtil.GetExecutable());

				return Assembly.ReflectionOnlyLoad(strName);
			};

			AppDomain d = AppDomain.CurrentDomain;
			d.ReflectionOnlyAssemblyResolve += eh;
			try
			{
				Assembly asm = Assembly.ReflectionOnlyLoadFrom(strFile);
				asm.GetTypes();
			}
			finally { d.ReflectionOnlyAssemblyResolve -= eh; }
		} */

		internal void AddMenuItems(PluginMenuType t, ToolStripItemCollection c,
			ToolStripItem tsiPrev)
		{
			if(c == null) { Debug.Assert(false); return; }

			List<ToolStripItem> l = new List<ToolStripItem>();
			foreach(PluginInfo pi in m_vPlugins)
			{
				if(pi == null) { Debug.Assert(false); continue; }

				Plugin p = pi.Interface;
				if(p == null) { Debug.Assert(false); continue; }

				ToolStripMenuItem tsmi = p.GetMenuItem(t);
				if(tsmi != null)
				{
					// string strTip = tsmi.ToolTipText;
					// if((strTip == null) || (strTip == tsmi.Text))
					//	strTip = string.Empty;
					// if(strTip.Length != 0) strTip += MessageService.NewParagraph;
					// strTip += KPRes.Plugin + ": " + pi.Name;
					// tsmi.ToolTipText = strTip;

					l.Add(tsmi);
				}
			}
			if(l.Count == 0) return;

			int iPrev = ((tsiPrev != null) ? c.IndexOf(tsiPrev) : -1);
			if(iPrev < 0) { Debug.Assert(false); iPrev = c.Count - 1; }
			int iIns = iPrev + 1;

			l.Sort(PluginManager.CompareToolStripItems);
			if((iPrev >= 0) && (iPrev < c.Count) && !(c[iPrev] is ToolStripSeparator))
				l.Insert(0, new ToolStripSeparator());
			if((iIns < c.Count) && !(c[iIns] is ToolStripSeparator))
				l.Add(new ToolStripSeparator());

			if(iIns == c.Count) c.AddRange(l.ToArray());
			else
			{
				for(int i = 0; i < l.Count; ++i)
					c.Insert(iIns + i, l[i]);
			}
		}

		private static int CompareToolStripItems(ToolStripItem x,
			ToolStripItem y)
		{
			return string.Compare(x.Text, y.Text, StrUtil.CaseIgnoreCmp);
		}
	}
}