1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Diagnostics;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using System.Security;
|
---|
28 | using System.Security.Permissions;
|
---|
29 | using System.Security.Policy;
|
---|
30 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.PluginInfrastructure.Sandboxing {
|
---|
33 | public class SandboxManager {
|
---|
34 |
|
---|
35 | // static class
|
---|
36 | private SandboxManager() { }
|
---|
37 |
|
---|
38 | private static StrongName CreateStrongName(Assembly assembly) {
|
---|
39 | if (assembly == null)
|
---|
40 | throw new ArgumentNullException("assembly");
|
---|
41 |
|
---|
42 | AssemblyName assemblyName = assembly.GetName();
|
---|
43 | Trace.Assert(assemblyName != null, "Could not get assembly name");
|
---|
44 |
|
---|
45 | // get the public key blob
|
---|
46 | byte[] publicKey = assemblyName.GetPublicKey();
|
---|
47 | if (publicKey == null || publicKey.Length == 0)
|
---|
48 | throw new InvalidOperationException("Assembly is not strongly named");
|
---|
49 |
|
---|
50 | StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
|
---|
51 |
|
---|
52 | // and create the StrongName
|
---|
53 | return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
|
---|
54 | }
|
---|
55 |
|
---|
56 | #region ISandboxManager Members
|
---|
57 | public static AppDomain CreateAndInitSandbox(string name) {
|
---|
58 | PermissionSet pset;
|
---|
59 |
|
---|
60 | #region permission set for sandbox
|
---|
61 | // Uncomment code for sandboxed appdomain
|
---|
62 | //pset = new PermissionSet(PermissionState.None);
|
---|
63 | //pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
|
---|
64 | //pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
|
---|
65 | //FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
|
---|
66 |
|
---|
67 | //foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
|
---|
68 | // fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, plugin.Files.ToArray());
|
---|
69 | //}
|
---|
70 |
|
---|
71 | //pset.AddPermission(fPerm);
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region permission set of unrestricted appdomain
|
---|
75 | // unrestricted appdomain
|
---|
76 | pset = new PermissionSet(PermissionState.Unrestricted);
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
|
---|
80 | //setup.PrivateBinPath = pluginDir;
|
---|
81 | setup.ApplicationBase = name;
|
---|
82 | setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
|
---|
83 | AppDomain applicationDomain = AppDomain.CreateDomain(name, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
|
---|
84 | Type applicationManagerType = typeof(DefaultApplicationManager);
|
---|
85 | DefaultApplicationManager applicationManager =
|
---|
86 | (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
|
---|
87 | PluginManager pm = new PluginManager(name);
|
---|
88 | pm.DiscoverAndCheckPlugins();
|
---|
89 | ApplicationDescription[] apps = pm.Applications.Cast<ApplicationDescription>().ToArray();
|
---|
90 | PluginDescription[] plugins = pm.Plugins.Cast<PluginDescription>().ToArray();
|
---|
91 | applicationManager.PrepareApplicationDomain(apps, plugins);
|
---|
92 | return applicationDomain;
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 | }
|
---|
96 | }
|
---|