Use self.log in authpam.py
[cascardo/ipsilon.git] / ipsilon / login / authpam.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from ipsilon.login.common import LoginFormBase, LoginManagerBase
21 from ipsilon.login.common import FACILITY
22 from ipsilon.util.plugin import PluginObject
23 import pam
24 import subprocess
25
26
27 class Pam(LoginFormBase):
28
29     def _authenticate(self, username, password):
30         if self.lm.service_name:
31             ok = pam.authenticate(username, password, self.lm.service_name)
32         else:
33             ok = pam.authenticate(username, password)
34
35         if ok:
36             self.log("User %s successfully authenticated." % username)
37             return username
38
39         self.log("User %s failed authentication." % username)
40         return None
41
42     def POST(self, *args, **kwargs):
43         username = kwargs.get("login_name")
44         password = kwargs.get("login_password")
45         user = None
46         error = None
47
48         if username and password:
49             user = self._authenticate(username, password)
50             if user:
51                 return self.lm.auth_successful(self.trans, user, 'password')
52             else:
53                 error = "Authentication failed"
54                 self.error(error)
55         else:
56             error = "Username or password is missing"
57             self.error("Error: " + error)
58
59         context = self.create_tmpl_context(
60             username=username,
61             error=error,
62             error_password=not password,
63             error_username=not username
64         )
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._options = {
80             'service name': [
81                 """ The name of the PAM service used to authenticate. """,
82                 'string',
83                 'remote'
84             ],
85             'help text': [
86                 """ The text shown to guide the user at login time. """,
87                 'string',
88                 'Insert your Username and Password and then submit.'
89             ],
90             'username text': [
91                 """ The text shown to ask for the username in the form. """,
92                 'string',
93                 'Username'
94             ],
95             'password text': [
96                 """ The text shown to ask for the password in the form. """,
97                 'string',
98                 'Password'
99             ],
100         }
101         self.conf_opt_order = ['service name', 'username text',
102                                'password text', 'help text']
103
104     @property
105     def service_name(self):
106         return self.get_config_value('service name')
107
108     @property
109     def help_text(self):
110         return self.get_config_value('help text')
111
112     @property
113     def username_text(self):
114         return self.get_config_value('username text')
115
116     @property
117     def password_text(self):
118         return self.get_config_value('password text')
119
120     def get_tree(self, site):
121         self.page = Pam(site, self, 'login/pam')
122         return self.page
123
124
125 class Installer(object):
126
127     def __init__(self):
128         self.name = 'pam'
129         self.ptype = 'login'
130
131     def install_args(self, group):
132         group.add_argument('--pam', choices=['yes', 'no'], default='no',
133                            help='Configure PAM authentication')
134         group.add_argument('--pam-service', action='store', default='remote',
135                            help='PAM service name to use for authentication')
136
137     def configure(self, opts):
138         if opts['pam'] != 'yes':
139             return
140
141         # Add configuration data to database
142         po = PluginObject()
143         po.name = 'pam'
144         po.wipe_data()
145
146         po.wipe_config_values(FACILITY)
147         config = {'service name': opts['pam_service']}
148         po.set_config(config)
149         po.save_plugin_config(FACILITY)
150
151         # Update global config to add login plugin
152         po = PluginObject()
153         po.name = 'global'
154         globalconf = po.get_plugin_config(FACILITY)
155         if 'order' in globalconf:
156             order = globalconf['order'].split(',')
157         else:
158             order = []
159         order.append('pam')
160         globalconf['order'] = ','.join(order)
161         po.set_config(globalconf)
162         po.save_plugin_config(FACILITY)
163
164         # for selinux enabled platforms, ignore if it fails just report
165         try:
166             subprocess.call(['/usr/sbin/setsebool', '-P',
167                              'httpd_mod_auth_pam=on',
168                              'httpd_tmp_exec=on'])
169         except Exception:  # pylint: disable=broad-except
170             pass