示例#1
0
        private static CommandLineData ParseCommandLine(string[] args)
        {
            Contract.Requires(args != null);

            try
            {
                int i = 0;

                CommandLineData ret = new CommandLineData();

                // The partial trust runner assembly needs to be in the full trust list in the
                // sandboxed domain in order to correctly function in that domain
                ret.FullTrustAssemblies.Add(Assembly.GetExecutingAssembly());

                // First search for parameters to control PartialTrustRunner itself
                while (i < args.Length && args[i].StartsWith("-", StringComparison.OrdinalIgnoreCase))
                {
                    // Add full trust assembly
                    if (String.Equals(args[i], "-af", StringComparison.OrdinalIgnoreCase))
                    {
                        Assembly asm = Assembly.LoadFrom(args[++i]);
                        ret.FullTrustAssemblies.Add(asm);
                    }
                    // Suppress PartialTrustRunner output
                    else if (String.Equals(args[i], "-nro", StringComparison.OrdinalIgnoreCase))
                    {
                        ret.NoRunnerOutput = true;
                    }
                    // The partial trust PermissionSet
                    else if (String.Equals(args[i], "-ps", StringComparison.OrdinalIgnoreCase))
                    {
                        ret.StandardPermissionSet = (StandardPermissionSet)Enum.Parse(typeof(StandardPermissionSet), args[++i], true);
                    }
                    // URL to provide same-site access to
                    else if (String.Equals(args[i], "-url", StringComparison.OrdinalIgnoreCase))
                    {
                        ret.SourceUrl = new Url(args[++i]);
                    }
                    // Permission set XML file
                    else if (String.Equals(args[i], "-xml", StringComparison.OrdinalIgnoreCase))
                    {
                        using (StreamReader sr = new StreamReader(args[++i]))
                        {
                            SecurityElement elem = SecurityElement.FromString(sr.ReadToEnd());
                        }
                    }
                    else
                    {
                        WriteOutput(String.Format(CultureInfo.CurrentCulture, Resources.UnknownOption, args[i]));
                        Usage();
                        return(null);
                    }

                    ++i;
                }

                // If we still have parameters left, they should go through to the program that will be run
                if (i < args.Length)
                {
                    // The first parameter is the application itself
                    ret.ProgramName = args[i++];

                    // Any remaining parameters are for the application itself
                    int argsSize = args.Length - i;
                    ret.Arguments = new string[argsSize];
                    if (argsSize > 0)
                    {
                        Array.Copy(args, i, ret.Arguments, 0, argsSize);
                    }

                    // We only want to return success if we found arguments for the program, since
                    // otherwise we don't know what program to run in the sandbox
                    return(ret);
                }
            }
            catch (ArgumentException ex)
            {
                WriteOutput(String.Format(CultureInfo.CurrentCulture, Resources.ErrorParsingCommandLine, ex.Message));
            }
            catch (InvalidOperationException ex)
            {
                WriteOutput(String.Format(CultureInfo.CurrentCulture, Resources.ErrorParsingCommandLine, ex.Message));
            }

            // If we got here, we have an invalid command line
            Usage();
            return(null);
        }
示例#2
0
        internal static int Main(string[] args)
        {
            try
            {
                // Make sure that we have a strong name signature, otherwise we won't be able
                // to correctly trust this assembly in the sandboxed domain.
                if (Assembly.GetExecutingAssembly().Evidence.GetHostEvidence <StrongName>() == null)
                {
                    WriteOutput(Resources.PartialTrustRunnerUnsigned);
                    return(-1);
                }

                // Parse the command line - and make sure it is valid
                CommandLineData commands = ParseCommandLine(args);
                if (commands == null)
                {
                    return(-1);
                }

                AppDomainSetup sandboxSetup = new AppDomainSetup();

                // We need the AppDomain to have its AppBase be in the same location as the target
                // program.  This allows the application to find all of its dependencies correctly
                sandboxSetup.ApplicationBase = Path.GetDirectoryName(Path.GetFullPath(commands.ProgramName));

                // The application name should match the entry point
                sandboxSetup.ApplicationName = Path.GetFileNameWithoutExtension(commands.ProgramName);

                // We also want the AppDomain to use the .exe.config file that the target has
                // specified for itself (if it exists)
                string configFile = Path.GetFullPath(commands.ProgramName) + ".config";
                if (File.Exists(configFile))
                {
                    sandboxSetup.ConfigurationFile = configFile;
                }

                // Get strong names for the full trust assemblies
                var fullTrustStrongNames = from fullTrustAssembly in commands.FullTrustAssemblies
                                           where fullTrustAssembly.Evidence.GetHostEvidence <StrongName>() != null
                                           select fullTrustAssembly.Evidence.GetHostEvidence <StrongName>();

                // Create the sandboxed domain
                AppDomain sandbox = AppDomain.CreateDomain(Path.GetFileNameWithoutExtension(commands.ProgramName),
                                                           null,
                                                           sandboxSetup,
                                                           commands.PermissionSet,
                                                           fullTrustStrongNames.ToArray());

                // Create an instance of our runner trampoline in the sandbox
                ObjectHandle runnerHandle = Activator.CreateInstanceFrom(sandbox,
                                                                         typeof(AssemblyRunner).Assembly.Location,
                                                                         typeof(AssemblyRunner).FullName);
                AssemblyRunner runner = runnerHandle.Unwrap() as AssemblyRunner;

                // Use the runner to execute the target assembly, and return the result of the assembly's
                // Main method.
                return(runner.ExecuteAssembly(commands.ProgramName, commands.Arguments, commands.NoRunnerOutput));
            }
            catch (Exception ex)
            {
                WriteOutput(String.Format(CultureInfo.CurrentCulture, Resources.GeneralError, ex.Message));
                return(-1);
            }
        }