Commit 3afb367149c91fd56df47d066ab21c2963f62ccc

Authored by Georg Hopp
1 parent 5977b250

handle binary data

... ... @@ -3,12 +3,16 @@
3 3 import time
4 4 import random
5 5 import mmap
6   -import sys, getopt
7   -from struct import pack
8   -from collections import deque
9 6
10 7 from os.path import dirname, realpath
  8 +import sys, getopt
  9 +reload(sys)
  10 +from sys import path, setdefaultencoding
11 11 path.append(dirname(realpath(__file__)) + '/lib')
  12 +setdefaultencoding('utf-8')
  13 +
  14 +from struct import pack
  15 +from collections import deque
12 16
13 17 from Server import Server
14 18
... ... @@ -136,7 +140,7 @@ def main():
136 140
137 141 server = Server(
138 142 Application(
139   - args[0], int(args[1], hosturi, binddn, basedn, password))
  143 + args[0], int(args[1]), hosturi, binddn, basedn, password))
140 144 server.bindTcp(args[0], int(args[1]), Http())
141 145 server.start(1.0)
142 146
... ...
... ... @@ -7,9 +7,9 @@ from struct import pack
7 7 from collections import deque
8 8
9 9 from os.path import dirname, realpath
10   -import sys
  10 +import sys, getopt
11 11 reload(sys)
12   -from sys import argv, path, setdefaultencoding
  12 +from sys import path, setdefaultencoding
13 13 path.append(dirname(realpath(__file__)) + '/lib')
14 14 setdefaultencoding('utf-8')
15 15 import re
... ... @@ -44,7 +44,12 @@ class Application(EventHandler):
44 44
45 45 @property
46 46 def _body(self):
47   - return self._template.render(ldaptree=self._ldaptree).encode('utf8')
  47 + try:
  48 + return self._template.render(ldaptree=self._ldaptree).encode('utf8')
  49 + except UnicodeDecodeError as e:
  50 + print e.object
  51 + raise TypeError('failed')
  52 +
48 53
49 54 def _handle_data(self, event):
50 55 protocol = event.subject.getProtocol()
... ... @@ -114,7 +119,7 @@ def main():
114 119
115 120 server = Server(
116 121 Application(
117   - args[0], int(args[1], hosturi, binddn, basedn, password))
  122 + args[0], int(args[1]), hosturi, binddn, basedn, password))
118 123 server.bindTcp(args[0], int(args[1]), Http())
119 124 server.start(1.0)
120 125
... ...
... ... @@ -73,16 +73,20 @@ def main():
73 73
74 74 info = LdapTree(hosturi, binddn, basedn, password, use_gssapi)
75 75
76   - if not creategraph:
77   - if outfile:
78   - info.text(outfile)
79   - else:
80   - print info.text()
81   - else:
82   - if outfile:
83   - info.graph(outfile)
  76 + try:
  77 + if not creategraph:
  78 + if outfile:
  79 + info.text(outfile)
  80 + else:
  81 + print info.text()
84 82 else:
85   - print info.graph()
  83 + if outfile:
  84 + info.graph(outfile)
  85 + else:
  86 + print info.graph()
  87 + except UnicodeDecodeError as e:
  88 + print e.object
  89 + raise TypeError('failed')
86 90
87 91 if __name__ == "__main__":
88 92 main()
... ...
... ... @@ -92,13 +92,36 @@ class LdapTree(object):
92 92
93 93 return thislen
94 94
  95 + def _encode(self, data):
  96 + if type(data) is str:
  97 + try:
  98 + unicode(data, 'utf-8')
  99 + except UnicodeDecodeError:
  100 + data = data.encode('base64')
  101 + return data
  102 +
95 103 @property
96 104 def all(self):
97 105 if self._data == None:
98 106 self._data = {}
99 107 result = self._ldap.search_s(self._basedn, ldap.SCOPE_SUBTREE)
100 108 for entry in result:
101   - self._data[entry[0]] = entry[1:][0]
  109 + if entry[1] is None:
  110 + self._data[entry[0]] = None
  111 + elif type(entry[1]) is str:
  112 + self._data[entry[0]] = self._encode(entry[1])
  113 + elif type(entry[1]) is list:
  114 + self._data[entry[0]] = [self._encode(v) for v in entry[1]]
  115 + elif type(entry[1]) is dict:
  116 + self._data[entry[0]] = {}
  117 + for k in entry[1].keys():
  118 + if type(entry[1][k]) is str:
  119 + self._data[entry[0]][k] = self._encode(entry[1])
  120 + else:
  121 + self._data[entry[0]][k] = [
  122 + self._encode(v) for v in entry[1][k]]
  123 + else:
  124 + raise TypeError("unsupported ldap type")
102 125
103 126 return self._data
104 127
... ... @@ -107,12 +130,13 @@ class LdapTree(object):
107 130 retval = {}
108 131 for d in self.all.keys():
109 132 current = retval
110   - for k in reversed(d.split(',')):
111   - try:
112   - current = current[k]
113   - except:
114   - current[k] = {}
115   - current = current[k]
  133 + if d:
  134 + for k in reversed(d.split(',')):
  135 + try:
  136 + current = current[k]
  137 + except:
  138 + current[k] = {}
  139 + current = current[k]
116 140 return retval
117 141
118 142 @property
... ...
... ... @@ -72,6 +72,9 @@
72 72 onclick="toggle(this, 'childs')">dn: {{ d[2]|e }}</span>
73 73 <button onclick="toggle(this, 'attributes')">[Attributes]</button>
74 74 <ul class="attributes">
  75 + {% if ldaptree.node(d[2]) is string -%}
  76 + <li>{{ ldaptree.node(d[2])|e }}</li>
  77 + {% else -%}
75 78 {% for k in ldaptree.node(d[2]).keys() -%}
76 79 {% if ldaptree.node(d[2]) is string -%}
77 80 <li>{{ k }}: {{ ldaptree.node(d[2])[k]|e }}</li>
... ... @@ -81,6 +84,7 @@
81 84 {% endfor -%}
82 85 {% endif -%}
83 86 {% endfor -%}
  87 + {% endif -%}
84 88 </ul>
85 89 {% endfor -%}
86 90 </ul>
... ...
Please register or login to post a comment