int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx, uint64_t dev, uint64_t ino, uint64_t extid) { struct locking_key lk; return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino, extid)) == 0 ? 0 : -1; }
int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx, uint64_t dev, uint64_t ino, uint64_t extid, const struct smb_share_mode_entry *set_entry, const struct smb_share_mode_entry *new_entry) { TDB_DATA db_data; struct locking_key lk; TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid); int num_share_modes = 0; struct locking_data *ld = NULL; /* internal samba db state. */ struct share_mode_entry *shares = NULL; size_t i; int found_entry = 0; db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key); if (!db_data.dptr) { return -1; /* Error - missing entry ! */ } ld = (struct locking_data *)db_data.dptr; num_share_modes = ld->u.s.num_share_mode_entries; shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data)); for (i = 0; i < num_share_modes; i++) { struct share_mode_entry *share = &shares[i]; struct server_id pid = share->pid; /* Check this process really exists. */ if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) { continue; /* No longer exists. */ } if (share_mode_entry_equal(set_entry, share)) { create_share_mode_entry(share, new_entry, share->name_hash); found_entry = 1; break; } } if (!found_entry) { free(db_data.dptr); return -1; } /* Save modified data. */ if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) { free(db_data.dptr); return -1; } free(db_data.dptr); return 0; }
int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx, uint64_t dev, uint64_t ino, uint64_t extid, const struct smb_share_mode_entry *del_entry) { TDB_DATA db_data; struct locking_key lk; TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid); int orig_num_share_modes = 0; struct locking_data *ld = NULL; /* internal samba db state. */ struct share_mode_entry *shares = NULL; uint8 *new_data_p = NULL; size_t remaining_size = 0; size_t i, num_share_modes; const uint8 *remaining_ptr = NULL; db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key); if (!db_data.dptr) { return -1; /* Error - missing entry ! */ } ld = (struct locking_data *)db_data.dptr; orig_num_share_modes = ld->u.s.num_share_mode_entries; shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data)); if (orig_num_share_modes == 1) { /* Only one entry - better be ours... */ if (!share_mode_entry_equal(del_entry, shares)) { /* Error ! We can't delete someone else's entry ! */ free(db_data.dptr); return -1; } /* It's ours - just remove the entire record. */ free(db_data.dptr); return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0; } /* More than one - allocate a new record minus the one we'll delete. */ new_data_p = (uint8 *)malloc( db_data.dsize - sizeof(struct share_mode_entry)); if (!new_data_p) { free(db_data.dptr); return -1; } /* Copy the header. */ memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data)); num_share_modes = 0; for (i = 0; i < orig_num_share_modes; i++) { struct share_mode_entry *share = &shares[i]; struct server_id pid = share->pid; /* Check this process really exists. */ if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) { continue; /* No longer exists. */ } if (share_mode_entry_equal(del_entry, share)) { continue; /* This is our delete taget. */ } memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)), share, sizeof(struct share_mode_entry) ); num_share_modes++; } if (num_share_modes == 0) { /* None left after pruning. Delete record. */ free(db_data.dptr); free(new_data_p); return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0; } /* Copy any delete tokens plus the terminating filenames. */ remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)); remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr); memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)), remaining_ptr, remaining_size); free(db_data.dptr); db_data.dptr = new_data_p; /* Re-save smaller record. */ ld = (struct locking_data *)db_data.dptr; ld->u.s.num_share_mode_entries = num_share_modes; db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size; if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) { free(db_data.dptr); return -1; } free(db_data.dptr); return 0; }
int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev, uint64_t ino, uint64_t extid, const struct smb_share_mode_entry *new_entry, const char *sharepath, /* Must be absolute utf8 path. */ const char *filename) /* Must be relative utf8 path. */ { TDB_DATA db_data; struct locking_key lk; TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid); int orig_num_share_modes = 0; struct locking_data *ld = NULL; /* internal samba db state. */ struct share_mode_entry *shares = NULL; uint8 *new_data_p = NULL; size_t new_data_size = 0; int err = 0; uint32_t name_hash = smb_name_hash(sharepath, filename, &err); if (err) { return -1; } db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key); if (!db_data.dptr) { /* We must create the entry. */ db_data.dptr = (uint8 *)malloc( sizeof(struct locking_data) + sizeof(struct share_mode_entry) + strlen(sharepath) + 1 + strlen(filename) + 1); if (!db_data.dptr) { return -1; } ld = (struct locking_data *)db_data.dptr; memset(ld, '\0', sizeof(struct locking_data)); ld->u.s.num_share_mode_entries = 1; ld->u.s.num_delete_token_entries = 0; shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data)); create_share_mode_entry(shares, new_entry, name_hash); memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry), sharepath, strlen(sharepath) + 1); memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) + strlen(sharepath) + 1, filename, strlen(filename) + 1); db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) + strlen(sharepath) + 1 + strlen(filename) + 1; if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) != 0) { free(db_data.dptr); return -1; } free(db_data.dptr); return 0; } /* Entry exists, we must add a new entry. */ new_data_p = (uint8 *)malloc( db_data.dsize + sizeof(struct share_mode_entry)); if (!new_data_p) { free(db_data.dptr); return -1; } ld = (struct locking_data *)db_data.dptr; orig_num_share_modes = ld->u.s.num_share_mode_entries; /* Copy the original data. */ memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry))); /* Add in the new share mode */ shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry))); create_share_mode_entry(shares, new_entry, name_hash); ld = (struct locking_data *)new_data_p; ld->u.s.num_share_mode_entries++; /* Append the original delete_tokens and filenames. */ memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)), db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)), db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry))); new_data_size = db_data.dsize + sizeof(struct share_mode_entry); free(db_data.dptr); db_data.dptr = new_data_p; db_data.dsize = new_data_size; if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) { free(db_data.dptr); return -1; } free(db_data.dptr); return 0; }
int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx, uint64_t dev, uint64_t ino, uint64_t extid, struct smb_share_mode_entry **pp_list, unsigned char *p_delete_on_close) { struct locking_key lk; TDB_DATA db_data; struct smb_share_mode_entry *list = NULL; int num_share_modes = 0; struct locking_data *ld = NULL; /* internal samba db state. */ struct share_mode_entry *shares = NULL; size_t i; int list_num; *pp_list = NULL; *p_delete_on_close = 0; db_data = tdb_fetch_compat(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino, extid)); if (!db_data.dptr) { return 0; } ld = (struct locking_data *)db_data.dptr; num_share_modes = ld->u.s.num_share_mode_entries; if (!num_share_modes) { free(db_data.dptr); return 0; } list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes); if (!list) { free(db_data.dptr); return -1; } memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry)); shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data)); list_num = 0; for (i = 0; i < num_share_modes; i++) { struct share_mode_entry *share = &shares[i]; struct smb_share_mode_entry *sme = &list[list_num]; struct server_id pid = share->pid; /* Check this process really exists. */ if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) { continue; /* No longer exists. */ } /* Ignore deferred open entries. */ if (share->op_type == DEFERRED_OPEN_ENTRY) { continue; } /* Copy into the external list. */ sme->dev = share->id.devid; sme->ino = share->id.inode; sme->extid = share->id.extid; sme->share_access = (uint32_t)share->share_access; sme->access_mask = (uint32_t)share->access_mask; sme->open_time.tv_sec = share->time.tv_sec; sme->open_time.tv_usec = share->time.tv_usec; sme->file_id = (uint32_t)share->share_file_id; sme->pid = share->pid; list_num++; } if (list_num == 0) { free(db_data.dptr); free(list); return 0; } *p_delete_on_close = ld->u.s.num_delete_token_entries != 0; *pp_list = list; free(db_data.dptr); return list_num; }
int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx, uint64_t dev, uint64_t ino) { return tdb_chainunlock(db_ctx->smb_tdb, get_locking_key(dev, ino)); }