nbt: fix WinXP S3 domain join: alignment of nbt_netlogon_response_from_pdc
[Samba.git] / source3 / libsmb / smb_share_modes.c
blob76e63e6968987aeb31c6b16d30ba38b9b6fc9c1e
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"
30 #include <tdb.h>
32 /* Database context handle. */
33 struct smbdb_ctx {
34 TDB_CONTEXT *smb_tdb;
37 /* Remove the paranoid malloc checker. */
38 #ifdef malloc
39 #undef malloc
40 #endif
42 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
43 uint64_t ino, uint64_t extid,
44 const struct smb_share_mode_entry *new_entry,
45 const char *sharepath, const char *filename);
47 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
49 return (p1->pid == p2->pid);
52 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
54 return proc->pid;
58 * open/close sharemode database.
61 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
63 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
65 if (!smb_db) {
66 return NULL;
69 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
71 smb_db->smb_tdb = tdb_open(db_path,
72 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
73 O_RDWR|O_CREAT,
74 0644);
76 if (!smb_db->smb_tdb) {
77 free(smb_db);
78 return NULL;
81 /* Should check that this is the correct version.... */
82 return smb_db;
85 /* key and data records in the tdb locking database */
86 struct locking_key {
87 SMB_DEV_T dev;
88 SMB_INO_T inode;
89 uint64_t extid;
92 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
94 int ret = tdb_close(db_ctx->smb_tdb);
95 free(db_ctx);
96 return ret;
99 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
100 uint64_t ino, uint64_t extid)
102 TDB_DATA ld;
104 memset(lk, '\0', sizeof(*lk));
105 lk->dev = (SMB_DEV_T)dev;
106 lk->inode = (SMB_INO_T)ino;
107 lk->extid = extid;
108 ld.dptr = (uint8 *)lk;
109 ld.dsize = sizeof(*lk);
110 return ld;
114 * lock/unlock entry in sharemode database.
117 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
118 uint64_t dev,
119 uint64_t ino,
120 uint64_t extid)
122 struct locking_key lk;
123 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
124 extid));
127 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
128 uint64_t dev,
129 uint64_t ino,
130 uint64_t extid)
132 struct locking_key lk;
133 return tdb_chainunlock(db_ctx->smb_tdb,
134 get_locking_key(&lk, dev, ino, extid));
138 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
141 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
142 const struct share_mode_entry *entry)
144 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
145 e_entry->file_id == (uint32_t)entry->share_file_id &&
146 e_entry->open_time.tv_sec == entry->time.tv_sec &&
147 e_entry->open_time.tv_usec == entry->time.tv_usec &&
148 e_entry->share_access == (uint32_t)entry->share_access &&
149 e_entry->access_mask == (uint32_t)entry->access_mask &&
150 e_entry->dev == entry->id.devid &&
151 e_entry->ino == entry->id.inode &&
152 e_entry->extid == entry->id.extid);
156 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
159 static void create_share_mode_entry(struct share_mode_entry *out,
160 const struct smb_share_mode_entry *in,
161 uint32_t name_hash)
163 memset(out, '\0', sizeof(struct share_mode_entry));
165 out->pid = in->pid;
166 out->share_file_id = (unsigned long)in->file_id;
167 out->time.tv_sec = in->open_time.tv_sec;
168 out->time.tv_usec = in->open_time.tv_usec;
169 out->share_access = in->share_access;
170 out->access_mask = in->access_mask;
171 out->id.devid = in->dev;
172 out->id.inode = in->ino;
173 out->id.extid = in->extid;
174 out->uid = (uint32)geteuid();
175 out->flags = 0;
176 out->name_hash = name_hash;
180 * Return the current share mode list for an open file.
181 * This uses similar (but simplified) logic to locking/locking.c
184 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
185 uint64_t dev,
186 uint64_t ino,
187 uint64_t extid,
188 struct smb_share_mode_entry **pp_list,
189 unsigned char *p_delete_on_close)
191 struct locking_key lk;
192 TDB_DATA db_data;
193 struct smb_share_mode_entry *list = NULL;
194 int num_share_modes = 0;
195 struct locking_data *ld = NULL; /* internal samba db state. */
196 struct share_mode_entry *shares = NULL;
197 size_t i;
198 int list_num;
200 *pp_list = NULL;
201 *p_delete_on_close = 0;
203 db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
204 extid));
205 if (!db_data.dptr) {
206 return 0;
209 ld = (struct locking_data *)db_data.dptr;
210 num_share_modes = ld->u.s.num_share_mode_entries;
212 if (!num_share_modes) {
213 free(db_data.dptr);
214 return 0;
217 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
218 if (!list) {
219 free(db_data.dptr);
220 return -1;
223 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
225 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
227 list_num = 0;
228 for (i = 0; i < num_share_modes; i++) {
229 struct share_mode_entry *share = &shares[i];
230 struct smb_share_mode_entry *sme = &list[list_num];
231 struct server_id pid = share->pid;
233 /* Check this process really exists. */
234 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
235 continue; /* No longer exists. */
238 /* Ignore deferred open entries. */
239 if (share->op_type == DEFERRED_OPEN_ENTRY) {
240 continue;
243 /* Copy into the external list. */
244 sme->dev = share->id.devid;
245 sme->ino = share->id.inode;
246 sme->extid = share->id.extid;
247 sme->share_access = (uint32_t)share->share_access;
248 sme->access_mask = (uint32_t)share->access_mask;
249 sme->open_time.tv_sec = share->time.tv_sec;
250 sme->open_time.tv_usec = share->time.tv_usec;
251 sme->file_id = (uint32_t)share->share_file_id;
252 sme->pid = share->pid;
253 list_num++;
256 if (list_num == 0) {
257 free(db_data.dptr);
258 free(list);
259 return 0;
262 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
263 *pp_list = list;
264 free(db_data.dptr);
265 return list_num;
268 static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
270 TDB_DATA key;
271 char *fullpath = NULL;
272 size_t sharepath_size = strlen(sharepath);
273 size_t filename_size = strlen(filename);
274 uint32_t name_hash;
276 *err = 0;
277 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
278 if (fullpath == NULL) {
279 *err = 1;
280 return 0;
282 memcpy(fullpath, sharepath, sharepath_size);
283 fullpath[sharepath_size] = '/';
284 memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
286 key.dptr = (uint8_t *)fullpath;
287 key.dsize = strlen(fullpath) + 1;
288 name_hash = tdb_jenkins_hash(&key);
289 free(fullpath);
290 return name_hash;
294 * Create an entry in the Samba share mode db.
297 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
298 uint64_t dev,
299 uint64_t ino,
300 uint64_t extid,
301 const struct smb_share_mode_entry *new_entry,
302 const char *sharepath, /* Must be absolute utf8 path. */
303 const char *filename) /* Must be relative utf8 path. */
305 TDB_DATA db_data;
306 struct locking_key lk;
307 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
308 int orig_num_share_modes = 0;
309 struct locking_data *ld = NULL; /* internal samba db state. */
310 struct share_mode_entry *shares = NULL;
311 uint8 *new_data_p = NULL;
312 size_t new_data_size = 0;
313 int err = 0;
314 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
316 if (err) {
317 return -1;
320 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
321 if (!db_data.dptr) {
322 /* We must create the entry. */
323 db_data.dptr = (uint8 *)malloc(
324 sizeof(struct locking_data) +
325 sizeof(struct share_mode_entry) +
326 strlen(sharepath) + 1 +
327 strlen(filename) + 1);
328 if (!db_data.dptr) {
329 return -1;
331 ld = (struct locking_data *)db_data.dptr;
332 memset(ld, '\0', sizeof(struct locking_data));
333 ld->u.s.num_share_mode_entries = 1;
334 ld->u.s.num_delete_token_entries = 0;
335 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
336 create_share_mode_entry(shares, new_entry, name_hash);
338 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
339 sharepath,
340 strlen(sharepath) + 1);
341 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
342 strlen(sharepath) + 1,
343 filename,
344 strlen(filename) + 1);
346 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
347 strlen(sharepath) + 1 +
348 strlen(filename) + 1;
349 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
350 free(db_data.dptr);
351 return -1;
353 free(db_data.dptr);
354 return 0;
357 /* Entry exists, we must add a new entry. */
358 new_data_p = (uint8 *)malloc(
359 db_data.dsize + sizeof(struct share_mode_entry));
360 if (!new_data_p) {
361 free(db_data.dptr);
362 return -1;
365 ld = (struct locking_data *)db_data.dptr;
366 orig_num_share_modes = ld->u.s.num_share_mode_entries;
368 /* Copy the original data. */
369 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
371 /* Add in the new share mode */
372 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
373 (orig_num_share_modes * sizeof(struct share_mode_entry)));
375 create_share_mode_entry(shares, new_entry, name_hash);
377 ld = (struct locking_data *)new_data_p;
378 ld->u.s.num_share_mode_entries++;
380 /* Append the original delete_tokens and filenames. */
381 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
382 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
383 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
385 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
387 free(db_data.dptr);
389 db_data.dptr = new_data_p;
390 db_data.dsize = new_data_size;
392 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
393 free(db_data.dptr);
394 return -1;
396 free(db_data.dptr);
397 return 0;
401 * Create an entry in the Samba share mode db. Original interface - doesn't
402 * Distinguish between share path and filename. Fudge this by using a
403 * sharepath of / and a relative filename of (filename+1).
406 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
407 uint64_t dev,
408 uint64_t ino,
409 uint64_t extid,
410 const struct smb_share_mode_entry *new_entry,
411 const char *filename) /* Must be absolute utf8 path. */
413 if (*filename != '/') {
414 abort();
416 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
417 "/", &filename[1]);
420 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
421 uint64_t dev,
422 uint64_t ino,
423 uint64_t extid,
424 const struct smb_share_mode_entry *del_entry)
426 TDB_DATA db_data;
427 struct locking_key lk;
428 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
429 int orig_num_share_modes = 0;
430 struct locking_data *ld = NULL; /* internal samba db state. */
431 struct share_mode_entry *shares = NULL;
432 uint8 *new_data_p = NULL;
433 size_t remaining_size = 0;
434 size_t i, num_share_modes;
435 const uint8 *remaining_ptr = NULL;
437 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
438 if (!db_data.dptr) {
439 return -1; /* Error - missing entry ! */
442 ld = (struct locking_data *)db_data.dptr;
443 orig_num_share_modes = ld->u.s.num_share_mode_entries;
444 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
446 if (orig_num_share_modes == 1) {
447 /* Only one entry - better be ours... */
448 if (!share_mode_entry_equal(del_entry, shares)) {
449 /* Error ! We can't delete someone else's entry ! */
450 free(db_data.dptr);
451 return -1;
453 /* It's ours - just remove the entire record. */
454 free(db_data.dptr);
455 return tdb_delete(db_ctx->smb_tdb, locking_key);
458 /* More than one - allocate a new record minus the one we'll delete. */
459 new_data_p = (uint8 *)malloc(
460 db_data.dsize - sizeof(struct share_mode_entry));
461 if (!new_data_p) {
462 free(db_data.dptr);
463 return -1;
466 /* Copy the header. */
467 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
469 num_share_modes = 0;
470 for (i = 0; i < orig_num_share_modes; i++) {
471 struct share_mode_entry *share = &shares[i];
472 struct server_id pid = share->pid;
474 /* Check this process really exists. */
475 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
476 continue; /* No longer exists. */
479 if (share_mode_entry_equal(del_entry, share)) {
480 continue; /* This is our delete taget. */
483 memcpy(new_data_p + sizeof(struct locking_data) +
484 (num_share_modes * sizeof(struct share_mode_entry)),
485 share, sizeof(struct share_mode_entry) );
487 num_share_modes++;
490 if (num_share_modes == 0) {
491 /* None left after pruning. Delete record. */
492 free(db_data.dptr);
493 free(new_data_p);
494 return tdb_delete(db_ctx->smb_tdb, locking_key);
497 /* Copy any delete tokens plus the terminating filenames. */
498 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
499 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
501 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
502 remaining_ptr,
503 remaining_size);
505 free(db_data.dptr);
507 db_data.dptr = new_data_p;
509 /* Re-save smaller record. */
510 ld = (struct locking_data *)db_data.dptr;
511 ld->u.s.num_share_mode_entries = num_share_modes;
513 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
515 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
516 free(db_data.dptr);
517 return -1;
519 free(db_data.dptr);
520 return 0;
523 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
524 uint64_t dev,
525 uint64_t ino,
526 uint64_t extid,
527 const struct smb_share_mode_entry *set_entry,
528 const struct smb_share_mode_entry *new_entry)
530 TDB_DATA db_data;
531 struct locking_key lk;
532 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
533 int num_share_modes = 0;
534 struct locking_data *ld = NULL; /* internal samba db state. */
535 struct share_mode_entry *shares = NULL;
536 size_t i;
537 int found_entry = 0;
539 db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
540 if (!db_data.dptr) {
541 return -1; /* Error - missing entry ! */
544 ld = (struct locking_data *)db_data.dptr;
545 num_share_modes = ld->u.s.num_share_mode_entries;
546 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
548 for (i = 0; i < num_share_modes; i++) {
549 struct share_mode_entry *share = &shares[i];
550 struct server_id pid = share->pid;
552 /* Check this process really exists. */
553 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
554 continue; /* No longer exists. */
557 if (share_mode_entry_equal(set_entry, share)) {
558 create_share_mode_entry(share, new_entry, share->name_hash);
559 found_entry = 1;
560 break;
564 if (!found_entry) {
565 free(db_data.dptr);
566 return -1;
569 /* Save modified data. */
570 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
571 free(db_data.dptr);
572 return -1;
574 free(db_data.dptr);
575 return 0;