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 "smb_share_modes.h"
30 /* Database context handle. */
35 /* Remove the paranoid malloc checker. */
40 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
, uint64_t dev
,
41 uint64_t ino
, const struct smb_share_mode_entry
*new_entry
,
42 const char *sharepath
, const char *filename
);
44 static bool sharemodes_procid_equal(const struct server_id
*p1
, const struct server_id
*p2
)
46 return (p1
->pid
== p2
->pid
);
49 static pid_t
sharemodes_procid_to_pid(const struct server_id
*proc
)
55 * open/close sharemode database.
58 struct smbdb_ctx
*smb_share_mode_db_open(const char *db_path
)
60 struct smbdb_ctx
*smb_db
= (struct smbdb_ctx
*)malloc(sizeof(struct smbdb_ctx
));
66 memset(smb_db
, '\0', sizeof(struct smbdb_ctx
));
68 smb_db
->smb_tdb
= tdb_open(db_path
,
69 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
,
73 if (!smb_db
->smb_tdb
) {
78 /* Should check that this is the correct version.... */
82 /* key and data records in the tdb locking database */
88 int smb_share_mode_db_close(struct smbdb_ctx
*db_ctx
)
90 int ret
= tdb_close(db_ctx
->smb_tdb
);
95 static TDB_DATA
get_locking_key(struct locking_key
*lk
, uint64_t dev
,
100 memset(lk
, '\0', sizeof(*lk
));
101 lk
->dev
= (SMB_DEV_T
)dev
;
102 lk
->inode
= (SMB_INO_T
)ino
;
103 ld
.dptr
= (uint8
*)lk
;
104 ld
.dsize
= sizeof(*lk
);
109 * lock/unlock entry in sharemode database.
112 int smb_lock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
116 struct locking_key lk
;
117 return tdb_chainlock(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
));
120 int smb_unlock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
124 struct locking_key lk
;
125 return tdb_chainunlock(db_ctx
->smb_tdb
,
126 get_locking_key(&lk
, dev
, ino
));
130 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
133 static int share_mode_entry_equal(const struct smb_share_mode_entry
*e_entry
,
134 const struct share_mode_entry
*entry
)
136 return (sharemodes_procid_equal(&e_entry
->pid
, &entry
->pid
) &&
137 e_entry
->file_id
== (uint32_t)entry
->share_file_id
&&
138 e_entry
->open_time
.tv_sec
== entry
->time
.tv_sec
&&
139 e_entry
->open_time
.tv_usec
== entry
->time
.tv_usec
&&
140 e_entry
->share_access
== (uint32_t)entry
->share_access
&&
141 e_entry
->access_mask
== (uint32_t)entry
->access_mask
&&
142 e_entry
->dev
== entry
->id
.devid
&&
143 e_entry
->ino
== entry
->id
.inode
);
147 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
150 static void create_share_mode_entry(struct share_mode_entry
*out
,
151 const struct smb_share_mode_entry
*in
)
153 memset(out
, '\0', sizeof(struct share_mode_entry
));
156 out
->share_file_id
= (unsigned long)in
->file_id
;
157 out
->time
.tv_sec
= in
->open_time
.tv_sec
;
158 out
->time
.tv_usec
= in
->open_time
.tv_usec
;
159 out
->share_access
= in
->share_access
;
160 out
->access_mask
= in
->access_mask
;
161 out
->id
.devid
= in
->dev
;
162 out
->id
.inode
= in
->ino
;
163 out
->uid
= (uint32
)geteuid();
168 * Return the current share mode list for an open file.
169 * This uses similar (but simplified) logic to locking/locking.c
172 int smb_get_share_mode_entries(struct smbdb_ctx
*db_ctx
,
175 struct smb_share_mode_entry
**pp_list
,
176 unsigned char *p_delete_on_close
)
178 struct locking_key lk
;
180 struct smb_share_mode_entry
*list
= NULL
;
181 int num_share_modes
= 0;
182 struct locking_data
*ld
= NULL
; /* internal samba db state. */
183 struct share_mode_entry
*shares
= NULL
;
188 *p_delete_on_close
= 0;
190 db_data
= tdb_fetch(db_ctx
->smb_tdb
, get_locking_key(&lk
, dev
, ino
));
195 ld
= (struct locking_data
*)db_data
.dptr
;
196 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
198 if (!num_share_modes
) {
203 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
209 memset(list
, '\0', num_share_modes
* sizeof(struct smb_share_mode_entry
));
211 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
214 for (i
= 0; i
< num_share_modes
; i
++) {
215 struct share_mode_entry
*share
= &shares
[i
];
216 struct smb_share_mode_entry
*sme
= &list
[list_num
];
217 struct server_id pid
= share
->pid
;
219 /* Check this process really exists. */
220 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
221 continue; /* No longer exists. */
224 /* Ignore deferred open entries. */
225 if (share
->op_type
== DEFERRED_OPEN_ENTRY
) {
229 /* Copy into the external list. */
230 sme
->dev
= share
->id
.devid
;
231 sme
->ino
= share
->id
.inode
;
232 sme
->share_access
= (uint32_t)share
->share_access
;
233 sme
->access_mask
= (uint32_t)share
->access_mask
;
234 sme
->open_time
.tv_sec
= share
->time
.tv_sec
;
235 sme
->open_time
.tv_usec
= share
->time
.tv_usec
;
236 sme
->file_id
= (uint32_t)share
->share_file_id
;
237 sme
->pid
= share
->pid
;
247 *p_delete_on_close
= ld
->u
.s
.delete_on_close
;
254 * Create an entry in the Samba share mode db.
257 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
260 const struct smb_share_mode_entry
*new_entry
,
261 const char *sharepath
, /* Must be absolute utf8 path. */
262 const char *filename
) /* Must be relative utf8 path. */
265 struct locking_key lk
;
266 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
);
267 int orig_num_share_modes
= 0;
268 struct locking_data
*ld
= NULL
; /* internal samba db state. */
269 struct share_mode_entry
*shares
= NULL
;
270 uint8
*new_data_p
= NULL
;
271 size_t new_data_size
= 0;
273 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
275 /* We must create the entry. */
276 db_data
.dptr
= (uint8
*)malloc(
277 sizeof(struct locking_data
) +
278 sizeof(struct share_mode_entry
) +
279 strlen(sharepath
) + 1 +
280 strlen(filename
) + 1);
284 ld
= (struct locking_data
*)db_data
.dptr
;
285 memset(ld
, '\0', sizeof(struct locking_data
));
286 ld
->u
.s
.num_share_mode_entries
= 1;
287 ld
->u
.s
.delete_on_close
= 0;
288 ld
->u
.s
.delete_token_size
= 0;
289 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
290 create_share_mode_entry(shares
, new_entry
);
292 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
),
294 strlen(sharepath
) + 1);
295 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
296 strlen(sharepath
) + 1,
298 strlen(filename
) + 1);
300 db_data
.dsize
= sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
301 strlen(sharepath
) + 1 +
302 strlen(filename
) + 1;
303 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_INSERT
) == -1) {
311 /* Entry exists, we must add a new entry. */
312 new_data_p
= (uint8
*)malloc(
313 db_data
.dsize
+ sizeof(struct share_mode_entry
));
319 ld
= (struct locking_data
*)db_data
.dptr
;
320 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
322 /* Copy the original data. */
323 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
325 /* Add in the new share mode */
326 shares
= (struct share_mode_entry
*)(new_data_p
+ sizeof(struct locking_data
) +
327 (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
329 create_share_mode_entry(shares
, new_entry
);
331 ld
= (struct locking_data
*)new_data_p
;
332 ld
->u
.s
.num_share_mode_entries
++;
334 /* Append the original delete_token and filenames. */
335 memcpy(new_data_p
+ sizeof(struct locking_data
) + (ld
->u
.s
.num_share_mode_entries
* sizeof(struct share_mode_entry
)),
336 db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)),
337 db_data
.dsize
- sizeof(struct locking_data
) - (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
339 new_data_size
= db_data
.dsize
+ sizeof(struct share_mode_entry
);
343 db_data
.dptr
= new_data_p
;
344 db_data
.dsize
= new_data_size
;
346 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
355 * Create an entry in the Samba share mode db. Original interface - doesn't
356 * Distinguish between share path and filename. Fudge this by using a
357 * sharepath of / and a relative filename of (filename+1).
360 int smb_create_share_mode_entry(struct smbdb_ctx
*db_ctx
,
363 const struct smb_share_mode_entry
*new_entry
,
364 const char *filename
) /* Must be absolute utf8 path. */
366 if (*filename
!= '/') {
369 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, new_entry
,
373 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
376 const struct smb_share_mode_entry
*del_entry
)
379 struct locking_key lk
;
380 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
);
381 int orig_num_share_modes
= 0;
382 struct locking_data
*ld
= NULL
; /* internal samba db state. */
383 struct share_mode_entry
*shares
= NULL
;
384 uint8
*new_data_p
= NULL
;
385 size_t remaining_size
= 0;
386 size_t i
, num_share_modes
;
387 const uint8
*remaining_ptr
= NULL
;
389 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
391 return -1; /* Error - missing entry ! */
394 ld
= (struct locking_data
*)db_data
.dptr
;
395 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
396 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
398 if (orig_num_share_modes
== 1) {
399 /* Only one entry - better be ours... */
400 if (!share_mode_entry_equal(del_entry
, shares
)) {
401 /* Error ! We can't delete someone else's entry ! */
405 /* It's ours - just remove the entire record. */
407 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
410 /* More than one - allocate a new record minus the one we'll delete. */
411 new_data_p
= (uint8
*)malloc(
412 db_data
.dsize
- sizeof(struct share_mode_entry
));
418 /* Copy the header. */
419 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
422 for (i
= 0; i
< orig_num_share_modes
; i
++) {
423 struct share_mode_entry
*share
= &shares
[i
];
424 struct server_id pid
= share
->pid
;
426 /* Check this process really exists. */
427 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
428 continue; /* No longer exists. */
431 if (share_mode_entry_equal(del_entry
, share
)) {
432 continue; /* This is our delete taget. */
435 memcpy(new_data_p
+ sizeof(struct locking_data
) +
436 (num_share_modes
* sizeof(struct share_mode_entry
)),
437 share
, sizeof(struct share_mode_entry
) );
442 if (num_share_modes
== 0) {
443 /* None left after pruning. Delete record. */
446 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
449 /* Copy any delete token plus the terminating filenames. */
450 remaining_ptr
= db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
));
451 remaining_size
= db_data
.dsize
- (remaining_ptr
- db_data
.dptr
);
453 memcpy(new_data_p
+ sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)),
459 db_data
.dptr
= new_data_p
;
461 /* Re-save smaller record. */
462 ld
= (struct locking_data
*)db_data
.dptr
;
463 ld
->u
.s
.num_share_mode_entries
= num_share_modes
;
465 db_data
.dsize
= sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)) + remaining_size
;
467 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
475 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
478 const struct smb_share_mode_entry
*set_entry
,
479 const struct smb_share_mode_entry
*new_entry
)
482 struct locking_key lk
;
483 TDB_DATA locking_key
= get_locking_key(&lk
, dev
, ino
);
484 int num_share_modes
= 0;
485 struct locking_data
*ld
= NULL
; /* internal samba db state. */
486 struct share_mode_entry
*shares
= NULL
;
490 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
492 return -1; /* Error - missing entry ! */
495 ld
= (struct locking_data
*)db_data
.dptr
;
496 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
497 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
499 for (i
= 0; i
< num_share_modes
; i
++) {
500 struct share_mode_entry
*share
= &shares
[i
];
501 struct server_id pid
= share
->pid
;
503 /* Check this process really exists. */
504 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
505 continue; /* No longer exists. */
508 if (share_mode_entry_equal(set_entry
, share
)) {
509 create_share_mode_entry(share
, new_entry
);
520 /* Save modified data. */
521 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {