|
Lines 33-101
Link Here
|
| 33 |
import sys, os, ipaddr, netifaces |
33 |
import sys, os, ipaddr, netifaces |
| 34 |
import socket, struct |
34 |
import socket, struct |
| 35 |
|
35 |
|
| 36 |
import univention.config_registry |
36 |
from univention.config_registry.frontend import ConfigRegistry, ucr_update |
| 37 |
|
|
|
| 38 |
configRegistry = univention.config_registry.ConfigRegistry() |
| 39 |
configRegistry.load() |
| 40 |
|
| 41 |
iface = os.environ.get('IFACE') |
| 42 |
|
37 |
|
| 43 |
# ignore lo and all |
38 |
# ignore lo and all |
| 44 |
if os.environ.get('METHOD') == 'loopback' or os.environ.get('ADDRFAM') == 'meta': |
39 |
if os.environ.get('METHOD') == 'loopback' or os.environ.get('ADDRFAM') == 'meta': |
| 45 |
sys.exit(0) |
40 |
sys.exit(0) |
| 46 |
|
41 |
|
| 47 |
# is interface configured as DHCP? |
42 |
# is interface configured as DHCP? |
|
|
43 |
iface = os.environ.get('IFACE') |
| 44 |
configRegistry = ConfigRegistry() |
| 45 |
configRegistry.load() |
| 48 |
if configRegistry.get('interfaces/%s/type' % iface) != 'dhcp': |
46 |
if configRegistry.get('interfaces/%s/type' % iface) != 'dhcp': |
| 49 |
sys.exit(0) |
47 |
sys.exit(0) |
| 50 |
|
48 |
|
| 51 |
# get first AF_INET interface |
49 |
# get first AF_INET interface |
| 52 |
inf = netifaces.ifaddresses(iface).get(netifaces.AF_INET)[0] |
50 |
inf = netifaces.ifaddresses(iface).get(netifaces.AF_INET)[0] |
| 53 |
|
51 |
try: |
| 54 |
ip = ipaddr.IPv4Network('%s/%s' % ( inf.get('addr'), inf.get('netmask') ) ) |
52 |
newip = inf.get('addr') |
|
|
53 |
ip = ipaddr.IPv4Network('%(addr)s/%(netmask)s' % inf) |
| 54 |
except KeyError: |
| 55 |
sys.exit(0) |
| 56 |
inf['address'] = newip |
| 55 |
inf['network'] = ip.network |
57 |
inf['network'] = ip.network |
| 56 |
|
58 |
|
| 57 |
# make the key equal to UCR |
|
|
| 58 |
inf['address'] = inf.get('addr') |
| 59 |
|
| 60 |
# save to UCR |
59 |
# save to UCR |
| 61 |
ucr_set = [] |
60 |
ucr_set = { |
|
|
61 |
'interfaces/restart/auto': configRegistry.get('interfaces/restart/auto'), |
| 62 |
} |
| 62 |
for k in ['netmask', 'address', 'broadcast', 'network']: |
63 |
for k in ['netmask', 'address', 'broadcast', 'network']: |
| 63 |
if inf.get(k): |
64 |
ucr_set['interfaces/%s/%s' % (iface, k)] = inf.get(k, None) |
| 64 |
ucr_set.append('interfaces/%s/%s=%s' % (iface, k, inf.get(k))) |
|
|
| 65 |
else: |
| 66 |
ucr_set.append('interfaces/%s/%s' % (iface, k)) |
| 67 |
|
65 |
|
| 68 |
# if old IP adress was set as nameserver, replace it with the new address |
66 |
# if old IP adress was set as nameserver, replace it with the new address |
| 69 |
oldip = configRegistry.get('interfaces/%s/address' % iface) |
67 |
oldip = configRegistry.get('interfaces/%s/address' % iface) |
| 70 |
if oldip: |
68 |
if oldip: |
| 71 |
for k in ['nameserver1', 'nameserver2', 'nameserver3']: |
69 |
for k in ['nameserver1', 'nameserver2', 'nameserver3']: |
| 72 |
if oldip == configRegistry.get(k): |
70 |
if oldip == configRegistry.get(k): |
| 73 |
ucr_set.append('%s=%s' % (k, inf.get('address'))) |
71 |
ucr_set[k] = newip |
| 74 |
|
72 |
|
| 75 |
# read gateway from proc |
73 |
# read gateway from proc |
| 76 |
gateway='' |
74 |
gateway = '' |
| 77 |
with open("/proc/net/route") as fh: |
75 |
with open("/proc/net/route") as fh: |
| 78 |
for line in fh: |
76 |
for line in fh: |
| 79 |
fields = line.strip().split() |
77 |
fields = line.strip().split() |
| 80 |
if fields[1] != '00000000' or not int(fields[3], 16) & 2: |
78 |
if fields[1] != '00000000' or not int(fields[3], 16) & 2: |
| 81 |
continue |
79 |
continue |
| 82 |
gateway=socket.inet_ntoa(struct.pack("<L", int(fields[2], 16))) |
80 |
gateway = socket.inet_ntoa(struct.pack("<L", int(fields[2], 16))) |
| 83 |
# write to UCR |
81 |
# write to UCR |
| 84 |
if configRegistry.get('gateway') != gateway: |
82 |
if configRegistry.get('gateway') != gateway: |
| 85 |
ucr_set.append('gateway=%s' % gateway) |
83 |
ucr_set['gateway'] = gateway |
| 86 |
|
84 |
|
| 87 |
# Redirect stdout |
85 |
# Redirect stdout |
| 88 |
null = open(os.path.devnull, 'w' ) |
86 |
sys.stdout = open(os.path.devnull, 'w') |
| 89 |
stdout = sys.stdout |
|
|
| 90 |
sys.stdout = null |
| 91 |
|
| 92 |
# Disable ifdown / ifup while setting new UCR variables to avoid an endless loop |
87 |
# Disable ifdown / ifup while setting new UCR variables to avoid an endless loop |
| 93 |
restart = configRegistry.get('interfaces/restart/auto', 'true') |
88 |
ucr_update({'interfaces/restart/auto': 'false'}) |
| 94 |
univention.config_registry.handler_set(['interfaces/restart/auto=false'], quiet=True) |
89 |
ucr_update(ucr_set) |
| 95 |
univention.config_registry.handler_set(ucr_set, quiet=True) |
|
|
| 96 |
univention.config_registry.handler_set(['interfaces/restart/auto=%s' % restart], quiet=True) |
| 97 |
|
| 98 |
sys.stdout = stdout |
| 99 |
|
| 100 |
sys.exit(0) |
| 101 |
|
| 102 |
- |
|
|
| 103 |
-- |
| 104 |
.../base/univention-network-manager/debian/control | 2 ++ |
90 |
.../base/univention-network-manager/debian/control | 2 ++ |
| 105 |
.../etc/network/if-up.d/00_resolvconf | 16 ++++++---------- |
91 |
.../etc/network/if-up.d/00_resolvconf | 16 ++++++---------- |
| 106 |
2 files changed, 8 insertions(+), 10 deletions(-) |
92 |
2 files changed, 8 insertions(+), 10 deletions(-) |