r2731: use debug level 10 everywhere
[ira/wip.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 BOOL append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
133 {
134         buf->data = realloc(buf->data, buf->length+length);
135
136         if (buf->data == NULL)
137                 return False;
138
139         memcpy(buf->data+buf->length, data, length);
140
141         buf->length += length;
142         return True;
143 }
144
145 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
146 {
147         NTSTATUS status;
148         DATA_BLOB tmp_blob;
149         BOOL ret;
150
151         status = socket_recv(sock, sock, &tmp_blob, 1024, 0);
152         if (!NT_STATUS_IS_OK(status)) {
153                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
154                 return False;
155         }
156
157         ret = append_to_buf(buf, tmp_blob.data, tmp_blob.length);
158
159         talloc_free(tmp_blob.data);
160
161         return ret;
162 }
163
164 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
165 {
166         NTSTATUS status;
167         DATA_BLOB tmp_blob;
168         size_t sendlen;
169
170         tmp_blob.data = buf->data;
171         tmp_blob.length = buf->length;
172
173         status = socket_send(sock, sock, &tmp_blob, &sendlen, 0);
174         if (!NT_STATUS_IS_OK(status)) {
175                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
176                 return False;
177         }
178
179         consumed_from_buf(buf, sendlen);
180
181         return True;
182 }
183
184 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
185                                size_t *out_length)
186 {
187         *out = buf->data;
188         *out_length = buf->length;
189 }
190
191 static BOOL ldap_append_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
192 {
193         DATA_BLOB blob;
194         BOOL res;
195
196         if (!ldap_encode(msg, &blob))
197                 return False;
198
199         res = append_to_buf(buf, blob.data, blob.length);
200
201         data_blob_free(&blob);
202         return res;
203 }
204
205 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, enum ldap_request_tag type)
206 {
207         struct ldapsrv_reply *reply;
208
209         reply = talloc_p(call, struct ldapsrv_reply);
210         if (!reply) {
211                 return NULL;
212         }
213
214         reply->prev = reply->next = NULL;
215         reply->state = LDAPSRV_REPLY_STATE_NEW;
216         reply->msg.messageid = call->request.messageid;
217         reply->msg.type = type;
218         reply->msg.mem_ctx = reply;
219
220         return reply;
221 }
222
223 NTSTATUS ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
224 {
225         DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
226         return NT_STATUS_OK;
227 }
228
229 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn)
230 {
231         if (strcasecmp("", dn) == 0) {
232                 return conn->service->rootDSE;
233         }
234
235         return conn->service->default_partition;
236 }
237
238 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
239 {
240         struct ldapsrv_reply *reply;
241         struct ldap_ExtendedResponse *r;
242
243         DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request.type, call->request.messageid));
244
245         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
246         if (!reply) {
247                 return NT_STATUS_NO_MEMORY;
248         }
249
250         r = &reply->msg.r.ExtendedResponse;
251         r->response.resultcode = error;
252         r->response.dn = NULL;
253         r->response.errormessage = NULL;
254         r->response.referral = NULL;
255         r->name = NULL;
256         r->value.data = NULL;
257         r->value.length = 0;
258
259         return ldapsrv_queue_reply(call, reply);
260 }
261
262 static NTSTATUS ldapsrv_BindRequest(struct ldapsrv_call *call)
263 {
264         struct ldap_BindRequest *req = &call->request.r.BindRequest;
265         struct ldapsrv_reply *reply;
266         struct ldap_BindResponse *resp;
267
268         DEBUG(10, ("BindRequest dn: %s\n",req->dn));
269
270         reply = ldapsrv_init_reply(call, LDAP_TAG_BindResponse);
271         if (!reply) {
272                 return NT_STATUS_NO_MEMORY;
273         }
274
275         resp = &reply->msg.r.BindResponse;
276         resp->response.resultcode = 0;
277         resp->response.dn = NULL;
278         resp->response.errormessage = NULL;
279         resp->response.referral = NULL;
280         resp->SASL.secblob = data_blob(NULL, 0);
281
282         return ldapsrv_queue_reply(call, reply);
283 }
284
285 static NTSTATUS ldapsrv_UnbindRequest(struct ldapsrv_call *call)
286 {
287 /*      struct ldap_UnbindRequest *req = &call->request->r.UnbindRequest;*/
288         DEBUG(10, ("UnbindRequest\n"));
289         return NT_STATUS_OK;
290 }
291
292 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
293 {
294         struct ldap_SearchRequest *req = &call->request.r.SearchRequest;
295         struct ldapsrv_partition *part;
296
297         DEBUG(10, ("SearchRequest"));
298         DEBUGADD(10, (" basedn: %s", req->basedn));
299         DEBUGADD(10, (" filter: %s\n", req->filter));
300
301         part = ldapsrv_get_partition(call->conn, req->basedn);
302
303         if (!part->ops->Search) {
304                 struct ldap_Result *done;
305                 struct ldapsrv_reply *done_r;
306
307                 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
308                 if (!done_r) {
309                         return NT_STATUS_NO_MEMORY;
310                 }
311
312                 done = &done_r->msg.r.SearchResultDone;
313                 done->resultcode = 53;
314                 done->dn = NULL;
315                 done->errormessage = NULL;
316                 done->referral = NULL;
317
318                 return ldapsrv_queue_reply(call, done_r);
319         }
320
321         return part->ops->Search(part, call, req);
322 }
323
324 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
325 {
326         struct ldap_ModifyRequest *req = &call->request.r.ModifyRequest;
327         struct ldapsrv_partition *part;
328
329         DEBUG(10, ("ModifyRequest"));
330         DEBUGADD(10, (" dn: %s", req->dn));
331
332         part = ldapsrv_get_partition(call->conn, req->dn);
333
334         if (!part->ops->Modify) {
335                 return ldapsrv_unwilling(call, 53);
336         }
337
338         return part->ops->Modify(part, call, req);
339 }
340
341 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
342 {
343         struct ldap_AddRequest *req = &call->request.r.AddRequest;
344         struct ldapsrv_partition *part;
345
346         DEBUG(10, ("AddRequest"));
347         DEBUGADD(10, (" dn: %s", req->dn));
348
349         part = ldapsrv_get_partition(call->conn, req->dn);
350
351         if (!part->ops->Add) {
352                 return ldapsrv_unwilling(call, 53);
353         }
354
355         return part->ops->Add(part, call, req);
356 }
357
358 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
359 {
360         struct ldap_DelRequest *req = &call->request.r.DelRequest;
361         struct ldapsrv_partition *part;
362
363         DEBUG(10, ("DelRequest"));
364         DEBUGADD(10, (" dn: %s", req->dn));
365
366         part = ldapsrv_get_partition(call->conn, req->dn);
367
368         if (!part->ops->Del) {
369                 return ldapsrv_unwilling(call, 53);
370         }
371
372         return part->ops->Del(part, call, req);
373 }
374
375 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
376 {
377         struct ldap_ModifyDNRequest *req = &call->request.r.ModifyDNRequest;
378         struct ldapsrv_partition *part;
379
380         DEBUG(10, ("ModifyDNRequrest"));
381         DEBUGADD(10, (" dn: %s", req->dn));
382         DEBUGADD(10, (" newrdn: %s", req->newrdn));
383
384         part = ldapsrv_get_partition(call->conn, req->dn);
385
386         if (!part->ops->ModifyDN) {
387                 return ldapsrv_unwilling(call, 53);
388         }
389
390         return part->ops->ModifyDN(part, call, req);
391 }
392
393 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
394 {
395         struct ldap_CompareRequest *req = &call->request.r.CompareRequest;
396         struct ldapsrv_partition *part;
397
398         DEBUG(10, ("CompareRequest"));
399         DEBUGADD(10, (" dn: %s", req->dn));
400
401         part = ldapsrv_get_partition(call->conn, req->dn);
402
403         if (!part->ops->Compare) {
404                 return ldapsrv_unwilling(call, 53);
405         }
406
407         return part->ops->Compare(part, call, req);
408 }
409
410 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
411 {
412 /*      struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
413         DEBUG(10, ("AbandonRequest\n"));
414         return NT_STATUS_OK;
415 }
416
417 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
418 {
419 /*      struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
420         struct ldapsrv_reply *reply;
421
422         DEBUG(10, ("Extended\n"));
423
424         reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
425         if (!reply) {
426                 return NT_STATUS_NO_MEMORY;
427         }
428
429         ZERO_STRUCT(reply->msg.r);
430
431         return ldapsrv_queue_reply(call, reply);
432 }
433
434 static NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
435 {
436         switch(call->request.type) {
437         case LDAP_TAG_BindRequest:
438                 return ldapsrv_BindRequest(call);
439         case LDAP_TAG_UnbindRequest:
440                 return ldapsrv_UnbindRequest(call);
441         case LDAP_TAG_SearchRequest:
442                 return ldapsrv_SearchRequest(call);
443         case LDAP_TAG_ModifyRequest:
444                 return ldapsrv_ModifyRequest(call);
445         case LDAP_TAG_AddRequest:
446                 return ldapsrv_AddRequest(call);
447         case LDAP_TAG_DelRequest:
448                 return ldapsrv_DelRequest(call);
449         case LDAP_TAG_ModifyDNRequest:
450                 return ldapsrv_ModifyDNRequest(call);
451         case LDAP_TAG_CompareRequest:
452                 return ldapsrv_CompareRequest(call);
453         case LDAP_TAG_AbandonRequest:
454                 return ldapsrv_AbandonRequest(call);
455         case LDAP_TAG_ExtendedRequest:
456                 return ldapsrv_ExtendedRequest(call);
457         default:
458                 return ldapsrv_unwilling(call, 2);
459         }
460 }
461
462 static NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
463 {
464         struct ldapsrv_call *call, *next_call = NULL;
465         struct ldapsrv_reply *reply, *next_reply = NULL;
466
467         for (call=conn->calls; call; call=next_call) {
468                 for (reply=call->replies; reply; reply=next_reply) {
469                         if (!ldap_append_to_buf(&reply->msg, &conn->out_buffer)) {
470                                 return NT_STATUS_FOOBAR;
471                         }
472                         next_reply = reply->next;
473                         DLIST_REMOVE(call->replies, reply);
474                         reply->state = LDAPSRV_REPLY_STATE_SEND;
475                         talloc_free(reply);
476                 }
477                 next_call = call->next;
478                 DLIST_REMOVE(conn->calls, call);
479                 call->state = LDAPSRV_CALL_STATE_COMPLETE;
480                 talloc_free(call);
481         }
482
483         return NT_STATUS_OK;
484 }
485
486 /*
487   called when a LDAP socket becomes readable
488 */
489 static void ldapsrv_recv(struct server_connection *conn, time_t t,
490                          uint16_t flags)
491 {
492         struct ldapsrv_connection *ldap_conn = conn->private_data;
493         uint8_t *buf;
494         int buf_length, msg_length;
495         DATA_BLOB blob;
496         ASN1_DATA data;
497         struct ldapsrv_call *call;
498         NTSTATUS status;
499
500         DEBUG(10,("ldapsrv_recv\n"));
501
502         if (!read_into_buf(conn->socket, &ldap_conn->in_buffer)) {
503                 ldapsrv_terminate_connection(ldap_conn, "read_into_buf() failed");
504                 return;
505         }
506
507         peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
508
509         while (buf_length > 0) {
510
511                 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
512                 /* LDAP Messages are always SEQUENCES */
513
514                 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
515                                         &msg_length)) {
516                         ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
517                         return;
518                 }
519
520                 if (buf_length < msg_length) {
521                         /* Not enough yet */
522                         break;
523                 }
524
525                 /* We've got a complete LDAP request in the in-buffer, convert
526                  * that to a ldap_message and put it into the incoming
527                  * queue. */
528
529                 blob.data = buf;
530                 blob.length = msg_length;
531
532                 if (!asn1_load(&data, blob)) {
533                         ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
534                         return;
535                 }
536
537                 call = talloc_p(ldap_conn, struct ldapsrv_call);
538                 if (!call) {
539                         ldapsrv_terminate_connection(ldap_conn, "no memory");
540                         return;         
541                 }
542
543                 ZERO_STRUCTP(call);
544                 call->state = LDAPSRV_CALL_STATE_NEW;
545                 call->conn = ldap_conn;
546                 /* TODO: we should use talloc_reference() here */
547                 call->session_info = ldap_conn->session_info;
548                 call->request.mem_ctx = call;
549
550                 if (!ldap_decode(&data, &call->request)) {
551                         dump_data(0,buf, msg_length);
552                         asn1_free(&data);
553                         ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
554                         return;
555                 }
556
557                 asn1_free(&data);
558
559                 DLIST_ADD_END(ldap_conn->calls, call,
560                               struct ldapsrv_call *);
561
562                 consumed_from_buf(&ldap_conn->in_buffer, msg_length);
563
564                 status = ldapsrv_do_call(call);
565                 if (!NT_STATUS_IS_OK(status)) {
566                         ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
567                         return;
568                 }
569
570                 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
571         }
572
573         status = ldapsrv_do_responses(ldap_conn);
574         if (!NT_STATUS_IS_OK(status)) {
575                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
576                 return;
577         }
578
579         if (ldap_conn->out_buffer.length > 0) {
580                 conn->event.fde->flags |= EVENT_FD_WRITE;
581         }
582
583         return;
584 }
585         
586 /*
587   called when a LDAP socket becomes writable
588 */
589 static void ldapsrv_send(struct server_connection *conn, time_t t,
590                          uint16_t flags)
591 {
592         struct ldapsrv_connection *ldap_conn = conn->private_data;
593
594         DEBUG(10,("ldapsrv_send\n"));
595
596         if (!write_from_buf(conn->socket, &ldap_conn->out_buffer)) {
597                 ldapsrv_terminate_connection(ldap_conn, "write_from_buf() failed");
598                 return;
599         }
600
601         if (ldap_conn->out_buffer.length == 0) {
602                 conn->event.fde->flags &= ~EVENT_FD_WRITE;
603         }
604
605         return;
606 }
607
608 /*
609   called when connection is idle
610 */
611 static void ldapsrv_idle(struct server_connection *conn, time_t t)
612 {
613         DEBUG(10,("ldapsrv_idle: not implemented!\n"));
614         return;
615 }
616
617 static void ldapsrv_close(struct server_connection *conn, const char *reason)
618 {
619         return;
620 }
621
622 /*
623   initialise a server_context from a open socket and register a event handler
624   for reading from that socket
625 */
626 static void ldapsrv_accept(struct server_connection *conn)
627 {
628         struct ldapsrv_connection *ldap_conn;
629
630         DEBUG(10, ("ldapsrv_accept\n"));
631
632         ldap_conn = talloc_p(conn, struct ldapsrv_connection);
633
634         if (ldap_conn == NULL)
635                 return;
636
637         ZERO_STRUCTP(ldap_conn);
638         ldap_conn->connection = conn;
639         ldap_conn->service = talloc_reference(ldap_conn, conn->service->private_data);
640
641         conn->private_data = ldap_conn;
642
643         return;
644 }
645
646 /*
647   called on a fatal error that should cause this server to terminate
648 */
649 static void ldapsrv_exit(struct server_service *service, const char *reason)
650 {
651         DEBUG(10,("ldapsrv_exit\n"));
652         return;
653 }
654
655 static const struct server_service_ops ldap_server_ops = {
656         .name                   = "ldap",
657         .service_init           = ldapsrv_init,
658         .accept_connection      = ldapsrv_accept,
659         .recv_handler           = ldapsrv_recv,
660         .send_handler           = ldapsrv_send,
661         .idle_handler           = ldapsrv_idle,
662         .close_connection       = ldapsrv_close,
663         .service_exit           = ldapsrv_exit, 
664 };
665
666 const struct server_service_ops *ldapsrv_get_ops(void)
667 {
668         return &ldap_server_ops;
669 }
670
671 NTSTATUS server_service_ldap_init(void)
672 {
673         return NT_STATUS_OK;    
674 }