Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Sandboxing/SandboxManager.cs @ 2612

Last change on this file since 2612 was 2612, checked in by gkronber, 14 years ago

Added missing license header. #799

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.PluginInfrastructure.Manager;
27using System.IO;
28using System.ComponentModel;
29using HeuristicLab.PluginInfrastructure.UpdateLocationReference;
30using System.Security;
31using System.Security.Permissions;
32using System.Security.Policy;
33using System.Reflection;
34using System.Diagnostics;
35
36namespace HeuristicLab.PluginInfrastructure.Sandboxing {
37  public class SandboxManager {
38
39    private static StrongName CreateStrongName(Assembly assembly) {
40      if (assembly == null)
41        throw new ArgumentNullException("assembly");
42
43      AssemblyName assemblyName = assembly.GetName();
44      Trace.Assert(assemblyName != null, "Could not get assembly name");
45
46      // get the public key blob
47      byte[] publicKey = assemblyName.GetPublicKey();
48      if (publicKey == null || publicKey.Length == 0)
49        throw new InvalidOperationException("Assembly is not strongly named");
50
51      StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
52
53      // and create the StrongName
54      return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
55    }
56
57    #region ISandboxManager Members
58
59    public static AppDomain CreateAndInitSandbox(string name) {
60      return CreateAndInitSandbox(name, Enumerable.Empty<byte[]>());
61    }
62
63
64    public static AppDomain CreateAndInitSandbox(string name, IEnumerable<byte[]> files) {
65      PermissionSet pset;
66
67      #region permission set for sandbox
68      // Uncomment code for sandboxed appdomain
69      //pset = new PermissionSet(PermissionState.None);
70      //pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
71      //pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
72      //FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
73
74      //foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
75      //  fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, plugin.Files.ToArray());
76      //}
77
78      //pset.AddPermission(fPerm);
79      #endregion
80
81      #region permission set of unrestricted appdomain
82      // unrestricted appdomain
83      pset = new PermissionSet(PermissionState.Unrestricted);
84      #endregion
85
86      AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
87      //setup.PrivateBinPath = pluginDir;
88      setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
89      AppDomain applicationDomain = AppDomain.CreateDomain(name, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
90      Type applicationManagerType = typeof(ApplicationManager);
91      ApplicationManager applicationManager =
92        (ApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
93      ApplicationDescription[] apps = ApplicationManager.Manager.Applications.Cast<ApplicationDescription>().ToArray();
94      PluginDescription[] plugins = ApplicationManager.Manager.Plugins.Cast<PluginDescription>().ToArray();
95      applicationManager.PrepareApplicationDomain(apps, plugins);
96      if (files != null && files.Count() > 0)
97        applicationManager.LoadAssemblies(files);
98      return applicationDomain;
99    }
100
101    #endregion
102  }
103}
Note: See TracBrowser for help on using the repository browser.