2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Volker Lendecke 2004
8 Copyright (C) Stefan Metzmacher 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/events/events.h"
26 #include "auth/auth.h"
27 #include "auth/credentials/credentials.h"
28 #include "librpc/gen_ndr/ndr_samr.h"
29 #include "../lib/util/dlinklist.h"
30 #include "../lib/util/asn1.h"
31 #include "ldap_server/ldap_server.h"
32 #include "smbd/service_task.h"
33 #include "smbd/service_stream.h"
34 #include "smbd/service.h"
35 #include "smbd/process_model.h"
36 #include "lib/tls/tls.h"
37 #include "lib/messaging/irpc.h"
38 #include "lib/ldb/include/ldb.h"
39 #include "lib/ldb/include/ldb_errors.h"
40 #include "libcli/ldap/ldap_proto.h"
41 #include "system/network.h"
42 #include "lib/socket/netif.h"
43 #include "dsdb/samdb/samdb.h"
44 #include "param/param.h"
46 close the socket and shutdown a server_context
48 void ldapsrv_terminate_connection(struct ldapsrv_connection *conn,
51 packet_recv_disable(conn->packet);
52 TALLOC_FREE(conn->packet);
53 TALLOC_FREE(conn->sockets.tls);
54 stream_terminate_connection(conn->connection, reason);
60 static void ldapsrv_error_handler(void *private_data, NTSTATUS status)
62 struct ldapsrv_connection *conn = talloc_get_type(private_data,
63 struct ldapsrv_connection);
64 ldapsrv_terminate_connection(conn, nt_errstr(status));
68 process a decoded ldap message
70 static void ldapsrv_process_message(struct ldapsrv_connection *conn,
71 struct ldap_message *msg)
73 struct ldapsrv_call *call;
77 call = talloc(conn, struct ldapsrv_call);
79 ldapsrv_terminate_connection(conn, "no memory");
83 call->request = talloc_steal(call, msg);
86 call->send_callback = NULL;
87 call->send_private = NULL;
90 status = ldapsrv_do_call(call);
91 if (!NT_STATUS_IS_OK(status)) {
96 blob = data_blob(NULL, 0);
98 if (call->replies == NULL) {
103 /* build all the replies into a single blob */
104 while (call->replies) {
108 msg = call->replies->msg;
109 if (!ldap_encode(msg, samba_ldap_control_handlers(), &b, call)) {
110 DEBUG(0,("Failed to encode ldap reply of type %d\n", msg->type));
115 ret = data_blob_append(call, &blob, b.data, b.length);
118 talloc_set_name_const(blob.data, "Outgoing, encoded LDAP packet");
125 DLIST_REMOVE(call->replies, call->replies);
128 packet_send_callback(conn->packet, blob,
129 call->send_callback, call->send_private);
135 disable packets on other sockets while processing this one
137 static void ldapsrv_disable_recv(struct ldapsrv_connection *conn)
139 struct ldapsrv_packet_interfaces *p;
140 for (p=conn->service->packet_interfaces; p; p=p->next) {
141 if (p->packet != conn->packet) {
142 packet_recv_disable(p->packet);
148 disable packets on other sockets while processing this one
150 static void ldapsrv_enable_recv(struct ldapsrv_connection *conn)
152 struct ldapsrv_packet_interfaces *p;
153 for (p=conn->service->packet_interfaces; p; p=p->next) {
154 if (p->packet != conn->packet) {
155 packet_recv_enable(p->packet);
163 static NTSTATUS ldapsrv_decode(void *private_data, DATA_BLOB blob)
166 struct ldapsrv_connection *conn = talloc_get_type(private_data,
167 struct ldapsrv_connection);
168 struct asn1_data *asn1 = asn1_init(conn);
169 struct ldap_message *msg = talloc(conn, struct ldap_message);
171 if (asn1 == NULL || msg == NULL) {
172 return NT_STATUS_NO_MEMORY;
175 if (!asn1_load(asn1, blob)) {
178 return NT_STATUS_NO_MEMORY;
181 status = ldap_decode(asn1, samba_ldap_control_handlers(), msg);
182 if (!NT_STATUS_IS_OK(status)) {
187 data_blob_free(&blob);
188 talloc_steal(conn, msg);
191 /* disable messages on other sockets while processing this one */
192 ldapsrv_disable_recv(conn);
194 ldapsrv_process_message(conn, msg);
196 ldapsrv_enable_recv(conn);
204 static void ldapsrv_conn_idle_timeout(struct tevent_context *ev,
205 struct tevent_timer *te,
209 struct ldapsrv_connection *conn = talloc_get_type(private_data, struct ldapsrv_connection);
211 ldapsrv_terminate_connection(conn, "Timeout. No requests after bind");
215 called when a LDAP socket becomes readable
217 void ldapsrv_recv(struct stream_connection *c, uint16_t flags)
219 struct ldapsrv_connection *conn =
220 talloc_get_type(c->private_data, struct ldapsrv_connection);
222 if (conn->limits.ite) { /* clean initial timeout if any */
223 talloc_free(conn->limits.ite);
224 conn->limits.ite = NULL;
227 if (conn->limits.te) { /* clean idle timeout if any */
228 talloc_free(conn->limits.te);
229 conn->limits.te = NULL;
232 packet_recv(conn->packet);
234 /* set idle timeout */
235 conn->limits.te = event_add_timed(c->event.ctx, conn,
236 timeval_current_ofs(conn->limits.conn_idle_time, 0),
237 ldapsrv_conn_idle_timeout, conn);
241 called when a LDAP socket becomes writable
243 static void ldapsrv_send(struct stream_connection *c, uint16_t flags)
245 struct ldapsrv_connection *conn =
246 talloc_get_type(c->private_data, struct ldapsrv_connection);
248 packet_queue_run(conn->packet);
251 static void ldapsrv_conn_init_timeout(struct tevent_context *ev,
252 struct tevent_timer *te,
256 struct ldapsrv_connection *conn = talloc_get_type(private_data, struct ldapsrv_connection);
258 ldapsrv_terminate_connection(conn, "Timeout. No requests after initial connection");
261 static int ldapsrv_load_limits(struct ldapsrv_connection *conn)
264 const char *attrs[] = { "configurationNamingContext", NULL };
265 const char *attrs2[] = { "lDAPAdminLimits", NULL };
266 struct ldb_message_element *el;
267 struct ldb_result *res = NULL;
268 struct ldb_dn *basedn;
269 struct ldb_dn *conf_dn;
270 struct ldb_dn *policy_dn;
274 /* set defaults limits in case of failure */
275 conn->limits.initial_timeout = 120;
276 conn->limits.conn_idle_time = 900;
277 conn->limits.max_page_size = 1000;
278 conn->limits.search_timeout = 120;
281 tmp_ctx = talloc_new(conn);
282 if (tmp_ctx == NULL) {
286 basedn = ldb_dn_new(tmp_ctx, conn->ldb, NULL);
287 if ( ! ldb_dn_validate(basedn)) {
291 ret = ldb_search(conn->ldb, tmp_ctx, &res, basedn, LDB_SCOPE_BASE, attrs, NULL);
292 if (ret != LDB_SUCCESS) {
296 if (res->count != 1) {
300 conf_dn = ldb_msg_find_attr_as_dn(conn->ldb, tmp_ctx, res->msgs[0], "configurationNamingContext");
301 if (conf_dn == NULL) {
305 policy_dn = ldb_dn_copy(tmp_ctx, conf_dn);
306 ldb_dn_add_child_fmt(policy_dn, "CN=Default Query Policy,CN=Query-Policies,CN=Directory Service,CN=Windows NT,CN=Services");
307 if (policy_dn == NULL) {
311 ret = ldb_search(conn->ldb, tmp_ctx, &res, policy_dn, LDB_SCOPE_BASE, attrs2, NULL);
312 if (ret != LDB_SUCCESS) {
316 if (res->count != 1) {
320 el = ldb_msg_find_element(res->msgs[0], "lDAPAdminLimits");
325 for (i = 0; i < el->num_values; i++) {
326 char policy_name[256];
329 s = sscanf((const char *)el->values[i].data, "%255[^=]=%d", policy_name, &policy_value);
330 if (ret != 2 || policy_value == 0)
333 if (strcasecmp("InitRecvTimeout", policy_name) == 0) {
334 conn->limits.initial_timeout = policy_value;
337 if (strcasecmp("MaxConnIdleTime", policy_name) == 0) {
338 conn->limits.conn_idle_time = policy_value;
341 if (strcasecmp("MaxPageSize", policy_name) == 0) {
342 conn->limits.max_page_size = policy_value;
345 if (strcasecmp("MaxQueryDuration", policy_name) == 0) {
346 conn->limits.search_timeout = policy_value;
354 DEBUG(0, ("Failed to load ldap server query policies\n"));
355 talloc_free(tmp_ctx);
360 remove a packet interface from the service level list
362 static int packet_interface_destructor(struct ldapsrv_packet_interfaces *packet_interface)
364 DLIST_REMOVE(packet_interface->service->packet_interfaces, packet_interface);
369 initialise a server_context from a open socket and register a event handler
370 for reading from that socket
372 static void ldapsrv_accept(struct stream_connection *c,
373 struct auth_session_info *session_info)
375 struct ldapsrv_service *ldapsrv_service =
376 talloc_get_type(c->private_data, struct ldapsrv_service);
377 struct ldapsrv_connection *conn;
378 struct cli_credentials *server_credentials;
379 struct socket_address *socket_address;
383 conn = talloc_zero(c, struct ldapsrv_connection);
385 stream_terminate_connection(c, "ldapsrv_accept: out of memory");
390 conn->connection = c;
391 conn->service = ldapsrv_service;
392 conn->sockets.raw = c->socket;
393 conn->lp_ctx = ldapsrv_service->task->lp_ctx;
395 c->private_data = conn;
397 socket_address = socket_get_my_addr(c->socket, conn);
398 if (!socket_address) {
399 ldapsrv_terminate_connection(conn, "ldapsrv_accept: failed to obtain local socket address!");
402 port = socket_address->port;
403 talloc_free(socket_address);
406 struct socket_context *tls_socket = tls_init_server(ldapsrv_service->tls_params, c->socket,
409 ldapsrv_terminate_connection(conn, "ldapsrv_accept: tls_init_server() failed");
412 talloc_steal(c, tls_socket);
413 c->socket = tls_socket;
414 conn->sockets.tls = tls_socket;
416 } else if (port == 3268) /* Global catalog */ {
417 conn->global_catalog = true;
419 conn->packet = packet_init(conn);
420 if (conn->packet == NULL) {
421 ldapsrv_terminate_connection(conn, "out of memory");
425 packet_set_private(conn->packet, conn);
426 packet_set_socket(conn->packet, c->socket);
427 packet_set_callback(conn->packet, ldapsrv_decode);
428 packet_set_full_request(conn->packet, ldap_full_packet);
429 packet_set_error_handler(conn->packet, ldapsrv_error_handler);
430 packet_set_event_context(conn->packet, c->event.ctx);
431 packet_set_fde(conn->packet, c->event.fde);
432 packet_set_serialise(conn->packet);
434 if (conn->sockets.tls) {
435 packet_set_unreliable_select(conn->packet);
438 /* Ensure we don't get packets until the database is ready below */
439 packet_recv_disable(conn->packet);
441 /* add to the service level list of packet interfaces, to
442 * allow us to serialise between connections
444 conn->packet_interface = talloc(conn, struct ldapsrv_packet_interfaces);
445 if (conn->packet_interface == NULL) {
446 ldapsrv_terminate_connection(conn, "out of memory");
448 conn->packet_interface->service = ldapsrv_service;
449 conn->packet_interface->packet = conn->packet;
450 DLIST_ADD(conn->service->packet_interfaces, conn->packet_interface);
451 talloc_set_destructor(conn->packet_interface, packet_interface_destructor);
453 server_credentials = cli_credentials_init(conn);
454 if (!server_credentials) {
455 stream_terminate_connection(c, "Failed to init server credentials\n");
459 cli_credentials_set_conf(server_credentials, conn->lp_ctx);
460 status = cli_credentials_set_machine_account(server_credentials, conn->lp_ctx);
461 if (!NT_STATUS_IS_OK(status)) {
462 stream_terminate_connection(c, talloc_asprintf(conn, "Failed to obtain server credentials, perhaps a standalone server?: %s\n", nt_errstr(status)));
465 conn->server_credentials = server_credentials;
467 conn->session_info = talloc_move(conn, &session_info);
469 if (!NT_STATUS_IS_OK(ldapsrv_backend_Init(conn))) {
470 ldapsrv_terminate_connection(conn, "backend Init failed");
474 /* load limits from the conf partition */
475 ldapsrv_load_limits(conn); /* should we fail on error ? */
477 /* register the server */
478 irpc_add_name(c->msg_ctx, "ldap_server");
480 /* set connections limits */
481 conn->limits.ite = event_add_timed(c->event.ctx, conn,
482 timeval_current_ofs(conn->limits.initial_timeout, 0),
483 ldapsrv_conn_init_timeout, conn);
485 packet_recv_enable(conn->packet);
489 static void ldapsrv_accept_nonpriv(struct stream_connection *c)
491 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
492 c->private_data, struct ldapsrv_service);
493 struct auth_session_info *session_info;
496 status = auth_anonymous_session_info(
497 c, ldapsrv_service->task->lp_ctx, &session_info);
498 if (!NT_STATUS_IS_OK(status)) {
499 stream_terminate_connection(c, "failed to setup anonymous "
503 ldapsrv_accept(c, session_info);
506 static const struct stream_server_ops ldap_stream_nonpriv_ops = {
508 .accept_connection = ldapsrv_accept_nonpriv,
509 .recv_handler = ldapsrv_recv,
510 .send_handler = ldapsrv_send,
513 /* The feature removed behind an #ifdef until we can do it properly
514 * with an EXTERNAL bind. */
516 #define WITH_LDAPI_PRIV_SOCKET
518 #ifdef WITH_LDAPI_PRIV_SOCKET
519 static void ldapsrv_accept_priv(struct stream_connection *c)
521 struct ldapsrv_service *ldapsrv_service = talloc_get_type_abort(
522 c->private_data, struct ldapsrv_service);
523 struct auth_session_info *session_info;
525 session_info = system_session(ldapsrv_service->task->lp_ctx);
527 stream_terminate_connection(c, "failed to setup system "
531 ldapsrv_accept(c, session_info);
534 static const struct stream_server_ops ldap_stream_priv_ops = {
536 .accept_connection = ldapsrv_accept_priv,
537 .recv_handler = ldapsrv_recv,
538 .send_handler = ldapsrv_send,
543 add a socket address to the list of events, one event per port
545 static NTSTATUS add_socket(struct tevent_context *event_context,
546 struct loadparm_context *lp_ctx,
547 const struct model_ops *model_ops,
548 const char *address, struct ldapsrv_service *ldap_service)
552 struct ldb_context *ldb;
554 status = stream_setup_socket(event_context, lp_ctx,
555 model_ops, &ldap_stream_nonpriv_ops,
556 "ipv4", address, &port,
557 lpcfg_socket_options(lp_ctx),
559 if (!NT_STATUS_IS_OK(status)) {
560 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
561 address, port, nt_errstr(status)));
565 if (tls_support(ldap_service->tls_params)) {
566 /* add ldaps server */
568 status = stream_setup_socket(event_context, lp_ctx,
570 &ldap_stream_nonpriv_ops,
571 "ipv4", address, &port,
572 lpcfg_socket_options(lp_ctx),
574 if (!NT_STATUS_IS_OK(status)) {
575 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
576 address, port, nt_errstr(status)));
581 /* Load LDAP database, but only to read our settings */
582 ldb = samdb_connect(ldap_service, ldap_service->task->event_ctx,
583 lp_ctx, system_session(lp_ctx));
585 return NT_STATUS_INTERNAL_DB_CORRUPTION;
588 if (samdb_is_gc(ldb)) {
590 status = stream_setup_socket(event_context, lp_ctx,
592 &ldap_stream_nonpriv_ops,
593 "ipv4", address, &port,
594 lpcfg_socket_options(lp_ctx),
596 if (!NT_STATUS_IS_OK(status)) {
597 DEBUG(0,("ldapsrv failed to bind to %s:%u - %s\n",
598 address, port, nt_errstr(status)));
603 /* And once we are bound, free the tempoary ldb, it will
604 * connect again on each incoming LDAP connection */
605 talloc_unlink(ldap_service, ldb);
611 open the ldap server sockets
613 static void ldapsrv_task_init(struct task_server *task)
616 #ifdef WITH_LDAPI_PRIV_SOCKET
619 struct ldapsrv_service *ldap_service;
621 const struct model_ops *model_ops;
623 switch (lpcfg_server_role(task->lp_ctx)) {
624 case ROLE_STANDALONE:
625 task_server_terminate(task, "ldap_server: no LDAP server required in standalone configuration",
628 case ROLE_DOMAIN_MEMBER:
629 task_server_terminate(task, "ldap_server: no LDAP server required in member server configuration",
632 case ROLE_DOMAIN_CONTROLLER:
633 /* Yes, we want an LDAP server */
637 task_server_set_title(task, "task[ldapsrv]");
639 /* run the ldap server as a single process */
640 model_ops = process_model_startup(task->event_ctx, "single");
641 if (!model_ops) goto failed;
643 ldap_service = talloc_zero(task, struct ldapsrv_service);
644 if (ldap_service == NULL) goto failed;
646 ldap_service->task = task;
648 ldap_service->tls_params = tls_initialise(ldap_service, task->lp_ctx);
649 if (ldap_service->tls_params == NULL) goto failed;
651 if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
652 struct interface *ifaces;
656 load_interfaces(task, lpcfg_interfaces(task->lp_ctx), &ifaces);
657 num_interfaces = iface_count(ifaces);
659 /* We have been given an interfaces line, and been
660 told to only bind to those interfaces. Create a
661 socket per interface and bind to only these.
663 for(i = 0; i < num_interfaces; i++) {
664 const char *address = iface_n_ip(ifaces, i);
665 status = add_socket(task->event_ctx, task->lp_ctx, model_ops, address, ldap_service);
666 if (!NT_STATUS_IS_OK(status)) goto failed;
669 status = add_socket(task->event_ctx, task->lp_ctx, model_ops,
670 lpcfg_socket_address(task->lp_ctx), ldap_service);
671 if (!NT_STATUS_IS_OK(status)) goto failed;
674 ldapi_path = private_path(ldap_service, task->lp_ctx, "ldapi");
679 status = stream_setup_socket(task->event_ctx, task->lp_ctx,
680 model_ops, &ldap_stream_nonpriv_ops,
681 "unix", ldapi_path, NULL,
682 lpcfg_socket_options(task->lp_ctx),
684 talloc_free(ldapi_path);
685 if (!NT_STATUS_IS_OK(status)) {
686 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
687 ldapi_path, nt_errstr(status)));
690 #ifdef WITH_LDAPI_PRIV_SOCKET
691 priv_dir = private_path(ldap_service, task->lp_ctx, "ldap_priv");
692 if (priv_dir == NULL) {
696 * Make sure the directory for the privileged ldapi socket exists, and
697 * is of the correct permissions
699 if (!directory_create_or_exist(priv_dir, geteuid(), 0750)) {
700 task_server_terminate(task, "Cannot create ldap "
701 "privileged ldapi directory", true);
704 ldapi_path = talloc_asprintf(ldap_service, "%s/ldapi", priv_dir);
705 talloc_free(priv_dir);
706 if (ldapi_path == NULL) {
710 status = stream_setup_socket(task->event_ctx, task->lp_ctx,
711 model_ops, &ldap_stream_priv_ops,
712 "unix", ldapi_path, NULL,
713 lpcfg_socket_options(task->lp_ctx),
715 talloc_free(ldapi_path);
716 if (!NT_STATUS_IS_OK(status)) {
717 DEBUG(0,("ldapsrv failed to bind to %s - %s\n",
718 ldapi_path, nt_errstr(status)));
725 task_server_terminate(task, "Failed to startup ldap server task", true);
729 NTSTATUS server_service_ldap_init(void)
731 return register_server_service("ldap", ldapsrv_task_init);