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);
38 add a socket address to the list of events, one event per port
40 static void add_socket(struct server_service *service,
41 const struct model_ops *model_ops,
42 struct ipv4_addr *ifip)
44 struct server_socket *srv_sock;
46 char *ip_str = talloc_strdup(service, sys_inet_ntoa(*ifip));
48 srv_sock = service_setup_socket(service, model_ops, "ipv4", ip_str, &port);
51 srv_sock = service_setup_socket(service, model_ops, "ipv4", ip_str, &port);
56 /****************************************************************************
57 Open the socket communication.
58 ****************************************************************************/
59 static void ldapsrv_init(struct server_service *service,
60 const struct model_ops *model_ops)
62 struct ldapsrv_service *ldap_service;
63 struct ldapsrv_partition *part;
65 DEBUG(10,("ldapsrv_init\n"));
67 ldap_service = talloc_p(service, struct ldapsrv_service);
69 DEBUG(0,("talloc_p(service, struct ldapsrv_service) failed\n"));
72 ZERO_STRUCTP(ldap_service);
74 part = talloc_p(ldap_service, struct ldapsrv_partition);
76 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
79 part->base_dn = ""; /* RootDSE */
80 part->ops = ldapsrv_get_rootdse_partition_ops();
82 ldap_service->rootDSE = part;
83 DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
85 part = talloc_p(ldap_service, struct ldapsrv_partition);
87 DEBUG(0,("talloc_p(ldap_service, struct ldapsrv_partition) failed\n"));
90 part->base_dn = "*"; /* default partition */
91 if (lp_parm_bool(-1, "ldapsrv", "hacked", False)) {
92 part->ops = ldapsrv_get_hldb_partition_ops();
94 part->ops = ldapsrv_get_sldb_partition_ops();
97 ldap_service->default_partition = part;
98 DLIST_ADD_END(ldap_service->partitions, part, struct ldapsrv_partition *);
100 service->private_data = ldap_service;
102 if (lp_interfaces() && lp_bind_interfaces_only()) {
103 int num_interfaces = iface_count();
106 /* We have been given an interfaces line, and been
107 told to only bind to those interfaces. Create a
108 socket per interface and bind to only these.
110 for(i = 0; i < num_interfaces; i++) {
111 struct ipv4_addr *ifip = iface_n_ip(i);
114 DEBUG(0,("ldapsrv_init: interface %d has NULL "
115 "IP address !\n", i));
119 add_socket(service, model_ops, ifip);
122 struct ipv4_addr ifip;
124 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
125 ifip = interpret_addr2(lp_socket_address());
126 add_socket(service, model_ops, &ifip);
130 /* This rw-buf api is made to avoid memcpy. For now do that like mad... The
131 idea is to write into a circular list of buffers where the ideal case is
132 that a read(2) holds a complete request that is then thrown away
135 void ldapsrv_consumed_from_buf(struct rw_buffer *buf,
138 memmove(buf->data, buf->data+length, buf->length-length);
139 buf->length -= length;
142 static void peek_into_read_buf(struct rw_buffer *buf, uint8_t **out,
146 *out_length = buf->length;
149 BOOL ldapsrv_append_to_buf(struct rw_buffer *buf, uint8_t *data, size_t length)
151 buf->data = realloc(buf->data, buf->length+length);
153 if (buf->data == NULL)
156 memcpy(buf->data+buf->length, data, length);
158 buf->length += length;
162 static BOOL read_into_buf(struct socket_context *sock, struct rw_buffer *buf)
169 tmp_blob = data_blob_talloc(sock, NULL, 1024);
170 if (tmp_blob.data == NULL) {
174 status = socket_recv(sock, tmp_blob.data, tmp_blob.length, &nread, 0);
175 if (NT_STATUS_IS_ERR(status)) {
176 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
177 talloc_free(tmp_blob.data);
180 tmp_blob.length = nread;
182 ret = ldapsrv_append_to_buf(buf, tmp_blob.data, tmp_blob.length);
184 talloc_free(tmp_blob.data);
189 static BOOL ldapsrv_read_buf(struct ldapsrv_connection *conn)
197 size_t buf_length, sasl_length;
198 struct socket_context *sock = conn->connection->socket;
203 return read_into_buf(sock, &conn->in_buffer);
205 if (!conn->session_info) {
206 return read_into_buf(sock, &conn->in_buffer);
208 if (!(gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
209 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL))) {
210 return read_into_buf(sock, &conn->in_buffer);
213 mem_ctx = talloc_new(conn);
215 DEBUG(0,("no memory\n"));
219 tmp_blob = data_blob_talloc(mem_ctx, NULL, 1024);
220 if (tmp_blob.data == NULL) {
221 talloc_free(mem_ctx);
225 status = socket_recv(sock, tmp_blob.data, tmp_blob.length, &nread, 0);
226 if (NT_STATUS_IS_ERR(status)) {
227 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
228 talloc_free(mem_ctx);
231 tmp_blob.length = nread;
233 ret = ldapsrv_append_to_buf(&conn->sasl_in_buffer, tmp_blob.data, tmp_blob.length);
235 talloc_free(mem_ctx);
239 peek_into_read_buf(&conn->sasl_in_buffer, &buf, &buf_length);
241 if (buf_length < 4) {
243 talloc_free(mem_ctx);
247 sasl_length = RIVAL(buf, 0);
249 if ((buf_length - 4) < sasl_length) {
251 talloc_free(mem_ctx);
255 wrapped.data = buf + 4;
256 wrapped.length = sasl_length;
258 status = gensec_unwrap(conn->gensec, mem_ctx,
261 if (!NT_STATUS_IS_OK(status)) {
262 DEBUG(0,("gensec_unwrap: %s\n",nt_errstr(status)));
263 talloc_free(mem_ctx);
267 ret = ldapsrv_append_to_buf(&conn->in_buffer, unwrapped.data, unwrapped.length);
269 talloc_free(mem_ctx);
273 ldapsrv_consumed_from_buf(&conn->sasl_in_buffer, 4 + sasl_length);
275 talloc_free(mem_ctx);
279 static BOOL write_from_buf(struct socket_context *sock, struct rw_buffer *buf)
285 tmp_blob.data = buf->data;
286 tmp_blob.length = buf->length;
288 status = socket_send(sock, &tmp_blob, &sendlen, 0);
289 if (!NT_STATUS_IS_OK(status)) {
290 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
294 ldapsrv_consumed_from_buf(buf, sendlen);
299 static BOOL ldapsrv_write_buf(struct ldapsrv_connection *conn)
307 struct socket_context *sock = conn->connection->socket;
312 return write_from_buf(sock, &conn->out_buffer);
314 if (!conn->session_info) {
315 return write_from_buf(sock, &conn->out_buffer);
317 if (!(gensec_have_feature(conn->gensec, GENSEC_FEATURE_SIGN) ||
318 gensec_have_feature(conn->gensec, GENSEC_FEATURE_SEAL))) {
319 return write_from_buf(sock, &conn->out_buffer);
322 mem_ctx = talloc_new(conn);
324 DEBUG(0,("no memory\n"));
328 if (conn->out_buffer.length == 0) {
332 tmp_blob.data = conn->out_buffer.data;
333 tmp_blob.length = conn->out_buffer.length;
334 status = gensec_wrap(conn->gensec, mem_ctx,
337 if (!NT_STATUS_IS_OK(status)) {
338 DEBUG(0,("gensec_wrap: %s\n",nt_errstr(status)));
339 talloc_free(mem_ctx);
343 sasl = data_blob_talloc(mem_ctx, NULL, 4 + wrapped.length);
345 DEBUG(0,("no memory\n"));
346 talloc_free(mem_ctx);
350 RSIVAL(sasl.data, 0, wrapped.length);
351 memcpy(sasl.data + 4, wrapped.data, wrapped.length);
353 ret = ldapsrv_append_to_buf(&conn->sasl_out_buffer, sasl.data, sasl.length);
355 talloc_free(mem_ctx);
358 ldapsrv_consumed_from_buf(&conn->out_buffer, conn->out_buffer.length);
360 tmp_blob.data = conn->sasl_out_buffer.data;
361 tmp_blob.length = conn->sasl_out_buffer.length;
363 status = socket_send(sock, &tmp_blob, &sendlen, 0);
364 if (!NT_STATUS_IS_OK(status)) {
365 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
366 talloc_free(mem_ctx);
370 ldapsrv_consumed_from_buf(&conn->sasl_out_buffer, sendlen);
372 talloc_free(mem_ctx);
377 static BOOL ldap_encode_to_buf(struct ldap_message *msg, struct rw_buffer *buf)
382 if (!ldap_encode(msg, &blob))
385 res = ldapsrv_append_to_buf(buf, blob.data, blob.length);
387 data_blob_free(&blob);
391 NTSTATUS ldapsrv_do_responses(struct ldapsrv_connection *conn)
393 struct ldapsrv_call *call, *next_call = NULL;
394 struct ldapsrv_reply *reply, *next_reply = NULL;
396 for (call=conn->calls; call; call=next_call) {
397 for (reply=call->replies; reply; reply=next_reply) {
398 if (!ldap_encode_to_buf(&reply->msg, &conn->out_buffer)) {
399 return NT_STATUS_FOOBAR;
401 next_reply = reply->next;
402 DLIST_REMOVE(call->replies, reply);
403 reply->state = LDAPSRV_REPLY_STATE_SEND;
406 next_call = call->next;
407 DLIST_REMOVE(conn->calls, call);
408 call->state = LDAPSRV_CALL_STATE_COMPLETE;
415 NTSTATUS ldapsrv_flush_responses(struct ldapsrv_connection *conn)
421 called when a LDAP socket becomes readable
423 static void ldapsrv_recv(struct server_connection *conn, struct timeval t,
426 struct ldapsrv_connection *ldap_conn = conn->private_data;
428 size_t buf_length, msg_length;
430 struct asn1_data data;
431 struct ldapsrv_call *call;
434 DEBUG(10,("ldapsrv_recv\n"));
436 if (!ldapsrv_read_buf(ldap_conn)) {
437 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_read_buf() failed");
441 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
443 while (buf_length > 0) {
444 /* LDAP Messages are always SEQUENCES */
446 if (!asn1_object_length(buf, buf_length, ASN1_SEQUENCE(0),
448 ldapsrv_terminate_connection(ldap_conn, "asn1_object_length() failed");
452 if (buf_length < msg_length) {
457 /* We've got a complete LDAP request in the in-buffer, convert
458 * that to a ldap_message and put it into the incoming
462 blob.length = msg_length;
464 if (!asn1_load(&data, blob)) {
465 ldapsrv_terminate_connection(ldap_conn, "asn1_load() failed");
469 call = talloc_p(ldap_conn, struct ldapsrv_call);
471 ldapsrv_terminate_connection(ldap_conn, "no memory");
476 call->state = LDAPSRV_CALL_STATE_NEW;
477 call->conn = ldap_conn;
478 call->request.mem_ctx = call;
480 if (!ldap_decode(&data, &call->request)) {
481 dump_data(0,buf, msg_length);
483 ldapsrv_terminate_connection(ldap_conn, "ldap_decode() failed");
489 DLIST_ADD_END(ldap_conn->calls, call,
490 struct ldapsrv_call *);
492 ldapsrv_consumed_from_buf(&ldap_conn->in_buffer, msg_length);
494 status = ldapsrv_do_call(call);
495 if (!NT_STATUS_IS_OK(status)) {
496 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_call() failed");
500 peek_into_read_buf(&ldap_conn->in_buffer, &buf, &buf_length);
503 status = ldapsrv_do_responses(ldap_conn);
504 if (!NT_STATUS_IS_OK(status)) {
505 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_do_responses() failed");
509 if ((ldap_conn->out_buffer.length > 0)||(ldap_conn->sasl_out_buffer.length > 0)) {
510 conn->event.fde->flags |= EVENT_FD_WRITE;
517 called when a LDAP socket becomes writable
519 static void ldapsrv_send(struct server_connection *conn, struct timeval t,
522 struct ldapsrv_connection *ldap_conn = conn->private_data;
524 DEBUG(10,("ldapsrv_send\n"));
526 if (!ldapsrv_write_buf(ldap_conn)) {
527 ldapsrv_terminate_connection(ldap_conn, "ldapsrv_write_buf() failed");
531 if (ldap_conn->out_buffer.length == 0 && ldap_conn->sasl_out_buffer.length == 0) {
532 conn->event.fde->flags &= ~EVENT_FD_WRITE;
539 called when connection is idle
541 static void ldapsrv_idle(struct server_connection *conn, struct timeval t)
543 DEBUG(10,("ldapsrv_idle: not implemented!\n"));
547 static void ldapsrv_close(struct server_connection *conn, const char *reason)
553 initialise a server_context from a open socket and register a event handler
554 for reading from that socket
556 static void ldapsrv_accept(struct server_connection *conn)
558 struct ldapsrv_connection *ldap_conn;
560 DEBUG(10, ("ldapsrv_accept\n"));
562 ldap_conn = talloc_p(conn, struct ldapsrv_connection);
564 if (ldap_conn == NULL)
567 ZERO_STRUCTP(ldap_conn);
568 ldap_conn->connection = conn;
569 ldap_conn->service = talloc_reference(ldap_conn, conn->service->private_data);
571 conn->private_data = ldap_conn;
577 called on a fatal error that should cause this server to terminate
579 static void ldapsrv_exit(struct server_service *service, const char *reason)
581 DEBUG(10,("ldapsrv_exit\n"));
585 static const struct server_service_ops ldap_server_ops = {
587 .service_init = ldapsrv_init,
588 .accept_connection = ldapsrv_accept,
589 .recv_handler = ldapsrv_recv,
590 .send_handler = ldapsrv_send,
591 .idle_handler = ldapsrv_idle,
592 .close_connection = ldapsrv_close,
593 .service_exit = ldapsrv_exit,
596 const struct server_service_ops *ldapsrv_get_ops(void)
598 return &ldap_server_ops;
601 NTSTATUS server_service_ldap_init(void)