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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
|
/*
Unix SMB/CIFS implementation.
LDAP server
Copyright (C) Volker Lendecke 2004
Copyright (C) Stefan Metzmacher 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
/*
close the socket and shutdown a server_context
*/
static void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, const char *reason)
{
server_terminate_connection(ldap_conn->connection, reason);
}
/*
add a socket address to the list of events, one event per port
*/
static void add_socket(struct server_service *service,
const struct model_ops *model_ops,
struct in_addr *ifip)
{
struct server_socket *srv_sock;
uint16_t port = 389;
char *ip_str = talloc_strdup(service->mem_ctx, inet_ntoa(*ifip));
srv_sock = service_setup_socket(service, model_ops, ip_str, &port);
talloc_free(ip_str);
}
/****************************************************************************
Open the socket communication.
****************************************************************************/
static void ldapsrv_init(struct server_service *service,
const struct model_ops *model_ops)
{
DEBUG(1,("ldapsrv_init\n"));
if (lp_interfaces() && lp_bind_interfaces_only()) {
int num_interfaces = iface_count();
int i;
/* We have been given an interfaces line, and been
told to only bind to those interfaces. Create a
socket per interface and bind to only these.
*/
for(i = 0; i < num_interfaces; i++) {
struct in_addr *ifip = iface_n_ip(i);
if (ifip == NULL) {
DEBUG(0,("ldapsrv_init: interface %d has NULL "
"IP address !\n", i));
continue;
}
add_socket(service, model_ops, ifip);
}
} else {
struct in_addr *ifip;
TALLOC_CTX *mem_ctx = talloc_init("ldapsrv_init");
if (!mem_ctx) {
smb_panic("No memory");
}
/* Just bind to lp_socket_address() (usually 0.0.0.0) */
ifip = interpret_addr2(mem_ctx, lp_socket_address());
add_socket(service, model_ops, ifip);
talloc_destroy(mem_ctx);
}
}
/* This rw-buf api is made to avoid memcpy. For now do that like mad... The
idea is to write into a circular list of buffers where the ideal case is
that a read(2) holds a complete request that is then thrown away
completely. */
static void consumed_from_buf(struct rw_buffer *buf,
size_t length)
{
memcpy(buf->data, buf->data+length, buf->length-length);
buf->length -= length;
}
static BOOL append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
{
buf->data = realloc(buf->data, buf->length+length);
if (buf->data == NULL)
return False;
memcpy(buf->data+buf->length, data, length);
buf->length += length;
return True;
}
static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
{
NTSTATUS status;
DATA_BLOB tmp_blob;
BOOL ret;
status = socket_recv(sock, sock, &tmp_blob, 1024, 0);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0,("socket_recv: %s\n",nt_errstr(status)));
return False;
}
ret = append_to_buf(buf, tmp_blob.data, tmp_blob.length);
talloc_free(tmp_blob.data);
return ret;
}
static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
{
NTSTATUS status;
DATA_BLOB tmp_blob;
size_t sendlen;
tmp_blob.data = buf->data;
tmp_blob.length = buf->length;
status = socket_send(sock, sock, &tmp_blob, &sendlen, 0);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0,("socket_send() %s\n",nt_errstr(status)));
return False;
}
consumed_from_buf(buf, sendlen);
return True;
}
static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
size_t *out_length)
{
*out = buf->data;
*out_length = buf->length;
}
static BOOL ldap_append_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
{
DATA_BLOB blob;
BOOL res;
if (!ldap_encode(msg, &blob))
return False;
res = append_to_buf(buf, blob.data, blob.length);
data_blob_free(&blob);
return res;
}
static struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, enum ldap_request_tag type)
{
struct ldapsrv_reply *reply;
reply = talloc_p(call, struct ldapsrv_reply);
if (!reply) {
return NULL;
}
reply->prev = reply->next = NULL;
reply->state = LDAPSRV_REPLY_STATE_NEW;
reply->msg.messageid = call->request.messageid;
reply->msg.type = type;
reply->msg.mem_ctx = reply;
return reply;
}
static void ldapsrv_unwilling(struct ldapsrv_call *call, int error)
{
struct ldapsrv_reply *reply;
struct ldap_ExtendedResponse *r;
DEBUG(0,("Unwilling type[%d] id[%d]\n", call->request.type, call->request.messageid));
reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
r = &reply->msg.r.ExtendedResponse;
r->response.resultcode = error;
r->response.dn = NULL;
r->response.errormessage = NULL;
r->response.referral = NULL;
r->name = NULL;
r->value.data = NULL;
r->value.length = 0;
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_BindRequest(struct ldapsrv_call *call)
{
struct ldap_BindRequest *req = &call->request.r.BindRequest;
struct ldapsrv_reply *reply;
struct ldap_BindResponse *resp;
DEBUG(5, ("Binding as %s with pw %s\n",
req->dn, req->creds.password));
reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
resp = &reply->msg.r.BindResponse;
resp->response.resultcode = 0;
resp->response.dn = NULL;
resp->response.errormessage = NULL;
resp->response.referral = NULL;
resp->SASL.secblob = data_blob(NULL, 0);
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_UnbindRequest(struct ldapsrv_call *call)
{
// struct ldap_UnbindRequest *req = &call->request->r.UnbindRequest;
DEBUG(10, ("Unbind\n"));
}
static void ldapsrv_SearchRequest(struct ldapsrv_call *call)
{
struct ldap_SearchRequest *req = &call->request.r.SearchRequest;
struct ldapsrv_reply *reply;
struct ldap_Result *resp;
DEBUG(10, ("Search filter: %s\n", req->filter));
/* Is this a rootdse request? */
if ((strlen(req->basedn) == 0) &&
(req->scope == LDAP_SEARCH_SCOPE_BASE) &&
strequal(req->filter, "(objectclass=*)")) {
}
reply = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
resp = &reply->msg.r.SearchResultDone;
resp->resultcode = 0;
resp->dn = NULL;
resp->errormessage = NULL;
resp->referral = NULL;
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_ModifyRequest(struct ldapsrv_call *call)
{
// struct ldap_ModifyRequest *req = &call->request.r.ModifyRequest;
struct ldapsrv_reply *reply;
DEBUG(10, ("Modify\n"));
reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
ZERO_STRUCT(reply->msg.r);
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_AddRequest(struct ldapsrv_call *call)
{
// struct ldap_AddRequest *req = &call->request.r.AddRequest;
struct ldapsrv_reply *reply;
DEBUG(10, ("Add\n"));
reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
ZERO_STRUCT(reply->msg.r);
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_DelRequest(struct ldapsrv_call *call)
{
// struct ldap_DelRequest *req = &call->request.r.DelRequest;
struct ldapsrv_reply *reply;
DEBUG(10, ("Del\n"));
reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
ZERO_STRUCT(reply->msg.r);
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
{
// struct ldap_ModifyDNRequest *req = &call->request.r.ModifyDNRequest;
struct ldapsrv_reply *reply;
DEBUG(10, ("Modify\n"));
reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
ZERO_STRUCT(reply->msg.r);
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_CompareRequest(struct ldapsrv_call *call)
{
// struct ldap_CompareRequest *req = &call->request.r.CompareRequest;
struct ldapsrv_reply *reply;
DEBUG(10, ("Compare\n"));
reply = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
ZERO_STRUCT(reply->msg.r);
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_AbandonRequest(struct ldapsrv_call *call)
{
// struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;
DEBUG(10, ("Abandon\n"));
}
static void ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
{
// struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;
struct ldapsrv_reply *reply;
DEBUG(10, ("Extended\n"));
reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
if (!reply) {
ldapsrv_terminate_connection(call->conn, "ldapsrv_init_reply() failed");
return;
}
ZERO_STRUCT(reply->msg.r);
DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
}
static void ldapsrv_do_call(struct ldapsrv_call *call)
{
switch(call->request.type) {
case LDAP_TAG_BindRequest:
ldapsrv_BindRequest(call);
break;
case LDAP_TAG_UnbindRequest:
ldapsrv_UnbindRequest(call);
break;
case LDAP_TAG_SearchRequest:
ldapsrv_SearchRequest(call);
break;
case LDAP_TAG_ModifyRequest:
ldapsrv_ModifyRequest(call);
break;
case LDAP_TAG_AddRequest:
ldapsrv_AddRequest(call);
break;
case LDAP_TAG_DelRequest:
ldapsrv_DelRequest(call);
break;
case LDAP_TAG_ModifyDNRequest:
ldapsrv_ModifyDNRequest(call);
break;
case LDAP_TAG_CompareRequest:
ldapsrv_CompareRequest(call);
break;
case LDAP_TAG_AbandonRequest:
ldapsrv_AbandonRequest(call);
break;
case LDAP_TAG_ExtendedRequest:
ldapsrv_ExtendedRequest(call);
break;
default:
ldapsrv_unwilling(call, 2);
break;
}
}
static void ldapsrv_do_responses(struct ldapsrv_connection *conn)
{
struct ldapsrv_call *call, *next_call = NULL;
struct ldapsrv_reply *reply, *next_reply = NULL;
for (call=conn->calls; call; call=next_call) {
for (reply=call->replies; reply; reply=next_reply) {
if (!ldap_append_to_buf(&reply->msg, &conn->out_buffer)) {
ldapsrv_terminate_connection(conn, "append_to_buf() failed");
return;
}
next_reply = reply->next;
DLIST_REMOVE(call->replies, reply);
reply->state = LDAPSRV_REPLY_STATE_SEND;
talloc_free(reply);
}
next_call = call->next;
DLIST_REMOVE(conn->calls, call);
call->state = LDAPSRV_CALL_STATE_COMPLETE;
talloc_free(call);
}
}
/*
called when a LDAP socket becomes readable
*/
static void ldapsrv_recv(struct server_connection *conn, time_t t,
uint16_t flags)
{
struct ldapsrv_connection *ldap_conn = conn->private_data;
uint8_t *buf;
int buf_length, msg_length;
DATA_BLOB blob;
ASN1_DATA data;
struct ldapsrv_call *call;
DEBUG(10,("ldapsrv_recv\n"));
if (!read_into_buf(conn->socket, &ldap_conn->in_buffer)) {
ldapsrv_terminate_connection(ldap_conn, "read_into_buf() failed");
return;
}
peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
while (buf_length > 0) {
peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
/* LDAP Messages are always SEQUENCES */
if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
&msg_length)) {
ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
return;
}
if (buf_length < msg_length) {
/* Not enough yet */
break;
}
/* We've got a complete LDAP request in the in-buffer, convert
* that to a ldap_message and put it into the incoming
* queue. */
blob.data = buf;
blob.length = msg_length;
if (!asn1_load(&data, blob)) {
ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
return;
}
call = talloc_p(ldap_conn, struct ldapsrv_call);
if (!call) {
ldapsrv_terminate_connection(ldap_conn, "no memory");
return;
}
ZERO_STRUCTP(call);
call->state = LDAPSRV_CALL_STATE_NEW;
call->conn = ldap_conn;
call->request.mem_ctx = call;
if (!ldap_decode(&data, &call->request)) {
dump_data(0,buf, msg_length);
ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
return;
}
DLIST_ADD_END(ldap_conn->calls, call,
struct ldapsrv_call *);
consumed_from_buf(&ldap_conn->in_buffer, msg_length);
ldapsrv_do_call(call);
peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
}
ldapsrv_do_responses(ldap_conn);
if (ldap_conn->out_buffer.length > 0) {
conn->event.fde->flags |= EVENT_FD_WRITE;
}
return;
}
/*
called when a LDAP socket becomes writable
*/
static void ldapsrv_send(struct server_connection *conn, time_t t,
uint16_t flags)
{
struct ldapsrv_connection *ldap_conn = conn->private_data;
DEBUG(10,("ldapsrv_send\n"));
if (!write_from_buf(conn->socket, &ldap_conn->out_buffer)) {
ldapsrv_terminate_connection(ldap_conn, "write_from_buf() failed");
return;
}
if (ldap_conn->out_buffer.length == 0) {
conn->event.fde->flags &= ~EVENT_FD_WRITE;
}
return;
}
/*
called when connection is idle
*/
static void ldapsrv_idle(struct server_connection *conn, time_t t)
{
DEBUG(10,("ldapsrv_idle: not implemented!\n"));
return;
}
static void ldapsrv_close(struct server_connection *conn, const char *reason)
{
struct ldapsrv_connection *ldap_conn = conn->private_data;
talloc_free(ldap_conn);
return;
}
/*
initialise a server_context from a open socket and register a event handler
for reading from that socket
*/
static void ldapsrv_accept(struct server_connection *conn)
{
struct ldapsrv_connection *ldap_conn;
DEBUG(5, ("ldapsrv_accept\n"));
ldap_conn = talloc_p(NULL, struct ldapsrv_connection);
if (ldap_conn == NULL)
return;
ZERO_STRUCTP(ldap_conn);
ldap_conn->connection = conn;
conn->private_data = ldap_conn;
return;
}
/*
called on a fatal error that should cause this server to terminate
*/
static void ldapsrv_exit(struct server_service *service, const char *reason)
{
DEBUG(1,("ldapsrv_exit\n"));
return;
}
static const struct server_service_ops ldap_server_ops = {
.name = "ldap",
.service_init = ldapsrv_init,
.accept_connection = ldapsrv_accept,
.recv_handler = ldapsrv_recv,
.send_handler = ldapsrv_send,
.idle_handler = ldapsrv_idle,
.close_connection = ldapsrv_close,
.service_exit = ldapsrv_exit,
};
const struct server_service_ops *ldapsrv_get_ops(void)
{
return &ldap_server_ops;
}
NTSTATUS server_service_ldap_init(void)
{
return NT_STATUS_OK;
}
|