s3:smb2cli: ignore the NBT/Length header in smb2cli_inbuf_parse_compound()
[Samba/gebeck_regimport.git] / source3 / libsmb / smb_share_modes.c
blob42640e0cea46a7ed043ee0e90a21dacdbee86da4
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 #define UID_WRAPPER_NOT_REPLACE
28 #include "includes.h"
29 #include "system/filesys.h"
30 #include "smb_share_modes.h"
31 #include "tdb_compat.h"
32 #include <ccan/hash/hash.h>
34 /* Database context handle. */
35 struct smbdb_ctx {
36 TDB_CONTEXT *smb_tdb;
39 /* Remove the paranoid malloc checker. */
40 #ifdef malloc
41 #undef malloc
42 #endif
44 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
45 uint64_t ino, uint64_t extid,
46 const struct smb_share_mode_entry *new_entry,
47 const char *sharepath, const char *filename);
49 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
51 return (p1->pid == p2->pid);
54 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
56 return proc->pid;
60 * open/close sharemode database.
63 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
65 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
67 if (!smb_db) {
68 return NULL;
71 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
73 /* FIXME: We should *never* open a tdb without logging! */
74 smb_db->smb_tdb = tdb_open_compat(db_path,
75 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
76 O_RDWR|O_CREAT,
77 0644,
78 NULL, NULL);
80 if (!smb_db->smb_tdb) {
81 free(smb_db);
82 return NULL;
85 /* Should check that this is the correct version.... */
86 return smb_db;
89 /* key and data records in the tdb locking database */
90 struct locking_key {
91 SMB_DEV_T dev;
92 SMB_INO_T inode;
93 uint64_t extid;
96 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
98 int ret = tdb_close(db_ctx->smb_tdb);
99 free(db_ctx);
100 return ret;
103 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
104 uint64_t ino, uint64_t extid)
106 TDB_DATA ld;
108 memset(lk, '\0', sizeof(*lk));
109 lk->dev = (SMB_DEV_T)dev;
110 lk->inode = (SMB_INO_T)ino;
111 lk->extid = extid;
112 ld.dptr = (uint8 *)lk;
113 ld.dsize = sizeof(*lk);
114 return ld;
118 * lock/unlock entry in sharemode database.
121 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
122 uint64_t dev,
123 uint64_t ino,
124 uint64_t extid)
126 struct locking_key lk;
127 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
128 extid)) == 0 ? 0 : -1;
131 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
132 uint64_t dev,
133 uint64_t ino,
134 uint64_t extid)
136 struct locking_key lk;
137 tdb_chainunlock(db_ctx->smb_tdb,
138 get_locking_key(&lk, dev, ino, extid));
139 return 0;
143 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
146 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
147 const struct share_mode_entry *entry)
149 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
150 e_entry->file_id == (uint32_t)entry->share_file_id &&
151 e_entry->open_time.tv_sec == entry->time.tv_sec &&
152 e_entry->open_time.tv_usec == entry->time.tv_usec &&
153 e_entry->share_access == (uint32_t)entry->share_access &&
154 e_entry->access_mask == (uint32_t)entry->access_mask &&
155 e_entry->dev == entry->id.devid &&
156 e_entry->ino == entry->id.inode &&
157 e_entry->extid == entry->id.extid);
161 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
164 static void create_share_mode_entry(struct share_mode_entry *out,
165 const struct smb_share_mode_entry *in,
166 uint32_t name_hash)
168 memset(out, '\0', sizeof(struct share_mode_entry));
170 out->pid = in->pid;
171 out->share_file_id = (unsigned long)in->file_id;
172 out->time.tv_sec = in->open_time.tv_sec;
173 out->time.tv_usec = in->open_time.tv_usec;
174 out->share_access = in->share_access;
175 out->access_mask = in->access_mask;
176 out->id.devid = in->dev;
177 out->id.inode = in->ino;
178 out->id.extid = in->extid;
179 out->uid = (uint32)geteuid();
180 out->flags = 0;
181 out->name_hash = name_hash;
185 * Return the current share mode list for an open file.
186 * This uses similar (but simplified) logic to locking/locking.c
189 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
190 uint64_t dev,
191 uint64_t ino,
192 uint64_t extid,
193 struct smb_share_mode_entry **pp_list,
194 unsigned char *p_delete_on_close)
196 struct locking_key lk;
197 TDB_DATA db_data;
198 struct smb_share_mode_entry *list = NULL;
199 int num_share_modes = 0;
200 struct locking_data *ld = NULL; /* internal samba db state. */
201 struct share_mode_entry *shares = NULL;
202 size_t i;
203 int list_num;
205 *pp_list = NULL;
206 *p_delete_on_close = 0;
208 db_data = tdb_fetch_compat(db_ctx->smb_tdb,
209 get_locking_key(&lk, dev, ino, extid));
210 if (!db_data.dptr) {
211 return 0;
214 ld = (struct locking_data *)db_data.dptr;
215 num_share_modes = ld->u.s.num_share_mode_entries;
217 if (!num_share_modes) {
218 free(db_data.dptr);
219 return 0;
222 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
223 if (!list) {
224 free(db_data.dptr);
225 return -1;
228 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
230 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
232 list_num = 0;
233 for (i = 0; i < num_share_modes; i++) {
234 struct share_mode_entry *share = &shares[i];
235 struct smb_share_mode_entry *sme = &list[list_num];
236 struct server_id pid = share->pid;
238 /* Check this process really exists. */
239 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
240 continue; /* No longer exists. */
243 /* Ignore deferred open entries. */
244 if (share->op_type == DEFERRED_OPEN_ENTRY) {
245 continue;
248 /* Copy into the external list. */
249 sme->dev = share->id.devid;
250 sme->ino = share->id.inode;
251 sme->extid = share->id.extid;
252 sme->share_access = (uint32_t)share->share_access;
253 sme->access_mask = (uint32_t)share->access_mask;
254 sme->open_time.tv_sec = share->time.tv_sec;
255 sme->open_time.tv_usec = share->time.tv_usec;
256 sme->file_id = (uint32_t)share->share_file_id;
257 sme->pid = share->pid;
258 list_num++;
261 if (list_num == 0) {
262 free(db_data.dptr);
263 free(list);
264 return 0;
267 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
268 *pp_list = list;
269 free(db_data.dptr);
270 return list_num;
273 static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
275 char *fullpath = NULL;
276 size_t sharepath_size = strlen(sharepath);
277 size_t filename_size = strlen(filename);
278 uint32_t name_hash;
280 *err = 0;
281 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
282 if (fullpath == NULL) {
283 *err = 1;
284 return 0;
286 memcpy(fullpath, sharepath, sharepath_size);
287 fullpath[sharepath_size] = '/';
288 memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
290 name_hash = hash(fullpath, strlen(fullpath) + 1, 0);
291 free(fullpath);
292 return name_hash;
296 * Create an entry in the Samba share mode db.
299 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
300 uint64_t dev,
301 uint64_t ino,
302 uint64_t extid,
303 const struct smb_share_mode_entry *new_entry,
304 const char *sharepath, /* Must be absolute utf8 path. */
305 const char *filename) /* Must be relative utf8 path. */
307 TDB_DATA db_data;
308 struct locking_key lk;
309 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
310 int orig_num_share_modes = 0;
311 struct locking_data *ld = NULL; /* internal samba db state. */
312 struct share_mode_entry *shares = NULL;
313 uint8 *new_data_p = NULL;
314 size_t new_data_size = 0;
315 int err = 0;
316 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
318 if (err) {
319 return -1;
322 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
323 if (!db_data.dptr) {
324 /* We must create the entry. */
325 db_data.dptr = (uint8 *)malloc(
326 sizeof(struct locking_data) +
327 sizeof(struct share_mode_entry) +
328 strlen(sharepath) + 1 +
329 strlen(filename) + 1);
330 if (!db_data.dptr) {
331 return -1;
333 ld = (struct locking_data *)db_data.dptr;
334 memset(ld, '\0', sizeof(struct locking_data));
335 ld->u.s.num_share_mode_entries = 1;
336 ld->u.s.num_delete_token_entries = 0;
337 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
338 create_share_mode_entry(shares, new_entry, name_hash);
340 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
341 sharepath,
342 strlen(sharepath) + 1);
343 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
344 strlen(sharepath) + 1,
345 filename,
346 strlen(filename) + 1);
348 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
349 strlen(sharepath) + 1 +
350 strlen(filename) + 1;
351 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) != 0) {
352 free(db_data.dptr);
353 return -1;
355 free(db_data.dptr);
356 return 0;
359 /* Entry exists, we must add a new entry. */
360 new_data_p = (uint8 *)malloc(
361 db_data.dsize + sizeof(struct share_mode_entry));
362 if (!new_data_p) {
363 free(db_data.dptr);
364 return -1;
367 ld = (struct locking_data *)db_data.dptr;
368 orig_num_share_modes = ld->u.s.num_share_mode_entries;
370 /* Copy the original data. */
371 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
373 /* Add in the new share mode */
374 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
375 (orig_num_share_modes * sizeof(struct share_mode_entry)));
377 create_share_mode_entry(shares, new_entry, name_hash);
379 ld = (struct locking_data *)new_data_p;
380 ld->u.s.num_share_mode_entries++;
382 /* Append the original delete_tokens and filenames. */
383 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
384 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
385 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
387 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
389 free(db_data.dptr);
391 db_data.dptr = new_data_p;
392 db_data.dsize = new_data_size;
394 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
395 free(db_data.dptr);
396 return -1;
398 free(db_data.dptr);
399 return 0;
403 * Create an entry in the Samba share mode db. Original interface - doesn't
404 * Distinguish between share path and filename. Fudge this by using a
405 * sharepath of / and a relative filename of (filename+1).
408 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
409 uint64_t dev,
410 uint64_t ino,
411 uint64_t extid,
412 const struct smb_share_mode_entry *new_entry,
413 const char *filename) /* Must be absolute utf8 path. */
415 if (*filename != '/') {
416 abort();
418 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
419 "/", &filename[1]);
422 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
423 uint64_t dev,
424 uint64_t ino,
425 uint64_t extid,
426 const struct smb_share_mode_entry *del_entry)
428 TDB_DATA db_data;
429 struct locking_key lk;
430 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
431 int orig_num_share_modes = 0;
432 struct locking_data *ld = NULL; /* internal samba db state. */
433 struct share_mode_entry *shares = NULL;
434 uint8 *new_data_p = NULL;
435 size_t remaining_size = 0;
436 size_t i, num_share_modes;
437 const uint8 *remaining_ptr = NULL;
439 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
440 if (!db_data.dptr) {
441 return -1; /* Error - missing entry ! */
444 ld = (struct locking_data *)db_data.dptr;
445 orig_num_share_modes = ld->u.s.num_share_mode_entries;
446 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
448 if (orig_num_share_modes == 1) {
449 /* Only one entry - better be ours... */
450 if (!share_mode_entry_equal(del_entry, shares)) {
451 /* Error ! We can't delete someone else's entry ! */
452 free(db_data.dptr);
453 return -1;
455 /* It's ours - just remove the entire record. */
456 free(db_data.dptr);
457 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
460 /* More than one - allocate a new record minus the one we'll delete. */
461 new_data_p = (uint8 *)malloc(
462 db_data.dsize - sizeof(struct share_mode_entry));
463 if (!new_data_p) {
464 free(db_data.dptr);
465 return -1;
468 /* Copy the header. */
469 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
471 num_share_modes = 0;
472 for (i = 0; i < orig_num_share_modes; i++) {
473 struct share_mode_entry *share = &shares[i];
474 struct server_id pid = share->pid;
476 /* Check this process really exists. */
477 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
478 continue; /* No longer exists. */
481 if (share_mode_entry_equal(del_entry, share)) {
482 continue; /* This is our delete taget. */
485 memcpy(new_data_p + sizeof(struct locking_data) +
486 (num_share_modes * sizeof(struct share_mode_entry)),
487 share, sizeof(struct share_mode_entry) );
489 num_share_modes++;
492 if (num_share_modes == 0) {
493 /* None left after pruning. Delete record. */
494 free(db_data.dptr);
495 free(new_data_p);
496 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
499 /* Copy any delete tokens plus the terminating filenames. */
500 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
501 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
503 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
504 remaining_ptr,
505 remaining_size);
507 free(db_data.dptr);
509 db_data.dptr = new_data_p;
511 /* Re-save smaller record. */
512 ld = (struct locking_data *)db_data.dptr;
513 ld->u.s.num_share_mode_entries = num_share_modes;
515 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
517 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
518 free(db_data.dptr);
519 return -1;
521 free(db_data.dptr);
522 return 0;
525 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
526 uint64_t dev,
527 uint64_t ino,
528 uint64_t extid,
529 const struct smb_share_mode_entry *set_entry,
530 const struct smb_share_mode_entry *new_entry)
532 TDB_DATA db_data;
533 struct locking_key lk;
534 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
535 int num_share_modes = 0;
536 struct locking_data *ld = NULL; /* internal samba db state. */
537 struct share_mode_entry *shares = NULL;
538 size_t i;
539 int found_entry = 0;
541 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
542 if (!db_data.dptr) {
543 return -1; /* Error - missing entry ! */
546 ld = (struct locking_data *)db_data.dptr;
547 num_share_modes = ld->u.s.num_share_mode_entries;
548 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
550 for (i = 0; i < num_share_modes; i++) {
551 struct share_mode_entry *share = &shares[i];
552 struct server_id pid = share->pid;
554 /* Check this process really exists. */
555 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
556 continue; /* No longer exists. */
559 if (share_mode_entry_equal(set_entry, share)) {
560 create_share_mode_entry(share, new_entry, share->name_hash);
561 found_entry = 1;
562 break;
566 if (!found_entry) {
567 free(db_data.dptr);
568 return -1;
571 /* Save modified data. */
572 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
573 free(db_data.dptr);
574 return -1;
576 free(db_data.dptr);
577 return 0;