10 changed files with 321 additions and 35 deletions
-
25LdapService.py
-
123LdapService2.py
-
2ldaptree.py
-
2lib/Communication/Connection.py
-
92lib/LdapTree.py
-
6lib/Transport/TcpSocket.py
-
89templates/simple.html.j2
-
12templates/simple.txt.j2
-
5templates/websocket.html.j2
-
BINtests/.TestAll.py.swp
@ -0,0 +1,123 @@ |
|||||
|
#!/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 |
||||
|
reload(sys) |
||||
|
from sys import argv, 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): |
||||
|
return self._template.render(ldaptree=self._ldaptree).encode('utf8') |
||||
|
|
||||
|
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: |
||||
@ -0,0 +1,89 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html> |
||||
|
<head> |
||||
|
<meta charset="utf-8"/> |
||||
|
<title>Ldap</title> |
||||
|
<style type="text/css"> |
||||
|
ul { |
||||
|
list-style-type: none; |
||||
|
} |
||||
|
ul.attributes { |
||||
|
border: 1px solid black; |
||||
|
font-weight: normal; |
||||
|
display: none; |
||||
|
padding: 5px; |
||||
|
margin: 0; |
||||
|
} |
||||
|
ul.childs { |
||||
|
display: none; |
||||
|
} |
||||
|
button { |
||||
|
cursor: pointer; |
||||
|
background-color: rgba(0,0,0,0); |
||||
|
border: none; |
||||
|
color: black; |
||||
|
padding: 2px; |
||||
|
text-align: center; |
||||
|
text-decoration: none; |
||||
|
display: inline-block; |
||||
|
font-size: 16px; |
||||
|
} |
||||
|
span.linked { |
||||
|
font-weight: bold; |
||||
|
cursor: pointer; |
||||
|
} |
||||
|
</style> |
||||
|
</head> |
||||
|
|
||||
|
<body> |
||||
|
<h1>Ldap</h1> |
||||
|
|
||||
|
<script language="Javascript"> |
||||
|
function toggle(e, cls) { |
||||
|
attr = e.parentElement.getElementsByClassName(cls)[0]; |
||||
|
if(attr.style.display == 'block') |
||||
|
attr.style.display = 'none'; |
||||
|
else |
||||
|
attr.style.display = 'block'; |
||||
|
} |
||||
|
</script> |
||||
|
|
||||
|
<ul class="tree"> |
||||
|
{% set depth=0 -%} |
||||
|
{% for d in ldaptree.hirarchy -%} |
||||
|
{% if depth>=d[0] and depth!=0 -%} |
||||
|
<ul class="childs" {% if depth < 3 -%}style="display: block;"{% endif -%}> |
||||
|
</ul> |
||||
|
</li> |
||||
|
{% endif -%} |
||||
|
{% if depth < d[0] -%} |
||||
|
{% set depth=d[0] -%} |
||||
|
<ul class="childs" {% if depth < 3 -%}style="display: block;"{% endif -%}> |
||||
|
{% endif -%} |
||||
|
{% if depth > d[0] -%} |
||||
|
{% for i in range(depth-d[0]) -%} |
||||
|
</ul> |
||||
|
</li> |
||||
|
{% endfor -%} |
||||
|
{% set depth=d[0] -%} |
||||
|
{% endif -%} |
||||
|
<li> |
||||
|
<span {% if ldaptree.childs(d[2]) -%}class="linked"{% endif -%} |
||||
|
onclick="toggle(this, 'childs')">dn: {{ d[2]|e }}</span> |
||||
|
<button onclick="toggle(this, 'attributes')">[Attributes]</button> |
||||
|
<ul class="attributes"> |
||||
|
{% for k in ldaptree.node(d[2]).keys() -%} |
||||
|
{% if ldaptree.node(d[2]) is string -%} |
||||
|
<li>{{ k }}: {{ ldaptree.node(d[2])[k]|e }}</li> |
||||
|
{% else -%} |
||||
|
{% for v in ldaptree.node(d[2])[k] -%} |
||||
|
<li>{{ k }}: {{ v|e }}</li> |
||||
|
{% endfor -%} |
||||
|
{% endif -%} |
||||
|
{% endfor -%} |
||||
|
</ul> |
||||
|
{% endfor -%} |
||||
|
</ul> |
||||
|
</body> |
||||
|
</html> |
||||
|
<!-- vim: set et ts=2 sw=2: --> |
||||
@ -0,0 +1,12 @@ |
|||||
|
{% for d in ldaptree.hirarchy -%} |
||||
|
{{ '--'*d[0] }} dn: {{ d[2] }} |
||||
|
{% for k in ldaptree.node(d[2]).keys() -%} |
||||
|
{% if ldaptree.node(d[2]) is string -%} |
||||
|
{{ ' '*d[0] }} {{ k }}: {{ ldaptree.node(d[2])[k] }} |
||||
|
{% else -%} |
||||
|
{% for v in ldaptree.node(d[2])[k] -%} |
||||
|
{{ ' '*d[0] }} {{ k }}: {{ v }} |
||||
|
{% endfor -%} |
||||
|
{% endif -%} |
||||
|
{% endfor -%} |
||||
|
{% endfor -%} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue