3a0e3d1cde0804c0a5bb2b5b9586ab393997594b
[mat/samba.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 "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
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, uint16_t flags)
332 {
333         struct ldapsrv_connection *ldap_conn = talloc_get_type(conn->private, struct ldapsrv_connection);
334         uint8_t *buf;
335         size_t buf_length, msg_length;
336         DATA_BLOB blob;
337         struct asn1_data data;
338         struct ldapsrv_call *call;
339         NTSTATUS status;
340
341         DEBUG(10,("ldapsrv_recv\n"));
342
343         if (!ldapsrv_read_buf(ldap_conn)) {
344                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
345                 return;
346         }
347
348         peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
349
350         while (buf_length > 0) {
351                 /* LDAP Messages are always SEQUENCES */
352
353                 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
354                                         &msg_length)) {
355                         ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
356                         return;
357                 }
358
359                 if (buf_length < msg_length) {
360                         /* Not enough yet */
361                         break;
362                 }
363
364                 /* We've got a complete LDAP request in the in-buffer, convert
365                  * that to a ldap_message and put it into the incoming
366                  * queue. */
367
368                 blob.data = buf;
369                 blob.length = msg_length;
370
371                 if (!asn1_load(&data, blob)) {
372                         ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
373                         return;
374                 }
375
376                 call = talloc(ldap_conn, struct ldapsrv_call);
377                 if (!call) {
378                         ldapsrv_terminate_connection(ldap_conn, "no memory");
379                         return;         
380                 }
381
382                 ZERO_STRUCTP(call);
383                 call->state = LDAPSRV_CALL_STATE_NEW;
384                 call->conn = ldap_conn;
385                 call->request.mem_ctx = call;
386
387                 if (!ldap_decode(&data, &call->request)) {
388                         dump_data(0,buf, msg_length);
389                         asn1_free(&data);
390                         ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
391                         return;
392                 }
393
394                 asn1_free(&data);
395
396                 DLIST_ADD_END(ldap_conn->calls, call,
397                               struct ldapsrv_call *);
398
399                 ldapsrv_consumed_from_buf(&ldap_conn->in_buffer, msg_length);
400
401                 status = ldapsrv_do_call(call);
402                 if (!NT_STATUS_IS_OK(status)) {
403                         ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
404                         return;
405                 }
406
407                 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
408         }
409
410         status = ldapsrv_do_responses(ldap_conn);
411         if (!NT_STATUS_IS_OK(status)) {
412                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
413                 return;
414         }
415
416         if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
417                 EVENT_FD_WRITEABLE(conn->event.fde);
418         }
419
420         return;
421 }
422         
423 /*
424   called when a LDAP socket becomes writable
425 */
426 static void ldapsrv_send(struct stream_connection *conn, uint16_t flags)
427 {
428         struct ldapsrv_connection *ldap_conn = talloc_get_type(conn->private, struct ldapsrv_connection);
429
430         DEBUG(10,("ldapsrv_send\n"));
431
432         if (!ldapsrv_write_buf(ldap_conn)) {
433                 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
434                 return;
435         }
436
437         if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
438                 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
439         }
440
441         return;
442 }
443
444 /*
445   initialise a server_context from a open socket and register a event handler
446   for reading from that socket
447 */
448 static void ldapsrv_accept(struct stream_connection *conn)
449 {
450         struct ldapsrv_connection *ldap_conn;
451
452         DEBUG(10, ("ldapsrv_accept\n"));
453
454         ldap_conn = talloc_zero(conn, struct ldapsrv_connection);
455
456         if (ldap_conn == NULL)
457                 return;
458
459         ldap_conn->connection = conn;
460         ldap_conn->service = talloc_get_type(conn->private, struct ldapsrv_service);
461         conn->private = ldap_conn;
462 }
463
464 static const struct stream_server_ops ldap_stream_ops = {
465         .name                   = "ldap",
466         .accept_connection      = ldapsrv_accept,
467         .recv_handler           = ldapsrv_recv,
468         .send_handler           = ldapsrv_send,
469 };
470
471 /*
472   add a socket address to the list of events, one event per port
473 */
474 static NTSTATUS add_socket(struct event_context *event_context, const struct model_ops *model_ops,
475                            const char *address, struct ldapsrv_service *ldap_service)
476 {
477         uint16_t port = 389;
478         NTSTATUS status;
479
480         status = stream_setup_socket(event_context, model_ops, &ldap_stream_ops, 
481                                      "ipv4", address, &port, ldap_service);
482         NT_STATUS_NOT_OK_RETURN(status);
483
484         port = 3268;
485
486         return stream_setup_socket(event_context, model_ops, &ldap_stream_ops, 
487                                    "ipv4", address, &port, ldap_service);
488 }
489
490 /*
491   open the ldap server sockets
492 */
493 static NTSTATUS ldapsrv_init(struct event_context *event_context, const struct model_ops *model_ops)
494 {       
495         struct ldapsrv_service *ldap_service;
496         struct ldapsrv_partition *rootDSE_part;
497         struct ldapsrv_partition *part;
498         NTSTATUS status;
499
500         DEBUG(10,("ldapsrv_init\n"));
501
502         ldap_service = talloc_zero(event_context, struct ldapsrv_service);
503         NT_STATUS_HAVE_NO_MEMORY(ldap_service);
504
505         rootDSE_part = talloc(ldap_service, struct ldapsrv_partition);
506         NT_STATUS_HAVE_NO_MEMORY(rootDSE_part);
507
508         rootDSE_part->base_dn = ""; /* RootDSE */
509         rootDSE_part->ops = ldapsrv_get_rootdse_partition_ops();
510
511         ldap_service->rootDSE = rootDSE_part;
512         DLIST_ADD_END(ldap_service->partitions, rootDSE_part, struct ldapsrv_partition *);
513
514         part = talloc(ldap_service, struct ldapsrv_partition);
515         NT_STATUS_HAVE_NO_MEMORY(part);
516
517         part->base_dn = "*"; /* default partition */
518         if (lp_parm_bool(-1, "ldapsrv", "hacked", False)) {
519                 part->ops = ldapsrv_get_hldb_partition_ops();
520         } else {
521                 part->ops = ldapsrv_get_sldb_partition_ops();
522         }
523
524         ldap_service->default_partition = part;
525         DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
526
527         if (lp_interfaces() && lp_bind_interfaces_only()) {
528                 int num_interfaces = iface_count();
529                 int i;
530
531                 /* We have been given an interfaces line, and been 
532                    told to only bind to those interfaces. Create a
533                    socket per interface and bind to only these.
534                 */
535                 for(i = 0; i < num_interfaces; i++) {
536                         const char *address = iface_n_ip(i);
537                         status = add_socket(event_context, model_ops, address, ldap_service);
538                         NT_STATUS_NOT_OK_RETURN(status);
539                 }
540         } else {
541                 status = add_socket(event_context, model_ops, lp_socket_address(), ldap_service);
542                 NT_STATUS_NOT_OK_RETURN(status);
543         }
544
545         return NT_STATUS_OK;
546 }
547
548
549 NTSTATUS server_service_ldap_init(void)
550 {
551         return register_server_service("ldap", ldapsrv_init);
552 }