2 // System.ServiceProcess.ServiceControllerPermission.cs
3 // (based on System.Diagnostics.EventLogPermission.cs)
6 // Jonathan Pryor (jonpryor@vt.edu)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // Sebastien Pouliot <sebastien@ximian.com>
10 // (C) 2002 Jonathan Pryor
11 // (C) 2003 Andreas Nahr
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Security.Permissions;
36 namespace System.ServiceProcess {
39 public sealed class ServiceControllerPermission : ResourcePermissionBase {
41 ServiceControllerPermissionEntryCollection innerCollection;
43 public ServiceControllerPermission ()
48 public ServiceControllerPermission (ServiceControllerPermissionEntry[] permissionAccessEntries)
50 if (permissionAccessEntries == null)
51 throw new ArgumentNullException ("permissionAccessEntries");
54 innerCollection = new ServiceControllerPermissionEntryCollection (this);
55 innerCollection.AddRange (permissionAccessEntries);
58 public ServiceControllerPermission (PermissionState state)
64 public ServiceControllerPermission (ServiceControllerPermissionAccess permissionAccess, string machineName, string serviceName)
67 ServiceControllerPermissionEntry scpe = new ServiceControllerPermissionEntry (permissionAccess, machineName, serviceName);
68 innerCollection = new ServiceControllerPermissionEntryCollection (this);
69 innerCollection.Add (scpe);
72 public ServiceControllerPermissionEntryCollection PermissionEntries {
74 if (innerCollection == null) {
75 // must be here to work with XML deserialization
76 innerCollection = new ServiceControllerPermissionEntryCollection (this);
78 return innerCollection;
86 TagNames = new string [2] { "Machine", "Service" };
87 PermissionAccessType = typeof (ServiceControllerPermissionAccess);
90 internal ResourcePermissionBaseEntry[] GetEntries ()
92 return base.GetPermissionEntries ();
95 internal void ClearEntries ()
100 internal void Add (object obj)
102 ServiceControllerPermissionEntry cspe = (obj as ServiceControllerPermissionEntry);
103 base.AddPermissionAccess (cspe.GetBaseEntry ());
106 internal void Remove (object obj)
108 ServiceControllerPermissionEntry cspe = (obj as ServiceControllerPermissionEntry);
109 base.RemovePermissionAccess (cspe.GetBaseEntry ());
114 private static char[] invalidChars = new char[] { '\t', '\n', '\v', '\f', '\r', ' ', '\\', '\x160' };
116 internal static void ValidateMachineName (string name)
118 // FIXME: maybe other checks are required (but not documented)
119 if ((name == null) || (name.Length == 0) || (name.IndexOfAny (invalidChars) != -1)) {
120 string msg = Locale.GetText ("Invalid machine name '{0}'.");
123 msg = String.Format (msg, name);
124 throw new ArgumentException (msg, "MachineName");
128 private static char[] invalidServiceNameChars = new char[] { '/', '\\' };
130 internal static void ValidateServiceName (string name)
133 throw new ArgumentNullException ("ServiceName");
134 // FIXME: maybe other checks are required (but not documented)
135 if ((name.Length == 0) || (name.IndexOfAny (invalidServiceNameChars) != -1)) {
136 string msg = String.Format (Locale.GetText ("Invalid service name '{0}'."), name);
137 throw new ArgumentException (msg, "ServiceName");