RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / modules / vfs_afsacl.c
index f4b254a109411d12a8a4d83333b505b58aeb339e..a1043d6f6ad0282d2606967c3cf650450888b5c4 100644 (file)
@@ -5,7 +5,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *  
  * This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  * GNU General Public License for more details.
  *  
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "includes.h"
 
 #define MAXSIZE 2048
 
-extern DOM_SID global_sid_World;
-extern DOM_SID global_sid_Builtin_Administrators;
-extern DOM_SID global_sid_Builtin_Backup_Operators;
-extern DOM_SID global_sid_Authenticated_Users;
-extern DOM_SID global_sid_NULL;
+extern const DOM_SID global_sid_World;
+extern const DOM_SID global_sid_Builtin_Administrators;
+extern const DOM_SID global_sid_Builtin_Backup_Operators;
+extern const DOM_SID global_sid_Authenticated_Users;
+extern const DOM_SID global_sid_NULL;
+
+static char space_replacement = '%';
+
+/* Do we expect SIDs as pts names? */
+static bool sidpts;
 
 extern int afs_syscall(int, char *, int, char *, int);
 
 struct afs_ace {
-       BOOL positive;
+       bool positive;
        char *name;
        DOM_SID sid;
-       enum SID_NAME_USE type;
+       enum lsa_SidType type;
        uint32 rights;
        struct afs_ace *next;
 };
@@ -61,7 +65,7 @@ struct afs_iob {
 };
 
 
-static BOOL init_afs_acl(struct afs_acl *acl)
+static bool init_afs_acl(struct afs_acl *acl)
 {
        ZERO_STRUCT(*acl);
        acl->ctx = talloc_init("afs_acl");
@@ -83,7 +87,7 @@ static void free_afs_acl(struct afs_acl *acl)
 
 static struct afs_ace *clone_afs_ace(TALLOC_CTX *mem_ctx, struct afs_ace *ace)
 {
-       struct afs_ace *result = talloc(mem_ctx, sizeof(struct afs_ace));
+       struct afs_ace *result = TALLOC_P(mem_ctx, struct afs_ace);
 
        if (result == NULL)
                return NULL;
@@ -100,33 +104,12 @@ static struct afs_ace *clone_afs_ace(TALLOC_CTX *mem_ctx, struct afs_ace *ace)
        return result;
 }
        
-
-/* Ok, this is sort-of a hack. We assume here that we have winbind users in
- * AFS. And yet another copy of parse_domain_user.... */
-
-static BOOL parse_domain_user(const char *domuser, fstring domain,
-                             fstring user)
-{
-       char *p = strchr(domuser,*lp_winbind_separator());
-
-       if (p==NULL) {
-               return False;
-       }
-
-       fstrcpy(user, p+1);
-       fstrcpy(domain, domuser);
-       domain[PTR_DIFF(p, domuser)] = 0;
-       strupper_m(domain);
-       
-       return True;
-}
-
 static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
-                                  BOOL positive,
+                                  bool positive,
                                   const char *name, uint32 rights)
 {
        DOM_SID sid;
-       enum SID_NAME_USE type;
+       enum lsa_SidType type;
        struct afs_ace *result;
 
        if (strcmp(name, "system:administrators") == 0) {
@@ -149,16 +132,31 @@ static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
                sid_copy(&sid, &global_sid_Builtin_Backup_Operators);
                type = SID_NAME_ALIAS;
 
+       } else if (sidpts) {
+               /* All PTS users/groups are expressed as SIDs */
+
+               sid_copy(&sid, &global_sid_NULL);
+               type = SID_NAME_UNKNOWN;
+
+               if (string_to_sid(&sid, name)) {
+                       const char *user, *domain;
+                       /* We have to find the type, look up the SID */
+                       lookup_sid(talloc_tos(), &sid,
+                                  &domain, &user, &type);
+               }
+
        } else {
 
-               fstring user, domain;
+               const char *domain, *uname;
+               char *p;
 
-               if (!parse_domain_user(name, domain, user)) {
-                       fstrcpy(user, name);
-                       fstrcpy(domain, lp_workgroup());
+               p = strchr_m(name, *lp_winbind_separator());
+               if (p != NULL) {
+                       *p = '\\';
                }
-                   
-               if (!lookup_name(domain, user, &sid, &type)) {
+
+               if (!lookup_name(talloc_tos(), name, LOOKUP_NAME_ALL,
+                                &domain, &uname, &sid, &type)) {
                        DEBUG(10, ("Could not find AFS user %s\n", name));
 
                        sid_copy(&sid, &global_sid_NULL);
@@ -167,7 +165,7 @@ static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
                }
        }
 
-       result = talloc(mem_ctx, sizeof(struct afs_ace));
+       result = TALLOC_P(mem_ctx, struct afs_ace);
 
        if (result == NULL) {
                DEBUG(0, ("Could not talloc AFS ace\n"));
@@ -190,7 +188,7 @@ static struct afs_ace *new_afs_ace(TALLOC_CTX *mem_ctx,
 }
 
 static void add_afs_ace(struct afs_acl *acl,
-                       BOOL positive,
+                       bool positive,
                        const char *name, uint32 rights)
 {
        struct afs_ace *ace;
@@ -229,7 +227,7 @@ static void add_afs_ace(struct afs_acl *acl,
  * Second field: Rights
  */
 
-static BOOL parse_afs_acl(struct afs_acl *acl, const char *acl_str)
+static bool parse_afs_acl(struct afs_acl *acl, const char *acl_str)
 {
        int nplus, nminus;
        int aces;
@@ -260,10 +258,12 @@ static BOOL parse_afs_acl(struct afs_acl *acl, const char *acl_str)
        for (aces = nplus+nminus; aces > 0; aces--)
        {
 
-               const char *name;
+               const char *namep;
+               fstring name;
                uint32 rights;
+               char *space;
 
-               name = p;
+               namep = p;
 
                if ((p = strchr(p, '\t')) == NULL)
                        return False;
@@ -277,6 +277,11 @@ static BOOL parse_afs_acl(struct afs_acl *acl, const char *acl_str)
                        return False;
                p += 1;
 
+               fstrcpy(name, namep);
+
+               while ((space = strchr_m(name, space_replacement)) != NULL)
+                       *space = ' ';
+
                add_afs_ace(acl, nplus>0, name, rights);
 
                nplus -= 1;
@@ -285,7 +290,7 @@ static BOOL parse_afs_acl(struct afs_acl *acl, const char *acl_str)
        return True;
 }
 
-static BOOL unparse_afs_acl(struct afs_acl *acl, char *acl_str)
+static bool unparse_afs_acl(struct afs_acl *acl, char *acl_str)
 {
        /* TODO: String length checks!!!! */
 
@@ -409,7 +414,7 @@ static void split_afs_acl(struct afs_acl *acl,
        return;
 }
 
-static BOOL same_principal(struct afs_ace *x, struct afs_ace *y)
+static bool same_principal(struct afs_ace *x, struct afs_ace *y)
 {
        return ( (x->positive == y->positive) &&
                 (sid_compare(&x->sid, &y->sid) == 0) );
@@ -425,7 +430,7 @@ static void merge_afs_acls(struct afs_acl *dir_acl,
 
        for (ace = dir_acl->acelist; ace != NULL; ace = ace->next) {
                struct afs_ace *file_ace;
-               BOOL found = False;
+               bool found = False;
 
                for (file_ace = file_acl->acelist;
                     file_ace != NULL;
@@ -445,7 +450,7 @@ static void merge_afs_acls(struct afs_acl *dir_acl,
 
        for (ace = file_acl->acelist; ace != NULL; ace = ace->next) {
                struct afs_ace *dir_ace;
-               BOOL already_seen = False;
+               bool already_seen = False;
 
                for (dir_ace = dir_acl->acelist;
                     dir_ace != NULL;
@@ -526,7 +531,7 @@ static struct static_dir_ace_mapping {
 static uint32 nt_to_afs_dir_rights(const char *filename, const SEC_ACE *ace)
 {
        uint32 result = 0;
-       uint32 rights = ace->info.mask;
+       uint32 rights = ace->access_mask;
        uint8 flags = ace->flags;
 
        struct static_dir_ace_mapping *m;
@@ -534,12 +539,12 @@ static uint32 nt_to_afs_dir_rights(const char *filename, const SEC_ACE *ace)
        for (m = &ace_mappings[0]; m->afs_rights != 9999; m++) {
                if ( (ace->type == m->type) &&
                     (ace->flags == m->flags) &&
-                    (ace->info.mask == m->mask) )
+                    (ace->access_mask == m->mask) )
                        return m->afs_rights;
        }
 
        DEBUG(1, ("AFSACL FALLBACK: 0x%X 0x%X 0x%X %s %X\n",
-                 ace->type, ace->flags, ace->info.mask, filename, rights));
+                 ace->type, ace->flags, ace->access_mask, filename, rights));
 
        if (rights & (GENERIC_ALL_ACCESS|WRITE_DAC_ACCESS)) {
                result |= PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT |
@@ -567,7 +572,7 @@ static uint32 nt_to_afs_dir_rights(const char *filename, const SEC_ACE *ace)
 static uint32 nt_to_afs_file_rights(const char *filename, const SEC_ACE *ace)
 {
        uint32 result = 0;
-       uint32 rights = ace->info.mask;
+       uint32 rights = ace->access_mask;
 
        if (rights & (GENERIC_READ_ACCESS|FILE_READ_DATA)) {
                result |= PRSFS_READ;
@@ -583,7 +588,7 @@ static uint32 nt_to_afs_file_rights(const char *filename, const SEC_ACE *ace)
 static size_t afs_to_nt_acl(struct afs_acl *afs_acl, 
                            struct files_struct *fsp,
                            uint32 security_info,
-                           struct security_descriptor_info **ppdesc)
+                           struct security_descriptor **ppdesc)
 {
        SEC_ACE *nt_ace_list;
        DOM_SID owner_sid, group_sid;
@@ -592,17 +597,17 @@ static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
        SEC_ACL *psa = NULL;
        int good_aces;
        size_t sd_size;
-       TALLOC_CTX *mem_ctx = main_loop_talloc_get();
+       TALLOC_CTX *mem_ctx = talloc_tos();
 
        struct afs_ace *afs_ace;
 
-       if (fsp->is_directory || fsp->fd == -1) {
+       if (fsp->is_directory || fsp->fh->fd == -1) {
                /* Get the stat struct for the owner info. */
                if(SMB_VFS_STAT(fsp->conn,fsp->fsp_name, &sbuf) != 0) {
                        return 0;
                }
        } else {
-               if(SMB_VFS_FSTAT(fsp,fsp->fd,&sbuf) != 0) {
+               if(SMB_VFS_FSTAT(fsp,fsp->fh->fd,&sbuf) != 0) {
                        return 0;
                }
        }
@@ -610,10 +615,14 @@ static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
        uid_to_sid(&owner_sid, sbuf.st_uid);
        gid_to_sid(&group_sid, sbuf.st_gid);
 
-       nt_ace_list = (SEC_ACE *)malloc(afs_acl->num_aces * sizeof(SEC_ACE));
+       if (afs_acl->num_aces) {
+               nt_ace_list = TALLOC_ARRAY(mem_ctx, SEC_ACE, afs_acl->num_aces);
 
-       if (nt_ace_list == NULL)
-               return 0;
+               if (nt_ace_list == NULL)
+                       return 0;
+       } else {
+               nt_ace_list = NULL;
+       }
 
        afs_ace = afs_acl->acelist;
        good_aces = 0;
@@ -659,7 +668,7 @@ static size_t afs_to_nt_acl(struct afs_acl *afs_acl,
        return sd_size;
 }
 
-static BOOL mappable_sid(const DOM_SID *sid)
+static bool mappable_sid(const DOM_SID *sid)
 {
        DOM_SID domain_sid;
        
@@ -683,9 +692,9 @@ static BOOL mappable_sid(const DOM_SID *sid)
        return False;
 }
 
-static BOOL nt_to_afs_acl(const char *filename,
+static bool nt_to_afs_acl(const char *filename,
                          uint32 security_info_sent,
-                         struct security_descriptor_info *psd,
+                         struct security_descriptor *psd,
                          uint32 (*nt_to_afs_rights)(const char *filename,
                                                     const SEC_ACE *ace),
                          struct afs_acl *afs_acl)
@@ -705,10 +714,10 @@ static BOOL nt_to_afs_acl(const char *filename,
        dacl = psd->dacl;
 
        for (i = 0; i < dacl->num_aces; i++) {
-               SEC_ACE *ace = &(dacl->ace[i]);
-               fstring dom_name;
-               fstring name;
-               enum SID_NAME_USE name_type;
+               SEC_ACE *ace = &(dacl->aces[i]);
+               const char *dom_name, *name;
+               enum lsa_SidType name_type;
+               char *p;
 
                if (ace->type != SEC_ACE_TYPE_ACCESS_ALLOWED) {
                        /* First cut: Only positive ACEs */
@@ -724,28 +733,28 @@ static BOOL nt_to_afs_acl(const char *filename,
                if (sid_compare(&ace->trustee,
                                &global_sid_Builtin_Administrators) == 0) {
 
-                       fstrcpy(name, "system:administrators");
+                       name = "system:administrators";
 
                } else if (sid_compare(&ace->trustee,
                                       &global_sid_World) == 0) {
 
-                       fstrcpy(name, "system:anyuser");
+                       name = "system:anyuser";
 
                } else if (sid_compare(&ace->trustee,
                                       &global_sid_Authenticated_Users) == 0) {
 
-                       fstrcpy(name, "system:authuser");
+                       name = "system:authuser";
 
                } else if (sid_compare(&ace->trustee,
                                       &global_sid_Builtin_Backup_Operators)
                           == 0) {
 
-                       fstrcpy(name, "system:backup");
+                       name = "system:backup";
 
                } else {
 
-                       if (!lookup_sid(&ace->trustee,
-                                       dom_name, name, &name_type)) {
+                       if (!lookup_sid(talloc_tos(), &ace->trustee,
+                                       &dom_name, &name, &name_type)) {
                                DEBUG(1, ("AFSACL: Could not lookup SID %s on file %s\n",
                                          sid_string_static(&ace->trustee), filename));
                                continue;
@@ -753,16 +762,32 @@ static BOOL nt_to_afs_acl(const char *filename,
 
                        if ( (name_type == SID_NAME_USER) ||
                             (name_type == SID_NAME_DOM_GRP) ||
-                            (name_type == SID_NAME_ALIAS) ) { 
-                               fstring only_username;
-                               fstrcpy(only_username, name);
-                               fstr_sprintf(name, "%s%s%s",
-                                            dom_name, lp_winbind_separator(),
-                                            only_username);
-                               strlower_m(name);
+                            (name_type == SID_NAME_ALIAS) ) {
+                               char *tmp;
+                               tmp = talloc_asprintf(talloc_tos(), "%s%s%s",
+                                                      dom_name, lp_winbind_separator(),
+                                                      name);
+                               if (tmp == NULL) {
+                                       return False;
+                               }
+                               strlower_m(tmp);
+                               name = tmp;
+                       }
+
+                       if (sidpts) {
+                               /* Expect all users/groups in pts as SIDs */
+                               name = talloc_strdup(
+                                       talloc_tos(),
+                                       sid_string_static(&ace->trustee));
+                               if (name == NULL) {
+                                       return False;
+                               }
                        }
                }
 
+               while ((p = strchr_m(name, ' ')) != NULL)
+                       *p = space_replacement;
+
                add_afs_ace(afs_acl, True, name,
                            nt_to_afs_rights(filename, ace));
        }
@@ -770,7 +795,7 @@ static BOOL nt_to_afs_acl(const char *filename,
        return True;
 }
 
-static BOOL afs_get_afs_acl(char *filename, struct afs_acl *acl)
+static bool afs_get_afs_acl(char *filename, struct afs_acl *acl)
 {
        struct afs_iob iob;
 
@@ -805,13 +830,15 @@ static BOOL afs_get_afs_acl(char *filename, struct afs_acl *acl)
 }
 
 static size_t afs_get_nt_acl(struct files_struct *fsp, uint32 security_info,
-                            struct security_descriptor_info **ppdesc)
+                            struct security_descriptor **ppdesc)
 {
        struct afs_acl acl;
        size_t sd_size;
 
        DEBUG(5, ("afs_get_nt_acl: %s\n", fsp->fsp_name));
 
+       sidpts = lp_parm_bool(SNUM(fsp->conn), "afsacl", "sidpts", False);
+
        if (!afs_get_afs_acl(fsp->fsp_name, &acl)) {
                return 0;
        }
@@ -855,9 +882,9 @@ static void merge_unknown_aces(struct afs_acl *src, struct afs_acl *dst)
        }
 }
 
-static BOOL afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
+static NTSTATUS afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
                           uint32 security_info_sent,
-                          struct security_descriptor_info *psd)
+                          struct security_descriptor *psd)
 {
        struct afs_acl old_afs_acl, new_afs_acl;
        struct afs_acl dir_acl, file_acl;
@@ -870,20 +897,24 @@ static BOOL afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
        fileacls = lp_parm_const_string(SNUM(handle->conn), "afsacl", "fileacls",
                                        "yes");
 
+       sidpts = lp_parm_bool(SNUM(handle->conn), "afsacl", "sidpts", False);
+
        ZERO_STRUCT(old_afs_acl);
        ZERO_STRUCT(new_afs_acl);
        ZERO_STRUCT(dir_acl);
        ZERO_STRUCT(file_acl);
 
-       pstr_sprintf(name, fsp->fsp_name);
+       pstrcpy(name, fsp->fsp_name);
 
        if (!fsp->is_directory) {
+               /* We need to get the name of the directory containing the
+                * file, this is where the AFS acls live */
                char *p = strrchr(name, '/');
-               if (p == NULL) {
-                       DEBUG(3, ("No / in file string\n"));
-                       return False;
+               if (p != NULL) {
+                       *p = '\0';
+               } else {
+                       pstrcpy(name, ".");
                }
-               *p = '\0';
        }
 
        if (!afs_get_afs_acl(name, &old_afs_acl)) {
@@ -948,25 +979,25 @@ static BOOL afs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
        free_afs_acl(&old_afs_acl);
        free_afs_acl(&new_afs_acl);
 
-       return (ret == 0);
+       return (ret == 0) ? NT_STATUS_OK : NT_STATUS_ACCESS_DENIED;
 }
 
 static size_t afsacl_fget_nt_acl(struct vfs_handle_struct *handle,
                                 struct files_struct *fsp,
                                 int fd,  uint32 security_info,
-                                struct security_descriptor_info **ppdesc)
+                                struct security_descriptor **ppdesc)
 {
        return afs_get_nt_acl(fsp, security_info, ppdesc);
 }
 static size_t afsacl_get_nt_acl(struct vfs_handle_struct *handle,
                                struct files_struct *fsp,
                                const char *name,  uint32 security_info,
-                               struct security_descriptor_info **ppdesc)
+                               struct security_descriptor **ppdesc)
 {
        return afs_get_nt_acl(fsp, security_info, ppdesc);
 }
 
-BOOL afsacl_fset_nt_acl(vfs_handle_struct *handle,
+NTSTATUS afsacl_fset_nt_acl(vfs_handle_struct *handle,
                         files_struct *fsp,
                         int fd, uint32 security_info_sent,
                         SEC_DESC *psd)
@@ -974,7 +1005,7 @@ BOOL afsacl_fset_nt_acl(vfs_handle_struct *handle,
        return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
 }
 
-BOOL afsacl_set_nt_acl(vfs_handle_struct *handle,
+NTSTATUS afsacl_set_nt_acl(vfs_handle_struct *handle,
                       files_struct *fsp,
                       const char *name, uint32 security_info_sent,
                       SEC_DESC *psd)
@@ -982,9 +1013,25 @@ BOOL afsacl_set_nt_acl(vfs_handle_struct *handle,
        return afs_set_nt_acl(handle, fsp, security_info_sent, psd);
 }
 
+static int afsacl_connect(vfs_handle_struct *handle, 
+                         const char *service, 
+                         const char *user)
+{
+       const char *spc;
+
+       spc = lp_parm_const_string(SNUM(handle->conn), "afsacl", "space", "%");
+
+       if (spc != NULL)
+               space_replacement = spc[0];
+       
+       return SMB_VFS_NEXT_CONNECT(handle, service, user);
+}
+
 /* VFS operations structure */
 
 static vfs_op_tuple afsacl_ops[] = {   
+       {SMB_VFS_OP(afsacl_connect), SMB_VFS_OP_CONNECT,
+        SMB_VFS_LAYER_TRANSPARENT},
        {SMB_VFS_OP(afsacl_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
         SMB_VFS_LAYER_TRANSPARENT},
        {SMB_VFS_OP(afsacl_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
@@ -996,6 +1043,7 @@ static vfs_op_tuple afsacl_ops[] = {
        {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
 };
 
+NTSTATUS vfs_afsacl_init(void);
 NTSTATUS vfs_afsacl_init(void)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "afsacl",