4e9f5c150ce83fe266dc620f313b145e67bc8367
[cascardo/ipsilon.git] / ipsilon / login / authform.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
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.
11 #
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.
16 #
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/>.
19
20 from ipsilon.login.common import LoginFormBase, LoginManagerBase
21 from ipsilon.login.common import FACILITY
22 from ipsilon.util.plugin import PluginObject
23 from ipsilon.util.user import UserSession
24 from ipsilon.util import config as pconfig
25 from string import Template
26 import cherrypy
27 import subprocess
28
29
30 class Form(LoginFormBase):
31
32     def POST(self, *args, **kwargs):
33         us = UserSession()
34         us.remote_login()
35         user = us.get_user()
36         if not user.is_anonymous:
37             return self.lm.auth_successful(self.trans, user.name, 'password')
38         else:
39             try:
40                 error = cherrypy.request.headers['EXTERNAL_AUTH_ERROR']
41             except KeyError:
42                 error = "Unknown error using external authentication"
43                 cherrypy.log.error("Error: %s" % error)
44             return self.lm.auth_failed(self.trans)
45
46
47 class LoginManager(LoginManagerBase):
48
49     def __init__(self, *args, **kwargs):
50         super(LoginManager, self).__init__(*args, **kwargs)
51         self.name = 'form'
52         self.path = 'form'
53         self.page = None
54         self.service_name = 'form'
55         self.description = """
56 Form based login Manager. Relies on mod_intercept_form_submit plugin for
57  actual authentication. """
58         self.new_config(
59             self.name,
60             pconfig.String(
61                 'username text',
62                 'Text used to ask for the username at login time.',
63                 'Username'),
64             pconfig.String(
65                 'password text',
66                 'Text used to ask for the password at login time.',
67                 'Password'),
68             pconfig.String(
69                 'help text',
70                 'Text used to guide the user at login time.',
71                 'Insert your Username and Password and then submit.')
72         )
73
74     @property
75     def help_text(self):
76         return self.get_config_value('help text')
77
78     @property
79     def username_text(self):
80         return self.get_config_value('username text')
81
82     @property
83     def password_text(self):
84         return self.get_config_value('password text')
85
86     def get_tree(self, site):
87         self.page = Form(site, self, 'login/form')
88         return self.page
89
90
91 CONF_TEMPLATE = """
92 LoadModule intercept_form_submit_module modules/mod_intercept_form_submit.so
93 LoadModule authnz_pam_module modules/mod_authnz_pam.so
94
95 <Location /${instance}/login/form>
96   InterceptFormPAMService ${service}
97   InterceptFormLogin login_name
98   InterceptFormPassword login_password
99   # InterceptFormLoginSkip admin
100   # InterceptFormClearRemoteUserForSkipped on
101   InterceptFormPasswordRedact on
102 </Location>
103 """
104
105
106 class Installer(object):
107
108     def __init__(self):
109         self.name = 'form'
110         self.ptype = 'login'
111
112     def install_args(self, group):
113         group.add_argument('--form', choices=['yes', 'no'], default='no',
114                            help='Configure External Form authentication')
115         group.add_argument('--form-service', action='store', default='remote',
116                            help='PAM service name to use for authentication')
117
118     def configure(self, opts):
119         if opts['form'] != 'yes':
120             return
121
122         confopts = {'instance': opts['instance'],
123                     'service': opts['form_service']}
124
125         tmpl = Template(CONF_TEMPLATE)
126         hunk = tmpl.substitute(**confopts)  # pylint: disable=star-args
127         with open(opts['httpd_conf'], 'a') as httpd_conf:
128             httpd_conf.write(hunk)
129
130         # Add configuration data to database
131         po = PluginObject()
132         po.name = 'form'
133         po.wipe_data()
134         po.wipe_config_values(FACILITY)
135
136         # Update global config, put 'krb' always first
137         po.name = 'global'
138         globalconf = po.get_plugin_config(FACILITY)
139         if 'order' in globalconf:
140             order = globalconf['order'].split(',')
141         else:
142             order = []
143         order.append('form')
144         globalconf['order'] = ','.join(order)
145         po.save_plugin_config(FACILITY, globalconf)
146
147         # for selinux enabled platforms, ignore if it fails just report
148         try:
149             subprocess.call(['/usr/sbin/setsebool', '-P',
150                              'httpd_mod_auth_pam=on'])
151         except Exception:  # pylint: disable=broad-except
152             pass