Welcome to mirror list, hosted at ThFree Co, Russian Federation.

java.security.cs « openjdk « runtime - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e794d881c6b641fe8cebe46f3e4900ac4892f9c (plain)
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
/*
  Copyright (C) 2007-2013 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using IKVM.Internal;

static class Java_java_security_AccessController
{
	public static object getStackAccessControlContext(java.security.AccessControlContext context, ikvm.@internal.CallerID callerID)
	{
#if FIRST_PASS
		return null;
#else
		List<java.security.ProtectionDomain> array = new List<java.security.ProtectionDomain>();
		bool is_privileged = GetProtectionDomains(array, callerID, new StackTrace(1));
		if (array.Count == 0)
		{
			if (is_privileged && context == null)
			{
				return null;
			}
		}
		return CreateAccessControlContext(array, is_privileged, context);
#endif
	}

#if !FIRST_PASS
	private static bool GetProtectionDomains(List<java.security.ProtectionDomain> array, ikvm.@internal.CallerID callerID, StackTrace stack)
	{
		// first we have to skip all AccessController related frames, because we can be called from a doPrivileged implementation (not the privileged action)
		// in which case we should ignore the doPrivileged frame
		int skip = 0;
		for (; skip < stack.FrameCount; skip++)
		{
			Type type  = stack.GetFrame(skip).GetMethod().DeclaringType;
			if (type != typeof(Java_java_security_AccessController) && type != typeof(java.security.AccessController))
			{
				break;
			}
		}
		java.security.ProtectionDomain previous_protection_domain = null;
		for (int i = skip; i < stack.FrameCount; i++)
		{
			bool is_privileged = false;
			java.security.ProtectionDomain protection_domain;
			MethodBase method = stack.GetFrame(i).GetMethod();
			if (method.DeclaringType == typeof(java.security.AccessController)
				&& method.Name == "doPrivileged")
			{
				is_privileged = true;
				java.lang.Class caller = callerID.getCallerClass();
				protection_domain = caller == null ? null : Java_java_lang_Class.getProtectionDomain0(caller);
			}
			else if (Java_sun_reflect_Reflection.IsHideFromStackWalk(method))
			{
				continue;
			}
			else
			{
				protection_domain = GetProtectionDomainFromType(method.DeclaringType);
			}

			if (previous_protection_domain != protection_domain && protection_domain != null)
			{
				previous_protection_domain = protection_domain;
				array.Add(protection_domain);
			}

			if (is_privileged)
			{
				return true;
			}
		}
		return false;
	}

	private static object CreateAccessControlContext(List<java.security.ProtectionDomain> context, bool is_privileged, java.security.AccessControlContext privileged_context)
	{
		java.security.AccessControlContext acc = new java.security.AccessControlContext(context == null || context.Count == 0 ? null : context.ToArray(), is_privileged);
		acc._privilegedContext(privileged_context);
		return acc;
	}

	private static java.security.ProtectionDomain GetProtectionDomainFromType(Type type)
	{
		if (type == null
			|| type.Assembly == typeof(object).Assembly
			|| type.Assembly == typeof(Java_java_security_AccessController).Assembly
			|| type.Assembly == Java_java_lang_SecurityManager.jniAssembly
			|| type.Assembly == typeof(java.lang.Thread).Assembly)
		{
			return null;
		}
		TypeWrapper tw = ClassLoaderWrapper.GetWrapperFromType(type);
		if (tw != null)
		{
			return Java_java_lang_Class.getProtectionDomain0(tw.ClassObject);
		}
		return null;
	}
#endif

	public static object getInheritedAccessControlContext()
	{
#if FIRST_PASS
		return null;
#else
		object inheritedAccessControlContext = java.lang.Thread.currentThread().inheritedAccessControlContext;
		java.security.AccessControlContext acc = inheritedAccessControlContext as java.security.AccessControlContext;
		if (acc != null)
		{
			return acc;
		}
		java.security.AccessController.LazyContext lc = inheritedAccessControlContext as java.security.AccessController.LazyContext;
		if (lc == null)
		{
			return null;
		}
		List<java.security.ProtectionDomain> list = new List<java.security.ProtectionDomain>();
		while (lc != null)
		{
			if (GetProtectionDomains(list, lc.callerID, lc.stackTrace))
			{
				return CreateAccessControlContext(list, true, lc.context);
			}
			lc = lc.parent;
		}
		return CreateAccessControlContext(list, false, null);
#endif
	}
}