dbus and OpenVPN Autostart

View: New views
10 Messages — Rating Filter:   Alert me  

dbus and OpenVPN Autostart

by David Guest-2 :: Rate this Message:

| View Threaded | Show Only this Message

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?

Thanks

David        





_______________________________________________
NetworkManager-list mailing list
NetworkManager-list@...
http://mail.gnome.org/mailman/listinfo/networkmanager-list

Re: dbus and OpenVPN Autostart

by Dan Williams :: Rate this Message:

| View Threaded | Show Only this Message

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).

Dan


_______________________________________________
NetworkManager-list mailing list
NetworkManager-list@...
http://mail.gnome.org/mailman/listinfo/networkmanager-list

Re: dbus and OpenVPN Autostart

by Tambet Ingo-2 :: Rate this Message:

| View Threaded | Show Only this Message

On Mon, Feb 9, 2009 at 18:47, Dan Williams <dcbw@...> 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

[vpn.py]

#!/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()


_______________________________________________
NetworkManager-list mailing list
NetworkManager-list@...
http://mail.gnome.org/mailman/listinfo/networkmanager-list

Re: dbus and OpenVPN Autostart

by Dan Williams :: Rate this Message:

| View Threaded | Show Only this Message

On Tue, 2009-02-10 at 11:26 +0200, Tambet Ingo wrote:

> On Mon, Feb 9, 2009 at 18:47, Dan Williams <dcbw@...> 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.

Sure!  Feel free to add it.  Note that git as of this morning allows
root to activate user connections as well; so the comment in the script
is somewhat misleading.

Dan


_______________________________________________
NetworkManager-list mailing list
NetworkManager-list@...
http://mail.gnome.org/mailman/listinfo/networkmanager-list

Re: dbus and OpenVPN Autostart

by Harald S. :: Rate this Message:

| View Threaded | Show Only this Message


Tambet Ingo-2 wrote:
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.
Thanks a lot for the script. My usecase is slightly different. At the office I need vpn to connect to the internet. I also need vpn to access my work email from home, but then I use split-tunnelling. Can you point me in the right direction to use one vpn connection for a specific access point and a different vpn connection for all other connections? I know some python but not how to deal with dbus.

Re: dbus and OpenVPN Autostart

by Dan Williams :: Rate this Message:

| View Threaded | Show Only this Message

On Tue, 2009-02-24 at 08:37 -0800, Harald S. wrote:

>
>
> Tambet Ingo-2 wrote:
> >
> >
> > 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.
> >
> >
>
> Thanks a lot for the script. My usecase is slightly different. At the office
> I need vpn to connect to the internet. I also need vpn to access my work
> email from home, but then I use split-tunnelling. Can you point me in the
> right direction to use one vpn connection for a specific access point and a
> different vpn connection for all other connections? I know some python but
> not how to deal with dbus.

Each connection in NM has a UUID.  Since you can obviously get the UUIDs
for both your Home and Work connections (look in gconf or wherever your
desktop environment stores connection details), you can use the UUID
(passed to the script in the CONNECTION_UUID environment variable) to
pick which VPN you want to bring up.

Dan

_______________________________________________
NetworkManager-list mailing list
NetworkManager-list@...
http://mail.gnome.org/mailman/listinfo/networkmanager-list

Re: dbus and OpenVPN Autostart

by Harald S. :: Rate this Message:

| View Threaded | Show Only this Message


Dan Williams wrote:
On Tue, 2009-02-24 at 08:37 -0800, Harald S. wrote:

> Thanks a lot for the script. My usecase is slightly different. At the office
> I need vpn to connect to the internet. I also need vpn to access my work
> email from home, but then I use split-tunnelling. Can you point me in the
> right direction to use one vpn connection for a specific access point and a
> different vpn connection for all other connections? I know some python but
> not how to deal with dbus.

Each connection in NM has a UUID.  Since you can obviously get the UUIDs
for both your Home and Work connections (look in gconf or wherever your
desktop environment stores connection details), you can use the UUID
(passed to the script in the CONNECTION_UUID environment variable) to
pick which VPN you want to bring up.
I forgot to mention that at the location I usually sit know I use wired network configured with DHCP. How does the script know the UUID for this connection? I also did not understand what was meant by "passed to the script in the CONNECTION_UUID environment variable".

Harald

Re: dbus and OpenVPN Autostart

by Dan Williams :: Rate this Message:

| View Threaded | Show Only this Message

On Tue, 2009-02-24 at 11:16 -0800, Harald S. wrote:

>
>
> Dan Williams wrote:
> >
> > On Tue, 2009-02-24 at 08:37 -0800, Harald S. wrote:
> >
> >> Thanks a lot for the script. My usecase is slightly different. At the
> >> office
> >> I need vpn to connect to the internet. I also need vpn to access my work
> >> email from home, but then I use split-tunnelling. Can you point me in the
> >> right direction to use one vpn connection for a specific access point and
> >> a
> >> different vpn connection for all other connections? I know some python
> >> but
> >> not how to deal with dbus.
> >
> > Each connection in NM has a UUID.  Since you can obviously get the UUIDs
> > for both your Home and Work connections (look in gconf or wherever your
> > desktop environment stores connection details), you can use the UUID
> > (passed to the script in the CONNECTION_UUID environment variable) to
> > pick which VPN you want to bring up.
> >
> >
>
> I forgot to mention that at the location I usually sit know I use wired
> network configured with DHCP. How does the script know the UUID for this
> connection? I also did not understand what was meant by "passed to the
> script in the CONNECTION_UUID environment variable".

By "Connection" I mean the package of config settings that
NetworkManager uses to bring up a network device.  Every
activate/deactivate action NM performs on a device will have an
associated "Connection" object.  These connections are stored in GConf
(for GNOME), somewhere else for KDE, or in your distro config files.
NetworkManager generates a UUID for each connection.  Thus, when your
dispatcher script gets called, its environment will already have the
CONNECTION_UUID variable set, and your dispatcher script can use that to
figure out what VPN to use.

Dan


_______________________________________________
NetworkManager-list mailing list
NetworkManager-list@...
http://mail.gnome.org/mailman/listinfo/networkmanager-list

Re: dbus and OpenVPN Autostart

by Harald S. :: Rate this Message:

| View Threaded | Show Only this Message

Is it possible to modify the script such that if I have vpn connecting automatically for all network connections, it will not automatically reconnect to the vpn if I disconnect the vpn connection manually.

What I would like is to have network-manager connecting automatically to a specific vpn access point, but I want to be able to choose a different access point manually when I need to.

Re: dbus and OpenVPN Autostart

by Dan Williams :: Rate this Message:

| View Threaded | Show Only this Message

On Mon, 2009-04-27 at 08:46 -0700, Harald S. wrote:
> Is it possible to modify the script such that if I have vpn connecting
> automatically for all network connections, it will not automatically
> reconnect to the vpn if I disconnect the vpn connection manually.

Hmm, that wouldn't be possible without a more complex script, since that
depends on specific state.  NM doesn't signal the script whether the
disconnect was user-triggered or a failure of some kind, although NM
does have that information internally.

If you wanted to do that now, you'd write a small daemon that listens on
D-Bus for the NM device state changes events, which include a result
code that tells you what the disconnection reason was.  The small daemon
could then do whatever it wanted to do with that information, and you
wouldn't need a dispatcher script at all.

Dispatcher scripts are simply kludges for stuff that can't talk D-Bus.
At a certain point, if you require functionality that's not reasonable
to implement in the dispatcher script case, you'd probably be better off
listening to dbus with a small daemon, because then you have the full NM
API to work with.

> What I would like is to have network-manager connecting automatically to a
> specific vpn access point, but I want to be able to choose a different
> access point manually when I need to.

In the end, we just need to make this work within NetworkManager.  It's
something people have been requesting for a while.

Dan


_______________________________________________
NetworkManager-list mailing list
NetworkManager-list@...
http://mail.gnome.org/mailman/listinfo/networkmanager-list