3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from ipsilon.login.common import LoginPageBase, LoginManagerBase
21 from ipsilon.login.common import FACILITY
22 from ipsilon.util.plugin import PluginObject
23 from string import Template
28 class Krb(LoginPageBase):
30 def root(self, *args, **kwargs):
31 # Someone typed manually or a robot is walking th tree.
32 # Redirect to default page
33 return self.lm.redirect_to_path(self.lm.path)
36 class KrbAuth(LoginPageBase):
38 def root(self, *args, **kwargs):
39 # If we can get here, we must be authenticated and remote_user
40 # was set. Check the session has a user set already or error.
41 if self.user and self.user.name:
42 userdata = {'krb_principal_name': self.user.name}
43 return self.lm.auth_successful(self.user.name, userdata)
45 return self.lm.auth_failed()
48 class KrbError(LoginPageBase):
50 def root(self, *args, **kwargs):
51 cherrypy.log.error('REQUEST: %s' % cherrypy.request.headers)
52 # If we have no negotiate header return whatever mod_auth_kerb
53 # generated and wait for the next request
55 if not 'WWW-Authenticate' in cherrypy.request.headers:
56 cherrypy.response.status = 401
58 if self.lm.next_login:
59 return self.lm.next_login.page.root(*args, **kwargs)
61 conturl = '%s/login' % self.basepath
62 return self._template('login/krb.html',
63 title='Kerberos Login',
66 # If we get here, negotiate failed
67 return self.lm.auth_failed()
70 class LoginManager(LoginManagerBase):
72 def __init__(self, *args, **kwargs):
73 super(LoginManager, self).__init__(*args, **kwargs)
75 self.path = 'krb/negotiate'
77 self.description = """
78 Kereros Negotiate authentication plugin. Relies on the mod_auth_kerb apache
79 plugin for actual authentication. """
81 def get_tree(self, site):
82 self.page = Krb(site, self)
83 self.page.__dict__['negotiate'] = KrbAuth(site, self)
84 self.page.__dict__['unauthorized'] = KrbError(site, self)
85 self.page.__dict__['failed'] = KrbError(site, self)
91 <Location /${instance}/login/krb/negotiate>
93 AuthName "Kerberos Login"
99 KrbSaveCredentials off
100 KrbConstrainedDelegation off
101 # KrbLocalUserMapping On
104 ErrorDocument 401 /${instance}/login/krb/unauthorized
105 ErrorDocument 500 /${instance}/login/krb/failed
110 class Installer(object):
116 def install_args(self, group):
117 group.add_argument('--krb', choices=['yes', 'no'], default='no',
118 help='Configure Kerberos authentication')
119 group.add_argument('--krb-realms',
120 help='Allowed Kerberos Auth Realms')
121 group.add_argument('--krb-httpd-keytab',
122 default='/etc/httpd/conf/http.keytab',
123 help='Kerberos keytab location for HTTPD')
125 def configure(self, opts):
126 if opts['krb'] != 'yes':
129 confopts = {'instance': opts['instance']}
131 if os.path.exists(opts['krb_httpd_keytab']):
132 confopts['keytab'] = ' Krb5KeyTab %s' % opts['krb_httpd_keytab']
134 raise Exception('Keytab not found')
136 if opts['krb_realms'] is None:
137 confopts['realms'] = ' # KrbAuthRealms - Any realm is allowed'
139 confopts['realms'] = ' KrbAuthRealms %s' % opts['krb_realms']
141 tmpl = Template(CONF_TEMPLATE)
142 hunk = tmpl.substitute(**confopts) # pylint: disable=star-args
143 with open(opts['httpd_conf'], 'a') as httpd_conf:
144 httpd_conf.write(hunk)
146 # Add configuration data to database
151 # Update global config, put 'krb' always first
153 globalconf = po.get_plugin_config(FACILITY)
154 if 'order' in globalconf:
155 order = globalconf['order'].split(',')
158 order.insert(0, 'krb')
159 globalconf['order'] = ','.join(order)
160 po.set_config(globalconf)
161 po.save_plugin_config(FACILITY)