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