1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
/*
* Unix SMB/CIFS implementation.
*
* Copyright (C) Volker Lendecke 2014
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "librpc/gen_ndr/notify.h"
#include "librpc/gen_ndr/messaging.h"
#include "lib/dbwrap/dbwrap.h"
#include "lib/dbwrap/dbwrap_rbt.h"
#include "lib/util/server_id.h"
#include "messages.h"
#include "proto.h"
#include "globals.h"
#include "tdb.h"
#include "util_tdb.h"
#include "lib/util/server_id_db.h"
#include "smbd/notifyd/notifyd.h"
struct notify_context {
struct server_id notifyd;
struct messaging_context *msg_ctx;
struct smbd_server_connection *sconn;
void (*callback)(struct smbd_server_connection *sconn,
void *private_data, struct timespec when,
const struct notify_event *ctx);
};
static void notify_handler(struct messaging_context *msg, void *private_data,
uint32_t msg_type, struct server_id src,
DATA_BLOB *data);
static int notify_context_destructor(struct notify_context *ctx);
struct notify_context *notify_init(
TALLOC_CTX *mem_ctx, struct messaging_context *msg,
struct smbd_server_connection *sconn,
void (*callback)(struct smbd_server_connection *sconn,
void *, struct timespec,
const struct notify_event *))
{
struct server_id_db *names_db;
struct notify_context *ctx;
NTSTATUS status;
ctx = talloc(mem_ctx, struct notify_context);
if (ctx == NULL) {
return NULL;
}
ctx->msg_ctx = msg;
ctx->sconn = sconn;
ctx->callback = callback;
names_db = messaging_names_db(msg);
if (!server_id_db_lookup_one(names_db, "notify-daemon",
&ctx->notifyd)) {
DBG_WARNING("No notify daemon around\n");
TALLOC_FREE(ctx);
return NULL;
}
{
struct server_id_buf tmp;
DBG_DEBUG("notifyd=%s\n",
server_id_str_buf(ctx->notifyd, &tmp));
}
if (callback != NULL) {
status = messaging_register(msg, ctx, MSG_PVFS_NOTIFY,
notify_handler);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("messaging_register failed: %s\n",
nt_errstr(status));
TALLOC_FREE(ctx);
return NULL;
}
}
talloc_set_destructor(ctx, notify_context_destructor);
return ctx;
}
static int notify_context_destructor(struct notify_context *ctx)
{
if (ctx->callback != NULL) {
messaging_deregister(ctx->msg_ctx, MSG_PVFS_NOTIFY, ctx);
}
return 0;
}
static void notify_handler(struct messaging_context *msg, void *private_data,
uint32_t msg_type, struct server_id src,
DATA_BLOB *data)
{
struct notify_context *ctx = talloc_get_type_abort(
private_data, struct notify_context);
struct notify_event_msg *event_msg;
struct notify_event event;
if (data->length < offsetof(struct notify_event_msg, path) + 1) {
DBG_WARNING("message too short: %zu\n", data->length);
return;
}
if (data->data[data->length-1] != 0) {
DBG_WARNING("path not 0-terminated\n");
return;
}
event_msg = (struct notify_event_msg *)data->data;
event.action = event_msg->action;
event.path = event_msg->path;
event.private_data = event_msg->private_data;
DBG_DEBUG("Got notify_event action=%"PRIu32", private_data=%p, "
"path=%s\n",
event.action,
event.private_data,
event.path);
ctx->callback(ctx->sconn, event.private_data, event_msg->when, &event);
}
NTSTATUS notify_add(struct notify_context *ctx,
const char *path, uint32_t filter, uint32_t subdir_filter,
void *private_data)
{
struct notify_rec_change_msg msg = {};
struct iovec iov[2];
size_t pathlen;
NTSTATUS status;
if (ctx == NULL) {
return NT_STATUS_NOT_IMPLEMENTED;
}
DBG_DEBUG("path=[%s], filter=%"PRIu32", subdir_filter=%"PRIu32", "
"private_data=%p\n",
path,
filter,
subdir_filter,
private_data);
pathlen = strlen(path)+1;
clock_gettime_mono(&msg.instance.creation_time);
msg.instance.filter = filter;
msg.instance.subdir_filter = subdir_filter;
msg.instance.private_data = private_data;
iov[0].iov_base = &msg;
iov[0].iov_len = offsetof(struct notify_rec_change_msg, path);
iov[1].iov_base = discard_const_p(char, path);
iov[1].iov_len = pathlen;
status = messaging_send_iov(
ctx->msg_ctx, ctx->notifyd, MSG_SMB_NOTIFY_REC_CHANGE,
iov, ARRAY_SIZE(iov), NULL, 0);
if (!NT_STATUS_IS_OK(status)) {
DBG_DEBUG("messaging_send_iov returned %s\n",
nt_errstr(status));
return status;
}
return NT_STATUS_OK;
}
NTSTATUS notify_remove(struct notify_context *ctx, void *private_data,
char *path)
{
struct notify_rec_change_msg msg = {};
struct iovec iov[2];
NTSTATUS status;
/* see if change notify is enabled at all */
if (ctx == NULL) {
return NT_STATUS_NOT_IMPLEMENTED;
}
msg.instance.private_data = private_data;
iov[0].iov_base = &msg;
iov[0].iov_len = offsetof(struct notify_rec_change_msg, path);
iov[1].iov_base = path;
iov[1].iov_len = strlen(path)+1;
status = messaging_send_iov(
ctx->msg_ctx, ctx->notifyd, MSG_SMB_NOTIFY_REC_CHANGE,
iov, ARRAY_SIZE(iov), NULL, 0);
return status;
}
void notify_trigger(struct notify_context *ctx,
uint32_t action, uint32_t filter,
const char *dir, const char *name)
{
struct notify_trigger_msg msg;
struct iovec iov[4];
char slash = '/';
DBG_DEBUG("notify_trigger called action=0x%"PRIx32", "
"filter=0x%"PRIx32", dir=%s, name=%s\n",
action,
filter,
dir,
name);
if (ctx == NULL) {
return;
}
msg.when = timespec_current();
msg.action = action;
msg.filter = filter;
iov[0].iov_base = &msg;
iov[0].iov_len = offsetof(struct notify_trigger_msg, path);
iov[1].iov_base = discard_const_p(char, dir);
iov[1].iov_len = strlen(dir);
iov[2].iov_base = &slash;
iov[2].iov_len = 1;
iov[3].iov_base = discard_const_p(char, name);
iov[3].iov_len = strlen(name)+1;
messaging_send_iov(
ctx->msg_ctx, ctx->notifyd, MSG_SMB_NOTIFY_TRIGGER,
iov, ARRAY_SIZE(iov), NULL, 0);
}
|