1 # Copyright (C) 2014 Ipsilon Project Contributors
3 # See the file named COPYING for the project license
5 from ipsilon.info.common import InfoProviderBase
6 from ipsilon.info.common import InfoProviderInstaller
7 from ipsilon.info.common import InfoMapping
8 from ipsilon.util.plugin import PluginObject
9 from ipsilon.util import config as pconfig
13 # TODO: fetch mapping from configuration
16 'commonname': 'fullname',
19 'destinationindicator': 'country',
20 'postalcode': 'postcode',
22 'statetorprovincename': 'state',
23 'streetaddress': 'street',
24 'telephonenumber': 'phone',
28 class InfoProvider(InfoProviderBase):
30 def __init__(self, *pargs):
31 super(InfoProvider, self).__init__(*pargs)
32 self.mapper = InfoMapping()
33 self.mapper.set_mapping(ldap_mapping)
35 self.description = """
36 Info plugin that uses LDAP to retrieve user data. """
41 'The LDAP server url.',
42 'ldap://example.com'),
45 'Template to turn username into DN.',
46 'uid=%(username)s,ou=People,dc=example,dc=com'),
49 'What TLS level show be required',
50 ['Demand', 'Allow', 'Try', 'Never', 'NoTLS'],
54 'DN to bind as, if empty uses anonymous bind.',
55 'uid=ipsilon,ou=People,dc=example,dc=com'),
58 'Password to use for bind operation'),
63 return self.get_config_value('server url')
67 return self.get_config_value('tls')
71 return self.get_config_value('bind dn')
74 def bind_password(self):
75 return self.get_config_value('bind password')
78 def user_dn_tmpl(self):
79 return self.get_config_value('user dn template')
83 tls = self.tls.lower()
86 tls_req_opt = ldap.OPT_X_TLS_NEVER
88 tls_req_opt = ldap.OPT_X_TLS_DEMAND
90 tls_req_opt = ldap.OPT_X_TLS_ALLOW
92 tls_req_opt = ldap.OPT_X_TLS_TRY
93 if tls_req_opt is not None:
94 ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, tls_req_opt)
96 conn = ldap.initialize(self.server_url)
99 if not self.server_url.startswith("ldaps"):
102 conn.simple_bind_s(self.bind_dn, self.bind_password)
106 def _get_user_data(self, conn, dn):
107 result = conn.search_s(dn, ldap.SCOPE_BASE)
108 if result is None or result == []:
109 raise Exception('User object could not be found!')
110 elif len(result) > 1:
111 raise Exception('No unique user object could be found!')
113 for name, value in result[0][1].iteritems():
114 if type(value) is list and len(value) == 1:
119 def _get_user_groups(self, conn, dn, ldapattrs):
120 # TODO: fixme to support RFC2307bis schemas
121 if 'memberuid' in ldapattrs:
122 return ldapattrs['memberuid']
126 def get_user_data_from_conn(self, conn, dn):
129 ldapattrs = self._get_user_data(conn, dn)
130 userattrs, extras = self.mapper.map_attrs(ldapattrs)
131 groups = self._get_user_groups(conn, dn, ldapattrs)
132 reply['userdata'] = userattrs
133 reply['groups'] = groups
134 reply['extras'] = {'ldap': extras}
135 except Exception, e: # pylint: disable=broad-except
140 def get_user_attrs(self, user):
142 conn = self._ldap_bind()
143 dn = self.user_dn_tmpl % {'username': user}
144 return self.get_user_data_from_conn(conn, dn)
145 except Exception, e: # pylint: disable=broad-except
150 class Installer(InfoProviderInstaller):
152 def __init__(self, *pargs):
153 super(Installer, self).__init__()
157 def install_args(self, group):
158 group.add_argument('--info-ldap', choices=['yes', 'no'], default='no',
159 help='Use LDAP to populate user attrs')
160 group.add_argument('--info-ldap-server-url', action='store',
161 help='LDAP Server Url')
162 group.add_argument('--info-ldap-bind-dn', action='store',
164 group.add_argument('--info-ldap-bind-pwd', action='store',
165 help='LDAP Bind Password')
166 group.add_argument('--info-ldap-user-dn-template', action='store',
167 help='LDAP User DN Template')
169 def configure(self, opts):
170 if opts['info_ldap'] != 'yes':
173 # Add configuration data to database
174 po = PluginObject(*self.pargs)
177 po.wipe_config_values()
179 if 'info_ldap_server_url' in opts:
180 config['server url'] = opts['info_ldap_server_url']
181 elif 'ldap_server_url' in opts:
182 config['server url'] = opts['ldap_server_url']
183 config = {'bind dn': opts['info_ldap_bind_dn']}
184 config = {'bind password': opts['info_ldap_bind_pwd']}
185 config = {'user dn template': opts['info_ldap_user_dn_template']}
186 if 'info_ldap_bind_dn' in opts:
187 config['bind dn'] = opts['info_ldap_bind_dn']
188 if 'info_ldap_bind_pwd' in opts:
189 config['bind password'] = opts['info_ldap_bind_pwd']
190 if 'info_ldap_user_dn_template' in opts:
191 config['user dn template'] = opts['info_ldap_user_dn_template']
192 elif 'ldap_bind_dn_template' in opts:
193 config['user dn template'] = opts['ldap_bind_dn_template']
194 config['tls'] = 'Demand'
195 po.save_plugin_config(config)
197 # Update global config to add info plugin
199 po.save_enabled_state()