2 // System.Messaging.MessageQueuePermission.cs
\r
5 // Peter Van Isacker (sclytrack@planetinternet.be)
\r
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Peter Van Isacker
\r
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Security;
\r
33 using System.Security.Permissions;
\r
35 namespace System.Messaging {
38 public sealed class MessageQueuePermission: CodeAccessPermission, IUnrestrictedPermission {
40 private const int version = 1;
42 private MessageQueuePermissionEntryCollection _list;
43 private bool _unrestricted;
45 public MessageQueuePermission ()
\r
47 _list = new MessageQueuePermissionEntryCollection (this);
\r
50 public MessageQueuePermission (MessageQueuePermissionEntry[] permissionAccessEntries)
53 foreach (MessageQueuePermissionEntry entry in permissionAccessEntries)
57 public MessageQueuePermission (PermissionState state)
\r
60 _unrestricted = (state == PermissionState.Unrestricted);
63 public MessageQueuePermission (MessageQueuePermissionAccess permissionAccess, string path)
\r
66 MessageQueuePermissionEntry entry = new MessageQueuePermissionEntry (permissionAccess, path);
70 public MessageQueuePermission (MessageQueuePermissionAccess permissionAccess,
\r
71 string machineName, string label, string category) : this ()
\r
73 MessageQueuePermissionEntry entry = new MessageQueuePermissionEntry (permissionAccess, machineName, label, category);
77 public MessageQueuePermissionEntryCollection PermissionEntries {
\r
78 get { return _list; }
\r
81 public override IPermission Copy ()
\r
84 return new MessageQueuePermission (PermissionState.Unrestricted);
86 MessageQueuePermission copy = new MessageQueuePermission (PermissionState.None);
87 foreach (MessageQueuePermissionEntry entry in _list)
88 copy._list.Add (entry);
93 public bool IsUnrestricted ()
\r
95 return _unrestricted;
\r
99 public override void FromXml (SecurityElement securityElement)
\r
101 CheckSecurityElement (securityElement, "securityElement", version, version);
102 // Note: we do not (yet) care about the return value
103 // as we only accept version 1 (min/max values)
105 _unrestricted = (IsUnrestricted (securityElement));
107 // TODO read elements
111 public override IPermission Intersect (IPermission target)
\r
113 MessageQueuePermission mqp = Cast (target);
\r
118 public override bool IsSubsetOf (IPermission target)
\r
120 MessageQueuePermission mqp = Cast (target);
\r
125 public override SecurityElement ToXml ()
\r
127 SecurityElement se = Element (version);
129 se.AddAttribute ("Unrestricted", "true");
137 public override IPermission Union (IPermission target)
\r
139 MessageQueuePermission mqp = Cast (target);
\r
145 private bool IsEmpty ()
147 return (!_unrestricted && (_list.Count == 0));
150 private MessageQueuePermission Cast (IPermission target)
155 MessageQueuePermission mqp = (target as MessageQueuePermission);
157 ThrowInvalidPermission (target, typeof (MessageQueuePermission));
165 private static char[] invalidChars = new char[] { '\t', '\n', '\v', '\f', '\r', ' ', '\\', '\x160' };
167 internal static void ValidateMachineName (string name)
169 // FIXME: maybe other checks are required (but not documented)
170 if ((name == null) || (name.Length == 0) || (name.IndexOfAny (invalidChars) != -1)) {
171 string msg = Locale.GetText ("Invalid machine name '{0}'.");
174 msg = String.Format (msg, name);
175 throw new ArgumentException (msg, "MachineName");
179 internal static void ValidatePath (string path)
181 // FIXME: maybe other checks are required (but not documented)
182 if ((path.Length > 0) && (path [0] != '\\')) {
183 string msg = Locale.GetText ("Invalid path '{0}'.");
184 throw new ArgumentException (String.Format (msg, path), "Path");
188 // NOTE: The following static methods should be moved out to a (static?) class
189 // if (ever) System.Drawing.dll gets more than one permission in it's assembly.
191 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
192 internal SecurityElement Element (int version)
194 SecurityElement se = new SecurityElement ("IPermission");
195 Type type = this.GetType ();
196 se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
197 se.AddAttribute ("version", version.ToString ());
201 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
205 case PermissionState.None:
207 case PermissionState.Unrestricted:
208 if (!allowUnrestricted) {
209 msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
210 throw new ArgumentException (msg, "state");
214 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
215 throw new ArgumentException (msg, "state");
220 // logic isn't identical to CodeAccessPermission.CheckSecurityElement - see unit tests
221 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
224 throw new ArgumentNullException (parameterName);
226 if (se.Attribute ("class") == null) {
227 string msg = Locale.GetText ("Missing 'class' attribute.");
228 throw new ArgumentException (msg, parameterName);
231 // we assume minimum version if no version number is supplied
232 int version = minimumVersion;
233 string v = se.Attribute ("version");
236 version = Int32.Parse (v);
238 catch (Exception e) {
239 string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
240 msg = String.Format (msg, v);
241 throw new ArgumentException (msg, parameterName, e);
245 if ((version < minimumVersion) || (version > maximumVersion)) {
246 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
247 msg = String.Format (msg, version, minimumVersion, maximumVersion);
248 throw new ArgumentException (msg, parameterName);
253 // must be called after CheckSecurityElement (i.e. se != null)
254 internal static bool IsUnrestricted (SecurityElement se)
256 string value = se.Attribute ("Unrestricted");
259 return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
262 internal static void ThrowInvalidPermission (IPermission target, Type expected)
264 string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
265 msg = String.Format (msg, target.GetType (), expected);
266 throw new ArgumentException (msg, "target");