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, \
20 from ipsilon.util.plugin import PluginObject
21 from ipsilon.util import config as pconfig
26 class Pam(LoginFormBase):
28 def _authenticate(self, username, password):
29 if self.lm.service_name:
30 ok = pam.authenticate(username, password, self.lm.service_name)
32 ok = pam.authenticate(username, password)
35 self.log("User %s successfully authenticated." % username)
38 self.log("User %s failed authentication." % username)
41 def POST(self, *args, **kwargs):
42 username = kwargs.get("login_name")
43 password = kwargs.get("login_password")
47 if username and password:
48 user = self._authenticate(username, password)
50 return self.lm.auth_successful(self.trans, user, 'password')
52 error = "Authentication failed"
55 error = "Username or password is missing"
56 self.error("Error: " + error)
58 context = self.create_tmpl_context(
61 error_password=not password,
62 error_username=not username
64 # pylint: disable=star-args
65 return self._template('login/form.html', **context)
68 class LoginManager(LoginManagerBase):
70 def __init__(self, *args, **kwargs):
71 super(LoginManager, self).__init__(*args, **kwargs)
75 self.description = """
76 Form based login Manager that uses the system's PAM infrastructure
77 for authentication. """
82 'The name of the PAM service used to authenticate.',
86 'Text used to ask for the username at login time.',
90 'Text used to ask for the password at login time.',
94 'Text used to guide the user at login time.',
95 'Provide your Username and Password')
99 def service_name(self):
100 return self.get_config_value('service name')
104 return self.get_config_value('help text')
107 def username_text(self):
108 return self.get_config_value('username text')
111 def password_text(self):
112 return self.get_config_value('password text')
114 def get_tree(self, site):
115 self.page = Pam(site, self, 'login/pam')
119 class Installer(LoginManagerInstaller):
121 def __init__(self, *pargs):
122 super(Installer, self).__init__()
126 def install_args(self, group):
127 group.add_argument('--pam', choices=['yes', 'no'], default='no',
128 help='Configure PAM authentication')
129 group.add_argument('--pam-service', action='store', default='remote',
130 help='PAM service name to use for authentication')
132 def configure(self, opts):
133 if opts['pam'] != 'yes':
136 # Add configuration data to database
137 po = PluginObject(*self.pargs)
140 po.wipe_config_values()
141 config = {'service name': opts['pam_service']}
142 po.save_plugin_config(config)
144 # Update global config to add login plugin
146 po.save_enabled_state()
148 # for selinux enabled platforms, ignore if it fails just report
150 subprocess.call(['/usr/sbin/setsebool', '-P',
151 'httpd_mod_auth_pam=on',
152 'httpd_tmp_exec=on'])
153 except Exception: # pylint: disable=broad-except