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