2 Samba share mode database library external interface library.
3 Used by non-Samba products needing access to the Samba share mode db.
7 THIS CODE IS NON-FUNCTIONAL IN SAMBA 4.2.0 AND ABOVE DUE TO THE CHANGES IN
8 SHARE MODE DATABASE SCHEMA FOR SMB2 LEASES.
10 CONTACT THE AUTHOR jra@samba.org IF YOU WISH TO RE-ENABLE
13 Copyright (C) Jeremy Allison 2005 - 2006
15 sharemodes_procid functions (C) Copyright (C) Volker Lendecke 2005
17 ** NOTE! The following LGPL license applies to this module only.
18 ** This does NOT imply that all of Samba is released
21 This library is free software; you can redistribute it and/or
22 modify it under the terms of the GNU Lesser General Public
23 License as published by the Free Software Foundation; either
24 version 3 of the License, or (at your option) any later version.
26 This library is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 Lesser General Public License for more details.
31 You should have received a copy of the GNU Lesser General Public
32 License along with this library; if not, see <http://www.gnu.org/licenses/>.
36 #include "system/filesys.h"
37 #include "smb_share_modes.h"
39 #include "librpc/gen_ndr/open_files.h"
41 /* Database context handle. */
46 /* Remove the paranoid malloc checker. */
52 * Internal structure of locking.tdb share mode db.
53 * Used by locking.c and libsmbsharemodes.c
59 int num_share_mode_entries
;
60 struct timespec old_write_time
;
61 struct timespec changed_write_time
;
62 uint32_t num_delete_token_entries
;
64 struct share_mode_entry dummy
; /* Needed for alignment. */
66 /* The following four entries are implicit
68 (1) struct share_mode_entry modes[num_share_mode_entries];
70 (2) A num_delete_token_entries of structs {
71 uint32_t len_delete_token;
72 char unix_token[len_delete_token] (divisible by 4).
75 (3) char share_name[];
80 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
, uint64_t dev
,
81 uint64_t ino
, uint64_t extid
,
82 const struct smb_share_mode_entry
*new_entry
,
83 const char *sharepath
, const char *filename
);
85 static bool sharemodes_procid_equal(const struct server_id
*p1
, const struct server_id
*p2
)
87 return (p1
->pid
== p2
->pid
);
90 static pid_t
sharemodes_procid_to_pid(const struct server_id
*proc
)
96 * open/close sharemode database.
99 struct smbdb_ctx
*smb_share_mode_db_open(const char *db_path
)
101 struct smbdb_ctx
*smb_db
= (struct smbdb_ctx
*)malloc(sizeof(struct smbdb_ctx
));
107 memset(smb_db
, '\0', sizeof(struct smbdb_ctx
));
109 /* FIXME: We should *never* open a tdb without logging! */
110 smb_db
->smb_tdb
= tdb_open(db_path
,
111 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
115 if (!smb_db
->smb_tdb
) {
120 /* Should check that this is the correct version.... */
124 /* key and data records in the tdb locking database */
131 int smb_share_mode_db_close(struct smbdb_ctx
*db_ctx
)
133 int ret
= tdb_close(db_ctx
->smb_tdb
);
138 static TDB_DATA
get_locking_key(struct locking_key
*lk
, uint64_t dev
,
139 uint64_t ino
, uint64_t extid
)
143 memset(lk
, '\0', sizeof(*lk
));
144 lk
->dev
= (SMB_DEV_T
)dev
;
145 lk
->inode
= (SMB_INO_T
)ino
;
147 ld
.dptr
= (uint8_t *)lk
;
148 ld
.dsize
= sizeof(*lk
);
153 * lock/unlock entry in sharemode database.
156 int smb_lock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
161 struct locking_key lk
;
162 return tdb_chainlock(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
,
163 extid
)) == 0 ? 0 : -1;
166 int smb_unlock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
171 struct locking_key lk
;
172 tdb_chainunlock(db_ctx
->smb_tdb
,
173 get_locking_key(&lk
, dev
, ino
, extid
));
178 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
181 static int share_mode_entry_equal(const struct smb_share_mode_entry
*e_entry
,
182 const struct share_mode_entry
*entry
)
184 return (sharemodes_procid_equal(&e_entry
->pid
, &entry
->pid
) &&
185 e_entry
->file_id
== (uint32_t)entry
->share_file_id
&&
186 e_entry
->open_time
.tv_sec
== entry
->time
.tv_sec
&&
187 e_entry
->open_time
.tv_usec
== entry
->time
.tv_usec
&&
188 e_entry
->share_access
== (uint32_t)entry
->share_access
&&
189 e_entry
->access_mask
== (uint32_t)entry
->access_mask
&&
190 e_entry
->dev
== entry
->id
.devid
&&
191 e_entry
->ino
== entry
->id
.inode
&&
192 e_entry
->extid
== entry
->id
.extid
);
196 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
199 static void create_share_mode_entry(struct share_mode_entry
*out
,
200 const struct smb_share_mode_entry
*in
,
203 memset(out
, '\0', sizeof(struct share_mode_entry
));
206 out
->share_file_id
= (unsigned long)in
->file_id
;
207 out
->time
.tv_sec
= in
->open_time
.tv_sec
;
208 out
->time
.tv_usec
= in
->open_time
.tv_usec
;
209 out
->share_access
= in
->share_access
;
210 out
->access_mask
= in
->access_mask
;
211 out
->id
.devid
= in
->dev
;
212 out
->id
.inode
= in
->ino
;
213 out
->id
.extid
= in
->extid
;
214 out
->uid
= (uint32_t)geteuid();
216 out
->name_hash
= name_hash
;
220 * Return the current share mode list for an open file.
221 * This uses similar (but simplified) logic to locking/locking.c
224 int smb_get_share_mode_entries(struct smbdb_ctx
*db_ctx
,
228 struct smb_share_mode_entry
**pp_list
,
229 unsigned char *p_delete_on_close
)
231 struct locking_key lk
;
233 struct smb_share_mode_entry
*list
= NULL
;
234 int num_share_modes
= 0;
235 struct locking_data
*ld
= NULL
; /* internal samba db state. */
236 struct share_mode_entry
*shares
= NULL
;
241 *p_delete_on_close
= 0;
243 db_data
= tdb_fetch(db_ctx
->smb_tdb
,
244 get_locking_key(&lk
, dev
, ino
, extid
));
249 ld
= (struct locking_data
*)db_data
.dptr
;
250 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
252 if (!num_share_modes
) {
257 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
263 memset(list
, '\0', num_share_modes
* sizeof(struct smb_share_mode_entry
));
265 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
268 for (i
= 0; i
< num_share_modes
; i
++) {
269 struct share_mode_entry
*share
= &shares
[i
];
270 struct smb_share_mode_entry
*sme
= &list
[list_num
];
271 struct server_id pid
= share
->pid
;
273 /* Check this process really exists. */
274 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
275 continue; /* No longer exists. */
278 /* Copy into the external list. */
279 sme
->dev
= share
->id
.devid
;
280 sme
->ino
= share
->id
.inode
;
281 sme
->extid
= share
->id
.extid
;
282 sme
->share_access
= (uint32_t)share
->share_access
;
283 sme
->access_mask
= (uint32_t)share
->access_mask
;
284 sme
->open_time
.tv_sec
= share
->time
.tv_sec
;
285 sme
->open_time
.tv_usec
= share
->time
.tv_usec
;
286 sme
->file_id
= (uint32_t)share
->share_file_id
;
287 sme
->pid
= share
->pid
;
297 *p_delete_on_close
= ld
->u
.s
.num_delete_token_entries
!= 0;
303 static uint32_t smb_name_hash(const char *sharepath
, const char *filename
, int *err
)
305 char *fullpath
= NULL
;
306 size_t sharepath_size
= strlen(sharepath
);
307 size_t filename_size
= strlen(filename
);
312 fullpath
= (char *)malloc(sharepath_size
+ filename_size
+ 2);
313 if (fullpath
== NULL
) {
317 memcpy(fullpath
, sharepath
, sharepath_size
);
318 fullpath
[sharepath_size
] = '/';
319 memcpy(&fullpath
[sharepath_size
+ 1], filename
, filename_size
+ 1);
321 key
= (TDB_DATA
) { .dptr
= (uint8_t *)fullpath
,
322 .dsize
= strlen(fullpath
) + 1 };
323 name_hash
= tdb_jenkins_hash(&key
);
329 * Create an entry in the Samba share mode db.
332 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
336 const struct smb_share_mode_entry
*new_entry
,
337 const char *sharepath
, /* Must be absolute utf8 path. */
338 const char *filename
) /* Must be relative utf8 path. */
341 struct locking_key lk
;
342 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
343 int orig_num_share_modes
= 0;
344 struct locking_data
*ld
= NULL
; /* internal samba db state. */
345 struct share_mode_entry
*shares
= NULL
;
346 uint8_t *new_data_p
= NULL
;
347 size_t new_data_size
= 0;
349 uint32_t name_hash
= smb_name_hash(sharepath
, filename
, &err
);
355 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
357 /* We must create the entry. */
358 db_data
.dptr
= (uint8_t *)malloc(
359 sizeof(struct locking_data
) +
360 sizeof(struct share_mode_entry
) +
361 strlen(sharepath
) + 1 +
362 strlen(filename
) + 1);
366 ld
= (struct locking_data
*)db_data
.dptr
;
367 memset(ld
, '\0', sizeof(struct locking_data
));
368 ld
->u
.s
.num_share_mode_entries
= 1;
369 ld
->u
.s
.num_delete_token_entries
= 0;
370 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
371 create_share_mode_entry(shares
, new_entry
, name_hash
);
373 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
),
375 strlen(sharepath
) + 1);
376 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
377 strlen(sharepath
) + 1,
379 strlen(filename
) + 1);
381 db_data
.dsize
= sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
382 strlen(sharepath
) + 1 +
383 strlen(filename
) + 1;
384 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_INSERT
) != 0) {
392 /* Entry exists, we must add a new entry. */
393 new_data_p
= (uint8_t *)malloc(
394 db_data
.dsize
+ sizeof(struct share_mode_entry
));
400 ld
= (struct locking_data
*)db_data
.dptr
;
401 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
403 /* Copy the original data. */
404 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
406 /* Add in the new share mode */
407 shares
= (struct share_mode_entry
*)(new_data_p
+ sizeof(struct locking_data
) +
408 (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
410 create_share_mode_entry(shares
, new_entry
, name_hash
);
412 ld
= (struct locking_data
*)new_data_p
;
413 ld
->u
.s
.num_share_mode_entries
++;
415 /* Append the original delete_tokens and filenames. */
416 memcpy(new_data_p
+ sizeof(struct locking_data
) + (ld
->u
.s
.num_share_mode_entries
* sizeof(struct share_mode_entry
)),
417 db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)),
418 db_data
.dsize
- sizeof(struct locking_data
) - (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
420 new_data_size
= db_data
.dsize
+ sizeof(struct share_mode_entry
);
424 db_data
.dptr
= new_data_p
;
425 db_data
.dsize
= new_data_size
;
427 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {
436 * Create an entry in the Samba share mode db. Original interface - doesn't
437 * Distinguish between share path and filename. Fudge this by using a
438 * sharepath of / and a relative filename of (filename+1).
441 int smb_create_share_mode_entry(struct smbdb_ctx
*db_ctx
,
445 const struct smb_share_mode_entry
*new_entry
,
446 const char *filename
) /* Must be absolute utf8 path. */
448 if (*filename
!= '/') {
451 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, extid
, new_entry
,
455 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
459 const struct smb_share_mode_entry
*del_entry
)
462 struct locking_key lk
;
463 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
464 int orig_num_share_modes
= 0;
465 struct locking_data
*ld
= NULL
; /* internal samba db state. */
466 struct share_mode_entry
*shares
= NULL
;
467 uint8_t *new_data_p
= NULL
;
468 size_t remaining_size
= 0;
469 size_t i
, num_share_modes
;
470 const uint8_t *remaining_ptr
= NULL
;
472 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
474 return -1; /* Error - missing entry ! */
477 ld
= (struct locking_data
*)db_data
.dptr
;
478 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
479 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
481 if (orig_num_share_modes
== 1) {
482 /* Only one entry - better be ours... */
483 if (!share_mode_entry_equal(del_entry
, shares
)) {
484 /* Error ! We can't delete someone else's entry ! */
488 /* It's ours - just remove the entire record. */
490 return tdb_delete(db_ctx
->smb_tdb
, locking_key
) ? -1 : 0;
493 /* More than one - allocate a new record minus the one we'll delete. */
494 new_data_p
= (uint8_t *)malloc(
495 db_data
.dsize
- sizeof(struct share_mode_entry
));
501 /* Copy the header. */
502 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
505 for (i
= 0; i
< orig_num_share_modes
; i
++) {
506 struct share_mode_entry
*share
= &shares
[i
];
507 struct server_id pid
= share
->pid
;
509 /* Check this process really exists. */
510 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
511 continue; /* No longer exists. */
514 if (share_mode_entry_equal(del_entry
, share
)) {
515 continue; /* This is our delete taget. */
518 memcpy(new_data_p
+ sizeof(struct locking_data
) +
519 (num_share_modes
* sizeof(struct share_mode_entry
)),
520 share
, sizeof(struct share_mode_entry
) );
525 if (num_share_modes
== 0) {
526 /* None left after pruning. Delete record. */
529 return tdb_delete(db_ctx
->smb_tdb
, locking_key
) ? -1 : 0;
532 /* Copy any delete tokens plus the terminating filenames. */
533 remaining_ptr
= db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
));
534 remaining_size
= db_data
.dsize
- (remaining_ptr
- db_data
.dptr
);
536 memcpy(new_data_p
+ sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)),
542 db_data
.dptr
= new_data_p
;
544 /* Re-save smaller record. */
545 ld
= (struct locking_data
*)db_data
.dptr
;
546 ld
->u
.s
.num_share_mode_entries
= num_share_modes
;
548 db_data
.dsize
= sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)) + remaining_size
;
550 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {
558 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
562 const struct smb_share_mode_entry
*set_entry
,
563 const struct smb_share_mode_entry
*new_entry
)
566 struct locking_key lk
;
567 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
568 int num_share_modes
= 0;
569 struct locking_data
*ld
= NULL
; /* internal samba db state. */
570 struct share_mode_entry
*shares
= NULL
;
574 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
576 return -1; /* Error - missing entry ! */
579 ld
= (struct locking_data
*)db_data
.dptr
;
580 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
581 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
583 for (i
= 0; i
< num_share_modes
; i
++) {
584 struct share_mode_entry
*share
= &shares
[i
];
585 struct server_id pid
= share
->pid
;
587 /* Check this process really exists. */
588 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
589 continue; /* No longer exists. */
592 if (share_mode_entry_equal(set_entry
, share
)) {
593 create_share_mode_entry(share
, new_entry
, share
->name_hash
);
604 /* Save modified data. */
605 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {