ntlm_auth4: Use new samba_getpass() function.
[Samba/wip.git] / source3 / locking / share_mode_lock.c
blob4f26099f6043893ab0d237bf9209f82575977dfe
1 /*
2 Unix SMB/CIFS implementation.
3 Locking functions
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1992-2006
6 Copyright (C) Volker Lendecke 2005
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.
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.
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/>.
21 Revision History:
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27 locking to deal with multiple share modes per open file.
29 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30 support.
32 rewritten completely to use new tdb code. Tridge, Dec '99
34 Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
35 Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
38 #include "includes.h"
39 #include "system/filesys.h"
40 #include "locking/proto.h"
41 #include "smbd/globals.h"
42 #include "dbwrap/dbwrap.h"
43 #include "dbwrap/dbwrap_open.h"
44 #include "../libcli/security/security.h"
45 #include "serverid.h"
46 #include "messages.h"
47 #include "util_tdb.h"
48 #include "../librpc/gen_ndr/ndr_open_files.h"
50 #undef DBGC_CLASS
51 #define DBGC_CLASS DBGC_LOCKING
53 #define NO_LOCKING_COUNT (-1)
55 /* the locking database handle */
56 static struct db_context *lock_db;
58 static bool locking_init_internal(bool read_only)
60 brl_init(read_only);
62 if (lock_db)
63 return True;
65 lock_db = db_open(NULL, lock_path("locking.tdb"),
66 lp_open_files_db_hash_size(),
67 TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
68 read_only?O_RDONLY:O_RDWR|O_CREAT, 0644,
69 DBWRAP_LOCK_ORDER_1);
71 if (!lock_db) {
72 DEBUG(0,("ERROR: Failed to initialise locking database\n"));
73 return False;
76 if (!posix_locking_init(read_only))
77 return False;
79 return True;
82 bool locking_init(void)
84 return locking_init_internal(false);
87 bool locking_init_readonly(void)
89 return locking_init_internal(true);
92 /*******************************************************************
93 Deinitialize the share_mode management.
94 ******************************************************************/
96 bool locking_end(void)
98 brl_shutdown();
99 TALLOC_FREE(lock_db);
100 return true;
103 /*******************************************************************
104 Form a static locking key for a dev/inode pair.
105 ******************************************************************/
107 static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
109 *tmp = *id;
110 return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
113 /*******************************************************************
114 Get all share mode entries for a dev/inode pair.
115 ********************************************************************/
117 static struct share_mode_data *parse_share_modes(TALLOC_CTX *mem_ctx,
118 const TDB_DATA dbuf)
120 struct share_mode_data *d;
121 enum ndr_err_code ndr_err;
122 DATA_BLOB blob;
124 d = talloc(mem_ctx, struct share_mode_data);
125 if (d == NULL) {
126 DEBUG(0, ("talloc failed\n"));
127 goto fail;
130 blob.data = dbuf.dptr;
131 blob.length = dbuf.dsize;
133 ndr_err = ndr_pull_struct_blob(
134 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
135 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
136 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
137 goto fail;
140 d->modified = false;
141 d->fresh = false;
143 if (DEBUGLEVEL >= 10) {
144 DEBUG(10, ("parse_share_modes:\n"));
145 NDR_PRINT_DEBUG(share_mode_data, d);
148 return d;
149 fail:
150 TALLOC_FREE(d);
151 return NULL;
154 /*******************************************************************
155 Create a storable data blob from a modified share_mode_data struct.
156 ********************************************************************/
158 static TDB_DATA unparse_share_modes(struct share_mode_data *d)
160 DATA_BLOB blob;
161 enum ndr_err_code ndr_err;
163 if (DEBUGLEVEL >= 10) {
164 DEBUG(10, ("unparse_share_modes:\n"));
165 NDR_PRINT_DEBUG(share_mode_data, d);
168 if (d->num_share_modes == 0) {
169 DEBUG(10, ("No used share mode found\n"));
170 return make_tdb_data(NULL, 0);
173 ndr_err = ndr_push_struct_blob(
174 &blob, d, d, (ndr_push_flags_fn_t)ndr_push_share_mode_data);
175 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
176 smb_panic("ndr_push_share_mode_lock failed");
179 return make_tdb_data(blob.data, blob.length);
182 /*******************************************************************
183 If modified, store the share_mode_data back into the database.
184 ********************************************************************/
186 static int share_mode_data_destructor(struct share_mode_data *d)
188 NTSTATUS status;
189 TDB_DATA data;
191 if (!d->modified) {
192 return 0;
195 data = unparse_share_modes(d);
197 if (data.dptr == NULL) {
198 if (!d->fresh) {
199 /* There has been an entry before, delete it */
201 status = dbwrap_record_delete(d->record);
202 if (!NT_STATUS_IS_OK(status)) {
203 char *errmsg;
205 DEBUG(0, ("delete_rec returned %s\n",
206 nt_errstr(status)));
208 if (asprintf(&errmsg, "could not delete share "
209 "entry: %s\n",
210 nt_errstr(status)) == -1) {
211 smb_panic("could not delete share"
212 "entry");
214 smb_panic(errmsg);
217 goto done;
220 status = dbwrap_record_store(d->record, data, TDB_REPLACE);
221 if (!NT_STATUS_IS_OK(status)) {
222 char *errmsg;
224 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
226 if (asprintf(&errmsg, "could not store share mode entry: %s",
227 nt_errstr(status)) == -1) {
228 smb_panic("could not store share mode entry");
230 smb_panic(errmsg);
233 done:
235 return 0;
238 /*******************************************************************
239 Allocate a new share_mode_data struct, mark it unmodified.
240 fresh is set to note that currently there is no database entry.
241 ********************************************************************/
243 static struct share_mode_data *fresh_share_mode_lock(
244 TALLOC_CTX *mem_ctx, const char *servicepath,
245 const struct smb_filename *smb_fname,
246 const struct timespec *old_write_time)
248 struct share_mode_data *d;
250 if ((servicepath == NULL) || (smb_fname == NULL) ||
251 (old_write_time == NULL)) {
252 return NULL;
255 d = talloc_zero(mem_ctx, struct share_mode_data);
256 if (d == NULL) {
257 goto fail;
259 d->base_name = talloc_strdup(d, smb_fname->base_name);
260 if (d->base_name == NULL) {
261 goto fail;
263 if (smb_fname->stream_name != NULL) {
264 d->stream_name = talloc_strdup(d, smb_fname->stream_name);
265 if (d->stream_name == NULL) {
266 goto fail;
269 d->servicepath = talloc_strdup(d, servicepath);
270 if (d->servicepath == NULL) {
271 goto fail;
273 d->old_write_time = *old_write_time;
274 d->modified = false;
275 d->fresh = true;
276 return d;
277 fail:
278 DEBUG(0, ("talloc failed\n"));
279 TALLOC_FREE(d);
280 return NULL;
283 /*******************************************************************
284 Either fetch a share mode from the database, or allocate a fresh
285 one if the record doesn't exist.
286 ********************************************************************/
288 static struct share_mode_lock *get_share_mode_lock_internal(
289 TALLOC_CTX *mem_ctx, const struct file_id id,
290 const char *servicepath, const struct smb_filename *smb_fname,
291 const struct timespec *old_write_time)
293 struct share_mode_lock *lck;
294 struct share_mode_data *d;
295 struct file_id tmp;
296 struct db_record *rec;
297 TDB_DATA key = locking_key(&id, &tmp);
298 TDB_DATA value;
300 rec = dbwrap_fetch_locked(lock_db, mem_ctx, key);
301 if (rec == NULL) {
302 DEBUG(3, ("Could not lock share entry\n"));
303 return NULL;
306 value = dbwrap_record_get_value(rec);
308 if (value.dptr == NULL) {
309 d = fresh_share_mode_lock(mem_ctx, servicepath, smb_fname,
310 old_write_time);
311 } else {
312 d = parse_share_modes(mem_ctx, value);
315 if (d == NULL) {
316 DEBUG(5, ("get_share_mode_lock_internal: "
317 "Could not get share mode lock\n"));
318 TALLOC_FREE(rec);
319 return NULL;
321 d->id = id;
322 d->record = talloc_move(d, &rec);
323 talloc_set_destructor(d, share_mode_data_destructor);
325 lck = talloc(mem_ctx, struct share_mode_lock);
326 if (lck == NULL) {
327 DEBUG(1, ("talloc failed\n"));
328 TALLOC_FREE(d);
329 return NULL;
331 lck->data = talloc_move(lck, &d);
332 return lck;
336 * We can only ever have one share mode locked. Users of
337 * get_share_mode_lock never see this, it will be refcounted by
338 * talloc_reference.
340 static struct share_mode_lock *the_lock;
342 static int the_lock_destructor(struct share_mode_lock *l)
344 the_lock = NULL;
345 return 0;
348 /*******************************************************************
349 Get a share_mode_lock, Reference counted to allow nested calls.
350 ********************************************************************/
352 struct share_mode_lock *get_share_mode_lock(
353 TALLOC_CTX *mem_ctx,
354 const struct file_id id,
355 const char *servicepath,
356 const struct smb_filename *smb_fname,
357 const struct timespec *old_write_time)
359 TALLOC_CTX *frame = talloc_stackframe();
361 struct share_mode_lock *lck;
363 if (the_lock == NULL) {
364 the_lock = get_share_mode_lock_internal(
365 frame, id, servicepath, smb_fname, old_write_time);
366 if (the_lock == NULL) {
367 goto fail;
369 talloc_set_destructor(the_lock, the_lock_destructor);
371 if (!file_id_equal(&the_lock->data->id, &id)) {
372 DEBUG(1, ("Can not lock two share modes simultaneously\n"));
373 goto fail;
375 lck = talloc(mem_ctx, struct share_mode_lock);
376 if (lck == NULL) {
377 DEBUG(1, ("talloc failed\n"));
378 goto fail;
380 if (talloc_reference(lck, the_lock) == NULL) {
381 DEBUG(1, ("talloc_reference failed\n"));
382 goto fail;
384 lck->data = the_lock->data;
385 TALLOC_FREE(frame);
386 return lck;
387 fail:
388 TALLOC_FREE(frame);
389 return NULL;
392 static void fetch_share_mode_unlocked_parser(
393 TDB_DATA key, TDB_DATA data, void *private_data)
395 struct share_mode_lock *lck = talloc_get_type_abort(
396 private_data, struct share_mode_lock);
398 lck->data = parse_share_modes(lck, data);
401 /*******************************************************************
402 Get a share_mode_lock without locking the database or reference
403 counting. Used by smbstatus to display existing share modes.
404 ********************************************************************/
406 struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
407 const struct file_id id)
409 struct share_mode_lock *lck;
410 struct file_id tmp;
411 TDB_DATA key = locking_key(&id, &tmp);
412 NTSTATUS status;
414 lck = talloc(mem_ctx, struct share_mode_lock);
415 if (lck == NULL) {
416 DEBUG(0, ("talloc failed\n"));
417 return NULL;
419 status = dbwrap_parse_record(
420 lock_db, key, fetch_share_mode_unlocked_parser, lck);
421 if (!NT_STATUS_IS_OK(status) ||
422 (lck->data == NULL)) {
423 TALLOC_FREE(lck);
424 return NULL;
426 return lck;
429 struct forall_state {
430 void (*fn)(const struct share_mode_entry *entry,
431 const char *sharepath,
432 const char *fname,
433 void *private_data);
434 void *private_data;
437 static int traverse_fn(struct db_record *rec, void *_state)
439 struct forall_state *state = (struct forall_state *)_state;
440 uint32_t i;
441 TDB_DATA key;
442 TDB_DATA value;
443 DATA_BLOB blob;
444 enum ndr_err_code ndr_err;
445 struct share_mode_data *d;
447 key = dbwrap_record_get_key(rec);
448 value = dbwrap_record_get_value(rec);
450 /* Ensure this is a locking_key record. */
451 if (key.dsize != sizeof(struct file_id))
452 return 0;
454 d = talloc(talloc_tos(), struct share_mode_data);
455 if (d == NULL) {
456 return 0;
459 blob.data = value.dptr;
460 blob.length = value.dsize;
462 ndr_err = ndr_pull_struct_blob(
463 &blob, d, d, (ndr_pull_flags_fn_t)ndr_pull_share_mode_data);
464 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
465 DEBUG(1, ("ndr_pull_share_mode_lock failed\n"));
466 return 0;
468 for (i=0; i<d->num_share_modes; i++) {
469 state->fn(&d->share_modes[i],
470 d->servicepath, d->base_name,
471 state->private_data);
473 TALLOC_FREE(d);
475 return 0;
478 /*******************************************************************
479 Call the specified function on each entry under management by the
480 share mode system.
481 ********************************************************************/
483 int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
484 const char *, void *),
485 void *private_data)
487 struct forall_state state;
488 NTSTATUS status;
489 int count;
491 if (lock_db == NULL)
492 return 0;
494 state.fn = fn;
495 state.private_data = private_data;
497 status = dbwrap_traverse_read(lock_db, traverse_fn, (void *)&state,
498 &count);
500 if (!NT_STATUS_IS_OK(status)) {
501 return -1;
502 } else {
503 return count;