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