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/>.
27 #define UID_WRAPPER_NOT_REPLACE
29 #include "system/filesys.h"
30 #include "smb_share_modes.h"
31 #include "tdb_compat.h"
32 #include <ccan/hash/hash.h>
34 /* Database context handle. */
39 /* Remove the paranoid malloc checker. */
44 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
, uint64_t dev
,
45 uint64_t ino
, uint64_t extid
,
46 const struct smb_share_mode_entry
*new_entry
,
47 const char *sharepath
, const char *filename
);
49 static bool sharemodes_procid_equal(const struct server_id
*p1
, const struct server_id
*p2
)
51 return (p1
->pid
== p2
->pid
);
54 static pid_t
sharemodes_procid_to_pid(const struct server_id
*proc
)
60 * open/close sharemode database.
63 struct smbdb_ctx
*smb_share_mode_db_open(const char *db_path
)
65 struct smbdb_ctx
*smb_db
= (struct smbdb_ctx
*)malloc(sizeof(struct smbdb_ctx
));
71 memset(smb_db
, '\0', sizeof(struct smbdb_ctx
));
73 /* FIXME: We should *never* open a tdb without logging! */
74 smb_db
->smb_tdb
= tdb_open_compat(db_path
,
75 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
80 if (!smb_db
->smb_tdb
) {
85 /* Should check that this is the correct version.... */
89 /* key and data records in the tdb locking database */
96 int smb_share_mode_db_close(struct smbdb_ctx
*db_ctx
)
98 int ret
= tdb_close(db_ctx
->smb_tdb
);
103 static TDB_DATA
get_locking_key(struct locking_key
*lk
, uint64_t dev
,
104 uint64_t ino
, uint64_t extid
)
108 memset(lk
, '\0', sizeof(*lk
));
109 lk
->dev
= (SMB_DEV_T
)dev
;
110 lk
->inode
= (SMB_INO_T
)ino
;
112 ld
.dptr
= (uint8
*)lk
;
113 ld
.dsize
= sizeof(*lk
);
118 * lock/unlock entry in sharemode database.
121 int smb_lock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
126 struct locking_key lk
;
127 return tdb_chainlock(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
,
128 extid
)) == 0 ? 0 : -1;
131 int smb_unlock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
136 struct locking_key lk
;
137 tdb_chainunlock(db_ctx
->smb_tdb
,
138 get_locking_key(&lk
, dev
, ino
, extid
));
143 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
146 static int share_mode_entry_equal(const struct smb_share_mode_entry
*e_entry
,
147 const struct share_mode_entry
*entry
)
149 return (sharemodes_procid_equal(&e_entry
->pid
, &entry
->pid
) &&
150 e_entry
->file_id
== (uint32_t)entry
->share_file_id
&&
151 e_entry
->open_time
.tv_sec
== entry
->time
.tv_sec
&&
152 e_entry
->open_time
.tv_usec
== entry
->time
.tv_usec
&&
153 e_entry
->share_access
== (uint32_t)entry
->share_access
&&
154 e_entry
->access_mask
== (uint32_t)entry
->access_mask
&&
155 e_entry
->dev
== entry
->id
.devid
&&
156 e_entry
->ino
== entry
->id
.inode
&&
157 e_entry
->extid
== entry
->id
.extid
);
161 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
164 static void create_share_mode_entry(struct share_mode_entry
*out
,
165 const struct smb_share_mode_entry
*in
,
168 memset(out
, '\0', sizeof(struct share_mode_entry
));
171 out
->share_file_id
= (unsigned long)in
->file_id
;
172 out
->time
.tv_sec
= in
->open_time
.tv_sec
;
173 out
->time
.tv_usec
= in
->open_time
.tv_usec
;
174 out
->share_access
= in
->share_access
;
175 out
->access_mask
= in
->access_mask
;
176 out
->id
.devid
= in
->dev
;
177 out
->id
.inode
= in
->ino
;
178 out
->id
.extid
= in
->extid
;
179 out
->uid
= (uint32
)geteuid();
181 out
->name_hash
= name_hash
;
185 * Return the current share mode list for an open file.
186 * This uses similar (but simplified) logic to locking/locking.c
189 int smb_get_share_mode_entries(struct smbdb_ctx
*db_ctx
,
193 struct smb_share_mode_entry
**pp_list
,
194 unsigned char *p_delete_on_close
)
196 struct locking_key lk
;
198 struct smb_share_mode_entry
*list
= NULL
;
199 int num_share_modes
= 0;
200 struct locking_data
*ld
= NULL
; /* internal samba db state. */
201 struct share_mode_entry
*shares
= NULL
;
206 *p_delete_on_close
= 0;
208 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
,
209 get_locking_key(&lk
, dev
, ino
, extid
));
214 ld
= (struct locking_data
*)db_data
.dptr
;
215 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
217 if (!num_share_modes
) {
222 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
228 memset(list
, '\0', num_share_modes
* sizeof(struct smb_share_mode_entry
));
230 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
233 for (i
= 0; i
< num_share_modes
; i
++) {
234 struct share_mode_entry
*share
= &shares
[i
];
235 struct smb_share_mode_entry
*sme
= &list
[list_num
];
236 struct server_id pid
= share
->pid
;
238 /* Check this process really exists. */
239 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
240 continue; /* No longer exists. */
243 /* Ignore deferred open entries. */
244 if (share
->op_type
== DEFERRED_OPEN_ENTRY
) {
248 /* Copy into the external list. */
249 sme
->dev
= share
->id
.devid
;
250 sme
->ino
= share
->id
.inode
;
251 sme
->extid
= share
->id
.extid
;
252 sme
->share_access
= (uint32_t)share
->share_access
;
253 sme
->access_mask
= (uint32_t)share
->access_mask
;
254 sme
->open_time
.tv_sec
= share
->time
.tv_sec
;
255 sme
->open_time
.tv_usec
= share
->time
.tv_usec
;
256 sme
->file_id
= (uint32_t)share
->share_file_id
;
257 sme
->pid
= share
->pid
;
267 *p_delete_on_close
= ld
->u
.s
.num_delete_token_entries
!= 0;
273 static uint32_t smb_name_hash(const char *sharepath
, const char *filename
, int *err
)
275 char *fullpath
= NULL
;
276 size_t sharepath_size
= strlen(sharepath
);
277 size_t filename_size
= strlen(filename
);
281 fullpath
= (char *)malloc(sharepath_size
+ filename_size
+ 2);
282 if (fullpath
== NULL
) {
286 memcpy(fullpath
, sharepath
, sharepath_size
);
287 fullpath
[sharepath_size
] = '/';
288 memcpy(&fullpath
[sharepath_size
+ 1], filename
, filename_size
+ 1);
290 name_hash
= hash(fullpath
, strlen(fullpath
) + 1, 0);
296 * Create an entry in the Samba share mode db.
299 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
303 const struct smb_share_mode_entry
*new_entry
,
304 const char *sharepath
, /* Must be absolute utf8 path. */
305 const char *filename
) /* Must be relative utf8 path. */
308 struct locking_key lk
;
309 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
310 int orig_num_share_modes
= 0;
311 struct locking_data
*ld
= NULL
; /* internal samba db state. */
312 struct share_mode_entry
*shares
= NULL
;
313 uint8
*new_data_p
= NULL
;
314 size_t new_data_size
= 0;
316 uint32_t name_hash
= smb_name_hash(sharepath
, filename
, &err
);
322 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
324 /* We must create the entry. */
325 db_data
.dptr
= (uint8
*)malloc(
326 sizeof(struct locking_data
) +
327 sizeof(struct share_mode_entry
) +
328 strlen(sharepath
) + 1 +
329 strlen(filename
) + 1);
333 ld
= (struct locking_data
*)db_data
.dptr
;
334 memset(ld
, '\0', sizeof(struct locking_data
));
335 ld
->u
.s
.num_share_mode_entries
= 1;
336 ld
->u
.s
.num_delete_token_entries
= 0;
337 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
338 create_share_mode_entry(shares
, new_entry
, name_hash
);
340 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
),
342 strlen(sharepath
) + 1);
343 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
344 strlen(sharepath
) + 1,
346 strlen(filename
) + 1);
348 db_data
.dsize
= sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
349 strlen(sharepath
) + 1 +
350 strlen(filename
) + 1;
351 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_INSERT
) != 0) {
359 /* Entry exists, we must add a new entry. */
360 new_data_p
= (uint8
*)malloc(
361 db_data
.dsize
+ sizeof(struct share_mode_entry
));
367 ld
= (struct locking_data
*)db_data
.dptr
;
368 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
370 /* Copy the original data. */
371 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
373 /* Add in the new share mode */
374 shares
= (struct share_mode_entry
*)(new_data_p
+ sizeof(struct locking_data
) +
375 (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
377 create_share_mode_entry(shares
, new_entry
, name_hash
);
379 ld
= (struct locking_data
*)new_data_p
;
380 ld
->u
.s
.num_share_mode_entries
++;
382 /* Append the original delete_tokens and filenames. */
383 memcpy(new_data_p
+ sizeof(struct locking_data
) + (ld
->u
.s
.num_share_mode_entries
* sizeof(struct share_mode_entry
)),
384 db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)),
385 db_data
.dsize
- sizeof(struct locking_data
) - (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
387 new_data_size
= db_data
.dsize
+ sizeof(struct share_mode_entry
);
391 db_data
.dptr
= new_data_p
;
392 db_data
.dsize
= new_data_size
;
394 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {
403 * Create an entry in the Samba share mode db. Original interface - doesn't
404 * Distinguish between share path and filename. Fudge this by using a
405 * sharepath of / and a relative filename of (filename+1).
408 int smb_create_share_mode_entry(struct smbdb_ctx
*db_ctx
,
412 const struct smb_share_mode_entry
*new_entry
,
413 const char *filename
) /* Must be absolute utf8 path. */
415 if (*filename
!= '/') {
418 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, extid
, new_entry
,
422 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
426 const struct smb_share_mode_entry
*del_entry
)
429 struct locking_key lk
;
430 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
431 int orig_num_share_modes
= 0;
432 struct locking_data
*ld
= NULL
; /* internal samba db state. */
433 struct share_mode_entry
*shares
= NULL
;
434 uint8
*new_data_p
= NULL
;
435 size_t remaining_size
= 0;
436 size_t i
, num_share_modes
;
437 const uint8
*remaining_ptr
= NULL
;
439 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
441 return -1; /* Error - missing entry ! */
444 ld
= (struct locking_data
*)db_data
.dptr
;
445 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
446 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
448 if (orig_num_share_modes
== 1) {
449 /* Only one entry - better be ours... */
450 if (!share_mode_entry_equal(del_entry
, shares
)) {
451 /* Error ! We can't delete someone else's entry ! */
455 /* It's ours - just remove the entire record. */
457 return tdb_delete(db_ctx
->smb_tdb
, locking_key
) ? -1 : 0;
460 /* More than one - allocate a new record minus the one we'll delete. */
461 new_data_p
= (uint8
*)malloc(
462 db_data
.dsize
- sizeof(struct share_mode_entry
));
468 /* Copy the header. */
469 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
472 for (i
= 0; i
< orig_num_share_modes
; i
++) {
473 struct share_mode_entry
*share
= &shares
[i
];
474 struct server_id pid
= share
->pid
;
476 /* Check this process really exists. */
477 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
478 continue; /* No longer exists. */
481 if (share_mode_entry_equal(del_entry
, share
)) {
482 continue; /* This is our delete taget. */
485 memcpy(new_data_p
+ sizeof(struct locking_data
) +
486 (num_share_modes
* sizeof(struct share_mode_entry
)),
487 share
, sizeof(struct share_mode_entry
) );
492 if (num_share_modes
== 0) {
493 /* None left after pruning. Delete record. */
496 return tdb_delete(db_ctx
->smb_tdb
, locking_key
) ? -1 : 0;
499 /* Copy any delete tokens plus the terminating filenames. */
500 remaining_ptr
= db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
));
501 remaining_size
= db_data
.dsize
- (remaining_ptr
- db_data
.dptr
);
503 memcpy(new_data_p
+ sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)),
509 db_data
.dptr
= new_data_p
;
511 /* Re-save smaller record. */
512 ld
= (struct locking_data
*)db_data
.dptr
;
513 ld
->u
.s
.num_share_mode_entries
= num_share_modes
;
515 db_data
.dsize
= sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)) + remaining_size
;
517 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {
525 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
529 const struct smb_share_mode_entry
*set_entry
,
530 const struct smb_share_mode_entry
*new_entry
)
533 struct locking_key lk
;
534 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
535 int num_share_modes
= 0;
536 struct locking_data
*ld
= NULL
; /* internal samba db state. */
537 struct share_mode_entry
*shares
= NULL
;
541 db_data
= tdb_fetch_compat(db_ctx
->smb_tdb
, locking_key
);
543 return -1; /* Error - missing entry ! */
546 ld
= (struct locking_data
*)db_data
.dptr
;
547 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
548 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
550 for (i
= 0; i
< num_share_modes
; i
++) {
551 struct share_mode_entry
*share
= &shares
[i
];
552 struct server_id pid
= share
->pid
;
554 /* Check this process really exists. */
555 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
556 continue; /* No longer exists. */
559 if (share_mode_entry_equal(set_entry
, share
)) {
560 create_share_mode_entry(share
, new_entry
, share
->name_hash
);
571 /* Save modified data. */
572 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) != 0) {