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