LdapService2.py
4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/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: