Package ldaptor :: Module config
[hide private]
[frames] | no frames]

Source Code for Module ldaptor.config

  1  import os.path 
  2  import ConfigParser 
  3  from zope.interface import implements 
  4  from ldaptor import interfaces 
  5  from ldaptor.insensitive import InsensitiveString 
  6  from ldaptor.protocols.ldap import distinguishedname 
  7   
8 -class MissingBaseDNError(Exception):
9 """Configuration must specify a base DN""" 10
11 - def __str__(self):
12 return self.__doc__
13
14 -class LDAPConfig(object):
15 implements(interfaces.ILDAPConfig) 16 17 baseDN = None 18 identityBaseDN = None 19 identitySearch = None 20
21 - def __init__(self, 22 baseDN=None, 23 serviceLocationOverrides=None, 24 identityBaseDN=None, 25 identitySearch=None):
26 if baseDN is not None: 27 baseDN = distinguishedname.DistinguishedName(baseDN) 28 self.baseDN = baseDN 29 self.serviceLocationOverrides = {} 30 if serviceLocationOverrides is not None: 31 for k,v in serviceLocationOverrides.items(): 32 dn = distinguishedname.DistinguishedName(k) 33 self.serviceLocationOverrides[dn]=v 34 if identityBaseDN is not None: 35 identityBaseDN = distinguishedname.DistinguishedName(identityBaseDN) 36 self.identityBaseDN = identityBaseDN 37 if identitySearch is not None: 38 self.identitySearch = identitySearch
39
40 - def getBaseDN(self):
41 if self.baseDN is not None: 42 return self.baseDN 43 44 cfg = loadConfig() 45 try: 46 return cfg.get('ldap', 'base') 47 except (ConfigParser.NoOptionError, 48 ConfigParser.NoSectionError): 49 raise MissingBaseDNError
50
52 r = self._loadServiceLocationOverrides() 53 r.update(self.serviceLocationOverrides) 54 return r
55
57 serviceLocationOverride = {} 58 cfg = loadConfig() 59 for section in cfg.sections(): 60 if section.lower().startswith('service-location '): 61 base = section[len('service-location '):].strip() 62 63 host = None 64 if cfg.has_option(section, 'host'): 65 host = cfg.get(section, 'host') 66 if not host: 67 host = None 68 69 port = None 70 if cfg.has_option(section, 'port'): 71 port = cfg.get(section, 'port') 72 if not port: 73 port = None 74 75 dn = distinguishedname.DistinguishedName(stringValue=base) 76 serviceLocationOverride[dn]=(host, port) 77 return serviceLocationOverride
78
79 - def copy(self, **kw):
80 if 'baseDN' not in kw: 81 kw['baseDN'] = self.baseDN 82 if 'serviceLocationOverrides' not in kw: 83 kw['serviceLocationOverrides'] = self.serviceLocationOverrides 84 if 'identityBaseDN' not in kw: 85 kw['identityBaseDN'] = self.identityBaseDN 86 if 'identitySearch' not in kw: 87 kw['identitySearch'] = self.identitySearch 88 r = self.__class__(**kw) 89 return r
90
91 - def getIdentityBaseDN(self):
92 if self.identityBaseDN is not None: 93 return self.identityBaseDN 94 95 cfg = loadConfig() 96 try: 97 return cfg.get('authentication', 'identity-base') 98 except (ConfigParser.NoOptionError, 99 ConfigParser.NoSectionError): 100 return self.getBaseDN()
101
102 - def getIdentitySearch(self, name):
103 data = { 104 'name': name, 105 } 106 107 if self.identitySearch is not None: 108 f = self.identitySearch % data 109 else: 110 cfg = loadConfig() 111 try: 112 f=cfg.get('authentication', 'identity-search', vars=data) 113 except (ConfigParser.NoOptionError, 114 ConfigParser.NoSectionError): 115 f='(|(cn=%(name)s)(uid=%(name)s))' % data 116 return f
117 118 119 DEFAULTS = { 120 'samba': { 'use-lmhash': 'no', 121 }, 122 } 123 124 CONFIG_FILES = [ 125 '/etc/ldaptor/global.cfg', 126 os.path.expanduser('~/.ldaptor/global.cfg'), 127 ] 128 129 __config = None 130
131 -def loadConfig(configFiles=None, 132 reload=False):
133 """ 134 Load configuration file. 135 """ 136 global __config 137 if __config is None or reload: 138 x = ConfigParser.SafeConfigParser() 139 x.optionxform = InsensitiveString 140 141 for section, options in DEFAULTS.items(): 142 x.add_section(section) 143 for option, value in options.items(): 144 x.set(section, option, value) 145 146 if configFiles is None: 147 configFiles = CONFIG_FILES 148 x.read(configFiles) 149 __config = x 150 return __config
151
152 -def useLMhash():
153 """ 154 Read configuration file if necessary and return whether 155 to use LanMan hashes or not. 156 """ 157 cfg = loadConfig() 158 return cfg.getboolean('samba', 'use-lmhash')
159