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