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):
32 def __init__(self, *pargs):
33 super(InfoProvider, self).__init__(*pargs)
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):
154 def __init__(self, *pargs):
155 super(Installer, self).__init__()
159 def install_args(self, group):
160 group.add_argument('--info-ldap', choices=['yes', 'no'], default='no',
161 help='Use LDAP to populate user attrs')
162 group.add_argument('--info-ldap-server-url', action='store',
163 help='LDAP Server Url')
164 group.add_argument('--info-ldap-bind-dn', action='store',
166 group.add_argument('--info-ldap-bind-pwd', action='store',
167 help='LDAP Bind Password')
168 group.add_argument('--info-ldap-user-dn-template', action='store',
169 help='LDAP User DN Template')
171 def configure(self, opts):
172 if opts['info_ldap'] != 'yes':
175 # Add configuration data to database
176 po = PluginObject(*self.pargs)
179 po.wipe_config_values()
181 if 'info_ldap_server_url' in opts:
182 config['server url'] = opts['info_ldap_server_url']
183 elif 'ldap_server_url' in opts:
184 config['server url'] = opts['ldap_server_url']
185 config = {'bind dn': opts['info_ldap_bind_dn']}
186 config = {'bind password': opts['info_ldap_bind_pwd']}
187 config = {'user dn template': opts['info_ldap_user_dn_template']}
188 if 'info_ldap_bind_dn' in opts:
189 config['bind dn'] = opts['info_ldap_bind_dn']
190 if 'info_ldap_bind_pwd' in opts:
191 config['bind password'] = opts['info_ldap_bind_pwd']
192 if 'info_ldap_user_dn_template' in opts:
193 config['user dn template'] = opts['info_ldap_user_dn_template']
194 elif 'ldap_bind_dn_template' in opts:
195 config['user dn template'] = opts['ldap_bind_dn_template']
196 config['tls'] = 'Demand'
197 po.save_plugin_config(config)
199 # Update global config to add login plugin
201 po.save_enabled_state()