2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.ServiceProcess / System.ServiceProcess / ServiceControllerPermission.cs
1 //
2 // System.ServiceProcess.ServiceControllerPermission.cs
3 //      (based on System.Diagnostics.EventLogPermission.cs)
4 //
5 // Authors:
6 //      Jonathan Pryor (jonpryor@vt.edu)
7 //      Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //      Sebastien Pouliot  <sebastien@ximian.com>
9 //
10 // (C) 2002 Jonathan Pryor
11 // (C) 2003 Andreas Nahr
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34 using System.Security.Permissions;
35
36 namespace System.ServiceProcess {
37
38         [Serializable]
39         public sealed class ServiceControllerPermission : ResourcePermissionBase {
40
41                 ServiceControllerPermissionEntryCollection innerCollection;
42
43                 public ServiceControllerPermission ()
44                 {
45                         SetUp ();
46                 }
47
48                 public ServiceControllerPermission (ServiceControllerPermissionEntry[] permissionAccessEntries)
49                 {
50                         if (permissionAccessEntries == null)
51                                 throw new ArgumentNullException ("permissionAccessEntries");
52
53                         SetUp ();
54                         innerCollection = new ServiceControllerPermissionEntryCollection (this);
55                         innerCollection.AddRange (permissionAccessEntries);
56                 }
57
58                 public ServiceControllerPermission (PermissionState state)
59                         : base (state)
60                 {
61                         SetUp ();
62                 }
63
64                 public ServiceControllerPermission (ServiceControllerPermissionAccess permissionAccess, string machineName, string serviceName)
65                 {
66                         SetUp ();
67                         ServiceControllerPermissionEntry scpe = new ServiceControllerPermissionEntry (permissionAccess, machineName, serviceName);
68                         innerCollection = new ServiceControllerPermissionEntryCollection (this);
69                         innerCollection.Add (scpe);
70                 }
71
72                 public ServiceControllerPermissionEntryCollection PermissionEntries {
73                         get {
74                                 if (innerCollection == null) {
75                                         // must be here to work with XML deserialization
76                                         innerCollection = new ServiceControllerPermissionEntryCollection (this);
77                                 }
78                                 return innerCollection;
79                         }
80                 }
81
82                 // private stuff
83
84                 private void SetUp () 
85                 {
86                         TagNames = new string [2] { "Machine", "Service" };
87                         PermissionAccessType = typeof (ServiceControllerPermissionAccess);
88                 }
89
90                 internal ResourcePermissionBaseEntry[] GetEntries ()
91                 {
92                         return base.GetPermissionEntries ();
93                 }
94
95                 internal void ClearEntries ()
96                 {
97                         base.Clear ();
98                 }
99
100                 internal void Add (object obj) 
101                 {
102                         ServiceControllerPermissionEntry cspe = (obj as ServiceControllerPermissionEntry);
103                         base.AddPermissionAccess (cspe.GetBaseEntry ());
104                 }
105
106                 internal void Remove (object obj) 
107                 {
108                         ServiceControllerPermissionEntry cspe = (obj as ServiceControllerPermissionEntry);
109                         base.RemovePermissionAccess (cspe.GetBaseEntry ());
110                 }
111
112                 // static helpers
113
114                 private static char[] invalidChars = new char[] { '\t', '\n', '\v', '\f', '\r', ' ', '\\', '\x160' };
115
116                 internal static void ValidateMachineName (string name)
117                 {
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}'.");
121                                 if (name == null)
122                                         name = "(null)";
123                                 msg = String.Format (msg, name);
124                                 throw new ArgumentException (msg, "MachineName");
125                         }
126                 }
127
128                 private static char[] invalidServiceNameChars = new char[] { '/', '\\' };
129
130                 internal static void ValidateServiceName (string name)
131                 {
132                         if (name == null)
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");
138                         }
139                 }
140         }
141 }