libcli/smb: move smb2cli_close.c from source3 to the toplevel
[Samba/gebeck_regimport.git] / source3 / libsmb / smb_share_modes.c
blobf6f8bf9a1bee615946ea7a391967ee8f8ff53244
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 #define UID_WRAPPER_NOT_REPLACE
28 #include "includes.h"
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. */
36 struct smbdb_ctx {
37 TDB_CONTEXT *smb_tdb;
40 /* Remove the paranoid malloc checker. */
41 #ifdef malloc
42 #undef malloc
43 #endif
46 * Internal structure of locking.tdb share mode db.
47 * Used by locking.c and libsmbsharemodes.c
50 struct locking_data {
51 union {
52 struct {
53 int num_share_mode_entries;
54 struct timespec old_write_time;
55 struct timespec changed_write_time;
56 uint32 num_delete_token_entries;
57 } s;
58 struct share_mode_entry dummy; /* Needed for alignment. */
59 } u;
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[];
70 (4) char file_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)
86 return proc->pid;
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));
97 if (!smb_db) {
98 return NULL;
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,
106 O_RDWR|O_CREAT,
107 0644,
108 NULL, NULL);
110 if (!smb_db->smb_tdb) {
111 free(smb_db);
112 return NULL;
115 /* Should check that this is the correct version.... */
116 return smb_db;
119 /* key and data records in the tdb locking database */
120 struct locking_key {
121 SMB_DEV_T dev;
122 SMB_INO_T inode;
123 uint64_t extid;
126 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
128 int ret = tdb_close(db_ctx->smb_tdb);
129 free(db_ctx);
130 return ret;
133 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
134 uint64_t ino, uint64_t extid)
136 TDB_DATA ld;
138 memset(lk, '\0', sizeof(*lk));
139 lk->dev = (SMB_DEV_T)dev;
140 lk->inode = (SMB_INO_T)ino;
141 lk->extid = extid;
142 ld.dptr = (uint8 *)lk;
143 ld.dsize = sizeof(*lk);
144 return ld;
148 * lock/unlock entry in sharemode database.
151 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
152 uint64_t dev,
153 uint64_t ino,
154 uint64_t extid)
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,
162 uint64_t dev,
163 uint64_t ino,
164 uint64_t extid)
166 struct locking_key lk;
167 tdb_chainunlock(db_ctx->smb_tdb,
168 get_locking_key(&lk, dev, ino, extid));
169 return 0;
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,
196 uint32_t name_hash)
198 memset(out, '\0', sizeof(struct share_mode_entry));
200 out->pid = in->pid;
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();
210 out->flags = 0;
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,
220 uint64_t dev,
221 uint64_t ino,
222 uint64_t extid,
223 struct smb_share_mode_entry **pp_list,
224 unsigned char *p_delete_on_close)
226 struct locking_key lk;
227 TDB_DATA db_data;
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;
232 size_t i;
233 int list_num;
235 *pp_list = 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));
240 if (!db_data.dptr) {
241 return 0;
244 ld = (struct locking_data *)db_data.dptr;
245 num_share_modes = ld->u.s.num_share_mode_entries;
247 if (!num_share_modes) {
248 free(db_data.dptr);
249 return 0;
252 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
253 if (!list) {
254 free(db_data.dptr);
255 return -1;
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));
262 list_num = 0;
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) {
275 continue;
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;
288 list_num++;
291 if (list_num == 0) {
292 free(db_data.dptr);
293 free(list);
294 return 0;
297 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
298 *pp_list = list;
299 free(db_data.dptr);
300 return list_num;
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);
308 uint32_t name_hash;
310 *err = 0;
311 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
312 if (fullpath == NULL) {
313 *err = 1;
314 return 0;
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);
321 free(fullpath);
322 return name_hash;
326 * Create an entry in the Samba share mode db.
329 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
330 uint64_t dev,
331 uint64_t ino,
332 uint64_t extid,
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. */
337 TDB_DATA db_data;
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;
345 int err = 0;
346 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
348 if (err) {
349 return -1;
352 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
353 if (!db_data.dptr) {
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);
360 if (!db_data.dptr) {
361 return -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),
371 sharepath,
372 strlen(sharepath) + 1);
373 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
374 strlen(sharepath) + 1,
375 filename,
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) {
382 free(db_data.dptr);
383 return -1;
385 free(db_data.dptr);
386 return 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));
392 if (!new_data_p) {
393 free(db_data.dptr);
394 return -1;
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);
419 free(db_data.dptr);
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) {
425 free(db_data.dptr);
426 return -1;
428 free(db_data.dptr);
429 return 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,
439 uint64_t dev,
440 uint64_t ino,
441 uint64_t extid,
442 const struct smb_share_mode_entry *new_entry,
443 const char *filename) /* Must be absolute utf8 path. */
445 if (*filename != '/') {
446 abort();
448 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
449 "/", &filename[1]);
452 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
453 uint64_t dev,
454 uint64_t ino,
455 uint64_t extid,
456 const struct smb_share_mode_entry *del_entry)
458 TDB_DATA db_data;
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);
470 if (!db_data.dptr) {
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 ! */
482 free(db_data.dptr);
483 return -1;
485 /* It's ours - just remove the entire record. */
486 free(db_data.dptr);
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));
493 if (!new_data_p) {
494 free(db_data.dptr);
495 return -1;
498 /* Copy the header. */
499 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
501 num_share_modes = 0;
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) );
519 num_share_modes++;
522 if (num_share_modes == 0) {
523 /* None left after pruning. Delete record. */
524 free(db_data.dptr);
525 free(new_data_p);
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)),
534 remaining_ptr,
535 remaining_size);
537 free(db_data.dptr);
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) {
548 free(db_data.dptr);
549 return -1;
551 free(db_data.dptr);
552 return 0;
555 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
556 uint64_t dev,
557 uint64_t ino,
558 uint64_t extid,
559 const struct smb_share_mode_entry *set_entry,
560 const struct smb_share_mode_entry *new_entry)
562 TDB_DATA db_data;
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;
568 size_t i;
569 int found_entry = 0;
571 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
572 if (!db_data.dptr) {
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);
591 found_entry = 1;
592 break;
596 if (!found_entry) {
597 free(db_data.dptr);
598 return -1;
601 /* Save modified data. */
602 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
603 free(db_data.dptr);
604 return -1;
606 free(db_data.dptr);
607 return 0;