Honor autoconnect user setting for VPN connections



The script below starts a configured VPN connection once the network is
up. It works for me on openSuSE 11.4 with NetworkManager 0.8.2 with a
gsm connection, ethernet may work as well. The script should be stored
in /etc/NetworkManager/dispatcher.d/vpn-auto.py

I send it here for review and comments.
Whats missing is a check wether the VPN connection is already active,
better logging.

I wonder when and how the autoconnect checkbox in the VPN config window
will actually work. Is it already implemented in 0.9?
If not, whats the plan to get this fixed?


Olaf


#!/usr/bin/python
# connect VPN once ethernet, wireless or umts connection is active:
#
# get kernel interface name from argv[1]
# search Devices in org.freedesktop.NetworkManager/ActiveConnections
# compare Interfaces/Properties/IpInterface with kernel interface name
# if match, search VPN connections with autoconnect enabled

import sys
import dbus
from syslog import syslog

debug = False
service_names = [ 'org.freedesktop.NetworkManagerUserSettings', 'org.freedesktop.NetworkManagerSystemSettings' ]
valid_connection_types = [ 'gsm', '802-3-ethernet' ]
invalid_connection_types = [ ]

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

def get_VPN_Connection_autoconnect(mydata):
	bus = dbus.SystemBus()
	for service_name in service_names:
		for connection in get_connections(service_name):
			if debug:
				syslog(" get_connection_autoconnect su %s connection %s" % (service_name, connection))
			proxy = bus.get_object(service_name, connection)
			iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
			settings = iface.GetSettings()
			if settings['connection']['type'] != 'vpn':
				continue
			if settings['connection'].has_key("autoconnect") and settings['connection']['autoconnect'] == 0:
				continue
			if debug:
				syslog(" get_connection_autoconnect settings '%s' " % settings['connection']['id'])
			mydata['service_name'] = service_name
			mydata['connection'] = connection
			return True
	return False


def find_IpInterface_in_Devices(interface, Devices):
	bus = dbus.SystemBus()
	for Device in Devices:
		proxy = bus.get_object('org.freedesktop.NetworkManager', Device)
		iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
		IpInterface = iface.Get('org.freedesktop.NetworkManager.Device', 'IpInterface')
		if debug:
			syslog("  IpInterface '%s' @ '%s'" % (IpInterface, Device))
		if interface == IpInterface:
			if debug:
				syslog("   Devices '%s'" % iface.GetAll('org.freedesktop.NetworkManager.Device'))
			return True
	return False

def get_ActiveConnections():
	bus = dbus.SystemBus()
	proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
	iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
	return iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')


def get_active_connection_path(mydata):
	ActiveConnections = get_ActiveConnections()
	bus = dbus.SystemBus()
	
	for connection in ActiveConnections:
		proxy = bus.get_object('org.freedesktop.NetworkManager', connection)
		iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
		Devices = iface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Devices')
		if debug:
			syslog(" Devices '%s'" % Devices)
		if not find_IpInterface_in_Devices(mydata['kernel_interface'], Devices):
			continue

		Connection = iface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection')
		ServiceName = iface.Get('org.freedesktop.NetworkManager.Connection.Active', 'ServiceName')
		if debug:
			syslog(" Connection '%s' ServiceName '%s'" % (Connection, ServiceName))

		proxy = bus.get_object(ServiceName, Connection)
		iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManagerSettings.Connection')
		Settings = iface.GetSettings()
		if debug:
			syslog(" Settings '%s'" % Settings)
		connection_type = Settings['connection']['type']
		syslog(" interface '%s' type '%s'" % (mydata['kernel_interface'], connection_type))
		if connection_type in invalid_connection_types:
			continue
		if connection_type in valid_connection_types:
			mydata['active_connection'] = connection
			return True

	return False

def activate_connection(mydata):

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

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

	service_name = mydata['service_name']
	connection = mydata['connection']
	device = dbus.ObjectPath("/")
	specific_object = mydata['active_connection']

	bus = dbus.SystemBus()
	proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
	iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager')

	if debug:
		syslog(" ActivateConnection(%s, %s, %s, %s)" % (service_name, connection, device, specific_object))
	ret = iface.ActivateConnection(service_name, connection, device, specific_object)
	if debug:
		syslog(" ret = '%s'" % ret)
        return


def do_it(kernel_interface):
	MyData = { }
	MyData['kernel_interface'] = kernel_interface
	if debug:
		syslog('looking for kernel interface "%s".' % MyData['kernel_interface'])

	if not get_active_connection_path(MyData):
		return

	if not get_VPN_Connection_autoconnect(MyData):
		return

	if debug:
		syslog("found interface %s: '%s'" % (kernel_interface, MyData))
	activate_connection(MyData)
	return

###
###
###

if debug:
	syslog("Argv %s" % sys.argv)
if len(sys.argv) != 3:
	syslog("Argv incorrect: %s" % sys.argv)
	sys.exit(0)

if sys.argv[2] != 'up':
	sys.exit(0);

do_it(sys.argv[1])

sys.exit(0);


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