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