Fix error returned from login plugins
[cascardo/ipsilon.git] / ipsilon / login / authpam.py
1 # Copyright (C) 2013  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 import config as pconfig
22 import pam
23 import subprocess
24
25
26 class Pam(LoginFormBase):
27
28     def _authenticate(self, username, password):
29         if self.lm.service_name:
30             ok = pam.authenticate(username, password, self.lm.service_name)
31         else:
32             ok = pam.authenticate(username, password)
33
34         if ok:
35             self.log("User %s successfully authenticated." % username)
36             return username
37
38         self.log("User %s failed authentication." % username)
39         return None
40
41     def POST(self, *args, **kwargs):
42         username = kwargs.get("login_name")
43         password = kwargs.get("login_password")
44         user = None
45         error = None
46
47         if username and password:
48             user = self._authenticate(username, password)
49             if user:
50                 return self.lm.auth_successful(self.trans, user, 'password')
51             else:
52                 error = "Authentication failed"
53                 self.error(error)
54         else:
55             error = "Username or password is missing"
56             self.error("Error: " + error)
57
58         context = self.create_tmpl_context(
59             username=username,
60             error=error,
61             error_password=not password,
62             error_username=not username
63         )
64         self.lm.set_auth_error()
65         # pylint: disable=star-args
66         return self._template('login/form.html', **context)
67
68
69 class LoginManager(LoginManagerBase):
70
71     def __init__(self, *args, **kwargs):
72         super(LoginManager, self).__init__(*args, **kwargs)
73         self.name = 'pam'
74         self.path = 'pam'
75         self.page = None
76         self.description = """
77 Form based login Manager that uses the system's PAM infrastructure
78 for authentication. """
79         self.new_config(
80             self.name,
81             pconfig.String(
82                 'service name',
83                 'The name of the PAM service used to authenticate.',
84                 'remote'),
85             pconfig.String(
86                 'username text',
87                 'Text used to ask for the username at login time.',
88                 'Username'),
89             pconfig.String(
90                 'password text',
91                 'Text used to ask for the password at login time.',
92                 'Password'),
93             pconfig.String(
94                 'help text',
95                 'Text used to guide the user at login time.',
96                 'Provide your Username and Password')
97         )
98
99     @property
100     def service_name(self):
101         return self.get_config_value('service name')
102
103     @property
104     def help_text(self):
105         return self.get_config_value('help text')
106
107     @property
108     def username_text(self):
109         return self.get_config_value('username text')
110
111     @property
112     def password_text(self):
113         return self.get_config_value('password text')
114
115     def get_tree(self, site):
116         self.page = Pam(site, self, 'login/pam')
117         return self.page
118
119
120 class Installer(LoginManagerInstaller):
121
122     def __init__(self, *pargs):
123         super(Installer, self).__init__()
124         self.name = 'pam'
125         self.pargs = pargs
126
127     def install_args(self, group):
128         group.add_argument('--pam', choices=['yes', 'no'], default='no',
129                            help='Configure PAM authentication')
130         group.add_argument('--pam-service', action='store', default='remote',
131                            help='PAM service name to use for authentication')
132
133     def configure(self, opts):
134         if opts['pam'] != 'yes':
135             return
136
137         # Add configuration data to database
138         po = PluginObject(*self.pargs)
139         po.name = 'pam'
140         po.wipe_data()
141         po.wipe_config_values()
142         config = {'service name': opts['pam_service']}
143         po.save_plugin_config(config)
144
145         # Update global config to add login plugin
146         po.is_enabled = True
147         po.save_enabled_state()
148
149         # for selinux enabled platforms, ignore if it fails just report
150         try:
151             subprocess.call(['/usr/sbin/setsebool', '-P',
152                              'httpd_mod_auth_pam=on',
153                              'httpd_tmp_exec=on'])
154         except Exception:  # pylint: disable=broad-except
155             pass