r13739: a fairly major overhaul of the opendb code to allow the BASE-DELETE
[jelmer/samba4-debian.git] / source / ntvfs / common / opendb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2004
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 open files database. It implements shared storage of
23   what files are open between server instances, and implements the rules
24   of shared access to files.
25
26   The caller needs to provide a file_key, which specifies what file
27   they are talking about. This needs to be a unique key across all
28   filesystems, and is usually implemented in terms of a device/inode
29   pair.
30
31   Before any operations can be performed the caller needs to establish
32   a lock on the record associated with file_key. That is done by
33   calling odb_lock(). The caller releases this lock by calling
34   talloc_free() on the returned handle.
35
36   All other operations on a record are done by passing the odb_lock()
37   handle back to this module. The handle contains internal
38   information about what file_key is being operated on.
39 */
40
41 #include "includes.h"
42 #include "system/filesys.h"
43 #include "lib/tdb/include/tdb.h"
44 #include "messaging/messaging.h"
45 #include "librpc/gen_ndr/ndr_security.h"
46 #include "db_wrap.h"
47 #include "smb_server/smb_server.h"
48 #include "lib/messaging/irpc.h"
49 #include "librpc/gen_ndr/ndr_opendb.h"
50
51 struct odb_context {
52         struct tdb_wrap *w;
53         uint32_t server;
54         struct messaging_context *messaging_ctx;
55 };
56
57 /*
58   an odb lock handle. You must obtain one of these using odb_lock() before doing
59   any other operations. 
60 */
61 struct odb_lock {
62         struct odb_context *odb;
63         TDB_DATA key;
64 };
65
66 /*
67   Open up the openfiles.tdb database. Close it down using
68   talloc_free(). We need the messaging_ctx to allow for pending open
69   notifications.
70 */
71 struct odb_context *odb_init(TALLOC_CTX *mem_ctx, uint32_t server, 
72                              struct messaging_context *messaging_ctx)
73 {
74         char *path;
75         struct odb_context *odb;
76
77         odb = talloc(mem_ctx, struct odb_context);
78         if (odb == NULL) {
79                 return NULL;
80         }
81
82         path = smbd_tmp_path(odb, "openfiles.tdb");
83         odb->w = tdb_wrap_open(odb, path, 0,  
84                                TDB_DEFAULT,
85                                O_RDWR|O_CREAT, 0600);
86         talloc_free(path);
87         if (odb->w == NULL) {
88                 talloc_free(odb);
89                 return NULL;
90         }
91
92         odb->server = server;
93         odb->messaging_ctx = messaging_ctx;
94
95         return odb;
96 }
97
98 /*
99   destroy a lock on the database
100 */
101 static int odb_lock_destructor(void *ptr)
102 {
103         struct odb_lock *lck = ptr;
104         tdb_chainunlock(lck->odb->w->tdb, lck->key);
105         return 0;
106 }
107
108 /*
109   get a lock on a entry in the odb. This call returns a lock handle,
110   which the caller should unlock using talloc_free().
111 */
112 struct odb_lock *odb_lock(TALLOC_CTX *mem_ctx,
113                           struct odb_context *odb, DATA_BLOB *file_key)
114 {
115         struct odb_lock *lck;
116
117         lck = talloc(mem_ctx, struct odb_lock);
118         if (lck == NULL) {
119                 return NULL;
120         }
121
122         lck->odb = talloc_reference(lck, odb);
123         lck->key.dptr = talloc_memdup(lck, file_key->data, file_key->length);
124         lck->key.dsize = file_key->length;
125         if (lck->key.dptr == NULL) {
126                 talloc_free(lck);
127                 return NULL;
128         }
129
130         if (tdb_chainlock(odb->w->tdb, lck->key) != 0) {
131                 talloc_free(lck);
132                 return NULL;
133         }
134
135         talloc_set_destructor(lck, odb_lock_destructor);
136         
137         return lck;
138 }
139
140 /*
141   determine if two odb_entry structures conflict
142
143   return NT_STATUS_OK on no conflict
144 */
145 static NTSTATUS share_conflict(struct opendb_entry *e1, struct opendb_entry *e2)
146 {
147         /* if either open involves no read.write or delete access then
148            it can't conflict */
149         if (!(e1->access_mask & (SEC_FILE_WRITE_DATA |
150                                  SEC_FILE_APPEND_DATA |
151                                  SEC_FILE_READ_DATA |
152                                  SEC_FILE_EXECUTE |
153                                  SEC_STD_DELETE))) {
154                 return NT_STATUS_OK;
155         }
156         if (!(e2->access_mask & (SEC_FILE_WRITE_DATA |
157                                  SEC_FILE_APPEND_DATA |
158                                  SEC_FILE_READ_DATA |
159                                  SEC_FILE_EXECUTE |
160                                  SEC_STD_DELETE))) {
161                 return NT_STATUS_OK;
162         }
163
164         /* data IO access masks. This is skipped if the two open handles
165            are on different streams (as in that case the masks don't
166            interact) */
167         if (e1->stream_id != e2->stream_id) {
168                 return NT_STATUS_OK;
169         }
170
171 #define CHECK_MASK(am, right, sa, share) \
172         if (((am) & (right)) && !((sa) & (share))) return NT_STATUS_SHARING_VIOLATION
173
174         CHECK_MASK(e1->access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
175                    e2->share_access, NTCREATEX_SHARE_ACCESS_WRITE);
176         CHECK_MASK(e2->access_mask, SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
177                    e1->share_access, NTCREATEX_SHARE_ACCESS_WRITE);
178         
179         CHECK_MASK(e1->access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
180                    e2->share_access, NTCREATEX_SHARE_ACCESS_READ);
181         CHECK_MASK(e2->access_mask, SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
182                    e1->share_access, NTCREATEX_SHARE_ACCESS_READ);
183
184         CHECK_MASK(e1->access_mask, SEC_STD_DELETE,
185                    e2->share_access, NTCREATEX_SHARE_ACCESS_DELETE);
186         CHECK_MASK(e2->access_mask, SEC_STD_DELETE,
187                    e1->share_access, NTCREATEX_SHARE_ACCESS_DELETE);
188
189         return NT_STATUS_OK;
190 }
191
192 /*
193   pull a record, translating from the db format to the opendb_file structure defined
194   in opendb.idl
195 */
196 static NTSTATUS odb_pull_record(struct odb_lock *lck, struct opendb_file *file)
197 {
198         struct odb_context *odb = lck->odb;
199         TDB_DATA dbuf;
200         DATA_BLOB blob;
201         NTSTATUS status;
202                 
203         dbuf = tdb_fetch(odb->w->tdb, lck->key);
204         if (dbuf.dptr == NULL) {
205                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
206         }
207
208         blob.data = dbuf.dptr;
209         blob.length = dbuf.dsize;
210
211         status = ndr_pull_struct_blob(&blob, lck, file, (ndr_pull_flags_fn_t)ndr_pull_opendb_file);
212
213         free(dbuf.dptr);
214
215         return status;
216 }
217
218 /*
219   push a record, translating from the opendb_file structure defined in opendb.idl
220 */
221 static NTSTATUS odb_push_record(struct odb_lock *lck, struct opendb_file *file)
222 {
223         struct odb_context *odb = lck->odb;
224         TDB_DATA dbuf;
225         DATA_BLOB blob;
226         NTSTATUS status;
227         int ret;
228
229         if (file->num_entries == 0) {
230                 ret = tdb_delete(odb->w->tdb, lck->key);
231                 if (ret != 0) {
232                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
233                 }
234                 return NT_STATUS_OK;
235         }
236
237         status = ndr_push_struct_blob(&blob, lck, file, (ndr_push_flags_fn_t)ndr_push_opendb_file);
238         NT_STATUS_NOT_OK_RETURN(status);
239
240         dbuf.dptr = blob.data;
241         dbuf.dsize = blob.length;
242                 
243         ret = tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE);
244         data_blob_free(&blob);
245         if (ret != 0) {
246                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
247         }
248
249         return NT_STATUS_OK;
250 }
251
252
253 /*
254   register an open file in the open files database. This implements the share_access
255   rules
256
257   Note that the path is only used by the delete on close logic, not
258   for comparing with other filenames
259 */
260 NTSTATUS odb_open_file(struct odb_lock *lck, void *file_handle,
261                        uint32_t stream_id, uint32_t share_access, 
262                        uint32_t access_mask, BOOL delete_on_close,
263                        const char *path)
264 {
265         struct odb_context *odb = lck->odb;
266         struct opendb_entry e;
267         int i;
268         struct opendb_file file;
269         NTSTATUS status;
270
271         status = odb_pull_record(lck, &file);
272         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
273                 /* initialise a blank structure */
274                 ZERO_STRUCT(file);
275                 file.path = path;
276         } else {
277                 NT_STATUS_NOT_OK_RETURN(status);
278         }
279
280
281         if (file.delete_on_close || 
282             (file.num_entries != 0 && delete_on_close)) {
283                 /* while delete on close is set, no new opens are allowed */
284                 return NT_STATUS_DELETE_PENDING;
285         }
286
287         /* see if it conflicts */
288         e.server          = odb->server;
289         e.file_handle     = file_handle;
290         e.stream_id       = stream_id;
291         e.share_access    = share_access;
292         e.access_mask     = access_mask;
293         e.delete_on_close = delete_on_close;
294                 
295         for (i=0;i<file.num_entries;i++) {
296                 status = share_conflict(&file.entries[i], &e);
297                 NT_STATUS_NOT_OK_RETURN(status);
298         }
299
300         /* it doesn't, so add it to the end */
301         file.entries = talloc_realloc(lck, file.entries, struct opendb_entry, 
302                                       file.num_entries+1);
303         NT_STATUS_HAVE_NO_MEMORY(file.entries);
304
305         file.entries[file.num_entries] = e;
306         file.num_entries++;
307
308         return odb_push_record(lck, &file);
309 }
310
311
312 /*
313   register a pending open file in the open files database
314 */
315 NTSTATUS odb_open_file_pending(struct odb_lock *lck, void *private)
316 {
317         struct odb_context *odb = lck->odb;
318         struct opendb_file file;
319         NTSTATUS status;
320                 
321         status = odb_pull_record(lck, &file);
322         NT_STATUS_NOT_OK_RETURN(status);
323
324         file.pending = talloc_realloc(lck, file.pending, struct opendb_pending, 
325                                       file.num_pending+1);
326         NT_STATUS_HAVE_NO_MEMORY(file.pending);
327
328         file.pending[file.num_pending].server = odb->server;
329         file.pending[file.num_pending].notify_ptr = private;
330
331         file.num_pending++;
332
333         return odb_push_record(lck, &file);
334 }
335
336
337 /*
338   remove a opendb entry
339 */
340 NTSTATUS odb_close_file(struct odb_lock *lck, void *file_handle)
341 {
342         struct odb_context *odb = lck->odb;
343         struct opendb_file file;
344         int i;
345         NTSTATUS status;
346
347         status = odb_pull_record(lck, &file);
348         NT_STATUS_NOT_OK_RETURN(status);
349
350         /* find the entry, and delete it */
351         for (i=0;i<file.num_entries;i++) {
352                 if (file_handle == file.entries[i].file_handle &&
353                     odb->server == file.entries[i].server) {
354                         if (file.entries[i].delete_on_close) {
355                                 file.delete_on_close = True;
356                         }
357                         if (i < file.num_entries-1) {
358                                 memmove(file.entries+i, file.entries+i+1, 
359                                         (file.num_entries - (i+1)) * 
360                                         sizeof(struct opendb_entry));
361                         }
362                         break;
363                 }
364         }
365
366         if (i == file.num_entries) {
367                 return NT_STATUS_UNSUCCESSFUL;
368         }
369
370         /* send any pending notifications, removing them once sent */
371         for (i=0;i<file.num_pending;i++) {
372                 messaging_send_ptr(odb->messaging_ctx, file.pending[i].server, 
373                                    MSG_PVFS_RETRY_OPEN, 
374                                    file.pending[i].notify_ptr);
375         }
376         file.num_pending = 0;
377
378         file.num_entries--;
379         
380         return odb_push_record(lck, &file);
381 }
382
383
384 /*
385   remove a pending opendb entry
386 */
387 NTSTATUS odb_remove_pending(struct odb_lock *lck, void *private)
388 {
389         struct odb_context *odb = lck->odb;
390         int i;
391         NTSTATUS status;
392         struct opendb_file file;
393
394         status = odb_pull_record(lck, &file);
395         NT_STATUS_NOT_OK_RETURN(status);
396
397         /* find the entry, and delete it */
398         for (i=0;i<file.num_pending;i++) {
399                 if (private == file.pending[i].notify_ptr &&
400                     odb->server == file.pending[i].server) {
401                         if (i < file.num_pending-1) {
402                                 memmove(file.pending+i, file.pending+i+1, 
403                                         (file.num_pending - (i+1)) * 
404                                         sizeof(struct opendb_pending));
405                         }
406                         break;
407                 }
408         }
409
410         if (i == file.num_pending) {
411                 return NT_STATUS_UNSUCCESSFUL;
412         }
413
414         file.num_pending--;
415         
416         return odb_push_record(lck, &file);
417 }
418
419
420 /*
421   rename the path in a open file
422 */
423 NTSTATUS odb_rename(struct odb_lock *lck, const char *path)
424 {
425         struct opendb_file file;
426         NTSTATUS status;
427
428         status = odb_pull_record(lck, &file);
429         if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
430                 /* not having the record at all is OK */
431                 return NT_STATUS_OK;
432         }
433         NT_STATUS_NOT_OK_RETURN(status);
434
435         file.path = path;
436         return odb_push_record(lck, &file);
437 }
438
439 /*
440   update delete on close flag on an open file
441 */
442 NTSTATUS odb_set_delete_on_close(struct odb_lock *lck, BOOL del_on_close)
443 {
444         NTSTATUS status;
445         struct opendb_file file;
446
447         status = odb_pull_record(lck, &file);
448         NT_STATUS_NOT_OK_RETURN(status);
449
450         file.delete_on_close = del_on_close;
451
452         return odb_push_record(lck, &file);
453 }
454
455 /*
456   return the current value of the delete_on_close bit, and how many
457   people still have the file open
458 */
459 NTSTATUS odb_get_delete_on_close(struct odb_context *odb, 
460                                  DATA_BLOB *key, BOOL *del_on_close, 
461                                  int *open_count, char **path)
462 {
463         NTSTATUS status;
464         struct opendb_file file;
465         struct odb_lock *lck;
466
467         lck = odb_lock(odb, odb, key);
468         NT_STATUS_HAVE_NO_MEMORY(lck);
469
470         status = odb_pull_record(lck, &file);
471         if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
472                 talloc_free(lck);
473                 (*del_on_close) = False;
474                 return NT_STATUS_OK;
475         }
476         if (!NT_STATUS_IS_OK(status)) {
477                 talloc_free(lck);
478                 return status;
479         }
480
481         (*del_on_close) = file.delete_on_close;
482         if (open_count != NULL) {
483                 (*open_count) = file.num_entries;
484         }
485         if (path != NULL) {
486                 *path = talloc_strdup(odb, file.path);
487                 NT_STATUS_HAVE_NO_MEMORY(*path);
488                 if (file.num_entries == 1 && file.entries[0].delete_on_close) {
489                         (*del_on_close) = True;
490                 }
491         }
492
493         talloc_free(lck);
494
495         return NT_STATUS_OK;
496 }
497
498
499 /*
500   determine if a file can be opened with the given share_access,
501   create_options and access_mask
502 */
503 NTSTATUS odb_can_open(struct odb_lock *lck,
504                       uint32_t share_access, uint32_t create_options, 
505                       uint32_t access_mask)
506 {
507         struct odb_context *odb = lck->odb;
508         NTSTATUS status;
509         struct opendb_file file;
510         struct opendb_entry e;
511         int i;
512
513         status = odb_pull_record(lck, &file);
514         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
515                 return NT_STATUS_OK;
516         }
517         NT_STATUS_NOT_OK_RETURN(status);
518
519         if ((create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) && 
520             file.num_entries != 0) {
521                 return NT_STATUS_SHARING_VIOLATION;
522         }
523
524         if (file.delete_on_close) {
525                 return NT_STATUS_DELETE_PENDING;
526         }
527
528         e.server       = odb->server;
529         e.file_handle  = NULL;
530         e.stream_id    = 0;
531         e.share_access = share_access;
532         e.access_mask  = access_mask;
533                 
534         for (i=0;i<file.num_entries;i++) {
535                 status = share_conflict(&file.entries[i], &e);
536                 if (!NT_STATUS_IS_OK(status)) {
537                         /* note that we discard the error code
538                            here. We do this as unless we are actually
539                            doing an open (which comes via a sdifferent
540                            function), we need to return a sharing
541                            violation */
542                         return NT_STATUS_SHARING_VIOLATION;
543                 }
544         }
545
546         return NT_STATUS_OK;
547 }