s3: Fix Coverity ID 2335, CHECKED_RETURN
[Samba.git] / source3 / libsmb / smb_share_modes.c
blob1a6c2123ed6698f5dc686606a157232a475df035
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 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/>.
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "smb_share_modes.h"
31 /* Database context handle. */
32 struct smbdb_ctx {
33 TDB_CONTEXT *smb_tdb;
36 /* Remove the paranoid malloc checker. */
37 #ifdef malloc
38 #undef malloc
39 #endif
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)
53 return proc->pid;
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));
64 if (!smb_db) {
65 return NULL;
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,
72 O_RDWR|O_CREAT,
73 0644);
75 if (!smb_db->smb_tdb) {
76 free(smb_db);
77 return NULL;
80 /* Should check that this is the correct version.... */
81 return smb_db;
84 /* key and data records in the tdb locking database */
85 struct locking_key {
86 SMB_DEV_T dev;
87 SMB_INO_T inode;
88 uint64_t extid;
91 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
93 int ret = tdb_close(db_ctx->smb_tdb);
94 free(db_ctx);
95 return ret;
98 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
99 uint64_t ino, uint64_t extid)
101 TDB_DATA ld;
103 memset(lk, '\0', sizeof(*lk));
104 lk->dev = (SMB_DEV_T)dev;
105 lk->inode = (SMB_INO_T)ino;
106 lk->extid = extid;
107 ld.dptr = (uint8 *)lk;
108 ld.dsize = sizeof(*lk);
109 return ld;
113 * lock/unlock entry in sharemode database.
116 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
117 uint64_t dev,
118 uint64_t ino,
119 uint64_t extid)
121 struct locking_key lk;
122 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
123 extid));
126 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
127 uint64_t dev,
128 uint64_t ino,
129 uint64_t extid)
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,
160 uint32_t name_hash)
162 memset(out, '\0', sizeof(struct share_mode_entry));
164 out->pid = in->pid;
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();
174 out->flags = 0;
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,
184 uint64_t dev,
185 uint64_t ino,
186 uint64_t extid,
187 struct smb_share_mode_entry **pp_list,
188 unsigned char *p_delete_on_close)
190 struct locking_key lk;
191 TDB_DATA db_data;
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;
196 size_t i;
197 int list_num;
199 *pp_list = NULL;
200 *p_delete_on_close = 0;
202 db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
203 extid));
204 if (!db_data.dptr) {
205 return 0;
208 ld = (struct locking_data *)db_data.dptr;
209 num_share_modes = ld->u.s.num_share_mode_entries;
211 if (!num_share_modes) {
212 free(db_data.dptr);
213 return 0;
216 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
217 if (!list) {
218 free(db_data.dptr);
219 return -1;
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));
226 list_num = 0;
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) {
239 continue;
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;
252 list_num++;
255 if (list_num == 0) {
256 free(db_data.dptr);
257 free(list);
258 return 0;
261 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
262 *pp_list = list;
263 free(db_data.dptr);
264 return list_num;
267 static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
269 TDB_DATA key;
270 char *fullpath = NULL;
271 size_t sharepath_size = strlen(sharepath);
272 size_t filename_size = strlen(filename);
273 uint32_t name_hash;
275 *err = 0;
276 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
277 if (fullpath == NULL) {
278 *err = 1;
279 return 0;
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);
288 free(fullpath);
289 return name_hash;
293 * Create an entry in the Samba share mode db.
296 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
297 uint64_t dev,
298 uint64_t ino,
299 uint64_t extid,
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. */
304 TDB_DATA db_data;
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;
312 int err = 0;
313 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
315 if (err) {
316 return -1;
319 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
320 if (!db_data.dptr) {
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);
327 if (!db_data.dptr) {
328 return -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),
338 sharepath,
339 strlen(sharepath) + 1);
340 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
341 strlen(sharepath) + 1,
342 filename,
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) {
349 free(db_data.dptr);
350 return -1;
352 free(db_data.dptr);
353 return 0;
356 /* Entry exists, we must add a new entry. */
357 new_data_p = (uint8 *)malloc(
358 db_data.dsize + sizeof(struct share_mode_entry));
359 if (!new_data_p) {
360 free(db_data.dptr);
361 return -1;
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);
386 free(db_data.dptr);
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) {
392 free(db_data.dptr);
393 return -1;
395 free(db_data.dptr);
396 return 0;
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,
406 uint64_t dev,
407 uint64_t ino,
408 uint64_t extid,
409 const struct smb_share_mode_entry *new_entry,
410 const char *filename) /* Must be absolute utf8 path. */
412 if (*filename != '/') {
413 abort();
415 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
416 "/", &filename[1]);
419 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
420 uint64_t dev,
421 uint64_t ino,
422 uint64_t extid,
423 const struct smb_share_mode_entry *del_entry)
425 TDB_DATA db_data;
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);
437 if (!db_data.dptr) {
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 ! */
449 free(db_data.dptr);
450 return -1;
452 /* It's ours - just remove the entire record. */
453 free(db_data.dptr);
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));
460 if (!new_data_p) {
461 free(db_data.dptr);
462 return -1;
465 /* Copy the header. */
466 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
468 num_share_modes = 0;
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) );
486 num_share_modes++;
489 if (num_share_modes == 0) {
490 /* None left after pruning. Delete record. */
491 free(db_data.dptr);
492 free(new_data_p);
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)),
501 remaining_ptr,
502 remaining_size);
504 free(db_data.dptr);
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) {
515 free(db_data.dptr);
516 return -1;
518 free(db_data.dptr);
519 return 0;
522 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
523 uint64_t dev,
524 uint64_t ino,
525 uint64_t extid,
526 const struct smb_share_mode_entry *set_entry,
527 const struct smb_share_mode_entry *new_entry)
529 TDB_DATA db_data;
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;
535 size_t i;
536 int found_entry = 0;
538 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
539 if (!db_data.dptr) {
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);
558 found_entry = 1;
559 break;
563 if (!found_entry) {
564 free(db_data.dptr);
565 return -1;
568 /* Save modified data. */
569 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
570 free(db_data.dptr);
571 return -1;
573 free(db_data.dptr);
574 return 0;