Coverage Report

Created: 2025-11-16 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/smbd/notifyd/notifyd_db.c
Line
Count
Source
1
/*
2
 * This program is free software; you can redistribute it and/or modify
3
 * it under the terms of the GNU General Public License as published by
4
 * the Free Software Foundation; either version 3 of the License, or
5
 * (at your option) any later version.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14
 */
15
16
#include "replace.h"
17
#include "lib/util/debug.h"
18
#include "lib/util/server_id_db.h"
19
#include "notifyd_private.h"
20
#include "notifyd_db.h"
21
22
struct notifyd_parse_db_state {
23
  bool (*fn)(const char *path,
24
       struct server_id server,
25
       const struct notify_instance *instance,
26
       void *private_data);
27
  void *private_data;
28
};
29
30
static bool notifyd_parse_db_parser(TDB_DATA key, TDB_DATA value,
31
            void *private_data)
32
0
{
33
0
  struct notifyd_parse_db_state *state = private_data;
34
0
  char path[key.dsize+1];
35
0
  struct notifyd_instance *instances = NULL;
36
0
  size_t num_instances = 0;
37
0
  size_t i;
38
0
  bool ok;
39
40
0
  memcpy(path, key.dptr, key.dsize);
41
0
  path[key.dsize] = 0;
42
43
0
  ok = notifyd_parse_entry(value.dptr,
44
0
         value.dsize,
45
0
         NULL,
46
0
         &instances,
47
0
         &num_instances);
48
0
  if (!ok) {
49
0
    DBG_DEBUG("Could not parse entry for path %s\n", path);
50
0
    return true;
51
0
  }
52
53
0
  for (i=0; i<num_instances; i++) {
54
0
    ok = state->fn(path, instances[i].client,
55
0
             &instances[i].instance,
56
0
             state->private_data);
57
0
    if (!ok) {
58
0
      return false;
59
0
    }
60
0
  }
61
62
0
  return true;
63
0
}
64
65
static NTSTATUS notifyd_parse_db(
66
  const uint8_t *buf,
67
  size_t buflen,
68
  uint64_t *log_index,
69
  bool (*fn)(const char *path,
70
       struct server_id server,
71
       const struct notify_instance *instance,
72
       void *private_data),
73
  void *private_data)
74
0
{
75
0
  struct notifyd_parse_db_state state = {
76
0
    .fn = fn, .private_data = private_data
77
0
  };
78
0
  NTSTATUS status;
79
80
0
  if (buflen < 8) {
81
0
    return NT_STATUS_INTERNAL_DB_CORRUPTION;
82
0
  }
83
0
  *log_index = BVAL(buf, 0);
84
85
0
  buf += 8;
86
0
  buflen -= 8;
87
88
0
  status = dbwrap_parse_marshall_buf(
89
0
    buf, buflen, notifyd_parse_db_parser, &state);
90
0
  return status;
91
0
}
92
93
NTSTATUS notify_walk(struct messaging_context *msg_ctx,
94
         bool (*fn)(const char *path, struct server_id server,
95
        const struct notify_instance *instance,
96
        void *private_data),
97
         void *private_data)
98
0
{
99
0
  struct server_id_db *names_db = NULL;
100
0
  struct server_id notifyd = { .pid = 0, };
101
0
  struct tevent_context *ev = NULL;
102
0
  struct tevent_req *req = NULL;
103
0
  struct messaging_rec *rec = NULL;
104
0
  uint64_t log_idx;
105
0
  NTSTATUS status;
106
0
  int ret;
107
0
  bool ok;
108
109
0
  names_db = messaging_names_db(msg_ctx);
110
0
  ok = server_id_db_lookup_one(names_db, "notify-daemon", &notifyd);
111
0
  if (!ok) {
112
0
    DBG_WARNING("No notify daemon around\n");
113
0
    return NT_STATUS_SERVER_UNAVAILABLE;
114
0
  }
115
116
0
  ev = samba_tevent_context_init(msg_ctx);
117
0
  if (ev == NULL) {
118
0
    return NT_STATUS_NO_MEMORY;
119
0
  }
120
121
0
  req = messaging_read_send(ev, ev, msg_ctx, MSG_SMB_NOTIFY_DB);
122
0
  if (req == NULL) {
123
0
    TALLOC_FREE(ev);
124
0
    return NT_STATUS_NO_MEMORY;
125
0
  }
126
127
0
  ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(10, 0));
128
0
  if (!ok) {
129
0
    TALLOC_FREE(ev);
130
0
    return NT_STATUS_NO_MEMORY;
131
0
  }
132
133
0
  status = messaging_send_buf(
134
0
    msg_ctx, notifyd, MSG_SMB_NOTIFY_GET_DB, NULL, 0);
135
0
  if (!NT_STATUS_IS_OK(status)) {
136
0
    DBG_DEBUG("messaging_send_buf failed: %s\n",
137
0
        nt_errstr(status));
138
0
    TALLOC_FREE(ev);
139
0
    return status;
140
0
  }
141
142
0
  ok = tevent_req_poll(req, ev);
143
0
  if (!ok) {
144
0
    DBG_DEBUG("tevent_req_poll failed\n");
145
0
    TALLOC_FREE(ev);
146
0
    return NT_STATUS_INTERNAL_ERROR;
147
0
  }
148
149
0
  ret = messaging_read_recv(req, ev, &rec);
150
0
  if (ret != 0) {
151
0
    DBG_DEBUG("messaging_read_recv failed: %s\n",
152
0
        strerror(ret));
153
0
    TALLOC_FREE(ev);
154
0
    return map_nt_error_from_unix(ret);
155
0
  }
156
157
0
  status = notifyd_parse_db(
158
0
    rec->buf.data, rec->buf.length, &log_idx, fn, private_data);
159
0
  if (!NT_STATUS_IS_OK(status)) {
160
0
    DBG_DEBUG("notifyd_parse_db failed: %s\n",
161
0
        nt_errstr(status));
162
0
    TALLOC_FREE(ev);
163
0
    return status;
164
0
  }
165
166
0
  TALLOC_FREE(ev);
167
0
  return NT_STATUS_OK;
168
0
}