From 1ce948b07a16576786402096543291dd3576f019 Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Sat, 4 Sep 2010 11:05:41 +0200 Subject: [PATCH] Aggiunto makefile --- 99_poisson_vpn.py | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 11 ++++ activate_vpn.py | 170 ------------------------------------------------------ 3 files changed, 181 insertions(+), 170 deletions(-) create mode 100755 99_poisson_vpn.py create mode 100644 Makefile delete mode 100755 activate_vpn.py diff --git a/99_poisson_vpn.py b/99_poisson_vpn.py new file mode 100755 index 0000000..63da7d7 --- /dev/null +++ b/99_poisson_vpn.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +import dbus, sys +from dbus.mainloop.glib import DBusGMainLoop + +DBusGMainLoop(set_as_default=True) + +def init(): + """ + Preconnection instructions + """ + global bus + bus = dbus.SystemBus() + +def is_poisson_vpn(settings): + """ + Recognize poisson vpn from the settings returned + by dbus + """ + if settings['connection']['type'] != "vpn": + return False + if settings['vpn']['data']['remote'] != "poisson.phc-priv": + return False + + # 1194 is the default port for the VPN so you don't need to specify it in the + # configuration, but if you do and if you speciy something different from 1194, + # this is not the right VPN + if settings['vpn']['data'].has_key("port") and settings['vpn']['data']['port'] != 1194: + return False + + # If you arrived here this really is poisson vpn! + return True + +def get_poisson_vpn(): + """ + Obtain DBus object for the poisson vpn connection + """ + + # Try to load configuration for the VPN to poisson + proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings", + "/org/freedesktop/NetworkManagerSettings") + interface = dbus.Interface(proxy, "org.freedesktop.NetworkManagerSettings") + + # If we can't find the vpn matching the following: + # - remote: poisson.phc-priv + # - port: 1194 + # then we return None + c = None + + # Check all connections stored in UserSettings to see if they match the + # Poisson vpn. We don't check SystemSettings at the moment being because + # it's likely to require root privileges (or not?) + for connection in interface.ListConnections(): + proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings", + connection) + settings = proxy.GetSettings(dbus_interface="org.freedesktop.NetworkManagerSettings.Connection") + + # This should be a deterministic check :) + if is_poisson_vpn (settings): + c = connection + + # Check if VPN is already active. If it is, return None because we have nothing + # to do + proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager") + interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + + # Try to match ConnectionSettings to an ActiveConnection + for connection in interface.Get("org.freedesktop.NetworkManager", "ActiveConnections"): + proxy = bus.get_object("org.freedesktop.NetworkManager", connection) + interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + connection = interface.Get("org.freedesktop.NetworkManager.Connection.Active", "Connection") + if connection == c: + return None + + # Return the connection (or None) + return c + +def get_base_connection(): + """ + Obtain active connection if matching the ones that can connect to + poisson.phc-priv. If not, return None + """ + + # Get the list of the active connections right now + proxy = bus.get_object("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager") + interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + active_connections = interface.Get('org.freedesktop.NetworkManager', 'ActiveConnections') + + # Examine every active connection and if one of the matching MAC address + # is found return the connection object + for connection in active_connections: + proxy = bus.get_object("org.freedesktop.NetworkManager", + connection) + interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + path = interface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection') + + proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', + path) + interface = dbus.Interface(proxy, 'org.freedesktop.NetworkManagerSettings.Connection') + settings = interface.GetSettings() + + # Check if the user is connected to a known wireless ap. If it is, then + # we can activate the VPN so we return a connection DBus object + if settings.has_key("802-11-wireless"): + + # MAC address of CDCWL1 ap + if settings['802-11-wireless']['seen-bssids'][0] == u'00:12:0E:8C:AE:A0': + return connection + + # MAC address of PHC-wifi ap + if settings['802-11-wireless']['seen-bssids'][0] == u'00:0f:cb:aa:16:52': + return connection + + # The active connections aren't directly connected to poisson.phc-priv + # so we can't activate the VPN + return None + + +def activate_connection(connection): + """ + Activate connection + """ + proxy = bus.get_object("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager") + interface = dbus.Interface (proxy, "org.freedesktop.NetworkManager") + + # Obtain base connection to active VPN on + base_connection = get_base_connection () + + if base_connection is not None: + interface.ActivateConnection('org.freedesktop.NetworkManagerUserSettings', + connection, + dbus.ObjectPath("/"), + base_connection) + +if __name__ == "__main__": + + # If you turn vpn donw you probably don't + # want me to fire it up again + if sys.argv[2] == "vpn-down": + sys.exit (0) + + # If you just tried to attach a VPN, you probably + # don't want another one + if sys.argv[2] == "vpn-up": + sys.exit (0) + + # If you deconfigured an interface, you probably + # don't want to autostart a VPN (o sì? :)) + if sys.argv[2] == "down": + sys.exit (0) + + # Preconnection scripts + init () + + # Obtain poisson vpn + poisson_vpn = get_poisson_vpn () + + # If poisson_vpn is None it means that it + # is already active, so exit now + if (poisson_vpn is None): + sys.exit (0) + + # Activate connection + activate_connection (poisson_vpn) + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..97e9b9b --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +SCRIPT_FILE=99_poisson_vpn.py +NM_DISPATCHER_DIR=/etc/NetworkManager/dispatcher.d/ + +all: + + +install: + install -m 755 -o root $(SCRIPT_FILE) $(NM_DISPATCHER_DIR) + +uninstall: + rm -f $(NM_DISPATCHER_DIR)$(SCRIPT_FILE) diff --git a/activate_vpn.py b/activate_vpn.py deleted file mode 100755 index 63da7d7..0000000 --- a/activate_vpn.py +++ /dev/null @@ -1,170 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - - -import dbus, sys -from dbus.mainloop.glib import DBusGMainLoop - -DBusGMainLoop(set_as_default=True) - -def init(): - """ - Preconnection instructions - """ - global bus - bus = dbus.SystemBus() - -def is_poisson_vpn(settings): - """ - Recognize poisson vpn from the settings returned - by dbus - """ - if settings['connection']['type'] != "vpn": - return False - if settings['vpn']['data']['remote'] != "poisson.phc-priv": - return False - - # 1194 is the default port for the VPN so you don't need to specify it in the - # configuration, but if you do and if you speciy something different from 1194, - # this is not the right VPN - if settings['vpn']['data'].has_key("port") and settings['vpn']['data']['port'] != 1194: - return False - - # If you arrived here this really is poisson vpn! - return True - -def get_poisson_vpn(): - """ - Obtain DBus object for the poisson vpn connection - """ - - # Try to load configuration for the VPN to poisson - proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings", - "/org/freedesktop/NetworkManagerSettings") - interface = dbus.Interface(proxy, "org.freedesktop.NetworkManagerSettings") - - # If we can't find the vpn matching the following: - # - remote: poisson.phc-priv - # - port: 1194 - # then we return None - c = None - - # Check all connections stored in UserSettings to see if they match the - # Poisson vpn. We don't check SystemSettings at the moment being because - # it's likely to require root privileges (or not?) - for connection in interface.ListConnections(): - proxy = bus.get_object("org.freedesktop.NetworkManagerUserSettings", - connection) - settings = proxy.GetSettings(dbus_interface="org.freedesktop.NetworkManagerSettings.Connection") - - # This should be a deterministic check :) - if is_poisson_vpn (settings): - c = connection - - # Check if VPN is already active. If it is, return None because we have nothing - # to do - proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager") - interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") - - # Try to match ConnectionSettings to an ActiveConnection - for connection in interface.Get("org.freedesktop.NetworkManager", "ActiveConnections"): - proxy = bus.get_object("org.freedesktop.NetworkManager", connection) - interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") - connection = interface.Get("org.freedesktop.NetworkManager.Connection.Active", "Connection") - if connection == c: - return None - - # Return the connection (or None) - return c - -def get_base_connection(): - """ - Obtain active connection if matching the ones that can connect to - poisson.phc-priv. If not, return None - """ - - # Get the list of the active connections right now - proxy = bus.get_object("org.freedesktop.NetworkManager", - "/org/freedesktop/NetworkManager") - interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") - active_connections = interface.Get('org.freedesktop.NetworkManager', 'ActiveConnections') - - # Examine every active connection and if one of the matching MAC address - # is found return the connection object - for connection in active_connections: - proxy = bus.get_object("org.freedesktop.NetworkManager", - connection) - interface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") - path = interface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection') - - proxy = bus.get_object('org.freedesktop.NetworkManagerUserSettings', - path) - interface = dbus.Interface(proxy, 'org.freedesktop.NetworkManagerSettings.Connection') - settings = interface.GetSettings() - - # Check if the user is connected to a known wireless ap. If it is, then - # we can activate the VPN so we return a connection DBus object - if settings.has_key("802-11-wireless"): - - # MAC address of CDCWL1 ap - if settings['802-11-wireless']['seen-bssids'][0] == u'00:12:0E:8C:AE:A0': - return connection - - # MAC address of PHC-wifi ap - if settings['802-11-wireless']['seen-bssids'][0] == u'00:0f:cb:aa:16:52': - return connection - - # The active connections aren't directly connected to poisson.phc-priv - # so we can't activate the VPN - return None - - -def activate_connection(connection): - """ - Activate connection - """ - proxy = bus.get_object("org.freedesktop.NetworkManager", - "/org/freedesktop/NetworkManager") - interface = dbus.Interface (proxy, "org.freedesktop.NetworkManager") - - # Obtain base connection to active VPN on - base_connection = get_base_connection () - - if base_connection is not None: - interface.ActivateConnection('org.freedesktop.NetworkManagerUserSettings', - connection, - dbus.ObjectPath("/"), - base_connection) - -if __name__ == "__main__": - - # If you turn vpn donw you probably don't - # want me to fire it up again - if sys.argv[2] == "vpn-down": - sys.exit (0) - - # If you just tried to attach a VPN, you probably - # don't want another one - if sys.argv[2] == "vpn-up": - sys.exit (0) - - # If you deconfigured an interface, you probably - # don't want to autostart a VPN (o sì? :)) - if sys.argv[2] == "down": - sys.exit (0) - - # Preconnection scripts - init () - - # Obtain poisson vpn - poisson_vpn = get_poisson_vpn () - - # If poisson_vpn is None it means that it - # is already active, so exit now - if (poisson_vpn is None): - sys.exit (0) - - # Activate connection - activate_connection (poisson_vpn) - - -- 2.1.4