2 Samba share mode database library external interface library.
3 Used by non-Samba products needing access to the Samba share mode db.
5 Copyright (C) Jeremy Allison 2005 - 2006
7 sharemodes_procid functions (C) Copyright (C) Volker Lendecke 2005
9 ** NOTE! The following LGPL license applies to this module only.
10 ** This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "system/filesys.h"
29 #include "smb_share_modes.h"
30 #include "tdb_compat.h"
31 #include <ccan/hash/hash.h>
33 /* Database context handle. */
38 /* Remove the paranoid malloc checker. */
43 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
44 uint64_t ino, uint64_t extid,
45 const struct smb_share_mode_entry *new_entry,
46 const char *sharepath, const char *filename);
48 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
50 return (p1->pid == p2->pid);
53 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
59 * open/close sharemode database.
62 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
64 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
70 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
72 /* FIXME: We should *never* open a tdb without logging! */
73 smb_db->smb_tdb = tdb_open_compat(db_path,
74 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
79 if (!smb_db->smb_tdb) {
84 /* Should check that this is the correct version.... */
88 /* key and data records in the tdb locking database */
95 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
97 int ret = tdb_close(db_ctx->smb_tdb);
102 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
103 uint64_t ino, uint64_t extid)
107 memset(lk, '\0', sizeof(*lk));
108 lk->dev = (SMB_DEV_T)dev;
109 lk->inode = (SMB_INO_T)ino;
111 ld.dptr = (uint8 *)lk;
112 ld.dsize = sizeof(*lk);
117 * lock/unlock entry in sharemode database.
120 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
125 struct locking_key lk;
126 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
127 extid)) == 0 ? 0 : -1;
130 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
135 struct locking_key lk;
136 tdb_chainunlock(db_ctx->smb_tdb,
137 get_locking_key(&lk, dev, ino, extid));
142 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
145 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
146 const struct share_mode_entry *entry)
148 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
149 e_entry->file_id == (uint32_t)entry->share_file_id &&
150 e_entry->open_time.tv_sec == entry->time.tv_sec &&
151 e_entry->open_time.tv_usec == entry->time.tv_usec &&
152 e_entry->share_access == (uint32_t)entry->share_access &&
153 e_entry->access_mask == (uint32_t)entry->access_mask &&
154 e_entry->dev == entry->id.devid &&
155 e_entry->ino == entry->id.inode &&
156 e_entry->extid == entry->id.extid);
160 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
163 static void create_share_mode_entry(struct share_mode_entry *out,
164 const struct smb_share_mode_entry *in,
167 memset(out, '\0', sizeof(struct share_mode_entry));
170 out->share_file_id = (unsigned long)in->file_id;
171 out->time.tv_sec = in->open_time.tv_sec;
172 out->time.tv_usec = in->open_time.tv_usec;
173 out->share_access = in->share_access;
174 out->access_mask = in->access_mask;
175 out->id.devid = in->dev;
176 out->id.inode = in->ino;
177 out->id.extid = in->extid;
178 out->uid = (uint32)geteuid();
180 out->name_hash = name_hash;
184 * Return the current share mode list for an open file.
185 * This uses similar (but simplified) logic to locking/locking.c
188 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
192 struct smb_share_mode_entry **pp_list,
193 unsigned char *p_delete_on_close)
195 struct locking_key lk;
197 struct smb_share_mode_entry *list = NULL;
198 int num_share_modes = 0;
199 struct locking_data *ld = NULL; /* internal samba db state. */
200 struct share_mode_entry *shares = NULL;
205 *p_delete_on_close = 0;
207 db_data = tdb_fetch_compat(db_ctx->smb_tdb,
208 get_locking_key(&lk, dev, ino, extid));
213 ld = (struct locking_data *)db_data.dptr;
214 num_share_modes = ld->u.s.num_share_mode_entries;
216 if (!num_share_modes) {
221 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
227 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
229 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
232 for (i = 0; i < num_share_modes; i++) {
233 struct share_mode_entry *share = &shares[i];
234 struct smb_share_mode_entry *sme = &list[list_num];
235 struct server_id pid = share->pid;
237 /* Check this process really exists. */
238 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
239 continue; /* No longer exists. */
242 /* Ignore deferred open entries. */
243 if (share->op_type == DEFERRED_OPEN_ENTRY) {
247 /* Copy into the external list. */
248 sme->dev = share->id.devid;
249 sme->ino = share->id.inode;
250 sme->extid = share->id.extid;
251 sme->share_access = (uint32_t)share->share_access;
252 sme->access_mask = (uint32_t)share->access_mask;
253 sme->open_time.tv_sec = share->time.tv_sec;
254 sme->open_time.tv_usec = share->time.tv_usec;
255 sme->file_id = (uint32_t)share->share_file_id;
256 sme->pid = share->pid;
266 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
272 static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
274 char *fullpath = NULL;
275 size_t sharepath_size = strlen(sharepath);
276 size_t filename_size = strlen(filename);
280 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
281 if (fullpath == NULL) {
285 memcpy(fullpath, sharepath, sharepath_size);
286 fullpath[sharepath_size] = '/';
287 memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
289 name_hash = hash(fullpath, strlen(fullpath) + 1, 0);
295 * Create an entry in the Samba share mode db.
298 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
302 const struct smb_share_mode_entry *new_entry,
303 const char *sharepath, /* Must be absolute utf8 path. */
304 const char *filename) /* Must be relative utf8 path. */
307 struct locking_key lk;
308 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
309 int orig_num_share_modes = 0;
310 struct locking_data *ld = NULL; /* internal samba db state. */
311 struct share_mode_entry *shares = NULL;
312 uint8 *new_data_p = NULL;
313 size_t new_data_size = 0;
315 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
321 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
323 /* We must create the entry. */
324 db_data.dptr = (uint8 *)malloc(
325 sizeof(struct locking_data) +
326 sizeof(struct share_mode_entry) +
327 strlen(sharepath) + 1 +
328 strlen(filename) + 1);
332 ld = (struct locking_data *)db_data.dptr;
333 memset(ld, '\0', sizeof(struct locking_data));
334 ld->u.s.num_share_mode_entries = 1;
335 ld->u.s.num_delete_token_entries = 0;
336 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
337 create_share_mode_entry(shares, new_entry, name_hash);
339 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
341 strlen(sharepath) + 1);
342 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
343 strlen(sharepath) + 1,
345 strlen(filename) + 1);
347 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
348 strlen(sharepath) + 1 +
349 strlen(filename) + 1;
350 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) != 0) {
358 /* Entry exists, we must add a new entry. */
359 new_data_p = (uint8 *)malloc(
360 db_data.dsize + sizeof(struct share_mode_entry));
366 ld = (struct locking_data *)db_data.dptr;
367 orig_num_share_modes = ld->u.s.num_share_mode_entries;
369 /* Copy the original data. */
370 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
372 /* Add in the new share mode */
373 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
374 (orig_num_share_modes * sizeof(struct share_mode_entry)));
376 create_share_mode_entry(shares, new_entry, name_hash);
378 ld = (struct locking_data *)new_data_p;
379 ld->u.s.num_share_mode_entries++;
381 /* Append the original delete_tokens and filenames. */
382 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
383 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
384 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
386 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
390 db_data.dptr = new_data_p;
391 db_data.dsize = new_data_size;
393 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
402 * Create an entry in the Samba share mode db. Original interface - doesn't
403 * Distinguish between share path and filename. Fudge this by using a
404 * sharepath of / and a relative filename of (filename+1).
407 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
411 const struct smb_share_mode_entry *new_entry,
412 const char *filename) /* Must be absolute utf8 path. */
414 if (*filename != '/') {
417 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
421 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
425 const struct smb_share_mode_entry *del_entry)
428 struct locking_key lk;
429 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
430 int orig_num_share_modes = 0;
431 struct locking_data *ld = NULL; /* internal samba db state. */
432 struct share_mode_entry *shares = NULL;
433 uint8 *new_data_p = NULL;
434 size_t remaining_size = 0;
435 size_t i, num_share_modes;
436 const uint8 *remaining_ptr = NULL;
438 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
440 return -1; /* Error - missing entry ! */
443 ld = (struct locking_data *)db_data.dptr;
444 orig_num_share_modes = ld->u.s.num_share_mode_entries;
445 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
447 if (orig_num_share_modes == 1) {
448 /* Only one entry - better be ours... */
449 if (!share_mode_entry_equal(del_entry, shares)) {
450 /* Error ! We can't delete someone else's entry ! */
454 /* It's ours - just remove the entire record. */
456 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
459 /* More than one - allocate a new record minus the one we'll delete. */
460 new_data_p = (uint8 *)malloc(
461 db_data.dsize - sizeof(struct share_mode_entry));
467 /* Copy the header. */
468 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
471 for (i = 0; i < orig_num_share_modes; i++) {
472 struct share_mode_entry *share = &shares[i];
473 struct server_id pid = share->pid;
475 /* Check this process really exists. */
476 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
477 continue; /* No longer exists. */
480 if (share_mode_entry_equal(del_entry, share)) {
481 continue; /* This is our delete taget. */
484 memcpy(new_data_p + sizeof(struct locking_data) +
485 (num_share_modes * sizeof(struct share_mode_entry)),
486 share, sizeof(struct share_mode_entry) );
491 if (num_share_modes == 0) {
492 /* None left after pruning. Delete record. */
495 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
498 /* Copy any delete tokens plus the terminating filenames. */
499 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
500 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
502 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
508 db_data.dptr = new_data_p;
510 /* Re-save smaller record. */
511 ld = (struct locking_data *)db_data.dptr;
512 ld->u.s.num_share_mode_entries = num_share_modes;
514 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
516 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
524 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
528 const struct smb_share_mode_entry *set_entry,
529 const struct smb_share_mode_entry *new_entry)
532 struct locking_key lk;
533 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
534 int num_share_modes = 0;
535 struct locking_data *ld = NULL; /* internal samba db state. */
536 struct share_mode_entry *shares = NULL;
540 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
542 return -1; /* Error - missing entry ! */
545 ld = (struct locking_data *)db_data.dptr;
546 num_share_modes = ld->u.s.num_share_mode_entries;
547 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
549 for (i = 0; i < num_share_modes; i++) {
550 struct share_mode_entry *share = &shares[i];
551 struct server_id pid = share->pid;
553 /* Check this process really exists. */
554 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
555 continue; /* No longer exists. */
558 if (share_mode_entry_equal(set_entry, share)) {
559 create_share_mode_entry(share, new_entry, share->name_hash);
570 /* Save modified data. */
571 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {