1 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
3 # see file 'COPYING' for use and warranty information
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.
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.
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/>.
18 from ipsilon.login.common import LoginFormBase, LoginManagerBase
19 from ipsilon.util.plugin import PluginObject
20 from ipsilon.util import config as pconfig
25 class Pam(LoginFormBase):
27 def _authenticate(self, username, password):
28 if self.lm.service_name:
29 ok = pam.authenticate(username, password, self.lm.service_name)
31 ok = pam.authenticate(username, password)
34 self.log("User %s successfully authenticated." % username)
37 self.log("User %s failed authentication." % username)
40 def POST(self, *args, **kwargs):
41 username = kwargs.get("login_name")
42 password = kwargs.get("login_password")
46 if username and password:
47 user = self._authenticate(username, password)
49 return self.lm.auth_successful(self.trans, user, 'password')
51 error = "Authentication failed"
54 error = "Username or password is missing"
55 self.error("Error: " + error)
57 context = self.create_tmpl_context(
60 error_password=not password,
61 error_username=not username
63 # pylint: disable=star-args
64 return self._template('login/form.html', **context)
67 class LoginManager(LoginManagerBase):
69 def __init__(self, *args, **kwargs):
70 super(LoginManager, self).__init__(*args, **kwargs)
74 self.description = """
75 Form based login Manager that uses the system's PAM infrastructure
76 for authentication. """
81 'The name of the PAM service used to authenticate.',
85 'Text used to ask for the username at login time.',
89 'Text used to ask for the password at login time.',
93 'Text used to guide the user at login time.',
94 'Provide your Username and Password')
98 def service_name(self):
99 return self.get_config_value('service name')
103 return self.get_config_value('help text')
106 def username_text(self):
107 return self.get_config_value('username text')
110 def password_text(self):
111 return self.get_config_value('password text')
113 def get_tree(self, site):
114 self.page = Pam(site, self, 'login/pam')
118 class Installer(object):
120 def __init__(self, *pargs):
125 def install_args(self, group):
126 group.add_argument('--pam', choices=['yes', 'no'], default='no',
127 help='Configure PAM authentication')
128 group.add_argument('--pam-service', action='store', default='remote',
129 help='PAM service name to use for authentication')
131 def configure(self, opts):
132 if opts['pam'] != 'yes':
135 # Add configuration data to database
136 po = PluginObject(*self.pargs)
139 po.wipe_config_values()
140 config = {'service name': opts['pam_service']}
141 po.save_plugin_config(config)
143 # Update global config to add login plugin
145 po.save_enabled_state()
147 # for selinux enabled platforms, ignore if it fails just report
149 subprocess.call(['/usr/sbin/setsebool', '-P',
150 'httpd_mod_auth_pam=on',
151 'httpd_tmp_exec=on'])
152 except Exception: # pylint: disable=broad-except