r2862: prepare LDAP SASL support for the server
[tprouty/samba.git] / source4 / ldap_server / ldap_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    LDAP server
4    Copyright (C) Volker Lendecke 2004
5    Copyright (C) Stefan Metzmacher 2004
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /*
25   close the socket and shutdown a server_context
26 */
27 static void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, const char *reason)
28 {
29         server_terminate_connection(ldap_conn->connection, reason);
30 }
31
32 /*
33   add a socket address to the list of events, one event per port
34 */
35 static void add_socket(struct server_service *service, 
36                        const struct model_ops *model_ops, 
37                        struct in_addr *ifip)
38 {
39         struct server_socket *srv_sock;
40         uint16_t port = 389;
41         char *ip_str = talloc_strdup(service, inet_ntoa(*ifip));
42
43         srv_sock = service_setup_socket(service, model_ops, ip_str, &port);
44
45         talloc_free(ip_str);
46 }
47
48 /****************************************************************************
49  Open the socket communication.
50 ****************************************************************************/
51 static void ldapsrv_init(struct server_service *service,
52                          const struct model_ops *model_ops)
53 {       
54         struct ldapsrv_service *ldap_service;
55         struct ldapsrv_partition *part;
56
57         DEBUG(10,("ldapsrv_init\n"));
58
59         ldap_service = talloc_p(service, struct ldapsrv_service);
60         if (!ldap_service) {
61                 DEBUG(0,("talloc_p(service, struct ldapsrv_service) failed\n"));
62                 return;
63         }
64         ZERO_STRUCTP(ldap_service);
65
66         part = talloc_p(ldap_service, struct ldapsrv_partition);
67         if (!ldap_service) {
68                 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
69                 return;
70         }
71         part->base_dn = ""; /* RootDSE */
72         part->ops = ldapsrv_get_rootdse_partition_ops();
73
74         ldap_service->rootDSE = part;
75         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
76
77         part = talloc_p(ldap_service, struct ldapsrv_partition);
78         if (!ldap_service) {
79                 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
80                 return;
81         }
82         part->base_dn = "*"; /* default partition */
83         part->ops = ldapsrv_get_sldb_partition_ops();
84
85         ldap_service->default_partition = part;
86         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
87
88         service->private_data = ldap_service;
89
90         if (lp_interfaces() && lp_bind_interfaces_only()) {
91                 int num_interfaces = iface_count();
92                 int i;
93
94                 /* We have been given an interfaces line, and been 
95                    told to only bind to those interfaces. Create a
96                    socket per interface and bind to only these.
97                 */
98                 for(i = 0; i < num_interfaces; i++) {
99                         struct in_addr *ifip = iface_n_ip(i);
100
101                         if (ifip == NULL) {
102                                 DEBUG(0,("ldapsrv_init: interface %d has NULL "
103                                          "IP address !\n", i));
104                                 continue;
105                         }
106
107                         add_socket(service, model_ops, ifip);
108                 }
109         } else {
110                 struct in_addr *ifip;
111
112                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
113                 ifip = interpret_addr2(service, lp_socket_address());
114                 add_socket(service, model_ops, ifip);
115
116                 talloc_destroy(ifip);
117         }
118 }
119
120 /* This rw-buf api is made to avoid memcpy. For now do that like mad...  The
121    idea is to write into a circular list of buffers where the ideal case is
122    that a read(2) holds a complete request that is then thrown away
123    completely. */
124
125 static void consumed_from_buf(struct rw_buffer *buf,
126                                    size_t length)
127 {
128         memcpy(buf->data, buf->data+length, buf->length-length);
129         buf->length -= length;
130 }
131
132 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
133                                size_t *out_length)
134 {
135         *out = buf->data;
136         *out_length = buf->length;
137 }
138
139 static BOOL append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
140 {
141         buf->data = realloc(buf->data, buf->length+length);
142
143         if (buf->data == NULL)
144                 return False;
145
146         memcpy(buf->data+buf->length, data, length);
147
148         buf->length += length;
149         return True;
150 }
151
152 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
153 {
154         NTSTATUS status;
155         DATA_BLOB tmp_blob;
156         BOOL ret;
157
158         status = socket_recv(sock, sock, &tmp_blob, 1024, 0);
159         if (!NT_STATUS_IS_OK(status)) {
160                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
161                 return False;
162         }
163
164         ret = append_to_buf(buf, tmp_blob.data, tmp_blob.length);
165
166         talloc_free(tmp_blob.data);
167
168         return ret;
169 }
170
171 static BOOL ldapsrv_read_buf(struct ldapsrv_connection *conn)
172 {
173         NTSTATUS status;
174         DATA_BLOB tmp_blob;
175         DATA_BLOB creds;
176         BOOL ret;
177         uint8_t *buf;
178         int buf_length, sasl_length;
179         struct socket_context *sock = conn->connection->socket;
180         TALLOC_CTX *mem_ctx;
181
182         if (!conn->gensec ||
183            !(gensec_have_feature(conn->gensec, GENSEC_WANT_SIGN) &&
184              gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL))) {
185                 return read_into_buf(sock, &conn->in_buffer);
186         }
187
188         mem_ctx = talloc(conn, 0);
189         if (!mem_ctx) {
190                 DEBUG(0,("no memory\n"));
191                 return False;
192         }
193
194         status = socket_recv(sock, mem_ctx, &tmp_blob, 1024, 0);
195         if (!NT_STATUS_IS_OK(status)) {
196                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
197                 talloc_free(mem_ctx);
198                 return False;
199         }
200
201         ret = append_to_buf(&conn->sasl_in_buffer, tmp_blob.data, tmp_blob.length);
202         if (!ret) {
203                 talloc_free(mem_ctx);
204                 return False;
205         }
206
207         peek_into_read_buf(&conn->sasl_in_buffer, &buf, &buf_length);
208
209         if (buf_length < 4) {
210                 /* not enough yet */
211                 talloc_free(mem_ctx);
212                 return True;
213         }
214
215         sasl_length = RIVAL(buf, 0);
216
217         if (buf_length < (4 + sasl_length)) {
218                 /* not enough yet */
219                 talloc_free(mem_ctx);
220                 return True;
221         }
222
223         creds.data = buf + 4;
224         creds.length = gensec_sig_size(conn->gensec);
225
226         if (creds.length > sasl_length) {
227                 /* invalid packet? */
228                 talloc_free(mem_ctx);
229                 return False;
230         }
231
232         tmp_blob.data = buf + (4 + creds.length);
233         tmp_blob.length = (4 + sasl_length) - (4 + creds.length);
234
235         if (gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL)) {
236                 status = gensec_unseal_packet(conn->gensec, mem_ctx,
237                                               tmp_blob.data, tmp_blob.length,
238                                               tmp_blob.data, tmp_blob.length,
239                                               &creds);
240                 if (!NT_STATUS_IS_OK(status)) {
241                         DEBUG(0,("gensec_unseal_packet: %s\n",nt_errstr(status)));
242                         talloc_free(mem_ctx);
243                         return False;
244                 }
245         } else {
246                 status = gensec_check_packet(conn->gensec, mem_ctx,
247                                               tmp_blob.data, tmp_blob.length,
248                                               tmp_blob.data, tmp_blob.length,
249                                               &creds);
250                 if (!NT_STATUS_IS_OK(status)) {
251                         DEBUG(0,("gensec_check_packet: %s\n",nt_errstr(status)));
252                         talloc_free(mem_ctx);
253                         return False;
254                 }
255         }
256
257         ret = append_to_buf(&conn->in_buffer, tmp_blob.data, tmp_blob.length);
258         if (!ret) {
259                 talloc_free(mem_ctx);
260                 return False;
261         }
262
263         consumed_from_buf(&conn->sasl_in_buffer, 4 + sasl_length);
264
265         talloc_free(mem_ctx);
266         return ret;
267 }
268
269 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
270 {
271         NTSTATUS status;
272         DATA_BLOB tmp_blob;
273         size_t sendlen;
274
275         tmp_blob.data = buf->data;
276         tmp_blob.length = buf->length;
277
278         status = socket_send(sock, sock, &tmp_blob, &sendlen, 0);
279         if (!NT_STATUS_IS_OK(status)) {
280                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
281                 return False;
282         }
283
284         consumed_from_buf(buf, sendlen);
285
286         return True;
287 }
288
289 static BOOL ldapsrv_write_buf(struct ldapsrv_connection *conn)
290 {
291         NTSTATUS status;
292         DATA_BLOB tmp_blob;
293         DATA_BLOB creds;
294         DATA_BLOB sasl;
295         size_t sendlen;
296         BOOL ret;
297         struct socket_context *sock = conn->connection->socket;
298         TALLOC_CTX *mem_ctx;
299
300         if (!conn->gensec ||
301            !(gensec_have_feature(conn->gensec, GENSEC_WANT_SIGN) &&
302              gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL))) {
303                 return write_from_buf(sock, &conn->out_buffer);
304         }
305
306         mem_ctx = talloc(conn, 0);
307         if (!mem_ctx) {
308                 DEBUG(0,("no memory\n"));
309                 return False;
310         }
311
312         tmp_blob.data = conn->out_buffer.data;
313         tmp_blob.length = conn->out_buffer.length;
314
315         if (gensec_have_feature(conn->gensec, GENSEC_WANT_SEAL)) {
316                 status = gensec_seal_packet(conn->gensec, mem_ctx,
317                                             tmp_blob.data, tmp_blob.length,
318                                             tmp_blob.data, tmp_blob.length,
319                                             &creds);
320                 if (!NT_STATUS_IS_OK(status)) {
321                         DEBUG(0,("gensec_seal_packet: %s\n",nt_errstr(status)));
322                         talloc_free(mem_ctx);
323                         return False;
324                 }
325         } else {
326                 status = gensec_sign_packet(conn->gensec, mem_ctx,
327                                             tmp_blob.data, tmp_blob.length,
328                                             tmp_blob.data, tmp_blob.length,
329                                             &creds);
330                 if (!NT_STATUS_IS_OK(status)) {
331                         DEBUG(0,("gensec_sign_packet: %s\n",nt_errstr(status)));
332                         talloc_free(mem_ctx);
333                         return False;
334                 }               
335         }
336
337         sasl = data_blob_talloc(mem_ctx, NULL, 4 + creds.length + tmp_blob.length);
338         if (!sasl.data) {
339                 DEBUG(0,("no memory\n"));
340                 talloc_free(mem_ctx);
341                 return False;
342         }
343
344         RSIVAL(sasl.data, 0, creds.length + tmp_blob.length);
345         memcpy(sasl.data + 4, creds.data, creds.length);
346         memcpy(sasl.data + 4 + creds.length, tmp_blob.data, tmp_blob.length);
347
348         ret = append_to_buf(&conn->sasl_out_buffer, sasl.data, sasl.length);
349         if (!ret) {
350                 talloc_free(mem_ctx);
351                 return False;
352         }
353         consumed_from_buf(&conn->out_buffer, tmp_blob.length);
354
355         status = socket_send(sock, mem_ctx, &tmp_blob, &sendlen, 0);
356         if (!NT_STATUS_IS_OK(status)) {
357                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
358                 talloc_free(mem_ctx);
359                 return False;
360         }
361
362         consumed_from_buf(&conn->sasl_out_buffer, sendlen);
363
364         talloc_free(mem_ctx);
365
366         return True;
367 }
368
369 static BOOL ldap_append_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
370 {
371         DATA_BLOB blob;
372         BOOL res;
373
374         if (!ldap_encode(msg, &blob))
375                 return False;
376
377         res = append_to_buf(buf, blob.data, blob.length);
378
379         data_blob_free(&blob);
380         return res;
381 }
382
383 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, enum ldap_request_tag type)
384 {
385         struct ldapsrv_reply *reply;
386
387         reply = talloc_p(call, struct ldapsrv_reply);
388         if (!reply) {
389                 return NULL;
390         }
391
392         reply->prev = reply->next = NULL;
393         reply->state = LDAPSRV_REPLY_STATE_NEW;
394         reply->msg.messageid = call->request.messageid;
395         reply->msg.type = type;
396         reply->msg.mem_ctx = reply;
397
398         return reply;
399 }
400
401 NTSTATUS ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
402 {
403         DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
404         return NT_STATUS_OK;
405 }
406
407 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn)
408 {
409         if (strcasecmp("", dn) == 0) {
410                 return conn->service->rootDSE;
411         }
412
413         return conn->service->default_partition;
414 }
415
416 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
417 {
418         struct ldapsrv_reply *reply;
419         struct ldap_ExtendedResponse *r;
420
421         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request.type, call->request.messageid));
422
423         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
424         if (!reply) {
425                 return NT_STATUS_NO_MEMORY;
426         }
427
428         r = &reply->msg.r.ExtendedResponse;
429         r->response.resultcode = error;
430         r->response.dn = NULL;
431         r->response.errormessage = NULL;
432         r->response.referral = NULL;
433         r->name = NULL;
434         r->value.data = NULL;
435         r->value.length = 0;
436
437         return ldapsrv_queue_reply(call, reply);
438 }
439
440 static NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
441 {
442         struct ldap_BindRequest *req = &call->request.r.BindRequest;
443         struct ldapsrv_reply *reply;
444         struct ldap_BindResponse *resp;
445
446         DEBUG(10, ("BindRequest dn: %s\n",req->dn));
447
448         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
449         if (!reply) {
450                 return NT_STATUS_NO_MEMORY;
451         }
452
453         resp = &reply->msg.r.BindResponse;
454         resp->response.resultcode = 0;
455         resp->response.dn = NULL;
456         resp->response.errormessage = NULL;
457         resp->response.referral = NULL;
458         resp->SASL.secblob = data_blob(NULL, 0);
459
460         return ldapsrv_queue_reply(call, reply);
461 }
462
463 static NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
464 {
465 /*      struct ldap_UnbindRequest *req = &call->request->r.UnbindRequest;*/
466         DEBUG(10, ("UnbindRequest\n"));
467         return NT_STATUS_OK;
468 }
469
470 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
471 {
472         struct ldap_SearchRequest *req = &call->request.r.SearchRequest;
473         struct ldapsrv_partition *part;
474
475         DEBUG(10, ("SearchRequest"));
476         DEBUGADD(10, (" basedn: %s", req->basedn));
477         DEBUGADD(10, (" filter: %s\n", req->filter));
478
479         part = ldapsrv_get_partition(call->conn, req->basedn);
480
481         if (!part->ops->Search) {
482                 struct ldap_Result *done;
483                 struct ldapsrv_reply *done_r;
484
485                 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
486                 if (!done_r) {
487                         return NT_STATUS_NO_MEMORY;
488                 }
489
490                 done = &done_r->msg.r.SearchResultDone;
491                 done->resultcode = 53;
492                 done->dn = NULL;
493                 done->errormessage = NULL;
494                 done->referral = NULL;
495
496                 return ldapsrv_queue_reply(call, done_r);
497         }
498
499         return part->ops->Search(part, call, req);
500 }
501
502 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
503 {
504         struct ldap_ModifyRequest *req = &call->request.r.ModifyRequest;
505         struct ldapsrv_partition *part;
506
507         DEBUG(10, ("ModifyRequest"));
508         DEBUGADD(10, (" dn: %s", req->dn));
509
510         part = ldapsrv_get_partition(call->conn, req->dn);
511
512         if (!part->ops->Modify) {
513                 return ldapsrv_unwilling(call, 53);
514         }
515
516         return part->ops->Modify(part, call, req);
517 }
518
519 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
520 {
521         struct ldap_AddRequest *req = &call->request.r.AddRequest;
522         struct ldapsrv_partition *part;
523
524         DEBUG(10, ("AddRequest"));
525         DEBUGADD(10, (" dn: %s", req->dn));
526
527         part = ldapsrv_get_partition(call->conn, req->dn);
528
529         if (!part->ops->Add) {
530                 return ldapsrv_unwilling(call, 53);
531         }
532
533         return part->ops->Add(part, call, req);
534 }
535
536 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
537 {
538         struct ldap_DelRequest *req = &call->request.r.DelRequest;
539         struct ldapsrv_partition *part;
540
541         DEBUG(10, ("DelRequest"));
542         DEBUGADD(10, (" dn: %s", req->dn));
543
544         part = ldapsrv_get_partition(call->conn, req->dn);
545
546         if (!part->ops->Del) {
547                 return ldapsrv_unwilling(call, 53);
548         }
549
550         return part->ops->Del(part, call, req);
551 }
552
553 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
554 {
555         struct ldap_ModifyDNRequest *req = &call->request.r.ModifyDNRequest;
556         struct ldapsrv_partition *part;
557
558         DEBUG(10, ("ModifyDNRequrest"));
559         DEBUGADD(10, (" dn: %s", req->dn));
560         DEBUGADD(10, (" newrdn: %s", req->newrdn));
561
562         part = ldapsrv_get_partition(call->conn, req->dn);
563
564         if (!part->ops->ModifyDN) {
565                 return ldapsrv_unwilling(call, 53);
566         }
567
568         return part->ops->ModifyDN(part, call, req);
569 }
570
571 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
572 {
573         struct ldap_CompareRequest *req = &call->request.r.CompareRequest;
574         struct ldapsrv_partition *part;
575
576         DEBUG(10, ("CompareRequest"));
577         DEBUGADD(10, (" dn: %s", req->dn));
578
579         part = ldapsrv_get_partition(call->conn, req->dn);
580
581         if (!part->ops->Compare) {
582                 return ldapsrv_unwilling(call, 53);
583         }
584
585         return part->ops->Compare(part, call, req);
586 }
587
588 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
589 {
590 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
591         DEBUG(10, ("AbandonRequest\n"));
592         return NT_STATUS_OK;
593 }
594
595 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
596 {
597 /*      struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
598         struct ldapsrv_reply *reply;
599
600         DEBUG(10, ("Extended\n"));
601
602         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
603         if (!reply) {
604                 return NT_STATUS_NO_MEMORY;
605         }
606
607         ZERO_STRUCT(reply->msg.r);
608
609         return ldapsrv_queue_reply(call, reply);
610 }
611
612 static NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
613 {
614         switch(call->request.type) {
615         case LDAP_TAG_BindRequest:
616                 return ldapsrv_BindRequest(call);
617         case LDAP_TAG_UnbindRequest:
618                 return ldapsrv_UnbindRequest(call);
619         case LDAP_TAG_SearchRequest:
620                 return ldapsrv_SearchRequest(call);
621         case LDAP_TAG_ModifyRequest:
622                 return ldapsrv_ModifyRequest(call);
623         case LDAP_TAG_AddRequest:
624                 return ldapsrv_AddRequest(call);
625         case LDAP_TAG_DelRequest:
626                 return ldapsrv_DelRequest(call);
627         case LDAP_TAG_ModifyDNRequest:
628                 return ldapsrv_ModifyDNRequest(call);
629         case LDAP_TAG_CompareRequest:
630                 return ldapsrv_CompareRequest(call);
631         case LDAP_TAG_AbandonRequest:
632                 return ldapsrv_AbandonRequest(call);
633         case LDAP_TAG_ExtendedRequest:
634                 return ldapsrv_ExtendedRequest(call);
635         default:
636                 return ldapsrv_unwilling(call, 2);
637         }
638 }
639
640 static NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
641 {
642         struct ldapsrv_call *call, *next_call = NULL;
643         struct ldapsrv_reply *reply, *next_reply = NULL;
644
645         for (call=conn->calls; call; call=next_call) {
646                 for (reply=call->replies; reply; reply=next_reply) {
647                         if (!ldap_append_to_buf(&reply->msg, &conn->out_buffer)) {
648                                 return NT_STATUS_FOOBAR;
649                         }
650                         next_reply = reply->next;
651                         DLIST_REMOVE(call->replies, reply);
652                         reply->state = LDAPSRV_REPLY_STATE_SEND;
653                         talloc_free(reply);
654                 }
655                 next_call = call->next;
656                 DLIST_REMOVE(conn->calls, call);
657                 call->state = LDAPSRV_CALL_STATE_COMPLETE;
658                 talloc_free(call);
659         }
660
661         return NT_STATUS_OK;
662 }
663
664 /*
665   called when a LDAP socket becomes readable
666 */
667 static void ldapsrv_recv(struct server_connection *conn, time_t t,
668                          uint16_t flags)
669 {
670         struct ldapsrv_connection *ldap_conn = conn->private_data;
671         uint8_t *buf;
672         int buf_length, msg_length;
673         DATA_BLOB blob;
674         ASN1_DATA data;
675         struct ldapsrv_call *call;
676         NTSTATUS status;
677
678         DEBUG(10,("ldapsrv_recv\n"));
679
680         if (!ldapsrv_read_buf(ldap_conn)) {
681                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
682                 return;
683         }
684
685         peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
686
687         while (buf_length > 0) {
688                 /* LDAP Messages are always SEQUENCES */
689
690                 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
691                                         &msg_length)) {
692                         ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
693                         return;
694                 }
695
696                 if (buf_length < msg_length) {
697                         /* Not enough yet */
698                         break;
699                 }
700
701                 /* We've got a complete LDAP request in the in-buffer, convert
702                  * that to a ldap_message and put it into the incoming
703                  * queue. */
704
705                 blob.data = buf;
706                 blob.length = msg_length;
707
708                 if (!asn1_load(&data, blob)) {
709                         ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
710                         return;
711                 }
712
713                 call = talloc_p(ldap_conn, struct ldapsrv_call);
714                 if (!call) {
715                         ldapsrv_terminate_connection(ldap_conn, "no memory");
716                         return;         
717                 }
718
719                 ZERO_STRUCTP(call);
720                 call->state = LDAPSRV_CALL_STATE_NEW;
721                 call->conn = ldap_conn;
722                 /* TODO: we should use talloc_reference() here */
723                 call->session_info = ldap_conn->session_info;
724                 call->request.mem_ctx = call;
725
726                 if (!ldap_decode(&data, &call->request)) {
727                         dump_data(0,buf, msg_length);
728                         asn1_free(&data);
729                         ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
730                         return;
731                 }
732
733                 asn1_free(&data);
734
735                 DLIST_ADD_END(ldap_conn->calls, call,
736                               struct ldapsrv_call *);
737
738                 consumed_from_buf(&ldap_conn->in_buffer, msg_length);
739
740                 status = ldapsrv_do_call(call);
741                 if (!NT_STATUS_IS_OK(status)) {
742                         ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
743                         return;
744                 }
745
746                 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
747         }
748
749         status = ldapsrv_do_responses(ldap_conn);
750         if (!NT_STATUS_IS_OK(status)) {
751                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
752                 return;
753         }
754
755         if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
756                 conn->event.fde->flags |= EVENT_FD_WRITE;
757         }
758
759         return;
760 }
761         
762 /*
763   called when a LDAP socket becomes writable
764 */
765 static void ldapsrv_send(struct server_connection *conn, time_t t,
766                          uint16_t flags)
767 {
768         struct ldapsrv_connection *ldap_conn = conn->private_data;
769
770         DEBUG(10,("ldapsrv_send\n"));
771
772         if (!ldapsrv_write_buf(ldap_conn)) {
773                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
774                 return;
775         }
776
777         if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
778                 conn->event.fde->flags &= ~EVENT_FD_WRITE;
779         }
780
781         return;
782 }
783
784 /*
785   called when connection is idle
786 */
787 static void ldapsrv_idle(struct server_connection *conn, time_t t)
788 {
789         DEBUG(10,("ldapsrv_idle: not implemented!\n"));
790         return;
791 }
792
793 static void ldapsrv_close(struct server_connection *conn, const char *reason)
794 {
795         return;
796 }
797
798 /*
799   initialise a server_context from a open socket and register a event handler
800   for reading from that socket
801 */
802 static void ldapsrv_accept(struct server_connection *conn)
803 {
804         struct ldapsrv_connection *ldap_conn;
805
806         DEBUG(10, ("ldapsrv_accept\n"));
807
808         ldap_conn = talloc_p(conn, struct ldapsrv_connection);
809
810         if (ldap_conn == NULL)
811                 return;
812
813         ZERO_STRUCTP(ldap_conn);
814         ldap_conn->connection = conn;
815         ldap_conn->service = talloc_reference(ldap_conn, conn->service->private_data);
816
817         conn->private_data = ldap_conn;
818
819         return;
820 }
821
822 /*
823   called on a fatal error that should cause this server to terminate
824 */
825 static void ldapsrv_exit(struct server_service *service, const char *reason)
826 {
827         DEBUG(10,("ldapsrv_exit\n"));
828         return;
829 }
830
831 static const struct server_service_ops ldap_server_ops = {
832         .name                   = "ldap",
833         .service_init           = ldapsrv_init,
834         .accept_connection      = ldapsrv_accept,
835         .recv_handler           = ldapsrv_recv,
836         .send_handler           = ldapsrv_send,
837         .idle_handler           = ldapsrv_idle,
838         .close_connection       = ldapsrv_close,
839         .service_exit           = ldapsrv_exit, 
840 };
841
842 const struct server_service_ops *ldapsrv_get_ops(void)
843 {
844         return &ldap_server_ops;
845 }
846
847 NTSTATUS server_service_ldap_init(void)
848 {
849         return NT_STATUS_OK;    
850 }