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