s3-includes: only include system/filesys.h when needed.
[vlendec/samba-autobuild/.git] / source3 / libsmb / smb_share_modes.c
index 4c49ecb7bd9489bf22bbb308be89b443c214b204..1a6c2123ed6698f5dc686606a157232a475df035 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Samba share mode database library external interface library.
    Used by non-Samba products needing access to the Samba share mode db.
-                                                                                                                                  
+
    Copyright (C) Jeremy Allison 2005 - 2006
 
    sharemodes_procid functions (C) Copyright (C) Volker Lendecke 2005
@@ -9,40 +9,46 @@
      ** NOTE! The following LGPL license applies to this module only.
      ** This does NOT imply that all of Samba is released
      ** under the LGPL
-                                                                                                                                  
+
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
-                                                                                                                                  
+   version 3 of the License, or (at your option) any later version.
+
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
-                                                                                                                                  
+
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "system/filesys.h"
 #include "smb_share_modes.h"
 
+/* Database context handle. */
+struct smbdb_ctx {
+       TDB_CONTEXT *smb_tdb;
+};
+
 /* Remove the paranoid malloc checker. */
 #ifdef malloc
 #undef malloc
 #endif
 
 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx, uint64_t dev,
-                               uint64_t ino, const struct smb_share_mode_entry *new_entry,
+                               uint64_t ino, uint64_t extid,
+                               const struct smb_share_mode_entry *new_entry,
                                const char *sharepath, const char *filename);
 
-static BOOL sharemodes_procid_equal(const struct process_id *p1, const struct process_id *p2)
+static bool sharemodes_procid_equal(const struct server_id *p1, const struct server_id *p2)
 {
        return (p1->pid == p2->pid);
 }
 
-static pid_t sharemodes_procid_to_pid(const struct process_id *proc)
+static pid_t sharemodes_procid_to_pid(const struct server_id *proc)
 {
        return proc->pid;
 }
@@ -62,7 +68,7 @@ struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
        memset(smb_db, '\0', sizeof(struct smbdb_ctx));
 
        smb_db->smb_tdb = tdb_open(db_path,
-                               0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
+                               0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
                                O_RDWR|O_CREAT,
                                0644);
 
@@ -79,6 +85,7 @@ struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
 struct locking_key {
         SMB_DEV_T dev;
         SMB_INO_T inode;
+       uint64_t extid;
 };
 
 int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
@@ -88,16 +95,17 @@ int smb_share_mode_db_close(struct smbdb_ctx *db_ctx)
        return ret;
 }
 
-static TDB_DATA get_locking_key(uint64_t dev, uint64_t ino)
+static TDB_DATA get_locking_key(struct locking_key *lk, uint64_t dev,
+                               uint64_t ino, uint64_t extid)
 {
-       static struct locking_key lk;
        TDB_DATA ld;
 
-       memset(&lk, '\0', sizeof(struct locking_key));
-       lk.dev = (SMB_DEV_T)dev;
-       lk.inode = (SMB_INO_T)ino;
-       ld.dptr = (char *)&lk;
-       ld.dsize = sizeof(lk);
+       memset(lk, '\0', sizeof(*lk));
+       lk->dev = (SMB_DEV_T)dev;
+       lk->inode = (SMB_INO_T)ino;
+       lk->extid = extid;
+       ld.dptr = (uint8 *)lk;
+       ld.dsize = sizeof(*lk);
        return ld;
 }
 
@@ -107,16 +115,22 @@ static TDB_DATA get_locking_key(uint64_t dev, uint64_t ino)
 
 int smb_lock_share_mode_entry(struct smbdb_ctx *db_ctx,
                                uint64_t dev,
-                               uint64_t ino)
+                               uint64_t ino,
+                               uint64_t extid)
 {
-       return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
+       struct locking_key lk;
+       return tdb_chainlock(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
+                                                             extid));
 }
-                                                                                                                                  
+
 int smb_unlock_share_mode_entry(struct smbdb_ctx *db_ctx,
                                 uint64_t dev,
-                                uint64_t ino)
+                                uint64_t ino,
+                                uint64_t extid)
 {
-       return tdb_chainunlock(db_ctx->smb_tdb, get_locking_key(dev, ino));
+       struct locking_key lk;
+       return tdb_chainunlock(db_ctx->smb_tdb,
+                              get_locking_key(&lk, dev, ino, extid));
 }
 
 /*
@@ -132,8 +146,9 @@ static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
                e_entry->open_time.tv_usec == entry->time.tv_usec &&
                e_entry->share_access == (uint32_t)entry->share_access &&
                e_entry->access_mask == (uint32_t)entry->access_mask &&
-               e_entry->dev == (uint64_t)entry->dev && 
-               e_entry->ino == (uint64_t)entry->inode);
+               e_entry->dev == entry->id.devid && 
+               e_entry->ino == entry->id.inode &&
+               e_entry->extid == entry->id.extid);
 }
 
 /*
@@ -141,7 +156,8 @@ static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
  */
 
 static void create_share_mode_entry(struct share_mode_entry *out,
-                               const struct smb_share_mode_entry *in)
+                               const struct smb_share_mode_entry *in,
+                               uint32_t name_hash)
 {
        memset(out, '\0', sizeof(struct share_mode_entry));
 
@@ -151,10 +167,12 @@ static void create_share_mode_entry(struct share_mode_entry *out,
        out->time.tv_usec = in->open_time.tv_usec;
        out->share_access = in->share_access;
        out->access_mask = in->access_mask;
-       out->dev = (SMB_DEV_T)in->dev;
-       out->inode = (SMB_INO_T)in->ino;
+       out->id.devid = in->dev;
+       out->id.inode = in->ino;
+       out->id.extid = in->extid;
        out->uid = (uint32)geteuid();
        out->flags = 0;
+       out->name_hash = name_hash;
 }
 
 /*
@@ -165,9 +183,11 @@ static void create_share_mode_entry(struct share_mode_entry *out,
 int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
                                uint64_t dev,
                                uint64_t ino,
+                               uint64_t extid,
                                struct smb_share_mode_entry **pp_list,
                                unsigned char *p_delete_on_close)
 {
+       struct locking_key lk;
        TDB_DATA db_data;
        struct smb_share_mode_entry *list = NULL;
        int num_share_modes = 0;
@@ -179,7 +199,8 @@ int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
        *pp_list = NULL;
        *p_delete_on_close = 0;
 
-       db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(dev, ino));
+       db_data = tdb_fetch(db_ctx->smb_tdb, get_locking_key(&lk, dev, ino,
+                                                            extid));
        if (!db_data.dptr) {
                return 0;
        }
@@ -200,13 +221,13 @@ int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
 
        memset(list, '\0', num_share_modes * sizeof(struct smb_share_mode_entry));
 
-       shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
+       shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
 
        list_num = 0;
        for (i = 0; i < num_share_modes; i++) {
                struct share_mode_entry *share = &shares[i];
                struct smb_share_mode_entry *sme = &list[list_num];
-               struct process_id pid = share->pid;
+               struct server_id pid = share->pid;
 
                /* Check this process really exists. */
                if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
@@ -219,8 +240,9 @@ int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
                }
 
                /* Copy into the external list. */
-               sme->dev = (uint64_t)share->dev;
-               sme->ino = (uint64_t)share->inode;
+               sme->dev = share->id.devid;
+               sme->ino = share->id.inode;
+               sme->extid = share->id.extid;
                sme->share_access = (uint32_t)share->share_access;
                sme->access_mask = (uint32_t)share->access_mask;
                sme->open_time.tv_sec = share->time.tv_sec;
@@ -236,12 +258,37 @@ int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
                return 0;
        }
 
-       *p_delete_on_close = ld->u.s.delete_on_close;
+       *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
        *pp_list = list;
        free(db_data.dptr);
        return list_num;
 }
 
+static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
+{
+       TDB_DATA key;
+       char *fullpath = NULL;
+       size_t sharepath_size = strlen(sharepath);
+       size_t filename_size = strlen(filename);
+       uint32_t name_hash;
+
+       *err = 0;
+       fullpath = (char *)malloc(sharepath_size + filename_size + 2);
+       if (fullpath == NULL) {
+               *err = 1;
+               return 0;
+       }
+       memcpy(fullpath, sharepath, sharepath_size);
+       fullpath[sharepath_size] = '/';
+       memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
+
+       key.dptr = (uint8_t *)fullpath;
+       key.dsize = strlen(fullpath) + 1;
+       name_hash = tdb_jenkins_hash(&key);
+       free(fullpath);
+       return name_hash;
+}
+
 /* 
  * Create an entry in the Samba share mode db.
  */
@@ -249,23 +296,32 @@ int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
 int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
                                uint64_t dev,
                                uint64_t ino,
+                               uint64_t extid,
                                const struct smb_share_mode_entry *new_entry,
                                const char *sharepath, /* Must be absolute utf8 path. */
                                const char *filename) /* Must be relative utf8 path. */
 {
        TDB_DATA db_data;
-       TDB_DATA locking_key =  get_locking_key(dev, ino);
+       struct locking_key lk;
+       TDB_DATA locking_key =  get_locking_key(&lk, dev, ino, extid);
        int orig_num_share_modes = 0;
        struct locking_data *ld = NULL; /* internal samba db state. */
        struct share_mode_entry *shares = NULL;
-       char *new_data_p = NULL;
+       uint8 *new_data_p = NULL;
        size_t new_data_size = 0;
+       int err = 0;
+       uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
+
+       if (err) {
+               return -1;
+       }
 
        db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
        if (!db_data.dptr) {
                /* We must create the entry. */
-               db_data.dptr = (char *)malloc(
-                       (2*sizeof(struct share_mode_entry)) +
+               db_data.dptr = (uint8 *)malloc(
+                       sizeof(struct locking_data) +
+                       sizeof(struct share_mode_entry) +
                        strlen(sharepath) + 1 +
                        strlen(filename) + 1);
                if (!db_data.dptr) {
@@ -274,20 +330,19 @@ int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
                ld = (struct locking_data *)db_data.dptr;
                memset(ld, '\0', sizeof(struct locking_data));
                ld->u.s.num_share_mode_entries = 1;
-               ld->u.s.delete_on_close = 0;
-               ld->u.s.delete_token_size = 0;
-               shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
-               create_share_mode_entry(shares, new_entry);
+               ld->u.s.num_delete_token_entries = 0;
+               shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
+               create_share_mode_entry(shares, new_entry, name_hash);
 
-               memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry),
+               memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
                        sharepath,
                        strlen(sharepath) + 1);
-               memcpy(db_data.dptr + 2*sizeof(struct share_mode_entry) +
+               memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
                        strlen(sharepath) + 1,
                        filename,
                        strlen(filename) + 1);
 
-               db_data.dsize = 2*sizeof(struct share_mode_entry) +
+               db_data.dsize = sizeof(struct locking_data) + sizeof(struct share_mode_entry) +
                                        strlen(sharepath) + 1 +
                                        strlen(filename) + 1;
                if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_INSERT) == -1) {
@@ -299,7 +354,7 @@ int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
        }
 
        /* Entry exists, we must add a new entry. */
-       new_data_p = (char *)malloc(
+       new_data_p = (uint8 *)malloc(
                db_data.dsize + sizeof(struct share_mode_entry));
        if (!new_data_p) {
                free(db_data.dptr);
@@ -310,21 +365,21 @@ int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
        orig_num_share_modes = ld->u.s.num_share_mode_entries;
 
        /* Copy the original data. */
-       memcpy(new_data_p, db_data.dptr, (orig_num_share_modes+1)*sizeof(struct share_mode_entry));
+       memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)));
 
        /* Add in the new share mode */
-       shares = (struct share_mode_entry *)(new_data_p +
-                       ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)));
+       shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
+                       (orig_num_share_modes * sizeof(struct share_mode_entry)));
 
-       create_share_mode_entry(shares, new_entry);
+       create_share_mode_entry(shares, new_entry, name_hash);
 
        ld = (struct locking_data *)new_data_p;
        ld->u.s.num_share_mode_entries++;
 
-       /* Append the original delete_token and filenames. */
-       memcpy(new_data_p + ((ld->u.s.num_share_mode_entries+1)*sizeof(struct share_mode_entry)),
-               db_data.dptr + ((orig_num_share_modes+1)*sizeof(struct share_mode_entry)),
-               db_data.dsize - ((orig_num_share_modes+1) * sizeof(struct share_mode_entry)));
+       /* Append the original delete_tokens and filenames. */
+       memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
+               db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
+               db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
 
        new_data_size = db_data.dsize + sizeof(struct share_mode_entry);
 
@@ -350,30 +405,33 @@ int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
 int smb_create_share_mode_entry(struct smbdb_ctx *db_ctx,
                                uint64_t dev,
                                uint64_t ino,
+                               uint64_t extid,
                                const struct smb_share_mode_entry *new_entry,
                                const char *filename) /* Must be absolute utf8 path. */
 {
        if (*filename != '/') {
                abort();
        }
-       return smb_create_share_mode_entry_ex(db_ctx, dev, ino, new_entry,
+       return smb_create_share_mode_entry_ex(db_ctx, dev, ino, extid, new_entry,
                                                "/", &filename[1]);
 }
 
 int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
                                uint64_t dev,
                                uint64_t ino,
+                               uint64_t extid,
                                const struct smb_share_mode_entry *del_entry)
 {
        TDB_DATA db_data;
-       TDB_DATA locking_key =  get_locking_key(dev, ino);
+       struct locking_key lk;
+       TDB_DATA locking_key =  get_locking_key(&lk, dev, ino, extid);
        int orig_num_share_modes = 0;
        struct locking_data *ld = NULL; /* internal samba db state. */
        struct share_mode_entry *shares = NULL;
-       char *new_data_p = NULL;
+       uint8 *new_data_p = NULL;
        size_t remaining_size = 0;
        size_t i, num_share_modes;
-       const char *remaining_ptr = NULL;
+       const uint8 *remaining_ptr = NULL;
 
        db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
        if (!db_data.dptr) {
@@ -382,7 +440,7 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
 
        ld = (struct locking_data *)db_data.dptr;
        orig_num_share_modes = ld->u.s.num_share_mode_entries;
-       shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
+       shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
 
        if (orig_num_share_modes == 1) {
                /* Only one entry - better be ours... */
@@ -397,7 +455,7 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
        }
 
        /* More than one - allocate a new record minus the one we'll delete. */
-       new_data_p = (char *)malloc(
+       new_data_p = (uint8 *)malloc(
                db_data.dsize - sizeof(struct share_mode_entry));
        if (!new_data_p) {
                free(db_data.dptr);
@@ -405,12 +463,12 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
        }
 
        /* Copy the header. */
-       memcpy(new_data_p, db_data.dptr, sizeof(struct share_mode_entry));
+       memcpy(new_data_p, db_data.dptr, sizeof(struct locking_data));
 
        num_share_modes = 0;
        for (i = 0; i < orig_num_share_modes; i++) {
                struct share_mode_entry *share = &shares[i];
-               struct process_id pid = share->pid;
+               struct server_id pid = share->pid;
 
                /* Check this process really exists. */
                if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
@@ -421,7 +479,8 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
                        continue; /* This is our delete taget. */
                }
 
-               memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
+               memcpy(new_data_p + sizeof(struct locking_data) +
+                               (num_share_modes * sizeof(struct share_mode_entry)),
                        share, sizeof(struct share_mode_entry) );
 
                num_share_modes++;
@@ -434,11 +493,11 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
                return tdb_delete(db_ctx->smb_tdb, locking_key);
        }
 
-       /* Copy any delete token plus the terminating filenames. */
-       remaining_ptr = db_data.dptr + ((orig_num_share_modes+1) * sizeof(struct share_mode_entry));
+       /* Copy any delete tokens plus the terminating filenames. */
+       remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
        remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
 
-       memcpy(new_data_p + ((num_share_modes+1)*sizeof(struct share_mode_entry)),
+       memcpy(new_data_p + sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)),
                remaining_ptr,
                remaining_size);
 
@@ -450,7 +509,7 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
        ld = (struct locking_data *)db_data.dptr;
        ld->u.s.num_share_mode_entries = num_share_modes;
 
-       db_data.dsize = ((num_share_modes+1)*sizeof(struct share_mode_entry)) + remaining_size;
+       db_data.dsize = sizeof(struct locking_data) + (num_share_modes * sizeof(struct share_mode_entry)) + remaining_size;
 
        if (tdb_store(db_ctx->smb_tdb, locking_key, db_data, TDB_REPLACE) == -1) {
                free(db_data.dptr);
@@ -463,11 +522,13 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
 int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
                                uint64_t dev,
                                uint64_t ino,
+                               uint64_t extid,
                                const struct smb_share_mode_entry *set_entry,
                                const struct smb_share_mode_entry *new_entry)
 {
        TDB_DATA db_data;
-       TDB_DATA locking_key =  get_locking_key(dev, ino);
+       struct locking_key lk;
+       TDB_DATA locking_key =  get_locking_key(&lk, dev, ino, extid);
        int num_share_modes = 0;
        struct locking_data *ld = NULL; /* internal samba db state. */
        struct share_mode_entry *shares = NULL;
@@ -481,11 +542,11 @@ int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
 
        ld = (struct locking_data *)db_data.dptr;
        num_share_modes = ld->u.s.num_share_mode_entries;
-       shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct share_mode_entry));
+       shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
 
        for (i = 0; i < num_share_modes; i++) {
                struct share_mode_entry *share = &shares[i];
-               struct process_id pid = share->pid;
+               struct server_id pid = share->pid;
 
                /* Check this process really exists. */
                if (kill(sharemodes_procid_to_pid(&pid), 0) == -1 && (errno == ESRCH)) {
@@ -493,7 +554,7 @@ int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
                }
 
                if (share_mode_entry_equal(set_entry, share)) {
-                       create_share_mode_entry(share, new_entry);
+                       create_share_mode_entry(share, new_entry, share->name_hash);
                        found_entry = 1;
                        break;
                }