2 Samba share mode database library external interface library.
3 Used by non-Samba products needing access to the Samba share mode db.
7 THIS CODE IS NON-FUNCTIONAL IN SAMBA 4.2.0 AND ABOVE DUE TO THE CHANGES IN
8 SHARE MODE DATABASE SCHEMA FOR SMB2 LEASES.
10 CONTACT THE AUTHOR jra@samba.org IF YOU WISH TO RE-ENABLE
13 Copyright (C) Jeremy Allison 2005 - 2006
15 sharemodes_procid functions (C) Copyright (C) Volker Lendecke 2005
17 ** NOTE! The following LGPL license applies to this module only.
18 ** This does NOT imply that all of Samba is released
21 This library is free software; you can redistribute it and/or
22 modify it under the terms of the GNU Lesser General Public
23 License as published by the Free Software Foundation; either
24 version 3 of the License, or (at your option) any later version.
26 This library is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 Lesser General Public License for more details.
31 You should have received a copy of the GNU Lesser General Public
32 License along with this library; if not, see <http://www.gnu.org/licenses/>.
36 #include "system/filesys.h"
37 #include "smb_share_modes.h"
38 #include "tdb_compat.h"
39 #include "librpc/gen_ndr/open_files.h"
40 #include <ccan/hash/hash.h>
42 /* Database context handle. */
47 /* Remove the paranoid malloc checker. */
53 * Internal structure of locking.tdb share mode db.
54 * Used by locking.c and libsmbsharemodes.c
60 int num_share_mode_entries
;
61 struct timespec old_write_time
;
62 struct timespec changed_write_time
;
63 uint32 num_delete_token_entries
;
65 struct share_mode_entry dummy
; /* Needed for alignment. */
67 /* The following four entries are implicit
69 (1) struct share_mode_entry modes[num_share_mode_entries];
71 (2) A num_delete_token_entries of structs {
72 uint32_t len_delete_token;
73 char unix_token[len_delete_token] (divisible by 4).
76 (3) char share_name[];
81 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
, uint64_t dev
,
82 uint64_t ino
, uint64_t extid
,
83 const struct smb_share_mode_entry
*new_entry
,
84 const char *sharepath
, const char *filename
);
86 static bool sharemodes_procid_equal(const struct server_id
*p1
, const struct server_id
*p2
)
88 return (p1
->pid
== p2
->pid
);
91 static pid_t
sharemodes_procid_to_pid(const struct server_id
*proc
)
97 * open/close sharemode database.
100 struct smbdb_ctx
*smb_share_mode_db_open(const char *db_path
)
102 struct smbdb_ctx
*smb_db
= (struct smbdb_ctx
*)malloc(sizeof(struct smbdb_ctx
));
108 memset(smb_db
, '\0', sizeof(struct smbdb_ctx
));
110 /* FIXME: We should *never* open a tdb without logging! */
111 smb_db
->smb_tdb
= tdb_open_compat(db_path
,
112 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
117 if (!smb_db
->smb_tdb
) {
122 /* Should check that this is the correct version.... */
126 /* key and data records in the tdb locking database */
133 int smb_share_mode_db_close(struct smbdb_ctx
*db_ctx
)
135 int ret
= tdb_close(db_ctx
->smb_tdb
);
140 static TDB_DATA
get_locking_key(struct locking_key
*lk
, uint64_t dev
,
141 uint64_t ino
, uint64_t extid
)
145 memset(lk
, '\0', sizeof(*lk
));
146 lk
->dev
= (SMB_DEV_T
)dev
;
147 lk
->inode
= (SMB_INO_T
)ino
;
149 ld
.dptr
= (uint8
*)lk
;
150 ld
.dsize
= sizeof(*lk
);
155 * lock/unlock entry in sharemode database.
158 int smb_lock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
163 struct locking_key lk
;
164 return tdb_chainlock(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
,
165 extid
)) == 0 ? 0 : -1;
168 int smb_unlock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
173 struct locking_key lk
;
174 tdb_chainunlock(db_ctx
->smb_tdb
,
175 get_locking_key(&lk
, dev
, ino
, extid
));
180 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
183 static int share_mode_entry_equal(const struct smb_share_mode_entry
*e_entry
,
184 const struct share_mode_entry
*entry
)
186 return (sharemodes_procid_equal(&e_entry
->pid
, &entry
->pid
) &&
187 e_entry
->file_id
== (uint32_t)entry
->share_file_id
&&
188 e_entry
->open_time
.tv_sec
== entry
->time
.tv_sec
&&
189 e_entry
->open_time
.tv_usec
== entry
->time
.tv_usec
&&
190 e_entry
->share_access
== (uint32_t)entry
->share_access
&&
191 e_entry
->access_mask
== (uint32_t)entry
->access_mask
&&
192 e_entry
->dev
== entry
->id
.devid
&&
193 e_entry
->ino
== entry
->id
.inode
&&
194 e_entry
->extid
== entry
->id
.extid
);
198 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
201 static void create_share_mode_entry(struct share_mode_entry
*out
,
202 const struct smb_share_mode_entry
*in
,
205 memset(out
, '\0', sizeof(struct share_mode_entry
));
208 out
->share_file_id
= (unsigned long)in
->file_id
;
209 out
->time
.tv_sec
= in
->open_time
.tv_sec
;
210 out
->time
.tv_usec
= in
->open_time
.tv_usec
;
211 out
->share_access
= in
->share_access
;
212 out
->access_mask
= in
->access_mask
;
213 out
->id
.devid
= in
->dev
;
214 out
->id
.inode
= in
->ino
;
215 out
->id
.extid
= in
->extid
;
216 out
->uid
= (uint32
)geteuid();
218 out
->name_hash
= name_hash
;
222 * Return the current share mode list for an open file.
223 * This uses similar (but simplified) logic to locking/locking.c
226 int smb_get_share_mode_entries(struct smbdb_ctx
*db_ctx
,
230 struct smb_share_mode_entry
**pp_list
,
231 unsigned char *p_delete_on_close
)
233 struct locking_key lk
;
235 struct smb_share_mode_entry
*list
= NULL
;
236 int num_share_modes
= 0;
237 struct locking_data
*ld
= NULL
; /* internal samba db state. */
238 struct share_mode_entry
*shares
= NULL
;
243 *p_delete_on_close
= 0;
245 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
,
246 get_locking_key(&lk
, dev
, ino
, extid
));
251 ld
= (struct locking_data
*)db_data
.dptr
;
252 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
254 if (!num_share_modes
) {
259 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
265 memset(list
, '\0', num_share_modes
* sizeof(struct smb_share_mode_entry
));
267 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
270 for (i
= 0; i
< num_share_modes
; i
++) {
271 struct share_mode_entry
*share
= &shares
[i
];
272 struct smb_share_mode_entry
*sme
= &list
[list_num
];
273 struct server_id pid
= share
->pid
;
275 /* Check this process really exists. */
276 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
277 continue; /* No longer exists. */
280 /* Copy into the external list. */
281 sme
->dev
= share
->id
.devid
;
282 sme
->ino
= share
->id
.inode
;
283 sme
->extid
= share
->id
.extid
;
284 sme
->share_access
= (uint32_t)share
->share_access
;
285 sme
->access_mask
= (uint32_t)share
->access_mask
;
286 sme
->open_time
.tv_sec
= share
->time
.tv_sec
;
287 sme
->open_time
.tv_usec
= share
->time
.tv_usec
;
288 sme
->file_id
= (uint32_t)share
->share_file_id
;
289 sme
->pid
= share
->pid
;
299 *p_delete_on_close
= ld
->u
.s
.num_delete_token_entries
!= 0;
305 static uint32_t smb_name_hash(const char *sharepath
, const char *filename
, int *err
)
307 char *fullpath
= NULL
;
308 size_t sharepath_size
= strlen(sharepath
);
309 size_t filename_size
= strlen(filename
);
313 fullpath
= (char *)malloc(sharepath_size
+ filename_size
+ 2);
314 if (fullpath
== NULL
) {
318 memcpy(fullpath
, sharepath
, sharepath_size
);
319 fullpath
[sharepath_size
] = '/';
320 memcpy(&fullpath
[sharepath_size
+ 1], filename
, filename_size
+ 1);
322 name_hash
= hash(fullpath
, strlen(fullpath
) + 1, 0);
328 * Create an entry in the Samba share mode db.
331 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
335 const struct smb_share_mode_entry
*new_entry
,
336 const char *sharepath
, /* Must be absolute utf8 path. */
337 const char *filename
) /* Must be relative utf8 path. */
340 struct locking_key lk
;
341 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
342 int orig_num_share_modes
= 0;
343 struct locking_data
*ld
= NULL
; /* internal samba db state. */
344 struct share_mode_entry
*shares
= NULL
;
345 uint8
*new_data_p
= NULL
;
346 size_t new_data_size
= 0;
348 uint32_t name_hash
= smb_name_hash(sharepath
, filename
, &err
);
354 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
356 /* We must create the entry. */
357 db_data
.dptr
= (uint8
*)malloc(
358 sizeof(struct locking_data
) +
359 sizeof(struct share_mode_entry
) +
360 strlen(sharepath
) + 1 +
361 strlen(filename
) + 1);
365 ld
= (struct locking_data
*)db_data
.dptr
;
366 memset(ld
, '\0', sizeof(struct locking_data
));
367 ld
->u
.s
.num_share_mode_entries
= 1;
368 ld
->u
.s
.num_delete_token_entries
= 0;
369 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
370 create_share_mode_entry(shares
, new_entry
, name_hash
);
372 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
),
374 strlen(sharepath
) + 1);
375 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
376 strlen(sharepath
) + 1,
378 strlen(filename
) + 1);
380 db_data
.dsize
= sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
381 strlen(sharepath
) + 1 +
382 strlen(filename
) + 1;
383 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_INSERT
) != 0) {
391 /* Entry exists, we must add a new entry. */
392 new_data_p
= (uint8
*)malloc(
393 db_data
.dsize
+ sizeof(struct share_mode_entry
));
399 ld
= (struct locking_data
*)db_data
.dptr
;
400 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
402 /* Copy the original data. */
403 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
405 /* Add in the new share mode */
406 shares
= (struct share_mode_entry
*)(new_data_p
+ sizeof(struct locking_data
) +
407 (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
409 create_share_mode_entry(shares
, new_entry
, name_hash
);
411 ld
= (struct locking_data
*)new_data_p
;
412 ld
->u
.s
.num_share_mode_entries
++;
414 /* Append the original delete_tokens and filenames. */
415 memcpy(new_data_p
+ sizeof(struct locking_data
) + (ld
->u
.s
.num_share_mode_entries
* sizeof(struct share_mode_entry
)),
416 db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)),
417 db_data
.dsize
- sizeof(struct locking_data
) - (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
419 new_data_size
= db_data
.dsize
+ sizeof(struct share_mode_entry
);
423 db_data
.dptr
= new_data_p
;
424 db_data
.dsize
= new_data_size
;
426 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {
435 * Create an entry in the Samba share mode db. Original interface - doesn't
436 * Distinguish between share path and filename. Fudge this by using a
437 * sharepath of / and a relative filename of (filename+1).
440 int smb_create_share_mode_entry(struct smbdb_ctx
*db_ctx
,
444 const struct smb_share_mode_entry
*new_entry
,
445 const char *filename
) /* Must be absolute utf8 path. */
447 if (*filename
!= '/') {
450 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, extid
, new_entry
,
454 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
458 const struct smb_share_mode_entry
*del_entry
)
461 struct locking_key lk
;
462 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
463 int orig_num_share_modes
= 0;
464 struct locking_data
*ld
= NULL
; /* internal samba db state. */
465 struct share_mode_entry
*shares
= NULL
;
466 uint8
*new_data_p
= NULL
;
467 size_t remaining_size
= 0;
468 size_t i
, num_share_modes
;
469 const uint8
*remaining_ptr
= NULL
;
471 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
473 return -1; /* Error - missing entry ! */
476 ld
= (struct locking_data
*)db_data
.dptr
;
477 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
478 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
480 if (orig_num_share_modes
== 1) {
481 /* Only one entry - better be ours... */
482 if (!share_mode_entry_equal(del_entry
, shares
)) {
483 /* Error ! We can't delete someone else's entry ! */
487 /* It's ours - just remove the entire record. */
489 return tdb_delete(db_ctx
->smb_tdb
, locking_key
) ? -1 : 0;
492 /* More than one - allocate a new record minus the one we'll delete. */
493 new_data_p
= (uint8
*)malloc(
494 db_data
.dsize
- sizeof(struct share_mode_entry
));
500 /* Copy the header. */
501 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
504 for (i
= 0; i
< orig_num_share_modes
; i
++) {
505 struct share_mode_entry
*share
= &shares
[i
];
506 struct server_id pid
= share
->pid
;
508 /* Check this process really exists. */
509 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
510 continue; /* No longer exists. */
513 if (share_mode_entry_equal(del_entry
, share
)) {
514 continue; /* This is our delete taget. */
517 memcpy(new_data_p
+ sizeof(struct locking_data
) +
518 (num_share_modes
* sizeof(struct share_mode_entry
)),
519 share
, sizeof(struct share_mode_entry
) );
524 if (num_share_modes
== 0) {
525 /* None left after pruning. Delete record. */
528 return tdb_delete(db_ctx
->smb_tdb
, locking_key
) ? -1 : 0;
531 /* Copy any delete tokens plus the terminating filenames. */
532 remaining_ptr
= db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
));
533 remaining_size
= db_data
.dsize
- (remaining_ptr
- db_data
.dptr
);
535 memcpy(new_data_p
+ sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)),
541 db_data
.dptr
= new_data_p
;
543 /* Re-save smaller record. */
544 ld
= (struct locking_data
*)db_data
.dptr
;
545 ld
->u
.s
.num_share_mode_entries
= num_share_modes
;
547 db_data
.dsize
= sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)) + remaining_size
;
549 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {
557 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
561 const struct smb_share_mode_entry
*set_entry
,
562 const struct smb_share_mode_entry
*new_entry
)
565 struct locking_key lk
;
566 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
567 int num_share_modes
= 0;
568 struct locking_data
*ld
= NULL
; /* internal samba db state. */
569 struct share_mode_entry
*shares
= NULL
;
573 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
575 return -1; /* Error - missing entry ! */
578 ld
= (struct locking_data
*)db_data
.dptr
;
579 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
580 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
582 for (i
= 0; i
< num_share_modes
; i
++) {
583 struct share_mode_entry
*share
= &shares
[i
];
584 struct server_id pid
= share
->pid
;
586 /* Check this process really exists. */
587 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
588 continue; /* No longer exists. */
591 if (share_mode_entry_equal(set_entry
, share
)) {
592 create_share_mode_entry(share
, new_entry
, share
->name_hash
);
603 /* Save modified data. */
604 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {