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

SocketPermission.cs « System.Net « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 56faa80056f17f89495dccd67c2292da4d9fe921 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// System.Net.SocketPermission.cs
//
// Author:
//   Lawrence Pit (loz@cable.a2000.nl)
//

using System;
using System.Collections;
using System.Security;
using System.Security.Permissions;

namespace System.Net
{
	[Serializable]
	public sealed class SocketPermission : CodeAccessPermission, IUnrestrictedPermission
	{
		// Fields
		ArrayList m_acceptList = new ArrayList ();
		ArrayList m_connectList = new ArrayList ();
		bool m_noRestriction;
		
		// Constructors
		public SocketPermission (PermissionState state) : base () 
		{						
			m_noRestriction = (state == PermissionState.Unrestricted);
		}
		
		public SocketPermission (NetworkAccess access, TransportType transport, 
					 string hostName, int portNumber) : base () 
		{
			m_noRestriction = false;
			AddPermission (access, transport, hostName, portNumber);
		}	
		
		// Fields
		public const int AllPorts = -1;
		
		// Properties

		public IEnumerator AcceptList {
			get { return m_acceptList.GetEnumerator (); }
		}

		public IEnumerator ConnectList {
			get { return m_connectList.GetEnumerator (); }
		}
		
		// Methods
		
		public void AddPermission (NetworkAccess access, TransportType transport,
					   string hostName, int portNumber)
		{
			if (m_noRestriction)
				return;
				
			EndpointPermission permission = new EndpointPermission (hostName, portNumber, transport);

			if (access == NetworkAccess.Accept)
				m_acceptList.Add (permission);
			else
				m_connectList.Add (permission);
		}		
		
		public override IPermission Copy ()
		{
			SocketPermission permission;

			permission = new SocketPermission (m_noRestriction ? 
						PermissionState.Unrestricted : 
						PermissionState.None);

			// as EndpointPermission's are immutable it's safe to do a shallow copy.						
			permission.m_connectList = (ArrayList) this.m_connectList.Clone ();
			permission.m_acceptList = (ArrayList) this.m_acceptList.Clone ();

			return permission;		
		}
		
		public override IPermission Intersect (IPermission target)
		{
			if (target == null) 
				return null;
				
			SocketPermission perm = target as SocketPermission;
			if (perm == null) 
				throw new ArgumentException ("Argument not of type SocketPermission");
			
			if (m_noRestriction) 
				return IntersectEmpty (perm) ? null : perm.Copy ();
				
			if (perm.m_noRestriction)
				return IntersectEmpty (this) ? null : this.Copy ();
				
			SocketPermission newperm = new SocketPermission (PermissionState.None);
			Intersect (this.m_connectList, perm.m_connectList, newperm.m_connectList);
			Intersect (this.m_acceptList, perm.m_acceptList, newperm.m_acceptList);
			return IntersectEmpty (newperm) ? null : newperm;			
		}
		
		private bool IntersectEmpty (SocketPermission permission)		
		{
			return !permission.m_noRestriction && 
			       (permission.m_connectList.Count == 0) &&
			       (permission.m_acceptList.Count == 0);
		}
		
		private void Intersect (ArrayList list1, ArrayList list2, ArrayList result)
		{
			foreach (EndpointPermission perm1 in list1) {
				foreach (EndpointPermission perm2 in list2) {
					EndpointPermission perm = perm1.Intersect (perm2);
					if (perm != null) {
						// instead of the below it's also okay to simply do:
						//     result.Add (perm);
						// below is only done to avoid double entries						
						bool replaced = false;
						for (int i = 0; i < result.Count; i++) {
							EndpointPermission res = (EndpointPermission) result [i];
							EndpointPermission resperm = perm.Intersect (res);
							if (resperm != null) {
								result [i] = resperm;
								replaced = true;
								break;
							}
						}
						if (!replaced) 
							result.Add (perm);
					}
				}
			}
		}
		
		public override bool IsSubsetOf (IPermission target) 
		{
			if (target == null)
				return (!m_noRestriction && m_connectList.Count == 0 && m_acceptList.Count ==	 0);
			
			SocketPermission perm = target as SocketPermission;

			if (perm == null) 
				throw new ArgumentException ("Parameter target must be of type SocketPermission");
			
			if (perm.m_noRestriction) 
				return true;

			if (this.m_noRestriction)
				return false;

			if (this.m_acceptList.Count == 0 && this.m_connectList.Count == 0)
				return true;

			if (perm.m_acceptList.Count == 0 && perm.m_connectList.Count == 0)
				return false;

			return IsSubsetOf (this.m_connectList, perm.m_connectList)
			    && IsSubsetOf (this.m_acceptList, perm.m_acceptList);
		}

		private bool IsSubsetOf (ArrayList list1, ArrayList list2)
		{
			foreach (EndpointPermission perm1 in list1) {
				bool issubset = false;
				foreach (EndpointPermission perm2 in list2) 
					if (perm1.IsSubsetOf (perm2)) {
						issubset = true;
						break;
					}
				if (!issubset) 
					return false;
			}
			return true;
		}
		
		public bool IsUnrestricted () 
		{
			return m_noRestriction;
		}

		/*
		
		SocketPermission s = new SocketPermission (NetworkAccess.Connect, TransportType.Tcp, "www.google.com", 80);
		s.AddPermission (NetworkAccess.Accept, TransportType.All, "localhost", 8080);
		s.AddPermission (NetworkAccess.Accept, TransportType.All, "localhost", SocketPermission.AllPorts);
		// s = new SocketPermission (PermissionState.None);
		SecurityElement sec = s.ToXml ();	
		Console.WriteLine (sec.ToString ());

		This is sample xml output:

		<IPermission class="System.Net.SocketPermission, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
			     version="1">
		   <ConnectAccess>
		      <ENDPOINT host="www.google.com"
				transport="Tcp"
				port="80"/>
		   </ConnectAccess>
		   <AcceptAccess>
		      <ENDPOINT host="localhost"
				transport="All"
				port="8080"/>
		      <ENDPOINT host="localhost"
				transport="All"
				port="All"/>
		   </AcceptAccess>
		</IPermission>



		This is a sample unrestricted socketpermission, no matter how many permissions you add:			

		<IPermission class="System.Net.SocketPermission, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
			     version="1"
			     Unrestricted="true"/>


		This is a sample constructed restricted socketpermission with no permissions added:

		<IPermission class="System.Net.SocketPermission, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
			     version="1"/>
		*/
		public override SecurityElement ToXml ()
		{
             
			SecurityElement root = new SecurityElement ("IPermission");

			root.AddAttribute ("class", this.GetType ().AssemblyQualifiedName);
			root.AddAttribute ("version", "1");
			if (m_noRestriction) {
				root.AddAttribute ("Unrestricted", "true");				
				return root;
			}				
				
			if (this.m_connectList.Count > 0)
				ToXml (root, "ConnectAccess", m_connectList.GetEnumerator ());
			
			if (this.m_acceptList.Count > 0) 
				ToXml (root, "AcceptAccess", m_acceptList.GetEnumerator ());			
			
			return root;
		}
		
		private void ToXml (SecurityElement root, string childName, IEnumerator enumerator)
		{
			SecurityElement child = new SecurityElement (childName);
			while (enumerator.MoveNext ()) {
				EndpointPermission perm = enumerator.Current as EndpointPermission;
				SecurityElement grandchild = new SecurityElement ("ENDPOINT");
				grandchild.AddAttribute ("host", perm.Hostname);
				grandchild.AddAttribute ("transport", perm.Transport.ToString ());
				grandchild.AddAttribute ("port", 
						perm.Port == AllPorts 
						? "All" 
						: ((Int32) perm.Port).ToString ());
				child.AddChild (grandchild);
			}
			root.AddChild (child);
		}
		
		public override void FromXml (SecurityElement securityElement)
		{
			if (securityElement == null)
				throw new ArgumentNullException ("securityElement");
				
			// LAMESPEC: it says to throw an ArgumentNullException in this case				
			if (securityElement.Tag != "IPermission")
				throw new ArgumentException ("securityElement");
				
			string classStr = securityElement.Attribute ("class");
			if (classStr == null || !classStr.StartsWith (this.GetType ().FullName + ","))
				throw new ArgumentException ("securityElement");
				
			string unrestricted = securityElement.Attribute ("Unrestricted");
			if (unrestricted != null) {
				this.m_noRestriction = (String.Compare (unrestricted, "true", true) == 0);
				if (this.m_noRestriction)
					return;
			}
			
			this.m_noRestriction = false;
			this.m_connectList = new ArrayList ();
			this.m_acceptList = new ArrayList ();
			
			ArrayList children = securityElement.Children;
			foreach (SecurityElement child in children) {
				if (child.Tag == "ConnectAccess") 
					FromXml (child.Children, NetworkAccess.Connect);
				else if (child.Tag == "AcceptAccess")
					FromXml (child.Children, NetworkAccess.Accept);
			}
		}		
		
		private void FromXml (ArrayList endpoints, NetworkAccess access)
		{
			foreach (SecurityElement endpoint in endpoints) {
				if (endpoint.Tag != "ENDPOINT")
					continue;
				string hostname = endpoint.Attribute ("host");
				TransportType transport = 
					(TransportType) Enum.Parse (typeof (TransportType), 
							            endpoint.Attribute ("transport"), 
							            true);
				string p = endpoint.Attribute ("port");
				int port = 0;
				if (p == "All") 
					port = SocketPermission.AllPorts;
				else
					port = Int32.Parse (p);

				AddPermission (access, transport, hostname, port);
			}
		}
		
		public override IPermission Union (IPermission target) 
		{
			// LAMESPEC: according to spec we should throw an 
			// exception when target is null. We'll follow the
			// behaviour of MS.Net instead of the spec, also
			// because it matches the Intersect behaviour.
			if (target == null)
				return null;
				// throw new ArgumentNullException ("target");
				
			SocketPermission perm = target as SocketPermission;
			if (perm == null)
				throw new ArgumentException ("Argument not of type SocketPermission");
			
			if (this.m_noRestriction || perm.m_noRestriction) 
				return new SocketPermission (PermissionState.Unrestricted);
				
			SocketPermission copy = (SocketPermission) perm.Copy ();
			copy.m_acceptList.InsertRange (copy.m_acceptList.Count, this.m_acceptList);
			copy.m_connectList.InsertRange (copy.m_connectList.Count, this.m_connectList);				
			
			return copy;
		}

	}
}