80f410891de15586043111546f0b43cad5588e8f
[mono.git] / mcs / class / System.Configuration / System.Configuration / InternalConfigurationHost.cs
1 //
2 // System.Configuration.InternalConfigurationHost.cs
3 //
4 // Authors:
5 //  Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
28
29 using System;
30 using System.IO;
31 using System.Security;
32 using System.Configuration.Internal;
33 using System.Runtime.CompilerServices;
34
35 namespace System.Configuration
36 {
37         abstract class InternalConfigurationHost: IInternalConfigHost
38         {
39                 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
40                 {
41                         return null;
42                 }
43                 
44                 public virtual object CreateDeprecatedConfigContext (string configPath)
45                 {
46                         return null;
47                 }
48                 
49                 public virtual void DeleteStream (string streamName)
50                 {
51                         File.Delete (streamName);
52                 }
53                 
54                 string IInternalConfigHost.DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
55                 {
56                         return protectedSection.DecryptSection (encryptedXml, protectionProvider);
57                 }
58                 
59                 string IInternalConfigHost.EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
60                 {
61                         return protectedSection.EncryptSection (clearXml, protectionProvider);
62                 }
63                 
64                 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locationSubPath)
65                 {
66                         return configPath;
67                 }
68                 
69                 public virtual Type GetConfigType (string typeName, bool throwOnError)
70                 {
71                         Type type = Type.GetType (typeName);
72                         if (type == null && throwOnError)
73                                 throw new ConfigurationErrorsException ("Type '" + typeName + "' not found.");
74                         return type;
75                 }
76                 
77                 public virtual string GetConfigTypeName (Type t)
78                 {
79                         return t.AssemblyQualifiedName;
80                 }
81                 
82                 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
83                 {
84                         throw new NotImplementedException ();
85                 }
86                 
87                 public abstract string GetStreamName (string configPath);
88                 public abstract void Init (IInternalConfigRoot root, params object[] hostInitParams);
89                 public abstract void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams);
90
91                 [MonoNotSupported ("mono does not support remote configuration")]
92                 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
93                 {
94                         throw new NotSupportedException ("mono does not support remote configuration");
95                 }
96                 
97                 public virtual object GetStreamVersion (string streamName)
98                 {
99                         throw new NotImplementedException ();
100                 }
101                 
102                 public virtual IDisposable Impersonate ()
103                 {
104                         throw new NotImplementedException ();
105                 }
106                 
107                 public virtual bool IsAboveApplication (string configPath)
108                 {
109                         throw new NotImplementedException ();
110                 }
111                 
112                 public virtual bool IsConfigRecordRequired (string configPath)
113                 {
114                         throw new NotImplementedException ();
115                 }
116                 
117                 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
118                 {
119                         switch (allowDefinition) {
120                                 case ConfigurationAllowDefinition.MachineOnly:
121                                         return configPath == "machine";
122                                 case ConfigurationAllowDefinition.MachineToApplication:
123                                         return configPath == "machine" || configPath == "exe";
124                                 default:
125                                         return true;
126                         }
127                 }
128                 
129                 public virtual bool IsFile (string streamName)
130                 {
131                         throw new NotImplementedException ();
132                 }
133
134                 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
135                 {
136                         throw new NotImplementedException ();
137                 }
138
139                 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
140                 {
141                         throw new NotImplementedException ();
142                 }
143
144                 public virtual bool IsLocationApplicable (string configPath)
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149                 public virtual bool IsRemote {
150                         get {
151                                 throw new NotImplementedException ();
152                         }
153                 }
154
155                 public virtual bool IsSecondaryRoot (string configPath)
156                 {
157                         throw new NotImplementedException ();
158                 }
159
160                 public virtual bool IsTrustedConfigPath (string configPath)
161                 {
162                         throw new NotImplementedException ();
163                 }
164 #if !TARGET_JVM
165                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
166                 extern private static string get_bundled_machine_config ();
167
168                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
169                 extern private static string get_bundled_app_config ();
170 #endif
171                 
172                 public virtual Stream OpenStreamForRead (string streamName)
173                 {
174                         if (String.CompareOrdinal (streamName, System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) == 0) {
175 #if TARGET_JVM
176                                 return (Stream) vmw.common.IOUtils.getStreamForGHConfigs (streamName);
177 #else
178                                 string bundle = get_bundled_machine_config ();
179                                 if (bundle != null)
180                                         return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
181 #endif
182                         }
183
184                         if (String.CompareOrdinal (streamName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == 0) {
185 #if TARGET_JVM
186                                 throw new NotImplementedException();
187 #else
188                                 string bundle = get_bundled_app_config ();
189                                 if (bundle != null)
190                                         return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
191 #endif
192                         }
193
194                         if (!File.Exists (streamName))
195                                 return null;
196                                 
197                         return new FileStream (streamName, FileMode.Open, FileAccess.Read);
198                 }
199
200                 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
201                 {
202                         throw new NotImplementedException ();
203                 }
204                 
205                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
206                 {
207                         string dir = Path.GetDirectoryName (streamName);
208                         if (!String.IsNullOrEmpty (dir) && !Directory.Exists (dir))
209                                 Directory.CreateDirectory (dir);
210                         return new FileStream (streamName, FileMode.Create, FileAccess.Write);
211                 }
212
213                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
214                 {
215                         throw new NotImplementedException ();
216                 }
217                 
218                 public virtual bool PrefetchAll (string configPath, string streamName)
219                 {
220                         throw new NotImplementedException ();
221                 }
222                 
223                 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
224                 {
225                         throw new NotImplementedException ();
226                 }
227
228                 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
229                 {
230                         throw new NotImplementedException ();
231                 }
232
233                 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
234                 {
235                         throw new NotImplementedException ();
236                 }
237                 
238                 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
239                 {
240                         throw new NotImplementedException ();
241                 }
242                 
243                 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
244                 {
245                         if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
246                                 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
247                 }
248                 
249                 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
250                 {
251                 }
252
253                 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
254                 {
255                 }
256                 
257                 public virtual bool SupportsChangeNotifications {
258                         get { return false; }
259                 }
260                 
261                 public virtual bool SupportsLocation {
262                         get { return false; }
263                 }
264                 
265                 public virtual bool SupportsPath {
266                         get { return false; }
267                 }
268                 
269                 public virtual bool SupportsRefresh {
270                         get { return false; }
271                 }
272         }
273         
274         class ExeConfigurationHost: InternalConfigurationHost
275         {
276                 ExeConfigurationFileMap map;
277                 ConfigurationUserLevel level;
278                 
279                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
280                 {
281                         map = (ExeConfigurationFileMap) hostInitParams [0];
282                         level = (ConfigurationUserLevel) hostInitParams [1];
283                         CheckFileMap (level, map);
284                 }
285
286                 static void CheckFileMap (ConfigurationUserLevel level, ExeConfigurationFileMap map)
287                 {
288                         switch (level) {
289                         case ConfigurationUserLevel.None:
290                                 if (string.IsNullOrEmpty (map.ExeConfigFilename))
291                                         throw new ArgumentException (
292                                                 "The 'ExeConfigFilename' argument cannot be null.");
293                                 break;
294                         case ConfigurationUserLevel.PerUserRoamingAndLocal:
295                                 if (string.IsNullOrEmpty (map.LocalUserConfigFilename))
296                                         throw new ArgumentException (
297                                                 "The 'LocalUserConfigFilename' argument cannot be null.");
298                                 goto case ConfigurationUserLevel.PerUserRoaming;
299                         case ConfigurationUserLevel.PerUserRoaming:
300                                 if (string.IsNullOrEmpty (map.RoamingUserConfigFilename))
301                                         throw new ArgumentException (
302                                                 "The 'RoamingUserConfigFilename' argument cannot be null.");
303                                 goto case ConfigurationUserLevel.None;
304                         }
305                 }
306                 
307                 public override string GetStreamName (string configPath)
308                 {
309                         switch (configPath) {
310                                 case "exe": return map.ExeConfigFilename; 
311                                 case "local": return map.LocalUserConfigFilename;
312                                 case "roaming": return map.RoamingUserConfigFilename;
313                                 case "machine": return map.MachineConfigFilename;
314                                 default://return map.ExeConfigFilename;
315                                 switch (level) {
316                                         case ConfigurationUserLevel.None:
317                                                 return map.ExeConfigFilename; 
318                                         case ConfigurationUserLevel.PerUserRoaming:
319                                                 return map.RoamingUserConfigFilename;
320                                         case ConfigurationUserLevel.PerUserRoamingAndLocal:
321                                                 return map.LocalUserConfigFilename;
322                                         default:
323                                                 return map.MachineConfigFilename;
324                                 }
325                         }
326                 }
327                 
328                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
329                 {
330                         map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
331
332                         if (hostInitConfigurationParams.Length > 1 && 
333                             hostInitConfigurationParams [1] is ConfigurationUserLevel)
334                                 level = (ConfigurationUserLevel) hostInitConfigurationParams [1];
335
336                         CheckFileMap (level, map);
337
338                         if (locationSubPath == null)
339                                 switch (level) {
340                                 case ConfigurationUserLevel.PerUserRoaming:
341                                         if (map.RoamingUserConfigFilename == null)
342                                                 throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
343                                         locationSubPath = "roaming";
344                                         break;
345                                 case ConfigurationUserLevel.PerUserRoamingAndLocal:
346                                         if (map.LocalUserConfigFilename == null)
347                                                 throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
348                                         locationSubPath = "local";
349                                         break;
350                                 }
351
352                         configPath = null;
353                         string next = null;
354
355                         locationConfigPath = null;
356
357                         if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
358                                 configPath = "exe";
359                                 next = "machine";
360                                 locationConfigPath = map.ExeConfigFilename;
361                         }
362                         
363                         if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
364                                 configPath = "local";
365                                 next = "roaming";
366                                 locationConfigPath = map.LocalUserConfigFilename;
367                         }
368                         
369                         if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
370                                 configPath = "roaming";
371                                 next = "exe";
372                                 locationConfigPath = map.RoamingUserConfigFilename;
373                         }
374                         
375                         if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
376                                 configPath = "machine";
377                                 next = null;
378                         }
379                         locationSubPath = next;
380                 }
381         }
382         
383         class MachineConfigurationHost: InternalConfigurationHost
384         {
385                 ConfigurationFileMap map;
386                 
387                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
388                 {
389                         map = (ConfigurationFileMap) hostInitParams [0];
390                 }
391                 
392                 public override string GetStreamName (string configPath)
393                 {
394                         return map.MachineConfigFilename;
395                 }
396                 
397                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
398                 {
399                         map = (ConfigurationFileMap) hostInitConfigurationParams [0];
400                         locationSubPath = null;
401                         configPath = null;
402                         locationConfigPath = null;
403                 }
404
405                 public override bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
406                 {
407                         return true;
408                 }
409         }
410 }
411