2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2004
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.
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.
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.
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.
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
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.
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.
43 #include "librpc/gen_ndr/ndr_security.h"
48 struct messaging_context *messaging_ctx;
52 the database is indexed by a file_key, and contains entries of the
59 uint32_t share_access;
60 uint32_t create_options;
68 an odb lock handle. You must obtain one of these using odb_lock() before doing
72 struct odb_context *odb;
77 Open up the openfiles.tdb database. Close it down using
78 talloc_free(). We need the messaging_ctx to allow for pending open
81 struct odb_context *odb_init(TALLOC_CTX *mem_ctx, servid_t server,
82 struct messaging_context *messaging_ctx)
85 struct odb_context *odb;
87 odb = talloc_p(mem_ctx, struct odb_context);
92 path = smbd_tmp_path(odb, "openfiles.tdb");
93 odb->w = tdb_wrap_open(odb, path, 0,
95 O_RDWR|O_CREAT, 0600);
102 odb->server = server;
103 odb->messaging_ctx = messaging_ctx;
109 destroy a lock on the database
111 static int odb_lock_destructor(void *ptr)
113 struct odb_lock *lck = ptr;
114 tdb_chainunlock(lck->odb->w->tdb, lck->key);
119 get a lock on a entry in the odb. This call returns a lock handle,
120 which the caller should unlock using talloc_free().
122 struct odb_lock *odb_lock(TALLOC_CTX *mem_ctx,
123 struct odb_context *odb, DATA_BLOB *file_key)
125 struct odb_lock *lck;
127 lck = talloc_p(mem_ctx, struct odb_lock);
132 lck->odb = talloc_reference(lck, odb);
133 lck->key.dptr = talloc_memdup(lck, file_key->data, file_key->length);
134 lck->key.dsize = file_key->length;
135 if (lck->key.dptr == NULL) {
140 if (tdb_chainlock(odb->w->tdb, lck->key) != 0) {
145 talloc_set_destructor(lck, odb_lock_destructor);
151 determine if two odb_entry structures conflict
153 static BOOL share_conflict(struct odb_entry *e1, struct odb_entry *e2)
155 #define CHECK_MASK(am, sa, right, share) if (((am) & (right)) && !((sa) & (share))) return True
157 if (e1->pending || e2->pending) return False;
159 /* if either open involves no read.write or delete access then
161 if (!(e1->access_mask & (SEC_FILE_WRITE_DATA |
162 SEC_FILE_APPEND_DATA |
168 if (!(e2->access_mask & (SEC_FILE_WRITE_DATA |
169 SEC_FILE_APPEND_DATA |
176 /* data IO access masks. This is skipped if the two open handles
177 are on different streams (as in that case the masks don't
179 if (e1->stream_id != e2->stream_id) {
183 CHECK_MASK(e1->access_mask, e2->share_access,
184 SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
185 NTCREATEX_SHARE_ACCESS_WRITE);
186 CHECK_MASK(e2->access_mask, e1->share_access,
187 SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA,
188 NTCREATEX_SHARE_ACCESS_WRITE);
190 CHECK_MASK(e1->access_mask, e2->share_access,
191 SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
192 NTCREATEX_SHARE_ACCESS_READ);
193 CHECK_MASK(e2->access_mask, e1->share_access,
194 SEC_FILE_READ_DATA | SEC_FILE_EXECUTE,
195 NTCREATEX_SHARE_ACCESS_READ);
197 CHECK_MASK(e1->access_mask, e2->share_access,
199 NTCREATEX_SHARE_ACCESS_DELETE);
200 CHECK_MASK(e2->access_mask, e1->share_access,
202 NTCREATEX_SHARE_ACCESS_DELETE);
204 /* if a delete is pending then a second open is not allowed */
205 if ((e1->create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) ||
206 (e2->create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE)) {
214 register an open file in the open files database. This implements the share_access
217 NTSTATUS odb_open_file(struct odb_lock *lck, void *file_handle,
219 uint32_t share_access, uint32_t create_options,
220 uint32_t access_mask)
222 struct odb_context *odb = lck->odb;
227 struct odb_entry *elist;
229 dbuf = tdb_fetch(odb->w->tdb, lck->key);
231 e.server = odb->server;
232 e.file_handle = file_handle;
233 e.stream_id = stream_id;
234 e.share_access = share_access;
235 e.create_options = create_options;
236 e.access_mask = access_mask;
240 /* check the existing file opens to see if they
242 elist = (struct odb_entry *)dbuf.dptr;
243 count = dbuf.dsize / sizeof(struct odb_entry);
245 for (i=0;i<count;i++) {
246 if (share_conflict(elist+i, &e)) {
247 if (dbuf.dptr) free(dbuf.dptr);
248 return NT_STATUS_SHARING_VIOLATION;
252 tp = Realloc(dbuf.dptr, (count+1) * sizeof(struct odb_entry));
254 if (dbuf.dptr) free(dbuf.dptr);
255 return NT_STATUS_NO_MEMORY;
259 dbuf.dsize = (count+1) * sizeof(struct odb_entry);
261 memcpy(dbuf.dptr + (count*sizeof(struct odb_entry)),
262 &e, sizeof(struct odb_entry));
264 if (tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE) != 0) {
266 return NT_STATUS_INTERNAL_DB_CORRUPTION;
275 register a pending open file in the open files database
277 NTSTATUS odb_open_file_pending(struct odb_lock *lck, void *private)
279 struct odb_context *odb = lck->odb;
283 struct odb_entry *elist;
286 dbuf = tdb_fetch(odb->w->tdb, lck->key);
288 e.server = odb->server;
289 e.file_handle = NULL;
292 e.create_options = 0;
294 e.notify_ptr = private;
297 /* check the existing file opens to see if they
299 elist = (struct odb_entry *)dbuf.dptr;
300 count = dbuf.dsize / sizeof(struct odb_entry);
302 tp = Realloc(dbuf.dptr, (count+1) * sizeof(struct odb_entry));
304 if (dbuf.dptr) free(dbuf.dptr);
305 return NT_STATUS_NO_MEMORY;
309 dbuf.dsize = (count+1) * sizeof(struct odb_entry);
311 memcpy(dbuf.dptr + (count*sizeof(struct odb_entry)),
312 &e, sizeof(struct odb_entry));
314 if (tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE) != 0) {
316 return NT_STATUS_INTERNAL_DB_CORRUPTION;
325 remove a opendb entry
327 NTSTATUS odb_close_file(struct odb_lock *lck, void *file_handle)
329 struct odb_context *odb = lck->odb;
331 struct odb_entry *elist;
335 dbuf = tdb_fetch(odb->w->tdb, lck->key);
337 if (dbuf.dptr == NULL) {
338 return NT_STATUS_UNSUCCESSFUL;
341 elist = (struct odb_entry *)dbuf.dptr;
342 count = dbuf.dsize / sizeof(struct odb_entry);
344 /* send any pending notifications, removing them once sent */
345 for (i=0;i<count;i++) {
346 if (elist[i].pending) {
347 messaging_send_ptr(odb->messaging_ctx, elist[i].server,
348 MSG_PVFS_RETRY_OPEN, elist[i].notify_ptr);
349 memmove(&elist[i], &elist[i+1], sizeof(struct odb_entry)*(count-(i+1)));
355 /* find the entry, and delete it */
356 for (i=0;i<count;i++) {
357 if (file_handle == elist[i].file_handle &&
358 odb->server == elist[i].server) {
360 memmove(elist+i, elist+i+1,
361 (count - (i+1)) * sizeof(struct odb_entry));
367 status = NT_STATUS_OK;
370 status = NT_STATUS_UNSUCCESSFUL;
371 } else if (count == 1) {
372 if (tdb_delete(odb->w->tdb, lck->key) != 0) {
373 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
376 dbuf.dsize = (count-1) * sizeof(struct odb_entry);
377 if (tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE) != 0) {
378 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
389 remove a pending opendb entry
391 NTSTATUS odb_remove_pending(struct odb_lock *lck, void *private)
393 struct odb_context *odb = lck->odb;
395 struct odb_entry *elist;
399 dbuf = tdb_fetch(odb->w->tdb, lck->key);
401 if (dbuf.dptr == NULL) {
402 return NT_STATUS_UNSUCCESSFUL;
405 elist = (struct odb_entry *)dbuf.dptr;
406 count = dbuf.dsize / sizeof(struct odb_entry);
408 /* find the entry, and delete it */
409 for (i=0;i<count;i++) {
410 if (private == elist[i].notify_ptr &&
411 odb->server == elist[i].server) {
413 memmove(elist+i, elist+i+1,
414 (count - (i+1)) * sizeof(struct odb_entry));
420 status = NT_STATUS_OK;
423 status = NT_STATUS_UNSUCCESSFUL;
424 } else if (count == 1) {
425 if (tdb_delete(odb->w->tdb, lck->key) != 0) {
426 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
429 dbuf.dsize = (count-1) * sizeof(struct odb_entry);
430 if (tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE) != 0) {
431 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
442 update create options on an open file
444 NTSTATUS odb_set_create_options(struct odb_lock *lck,
445 void *file_handle, uint32_t create_options)
447 struct odb_context *odb = lck->odb;
449 struct odb_entry *elist;
453 dbuf = tdb_fetch(odb->w->tdb, lck->key);
454 if (dbuf.dptr == NULL) {
455 return NT_STATUS_UNSUCCESSFUL;
458 elist = (struct odb_entry *)dbuf.dptr;
459 count = dbuf.dsize / sizeof(struct odb_entry);
461 /* find the entry, and modify it */
462 for (i=0;i<count;i++) {
463 if (file_handle == elist[i].file_handle &&
464 odb->server == elist[i].server) {
465 elist[i].create_options = create_options;
470 if (tdb_store(odb->w->tdb, lck->key, dbuf, TDB_REPLACE) != 0) {
471 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
473 status = NT_STATUS_OK;
483 determine if a file can be opened with the given share_access,
484 create_options and access_mask
486 NTSTATUS odb_can_open(struct odb_context *odb, DATA_BLOB *key,
487 uint32_t share_access, uint32_t create_options,
488 uint32_t access_mask)
492 struct odb_entry *elist;
496 kbuf.dptr = (char *)key->data;
497 kbuf.dsize = key->length;
499 dbuf = tdb_fetch(odb->w->tdb, kbuf);
500 if (dbuf.dptr == NULL) {
504 elist = (struct odb_entry *)dbuf.dptr;
505 count = dbuf.dsize / sizeof(struct odb_entry);
512 e.server = odb->server;
513 e.file_handle = NULL;
515 e.share_access = share_access;
516 e.create_options = create_options;
517 e.access_mask = access_mask;
521 for (i=0;i<count;i++) {
522 if (share_conflict(elist+i, &e)) {
523 if (dbuf.dptr) free(dbuf.dptr);
524 return NT_STATUS_SHARING_VIOLATION;