sha1 supprt checkpoint
[certmaster.git] / certmaster / config.py
index 8202457..c6e9174 100644 (file)
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-# Copyright 2002 Duke University 
+# Copyright 2002 Duke University
 # filched from yum  - menno smits wrote this - he rocks
 
 
 import os
 import sys
 # filched from yum  - menno smits wrote this - he rocks
 
 
 import os
 import sys
-import warnings
 import copy
 import urlparse
 from ConfigParser import NoSectionError, NoOptionError, ConfigParser
 from ConfigParser import ParsingError
 import exceptions
 
 import copy
 import urlparse
 from ConfigParser import NoSectionError, NoOptionError, ConfigParser
 from ConfigParser import ParsingError
 import exceptions
 
-CONFIG_FILE = "/etc/func/certmaster.conf"
+CONFIG_FILE = "/etc/certmaster/certmaster.conf"
 
 class ConfigError(exceptions.Exception):
     def __init__(self, value=None):
 
 class ConfigError(exceptions.Exception):
     def __init__(self, value=None):
@@ -32,13 +31,13 @@ class ConfigError(exceptions.Exception):
         self.value = value
     def __str__(self):
         return "%s" %(self.value,)
         self.value = value
     def __str__(self):
         return "%s" %(self.value,)
-   
+
+
 class Option(object):
     '''
     This class handles a single Yum configuration file option. Create
     subclasses for each type of supported configuration option.
 class Option(object):
     '''
     This class handles a single Yum configuration file option. Create
     subclasses for each type of supported configuration option.
-    
+
     Python descriptor foo (__get__ and __set__) is used to make option
     definition easy and consise.
     '''
     Python descriptor foo (__get__ and __set__) is used to make option
     definition easy and consise.
     '''
@@ -55,12 +54,12 @@ class Option(object):
         self._attrname = '__opt%d' % id(self)
 
     def __get__(self, obj, objtype):
         self._attrname = '__opt%d' % id(self)
 
     def __get__(self, obj, objtype):
-        '''Called when the option is read (via the descriptor protocol). 
+        '''Called when the option is read (via the descriptor protocol).
 
         @param obj: The configuration instance to modify.
         @param objtype: The type of the config instance (not used).
         @return: The parsed option value or the default value if the value
 
         @param obj: The configuration instance to modify.
         @param objtype: The type of the config instance (not used).
         @return: The parsed option value or the default value if the value
-            wasn't set in the configuration file.
+            was not set in the configuration file.
         '''
         if obj is None:
             return self
         '''
         if obj is None:
             return self
@@ -68,7 +67,7 @@ class Option(object):
         return getattr(obj, self._attrname, None)
 
     def __set__(self, obj, value):
         return getattr(obj, self._attrname, None)
 
     def __set__(self, obj, value):
-        '''Called when the option is set (via the descriptor protocol). 
+        '''Called when the option is set (via the descriptor protocol).
 
         @param obj: The configuration instance to modify.
         @param value: The value to set the option to.
 
         @param obj: The configuration instance to modify.
         @param value: The value to set the option to.
@@ -85,8 +84,8 @@ class Option(object):
         setattr(obj, self._attrname, value)
 
     def setup(self, obj, name):
         setattr(obj, self._attrname, value)
 
     def setup(self, obj, name):
-        '''Initialise the option for a config instance. 
-        This must be called before the option can be set or retrieved. 
+        '''Initialise the option for a config instance.
+        This must be called before the option can be set or retrieved.
 
         @param obj: BaseConfig (or subclass) instance.
         @param name: Name of the option.
 
         @param obj: BaseConfig (or subclass) instance.
         @param name: Name of the option.
@@ -105,7 +104,7 @@ class Option(object):
 
         @param s: Raw string value to parse.
         @return: Validated native value.
 
         @param s: Raw string value to parse.
         @return: Validated native value.
-    
+
         Will raise ValueError if there was a problem parsing the string.
         Subclasses should override this.
         '''
         Will raise ValueError if there was a problem parsing the string.
         Subclasses should override this.
         '''
@@ -164,7 +163,7 @@ class UrlOption(Option):
     This option handles lists of URLs with validation of the URL scheme.
     '''
 
     This option handles lists of URLs with validation of the URL scheme.
     '''
 
-    def __init__(self, default=None, schemes=('http', 'ftp', 'file', 'https'), 
+    def __init__(self, default=None, schemes=('http', 'ftp', 'file', 'https'),
             allow_none=False):
         super(UrlOption, self).__init__(default)
         self.schemes = schemes
             allow_none=False):
         super(UrlOption, self).__init__(default)
         self.schemes = schemes
@@ -208,7 +207,7 @@ class UrlListOption(ListOption):
 
         # Hold a UrlOption instance to assist with parsing
         self._urloption = UrlOption(schemes=schemes)
 
         # Hold a UrlOption instance to assist with parsing
         self._urloption = UrlOption(schemes=schemes)
-        
+
     def parse(self, s):
         out = []
         for url in super(UrlListOption, self).parse(s):
     def parse(self, s):
         out = []
         for url in super(UrlListOption, self).parse(s):
@@ -255,7 +254,7 @@ class SelectionOption(Option):
     def __init__(self, default=None, allowed=()):
         super(SelectionOption, self).__init__(default)
         self._allowed = allowed
     def __init__(self, default=None, allowed=()):
         super(SelectionOption, self).__init__(default)
         self._allowed = allowed
-        
+
     def parse(self, s):
         if s not in self._allowed:
             raise ValueError('"%s" is not an allowed value' % s)
     def parse(self, s):
         if s not in self._allowed:
             raise ValueError('"%s" is not an allowed value' % s)
@@ -276,7 +275,7 @@ class BytesOption(Option):
         The input should be a string containing a (possibly floating point)
         number followed by an optional single character unit. Valid units are
         'k', 'M', 'G'. Case is ignored.
         The input should be a string containing a (possibly floating point)
         number followed by an optional single character unit. Valid units are
         'k', 'M', 'G'. Case is ignored.
-       
+
         Valid inputs: 100, 123M, 45.6k, 12.4G, 100K, 786.3, 0
         Invalid inputs: -10, -0.1, 45.6L, 123Mb
 
         Valid inputs: 100, 123M, 45.6k, 12.4G, 100K, 786.3, 0
         Invalid inputs: -10, -0.1, 45.6L, 123Mb
 
@@ -298,7 +297,7 @@ class BytesOption(Option):
         else:
             n = s
             mult = 1
         else:
             n = s
             mult = 1
-             
+
         try:
             n = float(n)
         except ValueError:
         try:
             n = float(n)
         except ValueError:
@@ -313,7 +312,7 @@ class BytesOption(Option):
 class ThrottleOption(BytesOption):
 
     def parse(self, s):
 class ThrottleOption(BytesOption):
 
     def parse(self, s):
-        """Get a throttle option. 
+        """Get a throttle option.
 
         Input may either be a percentage or a "friendly bandwidth value" as
         accepted by the BytesOption.
 
         Input may either be a percentage or a "friendly bandwidth value" as
         accepted by the BytesOption.
@@ -382,7 +381,7 @@ class BaseConfig(object):
                 # No matching option in this section, try inheriting
                 if parent and option.inherit:
                     value = getattr(parent, name)
                 # No matching option in this section, try inheriting
                 if parent and option.inherit:
                     value = getattr(parent, name)
-               
+
             if value is not None:
                 setattr(self, name, value)
 
             if value is not None:
                 setattr(self, name, value)
 
@@ -397,7 +396,7 @@ class BaseConfig(object):
     optionobj = classmethod(optionobj)
 
     def isoption(cls, name):
     optionobj = classmethod(optionobj)
 
     def isoption(cls, name):
-        '''Return True if the given name refers to a defined option 
+        '''Return True if the given name refers to a defined option
         '''
         try:
             cls.optionobj(name)
         '''
         try:
             cls.optionobj(name)
@@ -438,7 +437,7 @@ class BaseConfig(object):
                 raise ValueError("not populated, don't know section")
             section = self._section
 
                 raise ValueError("not populated, don't know section")
             section = self._section
 
-        # Updated the ConfigParser with the changed values    
+        # Updated the ConfigParser with the changed values
         cfgOptions = self.cfg.options(section)
         for name,value in self.iteritems():
             option = self.optionobj(name)
         cfgOptions = self.cfg.options(section)
         for name,value in self.iteritems():
             option = self.optionobj(name)
@@ -448,7 +447,7 @@ class BaseConfig(object):
         self.cfg.write(fileobj)
 
     def getConfigOption(self, option, default=None):
         self.cfg.write(fileobj)
 
     def getConfigOption(self, option, default=None):
-        warnings.warn('getConfigOption() will go away in a future version of Yum.\n'
+        warnings.warn('getConfigOption() will go away in a future version of certmaster.\n'
                 'Please access option values as attributes or using getattr().',
                 DeprecationWarning)
         if hasattr(self, option):
                 'Please access option values as attributes or using getattr().',
                 DeprecationWarning)
         if hasattr(self, option):
@@ -456,7 +455,7 @@ class BaseConfig(object):
         return default
 
     def setConfigOption(self, option, value):
         return default
 
     def setConfigOption(self, option, value):
-        warnings.warn('setConfigOption() will go away in a future version of Yum.\n'
+        warnings.warn('setConfigOption() will go away in a future version of certmaster.\n'
                 'Please set option values as attributes or using setattr().',
                 DeprecationWarning)
         if hasattr(self, option):
                 'Please set option values as attributes or using setattr().',
                 DeprecationWarning)
         if hasattr(self, option):
@@ -475,4 +474,34 @@ def read_config(config_file, BaseConfigDerived):
             print >> sys.stderr, "Error reading config file: %s" % e
             sys.exit(1)
     opts.populate(confparser, 'main')
             print >> sys.stderr, "Error reading config file: %s" % e
             sys.exit(1)
     opts.populate(confparser, 'main')
+
+    ## build up the cas structure
+    opts.ca = {}
+
+    ## Add the default items when just using a single ca
+    opts.ca[''] = BaseConfigDerived()
+    opts.ca[''].hash_function = None
+    opts.ca[''].populate(confparser,'main')
+
+    if opts.ca[''].hash_function == 'sha1':
+        log.warning('hash_function value of sha1 is deprecated', DeprecationWarning)
+    elif opts.ca[''].hash_function == 'md5':
+        print >> sys.stderr, "Error:  hash_function of md5 is not supported" 
+
+    ## Add additonal ca sections
+    sections = confparser.sections()
+    for a_section in sections:
+        if a_section.startswith('ca:'):
+            ca_name = a_section[3:]
+            opts.ca[ca_name] = BaseConfigDerived()
+            opts.ca[ca_name].hash_function = None
+            opts.ca[ca_name].populate(confparser,a_section)
+            opts.ca[ca_name].cakey = None
+            opts.ca[ca_name].cacert = None
+
+            if opts.ca[ca_name].hash_function == 'sha1':
+                warnings.warn('hash_function value of sha1 is deprecated in ca:%s section' % ca_name, DeprecationWarning)
+            elif opts.ca[ca_name].hash_function == 'md5':
+                print >> sys.stderr, "Error:  hash_function of md5 is not supported in ca:% section" % ca_name 
+    
     return opts
     return opts