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"
31 /* Database context handle. */
36 /* Remove the paranoid malloc checker. */
41 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
, uint64_t dev
,
42 uint64_t ino
, uint64_t extid
,
43 const struct smb_share_mode_entry
*new_entry
,
44 const char *sharepath
, const char *filename
);
46 static bool sharemodes_procid_equal(const struct server_id
*p1
, const struct server_id
*p2
)
48 return (p1
->pid
== p2
->pid
);
51 static pid_t
sharemodes_procid_to_pid(const struct server_id
*proc
)
57 * open/close sharemode database.
60 struct smbdb_ctx
*smb_share_mode_db_open(const char *db_path
)
62 struct smbdb_ctx
*smb_db
= (struct smbdb_ctx
*)malloc(sizeof(struct smbdb_ctx
));
68 memset(smb_db
, '\0', sizeof(struct smbdb_ctx
));
70 smb_db
->smb_tdb
= tdb_open(db_path
,
71 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
75 if (!smb_db
->smb_tdb
) {
80 /* Should check that this is the correct version.... */
84 /* key and data records in the tdb locking database */
91 int smb_share_mode_db_close(struct smbdb_ctx
*db_ctx
)
93 int ret
= tdb_close(db_ctx
->smb_tdb
);
98 static TDB_DATA
get_locking_key(struct locking_key
*lk
, uint64_t dev
,
99 uint64_t ino
, uint64_t extid
)
103 memset(lk
, '\0', sizeof(*lk
));
104 lk
->dev
= (SMB_DEV_T
)dev
;
105 lk
->inode
= (SMB_INO_T
)ino
;
107 ld
.dptr
= (uint8
*)lk
;
108 ld
.dsize
= sizeof(*lk
);
113 * lock/unlock entry in sharemode database.
116 int smb_lock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
121 struct locking_key lk
;
122 return tdb_chainlock(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
,
126 int smb_unlock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
131 struct locking_key lk
;
132 return tdb_chainunlock(db_ctx
->smb_tdb
,
133 get_locking_key(&lk
, dev
, ino
, extid
));
137 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
140 static int share_mode_entry_equal(const struct smb_share_mode_entry
*e_entry
,
141 const struct share_mode_entry
*entry
)
143 return (sharemodes_procid_equal(&e_entry
->pid
, &entry
->pid
) &&
144 e_entry
->file_id
== (uint32_t)entry
->share_file_id
&&
145 e_entry
->open_time
.tv_sec
== entry
->time
.tv_sec
&&
146 e_entry
->open_time
.tv_usec
== entry
->time
.tv_usec
&&
147 e_entry
->share_access
== (uint32_t)entry
->share_access
&&
148 e_entry
->access_mask
== (uint32_t)entry
->access_mask
&&
149 e_entry
->dev
== entry
->id
.devid
&&
150 e_entry
->ino
== entry
->id
.inode
&&
151 e_entry
->extid
== entry
->id
.extid
);
155 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
158 static void create_share_mode_entry(struct share_mode_entry
*out
,
159 const struct smb_share_mode_entry
*in
,
162 memset(out
, '\0', sizeof(struct share_mode_entry
));
165 out
->share_file_id
= (unsigned long)in
->file_id
;
166 out
->time
.tv_sec
= in
->open_time
.tv_sec
;
167 out
->time
.tv_usec
= in
->open_time
.tv_usec
;
168 out
->share_access
= in
->share_access
;
169 out
->access_mask
= in
->access_mask
;
170 out
->id
.devid
= in
->dev
;
171 out
->id
.inode
= in
->ino
;
172 out
->id
.extid
= in
->extid
;
173 out
->uid
= (uint32
)geteuid();
175 out
->name_hash
= name_hash
;
179 * Return the current share mode list for an open file.
180 * This uses similar (but simplified) logic to locking/locking.c
183 int smb_get_share_mode_entries(struct smbdb_ctx
*db_ctx
,
187 struct smb_share_mode_entry
**pp_list
,
188 unsigned char *p_delete_on_close
)
190 struct locking_key lk
;
192 struct smb_share_mode_entry
*list
= NULL
;
193 int num_share_modes
= 0;
194 struct locking_data
*ld
= NULL
; /* internal samba db state. */
195 struct share_mode_entry
*shares
= NULL
;
200 *p_delete_on_close
= 0;
202 db_data
= tdb_fetch(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
,
208 ld
= (struct locking_data
*)db_data
.dptr
;
209 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
211 if (!num_share_modes
) {
216 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
222 memset(list
, '\0', num_share_modes
* sizeof(struct smb_share_mode_entry
));
224 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
227 for (i
= 0; i
< num_share_modes
; i
++) {
228 struct share_mode_entry
*share
= &shares
[i
];
229 struct smb_share_mode_entry
*sme
= &list
[list_num
];
230 struct server_id pid
= share
->pid
;
232 /* Check this process really exists. */
233 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
234 continue; /* No longer exists. */
237 /* Ignore deferred open entries. */
238 if (share
->op_type
== DEFERRED_OPEN_ENTRY
) {
242 /* Copy into the external list. */
243 sme
->dev
= share
->id
.devid
;
244 sme
->ino
= share
->id
.inode
;
245 sme
->extid
= share
->id
.extid
;
246 sme
->share_access
= (uint32_t)share
->share_access
;
247 sme
->access_mask
= (uint32_t)share
->access_mask
;
248 sme
->open_time
.tv_sec
= share
->time
.tv_sec
;
249 sme
->open_time
.tv_usec
= share
->time
.tv_usec
;
250 sme
->file_id
= (uint32_t)share
->share_file_id
;
251 sme
->pid
= share
->pid
;
261 *p_delete_on_close
= ld
->u
.s
.num_delete_token_entries
!= 0;
267 static uint32_t smb_name_hash(const char *sharepath
, const char *filename
, int *err
)
270 char *fullpath
= NULL
;
271 size_t sharepath_size
= strlen(sharepath
);
272 size_t filename_size
= strlen(filename
);
276 fullpath
= (char *)malloc(sharepath_size
+ filename_size
+ 2);
277 if (fullpath
== NULL
) {
281 memcpy(fullpath
, sharepath
, sharepath_size
);
282 fullpath
[sharepath_size
] = '/';
283 memcpy(&fullpath
[sharepath_size
+ 1], filename
, filename_size
+ 1);
285 key
.dptr
= (uint8_t *)fullpath
;
286 key
.dsize
= strlen(fullpath
) + 1;
287 name_hash
= tdb_jenkins_hash(&key
);
293 * Create an entry in the Samba share mode db.
296 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
300 const struct smb_share_mode_entry
*new_entry
,
301 const char *sharepath
, /* Must be absolute utf8 path. */
302 const char *filename
) /* Must be relative utf8 path. */
305 struct locking_key lk
;
306 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
307 int orig_num_share_modes
= 0;
308 struct locking_data
*ld
= NULL
; /* internal samba db state. */
309 struct share_mode_entry
*shares
= NULL
;
310 uint8
*new_data_p
= NULL
;
311 size_t new_data_size
= 0;
313 uint32_t name_hash
= smb_name_hash(sharepath
, filename
, &err
);
319 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
321 /* We must create the entry. */
322 db_data
.dptr
= (uint8
*)malloc(
323 sizeof(struct locking_data
) +
324 sizeof(struct share_mode_entry
) +
325 strlen(sharepath
) + 1 +
326 strlen(filename
) + 1);
330 ld
= (struct locking_data
*)db_data
.dptr
;
331 memset(ld
, '\0', sizeof(struct locking_data
));
332 ld
->u
.s
.num_share_mode_entries
= 1;
333 ld
->u
.s
.num_delete_token_entries
= 0;
334 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
335 create_share_mode_entry(shares
, new_entry
, name_hash
);
337 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
),
339 strlen(sharepath
) + 1);
340 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
341 strlen(sharepath
) + 1,
343 strlen(filename
) + 1);
345 db_data
.dsize
= sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
346 strlen(sharepath
) + 1 +
347 strlen(filename
) + 1;
348 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_INSERT
) == -1) {
356 /* Entry exists, we must add a new entry. */
357 new_data_p
= (uint8
*)malloc(
358 db_data
.dsize
+ sizeof(struct share_mode_entry
));
364 ld
= (struct locking_data
*)db_data
.dptr
;
365 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
367 /* Copy the original data. */
368 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
370 /* Add in the new share mode */
371 shares
= (struct share_mode_entry
*)(new_data_p
+ sizeof(struct locking_data
) +
372 (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
374 create_share_mode_entry(shares
, new_entry
, name_hash
);
376 ld
= (struct locking_data
*)new_data_p
;
377 ld
->u
.s
.num_share_mode_entries
++;
379 /* Append the original delete_tokens and filenames. */
380 memcpy(new_data_p
+ sizeof(struct locking_data
) + (ld
->u
.s
.num_share_mode_entries
* sizeof(struct share_mode_entry
)),
381 db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)),
382 db_data
.dsize
- sizeof(struct locking_data
) - (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
384 new_data_size
= db_data
.dsize
+ sizeof(struct share_mode_entry
);
388 db_data
.dptr
= new_data_p
;
389 db_data
.dsize
= new_data_size
;
391 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
400 * Create an entry in the Samba share mode db. Original interface - doesn't
401 * Distinguish between share path and filename. Fudge this by using a
402 * sharepath of / and a relative filename of (filename+1).
405 int smb_create_share_mode_entry(struct smbdb_ctx
*db_ctx
,
409 const struct smb_share_mode_entry
*new_entry
,
410 const char *filename
) /* Must be absolute utf8 path. */
412 if (*filename
!= '/') {
415 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, extid
, new_entry
,
419 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
423 const struct smb_share_mode_entry
*del_entry
)
426 struct locking_key lk
;
427 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
428 int orig_num_share_modes
= 0;
429 struct locking_data
*ld
= NULL
; /* internal samba db state. */
430 struct share_mode_entry
*shares
= NULL
;
431 uint8
*new_data_p
= NULL
;
432 size_t remaining_size
= 0;
433 size_t i
, num_share_modes
;
434 const uint8
*remaining_ptr
= NULL
;
436 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
438 return -1; /* Error - missing entry ! */
441 ld
= (struct locking_data
*)db_data
.dptr
;
442 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
443 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
445 if (orig_num_share_modes
== 1) {
446 /* Only one entry - better be ours... */
447 if (!share_mode_entry_equal(del_entry
, shares
)) {
448 /* Error ! We can't delete someone else's entry ! */
452 /* It's ours - just remove the entire record. */
454 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
457 /* More than one - allocate a new record minus the one we'll delete. */
458 new_data_p
= (uint8
*)malloc(
459 db_data
.dsize
- sizeof(struct share_mode_entry
));
465 /* Copy the header. */
466 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
469 for (i
= 0; i
< orig_num_share_modes
; i
++) {
470 struct share_mode_entry
*share
= &shares
[i
];
471 struct server_id pid
= share
->pid
;
473 /* Check this process really exists. */
474 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
475 continue; /* No longer exists. */
478 if (share_mode_entry_equal(del_entry
, share
)) {
479 continue; /* This is our delete taget. */
482 memcpy(new_data_p
+ sizeof(struct locking_data
) +
483 (num_share_modes
* sizeof(struct share_mode_entry
)),
484 share
, sizeof(struct share_mode_entry
) );
489 if (num_share_modes
== 0) {
490 /* None left after pruning. Delete record. */
493 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
496 /* Copy any delete tokens plus the terminating filenames. */
497 remaining_ptr
= db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
));
498 remaining_size
= db_data
.dsize
- (remaining_ptr
- db_data
.dptr
);
500 memcpy(new_data_p
+ sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)),
506 db_data
.dptr
= new_data_p
;
508 /* Re-save smaller record. */
509 ld
= (struct locking_data
*)db_data
.dptr
;
510 ld
->u
.s
.num_share_mode_entries
= num_share_modes
;
512 db_data
.dsize
= sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)) + remaining_size
;
514 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
522 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
526 const struct smb_share_mode_entry
*set_entry
,
527 const struct smb_share_mode_entry
*new_entry
)
530 struct locking_key lk
;
531 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
, extid
);
532 int num_share_modes
= 0;
533 struct locking_data
*ld
= NULL
; /* internal samba db state. */
534 struct share_mode_entry
*shares
= NULL
;
538 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
540 return -1; /* Error - missing entry ! */
543 ld
= (struct locking_data
*)db_data
.dptr
;
544 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
545 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
547 for (i
= 0; i
< num_share_modes
; i
++) {
548 struct share_mode_entry
*share
= &shares
[i
];
549 struct server_id pid
= share
->pid
;
551 /* Check this process really exists. */
552 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
553 continue; /* No longer exists. */
556 if (share_mode_entry_equal(set_entry
, share
)) {
557 create_share_mode_entry(share
, new_entry
, share
->name_hash
);
568 /* Save modified data. */
569 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {