2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2004
5 Copyright (C) Stefan Metzmacher 2004
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.
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.
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.
24 #include "auth/auth.h"
25 #include "dlinklist.h"
27 #include "ldap_server/ldap_server.h"
30 close the socket and shutdown a server_context
32 static void ldapsrv_terminate_connection(struct ldapsrv_connection *ldap_conn, const char *reason)
34 server_terminate_connection(ldap_conn->connection, reason);
37 static const struct server_stream_ops *ldapsrv_get_stream_ops(void);
40 add a socket address to the list of events, one event per port
42 static void add_socket(struct server_service *service,
43 struct ipv4_addr *ifip)
45 struct server_stream_socket *stream_socket;
47 char *ip_str = talloc_strdup(service, sys_inet_ntoa(*ifip));
49 stream_socket = service_setup_stream_socket(service, ldapsrv_get_stream_ops(), "ipv4", ip_str, &port);
52 stream_socket = service_setup_stream_socket(service, ldapsrv_get_stream_ops(), "ipv4", ip_str, &port);
57 /****************************************************************************
58 Open the socket communication.
59 ****************************************************************************/
60 static void ldapsrv_init(struct server_service *service)
62 struct ldapsrv_service *ldap_service;
63 struct ldapsrv_partition *rootDSE_part;
64 struct ldapsrv_partition *part;
66 DEBUG(10,("ldapsrv_init\n"));
68 ldap_service = talloc_p(service, struct ldapsrv_service);
70 DEBUG(0,("talloc_p(service, struct ldapsrv_service) failed\n"));
73 ZERO_STRUCTP(ldap_service);
75 rootDSE_part = talloc_p(ldap_service, struct ldapsrv_partition);
77 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
80 rootDSE_part->base_dn = ""; /* RootDSE */
81 rootDSE_part->ops = ldapsrv_get_rootdse_partition_ops();
83 ldap_service->rootDSE = rootDSE_part;
84 DLIST_ADD_END(ldap_service->partitions, rootDSE_part, struct ldapsrv_partition *);
86 part = talloc_p(ldap_service, struct ldapsrv_partition);
88 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
91 part->base_dn = "*"; /* default partition */
92 if (lp_parm_bool(-1, "ldapsrv", "hacked", False)) {
93 part->ops = ldapsrv_get_hldb_partition_ops();
95 part->ops = ldapsrv_get_sldb_partition_ops();
98 ldap_service->default_partition = part;
99 DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
101 service->service.private_data = ldap_service;
103 if (lp_interfaces() && lp_bind_interfaces_only()) {
104 int num_interfaces = iface_count();
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.
111 for(i = 0; i < num_interfaces; i++) {
112 struct ipv4_addr *ifip = iface_n_ip(i);
115 DEBUG(0,("ldapsrv_init: interface %d has NULL "
116 "IP address !\n", i));
120 add_socket(service, ifip);
123 struct ipv4_addr ifip;
125 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
126 ifip = interpret_addr2(lp_socket_address());
127 add_socket(service, &ifip);
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
136 void ldapsrv_consumed_from_buf(struct rw_buffer *buf,
139 memmove(buf->data, buf->data+length, buf->length-length);
140 buf->length -= length;
143 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
147 *out_length = buf->length;
150 BOOL ldapsrv_append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
152 buf->data = realloc(buf->data, buf->length+length);
154 if (buf->data == NULL)
157 memcpy(buf->data+buf->length, data, length);
159 buf->length += length;
163 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
170 tmp_blob = data_blob_talloc(sock, NULL, 1024);
171 if (tmp_blob.data == NULL) {
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);
181 tmp_blob.length = nread;
183 ret = ldapsrv_append_to_buf(buf, tmp_blob.data, tmp_blob.length);
185 talloc_free(tmp_blob.data);
190 static BOOL ldapsrv_read_buf(struct ldapsrv_connection *conn)
198 size_t buf_length, sasl_length;
199 struct socket_context *sock = conn->connection->socket;
204 return read_into_buf(sock, &conn->in_buffer);
206 if (!conn->session_info) {
207 return read_into_buf(sock, &conn->in_buffer);
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);
214 mem_ctx = talloc_new(conn);
216 DEBUG(0,("no memory\n"));
220 tmp_blob = data_blob_talloc(mem_ctx, NULL, 1024);
221 if (tmp_blob.data == NULL) {
222 talloc_free(mem_ctx);
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);
232 tmp_blob.length = nread;
234 ret = ldapsrv_append_to_buf(&conn->sasl_in_buffer, tmp_blob.data, tmp_blob.length);
236 talloc_free(mem_ctx);
240 peek_into_read_buf(&conn->sasl_in_buffer, &buf, &buf_length);
242 if (buf_length < 4) {
244 talloc_free(mem_ctx);
248 sasl_length = RIVAL(buf, 0);
250 if ((buf_length - 4) < sasl_length) {
252 talloc_free(mem_ctx);
256 wrapped.data = buf + 4;
257 wrapped.length = sasl_length;
259 status = gensec_unwrap(conn->gensec, mem_ctx,
262 if (!NT_STATUS_IS_OK(status)) {
263 DEBUG(0,("gensec_unwrap: %s\n",nt_errstr(status)));
264 talloc_free(mem_ctx);
268 ret = ldapsrv_append_to_buf(&conn->in_buffer, unwrapped.data, unwrapped.length);
270 talloc_free(mem_ctx);
274 ldapsrv_consumed_from_buf(&conn->sasl_in_buffer, 4 + sasl_length);
276 talloc_free(mem_ctx);
280 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
286 tmp_blob.data = buf->data;
287 tmp_blob.length = buf->length;
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)));
295 ldapsrv_consumed_from_buf(buf, sendlen);
300 static BOOL ldapsrv_write_buf(struct ldapsrv_connection *conn)
308 struct socket_context *sock = conn->connection->socket;
313 return write_from_buf(sock, &conn->out_buffer);
315 if (!conn->session_info) {
316 return write_from_buf(sock, &conn->out_buffer);
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);
323 mem_ctx = talloc_new(conn);
325 DEBUG(0,("no memory\n"));
329 if (conn->out_buffer.length == 0) {
333 tmp_blob.data = conn->out_buffer.data;
334 tmp_blob.length = conn->out_buffer.length;
335 status = gensec_wrap(conn->gensec, mem_ctx,
338 if (!NT_STATUS_IS_OK(status)) {
339 DEBUG(0,("gensec_wrap: %s\n",nt_errstr(status)));
340 talloc_free(mem_ctx);
344 sasl = data_blob_talloc(mem_ctx, NULL, 4 + wrapped.length);
346 DEBUG(0,("no memory\n"));
347 talloc_free(mem_ctx);
351 RSIVAL(sasl.data, 0, wrapped.length);
352 memcpy(sasl.data + 4, wrapped.data, wrapped.length);
354 ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, sasl.data, sasl.length);
356 talloc_free(mem_ctx);
359 ldapsrv_consumed_from_buf(&conn->out_buffer, conn->out_buffer.length);
361 tmp_blob.data = conn->sasl_out_buffer.data;
362 tmp_blob.length = conn->sasl_out_buffer.length;
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);
371 ldapsrv_consumed_from_buf(&conn->sasl_out_buffer, sendlen);
373 talloc_free(mem_ctx);
378 static BOOL ldap_encode_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
383 if (!ldap_encode(msg, &blob))
386 res = ldapsrv_append_to_buf(buf, blob.data, blob.length);
388 data_blob_free(&blob);
392 NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
394 struct ldapsrv_call *call, *next_call = NULL;
395 struct ldapsrv_reply *reply, *next_reply = NULL;
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;
402 next_reply = reply->next;
403 DLIST_REMOVE(call->replies, reply);
404 reply->state = LDAPSRV_REPLY_STATE_SEND;
407 next_call = call->next;
408 DLIST_REMOVE(conn->calls, call);
409 call->state = LDAPSRV_CALL_STATE_COMPLETE;
416 NTSTATUS ldapsrv_flush_responses(struct ldapsrv_connection *conn)
422 called when a LDAP socket becomes readable
424 static void ldapsrv_recv(struct server_connection *conn, struct timeval t,
427 struct ldapsrv_connection *ldap_conn = conn->connection.private_data;
429 size_t buf_length, msg_length;
431 struct asn1_data data;
432 struct ldapsrv_call *call;
435 DEBUG(10,("ldapsrv_recv\n"));
437 if (!ldapsrv_read_buf(ldap_conn)) {
438 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
442 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
444 while (buf_length > 0) {
445 /* LDAP Messages are always SEQUENCES */
447 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
449 ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
453 if (buf_length < msg_length) {
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
463 blob.length = msg_length;
465 if (!asn1_load(&data, blob)) {
466 ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
470 call = talloc_p(ldap_conn, struct ldapsrv_call);
472 ldapsrv_terminate_connection(ldap_conn, "no memory");
477 call->state = LDAPSRV_CALL_STATE_NEW;
478 call->conn = ldap_conn;
479 call->request.mem_ctx = call;
481 if (!ldap_decode(&data, &call->request)) {
482 dump_data(0,buf, msg_length);
484 ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
490 DLIST_ADD_END(ldap_conn->calls, call,
491 struct ldapsrv_call *);
493 ldapsrv_consumed_from_buf(&ldap_conn->in_buffer, msg_length);
495 status = ldapsrv_do_call(call);
496 if (!NT_STATUS_IS_OK(status)) {
497 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
501 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
504 status = ldapsrv_do_responses(ldap_conn);
505 if (!NT_STATUS_IS_OK(status)) {
506 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
510 if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
511 conn->event.fde->flags |= EVENT_FD_WRITE;
518 called when a LDAP socket becomes writable
520 static void ldapsrv_send(struct server_connection *conn, struct timeval t,
523 struct ldapsrv_connection *ldap_conn = conn->connection.private_data;
525 DEBUG(10,("ldapsrv_send\n"));
527 if (!ldapsrv_write_buf(ldap_conn)) {
528 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
532 if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
533 conn->event.fde->flags &= ~EVENT_FD_WRITE;
540 initialise a server_context from a open socket and register a event handler
541 for reading from that socket
543 static void ldapsrv_accept(struct server_connection *conn)
545 struct ldapsrv_connection *ldap_conn;
547 DEBUG(10, ("ldapsrv_accept\n"));
549 ldap_conn = talloc_p(conn, struct ldapsrv_connection);
551 if (ldap_conn == NULL)
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);
558 conn->connection.private_data = ldap_conn;
563 static const struct server_stream_ops ldap_stream_ops = {
566 .accept_connection = ldapsrv_accept,
567 .recv_handler = ldapsrv_recv,
568 .send_handler = ldapsrv_send,
569 .idle_handler = NULL,
570 .close_connection = NULL
573 static const struct server_stream_ops *ldapsrv_get_stream_ops(void)
575 return &ldap_stream_ops;
578 static const struct server_service_ops ldap_server_ops = {
580 .service_init = ldapsrv_init
583 const struct server_service_ops *ldapsrv_get_ops(void)
585 return &ldap_server_ops;
588 NTSTATUS server_service_ldap_init(void)