Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/Sandboxing/SandboxManager.cs @ 5955

Last change on this file since 5955 was 4140, checked in by kgrading, 14 years ago

#828 added various improvements to the plugin cache manager, the execution engine, the transaction handling on the serverside and the server console

File size: 4.4 KB
RevLine 
[2612]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2612]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;
[2587]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.PluginInfrastructure.Manager;
27using System.IO;
28using System.ComponentModel;
29using System.Security;
30using System.Security.Permissions;
31using System.Security.Policy;
32using System.Reflection;
33using System.Diagnostics;
34
35namespace HeuristicLab.PluginInfrastructure.Sandboxing {
36  public class 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
58    public static AppDomain CreateAndInitSandbox(string name) {
59      return CreateAndInitSandbox(name, Enumerable.Empty<byte[]>());
60    }
61
62
63    public static AppDomain CreateAndInitSandbox(string name, IEnumerable<byte[]> files) {
64      PermissionSet pset;
65
66      #region permission set for sandbox
[4042]67      //Uncomment code for sandboxed appdomain
[4050]68     /* pset = new PermissionSet(PermissionState.None);
[4042]69      pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
70      pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.AllFlags));
71      FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
[2587]72
[4042]73      foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
74        foreach (IPluginFile pluginFile in plugin.Files) {
75          fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, pluginFile.Name); 
76          //fPerm.AddPathList(FileIOPermissionAccess.AllAccess, pluginFile.Name); 
77        }       
78      }
[2587]79
[4050]80      pset.AddPermission(fPerm);*/
[2587]81      #endregion
82
83      #region permission set of unrestricted appdomain
84      // unrestricted appdomain
[4050]85      pset = new PermissionSet(PermissionState.Unrestricted);
[3247]86      #endregion
[2587]87
88      AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
89      //setup.PrivateBinPath = pluginDir;
90      setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
[4042]91      AppDomain applicationDomain = AppDomain.CreateDomain(name, AppDomain.CurrentDomain.Evidence, setup, pset, new StrongName[] {CreateStrongName(Assembly.GetExecutingAssembly())});
92
[3247]93      Type applicationManagerType = typeof(DefaultApplicationManager);
94      DefaultApplicationManager applicationManager =
95        (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
[4140]96      PluginManager pm = new PluginManager(name);
97      pm.DiscoverAndCheckPlugins();
98      ApplicationDescription[] apps = pm.Applications.Cast<ApplicationDescription>().ToArray();
99      PluginDescription[] plugins = pm.Plugins.Cast<PluginDescription>().ToArray();
100      applicationManager.PrepareApplicationDomain(apps, plugins);     
[2587]101      return applicationDomain;
102    }
103
104    #endregion
105  }
106}
Note: See TracBrowser for help on using the repository browser.