1
# -*- coding: utf-8 -*-
3
# (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
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 2 of the License, or
8
# (at your option) any later version.
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.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# Author: Stan Dolson , Goutam Kodu
34
from base.logger import *
36
from base.codes import *
37
from base import utils, password,services
38
from installer import pluginhandler
46
# Ignore: .../dbus/connection.py:242: DeprecationWarning: object.__init__() takes no parameters
47
# (occurring on Python 2.6/dBus 0.83/Ubuntu 9.04)
48
warnings.simplefilter("ignore", DeprecationWarning)
51
class AccessDeniedException(dbus.DBusException):
52
_dbus_error_name = 'com.hp.hplip.AccessDeniedException'
54
class UnsupportedException(dbus.DBusException):
55
_dbus_error_name = 'com.hp.hplip.UnsupportedException'
57
class UsageError(dbus.DBusException):
58
_dbus_error_name = 'com.hp.hplip.UsageError'
61
POLICY_KIT_ACTION = "com.hp.hplip"
62
INSTALL_PLUGIN_ACTION = "com.hp.hplip.installplugin"
65
def get_service_bus():
66
return dbus.SystemBus()
69
def get_service(bus=None):
71
bus = get_service_bus()
73
service = bus.get_object(BackendService.SERVICE_NAME, '/')
74
service = dbus.Interface(service, BackendService.INTERFACE_NAME)
78
class PolicyKitAuthentication(object):
80
super(PolicyKitAuthentication, self).__init__()
85
def is_authorized(self, action_id, pid=None):
89
pid = dbus.UInt32(pid)
91
authorized = self.policy_kit.IsProcessAuthorized(action_id, pid, False)
92
log.debug("is_authorized(%s) = %r" % (action_id, authorized))
94
return (authorized == 'yes')
97
def obtain_authorization(self, action_id, widget=None):
98
if self.is_authorized(action_id):
101
xid = (widget and widget.get_toplevel().window.xid or 0)
102
xid, pid = dbus.UInt32(xid), dbus.UInt32(os.getpid())
104
granted = self.auth_agent.ObtainAuthorization(action_id, xid, pid)
105
log.debug("obtain_authorization(%s) = %r" % (action_id, granted))
110
def get_policy_kit(self):
114
service = dbus.SystemBus().get_object('org.freedesktop.PolicyKit', '/')
115
self.pkit = dbus.Interface(service, 'org.freedesktop.PolicyKit')
118
policy_kit = property(get_policy_kit)
121
def get_auth_agent(self):
125
self.auth = dbus.SessionBus().get_object(
126
'org.freedesktop.PolicyKit.AuthenticationAgent', '/')
129
auth_agent = property(get_auth_agent)
133
class PolicyKitService(dbus.service.Object):
134
def check_permission_v0(self, sender, action=POLICY_KIT_ACTION):
136
log.error("Session not authorized by PolicyKit")
137
raise AccessDeniedException('Session not authorized by PolicyKit')
140
policy_auth = PolicyKitAuthentication()
141
bus = dbus.SystemBus()
143
dbus_object = bus.get_object('org.freedesktop.DBus', '/')
144
dbus_object = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
146
pid = dbus.UInt32(dbus_object.GetConnectionUnixProcessID(sender))
148
granted = policy_auth.is_authorized(action, pid)
150
log.error("Process not authorized by PolicyKit")
151
raise AccessDeniedException('Process not authorized by PolicyKit')
153
granted = policy_auth.policy_kit.IsSystemBusNameAuthorized(action,
157
log.error("Session not authorized by PolicyKit version 0")
158
raise AccessDeniedException('Session not authorized by PolicyKit')
160
except AccessDeniedException:
161
log.warning("AccessDeniedException")
164
except dbus.DBusException, ex:
165
log.warning("AccessDeniedException %r", ex)
166
raise AccessDeniedException(ex.message)
169
def check_permission_v1(self, sender, connection, action=POLICY_KIT_ACTION):
170
if not sender or not connection:
171
log.error("Session not authorized by PolicyKit")
172
raise AccessDeniedException('Session not authorized by PolicyKit')
174
system_bus = dbus.SystemBus()
175
obj = system_bus.get_object("org.freedesktop.PolicyKit1",
176
"/org/freedesktop/PolicyKit1/Authority",
177
"org.freedesktop.PolicyKit1.Authority")
178
policy_kit = dbus.Interface(obj, "org.freedesktop.PolicyKit1.Authority")
182
{ 'name' : dbus.String(sender, variant_level = 1) }
184
details = { '' : '' }
185
flags = dbus.UInt32(1) # AllowUserInteraction = 0x00000001
188
(ok, notused, details) = \
189
policy_kit.CheckAuthorization(subject,
195
log.error("Session not authorized by PolicyKit version 1")
196
raise AccessDeniedException("Session not authorized by PolicyKit")
201
if utils.to_bool(sys_conf.get('configure', 'policy-kit')):
202
class BackendService(PolicyKitService):
203
INTERFACE_NAME = 'com.hp.hplip'
204
SERVICE_NAME = 'com.hp.hplip'
205
LOGFILE_NAME = '/tmp/hp-pkservice.log'
207
def __init__(self, connection=None, path='/', logfile=LOGFILE_NAME):
208
if connection is None:
209
connection = get_service_bus()
211
super(BackendService, self).__init__(connection, path)
213
self.name = dbus.service.BusName(self.SERVICE_NAME, connection)
214
self.loop = gobject.MainLoop()
217
log.set_logfile("%s.%d" % (logfile, os.getpid()))
218
log.set_level("debug")
220
def run(self, version=None):
222
version = policykit_version()
224
log.error("Unable to determine installed PolicyKit version")
227
self.version = version
228
log.set_where(Logger.LOG_TO_CONSOLE_AND_FILE)
229
log.debug("Starting back-end service loop (version %d)" % version)
234
@dbus.service.method(dbus_interface=INTERFACE_NAME,
235
in_signature='s', out_signature='b',
236
sender_keyword='sender',
237
connection_keyword='connection')
238
def installPlugin(self, src_dir, sender=None, connection=None):
239
if self.version == 0:
241
self.check_permission_v0(sender, INSTALL_PLUGIN_ACTION)
242
except AccessDeniedException, e:
245
elif self.version == 1:
246
if not self.check_permission_v1(sender,
248
INSTALL_PLUGIN_ACTION):
252
log.error("installPlugin: invalid PolicyKit version %d" % self.version)
255
log.debug("installPlugin: installing from '%s'" % src_dir)
257
from installer import pluginhandler
258
except ImportError,e:
259
log.error("Failed to Import pluginhandler")
262
pluginObj = pluginhandler.PluginHandle()
263
if not pluginObj.copyFiles(src_dir):
264
log.error("Plugin installation failed")
270
@dbus.service.method(dbus_interface=INTERFACE_NAME,
271
in_signature='s', out_signature='b',
272
sender_keyword='sender',
273
connection_keyword='connection')
274
def shutdown(self, arg, sender=None, connection=None):
275
log.debug("Stopping backend service")
282
class PolicyKit(object):
283
def __init__(self, version=None):
285
version = policykit_version()
287
log.debug("Unable to determine installed PolicyKit version")
290
self.bus = dbus.SystemBus()
291
self.obj = self.bus.get_object(POLICY_KIT_ACTION, "/")
292
self.iface = dbus.Interface(self.obj, dbus_interface=POLICY_KIT_ACTION)
293
self.version = version
295
def installPlugin(self, src_dir):
296
if self.version == 0:
297
auth = PolicyKitAuthentication()
298
if not auth.is_authorized(INSTALL_PLUGIN_ACTION):
299
if not auth.obtain_authorization(INSTALL_PLUGIN_ACTION):
303
ok = self.iface.installPlugin(src_dir)
305
except dbus.DBusException, e:
306
log.debug("installPlugin: %s" % str(e))
311
if self.version == 0:
312
auth = PolicyKitAuthentication()
313
if not auth.is_authorized(INSTALL_PLUGIN_ACTION):
314
if not auth.obtain_authorization(INSTALL_PLUGIN_ACTION):
318
ok = self.iface.shutdown("")
320
except dbus.DBusException, e:
321
log.debug("shutdown: %s" % str(e))
328
def run_plugin_command(required=True, plugin_reason=PLUGIN_REASON_NONE, Mode = GUI_MODE):
334
if utils.to_bool(sys_conf.get('configure', 'policy-kit')):
339
log.debug("Using PolicyKit for authentication")
340
except dbus.DBusException, ex:
341
log.error("PolicyKit NOT installed when configured for use")
343
if os.geteuid() == 0:
347
passwordObj = password.Password(Mode)
349
su_sudo = passwordObj.getAuthType()
352
log.error("Unable to find a suitable sudo command to run 'hp-plugin'")
353
return (False, False)
359
if utils.which("hp-plugin"):
362
p_path="python ./plugin.py"
364
if 'gksu' in su_sudo:
365
cmd = passwordObj.getAuthCmd() % ("%s -u %s --reason %s" % (p_path, req, plugin_reason))
367
cmd += (" \"hp-plugin:- HP Device requires to install HP proprietary plugin. Please enter root password to continue\"")
369
cmd = passwordObj.getAuthCmd() % ("%s -u %s --reason %s To_install_plugin_for_HP_Device" % (p_path, req, plugin_reason))
372
log.debug("%s" % cmd)
373
status, output = utils.run(cmd, passwordObj)
375
return (status == 0, True)
378
def policykit_version():
379
if os.path.isdir("/usr/share/polkit-1"):
381
elif os.path.isdir("/usr/share/PolicyKit"):