LdapService.py
4.76 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
import time
import random
import mmap
import sys, getopt
from struct import pack
from collections import deque
from os.path import dirname, realpath
from sys import argv, path
path.append(dirname(realpath(__file__)) + '/lib')
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 LdapTree import LdapTree
class Application(EventHandler):
def __init__(self, hosturi, binddn, basedn, password):
super(Application, self).__init__()
self._event_methods = {
EventDispatcher.eventId('heartbeat') : self._heartbeat,
CommunicationEndPoint.eventId('new_msg') : self._handle_data,
CommunicationEndPoint.eventId('close') : self._handle_close,
CommunicationEndPoint.eventId('upgrade') : self._upgrade
}
self._websockets = []
self._wstest = open('websocket.html', 'r+b')
self._wstestmm = mmap.mmap(self._wstest.fileno(), 0)
random.seed()
self.ldaptree = LdapTree(hosturi, binddn, basedn, password, False)
def __del__(self):
self._wstestmm.close()
self._wstest.close()
def _upgrade(self, event):
self._websockets.append(event.subject)
# let other also handle the upgrade .. no return True
def _heartbeat(self, event):
now = pack('!d', time.time())
for event.subject in self._websockets:
self.issueEvent(event.subject, 'send_msg', now)
return True
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._wstestmm[0:])
elif event.data.getUri() == '/ldap':
resp = protocol.createResponse(event.data, 200, 'OK')
resp.setHeader('Content-Type', 'image/svg+xml')
resp.setBody(self.ldaptree.graph())
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 _handle_close(self, event):
if event.subject in self._websockets:
print 'websocket closed...'
self._websockets = [w for w in self._websockets if w!=event.subject]
return True
def usage():
print "Usage: " + sys.argv[0] + " -[HDbhpk] bindip bindport\n"
print "Create a tree representation of all DNs starting with a given base DN."
print "Only simple binds to the directory with DN and password are supported."
print "If no password OPTION is given the password will be asked interactive."
print "If no outfile the given the result will be written to stdout.\n"
print "Required OPTIONS are:\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 "Optional OPTIONS are:\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(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: