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 string import Template
18 SSSD_CONF = '/etc/sssd/sssd.conf'
20 # LDAP attributes to tell SSSD to fetch over the InfoPipe
29 # Map the mod_lookup_identity env variables to Ipsilon. The inverse of
30 # this is in the httpd template.
32 ['REMOTE_USER_GECOS', 'fullname'],
33 ['REMOTE_USER_EMAIL', 'email'],
34 ['REMOTE_USER_FIRSTNAME', 'givenname'],
35 ['REMOTE_USER_LASTNAME', 'surname'],
36 ['REMOTE_USER_STREET', 'street'],
37 ['REMOTE_USER_STATE', 'state'],
38 ['REMOTE_USER_POSTALCODE', 'postcode'],
39 ['REMOTE_USER_TELEPHONENUMBER', 'phone'],
43 class InfoProvider(InfoProviderBase):
45 def __init__(self, *pargs):
46 super(InfoProvider, self).__init__(*pargs)
47 self.mapper = Policy(sssd_mapping)
49 self.new_config(self.name)
51 def _get_user_data(self, user):
54 expectgroups = int(cherrypy.request.wsgi_environ.get(
55 'REMOTE_USER_GROUP_N', 0))
56 for key in cherrypy.request.wsgi_environ:
57 if key.startswith('REMOTE_USER_'):
58 if key == 'REMOTE_USER_GROUP_N':
60 if key.startswith('REMOTE_USER_GROUP_'):
61 groups.append(cherrypy.request.wsgi_environ[key])
63 reply[key] = cherrypy.request.wsgi_environ[key]
64 if len(groups) != expectgroups:
65 self.error('Number of groups expected was not found. Expected'
66 ' %d got %d' % (expectgroups, len(groups)))
69 def get_user_attrs(self, user):
72 attrs, groups = self._get_user_data(user)
73 userattrs, extras = self.mapper.map_attributes(attrs)
75 reply['_groups'] = groups
76 reply['_extras'] = {'sssd': extras}
85 LoadModule lookup_identity_module modules/mod_lookup_identity.so
87 <Location /${instance}>
88 LookupUserAttr sn REMOTE_USER_LASTNAME
89 LookupUserAttr locality REMOTE_USER_STATE
90 LookupUserAttr street REMOTE_USER_STREET
91 LookupUserAttr telephoneNumber REMOTE_USER_TELEPHONENUMBER
92 LookupUserAttr givenname REMOTE_USER_FIRSTNAME
93 LookupUserAttr mail REMOTE_USER_EMAIL
94 LookupUserAttr postalCode REMOTE_USER_POSTALCODE
95 LookupUserGroupsIter REMOTE_USER_GROUP
100 class Installer(InfoProviderInstaller):
102 def __init__(self, *pargs):
103 super(Installer, self).__init__()
107 def install_args(self, group):
108 group.add_argument('--info-sssd', choices=['yes', 'no'],
110 help='Use mod_lookup_identity and SSSD to populate'
112 group.add_argument('--info-sssd-domain', action='append',
113 help='SSSD domain to enable mod_lookup_identity'
116 def configure(self, opts):
117 if opts['info_sssd'] != 'yes':
122 confopts = {'instance': opts['instance']}
124 tmpl = Template(CONF_TEMPLATE)
125 hunk = tmpl.substitute(**confopts) # pylint: disable=star-args
126 with open(opts['httpd_conf'], 'a') as httpd_conf:
127 httpd_conf.write(hunk)
130 sssdconfig = SSSDConfig.SSSDConfig()
131 sssdconfig.import_config()
132 except Exception as e: # pylint: disable=broad-except
133 # Unable to read existing SSSD config so it is probably not
135 print 'Loading SSSD config failed: %s' % e
138 if not opts['info_sssd_domain']:
139 domains = sssdconfig.list_domains()
141 domains = opts['info_sssd_domain']
143 for domain in domains:
145 sssd_domain = sssdconfig.get_domain(domain)
146 except SSSDConfig.NoDomainError:
147 print 'No SSSD domain %s' % domain
150 sssd_domain.set_option(
151 'ldap_user_extra_attrs', ', '.join(SSSD_ATTRS)
153 sssdconfig.save_domain(sssd_domain)
155 print "Configured SSSD domain %s" % domain
158 print 'No SSSD domains configured'
162 sssdconfig.new_service('ifp')
163 except SSSDConfig.ServiceAlreadyExists:
166 sssdconfig.activate_service('ifp')
168 ifp = sssdconfig.get_service('ifp')
169 ifp.set_option('allowed_uids', 'apache, root')
170 ifp.set_option('user_attributes', '+' + ', +'.join(SSSD_ATTRS))
172 sssdconfig.save_service(ifp)
173 sssdconfig.write(SSSD_CONF)
175 # for selinux enabled platforms, ignore if it fails just report
177 subprocess.call(['/usr/sbin/setsebool', '-P',
178 'httpd_dbus_sssd=on'])
179 except Exception: # pylint: disable=broad-except
183 subprocess.call(['/sbin/service', 'sssd', 'restart'])
184 except Exception: # pylint: disable=broad-except
187 # Give SSSD a chance to restart
190 # Add configuration data to database
191 po = PluginObject(*self.pargs)
194 po.wipe_config_values()
196 # Update global config to add info plugin
198 po.save_enabled_state()