From: Samuel Cabrero Date: Wed, 1 Feb 2023 16:01:21 +0000 (+0100) Subject: winbind:varlink: Add varlink service X-Git-Tag: tevent-0.17.0~748 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=776eea8090c9edfdf68e64fd840523a98fadbe64;p=thirdparty%2Fsamba.git winbind:varlink: Add varlink service $> 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 Reviewed-by: Andreas Schneider --- diff --git a/source3/winbindd/winbindd.c b/source3/winbindd/winbindd.c index 605afd23278..64891976882 100644 --- a/source3/winbindd/winbindd.c +++ b/source3/winbindd/winbindd.c @@ -55,6 +55,10 @@ #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 index 00000000000..ccde59e953f --- /dev/null +++ b/source3/winbindd/winbindd_varlink.c @@ -0,0 +1,127 @@ +/* + Unix SMB/CIFS implementation. + + Copyright (C) Samuel Cabrero 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 . +*/ + +#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 index 00000000000..ed5ca6a5dd3 --- /dev/null +++ b/source3/winbindd/winbindd_varlink.h @@ -0,0 +1,31 @@ +/* + Unix SMB/CIFS implementation. + + Copyright (C) Samuel Cabrero 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 . +*/ + +#ifndef _SOURCE3_WINBIND_VARLINK_H_ +#define _SOURCE3_WINBIND_VARLINK_H_ + +#include +#include +#include + +#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_ */ diff --git a/source3/winbindd/wscript_build b/source3/winbindd/wscript_build index 03aa6e5c060..ee331c75976 100644 --- a/source3/winbindd/wscript_build +++ b/source3/winbindd/wscript_build @@ -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',