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
9 """Configuration must specify a base DN"""
10
13
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):
39
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
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
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
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):
151
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