Skip to content

Commit 535a846

Browse files
committed
More robust argument preprocessing
- Make a copy of the incoming arguments to avoid modifying data that was passed to us - Also pre-process arguments for the UpdateUnsaved method - Remove empty lists and dicts from the arguments to commands that accept settings to make round-tripping GetSettings -> Update work
1 parent e96f64d commit 535a846

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

NetworkManager.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# (C)2011-2016 Dennis Kaarsemaker
55
# License: zlib
66

7+
import copy
78
import dbus
89
import os
910
import socket
@@ -88,9 +89,9 @@ def unwrap(self, val):
8889
def wrap(self, val):
8990
if isinstance(val, NMDbusInterface):
9091
return val.object_path
91-
if hasattr(val, 'mro'):
92-
for klass in val.mro():
93-
if klass.__module__ == '_dbus_bindings':
92+
if hasattr(val.__class__, 'mro'):
93+
for klass in val.__class__.mro():
94+
if klass.__module__ in ('dbus', '_dbus_bindings'):
9495
return val
9596
if hasattr(val, '__iter__') and not isinstance(val, basestring):
9697
if hasattr(val, 'items'):
@@ -138,8 +139,8 @@ class NetworkManager(NMDbusInterface):
138139
object_path = '/org/freedesktop/NetworkManager'
139140

140141
def preprocess(self, name, args, kwargs):
141-
if name in ('AddConnection', 'Update', 'AddAndActivateConnection'):
142-
settings = args[0]
142+
if name in ('AddConnection', 'Update', 'UpdateUnsaved', 'AddAndActivateConnection'):
143+
settings = copy.deepcopy(args[0])
143144
for key in settings:
144145
if 'mac-address' in settings[key]:
145146
settings[key]['mac-address'] = fixups.mac_to_dbus(settings[key]['mac-address'])
@@ -163,6 +164,18 @@ def preprocess(self, name, args, kwargs):
163164
settings['ipv6']['routes'] = [fixups.route_to_dbus(route,socket.AF_INET6) for route in settings['ipv6']['routes']]
164165
if 'dns' in settings['ipv6']:
165166
settings['ipv6']['dns'] = [fixups.addr_to_dbus(addr,socket.AF_INET6) for addr in settings['ipv6']['dns']]
167+
# Get rid of empty arrays/dicts. dbus barfs on them (can't guess
168+
# signatures), and if they were to get through, NetworkManager
169+
# ignores them anyway.
170+
for key in list(settings.keys()):
171+
if isinstance(settings[key], dict):
172+
for key2 in list(settings[key].keys()):
173+
if settings[key][key2] in ({}, []):
174+
del settings[key][key2]
175+
if settings[key] in ({}, []):
176+
del settings[key]
177+
return (settings,) + args[1:], kwargs
178+
166179
return args, kwargs
167180
NetworkManager = NetworkManager()
168181

@@ -335,7 +348,7 @@ def preprocess(self, name, args, kwargs):
335348
conf['addresses'] = [fixups.addrconf_to_python(addr,socket.AF_INET) for addr in conf['addresses']]
336349
conf['routes'] = [fixups.route_to_python(route,socket.AF_INET) for route in conf['routes']]
337350
conf['dns'] = [fixups.addr_to_python(addr,socket.AF_INET) for addr in conf['dns']]
338-
return args, kwargs
351+
return (conf,) + args[1:], kwargs
339352

340353
class VPNPlugin(NMDbusInterface):
341354
interface_name = 'org.freedesktop.NetworkManager.VPN.Plugin'

0 commit comments

Comments
 (0)