#!/usr/bin/python
import sys
import ldap

controls = {
'1.2.840.113556.1.4.473': 'Server side sort request',
'1.3.6.1.4.1.1466.29539.12': 'Chaining loop detection',
'1.3.6.1.4.1.42.2.27.8.5.1': 'Password policy request/response',
'1.3.6.1.4.1.42.2.27.9.5.2': 'Get effective rights',
'2.16.840.1.113730.3.4.12': 'Proxied authorization (version 1)',
'2.16.840.1.113730.3.4.13': 'Replication update information',
'2.16.840.1.113730.3.4.14': 'Search on specific backend',
'2.16.840.1.113730.3.4.15': 'Authentication response',
'2.16.840.1.113730.3.4.16': 'Authentication request',
'2.16.840.1.113730.3.4.17': 'Real attributes only',
'2.16.840.1.113730.3.4.18': 'Proxied authorization (version 2)',
'2.16.840.1.113730.3.4.19': 'Virtual attributes only',
'2.16.840.1.113730.3.4.2': 'Manage DSA IT',
'2.16.840.1.113730.3.4.20': 'Search on one backend',
'2.16.840.1.113730.3.4.3': 'Persistent search',
'2.16.840.1.113730.3.4.4': 'Password expired',
'2.16.840.1.113730.3.4.5': 'Password expiring',
'2.16.840.1.113730.3.4.6': 'NT synchcronization client',
'2.16.840.1.113730.3.4.7': 'Entry change notification',
'2.16.840.1.113730.3.4.9': 'VLV request',
'2.16.840.1.113730.3.4.10': 'VLV response',
'2.16.840.1.113730.3.4.11': 'Transaction ID response',
'2.16.840.1.113730.3.4.999': 'Replication modrdn extra mods'
}

extensions = {
'1.3.6.1.4.1.4203.1.11.1': 'Modify password',
'2.16.840.1.113730.3.5.1': 'Transaction request',
'2.16.840.1.113730.3.5.2': 'Transaction response',
'2.16.840.1.113730.3.5.3': 'Start replication request',
'2.16.840.1.113730.3.5.4': 'Replication response',
'2.16.840.1.113730.3.5.5': 'End replication request',
'2.16.840.1.113730.3.5.6': 'Replication entry request',
'2.16.840.1.113730.3.5.7': 'Bulk import start',
'2.16.840.1.113730.3.5.8': 'Bulk import finished',
'2.16.840.1.113730.3.5.9': 'Digest authentication calculation'
}

def translate_value(attr, value):
	ret = value
	a = attr.lower()

	if 'supportedcontrol' == a:
		if controls.has_key(value):
			ret = controls[value] + " (" + value + ")"
        if 'supportedextension' == a:
                if extensions.has_key(value):
                        ret = extensions[value] + " (" + value + ")"
	return ret

def parse_entry(entry): 
	dn, attrs = entry
	out = []

        for attr in attrs:
        	for value in attrs[attr]:
                	line = attr + ': ' + translate_value(attr, value)
			out.append(line)

	out.sort()
	return out


try:
	host = sys.argv[1]
except IndexError:
	host = ""

try:
	l = ldap.open(host)
	l.protocol_version = ldap.VERSION3

	id = l.search("", ldap.SCOPE_BASE)

	while 1:
		msg_type, data = l.result(id, 0)
		if (data == []):
			break
		else:
			if msg_type == ldap.RES_SEARCH_ENTRY:
				out = parse_entry(data[0])
				for line in out:
					print line
except ldap.LDAPError, e:
	print e
