r23474: Here's a small patch that disables the libkrb5.so replay cache
[tprouty/samba.git] / source / smbd / notify_internal.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2006
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22   this is the change notify database. It implements mechanisms for
23   storing current change notify waiters in a tdb, and checking if a
24   given event matches any of the stored notify waiiters.
25 */
26
27 #include "includes.h"
28 #include "librpc/gen_ndr/ndr_notify.h"
29
30 struct notify_context {
31         struct db_context *db;
32         struct server_id server;
33         struct messaging_context *messaging_ctx;
34         struct notify_list *list;
35         struct notify_array *array;
36         int seqnum;
37         struct sys_notify_context *sys_notify_ctx;
38         TDB_DATA key;
39 };
40
41
42 struct notify_list {
43         struct notify_list *next, *prev;
44         void *private_data;
45         void (*callback)(void *, const struct notify_event *);
46         void *sys_notify_handle;
47         int depth;
48 };
49
50 #define NOTIFY_KEY "notify array"
51
52 #define NOTIFY_ENABLE           "notify:enable"
53 #define NOTIFY_ENABLE_DEFAULT   True
54
55 static NTSTATUS notify_remove_all(struct notify_context *notify,
56                                   const struct server_id *server);
57 static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
58                            uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);
59
60 /*
61   destroy the notify context
62 */
63 static int notify_destructor(struct notify_context *notify)
64 {
65         messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
66
67         if (notify->list != NULL) {
68                 notify_remove_all(notify, &notify->server);
69         }
70
71         return 0;
72 }
73
74 /*
75   Open up the notify.tdb database. You should close it down using
76   talloc_free(). We need the messaging_ctx to allow for notifications
77   via internal messages
78 */
79 struct notify_context *notify_init(TALLOC_CTX *mem_ctx, struct server_id server, 
80                                    struct messaging_context *messaging_ctx,
81                                    struct event_context *ev,
82                                    connection_struct *conn)
83 {
84         struct notify_context *notify;
85
86         if (!lp_change_notify(conn->params)) {
87                 return NULL;
88         }
89
90         notify = talloc(mem_ctx, struct notify_context);
91         if (notify == NULL) {
92                 return NULL;
93         }
94
95         notify->db = db_open(notify, lock_path("notify.tdb"),
96                                   0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST,
97                                   O_RDWR|O_CREAT, 0644);
98         if (notify->db == NULL) {
99                 talloc_free(notify);
100                 return NULL;
101         }
102
103         notify->server = server;
104         notify->messaging_ctx = messaging_ctx;
105         notify->list = NULL;
106         notify->array = NULL;
107         notify->seqnum = notify->db->get_seqnum(notify->db);
108         notify->key = string_term_tdb_data(NOTIFY_KEY);
109
110         talloc_set_destructor(notify, notify_destructor);
111
112         /* register with the messaging subsystem for the notify
113            message type */
114         messaging_register(notify->messaging_ctx, notify, 
115                            MSG_PVFS_NOTIFY, notify_handler);
116
117         notify->sys_notify_ctx = sys_notify_context_create(conn, notify, ev);
118
119         return notify;
120 }
121
122 /*
123   lock and fetch the record
124 */
125 static NTSTATUS notify_fetch_locked(struct notify_context *notify, struct db_record **rec)
126 {
127         *rec = notify->db->fetch_locked(notify->db, notify, notify->key);
128         if (*rec == NULL) {
129                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
130         }
131         return NT_STATUS_OK;
132 }
133
134 /*
135   load the notify array
136 */
137 static NTSTATUS notify_load(struct notify_context *notify, struct db_record *rec)
138 {
139         TDB_DATA dbuf;
140         DATA_BLOB blob;
141         NTSTATUS status;
142         int seqnum;
143
144         seqnum = notify->db->get_seqnum(notify->db);
145
146         if (seqnum == notify->seqnum && notify->array != NULL) {
147                 return NT_STATUS_OK;
148         }
149
150         notify->seqnum = seqnum;
151
152         talloc_free(notify->array);
153         notify->array = TALLOC_ZERO_P(notify, struct notify_array);
154         NT_STATUS_HAVE_NO_MEMORY(notify->array);
155
156         if (!rec) {
157                 if (notify->db->fetch(notify->db, notify, notify->key, &dbuf) != 0) {
158                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
159                 }
160         } else {
161                 dbuf = rec->value;
162         }
163
164         blob.data = (uint8 *)dbuf.dptr;
165         blob.length = dbuf.dsize;
166
167         if (blob.length == 0) {
168                 status = NT_STATUS_OK;
169         } else {
170                 status = ndr_pull_struct_blob(&blob, notify->array, notify->array, 
171                                               (ndr_pull_flags_fn_t)ndr_pull_notify_array);
172         }
173
174         if (DEBUGLEVEL >= 10) {
175                 DEBUG(10, ("notify_load:\n"));
176                 NDR_PRINT_DEBUG(notify_array, notify->array);
177         }
178
179         if (!rec) {
180                 talloc_free(dbuf.dptr);
181         }
182
183         return status;
184 }
185
186 /*
187   compare notify entries for sorting
188 */
189 static int notify_compare(const void *p1, const void *p2)
190 {
191         const struct notify_entry *e1 = (const struct notify_entry *)p1;
192         const struct notify_entry *e2 = (const struct notify_entry *)p2;
193         return strcmp(e1->path, e2->path);
194 }
195
196 /*
197   save the notify array
198 */
199 static NTSTATUS notify_save(struct notify_context *notify, struct db_record *rec)
200 {
201         TDB_DATA dbuf;
202         DATA_BLOB blob;
203         NTSTATUS status;
204         TALLOC_CTX *tmp_ctx;
205
206         /* if possible, remove some depth arrays */
207         while (notify->array->num_depths > 0 &&
208                notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
209                 notify->array->num_depths--;
210         }
211
212         /* we might just be able to delete the record */
213         if (notify->array->num_depths == 0) {
214                 return rec->delete_rec(rec);
215         }
216
217         tmp_ctx = talloc_new(notify);
218         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
219
220         status = ndr_push_struct_blob(&blob, tmp_ctx, notify->array, 
221                                       (ndr_push_flags_fn_t)ndr_push_notify_array);
222         if (!NT_STATUS_IS_OK(status)) {
223                 talloc_free(tmp_ctx);
224                 return status;
225         }
226
227         if (DEBUGLEVEL >= 10) {
228                 DEBUG(10, ("notify_save:\n"));
229                 NDR_PRINT_DEBUG(notify_array, notify->array);
230         }
231
232         dbuf.dptr = blob.data;
233         dbuf.dsize = blob.length;
234
235         status = rec->store(rec, dbuf, TDB_REPLACE);
236         talloc_free(tmp_ctx);
237
238         return status;
239 }
240
241
242 /*
243   handle incoming notify messages
244 */
245 static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
246                            uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
247 {
248         struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
249         NTSTATUS status;
250         struct notify_event ev;
251         TALLOC_CTX *tmp_ctx = talloc_new(notify);
252         struct notify_list *listel;
253
254         if (tmp_ctx == NULL) {
255                 return;
256         }
257
258         status = ndr_pull_struct_blob(data, tmp_ctx, &ev, 
259                                       (ndr_pull_flags_fn_t)ndr_pull_notify_event);
260         if (!NT_STATUS_IS_OK(status)) {
261                 talloc_free(tmp_ctx);
262                 return;
263         }
264
265         for (listel=notify->list;listel;listel=listel->next) {
266                 if (listel->private_data == ev.private_data) {
267                         listel->callback(listel->private_data, &ev);
268                         break;
269                 }
270         }
271
272         talloc_free(tmp_ctx);   
273 }
274
275 /*
276   callback from sys_notify telling us about changes from the OS
277 */
278 static void sys_notify_callback(struct sys_notify_context *ctx, 
279                                 void *ptr, struct notify_event *ev)
280 {
281         struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
282         ev->private_data = listel;
283         DEBUG(10, ("sys_notify_callback called with action=%d, for %s\n",
284                    ev->action, ev->path));
285         listel->callback(listel->private_data, ev);
286 }
287
288 /*
289   add an entry to the notify array
290 */
291 static NTSTATUS notify_add_array(struct notify_context *notify, struct db_record *rec,
292                                  struct notify_entry *e,
293                                  void *private_data, int depth)
294 {
295         int i;
296         struct notify_depth *d;
297         struct notify_entry *ee;
298
299         /* possibly expand the depths array */
300         if (depth >= notify->array->num_depths) {
301                 d = talloc_realloc(notify->array, notify->array->depth, 
302                                    struct notify_depth, depth+1);
303                 NT_STATUS_HAVE_NO_MEMORY(d);
304                 for (i=notify->array->num_depths;i<=depth;i++) {
305                         ZERO_STRUCT(d[i]);
306                 }
307                 notify->array->depth = d;
308                 notify->array->num_depths = depth+1;
309         }
310         d = &notify->array->depth[depth];
311
312         /* expand the entries array */
313         ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
314                             d->num_entries+1);
315         NT_STATUS_HAVE_NO_MEMORY(ee);
316         d->entries = ee;
317
318         d->entries[d->num_entries] = *e;
319         d->entries[d->num_entries].private_data = private_data;
320         d->entries[d->num_entries].server = notify->server;
321         d->entries[d->num_entries].path_len = strlen(e->path);
322         d->num_entries++;
323
324         d->max_mask |= e->filter;
325         d->max_mask_subdir |= e->subdir_filter;
326
327         if (d->num_entries > 1) {
328                 qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
329         }
330
331         /* recalculate the maximum masks */
332         d->max_mask = 0;
333         d->max_mask_subdir = 0;
334
335         for (i=0;i<d->num_entries;i++) {
336                 d->max_mask |= d->entries[i].filter;
337                 d->max_mask_subdir |= d->entries[i].subdir_filter;
338         }
339
340         return notify_save(notify, rec);
341 }
342
343 /*
344   add a notify watch. This is called when a notify is first setup on a open
345   directory handle.
346 */
347 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
348                     void (*callback)(void *, const struct notify_event *), 
349                     void *private_data)
350 {
351         struct notify_entry e = *e0;
352         NTSTATUS status;
353         char *tmp_path = NULL;
354         struct notify_list *listel;
355         size_t len;
356         int depth;
357         struct db_record *rec;
358
359         /* see if change notify is enabled at all */
360         if (notify == NULL) {
361                 return NT_STATUS_NOT_IMPLEMENTED;
362         }
363
364         status = notify_fetch_locked(notify, &rec);
365         NT_STATUS_NOT_OK_RETURN(status);
366
367         status = notify_load(notify, rec);
368         if (!NT_STATUS_IS_OK(status)) {
369                 talloc_free(rec);
370                 return status;
371         }
372
373         /* cope with /. on the end of the path */
374         len = strlen(e.path);
375         if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
376                 tmp_path = talloc_strndup(notify, e.path, len-2);
377                 if (tmp_path == NULL) {
378                         status = NT_STATUS_NO_MEMORY;
379                         goto done;
380                 }
381                 e.path = tmp_path;
382         }
383
384         depth = count_chars(e.path, '/');
385
386         listel = TALLOC_ZERO_P(notify, struct notify_list);
387         if (listel == NULL) {
388                 status = NT_STATUS_NO_MEMORY;
389                 goto done;
390         }
391
392         listel->private_data = private_data;
393         listel->callback = callback;
394         listel->depth = depth;
395         DLIST_ADD(notify->list, listel);
396
397         /* ignore failures from sys_notify */
398         if (notify->sys_notify_ctx != NULL) {
399                 /*
400                   this call will modify e.filter and e.subdir_filter
401                   to remove bits handled by the backend
402                 */
403                 status = sys_notify_watch(notify->sys_notify_ctx, &e,
404                                           sys_notify_callback, listel, 
405                                           &listel->sys_notify_handle);
406                 if (NT_STATUS_IS_OK(status)) {
407                         talloc_steal(listel, listel->sys_notify_handle);
408                 }
409         }
410
411         /* if the system notify handler couldn't handle some of the
412            filter bits, or couldn't handle a request for recursion
413            then we need to install it in the array used for the
414            intra-samba notify handling */
415         if (e.filter != 0 || e.subdir_filter != 0) {
416                 status = notify_add_array(notify, rec, &e, private_data, depth);
417         }
418
419 done:
420         talloc_free(rec);
421         talloc_free(tmp_path);
422
423         return status;
424 }
425
426 /*
427   remove a notify watch. Called when the directory handle is closed
428 */
429 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
430 {
431         NTSTATUS status;
432         struct notify_list *listel;
433         int i, depth;
434         struct notify_depth *d;
435         struct db_record *rec;
436
437         /* see if change notify is enabled at all */
438         if (notify == NULL) {
439                 return NT_STATUS_NOT_IMPLEMENTED;
440         }
441
442         for (listel=notify->list;listel;listel=listel->next) {
443                 if (listel->private_data == private_data) {
444                         DLIST_REMOVE(notify->list, listel);
445                         break;
446                 }
447         }
448         if (listel == NULL) {
449                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
450         }
451
452         depth = listel->depth;
453
454         talloc_free(listel);
455
456         status = notify_fetch_locked(notify, &rec);
457         NT_STATUS_NOT_OK_RETURN(status);
458
459         status = notify_load(notify, rec);
460         if (!NT_STATUS_IS_OK(status)) {
461                 talloc_free(rec);
462                 return status;
463         }
464
465         if (depth >= notify->array->num_depths) {
466                 talloc_free(rec);
467                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
468         }
469
470         /* we only have to search at the depth of this element */
471         d = &notify->array->depth[depth];
472
473         for (i=0;i<d->num_entries;i++) {
474                 if (private_data == d->entries[i].private_data &&
475                     cluster_id_equal(&notify->server, &d->entries[i].server)) {
476                         break;
477                 }
478         }
479         if (i == d->num_entries) {
480                 talloc_free(rec);
481                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
482         }
483
484         if (i < d->num_entries-1) {
485                 memmove(&d->entries[i], &d->entries[i+1], 
486                         sizeof(d->entries[i])*(d->num_entries-(i+1)));
487         }
488         d->num_entries--;
489
490         status = notify_save(notify, rec);
491
492         talloc_free(rec);
493
494         return status;
495 }
496
497 /*
498   remove all notify watches for a messaging server
499 */
500 static NTSTATUS notify_remove_all(struct notify_context *notify,
501                                   const struct server_id *server)
502 {
503         NTSTATUS status;
504         int i, depth, del_count=0;
505         struct db_record *rec;
506
507         status = notify_fetch_locked(notify, &rec);
508         NT_STATUS_NOT_OK_RETURN(status);
509
510         status = notify_load(notify, rec);
511         if (!NT_STATUS_IS_OK(status)) {
512                 talloc_free(rec);
513                 return status;
514         }
515
516         /* we have to search for all entries across all depths, looking for matches
517            for the server id */
518         for (depth=0;depth<notify->array->num_depths;depth++) {
519                 struct notify_depth *d = &notify->array->depth[depth];
520                 for (i=0;i<d->num_entries;i++) {
521                         if (cluster_id_equal(server, &d->entries[i].server)) {
522                                 if (i < d->num_entries-1) {
523                                         memmove(&d->entries[i], &d->entries[i+1], 
524                                                 sizeof(d->entries[i])*(d->num_entries-(i+1)));
525                                 }
526                                 i--;
527                                 d->num_entries--;
528                                 del_count++;
529                         }
530                 }
531         }
532
533         if (del_count > 0) {
534                 status = notify_save(notify, rec);
535         }
536
537         talloc_free(rec);
538
539         return status;
540 }
541
542
543 /*
544   send a notify message to another messaging server
545 */
546 static NTSTATUS notify_send(struct notify_context *notify, struct notify_entry *e,
547                             const char *path, uint32_t action)
548 {
549         struct notify_event ev;
550         DATA_BLOB data;
551         NTSTATUS status;
552         TALLOC_CTX *tmp_ctx;
553
554         ev.action = action;
555         ev.path = path;
556         ev.private_data = e->private_data;
557
558         tmp_ctx = talloc_new(notify);
559
560         status = ndr_push_struct_blob(&data, tmp_ctx, &ev, 
561                                       (ndr_push_flags_fn_t)ndr_push_notify_event);
562         if (!NT_STATUS_IS_OK(status)) {
563                 talloc_free(tmp_ctx);
564                 return status;
565         }
566
567         status = messaging_send(notify->messaging_ctx, e->server, 
568                                 MSG_PVFS_NOTIFY, &data);
569         talloc_free(tmp_ctx);
570         return status;
571 }
572
573
574 /*
575   trigger a notify message for anyone waiting on a matching event
576
577   This function is called a lot, and needs to be very fast. The unusual data structure
578   and traversal is designed to be fast in the average case, even for large numbers of
579   notifies
580 */
581 void notify_trigger(struct notify_context *notify,
582                     uint32_t action, uint32_t filter, const char *path)
583 {
584         NTSTATUS status;
585         int depth;
586         const char *p, *next_p;
587
588         DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
589                    "path=%s\n", (unsigned)action, (unsigned)filter, path));
590
591         /* see if change notify is enabled at all */
592         if (notify == NULL) {
593                 return;
594         }
595
596  again:
597         status = notify_load(notify, NULL);
598         if (!NT_STATUS_IS_OK(status)) {
599                 return;
600         }
601
602         /* loop along the given path, working with each directory depth separately */
603         for (depth=0,p=path;
604              p && depth < notify->array->num_depths;
605              p=next_p,depth++) {
606                 int p_len = p - path;
607                 int min_i, max_i, i;
608                 struct notify_depth *d = &notify->array->depth[depth];
609                 next_p = strchr(p+1, '/');
610
611                 /* see if there are any entries at this depth */
612                 if (d->num_entries == 0) continue;
613                 
614                 /* try to skip based on the maximum mask. If next_p is
615                  NULL then we know it will be a 'this directory'
616                  match, otherwise it must be a subdir match */
617                 if (next_p != NULL) {
618                         if (0 == (filter & d->max_mask_subdir)) {
619                                 continue;
620                         }
621                 } else {
622                         if (0 == (filter & d->max_mask)) {
623                                 continue;
624                         }
625                 }
626
627                 /* we know there is an entry here worth looking
628                  for. Use a bisection search to find the first entry
629                  with a matching path */
630                 min_i = 0;
631                 max_i = d->num_entries-1;
632
633                 while (min_i < max_i) {
634                         struct notify_entry *e;
635                         int cmp;
636                         i = (min_i+max_i)/2;
637                         e = &d->entries[i];
638                         cmp = strncmp(path, e->path, p_len);
639                         if (cmp == 0) {
640                                 if (p_len == e->path_len) {
641                                         max_i = i;
642                                 } else {
643                                         max_i = i-1;
644                                 }
645                         } else if (cmp < 0) {
646                                 max_i = i-1;
647                         } else {
648                                 min_i = i+1;
649                         }
650                 }
651
652                 if (min_i != max_i) {
653                         /* none match */
654                         continue;
655                 }
656
657                 /* we now know that the entries start at min_i */
658                 for (i=min_i;i<d->num_entries;i++) {
659                         struct notify_entry *e = &d->entries[i];
660                         if (p_len != e->path_len ||
661                             strncmp(path, e->path, p_len) != 0) break;
662                         if (next_p != NULL) {
663                                 if (0 == (filter & e->subdir_filter)) {
664                                         continue;
665                                 }
666                         } else {
667                                 if (0 == (filter & e->filter)) {
668                                         continue;
669                                 }
670                         }
671                         status = notify_send(notify, e, path + e->path_len + 1,
672                                              action);
673
674                         if (NT_STATUS_EQUAL(
675                                     status, NT_STATUS_INVALID_HANDLE)) {
676                                 struct server_id server = e->server;
677
678                                 DEBUG(10, ("Deleting notify entries for "
679                                            "process %s because it's gone\n",
680                                            procid_str_static(&e->server)));
681                                 notify_remove_all(notify, &server);
682                                 goto again;
683                         }
684                 }
685         }
686 }