~ubuntu-branches/ubuntu/vivid/hplip/vivid-proposed

« back to all changes in this revision

Viewing changes to .pc/CVE-2013-6402.patch/base/pkit.py

  • Committer: Package Import Robot
  • Author(s): Mark Purcell, Till Kamppeter, Mark Purcell
  • Date: 2014-01-19 13:56:10 UTC
  • mfrom: (1.7.5)
  • Revision ID: package-import@ubuntu.com-20140119135610-7vvsrfbm2vfj0u1s
Tags: 3.14.1-1
* New upstream release 
  - CVE-2013-6427: hplip: insecure auto update feature
  - CVE-2013-6402: hplip: insecure temporary file handling in pkit.py

[ Till Kamppeter ]
* debian/control: Changed Depends/Recommends on foomatic-filters to
  "cups-filters (>= 1.0.42) | foomatic-filters" as foomatic-rip has
  moved to cups-filters from version 1.0.42 on (Closes: #734140).

[ Mark Purcell ]
* Ack NMU - thanks ~carnil
* Drop CVE-2013-6402.patch - addressed upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#
3
 
# (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
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 2 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, write to the Free Software
17
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
 
#
19
 
# Author: Stan Dolson , Goutam Kodu
20
 
#
21
 
 
22
 
# Std Lib
23
 
import os
24
 
import os.path
25
 
import sys
26
 
import re
27
 
import time
28
 
import cStringIO
29
 
import ConfigParser
30
 
import shutil
31
 
import stat
32
 
 
33
 
# Local
34
 
from base.logger import *
35
 
from base.g import *
36
 
from base.codes import *
37
 
from base import utils, password,services
38
 
from installer import pluginhandler
39
 
 
40
 
# DBus
41
 
import dbus
42
 
import dbus.service
43
 
import gobject
44
 
 
45
 
import warnings
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)
49
 
 
50
 
 
51
 
class AccessDeniedException(dbus.DBusException):
52
 
    _dbus_error_name = 'com.hp.hplip.AccessDeniedException'
53
 
 
54
 
class UnsupportedException(dbus.DBusException):
55
 
    _dbus_error_name = 'com.hp.hplip.UnsupportedException'
56
 
 
57
 
class UsageError(dbus.DBusException):
58
 
    _dbus_error_name = 'com.hp.hplip.UsageError'
59
 
 
60
 
 
61
 
POLICY_KIT_ACTION = "com.hp.hplip"
62
 
INSTALL_PLUGIN_ACTION = "com.hp.hplip.installplugin"
63
 
 
64
 
 
65
 
def get_service_bus():
66
 
    return dbus.SystemBus()
67
 
 
68
 
 
69
 
def get_service(bus=None):
70
 
    if not bus:
71
 
        bus = get_service_bus()
72
 
 
73
 
    service = bus.get_object(BackendService.SERVICE_NAME, '/')
74
 
    service = dbus.Interface(service, BackendService.INTERFACE_NAME)
75
 
    return service
76
 
 
77
 
 
78
 
class PolicyKitAuthentication(object):
79
 
    def __init__(self):
80
 
        super(PolicyKitAuthentication, self).__init__()
81
 
        self.pkit = None
82
 
        self.auth = None
83
 
 
84
 
 
85
 
    def is_authorized(self, action_id, pid=None):
86
 
        if pid == None:
87
 
            pid = os.getpid()
88
 
 
89
 
        pid = dbus.UInt32(pid)
90
 
 
91
 
        authorized = self.policy_kit.IsProcessAuthorized(action_id, pid, False)
92
 
        log.debug("is_authorized(%s) = %r" % (action_id, authorized))
93
 
 
94
 
        return (authorized == 'yes')
95
 
 
96
 
 
97
 
    def obtain_authorization(self, action_id, widget=None):
98
 
        if self.is_authorized(action_id):
99
 
            return True
100
 
 
101
 
        xid = (widget and widget.get_toplevel().window.xid or 0)
102
 
        xid, pid = dbus.UInt32(xid), dbus.UInt32(os.getpid())
103
 
 
104
 
        granted = self.auth_agent.ObtainAuthorization(action_id, xid, pid)
105
 
        log.debug("obtain_authorization(%s) = %r" % (action_id, granted))
106
 
 
107
 
        return bool(granted)
108
 
 
109
 
 
110
 
    def get_policy_kit(self):
111
 
        if self.pkit:
112
 
            return self.pkit
113
 
 
114
 
        service = dbus.SystemBus().get_object('org.freedesktop.PolicyKit', '/')
115
 
        self.pkit = dbus.Interface(service, 'org.freedesktop.PolicyKit')
116
 
        return self.pkit
117
 
 
118
 
    policy_kit = property(get_policy_kit)
119
 
 
120
 
 
121
 
    def get_auth_agent(self):
122
 
        if self.auth:
123
 
            return self.auth
124
 
 
125
 
        self.auth = dbus.SessionBus().get_object(
126
 
            'org.freedesktop.PolicyKit.AuthenticationAgent', '/')
127
 
        return self.auth
128
 
 
129
 
    auth_agent = property(get_auth_agent)
130
 
 
131
 
 
132
 
 
133
 
class PolicyKitService(dbus.service.Object):
134
 
    def check_permission_v0(self, sender, action=POLICY_KIT_ACTION):
135
 
        if not sender:
136
 
            log.error("Session not authorized by PolicyKit")
137
 
            raise AccessDeniedException('Session not authorized by PolicyKit')
138
 
 
139
 
        try:
140
 
            policy_auth = PolicyKitAuthentication()
141
 
            bus = dbus.SystemBus()
142
 
 
143
 
            dbus_object = bus.get_object('org.freedesktop.DBus', '/')
144
 
            dbus_object = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
145
 
 
146
 
            pid = dbus.UInt32(dbus_object.GetConnectionUnixProcessID(sender))
147
 
 
148
 
            granted = policy_auth.is_authorized(action, pid)
149
 
            if not granted:
150
 
                log.error("Process not authorized by PolicyKit")
151
 
                raise AccessDeniedException('Process not authorized by PolicyKit')
152
 
 
153
 
            granted = policy_auth.policy_kit.IsSystemBusNameAuthorized(action,
154
 
                                                                       sender,
155
 
                                                                       False)
156
 
            if granted != 'yes':
157
 
                log.error("Session not authorized by PolicyKit version 0")
158
 
                raise AccessDeniedException('Session not authorized by PolicyKit')
159
 
 
160
 
        except AccessDeniedException:
161
 
            log.warning("AccessDeniedException")
162
 
            raise
163
 
 
164
 
        except dbus.DBusException, ex:
165
 
            log.warning("AccessDeniedException %r", ex)
166
 
            raise AccessDeniedException(ex.message)
167
 
 
168
 
 
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')
173
 
 
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")
179
 
 
180
 
        subject = (
181
 
           'system-bus-name',
182
 
            { 'name' : dbus.String(sender, variant_level = 1) }
183
 
        )
184
 
        details = { '' : '' }
185
 
        flags = dbus.UInt32(1)         # AllowUserInteraction = 0x00000001
186
 
        cancel_id = ''
187
 
 
188
 
        (ok, notused, details) = \
189
 
            policy_kit.CheckAuthorization(subject,
190
 
                                          action,
191
 
                                          details,
192
 
                                          flags,
193
 
                                          cancel_id)
194
 
        if not ok:
195
 
            log.error("Session not authorized by PolicyKit version 1")
196
 
            raise AccessDeniedException("Session not authorized by PolicyKit")
197
 
 
198
 
        return ok
199
 
 
200
 
 
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'
206
 
 
207
 
        def __init__(self, connection=None, path='/', logfile=LOGFILE_NAME):
208
 
            if connection is None:
209
 
                connection = get_service_bus()
210
 
 
211
 
            super(BackendService, self).__init__(connection, path)
212
 
 
213
 
            self.name = dbus.service.BusName(self.SERVICE_NAME, connection)
214
 
            self.loop = gobject.MainLoop()
215
 
            self.version = 0
216
 
 
217
 
            log.set_logfile("%s.%d" % (logfile, os.getpid()))
218
 
            log.set_level("debug")
219
 
 
220
 
        def run(self, version=None):
221
 
            if version is None:
222
 
                version = policykit_version()
223
 
                if version is None:
224
 
                    log.error("Unable to determine installed PolicyKit version")
225
 
                    return
226
 
 
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)
230
 
 
231
 
            self.loop.run()
232
 
 
233
 
 
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:
240
 
                try:
241
 
                    self.check_permission_v0(sender, INSTALL_PLUGIN_ACTION)
242
 
                except AccessDeniedException, e:
243
 
                    return False
244
 
 
245
 
            elif self.version == 1:
246
 
                if not self.check_permission_v1(sender,
247
 
                                                connection,
248
 
                                                INSTALL_PLUGIN_ACTION):
249
 
                    return False
250
 
 
251
 
            else:
252
 
                log.error("installPlugin: invalid PolicyKit version %d" % self.version)
253
 
                return False
254
 
 
255
 
            log.debug("installPlugin: installing from '%s'" % src_dir)
256
 
            try:
257
 
                from installer import pluginhandler
258
 
            except ImportError,e:
259
 
                log.error("Failed to Import pluginhandler")
260
 
                return False
261
 
 
262
 
            pluginObj = pluginhandler.PluginHandle()
263
 
            if not pluginObj.copyFiles(src_dir):
264
 
                log.error("Plugin installation failed")
265
 
                return False
266
 
 
267
 
            return True
268
 
 
269
 
 
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")
276
 
            self.loop.quit()
277
 
 
278
 
            return True
279
 
 
280
 
 
281
 
 
282
 
class PolicyKit(object):
283
 
    def __init__(self, version=None):
284
 
        if version is None:
285
 
            version = policykit_version()
286
 
            if version is None:
287
 
                log.debug("Unable to determine installed PolicyKit version")
288
 
                return
289
 
 
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
294
 
 
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):
300
 
                    return None
301
 
 
302
 
        try:
303
 
            ok = self.iface.installPlugin(src_dir)
304
 
            return ok
305
 
        except dbus.DBusException, e:
306
 
            log.debug("installPlugin: %s" % str(e))
307
 
            return False
308
 
 
309
 
 
310
 
    def shutdown(self):
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):
315
 
                    return None
316
 
 
317
 
        try:
318
 
            ok = self.iface.shutdown("")
319
 
            return ok
320
 
        except dbus.DBusException, e:
321
 
            log.debug("shutdown: %s" % str(e))
322
 
            return False
323
 
 
324
 
 
325
 
 
326
 
 
327
 
 
328
 
def run_plugin_command(required=True, plugin_reason=PLUGIN_REASON_NONE, Mode = GUI_MODE):
329
 
    su_sudo = None
330
 
    need_sudo = True
331
 
    name = None
332
 
    version = None
333
 
 
334
 
    if utils.to_bool(sys_conf.get('configure', 'policy-kit')):
335
 
        try:
336
 
            obj = PolicyKit()
337
 
            su_sudo = "%s"
338
 
            need_sudo = False
339
 
            log.debug("Using PolicyKit for authentication")
340
 
        except dbus.DBusException, ex:
341
 
            log.error("PolicyKit NOT installed when configured for use")
342
 
 
343
 
    if os.geteuid() == 0:
344
 
        su_sudo = "%s"
345
 
        need_sudo = False
346
 
        
347
 
    passwordObj = password.Password(Mode)
348
 
    if need_sudo:
349
 
        su_sudo = passwordObj.getAuthType()
350
 
 
351
 
    if su_sudo is None:
352
 
        log.error("Unable to find a suitable sudo command to run 'hp-plugin'")
353
 
        return (False, False)
354
 
 
355
 
    req = '--required'
356
 
    if not required:
357
 
        req = '--optional'
358
 
 
359
 
    if utils.which("hp-plugin"):
360
 
        p_path="hp-plugin"
361
 
    else:
362
 
        p_path="python ./plugin.py"
363
 
 
364
 
    if 'gksu' in su_sudo:
365
 
        cmd = passwordObj.getAuthCmd() % ("%s -u %s --reason %s" % (p_path, req, plugin_reason))
366
 
        cmd +=" -m" 
367
 
        cmd += (" \"hp-plugin:- HP Device requires to install HP proprietary plugin. Please enter root password to continue\"")
368
 
    else:
369
 
        cmd = passwordObj.getAuthCmd() % ("%s -u %s --reason %s To_install_plugin_for_HP_Device" % (p_path, req, plugin_reason))
370
 
 
371
 
        
372
 
    log.debug("%s" % cmd)
373
 
    status, output = utils.run(cmd, passwordObj)
374
 
 
375
 
    return (status == 0, True)
376
 
 
377
 
 
378
 
def policykit_version():
379
 
    if os.path.isdir("/usr/share/polkit-1"):
380
 
        return 1
381
 
    elif os.path.isdir("/usr/share/PolicyKit"):
382
 
        return 0
383
 
    else:
384
 
        return None