-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap.lua
322 lines (273 loc) · 6.99 KB
/
ldap.lua
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
local socket = require("socket")
--------------------------------------------------------------------------------
local function printf(...)
io.write(string.format(...))
end
local function hexdump(payload)
local t = { string.byte(payload, 1, #payload) }
for k, v in ipairs(t) do
printf("%.2x ", v)
if (k % 16) == 0 then
io.write("\n")
elseif (k % 8) == 0 then
io.write(" ")
end
end
print()
end
--------------------------------------------------------------------------------
local response_codes = {
[0] = "success",
[1] = "operationsError",
[2] = "protocolError",
[3] = "timeLimitExceeded",
[4] = "sizeLimitExceeded",
[5] = "compareFalse",
[6] = "compareTrue",
[7] = "authMethodNotSupported",
[8] = "strongerAuthRequired",
-- 9 reserved --
[10] = "referral",
[11] = "adminLimitExceeded",
[12] = "unavailableCriticalExtension",
[13] = "confidentialityRequired",
[14] = "saslBindInProgress",
-- 15 ??? --
[16] = "noSuchAttribute",
[17] = "undefinedAttributeType",
[18] = "inappropriateMatching",
[19] = "constraintViolation",
[20] = "attributeOrValueExists",
[21] = "invalidAttributeSyntax",
-- 22-31 unused --
[32] = "noSuchObject",
[33] = "aliasProblem",
[34] = "invalidDNSyntax",
-- 35 reserved for undefined isLeaf --
[36] = "aliasDereferencingProblem",
-- 37-47 unused --
[48] = "inappropriateAuthentication",
[49] = "invalidCredentials",
[50] = "insufficientAccessRights",
[51] = "busy",
[52] = "unavailable",
[53] = "unwillingToPerform",
[54] = "loopDetect",
-- 55-63 unused --
[64] = "namingViolation",
[65] = "objectClassViolation",
[66] = "notAllowedOnNonLeaf",
[67] = "notAllowedOnRDN",
[68] = "entryAlreadyExists",
[69] = "objectClassModsProhibited",
-- 70 reserved for CLDAP --
[71] = "affectsMultipleDSAs",
-- 72-79 unused --
[80] = "other",
}
--------------------------------------------------------------------------------
local meta = {}
function meta:peek()
return string.byte(self._str, self._i, self._i)
end
function meta:byte()
local b = string.byte(self._str, self._i, self._i)
self._i = self._i + 1
return b
end
function meta:string(n)
local b = self:byte()
local n = self:size()
if n == 0 then
return nil
end
local str = string.sub(self._str, self._i, self._i+n-1)
self._i = self._i + n
return str
end
function meta:integer()
local b = self:byte()
local n = self:byte()
-- null?
local v = string.unpack(">i"..n, self._str, self._i)
self._i = self._i + n
return v
end
function meta:enum()
return self:integer()
end
function meta:boolean()
local b = self:byte()
local n = self:byte()
return (self:byte() == 0xff)
end
function meta:size()
local size = self:byte()
if (size & 0x80) == 0 then
return size
end
local n = size & 0x7F
if n > 4 then
error("size not supported")
end
size = string.unpack(">i"..n, self._str, self._i)
self._i = self._i + n
return size
end
function newbuffer(str)
local buf = {
_str = str,
_i = 1,
_n = #str,
}
return setmetatable(buf, { __index = meta })
end
--------------------------------------------------------------------------------
local function next_byte(s)
local b, err = s:receive(1)
if err then
error(err)
end
return string.byte(b)
end
--------------------------------------------------------------------------------
local function next_size(s)
local size = next_byte(s)
if (size & 0x80) == 0 then
return size
end
size = size & 0x7F
if size > 4 then
error("size not supported")
end
local n, err = s:receive(size)
if err then
error(err)
end
local fmt = ">i" .. size
return string.unpack(fmt, n)
end
--------------------------------------------------------------------------------
local function packet_recv(s)
local code = next_byte(s)
assert(code == 0x30, "invalid code")
local size = next_size(s)
local payload, err = s:receive(size)
if err then
error(err)
end
return newbuffer(payload)
end
--------------------------------------------------------------------------------
local function next_id(self)
local id = self._id
self._id = self._id + 1
if self._id == 2147483647 then
self._id = 1
end
return id
end
--------------------------------------------------------------------------------
local function bind_response(sock, sid)
local buf = packet_recv(sock)
-- Message ID
local id = buf:integer()
assert(id == sid, "invalid message ID")
-- Bind response: 0x61
local code = buf:byte()
assert(code == 0x61, "invalid code")
-- Message size
local size = buf:size()
-- Enumerate
local enum = buf:enum()
-- Extra
local dn = buf:string()
local msg = buf:string()
if enum == 0 then
return true
end
return false, (response_codes[enum] or "unknown")
end
--------------------------------------------------------------------------------
local function bind(self, name, pwd)
if type(name) ~= "string" or type(pwd) ~= "string" then
return nil, "invalid parameters"
end
local msg
local id = self:next_id()
msg = string.pack(">BBs4", 0x80, 0x84, pwd)
msg = string.pack(">BBs4", 0x04, 0x84, name) .. msg
-- version 3
msg = string.pack(">BBB", 0x02, 0x01, 0x03) .. msg
-- bind
msg = string.pack(">BBs4", 0x60, 0x84, msg)
-- message id
msg = string.pack(">BBi4", 0x02, 0x04, id) .. msg
-- package
msg = string.pack(">BBs4", 0x30, 0x84, msg)
self._sock:send(msg)
local succ, resp, msg = pcall(bind_response, self._sock, id)
if not succ then
return false, resp
end
return resp, msg
end
--------------------------------------------------------------------------------
local function unbind(self)
local msg
local id = self:next_id()
-- unbind
msg = string.pack(">BB", 0x42, 0x00)
-- message id
msg = string.pack(">BBi4", 0x02, 0x04, id) .. msg
-- package
msg = string.pack(">BBs4", 0x30, 0x84, msg)
self._sock:send(msg)
end
--------------------------------------------------------------------------------
local operations = {
__index = {
bind = bind,
unbind = unbind,
next_id = next_id
}
}
--------------------------------------------------------------------------------
local function parse_server(server)
local host, port = string.match(server, "^([^:]+):(d+)$")
if host then
return host, port
end
host, port = string.match(server, "^([^:]+):?$")
if host then
return host, 389
end
return nil, nil
end
--------------------------------------------------------------------------------
local function open_simple(server, who, pwd)
local host, port = parse_server(server)
if not host then
return nil, "invalid server"
end
local s = socket.tcp()
local succ, msg = s:connect(host, port)
if not succ then
return nil, msg
end
s:setoption('tcp-nodelay', true)
s:setoption('keepalive', true)
local ld = {
_sock = s,
_id = 1
}
ld = setmetatable(ld, operations)
succ, msg = ld:bind(who, pwd)
if not succ then
return nil, msg
end
return ld
end
return {
open_simple = open_simple
}