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