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 2 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, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "smb_share_modes.h"
31 /* Remove the paranoid malloc checker. */
36 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
37 uint64_t ino, const struct smb_share_mode_entry *new_entry,
38 const char *sharepath, const char *filename);
40 static BOOL sharemodes_procid_equal(const struct process_id *p1, const struct process_id *p2)
42 return (p1->pid == p2->pid);
45 static pid_t sharemodes_procid_to_pid(const struct process_id *proc)
51 * open/close sharemode database.
54 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
56 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
62 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
64 smb_db->smb_tdb = tdb_open(db_path,
65 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
69 if (!smb_db->smb_tdb) {
74 /* Should check that this is the correct version.... */
78 /* key and data records in the tdb locking database */
84 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
86 int ret = tdb_close(db_ctx->smb_tdb);
91 static TDB_DATA get_locking_key(uint64_t dev, uint64_t ino)
93 static struct locking_key lk;
96 memset(&lk, '\0', sizeof(struct locking_key));
97 lk.dev = (SMB_DEV_T)dev;
98 lk.inode = (SMB_INO_T)ino;
99 ld.dptr = (char *)&lk;
100 ld.dsize = sizeof(lk);
105 * lock/unlock entry in sharemode database.
108 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
112 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
115 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
119 return tdb_chainunlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
123 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
126 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
127 const struct share_mode_entry *entry)
129 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
130 e_entry->file_id == (uint32_t)entry->share_file_id &&
131 e_entry->open_time.tv_sec == entry->time.tv_sec &&
132 e_entry->open_time.tv_usec == entry->time.tv_usec &&
133 e_entry->share_access == (uint32_t)entry->share_access &&
134 e_entry->access_mask == (uint32_t)entry->access_mask &&
135 e_entry->dev == (uint64_t)entry->dev &&
136 e_entry->ino == (uint64_t)entry->inode);
140 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
143 static void create_share_mode_entry(struct share_mode_entry *out,
144 const struct smb_share_mode_entry *in)
146 memset(out, '\0', sizeof(struct share_mode_entry));
149 out->share_file_id = (unsigned long)in->file_id;
150 out->time.tv_sec = in->open_time.tv_sec;
151 out->time.tv_usec = in->open_time.tv_usec;
152 out->share_access = in->share_access;
153 out->access_mask = in->access_mask;
154 out->dev = (SMB_DEV_T)in->dev;
155 out->inode = (SMB_INO_T)in->ino;
156 out->uid = (uint32)geteuid();
161 * Return the current share mode list for an open file.
162 * This uses similar (but simplified) logic to locking/locking.c
165 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
168 struct smb_share_mode_entry **pp_list,
169 unsigned char *p_delete_on_close)
172 struct smb_share_mode_entry *list = NULL;
173 int num_share_modes = 0;
174 struct locking_data *ld = NULL; /* internal samba db state. */
175 struct share_mode_entry *shares = NULL;
180 *p_delete_on_close = 0;
182 db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(dev, ino));
187 ld = (struct locking_data *)db_data.dptr;
188 num_share_modes = ld->u.s.num_share_mode_entries;
190 if (!num_share_modes) {
195 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
201 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
203 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
206 for (i = 0; i < num_share_modes; i++) {
207 struct share_mode_entry *share = &shares[i];
208 struct smb_share_mode_entry *sme = &list[list_num];
209 struct process_id pid = share->pid;
211 /* Check this process really exists. */
212 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
213 continue; /* No longer exists. */
216 /* Ignore deferred open entries. */
217 if (share->op_type == DEFERRED_OPEN_ENTRY) {
221 /* Copy into the external list. */
222 sme->dev = (uint64_t)share->dev;
223 sme->ino = (uint64_t)share->inode;
224 sme->share_access = (uint32_t)share->share_access;
225 sme->access_mask = (uint32_t)share->access_mask;
226 sme->open_time.tv_sec = share->time.tv_sec;
227 sme->open_time.tv_usec = share->time.tv_usec;
228 sme->file_id = (uint32_t)share->share_file_id;
229 sme->pid = share->pid;
239 *p_delete_on_close = ld->u.s.delete_on_close;
246 * Create an entry in the Samba share mode db.
249 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
252 const struct smb_share_mode_entry *new_entry,
253 const char *sharepath, /* Must be absolute utf8 path. */
254 const char *filename) /* Must be relative utf8 path. */
257 TDB_DATA locking_key = get_locking_key(dev, ino);
258 int orig_num_share_modes = 0;
259 struct locking_data *ld = NULL; /* internal samba db state. */
260 struct share_mode_entry *shares = NULL;
261 char *new_data_p = NULL;
262 size_t new_data_size = 0;
264 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
266 /* We must create the entry. */
267 db_data.dptr = (char *)malloc(
268 (2*sizeof(struct share_mode_entry)) +
269 strlen(sharepath) + 1 +
270 strlen(filename) + 1);
274 ld = (struct locking_data *)db_data.dptr;
275 memset(ld, '\0', sizeof(struct locking_data));
276 ld->u.s.num_share_mode_entries = 1;
277 ld->u.s.delete_on_close = 0;
278 ld->u.s.delete_token_size = 0;
279 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
280 create_share_mode_entry(shares, new_entry);
282 memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry),
284 strlen(sharepath) + 1);
285 memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry) +
286 strlen(sharepath) + 1,
288 strlen(filename) + 1);
290 db_data.dsize = 2*sizeof(struct share_mode_entry) +
291 strlen(sharepath) + 1 +
292 strlen(filename) + 1;
293 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
301 /* Entry exists, we must add a new entry. */
302 new_data_p = (char *)malloc(
303 db_data.dsize + sizeof(struct share_mode_entry));
309 ld = (struct locking_data *)db_data.dptr;
310 orig_num_share_modes = ld->u.s.num_share_mode_entries;
312 /* Copy the original data. */
313 memcpy(new_data_p, db_data.dptr, (orig_num_share_modes+1)*sizeof(struct share_mode_entry));
315 /* Add in the new share mode */
316 shares = (struct share_mode_entry *)(new_data_p +
317 ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)));
319 create_share_mode_entry(shares, new_entry);
321 ld = (struct locking_data *)new_data_p;
322 ld->u.s.num_share_mode_entries++;
324 /* Append the original delete_token and filenames. */
325 memcpy(new_data_p + ((ld->u.s.num_share_mode_entries+1)*sizeof(struct share_mode_entry)),
326 db_data.dptr + ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)),
327 db_data.dsize - ((orig_num_share_modes+1) * sizeof(struct share_mode_entry)));
329 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
333 db_data.dptr = new_data_p;
334 db_data.dsize = new_data_size;
336 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
345 * Create an entry in the Samba share mode db. Original interface - doesn't
346 * Distinguish between share path and filename. Fudge this by using a
347 * sharepath of / and a relative filename of (filename+1).
350 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
353 const struct smb_share_mode_entry *new_entry,
354 const char *filename) /* Must be absolute utf8 path. */
356 if (*filename != '/') {
359 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
363 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
366 const struct smb_share_mode_entry *del_entry)
369 TDB_DATA locking_key = get_locking_key(dev, ino);
370 int orig_num_share_modes = 0;
371 struct locking_data *ld = NULL; /* internal samba db state. */
372 struct share_mode_entry *shares = NULL;
373 char *new_data_p = NULL;
374 size_t remaining_size = 0;
375 size_t i, num_share_modes;
376 const char *remaining_ptr = NULL;
378 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
380 return -1; /* Error - missing entry ! */
383 ld = (struct locking_data *)db_data.dptr;
384 orig_num_share_modes = ld->u.s.num_share_mode_entries;
385 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
387 if (orig_num_share_modes == 1) {
388 /* Only one entry - better be ours... */
389 if (!share_mode_entry_equal(del_entry, shares)) {
390 /* Error ! We can't delete someone else's entry ! */
394 /* It's ours - just remove the entire record. */
396 return tdb_delete(db_ctx->smb_tdb, locking_key);
399 /* More than one - allocate a new record minus the one we'll delete. */
400 new_data_p = (char *)malloc(
401 db_data.dsize - sizeof(struct share_mode_entry));
407 /* Copy the header. */
408 memcpy(new_data_p, db_data.dptr, sizeof(struct share_mode_entry));
411 for (i = 0; i < orig_num_share_modes; i++) {
412 struct share_mode_entry *share = &shares[i];
413 struct process_id pid = share->pid;
415 /* Check this process really exists. */
416 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
417 continue; /* No longer exists. */
420 if (share_mode_entry_equal(del_entry, share)) {
421 continue; /* This is our delete taget. */
424 memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
425 share, sizeof(struct share_mode_entry) );
430 if (num_share_modes == 0) {
431 /* None left after pruning. Delete record. */
434 return tdb_delete(db_ctx->smb_tdb, locking_key);
437 /* Copy any delete token plus the terminating filenames. */
438 remaining_ptr = db_data.dptr + ((orig_num_share_modes+1) * sizeof(struct share_mode_entry));
439 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
441 memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
447 db_data.dptr = new_data_p;
449 /* Re-save smaller record. */
450 ld = (struct locking_data *)db_data.dptr;
451 ld->u.s.num_share_mode_entries = num_share_modes;
453 db_data.dsize = ((num_share_modes+1)*sizeof(struct share_mode_entry)) + remaining_size;
455 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
463 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
466 const struct smb_share_mode_entry *set_entry,
467 const struct smb_share_mode_entry *new_entry)
470 TDB_DATA locking_key = get_locking_key(dev, ino);
471 int num_share_modes = 0;
472 struct locking_data *ld = NULL; /* internal samba db state. */
473 struct share_mode_entry *shares = NULL;
477 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
479 return -1; /* Error - missing entry ! */
482 ld = (struct locking_data *)db_data.dptr;
483 num_share_modes = ld->u.s.num_share_mode_entries;
484 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
486 for (i = 0; i < num_share_modes; i++) {
487 struct share_mode_entry *share = &shares[i];
488 struct process_id pid = share->pid;
490 /* Check this process really exists. */
491 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
492 continue; /* No longer exists. */
495 if (share_mode_entry_equal(set_entry, share)) {
496 create_share_mode_entry(share, new_entry);
507 /* Save modified data. */
508 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {