Fix file permissions and remove shebang's
[cascardo/ipsilon.git] / ipsilon / login / authform.py
1 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
2 #
3 # see file 'COPYING' for use and warranty information
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 from ipsilon.login.common import LoginFormBase, LoginManagerBase
19 from ipsilon.util.plugin import PluginObject
20 from ipsilon.util.user import UserSession
21 from ipsilon.util import config as pconfig
22 from string import Template
23 import cherrypy
24 import subprocess
25
26
27 class Form(LoginFormBase):
28
29     def POST(self, *args, **kwargs):
30         us = UserSession()
31         us.remote_login()
32         user = us.get_user()
33         if not user.is_anonymous:
34             return self.lm.auth_successful(self.trans, user.name, 'password')
35         else:
36             try:
37                 error = cherrypy.request.headers['EXTERNAL_AUTH_ERROR']
38             except KeyError:
39                 error = "Unknown error using external authentication"
40                 cherrypy.log.error("Error: %s" % error)
41             return self.lm.auth_failed(self.trans)
42
43
44 class LoginManager(LoginManagerBase):
45
46     def __init__(self, *args, **kwargs):
47         super(LoginManager, self).__init__(*args, **kwargs)
48         self.name = 'form'
49         self.path = 'form'
50         self.page = None
51         self.service_name = 'form'
52         self.description = """
53 Form based login Manager. Relies on mod_intercept_form_submit plugin for
54  actual authentication. """
55         self.new_config(
56             self.name,
57             pconfig.String(
58                 'username text',
59                 'Text used to ask for the username at login time.',
60                 'Username'),
61             pconfig.String(
62                 'password text',
63                 'Text used to ask for the password at login time.',
64                 'Password'),
65             pconfig.String(
66                 'help text',
67                 'Text used to guide the user at login time.',
68                 'Insert your Username and Password and then submit.')
69         )
70
71     @property
72     def help_text(self):
73         return self.get_config_value('help text')
74
75     @property
76     def username_text(self):
77         return self.get_config_value('username text')
78
79     @property
80     def password_text(self):
81         return self.get_config_value('password text')
82
83     def get_tree(self, site):
84         self.page = Form(site, self, 'login/form')
85         return self.page
86
87
88 CONF_TEMPLATE = """
89 LoadModule intercept_form_submit_module modules/mod_intercept_form_submit.so
90 LoadModule authnz_pam_module modules/mod_authnz_pam.so
91
92 <Location /${instance}/login/form>
93   InterceptFormPAMService ${service}
94   InterceptFormLogin login_name
95   InterceptFormPassword login_password
96   # InterceptFormLoginSkip admin
97   # InterceptFormClearRemoteUserForSkipped on
98   InterceptFormPasswordRedact on
99 </Location>
100 """
101
102
103 class Installer(object):
104
105     def __init__(self, *pargs):
106         self.name = 'form'
107         self.ptype = 'login'
108         self.pargs = pargs
109
110     def install_args(self, group):
111         group.add_argument('--form', choices=['yes', 'no'], default='no',
112                            help='Configure External Form authentication')
113         group.add_argument('--form-service', action='store', default='remote',
114                            help='PAM service name to use for authentication')
115
116     def configure(self, opts):
117         if opts['form'] != 'yes':
118             return
119
120         confopts = {'instance': opts['instance'],
121                     'service': opts['form_service']}
122
123         tmpl = Template(CONF_TEMPLATE)
124         hunk = tmpl.substitute(**confopts)  # pylint: disable=star-args
125         with open(opts['httpd_conf'], 'a') as httpd_conf:
126             httpd_conf.write(hunk)
127
128         # Add configuration data to database
129         po = PluginObject(*self.pargs)
130         po.name = 'form'
131         po.wipe_data()
132         po.wipe_config_values()
133
134         # Update global config to add login plugin
135         po.is_enabled = True
136         po.save_enabled_state()
137
138         # for selinux enabled platforms, ignore if it fails just report
139         try:
140             subprocess.call(['/usr/sbin/setsebool', '-P',
141                              'httpd_mod_auth_pam=on'])
142         except Exception:  # pylint: disable=broad-except
143             pass