~ubuntu-branches/ubuntu/quantal/ubuntu-system-service/quantal-updates

« back to all changes in this revision

Viewing changes to UbuntuSystemService/backend.py

  • Committer: Package Import Robot
  • Author(s): Michael Vogt, Rodrigo Moya
  • Date: 2011-11-28 11:48:38 UTC
  • Revision ID: package-import@ubuntu.com-20111128114838-umgqjjb36ds7dvlm
Tags: 0.2.1
[ Rodrigo Moya ]
* merged lp:~rodrigo-moya/system-service/new-interfaces:
  - add org.freedesktop.hostname1 and org.freedesktop.locale1
    interfaces for systemd compatbility

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
    SUPPORTED_PROXIES = ("http","ftp", "https", "socks")
24
24
 
25
25
    # default files
26
 
    CONSOLE_SETUP_DEFAULT = "/etc/default/console-setup"
27
26
    DPKG_LOCK = "/var/lib/dpkg/lock"
28
27
    APT_ARCHIVES_LOCK = "/var/cache/apt/archives/lock"
29
28
    APT_LISTS_LOCK = "/var/lib/apt/lists/lock"
37
36
        dbus.service.Object.__init__(self, bus_name, '/')
38
37
        apt_pkg.init_config()
39
38
 
40
 
    def _authWithPolicyKit(self, sender, connection, priv):
41
 
        #print "_authWithPolicyKit()"
42
 
        system_bus = dbus.SystemBus()
43
 
        obj = system_bus.get_object("org.freedesktop.PolicyKit1", 
44
 
                                    "/org/freedesktop/PolicyKit1/Authority", 
45
 
                                    "org.freedesktop.PolicyKit1.Authority")
46
 
        policykit = dbus.Interface(obj, "org.freedesktop.PolicyKit1.Authority")
47
 
        info = dbus.Interface(connection.get_object('org.freedesktop.DBus',
48
 
                                              '/org/freedesktop/DBus/Bus', 
49
 
                                              False), 
50
 
                              'org.freedesktop.DBus')
51
 
        pid = info.GetConnectionUnixProcessID(sender) 
52
 
        #print "pid is:",pid
53
 
        #print "priv: ", priv
54
 
        subject = ('unix-process', 
55
 
                   { 'pid' : dbus.UInt32(pid, variant_level=1),
56
 
                     'start-time' : dbus.UInt64(0),
57
 
                   }
58
 
                  )
59
 
        details = { '' : '' }
60
 
        flags = dbus.UInt32(1) #   AllowUserInteraction = 0x00000001
61
 
        cancel_id = ''
62
 
        (ok, notused, details) = policykit.CheckAuthorization(subject,
63
 
                                                              priv, 
64
 
                                                              details,
65
 
                                                              flags,
66
 
                                                              cancel_id)
67
 
                                                              
68
 
        #print "ok: ", ok
69
 
        return ok
70
 
 
71
39
    # proxy stuff ---------------------------------------------------
72
40
    def _etc_environment_proxy(self, proxy_type):
73
41
        " internal that returns the /etc/environment proxy "
236
204
        modify /etc/environment
237
205
        
238
206
        """
239
 
        if not self._authWithPolicyKit(sender, conn, 
240
 
                                       "com.ubuntu.systemservice.setproxy"):
241
 
            if not self._authWithPolicyKit(sender, conn,
242
 
                                           "org.gnome.gconf.defaults.set-system"):
 
207
        if not authWithPolicyKit(sender, conn, 
 
208
                                 "com.ubuntu.systemservice.setproxy"):
 
209
            if not authWithPolicyKit(sender, conn,
 
210
                                     "org.gnome.gconf.defaults.set-system"):
243
211
                raise PermissionDeniedError, "Permission denied by policy"
244
212
        
245
213
        # check if something supported is set
298
266
        This function will modify /etc/environment
299
267
        
300
268
        """
301
 
        if not self._authWithPolicyKit(sender, conn, 
302
 
                                       "com.ubuntu.systemservice.setnoproxy"):
303
 
            if not self._authWithPolicyKit(sender, conn,
304
 
                                           "org.gnome.gconf.defaults.set-system"):
 
269
        if not authWithPolicyKit(sender, conn, 
 
270
                                 "com.ubuntu.systemservice.setnoproxy"):
 
271
            if not authWithPolicyKit(sender, conn,
 
272
                                     "org.gnome.gconf.defaults.set-system"):
305
273
                raise PermissionDeniedError, "Permission denied by policy"
306
274
        
307
275
        # set (or reset)
312
280
        return res
313
281
 
314
282
    # keyboard stuff ---------------------------------------------------
315
 
    def _get_keyboard_from_etc(self):
316
 
        """ 
317
 
        helper that reads /etc/default/console-setup and gets the 
318
 
        keyboard settings there
319
 
        """
320
 
        model = ""
321
 
        layout = ""
322
 
        variant = ""
323
 
        options = ""
324
 
        for line in open(self.CONSOLE_SETUP_DEFAULT):
325
 
            if line.startswith("XKBMODEL="):
326
 
                model = line.split("=")[1].strip('"\n')
327
 
            elif line.startswith("XKBLAYOUT="):
328
 
                layout = line.split("=")[1].strip('"\n')
329
 
            elif line.startswith("XKBVARIANT="):
330
 
                variant = line.split("=")[1].strip('"\n')
331
 
            elif line.startswith("XKBOPTIONS="):
332
 
                options = line.split("=")[1].strip('"\n')
333
 
        return (model, layout, variant, options)
334
 
 
335
283
    @dbus.service.method(DBUS_INTERFACE_NAME,
336
284
                         in_signature='', 
337
285
                         out_signature='ssss',
349
297
 
350
298
        It returns True on sucess
351
299
        """
352
 
        (model, layout, variant, options) = self._get_keyboard_from_etc()
 
300
        (model, layout, variant, options) = get_keyboard_from_etc()
353
301
        return (model, layout, variant, options)
354
302
 
355
 
    def _set_keyboard_to_etc(self, model, layout, variant, options):
356
 
        """ 
357
 
        helper that writes /etc/default/console-setup 
358
 
        """
359
 
        #print "set_keyboard_to_etc"
360
 
        # FIXME: what to do if not os.path.exists(self.CONSOLE_SETUP_DEFAULT)
361
 
        content = []
362
 
        for line in open(self.CONSOLE_SETUP_DEFAULT):
363
 
            if line.startswith("XKBMODEL="):
364
 
                line = 'XKBMODEL="%s"\n' % model
365
 
            elif line.startswith("XKBLAYOUT="):
366
 
                line = 'XKBLAYOUT="%s"\n' % layout
367
 
            elif line.startswith("XKBVARIANT="):
368
 
                line = 'XKBVARIANT="%s"\n' % variant
369
 
            elif line.startswith("XKBOPTIONS="):
370
 
                line = 'XKBOPTIONS="%s"\n' % options
371
 
            content.append(line)
372
 
        # if something changed, write 
373
 
        if content != open(self.CONSOLE_SETUP_DEFAULT).readlines():
374
 
            #print "content changed, writing"
375
 
            open(self.CONSOLE_SETUP_DEFAULT+".new","w").write("".join(content))
376
 
            os.rename(self.CONSOLE_SETUP_DEFAULT+".new", 
377
 
                      self.CONSOLE_SETUP_DEFAULT)
378
 
        return True
379
 
 
380
 
    def _verify_keyboard_settings(self, model, layout, variant, options):
381
 
        " helper that verfies the settings "
382
 
        # check against char whitelist
383
 
        allowed = "^[0-9a-zA-Z:,_]*$"
384
 
        for s in (model, layout, variant, options):
385
 
            if not re.match(allowed, s):
386
 
                #print "illegal chars in '%s'" % s
387
 
                return False
388
 
        # check if 'ckbcomp' can compile it
389
 
        cmd = ["ckbcomp"]
390
 
        if model:
391
 
            cmd += ["-model",model]
392
 
        if layout:
393
 
            cmd += ["-layout", layout]
394
 
        if variant:
395
 
            cmd += ["-variant", variant]
396
 
        if options:
397
 
            cmd += ["-option", options]
398
 
        ret = subprocess.call(cmd, stdout=open(os.devnull))
399
 
        return (ret == 0)
400
 
 
401
 
    def _run_setupcon(self):
402
 
        """
403
 
        helper that runs setupcon to activate the settings, taken from 
404
 
        oem-config (/usr/lib/oem-config/console/console-setup-apply)
405
 
        """
406
 
        ret = subprocess.call(["setupcon","--save-only"])
407
 
        subprocess.Popen(["/usr/sbin/update-initramfs","-u"])
408
 
        return (ret == 0)
409
 
 
410
303
    @dbus.service.method(DBUS_INTERFACE_NAME,
411
304
                         in_signature='ssss', 
412
305
                         out_signature='b',
418
311
        strings: (model, layout, variant, options)
419
312
        """
420
313
        #print "set_keyboard: ", model, layout, variant, options
421
 
        if not self._authWithPolicyKit(sender, conn, 
422
 
                                       "com.ubuntu.systemservice.setkeyboard"):
423
 
            if not self._authWithPolicyKit(sender, conn,
424
 
                                           "org.gnome.gconf.defaults.set-system"):
 
314
        if not authWithPolicyKit(sender, conn, 
 
315
                                 "com.ubuntu.systemservice.setkeyboard"):
 
316
            if not authWithPolicyKit(sender, conn,
 
317
                                     "org.gnome.gconf.defaults.set-system"):
425
318
 
426
319
                raise PermissionDeniedError, "Permission denied by policy"
427
 
 
428
 
        # if no keyboard model is set, try to guess one
429
 
        # this is based on the "console-setup.config" code that
430
 
        # defaults to pc105
431
 
        if not model:
432
 
            model = "pc105"
433
 
            if layout == "us":
434
 
                model = "pc104"
435
 
            elif layout == "br":
436
 
                model = "abnt2"
437
 
            elif layout == "jp":
438
 
                model = "jp106"
439
 
 
440
 
        # verify the settings
441
 
        if not self._verify_keyboard_settings(model, layout, variant, options):
442
 
            #print "verify_keyboard failed"
443
 
            raise InvalidKeyboardTypeError, "Invalid keyboard set"
444
320
        
445
321
        # apply
446
 
        if not self._set_keyboard_to_etc(model, layout, variant, options):
447
 
            #print "could not write keyboard to /etc"
448
 
            return False
449
 
        if not self._run_setupcon():
450
 
            #print "setupcon failed"
 
322
        if not set_keyboard_to_etc(model, layout, variant, options):
 
323
            print "could not write keyboard to /etc"
451
324
            return False
452
325
        return True
453
326
 
498
371
        Check if the package system is locked
499
372
        """
500
373
        #print "set_keyboard: ", model, layout, variant, options
501
 
        if not self._authWithPolicyKit(sender, conn, 
502
 
                                       "com.ubuntu.systemservice.ispkgsystemlocked"):
503
 
                raise PermissionDeniedError, "Permission denied by policy"
 
374
        if not authWithPolicyKit(sender, conn, 
 
375
                                 "com.ubuntu.systemservice.ispkgsystemlocked"):
 
376
            raise PermissionDeniedError, "Permission denied by policy"
504
377
        return self._is_package_system_locked()