r4716: add a real ugly hack to get all ldap queries of an
[bbaumbach/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 #include "events.h"
24 #include "auth/auth.h"
25 #include "dlinklist.h"
26 #include "asn_1.h"
27 #include "ldap_server/ldap_server.h"
28
29 /*
30   close the socket and shutdown a server_context
31 */
32 static void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, const char *reason)
33 {
34         server_terminate_connection(ldap_conn->connection, reason);
35 }
36
37 /*
38   add a socket address to the list of events, one event per port
39 */
40 static void add_socket(struct server_service *service, 
41                        const struct model_ops *model_ops, 
42                        struct ipv4_addr *ifip)
43 {
44         struct server_socket *srv_sock;
45         uint16_t port = 389;
46         char *ip_str = talloc_strdup(service, sys_inet_ntoa(*ifip));
47
48         srv_sock = service_setup_socket(service, model_ops, "ipv4", ip_str, &port);
49
50         port = 3268;
51         srv_sock = service_setup_socket(service, model_ops, "ipv4", ip_str, &port);
52
53         talloc_free(ip_str);
54 }
55
56 /****************************************************************************
57  Open the socket communication.
58 ****************************************************************************/
59 static void ldapsrv_init(struct server_service *service,
60                          const struct model_ops *model_ops)
61 {       
62         struct ldapsrv_service *ldap_service;
63         struct ldapsrv_partition *part;
64
65         DEBUG(10,("ldapsrv_init\n"));
66
67         ldap_service = talloc_p(service, struct ldapsrv_service);
68         if (!ldap_service) {
69                 DEBUG(0,("talloc_p(service, struct ldapsrv_service) failed\n"));
70                 return;
71         }
72         ZERO_STRUCTP(ldap_service);
73
74         part = talloc_p(ldap_service, struct ldapsrv_partition);
75         if (!ldap_service) {
76                 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
77                 return;
78         }
79         part->base_dn = ""; /* RootDSE */
80         part->ops = ldapsrv_get_rootdse_partition_ops();
81
82         ldap_service->rootDSE = part;
83         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
84
85         part = talloc_p(ldap_service, struct ldapsrv_partition);
86         if (!ldap_service) {
87                 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
88                 return;
89         }
90         part->base_dn = "*"; /* default partition */
91         if (lp_parm_bool(-1, "ldapsrv", "hacked", False)) {
92                 part->ops = ldapsrv_get_hldb_partition_ops();
93         } else {
94                 part->ops = ldapsrv_get_sldb_partition_ops();
95         }
96
97         ldap_service->default_partition = part;
98         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
99
100         service->private_data = ldap_service;
101
102         if (lp_interfaces() && lp_bind_interfaces_only()) {
103                 int num_interfaces = iface_count();
104                 int i;
105
106                 /* We have been given an interfaces line, and been 
107                    told to only bind to those interfaces. Create a
108                    socket per interface and bind to only these.
109                 */
110                 for(i = 0; i < num_interfaces; i++) {
111                         struct ipv4_addr *ifip = iface_n_ip(i);
112
113                         if (ifip == NULL) {
114                                 DEBUG(0,("ldapsrv_init: interface %d has NULL "
115                                          "IP address !\n", i));
116                                 continue;
117                         }
118
119                         add_socket(service, model_ops, ifip);
120                 }
121         } else {
122                 struct ipv4_addr ifip;
123
124                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
125                 ifip = interpret_addr2(lp_socket_address());
126                 add_socket(service, model_ops, &ifip);
127         }
128 }
129
130 /* This rw-buf api is made to avoid memcpy. For now do that like mad...  The
131    idea is to write into a circular list of buffers where the ideal case is
132    that a read(2) holds a complete request that is then thrown away
133    completely. */
134
135 void ldapsrv_consumed_from_buf(struct rw_buffer *buf,
136                                    size_t length)
137 {
138         memmove(buf->data, buf->data+length, buf->length-length);
139         buf->length -= length;
140 }
141
142 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
143                                size_t *out_length)
144 {
145         *out = buf->data;
146         *out_length = buf->length;
147 }
148
149 BOOL ldapsrv_append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
150 {
151         buf->data = realloc(buf->data, buf->length+length);
152
153         if (buf->data == NULL)
154                 return False;
155
156         memcpy(buf->data+buf->length, data, length);
157
158         buf->length += length;
159         return True;
160 }
161
162 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
163 {
164         NTSTATUS status;
165         DATA_BLOB tmp_blob;
166         BOOL ret;
167         size_t nread;
168
169         tmp_blob = data_blob_talloc(sock, NULL, 1024);
170         if (tmp_blob.data == NULL) {
171                 return False;
172         }
173
174         status = socket_recv(sock, tmp_blob.data, tmp_blob.length, &nread, 0);
175         if (NT_STATUS_IS_ERR(status)) {
176                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
177                 talloc_free(tmp_blob.data);
178                 return False;
179         }
180         tmp_blob.length = nread;
181
182         ret = ldapsrv_append_to_buf(buf, tmp_blob.data, tmp_blob.length);
183
184         talloc_free(tmp_blob.data);
185
186         return ret;
187 }
188
189 static BOOL ldapsrv_read_buf(struct ldapsrv_connection *conn)
190 {
191         NTSTATUS status;
192         DATA_BLOB tmp_blob;
193         DATA_BLOB wrapped;
194         DATA_BLOB unwrapped;
195         BOOL ret;
196         uint8_t *buf;
197         size_t buf_length, sasl_length;
198         struct socket_context *sock = conn->connection->socket;
199         TALLOC_CTX *mem_ctx;
200         size_t nread;
201
202         if (!conn->gensec) {
203                 return read_into_buf(sock, &conn->in_buffer);
204         }
205         if (!conn->session_info) {
206                 return read_into_buf(sock, &conn->in_buffer);
207         }
208         if (!(gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
209               gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL))) {
210                 return read_into_buf(sock, &conn->in_buffer);
211         }
212
213         mem_ctx = talloc_new(conn);
214         if (!mem_ctx) {
215                 DEBUG(0,("no memory\n"));
216                 return False;
217         }
218
219         tmp_blob = data_blob_talloc(mem_ctx, NULL, 1024);
220         if (tmp_blob.data == NULL) {
221                 talloc_free(mem_ctx);
222                 return False;
223         }
224
225         status = socket_recv(sock, tmp_blob.data, tmp_blob.length, &nread, 0);
226         if (NT_STATUS_IS_ERR(status)) {
227                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
228                 talloc_free(mem_ctx);
229                 return False;
230         }
231         tmp_blob.length = nread;
232
233         ret = ldapsrv_append_to_buf(&conn->sasl_in_buffer, tmp_blob.data, tmp_blob.length);
234         if (!ret) {
235                 talloc_free(mem_ctx);
236                 return False;
237         }
238
239         peek_into_read_buf(&conn->sasl_in_buffer, &buf, &buf_length);
240
241         if (buf_length < 4) {
242                 /* not enough yet */
243                 talloc_free(mem_ctx);
244                 return True;
245         }
246
247         sasl_length = RIVAL(buf, 0);
248
249         if ((buf_length - 4) < sasl_length) {
250                 /* not enough yet */
251                 talloc_free(mem_ctx);
252                 return True;
253         }
254
255         wrapped.data = buf + 4;
256         wrapped.length = sasl_length;
257
258         status = gensec_unwrap(conn->gensec, mem_ctx,
259                                &wrapped, 
260                                &unwrapped);
261         if (!NT_STATUS_IS_OK(status)) {
262                 DEBUG(0,("gensec_unwrap: %s\n",nt_errstr(status)));
263                 talloc_free(mem_ctx);
264                 return False;
265         }
266
267         ret = ldapsrv_append_to_buf(&conn->in_buffer, unwrapped.data, unwrapped.length);
268         if (!ret) {
269                 talloc_free(mem_ctx);
270                 return False;
271         }
272
273         ldapsrv_consumed_from_buf(&conn->sasl_in_buffer, 4 + sasl_length);
274
275         talloc_free(mem_ctx);
276         return ret;
277 }
278
279 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
280 {
281         NTSTATUS status;
282         DATA_BLOB tmp_blob;
283         size_t sendlen;
284
285         tmp_blob.data = buf->data;
286         tmp_blob.length = buf->length;
287
288         status = socket_send(sock, &tmp_blob, &sendlen, 0);
289         if (!NT_STATUS_IS_OK(status)) {
290                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
291                 return False;
292         }
293
294         ldapsrv_consumed_from_buf(buf, sendlen);
295
296         return True;
297 }
298
299 static BOOL ldapsrv_write_buf(struct ldapsrv_connection *conn)
300 {
301         NTSTATUS status;
302         DATA_BLOB wrapped;
303         DATA_BLOB tmp_blob;
304         DATA_BLOB sasl;
305         size_t sendlen;
306         BOOL ret;
307         struct socket_context *sock = conn->connection->socket;
308         TALLOC_CTX *mem_ctx;
309
310
311         if (!conn->gensec) {
312                 return write_from_buf(sock, &conn->out_buffer);
313         }
314         if (!conn->session_info) {
315                 return write_from_buf(sock, &conn->out_buffer);
316         }
317         if (!(gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
318               gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL))) {
319                 return write_from_buf(sock, &conn->out_buffer);
320         }
321
322         mem_ctx = talloc_new(conn);
323         if (!mem_ctx) {
324                 DEBUG(0,("no memory\n"));
325                 return False;
326         }
327
328         if (conn->out_buffer.length == 0) {
329                 goto nodata;
330         }
331
332         tmp_blob.data = conn->out_buffer.data;
333         tmp_blob.length = conn->out_buffer.length;
334         status = gensec_wrap(conn->gensec, mem_ctx,
335                              &tmp_blob,
336                              &wrapped);
337         if (!NT_STATUS_IS_OK(status)) {
338                 DEBUG(0,("gensec_wrap: %s\n",nt_errstr(status)));
339                 talloc_free(mem_ctx);
340                 return False;
341         }
342
343         sasl = data_blob_talloc(mem_ctx, NULL, 4 + wrapped.length);
344         if (!sasl.data) {
345                 DEBUG(0,("no memory\n"));
346                 talloc_free(mem_ctx);
347                 return False;
348         }
349
350         RSIVAL(sasl.data, 0, wrapped.length);
351         memcpy(sasl.data + 4, wrapped.data, wrapped.length);
352
353         ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, sasl.data, sasl.length);
354         if (!ret) {
355                 talloc_free(mem_ctx);
356                 return False;
357         }
358         ldapsrv_consumed_from_buf(&conn->out_buffer, conn->out_buffer.length);
359 nodata:
360         tmp_blob.data = conn->sasl_out_buffer.data;
361         tmp_blob.length = conn->sasl_out_buffer.length;
362
363         status = socket_send(sock, &tmp_blob, &sendlen, 0);
364         if (!NT_STATUS_IS_OK(status)) {
365                 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
366                 talloc_free(mem_ctx);
367                 return False;
368         }
369
370         ldapsrv_consumed_from_buf(&conn->sasl_out_buffer, sendlen);
371
372         talloc_free(mem_ctx);
373
374         return True;
375 }
376
377 static BOOL ldap_encode_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
378 {
379         DATA_BLOB blob;
380         BOOL res;
381
382         if (!ldap_encode(msg, &blob))
383                 return False;
384
385         res = ldapsrv_append_to_buf(buf, blob.data, blob.length);
386
387         data_blob_free(&blob);
388         return res;
389 }
390
391 NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
392 {
393         struct ldapsrv_call *call, *next_call = NULL;
394         struct ldapsrv_reply *reply, *next_reply = NULL;
395
396         for (call=conn->calls; call; call=next_call) {
397                 for (reply=call->replies; reply; reply=next_reply) {
398                         if (!ldap_encode_to_buf(&reply->msg, &conn->out_buffer)) {
399                                 return NT_STATUS_FOOBAR;
400                         }
401                         next_reply = reply->next;
402                         DLIST_REMOVE(call->replies, reply);
403                         reply->state = LDAPSRV_REPLY_STATE_SEND;
404                         talloc_free(reply);
405                 }
406                 next_call = call->next;
407                 DLIST_REMOVE(conn->calls, call);
408                 call->state = LDAPSRV_CALL_STATE_COMPLETE;
409                 talloc_free(call);
410         }
411
412         return NT_STATUS_OK;
413 }
414
415 NTSTATUS ldapsrv_flush_responses(struct ldapsrv_connection *conn)
416 {
417         return NT_STATUS_OK;
418 }
419
420 /*
421   called when a LDAP socket becomes readable
422 */
423 static void ldapsrv_recv(struct server_connection *conn, struct timeval t,
424                          uint16_t flags)
425 {
426         struct ldapsrv_connection *ldap_conn = conn->private_data;
427         uint8_t *buf;
428         size_t buf_length, msg_length;
429         DATA_BLOB blob;
430         struct asn1_data data;
431         struct ldapsrv_call *call;
432         NTSTATUS status;
433
434         DEBUG(10,("ldapsrv_recv\n"));
435
436         if (!ldapsrv_read_buf(ldap_conn)) {
437                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
438                 return;
439         }
440
441         peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
442
443         while (buf_length > 0) {
444                 /* LDAP Messages are always SEQUENCES */
445
446                 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
447                                         &msg_length)) {
448                         ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
449                         return;
450                 }
451
452                 if (buf_length < msg_length) {
453                         /* Not enough yet */
454                         break;
455                 }
456
457                 /* We've got a complete LDAP request in the in-buffer, convert
458                  * that to a ldap_message and put it into the incoming
459                  * queue. */
460
461                 blob.data = buf;
462                 blob.length = msg_length;
463
464                 if (!asn1_load(&data, blob)) {
465                         ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
466                         return;
467                 }
468
469                 call = talloc_p(ldap_conn, struct ldapsrv_call);
470                 if (!call) {
471                         ldapsrv_terminate_connection(ldap_conn, "no memory");
472                         return;         
473                 }
474
475                 ZERO_STRUCTP(call);
476                 call->state = LDAPSRV_CALL_STATE_NEW;
477                 call->conn = ldap_conn;
478                 call->request.mem_ctx = call;
479
480                 if (!ldap_decode(&data, &call->request)) {
481                         dump_data(0,buf, msg_length);
482                         asn1_free(&data);
483                         ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
484                         return;
485                 }
486
487                 asn1_free(&data);
488
489                 DLIST_ADD_END(ldap_conn->calls, call,
490                               struct ldapsrv_call *);
491
492                 ldapsrv_consumed_from_buf(&ldap_conn->in_buffer, msg_length);
493
494                 status = ldapsrv_do_call(call);
495                 if (!NT_STATUS_IS_OK(status)) {
496                         ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
497                         return;
498                 }
499
500                 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
501         }
502
503         status = ldapsrv_do_responses(ldap_conn);
504         if (!NT_STATUS_IS_OK(status)) {
505                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
506                 return;
507         }
508
509         if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
510                 conn->event.fde->flags |= EVENT_FD_WRITE;
511         }
512
513         return;
514 }
515         
516 /*
517   called when a LDAP socket becomes writable
518 */
519 static void ldapsrv_send(struct server_connection *conn, struct timeval t,
520                          uint16_t flags)
521 {
522         struct ldapsrv_connection *ldap_conn = conn->private_data;
523
524         DEBUG(10,("ldapsrv_send\n"));
525
526         if (!ldapsrv_write_buf(ldap_conn)) {
527                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
528                 return;
529         }
530
531         if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
532                 conn->event.fde->flags &= ~EVENT_FD_WRITE;
533         }
534
535         return;
536 }
537
538 /*
539   called when connection is idle
540 */
541 static void ldapsrv_idle(struct server_connection *conn, struct timeval t)
542 {
543         DEBUG(10,("ldapsrv_idle: not implemented!\n"));
544         return;
545 }
546
547 static void ldapsrv_close(struct server_connection *conn, const char *reason)
548 {
549         return;
550 }
551
552 /*
553   initialise a server_context from a open socket and register a event handler
554   for reading from that socket
555 */
556 static void ldapsrv_accept(struct server_connection *conn)
557 {
558         struct ldapsrv_connection *ldap_conn;
559
560         DEBUG(10, ("ldapsrv_accept\n"));
561
562         ldap_conn = talloc_p(conn, struct ldapsrv_connection);
563
564         if (ldap_conn == NULL)
565                 return;
566
567         ZERO_STRUCTP(ldap_conn);
568         ldap_conn->connection = conn;
569         ldap_conn->service = talloc_reference(ldap_conn, conn->service->private_data);
570
571         conn->private_data = ldap_conn;
572
573         return;
574 }
575
576 /*
577   called on a fatal error that should cause this server to terminate
578 */
579 static void ldapsrv_exit(struct server_service *service, const char *reason)
580 {
581         DEBUG(10,("ldapsrv_exit\n"));
582         return;
583 }
584
585 static const struct server_service_ops ldap_server_ops = {
586         .name                   = "ldap",
587         .service_init           = ldapsrv_init,
588         .accept_connection      = ldapsrv_accept,
589         .recv_handler           = ldapsrv_recv,
590         .send_handler           = ldapsrv_send,
591         .idle_handler           = ldapsrv_idle,
592         .close_connection       = ldapsrv_close,
593         .service_exit           = ldapsrv_exit, 
594 };
595
596 const struct server_service_ops *ldapsrv_get_ops(void)
597 {
598         return &ldap_server_ops;
599 }
600
601 NTSTATUS server_service_ldap_init(void)
602 {
603         return NT_STATUS_OK;    
604 }