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