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(uint64_t dev
, uint64_t ino
)
97 static struct locking_key lk
;
100 memset(&lk
, '\0', sizeof(struct locking_key
));
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 return tdb_chainlock(db_ctx
->smb_tdb
, get_locking_key(dev
, ino
));
119 int smb_unlock_share_mode_entry(struct smbdb_ctx
*db_ctx
,
123 return tdb_chainunlock(db_ctx
->smb_tdb
, get_locking_key(dev
, ino
));
127 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
130 static int share_mode_entry_equal(const struct smb_share_mode_entry
*e_entry
,
131 const struct share_mode_entry
*entry
)
133 return (sharemodes_procid_equal(&e_entry
->pid
, &entry
->pid
) &&
134 e_entry
->file_id
== (uint32_t)entry
->share_file_id
&&
135 e_entry
->open_time
.tv_sec
== entry
->time
.tv_sec
&&
136 e_entry
->open_time
.tv_usec
== entry
->time
.tv_usec
&&
137 e_entry
->share_access
== (uint32_t)entry
->share_access
&&
138 e_entry
->access_mask
== (uint32_t)entry
->access_mask
&&
139 e_entry
->dev
== entry
->id
.devid
&&
140 e_entry
->ino
== entry
->id
.inode
);
144 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
147 static void create_share_mode_entry(struct share_mode_entry
*out
,
148 const struct smb_share_mode_entry
*in
)
150 memset(out
, '\0', sizeof(struct share_mode_entry
));
153 out
->share_file_id
= (unsigned long)in
->file_id
;
154 out
->time
.tv_sec
= in
->open_time
.tv_sec
;
155 out
->time
.tv_usec
= in
->open_time
.tv_usec
;
156 out
->share_access
= in
->share_access
;
157 out
->access_mask
= in
->access_mask
;
158 out
->id
.devid
= in
->dev
;
159 out
->id
.inode
= in
->ino
;
160 out
->uid
= (uint32
)geteuid();
165 * Return the current share mode list for an open file.
166 * This uses similar (but simplified) logic to locking/locking.c
169 int smb_get_share_mode_entries(struct smbdb_ctx
*db_ctx
,
172 struct smb_share_mode_entry
**pp_list
,
173 unsigned char *p_delete_on_close
)
176 struct smb_share_mode_entry
*list
= NULL
;
177 int num_share_modes
= 0;
178 struct locking_data
*ld
= NULL
; /* internal samba db state. */
179 struct share_mode_entry
*shares
= NULL
;
184 *p_delete_on_close
= 0;
186 db_data
= tdb_fetch(db_ctx
->smb_tdb
, get_locking_key(dev
, ino
));
191 ld
= (struct locking_data
*)db_data
.dptr
;
192 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
194 if (!num_share_modes
) {
199 list
= (struct smb_share_mode_entry
*)malloc(sizeof(struct smb_share_mode_entry
)*num_share_modes
);
205 memset(list
, '\0', num_share_modes
* sizeof(struct smb_share_mode_entry
));
207 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
210 for (i
= 0; i
< num_share_modes
; i
++) {
211 struct share_mode_entry
*share
= &shares
[i
];
212 struct smb_share_mode_entry
*sme
= &list
[list_num
];
213 struct server_id pid
= share
->pid
;
215 /* Check this process really exists. */
216 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
217 continue; /* No longer exists. */
220 /* Ignore deferred open entries. */
221 if (share
->op_type
== DEFERRED_OPEN_ENTRY
) {
225 /* Copy into the external list. */
226 sme
->dev
= share
->id
.devid
;
227 sme
->ino
= share
->id
.inode
;
228 sme
->share_access
= (uint32_t)share
->share_access
;
229 sme
->access_mask
= (uint32_t)share
->access_mask
;
230 sme
->open_time
.tv_sec
= share
->time
.tv_sec
;
231 sme
->open_time
.tv_usec
= share
->time
.tv_usec
;
232 sme
->file_id
= (uint32_t)share
->share_file_id
;
233 sme
->pid
= share
->pid
;
243 *p_delete_on_close
= ld
->u
.s
.delete_on_close
;
250 * Create an entry in the Samba share mode db.
253 int smb_create_share_mode_entry_ex(struct smbdb_ctx
*db_ctx
,
256 const struct smb_share_mode_entry
*new_entry
,
257 const char *sharepath
, /* Must be absolute utf8 path. */
258 const char *filename
) /* Must be relative utf8 path. */
261 TDB_DATA locking_key
= get_locking_key(dev
, ino
);
262 int orig_num_share_modes
= 0;
263 struct locking_data
*ld
= NULL
; /* internal samba db state. */
264 struct share_mode_entry
*shares
= NULL
;
265 uint8
*new_data_p
= NULL
;
266 size_t new_data_size
= 0;
268 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
270 /* We must create the entry. */
271 db_data
.dptr
= (uint8
*)malloc(
272 sizeof(struct locking_data
) +
273 sizeof(struct share_mode_entry
) +
274 strlen(sharepath
) + 1 +
275 strlen(filename
) + 1);
279 ld
= (struct locking_data
*)db_data
.dptr
;
280 memset(ld
, '\0', sizeof(struct locking_data
));
281 ld
->u
.s
.num_share_mode_entries
= 1;
282 ld
->u
.s
.delete_on_close
= 0;
283 ld
->u
.s
.delete_token_size
= 0;
284 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
285 create_share_mode_entry(shares
, new_entry
);
287 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
),
289 strlen(sharepath
) + 1);
290 memcpy(db_data
.dptr
+ sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
291 strlen(sharepath
) + 1,
293 strlen(filename
) + 1);
295 db_data
.dsize
= sizeof(struct locking_data
) + sizeof(struct share_mode_entry
) +
296 strlen(sharepath
) + 1 +
297 strlen(filename
) + 1;
298 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_INSERT
) == -1) {
306 /* Entry exists, we must add a new entry. */
307 new_data_p
= (uint8
*)malloc(
308 db_data
.dsize
+ sizeof(struct share_mode_entry
));
314 ld
= (struct locking_data
*)db_data
.dptr
;
315 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
317 /* Copy the original data. */
318 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
320 /* Add in the new share mode */
321 shares
= (struct share_mode_entry
*)(new_data_p
+ sizeof(struct locking_data
) +
322 (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
324 create_share_mode_entry(shares
, new_entry
);
326 ld
= (struct locking_data
*)new_data_p
;
327 ld
->u
.s
.num_share_mode_entries
++;
329 /* Append the original delete_token and filenames. */
330 memcpy(new_data_p
+ sizeof(struct locking_data
) + (ld
->u
.s
.num_share_mode_entries
* sizeof(struct share_mode_entry
)),
331 db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
)),
332 db_data
.dsize
- sizeof(struct locking_data
) - (orig_num_share_modes
* sizeof(struct share_mode_entry
)));
334 new_data_size
= db_data
.dsize
+ sizeof(struct share_mode_entry
);
338 db_data
.dptr
= new_data_p
;
339 db_data
.dsize
= new_data_size
;
341 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
350 * Create an entry in the Samba share mode db. Original interface - doesn't
351 * Distinguish between share path and filename. Fudge this by using a
352 * sharepath of / and a relative filename of (filename+1).
355 int smb_create_share_mode_entry(struct smbdb_ctx
*db_ctx
,
358 const struct smb_share_mode_entry
*new_entry
,
359 const char *filename
) /* Must be absolute utf8 path. */
361 if (*filename
!= '/') {
364 return smb_create_share_mode_entry_ex(db_ctx
, dev
, ino
, new_entry
,
368 int smb_delete_share_mode_entry(struct smbdb_ctx
*db_ctx
,
371 const struct smb_share_mode_entry
*del_entry
)
374 TDB_DATA locking_key
= get_locking_key(dev
, ino
);
375 int orig_num_share_modes
= 0;
376 struct locking_data
*ld
= NULL
; /* internal samba db state. */
377 struct share_mode_entry
*shares
= NULL
;
378 uint8
*new_data_p
= NULL
;
379 size_t remaining_size
= 0;
380 size_t i
, num_share_modes
;
381 const uint8
*remaining_ptr
= NULL
;
383 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
385 return -1; /* Error - missing entry ! */
388 ld
= (struct locking_data
*)db_data
.dptr
;
389 orig_num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
390 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
392 if (orig_num_share_modes
== 1) {
393 /* Only one entry - better be ours... */
394 if (!share_mode_entry_equal(del_entry
, shares
)) {
395 /* Error ! We can't delete someone else's entry ! */
399 /* It's ours - just remove the entire record. */
401 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
404 /* More than one - allocate a new record minus the one we'll delete. */
405 new_data_p
= (uint8
*)malloc(
406 db_data
.dsize
- sizeof(struct share_mode_entry
));
412 /* Copy the header. */
413 memcpy(new_data_p
, db_data
.dptr
, sizeof(struct locking_data
));
416 for (i
= 0; i
< orig_num_share_modes
; i
++) {
417 struct share_mode_entry
*share
= &shares
[i
];
418 struct server_id pid
= share
->pid
;
420 /* Check this process really exists. */
421 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
422 continue; /* No longer exists. */
425 if (share_mode_entry_equal(del_entry
, share
)) {
426 continue; /* This is our delete taget. */
429 memcpy(new_data_p
+ sizeof(struct locking_data
) +
430 (num_share_modes
* sizeof(struct share_mode_entry
)),
431 share
, sizeof(struct share_mode_entry
) );
436 if (num_share_modes
== 0) {
437 /* None left after pruning. Delete record. */
440 return tdb_delete(db_ctx
->smb_tdb
, locking_key
);
443 /* Copy any delete token plus the terminating filenames. */
444 remaining_ptr
= db_data
.dptr
+ sizeof(struct locking_data
) + (orig_num_share_modes
* sizeof(struct share_mode_entry
));
445 remaining_size
= db_data
.dsize
- (remaining_ptr
- db_data
.dptr
);
447 memcpy(new_data_p
+ sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)),
453 db_data
.dptr
= new_data_p
;
455 /* Re-save smaller record. */
456 ld
= (struct locking_data
*)db_data
.dptr
;
457 ld
->u
.s
.num_share_mode_entries
= num_share_modes
;
459 db_data
.dsize
= sizeof(struct locking_data
) + (num_share_modes
* sizeof(struct share_mode_entry
)) + remaining_size
;
461 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {
469 int smb_change_share_mode_entry(struct smbdb_ctx
*db_ctx
,
472 const struct smb_share_mode_entry
*set_entry
,
473 const struct smb_share_mode_entry
*new_entry
)
476 TDB_DATA locking_key
= get_locking_key(dev
, ino
);
477 int num_share_modes
= 0;
478 struct locking_data
*ld
= NULL
; /* internal samba db state. */
479 struct share_mode_entry
*shares
= NULL
;
483 db_data
= tdb_fetch(db_ctx
->smb_tdb
, locking_key
);
485 return -1; /* Error - missing entry ! */
488 ld
= (struct locking_data
*)db_data
.dptr
;
489 num_share_modes
= ld
->u
.s
.num_share_mode_entries
;
490 shares
= (struct share_mode_entry
*)(db_data
.dptr
+ sizeof(struct locking_data
));
492 for (i
= 0; i
< num_share_modes
; i
++) {
493 struct share_mode_entry
*share
= &shares
[i
];
494 struct server_id pid
= share
->pid
;
496 /* Check this process really exists. */
497 if (kill(sharemodes_procid_to_pid(&pid
), 0) == -1 && (errno
== ESRCH
)) {
498 continue; /* No longer exists. */
501 if (share_mode_entry_equal(set_entry
, share
)) {
502 create_share_mode_entry(share
, new_entry
);
513 /* Save modified data. */
514 if (tdb_store(db_ctx
->smb_tdb
, locking_key
, db_data
, TDB_REPLACE
) == -1) {