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"
32 /* Database context handle. */
37 /* Remove the paranoid malloc checker. */
42 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
, uint64_t dev
,
43 uint64_t ino
, uint64_t extid
,
44 const struct smb_share_mode_entry
*new_entry
,
45 const char *sharepath
, const char *filename
);
47 static bool sharemodes_procid_equal(const struct server_id
*p1
, const struct server_id
*p2
)
49 return (p1
->pid
== p2
->pid
);
52 static pid_t
sharemodes_procid_to_pid(const struct server_id
*proc
)
58 * open/close sharemode database.
61 struct smbdb_ctx
*smb_share_mode_db_open(const char *db_path
)
63 struct smbdb_ctx
*smb_db
= (struct smbdb_ctx
*)malloc(sizeof(struct smbdb_ctx
));
69 memset(smb_db
, '\0', sizeof(struct smbdb_ctx
));
71 smb_db
->smb_tdb
= tdb_open(db_path
,
72 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
76 if (!smb_db
->smb_tdb
) {
81 /* Should check that this is the correct version.... */
85 /* key and data records in the tdb locking database */
92 int smb_share_mode_db_close(struct smbdb_ctx
*db_ctx
)
94 int ret
= tdb_close(db_ctx
->smb_tdb
);
99 static TDB_DATA
get_locking_key(struct locking_key
*lk
, uint64_t dev
,
100 uint64_t ino
, uint64_t extid
)
104 memset(lk
, '\0', sizeof(*lk
));
105 lk
->dev
= (SMB_DEV_T
)dev
;
106 lk
->inode
= (SMB_INO_T
)ino
;
108 ld
.dptr
= (uint8
*)lk
;
109 ld
.dsize
= sizeof(*lk
);
114 * lock/unlock entry in sharemode database.
117 int smb_lock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
122 struct locking_key lk
;
123 return tdb_chainlock(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
,
127 int smb_unlock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
132 struct locking_key lk
;
133 return tdb_chainunlock(db_ctx
->smb_tdb
,
134 get_locking_key(&lk
, dev
, ino
, extid
));
138 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
141 static int share_mode_entry_equal(const struct smb_share_mode_entry
*e_entry
,
142 const struct share_mode_entry
*entry
)
144 return (sharemodes_procid_equal(&e_entry
->pid
, &entry
->pid
) &&
145 e_entry
->file_id
== (uint32_t)entry
->share_file_id
&&
146 e_entry
->open_time
.tv_sec
== entry
->time
.tv_sec
&&
147 e_entry
->open_time
.tv_usec
== entry
->time
.tv_usec
&&
148 e_entry
->share_access
== (uint32_t)entry
->share_access
&&
149 e_entry
->access_mask
== (uint32_t)entry
->access_mask
&&
150 e_entry
->dev
== entry
->id
.devid
&&
151 e_entry
->ino
== entry
->id
.inode
&&
152 e_entry
->extid
== entry
->id
.extid
);
156 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
159 static void create_share_mode_entry(struct share_mode_entry
*out
,
160 const struct smb_share_mode_entry
*in
,
163 memset(out
, '\0', sizeof(struct share_mode_entry
));
166 out
->share_file_id
= (unsigned long)in
->file_id
;
167 out
->time
.tv_sec
= in
->open_time
.tv_sec
;
168 out
->time
.tv_usec
= in
->open_time
.tv_usec
;
169 out
->share_access
= in
->share_access
;
170 out
->access_mask
= in
->access_mask
;
171 out
->id
.devid
= in
->dev
;
172 out
->id
.inode
= in
->ino
;
173 out
->id
.extid
= in
->extid
;
174 out
->uid
= (uint32
)geteuid();
176 out
->name_hash
= name_hash
;
180 * Return the current share mode list for an open file.
181 * This uses similar (but simplified) logic to locking/locking.c
184 int smb_get_share_mode_entries(struct smbdb_ctx
*db_ctx
,
188 struct smb_share_mode_entry
**pp_list
,
189 unsigned char *p_delete_on_close
)
191 struct locking_key lk
;
193 struct smb_share_mode_entry
*list
= NULL
;
194 int num_share_modes
= 0;
195 struct locking_data
*ld
= NULL
; /* internal samba db state. */
196 struct share_mode_entry
*shares
= NULL
;
201 *p_delete_on_close
= 0;
203 db_data
= tdb_fetch(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
,
209 ld
= (struct locking_data
*)db_data
.dptr
;
210 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
212 if (!num_share_modes
) {
217 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
223 memset(list
, '\0', num_share_modes
* sizeof(struct smb_share_mode_entry
));
225 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
228 for (i
= 0; i
< num_share_modes
; i
++) {
229 struct share_mode_entry
*share
= &shares
[i
];
230 struct smb_share_mode_entry
*sme
= &list
[list_num
];
231 struct server_id pid
= share
->pid
;
233 /* Check this process really exists. */
234 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
235 continue; /* No longer exists. */
238 /* Ignore deferred open entries. */
239 if (share
->op_type
== DEFERRED_OPEN_ENTRY
) {
243 /* Copy into the external list. */
244 sme
->dev
= share
->id
.devid
;
245 sme
->ino
= share
->id
.inode
;
246 sme
->extid
= share
->id
.extid
;
247 sme
->share_access
= (uint32_t)share
->share_access
;
248 sme
->access_mask
= (uint32_t)share
->access_mask
;
249 sme
->open_time
.tv_sec
= share
->time
.tv_sec
;
250 sme
->open_time
.tv_usec
= share
->time
.tv_usec
;
251 sme
->file_id
= (uint32_t)share
->share_file_id
;
252 sme
->pid
= share
->pid
;
262 *p_delete_on_close
= ld
->u
.s
.num_delete_token_entries
!= 0;
268 static uint32_t smb_name_hash(const char *sharepath
, const char *filename
, int *err
)
271 char *fullpath
= NULL
;
272 size_t sharepath_size
= strlen(sharepath
);
273 size_t filename_size
= strlen(filename
);
277 fullpath
= (char *)malloc(sharepath_size
+ filename_size
+ 2);
278 if (fullpath
== NULL
) {
282 memcpy(fullpath
, sharepath
, sharepath_size
);
283 fullpath
[sharepath_size
] = '/';
284 memcpy(&fullpath
[sharepath_size
+ 1], filename
, filename_size
+ 1);
286 key
.dptr
= (uint8_t *)fullpath
;
287 key
.dsize
= strlen(fullpath
) + 1;
288 name_hash
= tdb_jenkins_hash(&key
);
294 * Create an entry in the Samba share mode db.
297 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
301 const struct smb_share_mode_entry
*new_entry
,
302 const char *sharepath
, /* Must be absolute utf8 path. */
303 const char *filename
) /* Must be relative utf8 path. */
306 struct locking_key lk
;
307 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
308 int orig_num_share_modes
= 0;
309 struct locking_data
*ld
= NULL
; /* internal samba db state. */
310 struct share_mode_entry
*shares
= NULL
;
311 uint8
*new_data_p
= NULL
;
312 size_t new_data_size
= 0;
314 uint32_t name_hash
= smb_name_hash(sharepath
, filename
, &err
);
320 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
322 /* We must create the entry. */
323 db_data
.dptr
= (uint8
*)malloc(
324 sizeof(struct locking_data
) +
325 sizeof(struct share_mode_entry
) +
326 strlen(sharepath
) + 1 +
327 strlen(filename
) + 1);
331 ld
= (struct locking_data
*)db_data
.dptr
;
332 memset(ld
, '\0', sizeof(struct locking_data
));
333 ld
->u
.s
.num_share_mode_entries
= 1;
334 ld
->u
.s
.num_delete_token_entries
= 0;
335 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
336 create_share_mode_entry(shares
, new_entry
, name_hash
);
338 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
),
340 strlen(sharepath
) + 1);
341 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
342 strlen(sharepath
) + 1,
344 strlen(filename
) + 1);
346 db_data
.dsize
= sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
347 strlen(sharepath
) + 1 +
348 strlen(filename
) + 1;
349 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_INSERT
) == -1) {
357 /* Entry exists, we must add a new entry. */
358 new_data_p
= (uint8
*)malloc(
359 db_data
.dsize
+ sizeof(struct share_mode_entry
));
365 ld
= (struct locking_data
*)db_data
.dptr
;
366 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
368 /* Copy the original data. */
369 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
371 /* Add in the new share mode */
372 shares
= (struct share_mode_entry
*)(new_data_p
+ sizeof(struct locking_data
) +
373 (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
375 create_share_mode_entry(shares
, new_entry
, name_hash
);
377 ld
= (struct locking_data
*)new_data_p
;
378 ld
->u
.s
.num_share_mode_entries
++;
380 /* Append the original delete_tokens and filenames. */
381 memcpy(new_data_p
+ sizeof(struct locking_data
) + (ld
->u
.s
.num_share_mode_entries
* sizeof(struct share_mode_entry
)),
382 db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)),
383 db_data
.dsize
- sizeof(struct locking_data
) - (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
385 new_data_size
= db_data
.dsize
+ sizeof(struct share_mode_entry
);
389 db_data
.dptr
= new_data_p
;
390 db_data
.dsize
= new_data_size
;
392 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
401 * Create an entry in the Samba share mode db. Original interface - doesn't
402 * Distinguish between share path and filename. Fudge this by using a
403 * sharepath of / and a relative filename of (filename+1).
406 int smb_create_share_mode_entry(struct smbdb_ctx
*db_ctx
,
410 const struct smb_share_mode_entry
*new_entry
,
411 const char *filename
) /* Must be absolute utf8 path. */
413 if (*filename
!= '/') {
416 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, extid
, new_entry
,
420 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
424 const struct smb_share_mode_entry
*del_entry
)
427 struct locking_key lk
;
428 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
429 int orig_num_share_modes
= 0;
430 struct locking_data
*ld
= NULL
; /* internal samba db state. */
431 struct share_mode_entry
*shares
= NULL
;
432 uint8
*new_data_p
= NULL
;
433 size_t remaining_size
= 0;
434 size_t i
, num_share_modes
;
435 const uint8
*remaining_ptr
= NULL
;
437 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
439 return -1; /* Error - missing entry ! */
442 ld
= (struct locking_data
*)db_data
.dptr
;
443 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
444 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
446 if (orig_num_share_modes
== 1) {
447 /* Only one entry - better be ours... */
448 if (!share_mode_entry_equal(del_entry
, shares
)) {
449 /* Error ! We can't delete someone else's entry ! */
453 /* It's ours - just remove the entire record. */
455 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
458 /* More than one - allocate a new record minus the one we'll delete. */
459 new_data_p
= (uint8
*)malloc(
460 db_data
.dsize
- sizeof(struct share_mode_entry
));
466 /* Copy the header. */
467 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
470 for (i
= 0; i
< orig_num_share_modes
; i
++) {
471 struct share_mode_entry
*share
= &shares
[i
];
472 struct server_id pid
= share
->pid
;
474 /* Check this process really exists. */
475 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
476 continue; /* No longer exists. */
479 if (share_mode_entry_equal(del_entry
, share
)) {
480 continue; /* This is our delete taget. */
483 memcpy(new_data_p
+ sizeof(struct locking_data
) +
484 (num_share_modes
* sizeof(struct share_mode_entry
)),
485 share
, sizeof(struct share_mode_entry
) );
490 if (num_share_modes
== 0) {
491 /* None left after pruning. Delete record. */
494 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
497 /* Copy any delete tokens plus the terminating filenames. */
498 remaining_ptr
= db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
));
499 remaining_size
= db_data
.dsize
- (remaining_ptr
- db_data
.dptr
);
501 memcpy(new_data_p
+ sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)),
507 db_data
.dptr
= new_data_p
;
509 /* Re-save smaller record. */
510 ld
= (struct locking_data
*)db_data
.dptr
;
511 ld
->u
.s
.num_share_mode_entries
= num_share_modes
;
513 db_data
.dsize
= sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)) + remaining_size
;
515 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
523 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
527 const struct smb_share_mode_entry
*set_entry
,
528 const struct smb_share_mode_entry
*new_entry
)
531 struct locking_key lk
;
532 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
533 int num_share_modes
= 0;
534 struct locking_data
*ld
= NULL
; /* internal samba db state. */
535 struct share_mode_entry
*shares
= NULL
;
539 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
541 return -1; /* Error - missing entry ! */
544 ld
= (struct locking_data
*)db_data
.dptr
;
545 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
546 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
548 for (i
= 0; i
< num_share_modes
; i
++) {
549 struct share_mode_entry
*share
= &shares
[i
];
550 struct server_id pid
= share
->pid
;
552 /* Check this process really exists. */
553 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
554 continue; /* No longer exists. */
557 if (share_mode_entry_equal(set_entry
, share
)) {
558 create_share_mode_entry(share
, new_entry
, share
->name_hash
);
569 /* Save modified data. */
570 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {