Re: dbus and OpenVPN Autostart



On Mon, Feb 9, 2009 at 18:47, Dan Williams <dcbw redhat com> wrote:
> On Mon, 2009-02-09 at 11:37 +1100, David Guest wrote:
>> I am attempting to create a dispatcher script to autostart an OpenVPN
>> connection, I am stuck on how to get the vpn to connect through dbus.
>> Would anyone have a working example, preferably in python but any
>> language will do?
>>
>> I am running Ubuntu 8.10 (NetworkManager 0.7), I have found at least one
>> example on the web but it appears to be for an earlier dbus Network
>> Manager API version as I get errors when running them.
>>
>> I have looked at the 0.7 dbus API but can't figure out what to send to
>> the org.freedesktop.NetworkManager.VPN.Plugin.Connect method or even if
>> this is the right approach?
>
> That's actually the wrong approach here; what you want to do is tell
> _NetworkManager_ to connect the VPN connection.  So you'll be using the
> org.freedesktop.NetworkManager.ActivateConnection method, and pass it
> the service name of the settings service (either user or system) that
> provides the connection, the object path of the connection as exported
> by that settings service, and the device you'd like to activate the VPN
> on (which would be the object path of the interface your script got
> called with, probably).

This functionality is very often requested and a dispatcher script to
do that is quite hard to implement. I wrote a script to do that, see
the attachment. It needs some configuration first: The UUID of the VPN
connection you'd like to get automatically activated, the UUID of the
connection with which you want your VPN automatically activated, and
the UID of the user who has the VPN connection defined. For the first
two, just run the script without any arguments and it'll print out all
known connections and their UUIDS. Find your UID with `id -u`. After
changing these variables in the beginning of the script with your
data, copy it to /etc/NetworkManager/dispatcher.d/ and make sure it's
executable.

Dan, maybe it makes sense to add some example dispatcher scripts to
the tree, starting with this one? There's a lot you can do with these,
change active printers, proxies, mounts, ..., and many people have no
idea how useful they can be.

Tambet
#!/usr/bin/python

# Run this script without any arguments to list the available connection uuids.

# The uuid of the VPN connection to activate
#VPN_CONNECTION_UUID="ddf87e7a-15f4-4db0-a41d-f79edf12b44d"
VPN_CONNECTION_UUID=""

# The uuid of the connection that needs to be active to start the VPN connection
#ACTIVE_CONNECTION_UUID="b5c1c880-2060-421c-9c96-535bf8910313"
ACTIVE_CONNECTION_UUID=""

# UID to use. Note that NM only allows the owner of the connection to activate it.
#UID=1000
UID=0

import sys
import os
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject

DBusGMainLoop(set_as_default=True)

def get_connections():
    bus = dbus.SystemBus()
    proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', '/org/freedesktop/NetworkManagerSettings')
    iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings')
    return iface.ListConnections()


def get_connection_by_uuid(uuid):
    bus = dbus.SystemBus()
    for c in get_connections():
        proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', c)
        iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
        settings = iface.GetSettings()
        if settings['connection']['uuid'] == uuid:
            return c

    return None


def list_uuids():
    bus = dbus.SystemBus()
    for c in get_connections():
        proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', c)
        iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
        settings = iface.GetSettings()
        conn = settings['connection']
        print "%s - %s (%s)" % (conn['uuid'], conn['id'], conn['type'])


def get_active_connection_path(uuid):
    bus = dbus.SystemBus()
    proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
    iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
    active_connections = iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
    all_connections = get_connections()

    for a in active_connections:
        proxy = bus.get_object('org.freedesktop.NetworkManager', a)
        iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
        path = iface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection')

        proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', path)
        iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
        settings = iface.GetSettings()

        if settings['connection']['uuid'] == uuid:
            return a

    return None


def activate_connection(vpn_connection, active_connection):

    def reply_handler(opath):
        sys.exit(0)

    def error_handler(*args):
        sys.exit(1)

    bus = dbus.SystemBus()
    proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
    iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager')
    iface.ActivateConnection('org.freedesktop.NetworkManagerUserSettings',
                             vpn_connection,
                             dbus.ObjectPath("/"), 
                             active_connection,
                             reply_handler=reply_handler,
                             error_handler=error_handler)


# Change the UID first if required
if UID != 0:
    os.setuid(UID)

# Are we configured?
if len(VPN_CONNECTION_UUID) < 1 or len(ACTIVE_CONNECTION_UUID) < 1:
    sys.exit(0)

# NM dispatcer always calls us with certain arguments.
# In case no arguments are provided, simply list currently known
# connections with their uuids to help with configuration
if len(sys.argv) == 1:
    list_uuids()
    sys.exit(0)

vpn_connection = get_connection_by_uuid(VPN_CONNECTION_UUID)
if not vpn_connection:
    # Configured VPN connection is not known to NM, check VPN_CONNECTION_UUID.
    sys.exit(1)

active_connection = get_connection_by_uuid(ACTIVE_CONNECTION_UUID)
if not active_connection:
    # Configured active connection is not known to NM, check ACTIVE_CONNECTION_UUID.
    sys.exit(1)

# Is it already activated?
if get_active_connection_path(VPN_CONNECTION_UUID):
    sys.exit(0)

active_connection_path = get_active_connection_path(ACTIVE_CONNECTION_UUID)
if not active_connection_path:
    # The required connection isn't active at the moment
    sys.exit(0)

activate_connection(vpn_connection, active_connection_path)
loop = gobject.MainLoop()
loop.run()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]