Exemple #1
0
 def test_ipv6_order_multi(self):
     """Test multiple IPv6 ordering."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/ipv6/foo/address': '1:2:3:4:5:6:7:8',
             'interfaces/eth0/ipv6/foo/prefix': '64',
             'interfaces/eth1/order': '2',
             'interfaces/eth1/ipv6/default/address': '1:2:3:4:5:6:7:8',
             'interfaces/eth1/ipv6/default/prefix': '64',
             'interfaces/eth1/ipv6/a/address': '2:3:4:5:6:7:8:9',
             'interfaces/eth1/ipv6/a/prefix': '72',
             'interfaces/eth2/order': '1',
             'interfaces/eth2/ipv6/z/address': '1:2:3:4:5:6:7:8',
             'interfaces/eth2/ipv6/z/prefix': '64',
             'interfaces/eth2/ipv6/default/address': '2:3:4:5:6:7:8:9',
             'interfaces/eth2/ipv6/default/prefix': '72',
             'interfaces/primary': 'eth2,eth1',
         })
     assert [] == [s.name for _n, s in t.ipv4_interfaces]
     assert [
      ('eth2', 'default'),
      ('eth2', 'z'),
      ('eth1', 'default'),
      ('eth1', 'a'),
      ('eth0', 'foo')] == \
      [(s.name, n) for s, n in t.ipv6_interfaces]
     assert IPv6Interface(
         u'2:3:4:5:6:7:8:9/72') == t.get_default_ip_address()
     assert t.get_default_ipv4_address() is None
     assert IPv6Interface(
         u'2:3:4:5:6:7:8:9/72') == t.get_default_ipv6_address()
Exemple #2
0
 def test_empty(self):
     """Test no interface."""
     t = Interfaces(ucr={})
     assert 'eth0' == t.primary
     assert t.ipv4_gateway is None
     assert t.ipv6_gateway is None
     assert t.ipv6_gateway_zone_index is None
     assert [] == [s.name for _n, s in t.ipv4_interfaces]
     assert [] == [s.name for s, _n in t.ipv6_interfaces]
     assert t.get_default_ip_address() is None
     assert t.get_default_ipv4_address() is None
     assert t.get_default_ipv6_address() is None
Exemple #3
0
 def test_incomplete_net(self):
     """Test incomplete interface with netmask/prefix."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/netmask': '255.255.255.0',
             'interfaces/eth0/ipv6/default/prefix': '64',
         })
     assert [] == [s.name for _n, s in t.ipv4_interfaces]
     assert [] == [s.name for s, _n in t.ipv6_interfaces]
     assert t.get_default_ip_address() is None
     assert t.get_default_ipv4_address() is None
     assert t.get_default_ipv6_address() is None
Exemple #4
0
 def test_incomplete_addr(self):
     """Test incomplete interface with address."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/address': '2.3.4.5',
             'interfaces/eth0/ipv6/default/address': '1:2:3:4:5:6:7:8',
         })
     assert [] == [s.name for _n, s in t.ipv4_interfaces]
     assert [] == [s.name for s, _n in t.ipv6_interfaces]
     assert t.get_default_ip_address() is None
     assert t.get_default_ipv4_address() is None
     assert t.get_default_ipv6_address() is None
Exemple #5
0
 def test_ipv4_only(self):
     """Test IPv4 only interface."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/address': '1.2.3.4',
             'interfaces/eth0/netmask': '255.255.255.0',
         })
     assert ['eth0'] == [s.name for _n, s in t.ipv4_interfaces]
     assert [] == [s.name for s, _n in t.ipv6_interfaces]
     assert IPv4Interface(u'1.2.3.4/24') == t.get_default_ip_address()
     assert IPv4Interface(u'1.2.3.4/24') == t.get_default_ipv4_address()
     assert t.get_default_ipv6_address() is None
Exemple #6
0
 def test_ipv4_multi(self):
     """Test multiple IPv4 interfaces."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/address': '1.2.3.4',
             'interfaces/eth0/netmask': '255.255.255.0',
             'interfaces/eth1/address': '2.3.4.5',
             'interfaces/eth1/netmask': '255.255.255.0',
         })
     assert ['eth0' == 'eth1'], [s.name for _n, s in t.ipv4_interfaces]
     assert [] == [s.name for s, _n in t.ipv6_interfaces]
     assert IPv4Interface(u'1.2.3.4/24') == t.get_default_ip_address()
     assert IPv4Interface(u'1.2.3.4/24') == t.get_default_ipv4_address()
     assert t.get_default_ipv6_address() is None
Exemple #7
0
 def test_ipv6_disjunct(self):
     """Test disjunct IPv4 IPv6 interfaces."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/address': '2.3.4.5',
             'interfaces/eth0/netmask': '255.255.255.0',
             'interfaces/eth1/ipv6/default/address': '1:2:3:4:5:6:7:8',
             'interfaces/eth1/ipv6/default/prefix': '64',
         })
     assert ['eth0'] == [s.name for _n, s in t.ipv4_interfaces]
     assert ['eth1'] == [s.name for s, _n in t.ipv6_interfaces]
     assert IPv4Interface(u'2.3.4.5/24') == t.get_default_ip_address()
     assert IPv4Interface(u'2.3.4.5/24') == t.get_default_ipv4_address()
     assert IPv6Interface(
         u'1:2:3:4:5:6:7:8/64') == t.get_default_ipv6_address()
Exemple #8
0
 def test_ipv6_multi(self):
     """Test multiple IPv6 interfaces."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/ipv6/default/address': '1:2:3:4:5:6:7:8',
             'interfaces/eth0/ipv6/default/prefix': '64',
             'interfaces/eth1/ipv6/default/address': '2:3:4:5:6:7:8:9',
             'interfaces/eth1/ipv6/default/prefix': '64',
         })
     assert [] == [s.name for _n, s in t.ipv4_interfaces]
     assert ['eth0', 'eth1'] == [s.name for s, _n in t.ipv6_interfaces]
     assert IPv6Interface(
         u'1:2:3:4:5:6:7:8/64') == t.get_default_ip_address()
     assert t.get_default_ipv4_address() is None
     assert IPv6Interface(
         u'1:2:3:4:5:6:7:8/64') == t.get_default_ipv6_address()
Exemple #9
0
 def test_ipv4_order(self):
     """Test IPv4 ordering."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/address': '1.2.3.4',
             'interfaces/eth0/netmask': '255.255.255.0',
             'interfaces/eth1/address': '2.3.4.5',
             'interfaces/eth1/netmask': '255.255.0.0',
             'interfaces/eth2/order': '1',
             'interfaces/eth2/address': '3.4.5.6',
             'interfaces/eth2/netmask': '255.0.0.0',
         })
     assert ['eth2', 'eth0',
             'eth1'] == [s.name for _n, s in t.ipv4_interfaces]
     assert [] == [s.name for s, _n in t.ipv6_interfaces]
     assert IPv4Interface(u'1.2.3.4/24') == t.get_default_ip_address()
     assert IPv4Interface(u'1.2.3.4/24') == t.get_default_ipv4_address()
     assert t.get_default_ipv6_address() is None
Exemple #10
0
 def test_ipv6_order(self):
     """Test IPv6 ordering."""
     t = Interfaces(
         ucr={
             'interfaces/eth0/ipv6/default/address': '1:2:3:4:5:6:7:8',
             'interfaces/eth0/ipv6/default/prefix': '64',
             'interfaces/eth1/ipv6/default/address': '2:3:4:5:6:7:8:9',
             'interfaces/eth1/ipv6/default/prefix': '72',
             'interfaces/eth2/order': '1',
             'interfaces/eth2/ipv6/default/address': '3:4:5:6:7:8:9:a',
             'interfaces/eth2/ipv6/default/prefix': '80',
         })
     assert [] == [s.name for _n, s in t.ipv4_interfaces]
     assert ['eth2', 'eth0',
             'eth1'] == [s.name for s, _n in t.ipv6_interfaces]
     assert IPv6Interface(
         u'1:2:3:4:5:6:7:8/64') == t.get_default_ip_address()
     assert t.get_default_ipv4_address() is None
     assert IPv6Interface(
         u'1:2:3:4:5:6:7:8/64') == t.get_default_ipv6_address()
def _handler(ucr, changes):
	changed_entries = set()
	for key in changes.keys():
		match = re.match('ucs/web/overview/entries/(admin|service)/([^/]+)/.*', key)
		if match:
			changed_entries.add(match.group(2))
	changed_entries -= set(['umc', 'invalid-certificate-list', 'root-certificate', 'ldap-master'])
	portal_logger.debug('Changed: %r' % changed_entries)
	if not changed_entries:
		return
	lo, pos = get_machine_connection()
	pos.setDn('cn=portal,cn=univention,%s' % ucr.get('ldap/base'))
	interfaces = Interfaces(ucr)
	hostname = '%s.%s' % (ucr.get('hostname'), ucr.get('domainname'))
	default_ipv4_address = interfaces.get_default_ipv4_address()
	if default_ipv4_address:
		default_ipv4_address = str(default_ipv4_address.ip)
	default_ipv6_address = interfaces.get_default_ipv6_address()
	if default_ipv6_address:
		default_ipv6_address = str(default_ipv6_address.ip)
	local_hosts = [hostname, default_ipv4_address, default_ipv6_address]
	portal_logger.debug('Local hosts are: %r' % local_hosts)
	attr_entries = {}
	for changed_entry in changed_entries:
		attr_entries[changed_entry] = {}
	for ucr_key in ucr.keys():
		match = re.match('ucs/web/overview/entries/([^/]+)/([^/]+)/(.*)', ucr_key)
		if not match:
			continue
		category = match.group(1)
		cn = match.group(2)
		key = match.group(3)
		value = ucr.get(ucr_key)
		if cn in attr_entries:
			portal_logger.debug('Matched %r -> %r' % (ucr_key, value))
			entry = attr_entries[cn]
			entry['category'] = category
			entry['name'] = cn
			if '_links' not in entry:
				links = []
				for host in local_hosts:
					if host:
						links.append(_Link(host=host))
				entry['_links'] = links
			if key == 'link':
				for link in entry['_links']:
					if value.startswith('http'):
						link.full = value
					else:
						link.path = value
			elif key == 'port_http':
				if value:
					for link in entry['_links'][:]:
						if link.protocol == 'https':
							link = copy(link)
							entry['_links'].append(link)
						link.protocol = 'http'
						link.port = value
			elif key == 'port_https':
				if value:
					for link in entry['_links'][:]:
						if link.protocol == 'http':
							link = copy(link)
							entry['_links'].append(link)
						link.protocol = 'https'
						link.port = value
			elif key == 'icon':
				try:
					if value.startswith('/univention-management-console'):
						value = '/univention%s' % value[30:]
					with open('/var/www/%s' % value) as fd:
						entry['icon'] = b64encode(fd.read())
				except EnvironmentError:
					pass
			elif key == 'label':
				entry.setdefault('displayName', [])
				entry['displayName'].append(('en_US', value))
			elif key == 'label/de':
				entry.setdefault('displayName', [])
				entry['displayName'].append(('de_DE', value))
			elif key == 'label/fr':
				entry.setdefault('displayName', [])
				entry['displayName'].append(('fr_FR', value))
			elif key == 'description':
				entry.setdefault('description', [])
				entry['description'].append(('en_US', value))
			elif key == 'description/de':
				entry.setdefault('description', [])
				entry['description'].append(('de_DE', value))
			elif key == 'description/fr':
				entry.setdefault('description', [])
				entry['description'].append(('fr_FR', value))
			else:
				portal_logger.info('Don\'t know how to handle UCR key %s' % ucr_key)
	for cn, attrs in attr_entries.items():
		dn = 'cn=%s,%s' % (escape_dn_chars(cn), pos.getDn())
		unprocessed_links = attrs.pop('_links', [])
		my_links = set()
		no_ports = all(not link.port for link in unprocessed_links)
		for link in unprocessed_links:
			if no_ports:
				if link.protocol == 'http':
					link.port = '80'
				elif link.protocol == 'https':
					link.port = '443'
			if link:
				my_links.add(str(link))
			if not link.protocol:
				link.protocol = 'http'
				if link:
					my_links.add(str(link))
				link.protocol = 'https'
				if link:
					my_links.add(str(link))
		my_links = list(my_links)
		portal_logger.debug('Processing %s' % dn)
		portal_logger.debug('Attrs: %r' % attrs)
		portal_logger.debug('Links: %r' % my_links)
		try:
			obj = init_object('settings/portal_entry', lo, pos, dn)
		except udm_errors.noObject:
			portal_logger.debug('DN not found...')
			if my_links:
				portal_logger.debug('... creating')
				attrs['link'] = my_links
				attrs['portal'] = ['cn=domain,cn=portal,cn=univention,%s' % ucr_get('ldap/base')]
				attrs['activated'] = True
				attrs['authRestriction'] = 'anonymous'
				try:
					create_object_if_not_exists('settings/portal_entry', lo, pos, **attrs)
				except udm_errors.insufficientInformation as exc:
					portal_logger.info('Cannot create: %s' % exc)
			continue
		links = obj['link']
		portal_logger.debug('Existing links: %r' % links)
		links = [_link for _link in links if urlsplit(_link).hostname not in local_hosts]
		links.extend(my_links)
		portal_logger.debug('New links: %r' % links)
		if not links:
			portal_logger.debug('Removing DN')
			remove_object_if_exists('settings/portal_entry', lo, pos, dn)
		else:
			portal_logger.debug('Modifying DN')
			attrs['link'] = links
			modify_object('settings/portal_entry', lo, pos, dn, **attrs)