r16472: final pass for 3.0.23rc3 I think. Current with SAMBA_3_0 r16471
[Samba.git] / source / libsmb / smb_share_modes.c
blob34ede9df296947a64e0ea5b49eb1d054854ec9cc
1 /*
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
11 ** under the LGPL
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 2 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, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "includes.h"
29 #include "smb_share_modes.h"
31 /* Remove the paranoid malloc checker. */
32 #ifdef malloc
33 #undef malloc
34 #endif
36 static BOOL sharemodes_procid_equal(const struct process_id *p1, const struct process_id *p2)
38 return (p1->pid == p2->pid);
41 static pid_t sharemodes_procid_to_pid(const struct process_id *proc)
43 return proc->pid;
47 * open/close sharemode database.
50 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
52 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
54 if (!smb_db) {
55 return NULL;
58 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
60 smb_db->smb_tdb = tdb_open(db_path,
61 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
62 O_RDWR|O_CREAT,
63 0644);
65 if (!smb_db->smb_tdb) {
66 free(smb_db);
67 return NULL;
70 /* Should check that this is the correct version.... */
71 return smb_db;
74 /* key and data records in the tdb locking database */
75 struct locking_key {
76 SMB_DEV_T dev;
77 SMB_INO_T inode;
80 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
82 int ret = tdb_close(db_ctx->smb_tdb);
83 free(db_ctx);
84 return ret;
87 static TDB_DATA get_locking_key(uint64_t dev, uint64_t ino)
89 static struct locking_key lk;
90 TDB_DATA ld;
92 memset(&lk, '\0', sizeof(struct locking_key));
93 lk.dev = (SMB_DEV_T)dev;
94 lk.inode = (SMB_INO_T)ino;
95 ld.dptr = (char *)&lk;
96 ld.dsize = sizeof(lk);
97 return ld;
101 * lock/unlock entry in sharemode database.
104 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
105 uint64_t dev,
106 uint64_t ino)
108 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
111 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
112 uint64_t dev,
113 uint64_t ino)
115 return tdb_chainunlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
119 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
122 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
123 const struct share_mode_entry *entry)
125 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
126 e_entry->file_id == (uint32_t)entry->share_file_id &&
127 e_entry->open_time.tv_sec == entry->time.tv_sec &&
128 e_entry->open_time.tv_usec == entry->time.tv_usec &&
129 e_entry->share_access == (uint32_t)entry->share_access &&
130 e_entry->access_mask == (uint32_t)entry->access_mask &&
131 e_entry->dev == (uint64_t)entry->dev &&
132 e_entry->ino == (uint64_t)entry->inode);
136 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
139 static void create_share_mode_entry(struct share_mode_entry *out,
140 const struct smb_share_mode_entry *in)
142 memset(out, '\0', sizeof(struct share_mode_entry));
144 out->pid = in->pid;
145 out->share_file_id = (unsigned long)in->file_id;
146 out->time.tv_sec = in->open_time.tv_sec;
147 out->time.tv_usec = in->open_time.tv_usec;
148 out->share_access = in->share_access;
149 out->access_mask = in->access_mask;
150 out->dev = (SMB_DEV_T)in->dev;
151 out->inode = (SMB_INO_T)in->ino;
152 out->uid = (uint32)geteuid();
156 * Return the current share mode list for an open file.
157 * This uses similar (but simplified) logic to locking/locking.c
160 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
161 uint64_t dev,
162 uint64_t ino,
163 struct smb_share_mode_entry **pp_list,
164 unsigned char *p_delete_on_close)
166 TDB_DATA db_data;
167 struct smb_share_mode_entry *list = NULL;
168 int num_share_modes = 0;
169 struct locking_data *ld = NULL; /* internal samba db state. */
170 struct share_mode_entry *shares = NULL;
171 size_t i;
172 int list_num;
174 *pp_list = NULL;
175 *p_delete_on_close = 0;
177 db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(dev, ino));
178 if (!db_data.dptr) {
179 return 0;
182 ld = (struct locking_data *)db_data.dptr;
183 num_share_modes = ld->u.s.num_share_mode_entries;
185 if (!num_share_modes) {
186 free(db_data.dptr);
187 return 0;
190 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
191 if (!list) {
192 free(db_data.dptr);
193 return -1;
196 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
198 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
200 list_num = 0;
201 for (i = 0; i < num_share_modes; i++) {
202 struct share_mode_entry *share = &shares[i];
203 struct smb_share_mode_entry *sme = &list[list_num];
204 struct process_id pid = share->pid;
206 /* Check this process really exists. */
207 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
208 continue; /* No longer exists. */
211 /* Ignore deferred open entries. */
212 if (share->op_type == DEFERRED_OPEN_ENTRY) {
213 continue;
216 /* Copy into the external list. */
217 sme->dev = (uint64_t)share->dev;
218 sme->ino = (uint64_t)share->inode;
219 sme->share_access = (uint32_t)share->share_access;
220 sme->access_mask = (uint32_t)share->access_mask;
221 sme->open_time.tv_sec = share->time.tv_sec;
222 sme->open_time.tv_usec = share->time.tv_usec;
223 sme->file_id = (uint32_t)share->share_file_id;
224 sme->pid = share->pid;
225 list_num++;
228 if (list_num == 0) {
229 free(db_data.dptr);
230 free(list);
231 return 0;
234 *p_delete_on_close = ld->u.s.delete_on_close;
235 *pp_list = list;
236 free(db_data.dptr);
237 return list_num;
241 * Create an entry in the Samba share mode db.
244 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
245 uint64_t dev,
246 uint64_t ino,
247 const struct smb_share_mode_entry *new_entry,
248 const char *sharepath, /* Must be absolute utf8 path. */
249 const char *filename) /* Must be relative utf8 path. */
251 TDB_DATA db_data;
252 TDB_DATA locking_key = get_locking_key(dev, ino);
253 int orig_num_share_modes = 0;
254 struct locking_data *ld = NULL; /* internal samba db state. */
255 struct share_mode_entry *shares = NULL;
256 char *new_data_p = NULL;
257 size_t new_data_size = 0;
259 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
260 if (!db_data.dptr) {
261 /* We must create the entry. */
262 db_data.dptr = malloc((2*sizeof(struct share_mode_entry)) +
263 strlen(sharepath) + 1 +
264 strlen(filename) + 1);
265 if (!db_data.dptr) {
266 return -1;
268 ld = (struct locking_data *)db_data.dptr;
269 memset(ld, '\0', sizeof(struct locking_data));
270 ld->u.s.num_share_mode_entries = 1;
271 ld->u.s.delete_on_close = 0;
272 ld->u.s.initial_delete_on_close = 0;
273 ld->u.s.delete_token_size = 0;
274 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
275 create_share_mode_entry(shares, new_entry);
277 memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry),
278 sharepath,
279 strlen(sharepath) + 1);
280 memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry) +
281 strlen(sharepath) + 1,
282 filename,
283 strlen(filename) + 1);
285 db_data.dsize = 2*sizeof(struct share_mode_entry) +
286 strlen(sharepath) + 1 +
287 strlen(filename) + 1;
288 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
289 free(db_data.dptr);
290 return -1;
292 free(db_data.dptr);
293 return 0;
296 /* Entry exists, we must add a new entry. */
297 new_data_p = malloc(db_data.dsize + sizeof(struct share_mode_entry));
298 if (!new_data_p) {
299 free(db_data.dptr);
300 return -1;
303 ld = (struct locking_data *)db_data.dptr;
304 orig_num_share_modes = ld->u.s.num_share_mode_entries;
306 /* Copy the original data. */
307 memcpy(new_data_p, db_data.dptr, (orig_num_share_modes+1)*sizeof(struct share_mode_entry));
309 /* Add in the new share mode */
310 shares = (struct share_mode_entry *)(new_data_p +
311 ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)));
313 create_share_mode_entry(shares, new_entry);
315 ld = (struct locking_data *)new_data_p;
316 ld->u.s.num_share_mode_entries++;
318 /* Append the original delete_token and filenames. */
319 memcpy(new_data_p + ((ld->u.s.num_share_mode_entries+1)*sizeof(struct share_mode_entry)),
320 db_data.dptr + ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)),
321 db_data.dsize - ((orig_num_share_modes+1) * sizeof(struct share_mode_entry)));
323 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
325 free(db_data.dptr);
327 db_data.dptr = new_data_p;
328 db_data.dsize = new_data_size;
330 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
331 free(db_data.dptr);
332 return -1;
334 free(db_data.dptr);
335 return 0;
339 * Create an entry in the Samba share mode db. Original interface - doesn't
340 * Distinguish between share path and filename. Fudge this by using a
341 * sharepath of / and a relative filename of (filename+1).
344 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
345 uint64_t dev,
346 uint64_t ino,
347 const struct smb_share_mode_entry *new_entry,
348 const char *filename) /* Must be absolute utf8 path. */
350 if (*filename != '/') {
351 abort();
353 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
354 "/", &filename[1]);
357 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
358 uint64_t dev,
359 uint64_t ino,
360 const struct smb_share_mode_entry *del_entry)
362 TDB_DATA db_data;
363 TDB_DATA locking_key = get_locking_key(dev, ino);
364 int orig_num_share_modes = 0;
365 struct locking_data *ld = NULL; /* internal samba db state. */
366 struct share_mode_entry *shares = NULL;
367 char *new_data_p = NULL;
368 size_t remaining_size = 0;
369 size_t i, num_share_modes;
370 const char *remaining_ptr = NULL;
372 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
373 if (!db_data.dptr) {
374 return -1; /* Error - missing entry ! */
377 ld = (struct locking_data *)db_data.dptr;
378 orig_num_share_modes = ld->u.s.num_share_mode_entries;
379 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
381 if (orig_num_share_modes == 1) {
382 /* Only one entry - better be ours... */
383 if (!share_mode_entry_equal(del_entry, shares)) {
384 /* Error ! We can't delete someone else's entry ! */
385 free(db_data.dptr);
386 return -1;
388 /* It's ours - just remove the entire record. */
389 free(db_data.dptr);
390 return tdb_delete(db_ctx->smb_tdb, locking_key);
393 /* More than one - allocate a new record minus the one we'll delete. */
394 new_data_p = malloc(db_data.dsize - sizeof(struct share_mode_entry));
395 if (!new_data_p) {
396 free(db_data.dptr);
397 return -1;
400 /* Copy the header. */
401 memcpy(new_data_p, db_data.dptr, sizeof(struct share_mode_entry));
403 num_share_modes = 0;
404 for (i = 0; i < orig_num_share_modes; i++) {
405 struct share_mode_entry *share = &shares[i];
406 struct process_id pid = share->pid;
408 /* Check this process really exists. */
409 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
410 continue; /* No longer exists. */
413 if (share_mode_entry_equal(del_entry, share)) {
414 continue; /* This is our delete taget. */
417 memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
418 share, sizeof(struct share_mode_entry) );
420 num_share_modes++;
423 if (num_share_modes == 0) {
424 /* None left after pruning. Delete record. */
425 free(db_data.dptr);
426 free(new_data_p);
427 return tdb_delete(db_ctx->smb_tdb, locking_key);
430 /* Copy any delete token plus the terminating filenames. */
431 remaining_ptr = db_data.dptr + ((orig_num_share_modes+1) * sizeof(struct share_mode_entry));
432 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
434 memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
435 remaining_ptr,
436 remaining_size);
438 free(db_data.dptr);
440 db_data.dptr = new_data_p;
442 /* Re-save smaller record. */
443 ld = (struct locking_data *)db_data.dptr;
444 ld->u.s.num_share_mode_entries = num_share_modes;
446 db_data.dsize = ((num_share_modes+1)*sizeof(struct share_mode_entry)) + remaining_size;
448 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
449 free(db_data.dptr);
450 return -1;
452 free(db_data.dptr);
453 return 0;
456 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
457 uint64_t dev,
458 uint64_t ino,
459 const struct smb_share_mode_entry *set_entry,
460 const struct smb_share_mode_entry *new_entry)
462 TDB_DATA db_data;
463 TDB_DATA locking_key = get_locking_key(dev, ino);
464 int num_share_modes = 0;
465 struct locking_data *ld = NULL; /* internal samba db state. */
466 struct share_mode_entry *shares = NULL;
467 size_t i;
468 int found_entry = 0;
470 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
471 if (!db_data.dptr) {
472 return -1; /* Error - missing entry ! */
475 ld = (struct locking_data *)db_data.dptr;
476 num_share_modes = ld->u.s.num_share_mode_entries;
477 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
479 for (i = 0; i < num_share_modes; i++) {
480 struct share_mode_entry *share = &shares[i];
481 struct process_id pid = share->pid;
483 /* Check this process really exists. */
484 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
485 continue; /* No longer exists. */
488 if (share_mode_entry_equal(set_entry, share)) {
489 create_share_mode_entry(share, new_entry);
490 found_entry = 1;
491 break;
495 if (!found_entry) {
496 free(db_data.dptr);
497 return -1;
500 /* Save modified data. */
501 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
502 free(db_data.dptr);
503 return -1;
505 free(db_data.dptr);
506 return 0;