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