3 # Copyright (C) 2014 Ipsilon Project Contributors
5 # See the file named COPYING for the project license
7 from ipsilon.info.common import InfoProviderBase
8 from ipsilon.info.common import InfoProviderInstaller
9 from ipsilon.info.common import InfoMapping
10 from ipsilon.util.plugin import PluginObject
11 from ipsilon.util import config as pconfig
15 # TODO: fetch mapping from configuration
18 'commonname': 'fullname',
21 'destinationindicator': 'country',
22 'postalcode': 'postcode',
24 'statetorprovincename': 'state',
25 'streetaddress': 'street',
26 'telephonenumber': 'phone',
30 class InfoProvider(InfoProviderBase):
33 super(InfoProvider, self).__init__()
34 self.mapper = InfoMapping()
35 self.mapper.set_mapping(ldap_mapping)
37 self.description = """
38 Info plugin that uses LDAP to retrieve user data. """
43 'The LDAP server url.',
44 'ldap://example.com'),
47 'Template to turn username into DN.',
48 'uid=%(username)s,ou=People,dc=example,dc=com'),
51 'What TLS level show be required',
52 ['Demand', 'Allow', 'Try', 'Never', 'NoTLS'],
56 'DN to bind as, if empty uses anonymous bind.',
57 'uid=ipsilon,ou=People,dc=example,dc=com'),
60 'Password to use for bind operation'),
65 return self.get_config_value('server url')
69 return self.get_config_value('tls')
73 return self.get_config_value('bind dn')
76 def bind_password(self):
77 return self.get_config_value('bind password')
80 def user_dn_tmpl(self):
81 return self.get_config_value('user dn template')
85 tls = self.tls.lower()
88 tls_req_opt = ldap.OPT_X_TLS_NEVER
90 tls_req_opt = ldap.OPT_X_TLS_DEMAND
92 tls_req_opt = ldap.OPT_X_TLS_ALLOW
94 tls_req_opt = ldap.OPT_X_TLS_TRY
95 if tls_req_opt is not None:
96 ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, tls_req_opt)
98 conn = ldap.initialize(self.server_url)
101 if not self.server_url.startswith("ldaps"):
104 conn.simple_bind_s(self.bind_dn, self.bind_password)
108 def _get_user_data(self, conn, dn):
109 result = conn.search_s(dn, ldap.SCOPE_BASE)
110 if result is None or result == []:
111 raise Exception('User object could not be found!')
112 elif len(result) > 1:
113 raise Exception('No unique user object could be found!')
115 for name, value in result[0][1].iteritems():
116 if type(value) is list and len(value) == 1:
121 def _get_user_groups(self, conn, dn, ldapattrs):
122 # TODO: fixme to support RFC2307bis schemas
123 if 'memberuid' in ldapattrs:
124 return ldapattrs['memberuid']
128 def get_user_data_from_conn(self, conn, dn):
131 ldapattrs = self._get_user_data(conn, dn)
132 userattrs, extras = self.mapper.map_attrs(ldapattrs)
133 groups = self._get_user_groups(conn, dn, ldapattrs)
134 reply['userdata'] = userattrs
135 reply['groups'] = groups
136 reply['extras'] = {'ldap': extras}
137 except Exception, e: # pylint: disable=broad-except
142 def get_user_attrs(self, user):
144 conn = self._ldap_bind()
145 dn = self.user_dn_tmpl % {'username': user}
146 return self.get_user_data_from_conn(conn, dn)
147 except Exception, e: # pylint: disable=broad-except
152 class Installer(InfoProviderInstaller):
155 super(Installer, self).__init__()
158 def install_args(self, group):
159 group.add_argument('--info-ldap', choices=['yes', 'no'], default='no',
160 help='Use LDAP to populate user attrs')
161 group.add_argument('--info-ldap-server-url', action='store',
162 help='LDAP Server Url')
163 group.add_argument('--info-ldap-bind-dn', action='store',
165 group.add_argument('--info-ldap-bind-pwd', action='store',
166 help='LDAP Bind Password')
167 group.add_argument('--info-ldap-user-dn-template', action='store',
168 help='LDAP User DN Template')
170 def configure(self, opts):
171 if opts['info_ldap'] != 'yes':
174 # Add configuration data to database
178 po.wipe_config_values(self.facility)
180 if 'info_ldap_server_url' in opts:
181 config['server url'] = opts['info_ldap_server_url']
182 elif 'ldap_server_url' in opts:
183 config['server url'] = opts['ldap_server_url']
184 config = {'bind dn': opts['info_ldap_bind_dn']}
185 config = {'bind password': opts['info_ldap_bind_pwd']}
186 config = {'user dn template': opts['info_ldap_user_dn_template']}
187 if 'info_ldap_bind_dn' in opts:
188 config['bind dn'] = opts['info_ldap_bind_dn']
189 if 'info_ldap_bind_pwd' in opts:
190 config['bind password'] = opts['info_ldap_bind_pwd']
191 if 'info_ldap_user_dn_template' in opts:
192 config['user dn template'] = opts['info_ldap_user_dn_template']
193 elif 'ldap_bind_dn_template' in opts:
194 config['user dn template'] = opts['ldap_bind_dn_template']
195 config['tls'] = 'Demand'
196 po.save_plugin_config(self.facility, config)
198 # Replace global config, only one plugin info can be used
200 globalconf = po.get_plugin_config(self.facility)
201 if 'order' in globalconf:
202 order = globalconf['order'].split(',')
206 globalconf['order'] = ','.join(order)
207 po.save_plugin_config(self.facility, globalconf)