s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / libsmb / smb_share_modes.c
blob7c0a6d2a4e67b046e9641d6dffe89cbaf7f6176b
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_compat.h"
31 #include <ccan/hash/hash.h>
33 /* Database context handle. */
34 struct smbdb_ctx {
35 TDB_CONTEXT *smb_tdb;
38 /* Remove the paranoid malloc checker. */
39 #ifdef malloc
40 #undef malloc
41 #endif
43 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
44 uint64_t ino, uint64_t extid,
45 const struct smb_share_mode_entry *new_entry,
46 const char *sharepath, const char *filename);
48 static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
50 return (p1->pid == p2->pid);
53 static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
55 return proc->pid;
59 * open/close sharemode database.
62 struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
64 struct smbdb_ctx *smb_db = (struct smbdb_ctx *)malloc(sizeof(struct smbdb_ctx));
66 if (!smb_db) {
67 return NULL;
70 memset(smb_db, '\0', sizeof(struct smbdb_ctx));
72 /* FIXME: We should *never* open a tdb without logging! */
73 smb_db->smb_tdb = tdb_open_compat(db_path,
74 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
75 O_RDWR|O_CREAT,
76 0644,
77 NULL, NULL);
79 if (!smb_db->smb_tdb) {
80 free(smb_db);
81 return NULL;
84 /* Should check that this is the correct version.... */
85 return smb_db;
88 /* key and data records in the tdb locking database */
89 struct locking_key {
90 SMB_DEV_T dev;
91 SMB_INO_T inode;
92 uint64_t extid;
95 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
97 int ret = tdb_close(db_ctx->smb_tdb);
98 free(db_ctx);
99 return ret;
102 static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
103 uint64_t ino, uint64_t extid)
105 TDB_DATA ld;
107 memset(lk, '\0', sizeof(*lk));
108 lk->dev = (SMB_DEV_T)dev;
109 lk->inode = (SMB_INO_T)ino;
110 lk->extid = extid;
111 ld.dptr = (uint8 *)lk;
112 ld.dsize = sizeof(*lk);
113 return ld;
117 * lock/unlock entry in sharemode database.
120 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
121 uint64_t dev,
122 uint64_t ino,
123 uint64_t extid)
125 struct locking_key lk;
126 return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
127 extid)) == 0 ? 0 : -1;
130 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
131 uint64_t dev,
132 uint64_t ino,
133 uint64_t extid)
135 struct locking_key lk;
136 tdb_chainunlock(db_ctx->smb_tdb,
137 get_locking_key(&lk, dev, ino, extid));
138 return 0;
142 * Check if an external smb_share_mode_entry and an internal share_mode entry match.
145 static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
146 const struct share_mode_entry *entry)
148 return (sharemodes_procid_equal(&e_entry->pid, &entry->pid) &&
149 e_entry->file_id == (uint32_t)entry->share_file_id &&
150 e_entry->open_time.tv_sec == entry->time.tv_sec &&
151 e_entry->open_time.tv_usec == entry->time.tv_usec &&
152 e_entry->share_access == (uint32_t)entry->share_access &&
153 e_entry->access_mask == (uint32_t)entry->access_mask &&
154 e_entry->dev == entry->id.devid &&
155 e_entry->ino == entry->id.inode &&
156 e_entry->extid == entry->id.extid);
160 * Create an internal Samba share_mode entry from an external smb_share_mode_entry.
163 static void create_share_mode_entry(struct share_mode_entry *out,
164 const struct smb_share_mode_entry *in,
165 uint32_t name_hash)
167 memset(out, '\0', sizeof(struct share_mode_entry));
169 out->pid = in->pid;
170 out->share_file_id = (unsigned long)in->file_id;
171 out->time.tv_sec = in->open_time.tv_sec;
172 out->time.tv_usec = in->open_time.tv_usec;
173 out->share_access = in->share_access;
174 out->access_mask = in->access_mask;
175 out->id.devid = in->dev;
176 out->id.inode = in->ino;
177 out->id.extid = in->extid;
178 out->uid = (uint32)geteuid();
179 out->flags = 0;
180 out->name_hash = name_hash;
184 * Return the current share mode list for an open file.
185 * This uses similar (but simplified) logic to locking/locking.c
188 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
189 uint64_t dev,
190 uint64_t ino,
191 uint64_t extid,
192 struct smb_share_mode_entry **pp_list,
193 unsigned char *p_delete_on_close)
195 struct locking_key lk;
196 TDB_DATA db_data;
197 struct smb_share_mode_entry *list = NULL;
198 int num_share_modes = 0;
199 struct locking_data *ld = NULL; /* internal samba db state. */
200 struct share_mode_entry *shares = NULL;
201 size_t i;
202 int list_num;
204 *pp_list = NULL;
205 *p_delete_on_close = 0;
207 db_data = tdb_fetch_compat(db_ctx->smb_tdb,
208 get_locking_key(&lk, dev, ino, extid));
209 if (!db_data.dptr) {
210 return 0;
213 ld = (struct locking_data *)db_data.dptr;
214 num_share_modes = ld->u.s.num_share_mode_entries;
216 if (!num_share_modes) {
217 free(db_data.dptr);
218 return 0;
221 list = (struct smb_share_mode_entry *)malloc(sizeof(struct smb_share_mode_entry)*num_share_modes);
222 if (!list) {
223 free(db_data.dptr);
224 return -1;
227 memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
229 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
231 list_num = 0;
232 for (i = 0; i < num_share_modes; i++) {
233 struct share_mode_entry *share = &shares[i];
234 struct smb_share_mode_entry *sme = &list[list_num];
235 struct server_id pid = share->pid;
237 /* Check this process really exists. */
238 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
239 continue; /* No longer exists. */
242 /* Ignore deferred open entries. */
243 if (share->op_type == DEFERRED_OPEN_ENTRY) {
244 continue;
247 /* Copy into the external list. */
248 sme->dev = share->id.devid;
249 sme->ino = share->id.inode;
250 sme->extid = share->id.extid;
251 sme->share_access = (uint32_t)share->share_access;
252 sme->access_mask = (uint32_t)share->access_mask;
253 sme->open_time.tv_sec = share->time.tv_sec;
254 sme->open_time.tv_usec = share->time.tv_usec;
255 sme->file_id = (uint32_t)share->share_file_id;
256 sme->pid = share->pid;
257 list_num++;
260 if (list_num == 0) {
261 free(db_data.dptr);
262 free(list);
263 return 0;
266 *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
267 *pp_list = list;
268 free(db_data.dptr);
269 return list_num;
272 static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
274 char *fullpath = NULL;
275 size_t sharepath_size = strlen(sharepath);
276 size_t filename_size = strlen(filename);
277 uint32_t name_hash;
279 *err = 0;
280 fullpath = (char *)malloc(sharepath_size + filename_size + 2);
281 if (fullpath == NULL) {
282 *err = 1;
283 return 0;
285 memcpy(fullpath, sharepath, sharepath_size);
286 fullpath[sharepath_size] = '/';
287 memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
289 name_hash = hash(fullpath, strlen(fullpath) + 1, 0);
290 free(fullpath);
291 return name_hash;
295 * Create an entry in the Samba share mode db.
298 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
299 uint64_t dev,
300 uint64_t ino,
301 uint64_t extid,
302 const struct smb_share_mode_entry *new_entry,
303 const char *sharepath, /* Must be absolute utf8 path. */
304 const char *filename) /* Must be relative utf8 path. */
306 TDB_DATA db_data;
307 struct locking_key lk;
308 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
309 int orig_num_share_modes = 0;
310 struct locking_data *ld = NULL; /* internal samba db state. */
311 struct share_mode_entry *shares = NULL;
312 uint8 *new_data_p = NULL;
313 size_t new_data_size = 0;
314 int err = 0;
315 uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
317 if (err) {
318 return -1;
321 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
322 if (!db_data.dptr) {
323 /* We must create the entry. */
324 db_data.dptr = (uint8 *)malloc(
325 sizeof(struct locking_data) +
326 sizeof(struct share_mode_entry) +
327 strlen(sharepath) + 1 +
328 strlen(filename) + 1);
329 if (!db_data.dptr) {
330 return -1;
332 ld = (struct locking_data *)db_data.dptr;
333 memset(ld, '\0', sizeof(struct locking_data));
334 ld->u.s.num_share_mode_entries = 1;
335 ld->u.s.num_delete_token_entries = 0;
336 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
337 create_share_mode_entry(shares, new_entry, name_hash);
339 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
340 sharepath,
341 strlen(sharepath) + 1);
342 memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
343 strlen(sharepath) + 1,
344 filename,
345 strlen(filename) + 1);
347 db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
348 strlen(sharepath) + 1 +
349 strlen(filename) + 1;
350 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) != 0) {
351 free(db_data.dptr);
352 return -1;
354 free(db_data.dptr);
355 return 0;
358 /* Entry exists, we must add a new entry. */
359 new_data_p = (uint8 *)malloc(
360 db_data.dsize + sizeof(struct share_mode_entry));
361 if (!new_data_p) {
362 free(db_data.dptr);
363 return -1;
366 ld = (struct locking_data *)db_data.dptr;
367 orig_num_share_modes = ld->u.s.num_share_mode_entries;
369 /* Copy the original data. */
370 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
372 /* Add in the new share mode */
373 shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
374 (orig_num_share_modes * sizeof(struct share_mode_entry)));
376 create_share_mode_entry(shares, new_entry, name_hash);
378 ld = (struct locking_data *)new_data_p;
379 ld->u.s.num_share_mode_entries++;
381 /* Append the original delete_tokens and filenames. */
382 memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
383 db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
384 db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
386 new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
388 free(db_data.dptr);
390 db_data.dptr = new_data_p;
391 db_data.dsize = new_data_size;
393 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
394 free(db_data.dptr);
395 return -1;
397 free(db_data.dptr);
398 return 0;
402 * Create an entry in the Samba share mode db. Original interface - doesn't
403 * Distinguish between share path and filename. Fudge this by using a
404 * sharepath of / and a relative filename of (filename+1).
407 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
408 uint64_t dev,
409 uint64_t ino,
410 uint64_t extid,
411 const struct smb_share_mode_entry *new_entry,
412 const char *filename) /* Must be absolute utf8 path. */
414 if (*filename != '/') {
415 abort();
417 return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
418 "/", &filename[1]);
421 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
422 uint64_t dev,
423 uint64_t ino,
424 uint64_t extid,
425 const struct smb_share_mode_entry *del_entry)
427 TDB_DATA db_data;
428 struct locking_key lk;
429 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
430 int orig_num_share_modes = 0;
431 struct locking_data *ld = NULL; /* internal samba db state. */
432 struct share_mode_entry *shares = NULL;
433 uint8 *new_data_p = NULL;
434 size_t remaining_size = 0;
435 size_t i, num_share_modes;
436 const uint8 *remaining_ptr = NULL;
438 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
439 if (!db_data.dptr) {
440 return -1; /* Error - missing entry ! */
443 ld = (struct locking_data *)db_data.dptr;
444 orig_num_share_modes = ld->u.s.num_share_mode_entries;
445 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
447 if (orig_num_share_modes == 1) {
448 /* Only one entry - better be ours... */
449 if (!share_mode_entry_equal(del_entry, shares)) {
450 /* Error ! We can't delete someone else's entry ! */
451 free(db_data.dptr);
452 return -1;
454 /* It's ours - just remove the entire record. */
455 free(db_data.dptr);
456 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
459 /* More than one - allocate a new record minus the one we'll delete. */
460 new_data_p = (uint8 *)malloc(
461 db_data.dsize - sizeof(struct share_mode_entry));
462 if (!new_data_p) {
463 free(db_data.dptr);
464 return -1;
467 /* Copy the header. */
468 memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
470 num_share_modes = 0;
471 for (i = 0; i < orig_num_share_modes; i++) {
472 struct share_mode_entry *share = &shares[i];
473 struct server_id pid = share->pid;
475 /* Check this process really exists. */
476 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
477 continue; /* No longer exists. */
480 if (share_mode_entry_equal(del_entry, share)) {
481 continue; /* This is our delete taget. */
484 memcpy(new_data_p + sizeof(struct locking_data) +
485 (num_share_modes * sizeof(struct share_mode_entry)),
486 share, sizeof(struct share_mode_entry) );
488 num_share_modes++;
491 if (num_share_modes == 0) {
492 /* None left after pruning. Delete record. */
493 free(db_data.dptr);
494 free(new_data_p);
495 return tdb_delete(db_ctx->smb_tdb, locking_key) ? -1 : 0;
498 /* Copy any delete tokens plus the terminating filenames. */
499 remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
500 remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
502 memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
503 remaining_ptr,
504 remaining_size);
506 free(db_data.dptr);
508 db_data.dptr = new_data_p;
510 /* Re-save smaller record. */
511 ld = (struct locking_data *)db_data.dptr;
512 ld->u.s.num_share_mode_entries = num_share_modes;
514 db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
516 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
517 free(db_data.dptr);
518 return -1;
520 free(db_data.dptr);
521 return 0;
524 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
525 uint64_t dev,
526 uint64_t ino,
527 uint64_t extid,
528 const struct smb_share_mode_entry *set_entry,
529 const struct smb_share_mode_entry *new_entry)
531 TDB_DATA db_data;
532 struct locking_key lk;
533 TDB_DATA locking_key = get_locking_key(&lk, dev, ino, extid);
534 int num_share_modes = 0;
535 struct locking_data *ld = NULL; /* internal samba db state. */
536 struct share_mode_entry *shares = NULL;
537 size_t i;
538 int found_entry = 0;
540 db_data = tdb_fetch_compat(db_ctx->smb_tdb, locking_key);
541 if (!db_data.dptr) {
542 return -1; /* Error - missing entry ! */
545 ld = (struct locking_data *)db_data.dptr;
546 num_share_modes = ld->u.s.num_share_mode_entries;
547 shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
549 for (i = 0; i < num_share_modes; i++) {
550 struct share_mode_entry *share = &shares[i];
551 struct server_id pid = share->pid;
553 /* Check this process really exists. */
554 if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
555 continue; /* No longer exists. */
558 if (share_mode_entry_equal(set_entry, share)) {
559 create_share_mode_entry(share, new_entry, share->name_hash);
560 found_entry = 1;
561 break;
565 if (!found_entry) {
566 free(db_data.dptr);
567 return -1;
570 /* Save modified data. */
571 if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) != 0) {
572 free(db_data.dptr);
573 return -1;
575 free(db_data.dptr);
576 return 0;