def update_dnsRecord(self): """ update the clienthost's IPv6 and IPv4 records in the DNS-ldap-backend ;) got modify example from: http://www.packtpub.com/article/python-ldap-applications-more-ldap-operations-and-the-ldap-url-library thanks. """ logging.debug("writing AAAA, lastSeen and A record for%s back to DNS.", self.userID) ip = func.get_local_ip() ipv6 = func.get_local_ipv6() time = nowstr() if not ip: logging.error('Host has no ip') return False if not ipv6: #logging.warning('Host has no ipv6.') #return False logging.info('Host has no ipv6') if not time: logging.error('Host has no clock') return False #removed till ipv6 is deployed. #mod_attr = [( ldap.MOD_REPLACE, 'aAAARecord', ipv6 ), mod_attr = [( ldap.MOD_REPLACE, 'aRecord', ip), ( ldap.MOD_REPLACE, 'lastSeen', time )] try: return self.conn.modify_s(self.userID, mod_attr) except ldap.INSUFFICIENT_ACCESS: logging.critical("%s HAS INSUFFICIENT_ACCESS (write) to ldap directory server, please contact the URZ about this issue !", self.userID ) return False logging.info('updated DNS record for %s', self.userID) return True
def add_host(self, hostname=None, target=None, classes=None, **args): """ @see UniDomain.Classes.db#add_host """ # some prechecks if not classes: classes = [] if not target: target = self.home[0] if not hostname: hostname = func.getlocalhostname() shortname = hostname.split('.')[0] if len(self.home) == 0: logging.warning( '%s does not have enough rights to add hosts to the database', self.userID) return False # setup host object host_dn = 'cn=%s,%s' % (shortname, target ) # we use first home dn as target container #FIXME: I dont like this krb5-dependancy... host_usid = 'host/%s@%s' % (hostname, self.config.krb5realm) logging.debug('using %s as usid', host_usid) if len(self.list_hosts(hostname)) > 0: logging.warning('Host %s already exists. Not changing.', hostname) return True logging.debug('Looks good. Adding %s to %s', hostname, target) host_data = [ ('cn', shortname), ('udSerial', '%i' % self.next_udSerial()), ('lastSeen', nowstr()), ('objectClass', ['top', 'dNSZone', 'udHost']), ('relativeDomainName', shortname), ('zoneName', self.config.dnszone), ('dNSTTL', '3600'), ('dNSClass', 'IN'), ('ARecord', func.get_local_ip()), ('FQDN', hostname), ('USID', host_usid), ('description', 'new registered host object'), ] ipv6 = func.get_local_ipv6() if ipv6: host_data.append(('aAAARecord', ipv6)) if len(classes) > 0: host_data.append(('udGroup', classes)) try: self.conn.result(self.conn.add(host_dn, host_data)) # add policies queries = [ self.conn.add( 'cn=%s,%s' % (policy, host_dn), [('objectClass', ['top', 'udPolicy']), args[policy]]) for policy in args ] [self.conn.result(query) for query in queries] except Exception, err: logging.warning('add_host(): Trouble adding to ldap.\n%s', str(err)) return False
def add_host(self, hostname = None, target = None, classes = None, **args): """ @see UniDomain.Classes.db#add_host """ # some prechecks if not classes: classes = [] if not target: target = self.home[0] if not hostname: hostname = func.getlocalhostname() shortname = hostname.split('.')[0] if len(self.home) == 0: logging.warning('%s does not have enough rights to add hosts to the database', self.userID) return False # setup host object host_dn = 'cn=%s,%s' % (shortname, target) # we use first home dn as target container #FIXME: I dont like this krb5-dependancy... host_usid = 'host/%s@%s' % (hostname, self.config.krb5realm) logging.debug('using %s as usid', host_usid) if len(self.list_hosts(hostname)) > 0: logging.warning('Host %s already exists. Not changing.', hostname) return True logging.debug('Looks good. Adding %s to %s', hostname, target) host_data = [ ('cn', shortname), ('udSerial', '%i' % self.next_udSerial()), ('lastSeen', nowstr()), ('objectClass', ['top', 'dNSZone', 'udHost']), ('relativeDomainName', shortname), ('zoneName', self.config.dnszone), ('dNSTTL', '3600'), ('dNSClass', 'IN'), ('ARecord', func.get_local_ip()), ('FQDN', hostname), ('USID', host_usid), ('description', 'new registered host object'), ] ipv6 = func.get_local_ipv6() if ipv6: host_data.append(('aAAARecord', ipv6)) if len(classes) > 0: host_data.append(('udGroup', classes)) try: self.conn.result(self.conn.add(host_dn, host_data)) # add policies queries = [ self.conn.add( 'cn=%s,%s' % (policy, host_dn), [('objectClass', ['top','udPolicy']),args[policy]] ) for policy in args ] [ self.conn.result(query) for query in queries ] except Exception, err: logging.warning('add_host(): Trouble adding to ldap.\n%s', str(err)) return False
def test_host_reg(self): """ check if we can add and delete hosts.""" nextSerial = self.db.next_udSerial() self.assertTrue(nextSerial, 'cant get a free udSerial') self.assertFalse(self.db.add_host('xyz_NotExistingBadWordFuck'), '!!!!I addwd this host to a nonexistent domain....run check ldap-server before someone important sees it.') self.assertFalse(self.db.add_host('spahantest'), 'adding this host again succeeded.') myname, mydomain = socket.getfqdn().split('.',1) try: funcs.set_newHostname('sp.' + mydomain) self.assertFalse(self.db.add_host('spahantest'), 'adding short host suceeded') funcs.set_newHostname('spahan01.' + mydomain) self.assertTrue(self.db.add_host('spahantest'), 'adding new host to domain failed.') host_id = self.db.get_itemID() self.assertTrue(host_id, 'hostreg: cant get host_ID of newly created host.') host_dn,hostatts = self.db.conn.search_s(host_id, ldap.SCOPE_BASE)[0] self.assertEqual(hostatts['FQDN'], ['spahan01.' + mydomain], 'hostreg: Bad FQDN value') self.assertEqual(hostatts['cn'], ['spahan01'], 'hostreg: Bad cn value') self.assertEqual(hostatts['udSerial'], [str(nextSerial)], 'hostreg: Bad udSerial value') self.assertTrue('lastSeen' in hostatts, 'hostreg: missing lastSeen value.') self.assertEqual(sorted(hostatts['objectClass']), sorted(['top', 'dNSZone', 'udHost']), 'hostreg: Bad objectClass Attribute values.') self.assertEqual(hostatts['relativeDomainName'], ['spahan01'], 'hostreg: Bad relativeDomainName value') self.assertEqual(hostatts['zoneName'], ['ud.unibas.ch'], 'hostreg: Bad zoneName value') self.assertEqual(hostatts['ARecord'], [funcs.get_local_ip()], 'hostreg: Wrong ARecord value') self.assertEqual(hostatts['description'], ['new registered host object'], 'wrong default description') self.assertEqual(hostatts['dNSClass'], ['IN'], 'hostreg: wrong DNSClass') self.assertEqual(hostatts['dNSTTL'], ['3600'], 'hostreg: wrong dns-ttl value') self.assertEqual(hostatts['USID'], ['host/spahan01.%[email protected]' % (mydomain)], 'hostreg: wrong USID value') self.assertEqual(self.db.next_udSerial(), nextSerial + 1, 'next udSerial is not next serial.') self.assertFalse(self.db.delete_host('spahan02.' + mydomain), 'delete fo inexistent host suceeded.') self.assertFalse(self.db.delete_host('spahan01.' + mydomain, 'spahannotExstenta'), 'delete fo host in inexistent domain suceeded.') self.assertTrue(self.db.delete_host('spahan01.' + mydomain), 'delete of host failed.') self.assertRaises(ldap.NO_SUCH_OBJECT, self.db.conn.search_s, host_id, ldap.SCOPE_BASE) self.assertEqual(self.db.next_udSerial(), nextSerial, 'next udSerial does not reset after delete of hostItem') funcs.set_newHostname('%s.%s' % (myname,mydomain)) except: #FIXME: doesnt work? funcs.set_newHostname('%s.%s' % (myname,mydomain)) raise
def update_dnsRecord(self): """ update the clienthost's IPv6 and IPv4 records in the DNS-ldap-backend ;) got modify example from: http://www.packtpub.com/article/python-ldap-applications-more-ldap-operations-and-the-ldap-url-library thanks. """ logging.debug("writing AAAA, lastSeen and A record for%s back to DNS.", self.userID) ip = func.get_local_ip() ipv6 = func.get_local_ipv6() time = nowstr() if not ip: logging.error('Host has no ip') return False if not ipv6: #logging.warning('Host has no ipv6.') #return False logging.info('Host has no ipv6') if not time: logging.error('Host has no clock') return False #removed till ipv6 is deployed. #mod_attr = [( ldap.MOD_REPLACE, 'aAAARecord', ipv6 ), mod_attr = [(ldap.MOD_REPLACE, 'aRecord', ip), (ldap.MOD_REPLACE, 'lastSeen', time)] try: return self.conn.modify_s(self.userID, mod_attr) except ldap.INSUFFICIENT_ACCESS: logging.critical( "%s HAS INSUFFICIENT_ACCESS (write) to ldap directory server, please contact the URZ about this issue !", self.userID) return False logging.info('updated DNS record for %s', self.userID) return True
def test_host_reg(self): """ check if we can add and delete hosts.""" nextSerial = self.db.next_udSerial() self.assertTrue(nextSerial, 'cant get a free udSerial') self.assertFalse( self.db.add_host('xyz_NotExistingBadWordFuck'), '!!!!I addwd this host to a nonexistent domain....run check ldap-server before someone important sees it.' ) self.assertFalse(self.db.add_host('spahantest'), 'adding this host again succeeded.') myname, mydomain = socket.getfqdn().split('.', 1) try: funcs.set_newHostname('sp.' + mydomain) self.assertFalse(self.db.add_host('spahantest'), 'adding short host suceeded') funcs.set_newHostname('spahan01.' + mydomain) self.assertTrue(self.db.add_host('spahantest'), 'adding new host to domain failed.') host_id = self.db.get_itemID() self.assertTrue( host_id, 'hostreg: cant get host_ID of newly created host.') host_dn, hostatts = self.db.conn.search_s(host_id, ldap.SCOPE_BASE)[0] self.assertEqual(hostatts['FQDN'], ['spahan01.' + mydomain], 'hostreg: Bad FQDN value') self.assertEqual(hostatts['cn'], ['spahan01'], 'hostreg: Bad cn value') self.assertEqual(hostatts['udSerial'], [str(nextSerial)], 'hostreg: Bad udSerial value') self.assertTrue('lastSeen' in hostatts, 'hostreg: missing lastSeen value.') self.assertEqual(sorted(hostatts['objectClass']), sorted(['top', 'dNSZone', 'udHost']), 'hostreg: Bad objectClass Attribute values.') self.assertEqual(hostatts['relativeDomainName'], ['spahan01'], 'hostreg: Bad relativeDomainName value') self.assertEqual(hostatts['zoneName'], ['ud.unibas.ch'], 'hostreg: Bad zoneName value') self.assertEqual(hostatts['ARecord'], [funcs.get_local_ip()], 'hostreg: Wrong ARecord value') self.assertEqual(hostatts['description'], ['new registered host object'], 'wrong default description') self.assertEqual(hostatts['dNSClass'], ['IN'], 'hostreg: wrong DNSClass') self.assertEqual(hostatts['dNSTTL'], ['3600'], 'hostreg: wrong dns-ttl value') self.assertEqual(hostatts['USID'], ['host/spahan01.%[email protected]' % (mydomain)], 'hostreg: wrong USID value') self.assertEqual(self.db.next_udSerial(), nextSerial + 1, 'next udSerial is not next serial.') self.assertFalse(self.db.delete_host('spahan02.' + mydomain), 'delete fo inexistent host suceeded.') self.assertFalse( self.db.delete_host('spahan01.' + mydomain, 'spahannotExstenta'), 'delete fo host in inexistent domain suceeded.') self.assertTrue(self.db.delete_host('spahan01.' + mydomain), 'delete of host failed.') self.assertRaises(ldap.NO_SUCH_OBJECT, self.db.conn.search_s, host_id, ldap.SCOPE_BASE) self.assertEqual( self.db.next_udSerial(), nextSerial, 'next udSerial does not reset after delete of hostItem') funcs.set_newHostname('%s.%s' % (myname, mydomain)) except: #FIXME: doesnt work? funcs.set_newHostname('%s.%s' % (myname, mydomain)) raise