Add uninstallation support.
[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         # pylint: disable=star-args
65         return self._template('login/form.html', **context)
66
67
68 class LoginManager(LoginManagerBase):
69
70     def __init__(self, *args, **kwargs):
71         super(LoginManager, self).__init__(*args, **kwargs)
72         self.name = 'pam'
73         self.path = 'pam'
74         self.page = None
75         self.description = """
76 Form based login Manager that uses the system's PAM infrastructure
77 for authentication. """
78         self.new_config(
79             self.name,
80             pconfig.String(
81                 'service name',
82                 'The name of the PAM service used to authenticate.',
83                 'remote'),
84             pconfig.String(
85                 'username text',
86                 'Text used to ask for the username at login time.',
87                 'Username'),
88             pconfig.String(
89                 'password text',
90                 'Text used to ask for the password at login time.',
91                 'Password'),
92             pconfig.String(
93                 'help text',
94                 'Text used to guide the user at login time.',
95                 'Provide your Username and Password')
96         )
97
98     @property
99     def service_name(self):
100         return self.get_config_value('service name')
101
102     @property
103     def help_text(self):
104         return self.get_config_value('help text')
105
106     @property
107     def username_text(self):
108         return self.get_config_value('username text')
109
110     @property
111     def password_text(self):
112         return self.get_config_value('password text')
113
114     def get_tree(self, site):
115         self.page = Pam(site, self, 'login/pam')
116         return self.page
117
118
119 class Installer(LoginManagerInstaller):
120
121     def __init__(self, *pargs):
122         super(Installer, self).__init__()
123         self.name = 'pam'
124         self.pargs = pargs
125
126     def install_args(self, group):
127         group.add_argument('--pam', choices=['yes', 'no'], default='no',
128                            help='Configure PAM authentication')
129         group.add_argument('--pam-service', action='store', default='remote',
130                            help='PAM service name to use for authentication')
131
132     def configure(self, opts):
133         if opts['pam'] != 'yes':
134             return
135
136         # Add configuration data to database
137         po = PluginObject(*self.pargs)
138         po.name = 'pam'
139         po.wipe_data()
140         po.wipe_config_values()
141         config = {'service name': opts['pam_service']}
142         po.save_plugin_config(config)
143
144         # Update global config to add login plugin
145         po.is_enabled = True
146         po.save_enabled_state()
147
148         # for selinux enabled platforms, ignore if it fails just report
149         try:
150             subprocess.call(['/usr/sbin/setsebool', '-P',
151                              'httpd_mod_auth_pam=on',
152                              'httpd_tmp_exec=on'])
153         except Exception:  # pylint: disable=broad-except
154             pass