From: Volker Lendecke Date: Wed, 21 Oct 2020 15:28:14 +0000 (+0200) Subject: notifyd: Factor out notify_walk() into its own file X-Git-Tag: talloc-2.3.2~115 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=49859ac11d682e46ec79266395b0abe2083f2cdd;p=thirdparty%2Fsamba.git notifyd: Factor out notify_walk() into its own file To be used in smbtorture, avoid having to include almost all of smbd just for this Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison --- diff --git a/source3/smbd/notify_msg.c b/source3/smbd/notify_msg.c index 7238cff6828..0ea992c4929 100644 --- a/source3/smbd/notify_msg.c +++ b/source3/smbd/notify_msg.c @@ -245,71 +245,3 @@ void notify_trigger(struct notify_context *ctx, ctx->msg_ctx, ctx->notifyd, MSG_SMB_NOTIFY_TRIGGER, iov, ARRAY_SIZE(iov), NULL, 0); } - -NTSTATUS notify_walk(struct notify_context *notify, - bool (*fn)(const char *path, struct server_id server, - const struct notify_instance *instance, - void *private_data), - void *private_data) -{ - struct tevent_context *ev; - struct tevent_req *req; - struct messaging_rec *rec; - uint64_t log_idx; - NTSTATUS status; - int ret; - bool ok; - - ev = samba_tevent_context_init(notify); - if (ev == NULL) { - return NT_STATUS_NO_MEMORY; - } - - req = messaging_read_send(ev, ev, notify->msg_ctx, MSG_SMB_NOTIFY_DB); - if (req == NULL) { - TALLOC_FREE(ev); - return NT_STATUS_NO_MEMORY; - } - - ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0)); - if (!ok) { - TALLOC_FREE(ev); - return NT_STATUS_NO_MEMORY; - } - - status = messaging_send_buf(notify->msg_ctx, notify->notifyd, - MSG_SMB_NOTIFY_GET_DB, NULL, 0); - if (!NT_STATUS_IS_OK(status)) { - DBG_DEBUG("messaging_send_buf failed: %s\n", - nt_errstr(status)); - TALLOC_FREE(ev); - return status; - } - - ok = tevent_req_poll(req, ev); - if (!ok) { - DBG_DEBUG("tevent_req_poll failed\n"); - TALLOC_FREE(ev); - return NT_STATUS_INTERNAL_ERROR; - } - - ret = messaging_read_recv(req, ev, &rec); - if (ret != 0) { - DBG_DEBUG("messaging_read_recv failed: %s\n", - strerror(ret)); - TALLOC_FREE(ev); - return map_nt_error_from_unix(ret); - } - - ret = notifyd_parse_db(rec->buf.data, rec->buf.length, &log_idx, - fn, private_data); - if (ret != 0) { - DBG_DEBUG("notifyd_parse_db failed: %s\n", - strerror(ret)); - TALLOC_FREE(ev); - return map_nt_error_from_unix(ret); - } - - TALLOC_FREE(ev); - return NT_STATUS_OK; -} diff --git a/source3/smbd/notifyd/notifyd.c b/source3/smbd/notifyd/notifyd.c index 9029e88dc57..601bd168cd8 100644 --- a/source3/smbd/notifyd/notifyd.c +++ b/source3/smbd/notifyd/notifyd.c @@ -1426,73 +1426,3 @@ static int notifyd_snoop_broadcast(struct tevent_context *ev, return 0; } #endif - -struct notifyd_parse_db_state { - bool (*fn)(const char *path, - struct server_id server, - const struct notify_instance *instance, - void *private_data); - void *private_data; -}; - -static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value, - void *private_data) -{ - struct notifyd_parse_db_state *state = private_data; - char path[key.dsize+1]; - struct notifyd_instance *instances = NULL; - size_t num_instances = 0; - size_t i; - bool ok; - - memcpy(path, key.dptr, key.dsize); - path[key.dsize] = 0; - - ok = notifyd_parse_entry(value.dptr, value.dsize, &instances, - &num_instances); - if (!ok) { - DBG_DEBUG("Could not parse entry for path %s\n", path); - return true; - } - - for (i=0; ifn(path, instances[i].client, - &instances[i].instance, - state->private_data); - if (!ok) { - return false; - } - } - - return true; -} - -int notifyd_parse_db(const uint8_t *buf, size_t buflen, - uint64_t *log_index, - bool (*fn)(const char *path, - struct server_id server, - const struct notify_instance *instance, - void *private_data), - void *private_data) -{ - struct notifyd_parse_db_state state = { - .fn = fn, .private_data = private_data - }; - NTSTATUS status; - - if (buflen < 8) { - return EINVAL; - } - *log_index = BVAL(buf, 0); - - buf += 8; - buflen -= 8; - - status = dbwrap_parse_marshall_buf( - buf, buflen, notifyd_parse_db_parser, &state); - if (!NT_STATUS_IS_OK(status)) { - return map_errno_from_nt_status(status); - } - - return 0; -} diff --git a/source3/smbd/notifyd/notifyd.h b/source3/smbd/notifyd/notifyd.h index 6904761af8f..5ef8c4c8d09 100644 --- a/source3/smbd/notifyd/notifyd.h +++ b/source3/smbd/notifyd/notifyd.h @@ -142,17 +142,4 @@ struct tevent_req *notifyd_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct sys_notify_context *sys_notify_ctx); int notifyd_recv(struct tevent_req *req); -/* - * Parse a database received via the MSG_SMB_NOTIFY_[GET_]DB messages to the - * notify daemon - */ -int notifyd_parse_db(const uint8_t *buf, size_t buflen, - uint64_t *log_index, - bool (*fn)(const char *path, - struct server_id server, - const struct notify_instance *instance, - void *private_data), - void *private_data); - - #endif diff --git a/source3/smbd/notifyd/notifyd_db.c b/source3/smbd/notifyd/notifyd_db.c new file mode 100644 index 00000000000..18228619e9a --- /dev/null +++ b/source3/smbd/notifyd/notifyd_db.c @@ -0,0 +1,165 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "replace.h" +#include "lib/util/debug.h" +#include "lib/util/server_id_db.h" +#include "notifyd_private.h" +#include "notifyd_db.h" + +struct notifyd_parse_db_state { + bool (*fn)(const char *path, + struct server_id server, + const struct notify_instance *instance, + void *private_data); + void *private_data; +}; + +static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value, + void *private_data) +{ + struct notifyd_parse_db_state *state = private_data; + char path[key.dsize+1]; + struct notifyd_instance *instances = NULL; + size_t num_instances = 0; + size_t i; + bool ok; + + memcpy(path, key.dptr, key.dsize); + path[key.dsize] = 0; + + ok = notifyd_parse_entry(value.dptr, value.dsize, &instances, + &num_instances); + if (!ok) { + DBG_DEBUG("Could not parse entry for path %s\n", path); + return true; + } + + for (i=0; ifn(path, instances[i].client, + &instances[i].instance, + state->private_data); + if (!ok) { + return false; + } + } + + return true; +} + +static NTSTATUS notifyd_parse_db( + const uint8_t *buf, + size_t buflen, + uint64_t *log_index, + bool (*fn)(const char *path, + struct server_id server, + const struct notify_instance *instance, + void *private_data), + void *private_data) +{ + struct notifyd_parse_db_state state = { + .fn = fn, .private_data = private_data + }; + NTSTATUS status; + + if (buflen < 8) { + return NT_STATUS_INTERNAL_DB_CORRUPTION; + } + *log_index = BVAL(buf, 0); + + buf += 8; + buflen -= 8; + + status = dbwrap_parse_marshall_buf( + buf, buflen, notifyd_parse_db_parser, &state); + return status; +} + +NTSTATUS notify_walk(struct messaging_context *msg_ctx, + bool (*fn)(const char *path, struct server_id server, + const struct notify_instance *instance, + void *private_data), + void *private_data) +{ + struct server_id_db *names_db = NULL; + struct server_id notifyd = { .pid = 0, }; + struct tevent_context *ev = NULL; + struct tevent_req *req = NULL; + struct messaging_rec *rec = NULL; + uint64_t log_idx; + NTSTATUS status; + int ret; + bool ok; + + names_db = messaging_names_db(msg_ctx); + ok = server_id_db_lookup_one(names_db, "notify-daemon", ¬ifyd); + if (!ok) { + DBG_WARNING("No notify daemon around\n"); + return NT_STATUS_SERVER_UNAVAILABLE; + } + + ev = samba_tevent_context_init(msg_ctx); + if (ev == NULL) { + return NT_STATUS_NO_MEMORY; + } + + req = messaging_read_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY_DB); + if (req == NULL) { + TALLOC_FREE(ev); + return NT_STATUS_NO_MEMORY; + } + + ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0)); + if (!ok) { + TALLOC_FREE(ev); + return NT_STATUS_NO_MEMORY; + } + + status = messaging_send_buf( + msg_ctx, notifyd, MSG_SMB_NOTIFY_GET_DB, NULL, 0); + if (!NT_STATUS_IS_OK(status)) { + DBG_DEBUG("messaging_send_buf failed: %s\n", + nt_errstr(status)); + TALLOC_FREE(ev); + return status; + } + + ok = tevent_req_poll(req, ev); + if (!ok) { + DBG_DEBUG("tevent_req_poll failed\n"); + TALLOC_FREE(ev); + return NT_STATUS_INTERNAL_ERROR; + } + + ret = messaging_read_recv(req, ev, &rec); + if (ret != 0) { + DBG_DEBUG("messaging_read_recv failed: %s\n", + strerror(ret)); + TALLOC_FREE(ev); + return map_nt_error_from_unix(ret); + } + + status = notifyd_parse_db( + rec->buf.data, rec->buf.length, &log_idx, fn, private_data); + if (!NT_STATUS_IS_OK(status)) { + DBG_DEBUG("notifyd_parse_db failed: %s\n", + nt_errstr(status)); + TALLOC_FREE(ev); + return status; + } + + TALLOC_FREE(ev); + return NT_STATUS_OK; +} diff --git a/source3/smbd/notifyd/notifyd_db.h b/source3/smbd/notifyd/notifyd_db.h new file mode 100644 index 00000000000..bd9e226d5e9 --- /dev/null +++ b/source3/smbd/notifyd/notifyd_db.h @@ -0,0 +1,27 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __NOTIFYD_NOTIFYD_DB_H__ +#define __NOTIFYD_NOTIFYD_DB_H__ +#include "replace.h" +#include "notifyd.h" + +NTSTATUS notify_walk(struct messaging_context *msg_ctx, + bool (*fn)(const char *path, struct server_id server, + const struct notify_instance *instance, + void *private_data), + void *private_data); + +#endif diff --git a/source3/smbd/notifyd/wscript_build b/source3/smbd/notifyd/wscript_build index e87421d4202..8d549bd0d25 100644 --- a/source3/smbd/notifyd/wscript_build +++ b/source3/smbd/notifyd/wscript_build @@ -1,8 +1,8 @@ #!/usr/bin/env python bld.SAMBA3_SUBSYSTEM('notifyd_db', - source='notifyd_entry.c', - deps='samba-debug') + source='notifyd_entry.c notifyd_db.c', + deps='samba-debug dbwrap errors3') bld.SAMBA3_SUBSYSTEM('notifyd', source='notifyd.c', diff --git a/source3/smbd/proto.h b/source3/smbd/proto.h index 40e7c3b04e7..b768e81afe9 100644 --- a/source3/smbd/proto.h +++ b/source3/smbd/proto.h @@ -660,13 +660,6 @@ void notify_trigger(struct notify_context *notify, uint32_t action, uint32_t filter, const char *dir, const char *path); -struct notify_instance; -NTSTATUS notify_walk(struct notify_context *notify, - bool (*fn)(const char *path, struct server_id server, - const struct notify_instance *instance, - void *private_data), - void *private_data); - /* The following definitions come from smbd/ntquotas.c */ NTSTATUS vfs_get_ntquota(files_struct *fsp, enum SMB_QUOTA_TYPE qtype, diff --git a/source3/utils/status.c b/source3/utils/status.c index 1c1cc4e9e52..05cc46086fe 100644 --- a/source3/utils/status.c +++ b/source3/utils/status.c @@ -47,7 +47,7 @@ #include "conn_tdb.h" #include "serverid.h" #include "status_profile.h" -#include "smbd/notifyd/notifyd.h" +#include "smbd/notifyd/notifyd_db.h" #include "cmdline_contexts.h" #include "locking/leases_db.h" #include "lib/util/string_wrappers.h" @@ -837,15 +837,7 @@ int main(int argc, const char *argv[]) } if (show_notify) { - struct notify_context *n; - - n = notify_init(talloc_tos(), msg_ctx, - NULL, NULL); - if (n == NULL) { - goto done; - } - notify_walk(n, print_notify_rec, NULL); - TALLOC_FREE(n); + notify_walk(msg_ctx, print_notify_rec, NULL); } done: