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 ipsilon.util.user import UserSession
24 from string import Template
29 class Form(LoginPageBase):
31 def GET(self, *args, **kwargs):
32 context = self.create_tmpl_context()
33 # pylint: disable=star-args
34 return self._template('login/form.html', **context)
36 def POST(self, *args, **kwargs):
40 if not user.is_anonymous:
41 return self.lm.auth_successful(user.name)
44 error = cherrypy.request.headers['EXTERNAL_AUTH_ERROR']
46 error = "Unknown error using external authentication"
47 cherrypy.log.error("Error: %s" % error)
48 return self.lm.auth_failed()
50 def root(self, *args, **kwargs):
51 op = getattr(self, cherrypy.request.method, self.GET)
53 return op(*args, **kwargs)
55 def create_tmpl_context(self, **kwargs):
57 if self.lm.next_login is not None:
58 next_url = self.lm.next_login.path
62 "action": '%s/login/form' % self.basepath,
63 "service_name": self.lm.service_name,
64 "username_text": self.lm.username_text,
65 "password_text": self.lm.password_text,
66 "description": self.lm.help_text,
69 context.update(kwargs)
73 class LoginManager(LoginManagerBase):
75 def __init__(self, *args, **kwargs):
76 super(LoginManager, self).__init__(*args, **kwargs)
80 self.description = """
81 Form based login Manager. Relies on mod_intercept_form_submit plugin for
82 actual authentication. """
85 """ The name of the PAM service used to authenticate. """,
90 """ The text shown to guide the user at login time. """,
92 'Insert your Username and Password and then submit.'
95 """ The text shown to ask for the username in the form. """,
100 """ The text shown to ask for the password in the form. """,
107 def service_name(self):
108 return self.get_config_value('service name')
112 return self.get_config_value('help text')
115 def username_text(self):
116 return self.get_config_value('username text')
119 def password_text(self):
120 return self.get_config_value('password text')
122 def get_tree(self, site):
123 self.page = Form(site, self)
128 LoadModule intercept_form_submit_module modules/mod_intercept_form_submit.so
129 LoadModule authnz_pam_module modules/mod_authnz_pam.so
131 <Location /${instance}/login/form>
132 InterceptFormPAMService ${service}
133 InterceptFormLogin login_name
134 InterceptFormPassword login_password
135 # InterceptFormLoginSkip admin
136 # InterceptFormClearRemoteUserForSkipped on
137 InterceptFormPasswordRedact on
142 class Installer(object):
148 def install_args(self, group):
149 group.add_argument('--form', choices=['yes', 'no'], default='no',
150 help='Configure External Form authentication')
151 group.add_argument('--form-service', action='store', default='remote',
152 help='PAM service name to use for authentication')
154 def configure(self, opts):
155 if opts['form'] != 'yes':
158 confopts = {'instance': opts['instance'],
159 'service': opts['form_service']}
161 tmpl = Template(CONF_TEMPLATE)
162 hunk = tmpl.substitute(**confopts) # pylint: disable=star-args
163 with open(opts['httpd_conf'], 'a') as httpd_conf:
164 httpd_conf.write(hunk)
166 # Add configuration data to database
170 po.wipe_config_values(FACILITY)
172 # Update global config, put 'krb' always first
174 globalconf = po.get_plugin_config(FACILITY)
175 if 'order' in globalconf:
176 order = globalconf['order'].split(',')
180 globalconf['order'] = ','.join(order)
181 po.set_config(globalconf)
182 po.save_plugin_config(FACILITY)
184 # for selinux enabled platforms, ignore if it fails just report
186 subprocess.call(['/usr/sbin/setsebool', '-P',
187 'httpd_mod_auth_pam=on'])
188 except Exception: # pylint: disable=broad-except