Some little python code to visualize an LDAP structure.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

128 lines
4.0 KiB

#!/usr/bin/python
import time
import random
import mmap
from struct import pack
from collections import deque
from os.path import dirname, realpath
import sys, getopt
reload(sys)
from sys import path, setdefaultencoding
path.append(dirname(realpath(__file__)) + '/lib')
setdefaultencoding('utf-8')
import re
from Server import Server
from Event.EventHandler import EventHandler
from Event.EventDispatcher import EventDispatcher
from Communication.EndPoint import CommunicationEndPoint
from Protocol.Http.Http import Http
from Protocol.Websocket.Websocket import Websocket
from jinja2 import Environment, FileSystemLoader
from LdapTree import LdapTree
class Application(EventHandler):
def __init__(self, ip, port, hosturi, binddn, basedn, password):
super(Application, self).__init__()
self._event_methods = {
CommunicationEndPoint.eventId('new_msg') : self._handle_data,
}
self._ldaptree = LdapTree(hosturi, binddn, basedn, password, False)
env = Environment(loader=FileSystemLoader(
dirname(realpath(__file__)) + '/templates'))
self._template = env.get_template('simple.html.j2')
random.seed()
@property
def _body(self):
try:
return self._template.render(ldaptree=self._ldaptree).encode('utf8')
except UnicodeDecodeError as e:
print e.object
raise TypeError('failed')
def _handle_data(self, event):
protocol = event.subject.getProtocol()
if event.subject.hasProtocol(Http):
if event.data.isRequest():
if event.data.getUri() == '/':
resp = protocol.createResponse(event.data, 200, 'OK')
resp.setBody(self._body)
else:
resp = protocol.createResponse(event.data, 404, 'Not Found')
resp.setBody('<h1>404 - Not Found</h1>')
self.issueEvent(event.subject, 'send_msg', resp)
return True
def usage():
print "Usage: " + sys.argv[0] + " ARGUMENT... [OPTIONS]... bindip bindport\n"
print "Start a webserver on the given bindip and bindport. On the page a"
print "tree representation of all DNs starting with a given base DN is"
print "visualized."
print "Only simple binds to the directory with DN and password are supported.\n"
print "ARGUMENTS:\n"
print " {:30s} : {:s}".format('-H, --hosturi=URI', 'The URI to the ldap server to query in the form:')
print " {:30s} {:s}".format('', 'ldap[s]://host.uri[:port]')
print " {:30s} : {:s}".format('-D, --binddn=DN', 'The DN to use for the LDAP bind.')
print " {:30s} : {:s}".format('-p, --password=PASSWORD', 'The password to use for the LDAP bind.')
print " {:30s} : {:s}\n".format('-b, --basedn=DN', 'The DN to start the tree with.')
print "OPTIONS:\n"
print " {:30s} : {:s}".format('-h, --help', 'Show this help page')
def main():
try:
opts, args = getopt.getopt(
sys.argv[1:],
'hH:D:b:p:',
['help', 'hosturi=', 'binddn=', 'basedn=', 'password='])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
hosturi = binddn = basedn = password = None
for o, a in opts:
if o in ["-h", "--help"]:
usage()
sys.exit(0)
elif o in ["-H", "--hosturi"]:
hosturi = a
elif o in ["-D", "--binddn"]:
binddn = a
elif o in ["-b", "--basedn"]:
basedn = a
elif o in ["-p", "--password"]:
password = a
else:
print "unknown parameter: " + a
usage()
sys.exit(2)
if not hosturi or not binddn or not basedn or not password:
usage()
sys.exit(2)
server = Server(
Application(
args[0], int(args[1]), hosturi, binddn, basedn, password))
server.bindTcp(args[0], int(args[1]), Http())
server.start(1.0)
if __name__ == '__main__':
main()
# vim: set ft=python et ts=8 sw=4 sts=4: