Fix for crash bug freeing a non-malloc'ed buffer if the client sends a non-encrypted...
[Samba/bb.git] / source / libsmb / smb_share_modes.c
blob16b3b1092545390c03e98b846408a8fb578647a4
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 "smb_share_modes.h"
30 /* Database context handle. */
31 struct smbdb_ctx {
32 TDB_CONTEXT *smb_tdb;
35 /* Remove the paranoid malloc checker. */
36 #ifdef malloc
37 #undef malloc
38 #endif
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)
51 return proc->pid;
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));
62 if (!smb_db) {
63 return NULL;
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,
70 O_RDWR|O_CREAT,
71 0644);
73 if (!smb_db->smb_tdb) {
74 free(smb_db);
75 return NULL;
78 /* Should check that this is the correct version.... */
79 return smb_db;
82 /* key and data records in the tdb locking database */
83 struct locking_key {
84 SMB_DEV_T dev;
85 SMB_INO_T inode;
88 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
90 int ret = tdb_close(db_ctx->smb_tdb);
91 free(db_ctx);
92 return ret;
95 static TDB_DATA get_locking_key(uint64_t dev, uint64_t ino)
97 static struct locking_key lk;
98 TDB_DATA ld;
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);
105 return ld;
109 * lock/unlock entry in sharemode database.
112 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
113 uint64_t dev,
114 uint64_t ino)
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,
120 uint64_t dev,
121 uint64_t ino)
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));
152 out->pid = in->pid;
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();
161 out->flags = 0;
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,
170 uint64_t dev,
171 uint64_t ino,
172 struct smb_share_mode_entry **pp_list,
173 unsigned char *p_delete_on_close)
175 TDB_DATA db_data;
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;
180 size_t i;
181 int list_num;
183 *pp_list = NULL;
184 *p_delete_on_close = 0;
186 db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(dev, ino));
187 if (!db_data.dptr) {
188 return 0;
191 ld = (struct locking_data *)db_data.dptr;
192 num_share_modes = ld->u.s.num_share_mode_entries;
194 if (!num_share_modes) {
195 free(db_data.dptr);
196 return 0;
199 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
200 if (!list) {
201 free(db_data.dptr);
202 return -1;
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));
209 list_num = 0;
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) {
222 continue;
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;
234 list_num++;
237 if (list_num == 0) {
238 free(db_data.dptr);
239 free(list);
240 return 0;
243 *p_delete_on_close = ld->u.s.delete_on_close;
244 *pp_list = list;
245 free(db_data.dptr);
246 return list_num;
250 * Create an entry in the Samba share mode db.
253 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
254 uint64_t dev,
255 uint64_t ino,
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. */
260 TDB_DATA db_data;
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);
269 if (!db_data.dptr) {
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);
276 if (!db_data.dptr) {
277 return -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),
288 sharepath,
289 strlen(sharepath) + 1);
290 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
291 strlen(sharepath) + 1,
292 filename,
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) {
299 free(db_data.dptr);
300 return -1;
302 free(db_data.dptr);
303 return 0;
306 /* Entry exists, we must add a new entry. */
307 new_data_p = (uint8 *)malloc(
308 db_data.dsize + sizeof(struct share_mode_entry));
309 if (!new_data_p) {
310 free(db_data.dptr);
311 return -1;
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);
336 free(db_data.dptr);
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) {
342 free(db_data.dptr);
343 return -1;
345 free(db_data.dptr);
346 return 0;
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,
356 uint64_t dev,
357 uint64_t ino,
358 const struct smb_share_mode_entry *new_entry,
359 const char *filename) /* Must be absolute utf8 path. */
361 if (*filename != '/') {
362 abort();
364 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
365 "/", &filename[1]);
368 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
369 uint64_t dev,
370 uint64_t ino,
371 const struct smb_share_mode_entry *del_entry)
373 TDB_DATA db_data;
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);
384 if (!db_data.dptr) {
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 ! */
396 free(db_data.dptr);
397 return -1;
399 /* It's ours - just remove the entire record. */
400 free(db_data.dptr);
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));
407 if (!new_data_p) {
408 free(db_data.dptr);
409 return -1;
412 /* Copy the header. */
413 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
415 num_share_modes = 0;
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) );
433 num_share_modes++;
436 if (num_share_modes == 0) {
437 /* None left after pruning. Delete record. */
438 free(db_data.dptr);
439 free(new_data_p);
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)),
448 remaining_ptr,
449 remaining_size);
451 free(db_data.dptr);
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) {
462 free(db_data.dptr);
463 return -1;
465 free(db_data.dptr);
466 return 0;
469 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
470 uint64_t dev,
471 uint64_t ino,
472 const struct smb_share_mode_entry *set_entry,
473 const struct smb_share_mode_entry *new_entry)
475 TDB_DATA db_data;
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;
480 size_t i;
481 int found_entry = 0;
483 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
484 if (!db_data.dptr) {
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);
503 found_entry = 1;
504 break;
508 if (!found_entry) {
509 free(db_data.dptr);
510 return -1;
513 /* Save modified data. */
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;