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