notifyd: Add notifyd_parse_db()
authorVolker Lendecke <vl@samba.org>
Fri, 9 Jan 2015 12:24:58 +0000 (12:24 +0000)
committerJeremy Allison <jra@samba.org>
Tue, 7 Jul 2015 21:51:24 +0000 (23:51 +0200)
The database format notifyd is "private" to it. This makes it
possible for smbcontrol and others to query notifyd's database with
MSG_SMB_NOTIFY_GET_DB and inspect it without having to know exactly what
format it uses.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/smbd/notifyd/notifyd.c
source3/smbd/notifyd/notifyd.h

index 0f31a61fd1f3b7b3ca5d4eabc96fb9d5789be132..d9fb11d8a3652955553d384b2a853af8b99d0c88 100644 (file)
@@ -1417,3 +1417,74 @@ static void notifyd_snoop_broadcast(uint32_t src_vnn, uint32_t dst_vnn,
                return;
        }
 }
+
+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) {
+               DEBUG(10, ("%s: Could not parse entry for path %s\n",
+                          __func__, path));
+               return true;
+       }
+
+       for (i=0; i<num_instances; i++) {
+               ok = state->fn(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;
+}
index dc0a4e8392d23c18ce47c24036b9730d93184190..672ffba202d56b35c1461bbc5d0b6b89f6f7921a 100644 (file)
@@ -140,4 +140,17 @@ 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