samba3dump: Add subunit-enabled test for samba3dump.
[Samba.git] / source3 / libsmb / smb_share_modes.c
blobf2decc1779a3bbe8dfc1cdcdd580bb89d214b863
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 "system/filesys.h"
29 #include "smb_share_modes.h"
30 #include "tdb_compat.h"
31 #include "librpc/gen_ndr/open_files.h"
32 #include <ccan/hash/hash.h>
34 /* Database context handle. */
35 struct smbdb_ctx {
36 TDB_CONTEXT *smb_tdb;
39 /* Remove the paranoid malloc checker. */
40 #ifdef malloc
41 #undef malloc
42 #endif
45 * Internal structure of locking.tdb share mode db.
46 * Used by locking.c and libsmbsharemodes.c
49 struct locking_data {
50 union {
51 struct {
52 int num_share_mode_entries;
53 struct timespec old_write_time;
54 struct timespec changed_write_time;
55 uint32 num_delete_token_entries;
56 } s;
57 struct share_mode_entry dummy; /* Needed for alignment. */
58 } u;
59 /* The following four entries are implicit
61 (1) struct share_mode_entry modes[num_share_mode_entries];
63 (2) A num_delete_token_entries of structs {
64 uint32_t len_delete_token;
65 char unix_token[len_delete_token] (divisible by 4).
68 (3) char share_name[];
69 (4) char file_name[];
73 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
74 uint64_t ino, uint64_t extid,
75 const struct smb_share_mode_entry *new_entry,
76 const char *sharepath, const char *filename);
78 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
80 return (p1->pid == p2->pid);
83 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
85 return proc->pid;
89 * open/close sharemode database.
92 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
94 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
96 if (!smb_db) {
97 return NULL;
100 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
102 /* FIXME: We should *never* open a tdb without logging! */
103 smb_db->smb_tdb = tdb_open_compat(db_path,
104 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
105 O_RDWR|O_CREAT,
106 0644,
107 NULL, NULL);
109 if (!smb_db->smb_tdb) {
110 free(smb_db);
111 return NULL;
114 /* Should check that this is the correct version.... */
115 return smb_db;
118 /* key and data records in the tdb locking database */
119 struct locking_key {
120 SMB_DEV_T dev;
121 SMB_INO_T inode;
122 uint64_t extid;
125 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
127 int ret = tdb_close(db_ctx->smb_tdb);
128 free(db_ctx);
129 return ret;
132 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
133 uint64_t ino, uint64_t extid)
135 TDB_DATA ld;
137 memset(lk, '\0', sizeof(*lk));
138 lk->dev = (SMB_DEV_T)dev;
139 lk->inode = (SMB_INO_T)ino;
140 lk->extid = extid;
141 ld.dptr = (uint8 *)lk;
142 ld.dsize = sizeof(*lk);
143 return ld;
147 * lock/unlock entry in sharemode database.
150 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
151 uint64_t dev,
152 uint64_t ino,
153 uint64_t extid)
155 struct locking_key lk;
156 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
157 extid)) == 0 ? 0 : -1;
160 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
161 uint64_t dev,
162 uint64_t ino,
163 uint64_t extid)
165 struct locking_key lk;
166 tdb_chainunlock(db_ctx->smb_tdb,
167 get_locking_key(&lk, dev, ino, extid));
168 return 0;
172 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
175 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
176 const struct share_mode_entry *entry)
178 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
179 e_entry->file_id == (uint32_t)entry->share_file_id &&
180 e_entry->open_time.tv_sec == entry->time.tv_sec &&
181 e_entry->open_time.tv_usec == entry->time.tv_usec &&
182 e_entry->share_access == (uint32_t)entry->share_access &&
183 e_entry->access_mask == (uint32_t)entry->access_mask &&
184 e_entry->dev == entry->id.devid &&
185 e_entry->ino == entry->id.inode &&
186 e_entry->extid == entry->id.extid);
190 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
193 static void create_share_mode_entry(struct share_mode_entry *out,
194 const struct smb_share_mode_entry *in,
195 uint32_t name_hash)
197 memset(out, '\0', sizeof(struct share_mode_entry));
199 out->pid = in->pid;
200 out->share_file_id = (unsigned long)in->file_id;
201 out->time.tv_sec = in->open_time.tv_sec;
202 out->time.tv_usec = in->open_time.tv_usec;
203 out->share_access = in->share_access;
204 out->access_mask = in->access_mask;
205 out->id.devid = in->dev;
206 out->id.inode = in->ino;
207 out->id.extid = in->extid;
208 out->uid = (uint32)geteuid();
209 out->flags = 0;
210 out->name_hash = name_hash;
214 * Return the current share mode list for an open file.
215 * This uses similar (but simplified) logic to locking/locking.c
218 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
219 uint64_t dev,
220 uint64_t ino,
221 uint64_t extid,
222 struct smb_share_mode_entry **pp_list,
223 unsigned char *p_delete_on_close)
225 struct locking_key lk;
226 TDB_DATA db_data;
227 struct smb_share_mode_entry *list = NULL;
228 int num_share_modes = 0;
229 struct locking_data *ld = NULL; /* internal samba db state. */
230 struct share_mode_entry *shares = NULL;
231 size_t i;
232 int list_num;
234 *pp_list = NULL;
235 *p_delete_on_close = 0;
237 db_data = tdb_fetch_compat(db_ctx->smb_tdb,
238 get_locking_key(&lk, dev, ino, extid));
239 if (!db_data.dptr) {
240 return 0;
243 ld = (struct locking_data *)db_data.dptr;
244 num_share_modes = ld->u.s.num_share_mode_entries;
246 if (!num_share_modes) {
247 free(db_data.dptr);
248 return 0;
251 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
252 if (!list) {
253 free(db_data.dptr);
254 return -1;
257 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
259 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
261 list_num = 0;
262 for (i = 0; i < num_share_modes; i++) {
263 struct share_mode_entry *share = &shares[i];
264 struct smb_share_mode_entry *sme = &list[list_num];
265 struct server_id pid = share->pid;
267 /* Check this process really exists. */
268 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
269 continue; /* No longer exists. */
272 /* Copy into the external list. */
273 sme->dev = share->id.devid;
274 sme->ino = share->id.inode;
275 sme->extid = share->id.extid;
276 sme->share_access = (uint32_t)share->share_access;
277 sme->access_mask = (uint32_t)share->access_mask;
278 sme->open_time.tv_sec = share->time.tv_sec;
279 sme->open_time.tv_usec = share->time.tv_usec;
280 sme->file_id = (uint32_t)share->share_file_id;
281 sme->pid = share->pid;
282 list_num++;
285 if (list_num == 0) {
286 free(db_data.dptr);
287 free(list);
288 return 0;
291 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
292 *pp_list = list;
293 free(db_data.dptr);
294 return list_num;
297 static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
299 char *fullpath = NULL;
300 size_t sharepath_size = strlen(sharepath);
301 size_t filename_size = strlen(filename);
302 uint32_t name_hash;
304 *err = 0;
305 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
306 if (fullpath == NULL) {
307 *err = 1;
308 return 0;
310 memcpy(fullpath, sharepath, sharepath_size);
311 fullpath[sharepath_size] = '/';
312 memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
314 name_hash = hash(fullpath, strlen(fullpath) + 1, 0);
315 free(fullpath);
316 return name_hash;
320 * Create an entry in the Samba share mode db.
323 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
324 uint64_t dev,
325 uint64_t ino,
326 uint64_t extid,
327 const struct smb_share_mode_entry *new_entry,
328 const char *sharepath, /* Must be absolute utf8 path. */
329 const char *filename) /* Must be relative utf8 path. */
331 TDB_DATA db_data;
332 struct locking_key lk;
333 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
334 int orig_num_share_modes = 0;
335 struct locking_data *ld = NULL; /* internal samba db state. */
336 struct share_mode_entry *shares = NULL;
337 uint8 *new_data_p = NULL;
338 size_t new_data_size = 0;
339 int err = 0;
340 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
342 if (err) {
343 return -1;
346 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
347 if (!db_data.dptr) {
348 /* We must create the entry. */
349 db_data.dptr = (uint8 *)malloc(
350 sizeof(struct locking_data) +
351 sizeof(struct share_mode_entry) +
352 strlen(sharepath) + 1 +
353 strlen(filename) + 1);
354 if (!db_data.dptr) {
355 return -1;
357 ld = (struct locking_data *)db_data.dptr;
358 memset(ld, '\0', sizeof(struct locking_data));
359 ld->u.s.num_share_mode_entries = 1;
360 ld->u.s.num_delete_token_entries = 0;
361 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
362 create_share_mode_entry(shares, new_entry, name_hash);
364 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
365 sharepath,
366 strlen(sharepath) + 1);
367 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
368 strlen(sharepath) + 1,
369 filename,
370 strlen(filename) + 1);
372 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
373 strlen(sharepath) + 1 +
374 strlen(filename) + 1;
375 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) != 0) {
376 free(db_data.dptr);
377 return -1;
379 free(db_data.dptr);
380 return 0;
383 /* Entry exists, we must add a new entry. */
384 new_data_p = (uint8 *)malloc(
385 db_data.dsize + sizeof(struct share_mode_entry));
386 if (!new_data_p) {
387 free(db_data.dptr);
388 return -1;
391 ld = (struct locking_data *)db_data.dptr;
392 orig_num_share_modes = ld->u.s.num_share_mode_entries;
394 /* Copy the original data. */
395 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
397 /* Add in the new share mode */
398 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
399 (orig_num_share_modes * sizeof(struct share_mode_entry)));
401 create_share_mode_entry(shares, new_entry, name_hash);
403 ld = (struct locking_data *)new_data_p;
404 ld->u.s.num_share_mode_entries++;
406 /* Append the original delete_tokens and filenames. */
407 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
408 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
409 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
411 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
413 free(db_data.dptr);
415 db_data.dptr = new_data_p;
416 db_data.dsize = new_data_size;
418 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
419 free(db_data.dptr);
420 return -1;
422 free(db_data.dptr);
423 return 0;
427 * Create an entry in the Samba share mode db. Original interface - doesn't
428 * Distinguish between share path and filename. Fudge this by using a
429 * sharepath of / and a relative filename of (filename+1).
432 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
433 uint64_t dev,
434 uint64_t ino,
435 uint64_t extid,
436 const struct smb_share_mode_entry *new_entry,
437 const char *filename) /* Must be absolute utf8 path. */
439 if (*filename != '/') {
440 abort();
442 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
443 "/", &filename[1]);
446 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
447 uint64_t dev,
448 uint64_t ino,
449 uint64_t extid,
450 const struct smb_share_mode_entry *del_entry)
452 TDB_DATA db_data;
453 struct locking_key lk;
454 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
455 int orig_num_share_modes = 0;
456 struct locking_data *ld = NULL; /* internal samba db state. */
457 struct share_mode_entry *shares = NULL;
458 uint8 *new_data_p = NULL;
459 size_t remaining_size = 0;
460 size_t i, num_share_modes;
461 const uint8 *remaining_ptr = NULL;
463 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
464 if (!db_data.dptr) {
465 return -1; /* Error - missing entry ! */
468 ld = (struct locking_data *)db_data.dptr;
469 orig_num_share_modes = ld->u.s.num_share_mode_entries;
470 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
472 if (orig_num_share_modes == 1) {
473 /* Only one entry - better be ours... */
474 if (!share_mode_entry_equal(del_entry, shares)) {
475 /* Error ! We can't delete someone else's entry ! */
476 free(db_data.dptr);
477 return -1;
479 /* It's ours - just remove the entire record. */
480 free(db_data.dptr);
481 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
484 /* More than one - allocate a new record minus the one we'll delete. */
485 new_data_p = (uint8 *)malloc(
486 db_data.dsize - sizeof(struct share_mode_entry));
487 if (!new_data_p) {
488 free(db_data.dptr);
489 return -1;
492 /* Copy the header. */
493 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
495 num_share_modes = 0;
496 for (i = 0; i < orig_num_share_modes; i++) {
497 struct share_mode_entry *share = &shares[i];
498 struct server_id pid = share->pid;
500 /* Check this process really exists. */
501 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
502 continue; /* No longer exists. */
505 if (share_mode_entry_equal(del_entry, share)) {
506 continue; /* This is our delete taget. */
509 memcpy(new_data_p + sizeof(struct locking_data) +
510 (num_share_modes * sizeof(struct share_mode_entry)),
511 share, sizeof(struct share_mode_entry) );
513 num_share_modes++;
516 if (num_share_modes == 0) {
517 /* None left after pruning. Delete record. */
518 free(db_data.dptr);
519 free(new_data_p);
520 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
523 /* Copy any delete tokens plus the terminating filenames. */
524 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
525 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
527 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
528 remaining_ptr,
529 remaining_size);
531 free(db_data.dptr);
533 db_data.dptr = new_data_p;
535 /* Re-save smaller record. */
536 ld = (struct locking_data *)db_data.dptr;
537 ld->u.s.num_share_mode_entries = num_share_modes;
539 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
541 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
542 free(db_data.dptr);
543 return -1;
545 free(db_data.dptr);
546 return 0;
549 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
550 uint64_t dev,
551 uint64_t ino,
552 uint64_t extid,
553 const struct smb_share_mode_entry *set_entry,
554 const struct smb_share_mode_entry *new_entry)
556 TDB_DATA db_data;
557 struct locking_key lk;
558 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
559 int num_share_modes = 0;
560 struct locking_data *ld = NULL; /* internal samba db state. */
561 struct share_mode_entry *shares = NULL;
562 size_t i;
563 int found_entry = 0;
565 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
566 if (!db_data.dptr) {
567 return -1; /* Error - missing entry ! */
570 ld = (struct locking_data *)db_data.dptr;
571 num_share_modes = ld->u.s.num_share_mode_entries;
572 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
574 for (i = 0; i < num_share_modes; i++) {
575 struct share_mode_entry *share = &shares[i];
576 struct server_id pid = share->pid;
578 /* Check this process really exists. */
579 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
580 continue; /* No longer exists. */
583 if (share_mode_entry_equal(set_entry, share)) {
584 create_share_mode_entry(share, new_entry, share->name_hash);
585 found_entry = 1;
586 break;
590 if (!found_entry) {
591 free(db_data.dptr);
592 return -1;
595 /* Save modified data. */
596 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
597 free(db_data.dptr);
598 return -1;
600 free(db_data.dptr);
601 return 0;