s4:ldap_server: don't store task_server in ldapsrv_service
authorJule Anger <janger@samba.org>
Wed, 1 Mar 2023 09:53:53 +0000 (09:53 +0000)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 25 Jul 2023 20:04:29 +0000 (20:04 +0000)
We store individual pointers we need and adjust them
as needed in ldapsrv_post_fork() and the newly added
ldapsrv_before_loop().

This will be required for the next steps.

Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>

Signed-off-by: Jule Anger <janger@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source4/ldap_server/ldap_server.c
source4/ldap_server/ldap_server.h

index 4198caa451a7da9fd4aa23f5f59f4ddfdef9a0d8..63388a55877f9df0f44a74750b4fc000cd756a9c 100644 (file)
@@ -338,7 +338,7 @@ static void ldapsrv_accept(struct stream_connection *c,
 
        conn->connection  = c;
        conn->service     = ldapsrv_service;
-       conn->lp_ctx      = ldapsrv_service->task->lp_ctx;
+       conn->lp_ctx      = ldapsrv_service->lp_ctx;
 
        c->private_data   = conn;
 
@@ -920,7 +920,7 @@ void ldapsrv_notification_retry_setup(struct ldapsrv_service *service, bool forc
        }
 
        service->notification.retry = tevent_wakeup_send(service,
-                                                        service->task->event_ctx,
+                                                        service->current_ev,
                                                         retry);
        if (service->notification.retry == NULL) {
                /* retry later */
@@ -1117,7 +1117,7 @@ static void ldapsrv_accept_nonpriv(struct stream_connection *c)
        NTSTATUS status;
 
        status = auth_anonymous_session_info(
-               c, ldapsrv_service->task->lp_ctx, &session_info);
+               c, ldapsrv_service->lp_ctx, &session_info);
        if (!NT_STATUS_IS_OK(status)) {
                stream_terminate_connection(c, "failed to setup anonymous "
                                            "session info");
@@ -1145,7 +1145,7 @@ static void ldapsrv_accept_priv(struct stream_connection *c)
                c->private_data, struct ldapsrv_service);
        struct auth_session_info *session_info;
 
-       session_info = system_session(ldapsrv_service->task->lp_ctx);
+       session_info = system_session(ldapsrv_service->lp_ctx);
        if (!session_info) {
                stream_terminate_connection(c, "failed to setup system "
                                            "session info");
@@ -1206,7 +1206,7 @@ static NTSTATUS add_socket(struct task_server *task,
 
        /* Load LDAP database, but only to read our settings */
        ldb = samdb_connect(ldap_service,
-                           ldap_service->task->event_ctx,
+                           ldap_service->current_ev,
                            lp_ctx,
                            system_session(lp_ctx),
                            NULL,
@@ -1289,7 +1289,9 @@ static NTSTATUS ldapsrv_task_init(struct task_server *task)
                goto failed;
        }
 
-       ldap_service->task = task;
+       ldap_service->lp_ctx = task->lp_ctx;
+       ldap_service->current_ev = task->event_ctx;
+       ldap_service->current_msg = task->msg_ctx;
 
        dns_host_name = talloc_asprintf(ldap_service, "%s.%s",
                                        lpcfg_netbios_name(task->lp_ctx),
@@ -1437,10 +1439,18 @@ static void ldapsrv_post_fork(struct task_server *task, struct process_details *
        struct ldapsrv_service *ldap_service =
                talloc_get_type_abort(task->private_data, struct ldapsrv_service);
 
+       /*
+        * As ldapsrv_before_loop() may changed the values for the parent loop
+        * we need to adjust the pointers to the correct value in the child
+        */
+       ldap_service->lp_ctx = task->lp_ctx;
+       ldap_service->current_ev = task->event_ctx;
+       ldap_service->current_msg = task->msg_ctx;
+
        ldap_service->sam_ctx = samdb_connect(ldap_service,
-                                             ldap_service->task->event_ctx,
-                                             ldap_service->task->lp_ctx,
-                                             system_session(ldap_service->task->lp_ctx),
+                                             ldap_service->current_ev,
+                                             ldap_service->lp_ctx,
+                                             system_session(ldap_service->lp_ctx),
                                              NULL,
                                              0);
        if (ldap_service->sam_ctx == NULL) {
@@ -1450,6 +1460,30 @@ static void ldapsrv_post_fork(struct task_server *task, struct process_details *
        }
 }
 
+static void ldapsrv_before_loop(struct task_server *task)
+{
+       struct ldapsrv_service *ldap_service =
+               talloc_get_type_abort(task->private_data, struct ldapsrv_service);
+
+       if (ldap_service->sam_ctx != NULL) {
+               /*
+                * Make sure the values are still the same
+                * as set in ldapsrv_post_fork()
+                */
+               SMB_ASSERT(task->lp_ctx == ldap_service->lp_ctx);
+               SMB_ASSERT(task->event_ctx == ldap_service->current_ev);
+               SMB_ASSERT(task->msg_ctx == ldap_service->current_msg);
+       } else {
+               /*
+                * We need to adjust the pointers to the correct value
+                * in the parent loop.
+                */
+               ldap_service->lp_ctx = task->lp_ctx;
+               ldap_service->current_ev = task->event_ctx;
+               ldap_service->current_msg = task->msg_ctx;
+       }
+}
+
 /*
  * Check the size of an ldap request packet.
  *
@@ -1535,6 +1569,7 @@ NTSTATUS server_service_ldap_init(TALLOC_CTX *ctx)
                .inhibit_pre_fork = false,
                .task_init = ldapsrv_task_init,
                .post_fork = ldapsrv_post_fork,
+               .before_loop = ldapsrv_before_loop,
        };
        return register_server_service(ctx, "ldap", &details);
 }
index c94bd914b9b748f934c2e326ff360d668f105cf9..f2feb4f3e2ff58f177cc3df10f012739684acf72 100644 (file)
@@ -115,7 +115,6 @@ struct ldapsrv_call {
 
 struct ldapsrv_service {
        struct tstream_tls_params *tls_params;
-       struct task_server *task;
        struct tevent_queue *call_queue;
        struct ldapsrv_connection *connections;
        struct {
@@ -123,6 +122,9 @@ struct ldapsrv_service {
                struct tevent_req *retry;
        } notification;
 
+       struct loadparm_context *lp_ctx;
+       struct tevent_context *current_ev;
+       struct imessaging_context *current_msg;
        struct ldb_context *sam_ctx;
 };