s3-winbindd: Use the correct enums for samr_QueryDomainInfo.
[Samba.git] / source3 / libsmb / smb_share_modes.c
blobbf3250bab4420870558fb098aa71d58fa3eff426
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, uint64_t extid,
42 const struct smb_share_mode_entry *new_entry,
43 const char *sharepath, const char *filename);
45 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
47 return (p1->pid == p2->pid);
50 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
52 return proc->pid;
56 * open/close sharemode database.
59 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
61 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
63 if (!smb_db) {
64 return NULL;
67 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
69 smb_db->smb_tdb = tdb_open(db_path,
70 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
71 O_RDWR|O_CREAT,
72 0644);
74 if (!smb_db->smb_tdb) {
75 free(smb_db);
76 return NULL;
79 /* Should check that this is the correct version.... */
80 return smb_db;
83 /* key and data records in the tdb locking database */
84 struct locking_key {
85 SMB_DEV_T dev;
86 SMB_INO_T inode;
87 uint64_t extid;
90 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
92 int ret = tdb_close(db_ctx->smb_tdb);
93 free(db_ctx);
94 return ret;
97 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
98 uint64_t ino, uint64_t extid)
100 TDB_DATA ld;
102 memset(lk, '\0', sizeof(*lk));
103 lk->dev = (SMB_DEV_T)dev;
104 lk->inode = (SMB_INO_T)ino;
105 lk->extid = extid;
106 ld.dptr = (uint8 *)lk;
107 ld.dsize = sizeof(*lk);
108 return ld;
112 * lock/unlock entry in sharemode database.
115 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
116 uint64_t dev,
117 uint64_t ino,
118 uint64_t extid)
120 struct locking_key lk;
121 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
122 extid));
125 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
126 uint64_t dev,
127 uint64_t ino,
128 uint64_t extid)
130 struct locking_key lk;
131 return tdb_chainunlock(db_ctx->smb_tdb,
132 get_locking_key(&lk, dev, ino, extid));
136 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
139 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
140 const struct share_mode_entry *entry)
142 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
143 e_entry->file_id == (uint32_t)entry->share_file_id &&
144 e_entry->open_time.tv_sec == entry->time.tv_sec &&
145 e_entry->open_time.tv_usec == entry->time.tv_usec &&
146 e_entry->share_access == (uint32_t)entry->share_access &&
147 e_entry->access_mask == (uint32_t)entry->access_mask &&
148 e_entry->dev == entry->id.devid &&
149 e_entry->ino == entry->id.inode &&
150 e_entry->extid == entry->id.extid);
154 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
157 static void create_share_mode_entry(struct share_mode_entry *out,
158 const struct smb_share_mode_entry *in,
159 uint32_t name_hash)
161 memset(out, '\0', sizeof(struct share_mode_entry));
163 out->pid = in->pid;
164 out->share_file_id = (unsigned long)in->file_id;
165 out->time.tv_sec = in->open_time.tv_sec;
166 out->time.tv_usec = in->open_time.tv_usec;
167 out->share_access = in->share_access;
168 out->access_mask = in->access_mask;
169 out->id.devid = in->dev;
170 out->id.inode = in->ino;
171 out->id.extid = in->extid;
172 out->uid = (uint32)geteuid();
173 out->flags = 0;
174 out->name_hash = name_hash;
178 * Return the current share mode list for an open file.
179 * This uses similar (but simplified) logic to locking/locking.c
182 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
183 uint64_t dev,
184 uint64_t ino,
185 uint64_t extid,
186 struct smb_share_mode_entry **pp_list,
187 unsigned char *p_delete_on_close)
189 struct locking_key lk;
190 TDB_DATA db_data;
191 struct smb_share_mode_entry *list = NULL;
192 int num_share_modes = 0;
193 struct locking_data *ld = NULL; /* internal samba db state. */
194 struct share_mode_entry *shares = NULL;
195 size_t i;
196 int list_num;
198 *pp_list = NULL;
199 *p_delete_on_close = 0;
201 db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
202 extid));
203 if (!db_data.dptr) {
204 return 0;
207 ld = (struct locking_data *)db_data.dptr;
208 num_share_modes = ld->u.s.num_share_mode_entries;
210 if (!num_share_modes) {
211 free(db_data.dptr);
212 return 0;
215 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
216 if (!list) {
217 free(db_data.dptr);
218 return -1;
221 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
223 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
225 list_num = 0;
226 for (i = 0; i < num_share_modes; i++) {
227 struct share_mode_entry *share = &shares[i];
228 struct smb_share_mode_entry *sme = &list[list_num];
229 struct server_id pid = share->pid;
231 /* Check this process really exists. */
232 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
233 continue; /* No longer exists. */
236 /* Ignore deferred open entries. */
237 if (share->op_type == DEFERRED_OPEN_ENTRY) {
238 continue;
241 /* Copy into the external list. */
242 sme->dev = share->id.devid;
243 sme->ino = share->id.inode;
244 sme->extid = share->id.extid;
245 sme->share_access = (uint32_t)share->share_access;
246 sme->access_mask = (uint32_t)share->access_mask;
247 sme->open_time.tv_sec = share->time.tv_sec;
248 sme->open_time.tv_usec = share->time.tv_usec;
249 sme->file_id = (uint32_t)share->share_file_id;
250 sme->pid = share->pid;
251 list_num++;
254 if (list_num == 0) {
255 free(db_data.dptr);
256 free(list);
257 return 0;
260 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
261 *pp_list = list;
262 free(db_data.dptr);
263 return list_num;
266 static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
268 TDB_DATA key;
269 char *fullpath = NULL;
270 size_t sharepath_size = strlen(sharepath);
271 size_t filename_size = strlen(filename);
272 uint32_t name_hash;
274 *err = 0;
275 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
276 if (fullpath == NULL) {
277 *err = 1;
278 return 0;
280 memcpy(fullpath, sharepath, sharepath_size);
281 fullpath[sharepath_size] = '/';
282 memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
284 key.dptr = (uint8_t *)fullpath;
285 key.dsize = strlen(fullpath) + 1;
286 name_hash = tdb_jenkins_hash(&key);
287 free(fullpath);
288 return name_hash;
292 * Create an entry in the Samba share mode db.
295 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
296 uint64_t dev,
297 uint64_t ino,
298 uint64_t extid,
299 const struct smb_share_mode_entry *new_entry,
300 const char *sharepath, /* Must be absolute utf8 path. */
301 const char *filename) /* Must be relative utf8 path. */
303 TDB_DATA db_data;
304 struct locking_key lk;
305 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
306 int orig_num_share_modes = 0;
307 struct locking_data *ld = NULL; /* internal samba db state. */
308 struct share_mode_entry *shares = NULL;
309 uint8 *new_data_p = NULL;
310 size_t new_data_size = 0;
311 int err = 0;
312 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
314 if (err) {
315 return -1;
318 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
319 if (!db_data.dptr) {
320 /* We must create the entry. */
321 db_data.dptr = (uint8 *)malloc(
322 sizeof(struct locking_data) +
323 sizeof(struct share_mode_entry) +
324 strlen(sharepath) + 1 +
325 strlen(filename) + 1);
326 if (!db_data.dptr) {
327 return -1;
329 ld = (struct locking_data *)db_data.dptr;
330 memset(ld, '\0', sizeof(struct locking_data));
331 ld->u.s.num_share_mode_entries = 1;
332 ld->u.s.num_delete_token_entries = 0;
333 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
334 create_share_mode_entry(shares, new_entry, name_hash);
336 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
337 sharepath,
338 strlen(sharepath) + 1);
339 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
340 strlen(sharepath) + 1,
341 filename,
342 strlen(filename) + 1);
344 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
345 strlen(sharepath) + 1 +
346 strlen(filename) + 1;
347 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
348 free(db_data.dptr);
349 return -1;
351 free(db_data.dptr);
352 return 0;
355 /* Entry exists, we must add a new entry. */
356 new_data_p = (uint8 *)malloc(
357 db_data.dsize + sizeof(struct share_mode_entry));
358 if (!new_data_p) {
359 free(db_data.dptr);
360 return -1;
363 ld = (struct locking_data *)db_data.dptr;
364 orig_num_share_modes = ld->u.s.num_share_mode_entries;
366 /* Copy the original data. */
367 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
369 /* Add in the new share mode */
370 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
371 (orig_num_share_modes * sizeof(struct share_mode_entry)));
373 create_share_mode_entry(shares, new_entry, name_hash);
375 ld = (struct locking_data *)new_data_p;
376 ld->u.s.num_share_mode_entries++;
378 /* Append the original delete_tokens and filenames. */
379 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
380 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
381 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
383 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
385 free(db_data.dptr);
387 db_data.dptr = new_data_p;
388 db_data.dsize = new_data_size;
390 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
391 free(db_data.dptr);
392 return -1;
394 free(db_data.dptr);
395 return 0;
399 * Create an entry in the Samba share mode db. Original interface - doesn't
400 * Distinguish between share path and filename. Fudge this by using a
401 * sharepath of / and a relative filename of (filename+1).
404 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
405 uint64_t dev,
406 uint64_t ino,
407 uint64_t extid,
408 const struct smb_share_mode_entry *new_entry,
409 const char *filename) /* Must be absolute utf8 path. */
411 if (*filename != '/') {
412 abort();
414 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
415 "/", &filename[1]);
418 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
419 uint64_t dev,
420 uint64_t ino,
421 uint64_t extid,
422 const struct smb_share_mode_entry *del_entry)
424 TDB_DATA db_data;
425 struct locking_key lk;
426 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
427 int orig_num_share_modes = 0;
428 struct locking_data *ld = NULL; /* internal samba db state. */
429 struct share_mode_entry *shares = NULL;
430 uint8 *new_data_p = NULL;
431 size_t remaining_size = 0;
432 size_t i, num_share_modes;
433 const uint8 *remaining_ptr = NULL;
435 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
436 if (!db_data.dptr) {
437 return -1; /* Error - missing entry ! */
440 ld = (struct locking_data *)db_data.dptr;
441 orig_num_share_modes = ld->u.s.num_share_mode_entries;
442 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
444 if (orig_num_share_modes == 1) {
445 /* Only one entry - better be ours... */
446 if (!share_mode_entry_equal(del_entry, shares)) {
447 /* Error ! We can't delete someone else's entry ! */
448 free(db_data.dptr);
449 return -1;
451 /* It's ours - just remove the entire record. */
452 free(db_data.dptr);
453 return tdb_delete(db_ctx->smb_tdb, locking_key);
456 /* More than one - allocate a new record minus the one we'll delete. */
457 new_data_p = (uint8 *)malloc(
458 db_data.dsize - sizeof(struct share_mode_entry));
459 if (!new_data_p) {
460 free(db_data.dptr);
461 return -1;
464 /* Copy the header. */
465 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
467 num_share_modes = 0;
468 for (i = 0; i < orig_num_share_modes; i++) {
469 struct share_mode_entry *share = &shares[i];
470 struct server_id pid = share->pid;
472 /* Check this process really exists. */
473 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
474 continue; /* No longer exists. */
477 if (share_mode_entry_equal(del_entry, share)) {
478 continue; /* This is our delete taget. */
481 memcpy(new_data_p + sizeof(struct locking_data) +
482 (num_share_modes * sizeof(struct share_mode_entry)),
483 share, sizeof(struct share_mode_entry) );
485 num_share_modes++;
488 if (num_share_modes == 0) {
489 /* None left after pruning. Delete record. */
490 free(db_data.dptr);
491 free(new_data_p);
492 return tdb_delete(db_ctx->smb_tdb, locking_key);
495 /* Copy any delete tokens plus the terminating filenames. */
496 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
497 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
499 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
500 remaining_ptr,
501 remaining_size);
503 free(db_data.dptr);
505 db_data.dptr = new_data_p;
507 /* Re-save smaller record. */
508 ld = (struct locking_data *)db_data.dptr;
509 ld->u.s.num_share_mode_entries = num_share_modes;
511 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
513 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
514 free(db_data.dptr);
515 return -1;
517 free(db_data.dptr);
518 return 0;
521 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
522 uint64_t dev,
523 uint64_t ino,
524 uint64_t extid,
525 const struct smb_share_mode_entry *set_entry,
526 const struct smb_share_mode_entry *new_entry)
528 TDB_DATA db_data;
529 struct locking_key lk;
530 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
531 int num_share_modes = 0;
532 struct locking_data *ld = NULL; /* internal samba db state. */
533 struct share_mode_entry *shares = NULL;
534 size_t i;
535 int found_entry = 0;
537 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
538 if (!db_data.dptr) {
539 return -1; /* Error - missing entry ! */
542 ld = (struct locking_data *)db_data.dptr;
543 num_share_modes = ld->u.s.num_share_mode_entries;
544 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
546 for (i = 0; i < num_share_modes; i++) {
547 struct share_mode_entry *share = &shares[i];
548 struct server_id pid = share->pid;
550 /* Check this process really exists. */
551 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
552 continue; /* No longer exists. */
555 if (share_mode_entry_equal(set_entry, share)) {
556 create_share_mode_entry(share, new_entry, share->name_hash);
557 found_entry = 1;
558 break;
562 if (!found_entry) {
563 free(db_data.dptr);
564 return -1;
567 /* Save modified data. */
568 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
569 free(db_data.dptr);
570 return -1;
572 free(db_data.dptr);
573 return 0;