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