s3: New notify implementation
[samba.git] / source3 / smbd / notify.c
1 /*
2    Unix SMB/CIFS implementation.
3    change notify handling
4    Copyright (C) Andrew Tridgell 2000
5    Copyright (C) Jeremy Allison 1994-1998
6    Copyright (C) Volker Lendecke 2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/ndr_notify.h"
26
27 struct notify_change_request {
28         struct notify_change_request *prev, *next;
29         struct files_struct *fsp;       /* backpointer for cancel by mid */
30         struct smb_request *req;
31         uint32 filter;
32         uint32 max_param;
33         void (*reply_fn)(struct smb_request *req,
34                          NTSTATUS error_code,
35                          uint8_t *buf, size_t len);
36         struct notify_mid_map *mid_map;
37         void *backend_data;
38 };
39
40 static void notify_fsp(files_struct *fsp, uint32 action, const char *name);
41
42 /*
43  * For NTCancel, we need to find the notify_change_request indexed by
44  * mid. Separate list here.
45  */
46
47 struct notify_mid_map {
48         struct notify_mid_map *prev, *next;
49         struct notify_change_request *req;
50         uint64_t mid;
51 };
52
53 static bool notify_change_record_identical(struct notify_change *c1,
54                                         struct notify_change *c2)
55 {
56         /* Note this is deliberately case sensitive. */
57         if (c1->action == c2->action &&
58                         strcmp(c1->name, c2->name) == 0) {
59                 return True;
60         }
61         return False;
62 }
63
64 static bool notify_marshall_changes(int num_changes,
65                                 uint32 max_offset,
66                                 struct notify_change *changes,
67                                 DATA_BLOB *final_blob)
68 {
69         int i;
70
71         if (num_changes == -1) {
72                 return false;
73         }
74
75         for (i=0; i<num_changes; i++) {
76                 enum ndr_err_code ndr_err;
77                 struct notify_change *c;
78                 struct FILE_NOTIFY_INFORMATION m;
79                 DATA_BLOB blob;
80
81                 /* Coalesce any identical records. */
82                 while (i+1 < num_changes &&
83                         notify_change_record_identical(&changes[i],
84                                                 &changes[i+1])) {
85                         i++;
86                 }
87
88                 c = &changes[i];
89
90                 m.FileName1 = c->name;
91                 m.FileNameLength = strlen_m(c->name)*2;
92                 m.Action = c->action;
93                 m.NextEntryOffset = (i == num_changes-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
94
95                 /*
96                  * Offset to next entry, only if there is one
97                  */
98
99                 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
100                         (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
101                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
102                         return false;
103                 }
104
105                 if (DEBUGLEVEL >= 10) {
106                         NDR_PRINT_DEBUG(FILE_NOTIFY_INFORMATION, &m);
107                 }
108
109                 if (!data_blob_append(talloc_tos(), final_blob,
110                                       blob.data, blob.length)) {
111                         data_blob_free(&blob);
112                         return false;
113                 }
114
115                 data_blob_free(&blob);
116
117                 if (final_blob->length > max_offset) {
118                         /* Too much data for client. */
119                         DEBUG(10, ("Client only wanted %d bytes, trying to "
120                                    "marshall %d bytes\n", (int)max_offset,
121                                    (int)final_blob->length));
122                         return False;
123                 }
124         }
125
126         return True;
127 }
128
129 /****************************************************************************
130  Setup the common parts of the return packet and send it.
131 *****************************************************************************/
132
133 void change_notify_reply(struct smb_request *req,
134                          NTSTATUS error_code,
135                          uint32_t max_param,
136                          struct notify_change_buf *notify_buf,
137                          void (*reply_fn)(struct smb_request *req,
138                                           NTSTATUS error_code,
139                                           uint8_t *buf, size_t len))
140 {
141         DATA_BLOB blob = data_blob_null;
142
143         if (!NT_STATUS_IS_OK(error_code)) {
144                 reply_fn(req, error_code, NULL, 0);
145                 return;
146         }
147
148         if (max_param == 0 || notify_buf == NULL) {
149                 reply_fn(req, NT_STATUS_OK, NULL, 0);
150                 return;
151         }
152
153         if (!notify_marshall_changes(notify_buf->num_changes, max_param,
154                                         notify_buf->changes, &blob)) {
155                 /*
156                  * We exceed what the client is willing to accept. Send
157                  * nothing.
158                  */
159                 data_blob_free(&blob);
160         }
161
162         reply_fn(req, NT_STATUS_OK, blob.data, blob.length);
163
164         data_blob_free(&blob);
165
166         TALLOC_FREE(notify_buf->changes);
167         notify_buf->num_changes = 0;
168 }
169
170 static void notify_callback(void *private_data, const struct notify_event *e)
171 {
172         files_struct *fsp = (files_struct *)private_data;
173         DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
174         notify_fsp(fsp, e->action, e->path);
175 }
176
177 static void sys_notify_callback(struct sys_notify_context *ctx,
178                                 void *private_data,
179                                 struct notify_event *e)
180 {
181         files_struct *fsp = (files_struct *)private_data;
182         DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp)));
183         notify_fsp(fsp, e->action, e->path);
184 }
185
186 NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
187                               bool recursive)
188 {
189         char *fullpath;
190         size_t len;
191         uint32_t subdir_filter;
192         NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
193
194         if (fsp->notify != NULL) {
195                 DEBUG(1, ("change_notify_create: fsp->notify != NULL, "
196                           "fname = %s\n", fsp->fsp_name->base_name));
197                 return NT_STATUS_INVALID_PARAMETER;
198         }
199
200         if (!(fsp->notify = talloc_zero(NULL, struct notify_change_buf))) {
201                 DEBUG(0, ("talloc failed\n"));
202                 return NT_STATUS_NO_MEMORY;
203         }
204
205         /* Do notify operations on the base_name. */
206         fullpath = talloc_asprintf(
207                 talloc_tos(), "%s/%s", fsp->conn->connectpath,
208                 fsp->fsp_name->base_name);
209         if (fullpath == NULL) {
210                 DEBUG(0, ("talloc_asprintf failed\n"));
211                 TALLOC_FREE(fsp->notify);
212                 return NT_STATUS_NO_MEMORY;
213         }
214
215         /*
216          * Avoid /. at the end of the path name. notify can't deal with it.
217          */
218         len = strlen(fullpath);
219         if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
220                 fullpath[len-2] = '\0';
221         }
222
223         subdir_filter = recursive ? filter : 0;
224
225         if (fsp->conn->sconn->sys_notify_ctx != NULL) {
226                 void *sys_notify_handle = NULL;
227
228                 status = SMB_VFS_NOTIFY_WATCH(
229                         fsp->conn, fsp->conn->sconn->sys_notify_ctx,
230                         fullpath, &filter, &subdir_filter,
231                         sys_notify_callback, fsp, &sys_notify_handle);
232
233                 if (NT_STATUS_IS_OK(status)) {
234                         talloc_steal(fsp->notify, sys_notify_handle);
235                 }
236         }
237
238         if ((filter != 0) || (subdir_filter != 0)) {
239                 status = notify_add(fsp->conn->sconn->notify_ctx,
240                                     fullpath, filter, subdir_filter,
241                                     notify_callback, fsp);
242         }
243         TALLOC_FREE(fullpath);
244         return status;
245 }
246
247 NTSTATUS change_notify_add_request(struct smb_request *req,
248                                 uint32 max_param,
249                                 uint32 filter, bool recursive,
250                                 struct files_struct *fsp,
251                                 void (*reply_fn)(struct smb_request *req,
252                                         NTSTATUS error_code,
253                                         uint8_t *buf, size_t len))
254 {
255         struct notify_change_request *request = NULL;
256         struct notify_mid_map *map = NULL;
257         struct smbd_server_connection *sconn = req->sconn;
258
259         DEBUG(10, ("change_notify_add_request: Adding request for %s: "
260                    "max_param = %d\n", fsp_str_dbg(fsp), (int)max_param));
261
262         if (!(request = talloc(NULL, struct notify_change_request))
263             || !(map = talloc(request, struct notify_mid_map))) {
264                 TALLOC_FREE(request);
265                 return NT_STATUS_NO_MEMORY;
266         }
267
268         request->mid_map = map;
269         map->req = request;
270
271         request->req = talloc_move(request, &req);
272         request->max_param = max_param;
273         request->filter = filter;
274         request->fsp = fsp;
275         request->reply_fn = reply_fn;
276         request->backend_data = NULL;
277
278         DLIST_ADD_END(fsp->notify->requests, request,
279                       struct notify_change_request *);
280
281         map->mid = request->req->mid;
282         DLIST_ADD(sconn->smb1.notify_mid_maps, map);
283
284         return NT_STATUS_OK;
285 }
286
287 static void change_notify_remove_request(struct smbd_server_connection *sconn,
288                                          struct notify_change_request *remove_req)
289 {
290         files_struct *fsp;
291         struct notify_change_request *req;
292
293         /*
294          * Paranoia checks, the fsp referenced must must have the request in
295          * its list of pending requests
296          */
297
298         fsp = remove_req->fsp;
299         SMB_ASSERT(fsp->notify != NULL);
300
301         for (req = fsp->notify->requests; req; req = req->next) {
302                 if (req == remove_req) {
303                         break;
304                 }
305         }
306
307         if (req == NULL) {
308                 smb_panic("notify_req not found in fsp's requests");
309         }
310
311         DLIST_REMOVE(fsp->notify->requests, req);
312         DLIST_REMOVE(sconn->smb1.notify_mid_maps, req->mid_map);
313         TALLOC_FREE(req);
314 }
315
316 /****************************************************************************
317  Delete entries by mid from the change notify pending queue. Always send reply.
318 *****************************************************************************/
319
320 void remove_pending_change_notify_requests_by_mid(
321         struct smbd_server_connection *sconn, uint64_t mid)
322 {
323         struct notify_mid_map *map;
324
325         for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
326                 if (map->mid == mid) {
327                         break;
328                 }
329         }
330
331         if (map == NULL) {
332                 return;
333         }
334
335         change_notify_reply(map->req->req,
336                             NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
337         change_notify_remove_request(sconn, map->req);
338 }
339
340 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
341 {
342         struct smbd_server_connection *sconn = smbreq->sconn;
343         struct notify_mid_map *map;
344
345         for (map = sconn->smb1.notify_mid_maps; map; map = map->next) {
346                 if (map->req->req == smbreq) {
347                         break;
348                 }
349         }
350
351         if (map == NULL) {
352                 return;
353         }
354
355         change_notify_reply(map->req->req,
356                             NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
357         change_notify_remove_request(sconn, map->req);
358 }
359
360 /****************************************************************************
361  Delete entries by fnum from the change notify pending queue.
362 *****************************************************************************/
363
364 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
365                                                   NTSTATUS status)
366 {
367         if (fsp->notify == NULL) {
368                 return;
369         }
370
371         while (fsp->notify->requests != NULL) {
372                 change_notify_reply(fsp->notify->requests->req,
373                                     status, 0, NULL,
374                                     fsp->notify->requests->reply_fn);
375                 change_notify_remove_request(fsp->conn->sconn,
376                                              fsp->notify->requests);
377         }
378 }
379
380 void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
381                   const char *path)
382 {
383         struct notify_context *notify_ctx = conn->sconn->notify_ctx;
384         char *fullpath;
385
386         if (path[0] == '.' && path[1] == '/') {
387                 path += 2;
388         }
389         fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
390                                    path);
391         if (fullpath == NULL) {
392                 DEBUG(0, ("asprintf failed\n"));
393                 return;
394         }
395         notify_trigger(notify_ctx, action, filter, fullpath);
396         TALLOC_FREE(fullpath);
397 }
398
399 static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
400 {
401         struct notify_change *change, *changes;
402         char *tmp;
403
404         if (fsp->notify == NULL) {
405                 /*
406                  * Nobody is waiting, don't queue
407                  */
408                 return;
409         }
410
411         /*
412          * Someone has triggered a notify previously, queue the change for
413          * later.
414          */
415
416         if ((fsp->notify->num_changes > 1000) || (name == NULL)) {
417                 /*
418                  * The real number depends on the client buf, just provide a
419                  * guard against a DoS here.  If name == NULL the CN backend is
420                  * alerting us to a problem.  Possibly dropped events.  Clear
421                  * queued changes and send the catch-all response to the client
422                  * if a request is pending.
423                  */
424                 TALLOC_FREE(fsp->notify->changes);
425                 fsp->notify->num_changes = -1;
426                 if (fsp->notify->requests != NULL) {
427                         change_notify_reply(fsp->notify->requests->req,
428                                             NT_STATUS_OK,
429                                             fsp->notify->requests->max_param,
430                                             fsp->notify,
431                                             fsp->notify->requests->reply_fn);
432                         change_notify_remove_request(fsp->conn->sconn,
433                                                      fsp->notify->requests);
434                 }
435                 return;
436         }
437
438         /* If we've exceeded the server side queue or received a NULL name
439          * from the underlying CN implementation, don't queue up any more
440          * requests until we can send a catch-all response to the client */
441         if (fsp->notify->num_changes == -1) {
442                 return;
443         }
444
445         if (!(changes = talloc_realloc(
446                       fsp->notify, fsp->notify->changes,
447                       struct notify_change, fsp->notify->num_changes+1))) {
448                 DEBUG(0, ("talloc_realloc failed\n"));
449                 return;
450         }
451
452         fsp->notify->changes = changes;
453
454         change = &(fsp->notify->changes[fsp->notify->num_changes]);
455
456         if (!(tmp = talloc_strdup(changes, name))) {
457                 DEBUG(0, ("talloc_strdup failed\n"));
458                 return;
459         }
460
461         string_replace(tmp, '/', '\\');
462         change->name = tmp;     
463
464         change->action = action;
465         fsp->notify->num_changes += 1;
466
467         if (fsp->notify->requests == NULL) {
468                 /*
469                  * Nobody is waiting, so don't send anything. The ot
470                  */
471                 return;
472         }
473
474         if (action == NOTIFY_ACTION_OLD_NAME) {
475                 /*
476                  * We have to send the two rename events in one reply. So hold
477                  * the first part back.
478                  */
479                 return;
480         }
481
482         /*
483          * Someone is waiting for the change, trigger the reply immediately.
484          *
485          * TODO: do we have to walk the lists of requests pending?
486          */
487
488         change_notify_reply(fsp->notify->requests->req,
489                             NT_STATUS_OK,
490                             fsp->notify->requests->max_param,
491                             fsp->notify,
492                             fsp->notify->requests->reply_fn);
493
494         change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
495 }
496
497 char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
498 {
499         char *result = NULL;
500
501         result = talloc_strdup(mem_ctx, "");
502
503         if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
504                 result = talloc_asprintf_append(result, "FILE_NAME|");
505         if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
506                 result = talloc_asprintf_append(result, "DIR_NAME|");
507         if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
508                 result = talloc_asprintf_append(result, "ATTRIBUTES|");
509         if (filter & FILE_NOTIFY_CHANGE_SIZE)
510                 result = talloc_asprintf_append(result, "SIZE|");
511         if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
512                 result = talloc_asprintf_append(result, "LAST_WRITE|");
513         if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
514                 result = talloc_asprintf_append(result, "LAST_ACCESS|");
515         if (filter & FILE_NOTIFY_CHANGE_CREATION)
516                 result = talloc_asprintf_append(result, "CREATION|");
517         if (filter & FILE_NOTIFY_CHANGE_EA)
518                 result = talloc_asprintf_append(result, "EA|");
519         if (filter & FILE_NOTIFY_CHANGE_SECURITY)
520                 result = talloc_asprintf_append(result, "SECURITY|");
521         if (filter & FILE_NOTIFY_CHANGE_STREAM_NAME)
522                 result = talloc_asprintf_append(result, "STREAM_NAME|");
523         if (filter & FILE_NOTIFY_CHANGE_STREAM_SIZE)
524                 result = talloc_asprintf_append(result, "STREAM_SIZE|");
525         if (filter & FILE_NOTIFY_CHANGE_STREAM_WRITE)
526                 result = talloc_asprintf_append(result, "STREAM_WRITE|");
527
528         if (result == NULL) return NULL;
529         if (*result == '\0') return result;
530
531         result[strlen(result)-1] = '\0';
532         return result;
533 }
534
535 struct sys_notify_context *sys_notify_context_create(TALLOC_CTX *mem_ctx,
536                                                      struct event_context *ev)
537 {
538         struct sys_notify_context *ctx;
539
540         if (!(ctx = talloc(mem_ctx, struct sys_notify_context))) {
541                 DEBUG(0, ("talloc failed\n"));
542                 return NULL;
543         }
544
545         ctx->ev = ev;
546         ctx->private_data = NULL;
547         return ctx;
548 }