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
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 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.Text;
using System.Reflection;
using System.Runtime.Remoting;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using KeePass.App;
using KeePass.Resources;
using KeePass.Plugins;
using KeePass.UI;
using KeePassLib;
using KeePassLib.Interfaces;
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;
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();
}
public void LoadAllPlugins(string strDirectory, SearchOption so,
string[] vExclNames)
{
Debug.Assert(m_host != null);
try
{
string strPath = strDirectory;
if(!Directory.Exists(strPath)) return; // No assert
DirectoryInfo di = new DirectoryInfo(strPath);
List<FileInfo> lFiles = UrlUtil.GetFileInfos(di, "*.dll", so);
FilterList(lFiles, vExclNames);
LoadPlugins(lFiles, null, null, true);
lFiles = UrlUtil.GetFileInfos(di, "*.exe", so);
FilterList(lFiles, vExclNames);
LoadPlugins(lFiles, null, null, true);
lFiles = UrlUtil.GetFileInfos(di, "*." + PlgxPlugin.PlgxExtension, so);
FilterList(lFiles, vExclNames);
if(lFiles.Count > 0)
{
OnDemandStatusDialog dlgStatus = new OnDemandStatusDialog(true, null);
dlgStatus.StartLogging(PwDefs.ShortProductName, false);
foreach(FileInfo fi in lFiles)
PlgxPlugin.Load(fi.FullName, dlgStatus);
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<FileInfo> l = new List<FileInfo>();
l.Add(new FileInfo(strFilePath));
LoadPlugins(l, strTypeName, strDisplayFilePath, bSkipCacheFile);
}
private void LoadPlugins(List<FileInfo> lFiles, string strTypeName,
string strDisplayFilePath, bool bSkipCacheFiles)
{
string strCacheRoot = PlgxCache.GetCacheRoot();
foreach(FileInfo fi in lFiles)
{
if(bSkipCacheFiles && fi.FullName.StartsWith(strCacheRoot,
StrUtil.CaseIgnoreCmp))
continue;
FileVersionInfo fvi = null;
try
{
fvi = FileVersionInfo.GetVersionInfo(fi.FullName);
if((fvi == null) || (fvi.ProductName == null) ||
(fvi.ProductName != AppDefs.PluginProductName))
{
continue;
}
}
catch(Exception) { continue; }
Exception exShowStd = null;
try
{
PluginInfo pi = new PluginInfo(fi.FullName, fvi, strDisplayFilePath);
pi.Interface = CreatePluginInstance(pi.FilePath, strTypeName);
if(!pi.Interface.Initialize(m_host))
continue; // Fail without error
m_vPlugins.Add(pi);
}
catch(BadImageFormatException exBif)
{
if(Is1xPlugin(fi.FullName))
MessageService.ShowWarning(KPRes.PluginIncompatible +
MessageService.NewLine + fi.FullName + MessageService.NewParagraph +
KPRes.Plugin1x + MessageService.NewParagraph + KPRes.Plugin1xHint);
else exShowStd = exBif;
}
catch(Exception exLoad)
{
if(Program.CommandLineArgs[AppDefs.CommandLineOptions.Debug] != null)
MessageService.ShowWarningExcp(fi.FullName, exLoad);
else exShowStd = exLoad;
}
if(exShowStd != null)
ShowLoadError(fi.FullName, 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<FileInfo> 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].FullName);
foreach(string strExcl in vExclNames)
{
if(string.IsNullOrEmpty(strExcl)) { Debug.Assert(false); continue; }
if(string.Equals(strName, strExcl, StrUtil.CaseIgnoreCmp))
{
l.RemoveAt(i);
break;
}
}
}
}
}
}
|