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/>.
27 #define UID_WRAPPER_NOT_REPLACE
29 #include "system/filesys.h"
30 #include "smb_share_modes.h"
31 #include "tdb_compat.h"
32 #include "librpc/gen_ndr/open_files.h"
33 #include <ccan/hash/hash.h>
35 /* Database context handle. */
40 /* Remove the paranoid malloc checker. */
46 * Internal structure of locking.tdb share mode db.
47 * Used by locking.c and libsmbsharemodes.c
53 int num_share_mode_entries;
54 struct timespec old_write_time;
55 struct timespec changed_write_time;
56 uint32 num_delete_token_entries;
58 struct share_mode_entry dummy; /* Needed for alignment. */
60 /* The following four entries are implicit
62 (1) struct share_mode_entry modes[num_share_mode_entries];
64 (2) A num_delete_token_entries of structs {
65 uint32_t len_delete_token;
66 char unix_token[len_delete_token] (divisible by 4).
69 (3) char share_name[];
74 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
75 uint64_t ino, uint64_t extid,
76 const struct smb_share_mode_entry *new_entry,
77 const char *sharepath, const char *filename);
79 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
81 return (p1->pid == p2->pid);
84 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
90 * open/close sharemode database.
93 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
95 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
101 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
103 /* FIXME: We should *never* open a tdb without logging! */
104 smb_db->smb_tdb = tdb_open_compat(db_path,
105 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
110 if (!smb_db->smb_tdb) {
115 /* Should check that this is the correct version.... */
119 /* key and data records in the tdb locking database */
126 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
128 int ret = tdb_close(db_ctx->smb_tdb);
133 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
134 uint64_t ino, uint64_t extid)
138 memset(lk, '\0', sizeof(*lk));
139 lk->dev = (SMB_DEV_T)dev;
140 lk->inode = (SMB_INO_T)ino;
142 ld.dptr = (uint8 *)lk;
143 ld.dsize = sizeof(*lk);
148 * lock/unlock entry in sharemode database.
151 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
156 struct locking_key lk;
157 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
158 extid)) == 0 ? 0 : -1;
161 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
166 struct locking_key lk;
167 tdb_chainunlock(db_ctx->smb_tdb,
168 get_locking_key(&lk, dev, ino, extid));
173 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
176 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
177 const struct share_mode_entry *entry)
179 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
180 e_entry->file_id == (uint32_t)entry->share_file_id &&
181 e_entry->open_time.tv_sec == entry->time.tv_sec &&
182 e_entry->open_time.tv_usec == entry->time.tv_usec &&
183 e_entry->share_access == (uint32_t)entry->share_access &&
184 e_entry->access_mask == (uint32_t)entry->access_mask &&
185 e_entry->dev == entry->id.devid &&
186 e_entry->ino == entry->id.inode &&
187 e_entry->extid == entry->id.extid);
191 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
194 static void create_share_mode_entry(struct share_mode_entry *out,
195 const struct smb_share_mode_entry *in,
198 memset(out, '\0', sizeof(struct share_mode_entry));
201 out->share_file_id = (unsigned long)in->file_id;
202 out->time.tv_sec = in->open_time.tv_sec;
203 out->time.tv_usec = in->open_time.tv_usec;
204 out->share_access = in->share_access;
205 out->access_mask = in->access_mask;
206 out->id.devid = in->dev;
207 out->id.inode = in->ino;
208 out->id.extid = in->extid;
209 out->uid = (uint32)geteuid();
211 out->name_hash = name_hash;
215 * Return the current share mode list for an open file.
216 * This uses similar (but simplified) logic to locking/locking.c
219 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
223 struct smb_share_mode_entry **pp_list,
224 unsigned char *p_delete_on_close)
226 struct locking_key lk;
228 struct smb_share_mode_entry *list = NULL;
229 int num_share_modes = 0;
230 struct locking_data *ld = NULL; /* internal samba db state. */
231 struct share_mode_entry *shares = NULL;
236 *p_delete_on_close = 0;
238 db_data = tdb_fetch_compat(db_ctx->smb_tdb,
239 get_locking_key(&lk, dev, ino, extid));
244 ld = (struct locking_data *)db_data.dptr;
245 num_share_modes = ld->u.s.num_share_mode_entries;
247 if (!num_share_modes) {
252 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
258 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
260 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
263 for (i = 0; i < num_share_modes; i++) {
264 struct share_mode_entry *share = &shares[i];
265 struct smb_share_mode_entry *sme = &list[list_num];
266 struct server_id pid = share->pid;
268 /* Check this process really exists. */
269 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
270 continue; /* No longer exists. */
273 /* Ignore deferred open entries. */
274 if (share->op_type == DEFERRED_OPEN_ENTRY) {
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);
311 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
312 if (fullpath == NULL) {
316 memcpy(fullpath, sharepath, sharepath_size);
317 fullpath[sharepath_size] = '/';
318 memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
320 name_hash = hash(fullpath, strlen(fullpath) + 1, 0);
326 * Create an entry in the Samba share mode db.
329 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
333 const struct smb_share_mode_entry *new_entry,
334 const char *sharepath, /* Must be absolute utf8 path. */
335 const char *filename) /* Must be relative utf8 path. */
338 struct locking_key lk;
339 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
340 int orig_num_share_modes = 0;
341 struct locking_data *ld = NULL; /* internal samba db state. */
342 struct share_mode_entry *shares = NULL;
343 uint8 *new_data_p = NULL;
344 size_t new_data_size = 0;
346 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
352 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
354 /* We must create the entry. */
355 db_data.dptr = (uint8 *)malloc(
356 sizeof(struct locking_data) +
357 sizeof(struct share_mode_entry) +
358 strlen(sharepath) + 1 +
359 strlen(filename) + 1);
363 ld = (struct locking_data *)db_data.dptr;
364 memset(ld, '\0', sizeof(struct locking_data));
365 ld->u.s.num_share_mode_entries = 1;
366 ld->u.s.num_delete_token_entries = 0;
367 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
368 create_share_mode_entry(shares, new_entry, name_hash);
370 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
372 strlen(sharepath) + 1);
373 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
374 strlen(sharepath) + 1,
376 strlen(filename) + 1);
378 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
379 strlen(sharepath) + 1 +
380 strlen(filename) + 1;
381 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) != 0) {
389 /* Entry exists, we must add a new entry. */
390 new_data_p = (uint8 *)malloc(
391 db_data.dsize + sizeof(struct share_mode_entry));
397 ld = (struct locking_data *)db_data.dptr;
398 orig_num_share_modes = ld->u.s.num_share_mode_entries;
400 /* Copy the original data. */
401 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
403 /* Add in the new share mode */
404 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
405 (orig_num_share_modes * sizeof(struct share_mode_entry)));
407 create_share_mode_entry(shares, new_entry, name_hash);
409 ld = (struct locking_data *)new_data_p;
410 ld->u.s.num_share_mode_entries++;
412 /* Append the original delete_tokens and filenames. */
413 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
414 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
415 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
417 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
421 db_data.dptr = new_data_p;
422 db_data.dsize = new_data_size;
424 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
433 * Create an entry in the Samba share mode db. Original interface - doesn't
434 * Distinguish between share path and filename. Fudge this by using a
435 * sharepath of / and a relative filename of (filename+1).
438 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
442 const struct smb_share_mode_entry *new_entry,
443 const char *filename) /* Must be absolute utf8 path. */
445 if (*filename != '/') {
448 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
452 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
456 const struct smb_share_mode_entry *del_entry)
459 struct locking_key lk;
460 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
461 int orig_num_share_modes = 0;
462 struct locking_data *ld = NULL; /* internal samba db state. */
463 struct share_mode_entry *shares = NULL;
464 uint8 *new_data_p = NULL;
465 size_t remaining_size = 0;
466 size_t i, num_share_modes;
467 const uint8 *remaining_ptr = NULL;
469 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
471 return -1; /* Error - missing entry ! */
474 ld = (struct locking_data *)db_data.dptr;
475 orig_num_share_modes = ld->u.s.num_share_mode_entries;
476 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
478 if (orig_num_share_modes == 1) {
479 /* Only one entry - better be ours... */
480 if (!share_mode_entry_equal(del_entry, shares)) {
481 /* Error ! We can't delete someone else's entry ! */
485 /* It's ours - just remove the entire record. */
487 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
490 /* More than one - allocate a new record minus the one we'll delete. */
491 new_data_p = (uint8 *)malloc(
492 db_data.dsize - sizeof(struct share_mode_entry));
498 /* Copy the header. */
499 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
502 for (i = 0; i < orig_num_share_modes; i++) {
503 struct share_mode_entry *share = &shares[i];
504 struct server_id pid = share->pid;
506 /* Check this process really exists. */
507 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
508 continue; /* No longer exists. */
511 if (share_mode_entry_equal(del_entry, share)) {
512 continue; /* This is our delete taget. */
515 memcpy(new_data_p + sizeof(struct locking_data) +
516 (num_share_modes * sizeof(struct share_mode_entry)),
517 share, sizeof(struct share_mode_entry) );
522 if (num_share_modes == 0) {
523 /* None left after pruning. Delete record. */
526 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
529 /* Copy any delete tokens plus the terminating filenames. */
530 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
531 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
533 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
539 db_data.dptr = new_data_p;
541 /* Re-save smaller record. */
542 ld = (struct locking_data *)db_data.dptr;
543 ld->u.s.num_share_mode_entries = num_share_modes;
545 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
547 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
555 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
559 const struct smb_share_mode_entry *set_entry,
560 const struct smb_share_mode_entry *new_entry)
563 struct locking_key lk;
564 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
565 int num_share_modes = 0;
566 struct locking_data *ld = NULL; /* internal samba db state. */
567 struct share_mode_entry *shares = NULL;
571 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
573 return -1; /* Error - missing entry ! */
576 ld = (struct locking_data *)db_data.dptr;
577 num_share_modes = ld->u.s.num_share_mode_entries;
578 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
580 for (i = 0; i < num_share_modes; i++) {
581 struct share_mode_entry *share = &shares[i];
582 struct server_id pid = share->pid;
584 /* Check this process really exists. */
585 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
586 continue; /* No longer exists. */
589 if (share_mode_entry_equal(set_entry, share)) {
590 create_share_mode_entry(share, new_entry, share->name_hash);
601 /* Save modified data. */
602 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {