]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
winbind:varlink: Add varlink service
authorSamuel Cabrero <scabrero@samba.org>
Wed, 1 Feb 2023 16:01:21 +0000 (17:01 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Thu, 20 Feb 2025 08:07:32 +0000 (08:07 +0000)
$> userdbctl services
SERVICE           LISTENING
org.samba.winbind yes

1 services listed.

$> varlink info unix:/run/systemd/userdb/org.samba.winbind
Vendor: Samba
Product: Winbind
Version: 1
URL: https://samba.org
Interfaces:
  org.varlink.service

Signed-off-by: Samuel Cabrero <scabrero@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
source3/winbindd/winbindd.c
source3/winbindd/winbindd_varlink.c [new file with mode: 0644]
source3/winbindd/winbindd_varlink.h [new file with mode: 0644]
source3/winbindd/wscript_build

index 605afd2327812444c02a83f3bc2e1c7f5c804c3c..6489197688269df1853da7972b8e0f690e45fe18 100644 (file)
 #include "winbindd_traceid.h"
 #include "lib/util/util_process.h"
 
+#if defined(WITH_SYSTEMD_USERDB)
+#include "winbindd_varlink.h"
+#endif
+
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
@@ -1080,6 +1084,16 @@ static bool winbindd_setup_listeners(void)
        }
        tevent_fd_set_auto_close(fde);
 
+#if defined(WITH_SYSTEMD_USERDB)
+       if (lp_winbind_varlink_service()) {
+               /* Setup varlink socket */
+               if (!winbind_setup_varlink(global_event_context(),
+                                          global_event_context())) {
+                       goto failed;
+               }
+       }
+#endif
+
        winbindd_scrub_clients_handler(global_event_context(), NULL,
                                       timeval_current(), NULL);
        return true;
diff --git a/source3/winbindd/winbindd_varlink.c b/source3/winbindd/winbindd_varlink.c
new file mode 100644 (file)
index 0000000..ccde59e
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Copyright (C) Samuel Cabrero <scabrero@samba.org> 2023
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "winbindd_varlink.h"
+
+#define WB_VL_SOCKET_DIR  "/run/systemd/userdb"
+
+struct wb_vl_state {
+       VarlinkService *service;
+       struct tevent_context *ev_ctx;
+       struct tevent_fd *fde;
+       int fd;
+};
+
+
+static void varlink_listen_fde_handler(struct tevent_context *ev,
+                                      struct tevent_fd *fde,
+                                      uint16_t flags,
+                                      void *private_data)
+{
+       struct wb_vl_state *s = talloc_get_type_abort(
+                       private_data, struct wb_vl_state);
+       long rc;
+
+       rc = varlink_service_process_events(s->service);
+       if (rc < 0) {
+               DBG_WARNING("Failed to process events: %s\n",
+                           varlink_error_string(rc));
+       }
+}
+
+static int wb_vl_state_destructor(struct wb_vl_state *s)
+{
+       if (s->service != NULL) {
+               s->service = varlink_service_free(s->service);
+       }
+       if (s->service != NULL) {
+               DBG_WARNING("Failed to free Varlink service\n");
+       }
+       return 0;
+}
+
+bool winbind_setup_varlink(TALLOC_CTX *mem_ctx,
+                          struct tevent_context *ev_ctx)
+{
+       struct wb_vl_state *state = NULL;
+       const char *socket_dir = NULL;
+       const char *socket_name = NULL;
+       char *uri = NULL;
+       long rc;
+
+       state = talloc_zero(mem_ctx, struct wb_vl_state);
+       if (state == NULL) {
+               DBG_ERR("No memory");
+               goto fail;
+       }
+       talloc_set_destructor(state, wb_vl_state_destructor);
+
+       state->ev_ctx = ev_ctx;
+
+       socket_dir = lp_parm_const_string(-1,
+                                         "winbind varlink",
+                                         "socket directory",
+                                         WB_VL_SOCKET_DIR);
+
+       socket_name = lp_parm_const_string(-1,
+                                          "winbind varlink",
+                                          "service name",
+                                          WB_VL_SERVICE_NAME);
+
+       uri = talloc_asprintf(state, "unix:%s/%s", socket_dir, socket_name);
+
+       rc = varlink_service_new(&state->service,
+                                "Samba",
+                                "Winbind",
+                                "1",
+                                "https://samba.org",
+                                uri,
+                                -1);
+       TALLOC_FREE(uri);
+       if (rc < 0) {
+               DBG_ERR("Failed to create Varlink service: %s\n",
+                       varlink_error_string(rc));
+               goto fail;
+       }
+
+       state->fd = varlink_service_get_fd(state->service);
+       if (state->fd < 0) {
+               DBG_ERR("Failed to get varlink fd: %s\n",
+                       varlink_error_string(rc));
+               goto fail;
+       }
+
+       state->fde = tevent_add_fd(state->ev_ctx,
+                                  state,
+                                  state->fd,
+                                  TEVENT_FD_READ,
+                                  varlink_listen_fde_handler,
+                                  state);
+       if (state->fde == NULL) {
+               DBG_ERR("Failed to create tevent fd event handler\n");
+               close(state->fd);
+               goto fail;
+       }
+
+       return true;
+fail:
+       TALLOC_FREE(state);
+       return false;
+}
diff --git a/source3/winbindd/winbindd_varlink.h b/source3/winbindd/winbindd_varlink.h
new file mode 100644 (file)
index 0000000..ed5ca6a
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Copyright (C) Samuel Cabrero <scabrero@samba.org> 2023
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _SOURCE3_WINBIND_VARLINK_H_
+#define _SOURCE3_WINBIND_VARLINK_H_
+
+#include <talloc.h>
+#include <tevent.h>
+#include <varlink.h>
+
+#define WB_VL_SERVICE_NAME "org.samba.winbind"
+
+bool winbind_setup_varlink(TALLOC_CTX *mem_ctx, struct tevent_context *ev_ctx);
+
+#endif /* _SOURCE3_WINBIND_VARLINK_H_ */
index 03aa6e5c06052c2c74408f8b1badb00e1cd91568..ee331c75976b2e6253ee33b010b5ce546983d155 100644 (file)
@@ -171,6 +171,13 @@ bld.SAMBA3_MODULE('idmap_script',
                  internal_module=bld.SAMBA3_IS_STATIC_MODULE('idmap_script'),
                  enabled=bld.SAMBA3_IS_ENABLED_MODULE('idmap_script'))
 
+bld.SAMBA3_SUBSYSTEM('VARLINK',
+                     source='''
+                            winbindd_varlink.c
+                            ''',
+                     deps='talloc tevent varlink',
+                     enabled=bld.env.with_systemd_userdb)
+
 bld.SAMBA3_SUBSYSTEM('winbindd-lib',
                     source='''
                     winbindd_group.c
@@ -274,6 +281,7 @@ bld.SAMBA3_SUBSYSTEM('winbindd-lib',
                     TDB_VALIDATE
                     MESSAGING
                     LIBLSA
+                    VARLINK
                     ''')
 
 bld.SAMBA3_BINARY('winbindd',