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