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