1 # Copyright (C) 2014 Ipsilon Project Contributors
3 # See the file named COPYING for the project license
5 # Info plugin for mod_lookup_identity Apache module via SSSD
6 # http://www.adelton.com/apache/mod_lookup_identity/
8 from ipsilon.info.common import InfoProviderBase
9 from ipsilon.info.common import InfoProviderInstaller
10 from ipsilon.util.plugin import PluginObject
11 from ipsilon.util.policy import Policy
12 from ipsilon.util import config as pconfig
13 from string import Template
20 SSSD_CONF = '/etc/sssd/sssd.conf'
22 # LDAP attributes to tell SSSD to fetch over the InfoPipe
31 # Map the mod_lookup_identity env variables to Ipsilon. The inverse of
32 # this is in the httpd template.
34 ['REMOTE_USER_GECOS', 'fullname'],
35 ['REMOTE_USER_EMAIL', 'email'],
36 ['REMOTE_USER_FIRSTNAME', 'givenname'],
37 ['REMOTE_USER_LASTNAME', 'surname'],
38 ['REMOTE_USER_STREET', 'street'],
39 ['REMOTE_USER_STATE', 'state'],
40 ['REMOTE_USER_POSTALCODE', 'postcode'],
41 ['REMOTE_USER_TELEPHONENUMBER', 'phone'],
45 class InfoProvider(InfoProviderBase):
47 def __init__(self, *pargs):
48 super(InfoProvider, self).__init__(*pargs)
49 self.mapper = Policy(sssd_mapping)
55 'SSSD can only be used when pre-configured',
59 def _get_user_data(self, user):
62 expectgroups = int(cherrypy.request.wsgi_environ.get(
63 'REMOTE_USER_GROUP_N', 0))
64 for key in cherrypy.request.wsgi_environ:
65 if key.startswith('REMOTE_USER_'):
66 if key == 'REMOTE_USER_GROUP_N':
68 if key.startswith('REMOTE_USER_GROUP_'):
69 groups.append(cherrypy.request.wsgi_environ[key])
71 reply[key] = cherrypy.request.wsgi_environ[key]
72 if len(groups) != expectgroups:
73 self.error('Number of groups expected was not found. Expected'
74 ' %d got %d' % (expectgroups, len(groups)))
77 def get_user_attrs(self, user):
80 attrs, groups = self._get_user_data(user)
81 userattrs, extras = self.mapper.map_attributes(attrs)
83 reply['_groups'] = groups
84 reply['_extras'] = {'sssd': extras}
91 def save_plugin_config(self, *args, **kwargs):
92 raise ValueError('Configuration cannot be modified live for SSSD')
94 def get_config_obj(self):
98 self.refresh_plugin_config()
99 if not self.get_config_value('preconfigured'):
100 raise Exception("SSSD Can be enabled only if pre-configured")
101 super(InfoProvider, self).enable()
105 LoadModule lookup_identity_module modules/mod_lookup_identity.so
107 <Location /${instance}>
108 LookupUserAttr sn REMOTE_USER_LASTNAME
109 LookupUserAttr locality REMOTE_USER_STATE
110 LookupUserAttr street REMOTE_USER_STREET
111 LookupUserAttr telephoneNumber REMOTE_USER_TELEPHONENUMBER
112 LookupUserAttr givenname REMOTE_USER_FIRSTNAME
113 LookupUserAttr mail REMOTE_USER_EMAIL
114 LookupUserAttr postalCode REMOTE_USER_POSTALCODE
115 LookupUserGroupsIter REMOTE_USER_GROUP
120 class Installer(InfoProviderInstaller):
122 def __init__(self, *pargs):
123 super(Installer, self).__init__()
127 def install_args(self, group):
128 group.add_argument('--info-sssd', choices=['yes', 'no'],
130 help='Use mod_lookup_identity and SSSD to populate'
132 group.add_argument('--info-sssd-domain', action='append',
133 help='SSSD domain to enable mod_lookup_identity'
136 def configure(self, opts):
137 if opts['info_sssd'] != 'yes':
142 confopts = {'instance': opts['instance']}
144 tmpl = Template(CONF_TEMPLATE)
145 hunk = tmpl.substitute(**confopts)
146 with open(opts['httpd_conf'], 'a') as httpd_conf:
147 httpd_conf.write(hunk)
150 sssdconfig = SSSDConfig.SSSDConfig()
151 sssdconfig.import_config()
152 except Exception as e: # pylint: disable=broad-except
153 # Unable to read existing SSSD config so it is probably not
155 logging.info('Loading SSSD config failed: %s', e)
158 if not opts['info_sssd_domain']:
159 domains = sssdconfig.list_domains()
161 domains = opts['info_sssd_domain']
163 for domain in domains:
165 sssd_domain = sssdconfig.get_domain(domain)
166 except SSSDConfig.NoDomainError:
167 logging.info('No SSSD domain %s', domain)
170 sssd_domain.set_option(
171 'ldap_user_extra_attrs', ', '.join(SSSD_ATTRS)
173 sssdconfig.save_domain(sssd_domain)
175 logging.info("Configured SSSD domain %s", domain)
178 logging.info('No SSSD domains configured')
182 sssdconfig.new_service('ifp')
183 except SSSDConfig.ServiceAlreadyExists:
186 sssdconfig.activate_service('ifp')
188 ifp = sssdconfig.get_service('ifp')
189 ifp.set_option('allowed_uids', 'apache, root')
190 ifp.set_option('user_attributes', '+' + ', +'.join(SSSD_ATTRS))
192 sssdconfig.save_service(ifp)
193 sssdconfig.write(SSSD_CONF)
195 # for selinux enabled platforms, ignore if it fails just report
197 subprocess.call(['/usr/sbin/setsebool', '-P',
198 'httpd_dbus_sssd=on'])
199 except Exception: # pylint: disable=broad-except
203 subprocess.call(['/sbin/service', 'sssd', 'restart'])
204 except Exception: # pylint: disable=broad-except
207 # Give SSSD a chance to restart
210 # Add configuration data to database
211 po = PluginObject(*self.pargs)
214 po.wipe_config_values()
215 config = {'preconfigured': True}
216 po.save_plugin_config(config)
218 # Update global config to add info plugin
220 po.save_enabled_state()