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 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/>.
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. */
39 /* Remove the paranoid malloc checker. */
45 * Internal structure of locking.tdb share mode db.
46 * Used by locking.c and libsmbsharemodes.c
52 int num_share_mode_entries
;
53 struct timespec old_write_time
;
54 struct timespec changed_write_time
;
55 uint32 num_delete_token_entries
;
57 struct share_mode_entry dummy
; /* Needed for alignment. */
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[];
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
)
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
));
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
,
109 if (!smb_db
->smb_tdb
) {
114 /* Should check that this is the correct version.... */
118 /* key and data records in the tdb locking database */
125 int smb_share_mode_db_close(struct smbdb_ctx
*db_ctx
)
127 int ret
= tdb_close(db_ctx
->smb_tdb
);
132 static TDB_DATA
get_locking_key(struct locking_key
*lk
, uint64_t dev
,
133 uint64_t ino
, uint64_t extid
)
137 memset(lk
, '\0', sizeof(*lk
));
138 lk
->dev
= (SMB_DEV_T
)dev
;
139 lk
->inode
= (SMB_INO_T
)ino
;
141 ld
.dptr
= (uint8
*)lk
;
142 ld
.dsize
= sizeof(*lk
);
147 * lock/unlock entry in sharemode database.
150 int smb_lock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
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
,
165 struct locking_key lk
;
166 tdb_chainunlock(db_ctx
->smb_tdb
,
167 get_locking_key(&lk
, dev
, ino
, extid
));
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
,
197 memset(out
, '\0', sizeof(struct share_mode_entry
));
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();
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
,
222 struct smb_share_mode_entry
**pp_list
,
223 unsigned char *p_delete_on_close
)
225 struct locking_key lk
;
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
;
235 *p_delete_on_close
= 0;
237 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
,
238 get_locking_key(&lk
, dev
, ino
, extid
));
243 ld
= (struct locking_data
*)db_data
.dptr
;
244 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
246 if (!num_share_modes
) {
251 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
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
));
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
;
291 *p_delete_on_close
= ld
->u
.s
.num_delete_token_entries
!= 0;
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
);
305 fullpath
= (char *)malloc(sharepath_size
+ filename_size
+ 2);
306 if (fullpath
== NULL
) {
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);
320 * Create an entry in the Samba share mode db.
323 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
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. */
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;
340 uint32_t name_hash
= smb_name_hash(sharepath
, filename
, &err
);
346 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
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);
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
),
366 strlen(sharepath
) + 1);
367 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
368 strlen(sharepath
) + 1,
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) {
383 /* Entry exists, we must add a new entry. */
384 new_data_p
= (uint8
*)malloc(
385 db_data
.dsize
+ sizeof(struct share_mode_entry
));
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
);
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) {
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
,
436 const struct smb_share_mode_entry
*new_entry
,
437 const char *filename
) /* Must be absolute utf8 path. */
439 if (*filename
!= '/') {
442 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, extid
, new_entry
,
446 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
450 const struct smb_share_mode_entry
*del_entry
)
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
);
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 ! */
479 /* It's ours - just remove the entire record. */
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
));
492 /* Copy the header. */
493 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
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
) );
516 if (num_share_modes
== 0) {
517 /* None left after pruning. Delete record. */
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
)),
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) {
549 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
553 const struct smb_share_mode_entry
*set_entry
,
554 const struct smb_share_mode_entry
*new_entry
)
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
;
565 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
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
);
595 /* Save modified data. */
596 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {