Fix bug #Bug 6090 renaming or deleting a "not matching/resolving" symlink is failing.
[Samba.git] / source3 / libsmb / smb_share_modes.c
blobaf3f7b0dd588a984683e1f9db1f1f2e784e7e387
1 /*
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
11 ** under the LGPL
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 #include "includes.h"
28 #include "smb_share_modes.h"
30 /* Database context handle. */
31 struct smbdb_ctx {
32 TDB_CONTEXT *smb_tdb;
35 /* Remove the paranoid malloc checker. */
36 #ifdef malloc
37 #undef malloc
38 #endif
40 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
41 uint64_t ino, const struct smb_share_mode_entry *new_entry,
42 const char *sharepath, const char *filename);
44 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
46 return (p1->pid == p2->pid);
49 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
51 return proc->pid;
55 * open/close sharemode database.
58 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
60 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
62 if (!smb_db) {
63 return NULL;
66 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
68 smb_db->smb_tdb = tdb_open(db_path,
69 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
70 O_RDWR|O_CREAT,
71 0644);
73 if (!smb_db->smb_tdb) {
74 free(smb_db);
75 return NULL;
78 /* Should check that this is the correct version.... */
79 return smb_db;
82 /* key and data records in the tdb locking database */
83 struct locking_key {
84 SMB_DEV_T dev;
85 SMB_INO_T inode;
88 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
90 int ret = tdb_close(db_ctx->smb_tdb);
91 free(db_ctx);
92 return ret;
95 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
96 uint64_t ino)
98 TDB_DATA ld;
100 memset(lk, '\0', sizeof(*lk));
101 lk->dev = (SMB_DEV_T)dev;
102 lk->inode = (SMB_INO_T)ino;
103 ld.dptr = (uint8 *)lk;
104 ld.dsize = sizeof(*lk);
105 return ld;
109 * lock/unlock entry in sharemode database.
112 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
113 uint64_t dev,
114 uint64_t ino)
116 struct locking_key lk;
117 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino));
120 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
121 uint64_t dev,
122 uint64_t ino)
124 struct locking_key lk;
125 return tdb_chainunlock(db_ctx->smb_tdb,
126 get_locking_key(&lk, dev, ino));
130 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
133 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
134 const struct share_mode_entry *entry)
136 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
137 e_entry->file_id == (uint32_t)entry->share_file_id &&
138 e_entry->open_time.tv_sec == entry->time.tv_sec &&
139 e_entry->open_time.tv_usec == entry->time.tv_usec &&
140 e_entry->share_access == (uint32_t)entry->share_access &&
141 e_entry->access_mask == (uint32_t)entry->access_mask &&
142 e_entry->dev == entry->id.devid &&
143 e_entry->ino == entry->id.inode);
147 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
150 static void create_share_mode_entry(struct share_mode_entry *out,
151 const struct smb_share_mode_entry *in)
153 memset(out, '\0', sizeof(struct share_mode_entry));
155 out->pid = in->pid;
156 out->share_file_id = (unsigned long)in->file_id;
157 out->time.tv_sec = in->open_time.tv_sec;
158 out->time.tv_usec = in->open_time.tv_usec;
159 out->share_access = in->share_access;
160 out->access_mask = in->access_mask;
161 out->id.devid = in->dev;
162 out->id.inode = in->ino;
163 out->uid = (uint32)geteuid();
164 out->flags = 0;
168 * Return the current share mode list for an open file.
169 * This uses similar (but simplified) logic to locking/locking.c
172 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
173 uint64_t dev,
174 uint64_t ino,
175 struct smb_share_mode_entry **pp_list,
176 unsigned char *p_delete_on_close)
178 struct locking_key lk;
179 TDB_DATA db_data;
180 struct smb_share_mode_entry *list = NULL;
181 int num_share_modes = 0;
182 struct locking_data *ld = NULL; /* internal samba db state. */
183 struct share_mode_entry *shares = NULL;
184 size_t i;
185 int list_num;
187 *pp_list = NULL;
188 *p_delete_on_close = 0;
190 db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino));
191 if (!db_data.dptr) {
192 return 0;
195 ld = (struct locking_data *)db_data.dptr;
196 num_share_modes = ld->u.s.num_share_mode_entries;
198 if (!num_share_modes) {
199 free(db_data.dptr);
200 return 0;
203 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
204 if (!list) {
205 free(db_data.dptr);
206 return -1;
209 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
211 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
213 list_num = 0;
214 for (i = 0; i < num_share_modes; i++) {
215 struct share_mode_entry *share = &shares[i];
216 struct smb_share_mode_entry *sme = &list[list_num];
217 struct server_id pid = share->pid;
219 /* Check this process really exists. */
220 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
221 continue; /* No longer exists. */
224 /* Ignore deferred open entries. */
225 if (share->op_type == DEFERRED_OPEN_ENTRY) {
226 continue;
229 /* Copy into the external list. */
230 sme->dev = share->id.devid;
231 sme->ino = share->id.inode;
232 sme->share_access = (uint32_t)share->share_access;
233 sme->access_mask = (uint32_t)share->access_mask;
234 sme->open_time.tv_sec = share->time.tv_sec;
235 sme->open_time.tv_usec = share->time.tv_usec;
236 sme->file_id = (uint32_t)share->share_file_id;
237 sme->pid = share->pid;
238 list_num++;
241 if (list_num == 0) {
242 free(db_data.dptr);
243 free(list);
244 return 0;
247 *p_delete_on_close = ld->u.s.delete_on_close;
248 *pp_list = list;
249 free(db_data.dptr);
250 return list_num;
254 * Create an entry in the Samba share mode db.
257 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
258 uint64_t dev,
259 uint64_t ino,
260 const struct smb_share_mode_entry *new_entry,
261 const char *sharepath, /* Must be absolute utf8 path. */
262 const char *filename) /* Must be relative utf8 path. */
264 TDB_DATA db_data;
265 struct locking_key lk;
266 TDB_DATA locking_key = get_locking_key(&lk, dev, ino);
267 int orig_num_share_modes = 0;
268 struct locking_data *ld = NULL; /* internal samba db state. */
269 struct share_mode_entry *shares = NULL;
270 uint8 *new_data_p = NULL;
271 size_t new_data_size = 0;
273 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
274 if (!db_data.dptr) {
275 /* We must create the entry. */
276 db_data.dptr = (uint8 *)malloc(
277 sizeof(struct locking_data) +
278 sizeof(struct share_mode_entry) +
279 strlen(sharepath) + 1 +
280 strlen(filename) + 1);
281 if (!db_data.dptr) {
282 return -1;
284 ld = (struct locking_data *)db_data.dptr;
285 memset(ld, '\0', sizeof(struct locking_data));
286 ld->u.s.num_share_mode_entries = 1;
287 ld->u.s.delete_on_close = 0;
288 ld->u.s.delete_token_size = 0;
289 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
290 create_share_mode_entry(shares, new_entry);
292 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
293 sharepath,
294 strlen(sharepath) + 1);
295 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
296 strlen(sharepath) + 1,
297 filename,
298 strlen(filename) + 1);
300 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
301 strlen(sharepath) + 1 +
302 strlen(filename) + 1;
303 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
304 free(db_data.dptr);
305 return -1;
307 free(db_data.dptr);
308 return 0;
311 /* Entry exists, we must add a new entry. */
312 new_data_p = (uint8 *)malloc(
313 db_data.dsize + sizeof(struct share_mode_entry));
314 if (!new_data_p) {
315 free(db_data.dptr);
316 return -1;
319 ld = (struct locking_data *)db_data.dptr;
320 orig_num_share_modes = ld->u.s.num_share_mode_entries;
322 /* Copy the original data. */
323 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
325 /* Add in the new share mode */
326 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
327 (orig_num_share_modes * sizeof(struct share_mode_entry)));
329 create_share_mode_entry(shares, new_entry);
331 ld = (struct locking_data *)new_data_p;
332 ld->u.s.num_share_mode_entries++;
334 /* Append the original delete_token and filenames. */
335 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
336 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
337 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
339 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
341 free(db_data.dptr);
343 db_data.dptr = new_data_p;
344 db_data.dsize = new_data_size;
346 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
347 free(db_data.dptr);
348 return -1;
350 free(db_data.dptr);
351 return 0;
355 * Create an entry in the Samba share mode db. Original interface - doesn't
356 * Distinguish between share path and filename. Fudge this by using a
357 * sharepath of / and a relative filename of (filename+1).
360 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
361 uint64_t dev,
362 uint64_t ino,
363 const struct smb_share_mode_entry *new_entry,
364 const char *filename) /* Must be absolute utf8 path. */
366 if (*filename != '/') {
367 abort();
369 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
370 "/", &filename[1]);
373 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
374 uint64_t dev,
375 uint64_t ino,
376 const struct smb_share_mode_entry *del_entry)
378 TDB_DATA db_data;
379 struct locking_key lk;
380 TDB_DATA locking_key = get_locking_key(&lk, dev, ino);
381 int orig_num_share_modes = 0;
382 struct locking_data *ld = NULL; /* internal samba db state. */
383 struct share_mode_entry *shares = NULL;
384 uint8 *new_data_p = NULL;
385 size_t remaining_size = 0;
386 size_t i, num_share_modes;
387 const uint8 *remaining_ptr = NULL;
389 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
390 if (!db_data.dptr) {
391 return -1; /* Error - missing entry ! */
394 ld = (struct locking_data *)db_data.dptr;
395 orig_num_share_modes = ld->u.s.num_share_mode_entries;
396 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
398 if (orig_num_share_modes == 1) {
399 /* Only one entry - better be ours... */
400 if (!share_mode_entry_equal(del_entry, shares)) {
401 /* Error ! We can't delete someone else's entry ! */
402 free(db_data.dptr);
403 return -1;
405 /* It's ours - just remove the entire record. */
406 free(db_data.dptr);
407 return tdb_delete(db_ctx->smb_tdb, locking_key);
410 /* More than one - allocate a new record minus the one we'll delete. */
411 new_data_p = (uint8 *)malloc(
412 db_data.dsize - sizeof(struct share_mode_entry));
413 if (!new_data_p) {
414 free(db_data.dptr);
415 return -1;
418 /* Copy the header. */
419 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
421 num_share_modes = 0;
422 for (i = 0; i < orig_num_share_modes; i++) {
423 struct share_mode_entry *share = &shares[i];
424 struct server_id pid = share->pid;
426 /* Check this process really exists. */
427 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
428 continue; /* No longer exists. */
431 if (share_mode_entry_equal(del_entry, share)) {
432 continue; /* This is our delete taget. */
435 memcpy(new_data_p + sizeof(struct locking_data) +
436 (num_share_modes * sizeof(struct share_mode_entry)),
437 share, sizeof(struct share_mode_entry) );
439 num_share_modes++;
442 if (num_share_modes == 0) {
443 /* None left after pruning. Delete record. */
444 free(db_data.dptr);
445 free(new_data_p);
446 return tdb_delete(db_ctx->smb_tdb, locking_key);
449 /* Copy any delete token plus the terminating filenames. */
450 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
451 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
453 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
454 remaining_ptr,
455 remaining_size);
457 free(db_data.dptr);
459 db_data.dptr = new_data_p;
461 /* Re-save smaller record. */
462 ld = (struct locking_data *)db_data.dptr;
463 ld->u.s.num_share_mode_entries = num_share_modes;
465 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
467 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
468 free(db_data.dptr);
469 return -1;
471 free(db_data.dptr);
472 return 0;
475 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
476 uint64_t dev,
477 uint64_t ino,
478 const struct smb_share_mode_entry *set_entry,
479 const struct smb_share_mode_entry *new_entry)
481 TDB_DATA db_data;
482 struct locking_key lk;
483 TDB_DATA locking_key = get_locking_key(&lk, dev, ino);
484 int num_share_modes = 0;
485 struct locking_data *ld = NULL; /* internal samba db state. */
486 struct share_mode_entry *shares = NULL;
487 size_t i;
488 int found_entry = 0;
490 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
491 if (!db_data.dptr) {
492 return -1; /* Error - missing entry ! */
495 ld = (struct locking_data *)db_data.dptr;
496 num_share_modes = ld->u.s.num_share_mode_entries;
497 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
499 for (i = 0; i < num_share_modes; i++) {
500 struct share_mode_entry *share = &shares[i];
501 struct server_id pid = share->pid;
503 /* Check this process really exists. */
504 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
505 continue; /* No longer exists. */
508 if (share_mode_entry_equal(set_entry, share)) {
509 create_share_mode_entry(share, new_entry);
510 found_entry = 1;
511 break;
515 if (!found_entry) {
516 free(db_data.dptr);
517 return -1;
520 /* Save modified data. */
521 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
522 free(db_data.dptr);
523 return -1;
525 free(db_data.dptr);
526 return 0;