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