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