]> wimlib.net Git - wimlib/blobdiff - src/ntfs-3g_security.c
Fix build without new libntfs-3g
[wimlib] / src / ntfs-3g_security.c
index a508be4542eb9b598a21c86a2b3da72a11aad6b6..7c8dec3c504db8dace198cadd1c1afc19bc36ed5 100644 (file)
  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#ifdef ENABLE_XATTR
+#define HAVE_SETXATTR
+#endif
+
+#include <stdarg.h>
+
 #include <stdio.h>
-#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif
-#ifdef HAVE_STRING_H
 #include <string.h>
-#endif
 #include <errno.h>
 #include <fcntl.h>
 #ifdef HAVE_SETXATTR
@@ -44,8 +44,6 @@
 #include <sys/stat.h>
 #endif
 
-#include <stdarg.h>
-
 #include <unistd.h>
 #include <pwd.h>
 #include <grp.h>
@@ -4125,6 +4123,77 @@ int ntfs_get_ntfs_attrib(ntfs_inode *ni, char *value, size_t size)
        return (outsize ? (int)outsize : -errno);
 }
 
+/*
+ *             Get the ntfs attributes from an inode
+ *     The attributes are returned according to cpu endianness
+ *
+ *     Returns the attributes if successful (cannot be zero)
+ *             0 if failed (errno to tell why)
+ */
+
+u32 ntfs_get_inode_attributes(ntfs_inode *ni)
+{
+       u32 attrib = -1;
+
+       if (ni) {
+               attrib = le32_to_cpu(ni->flags);
+               if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
+                       attrib |= const_le32_to_cpu(FILE_ATTR_DIRECTORY);
+               else
+                       attrib &= ~const_le32_to_cpu(FILE_ATTR_DIRECTORY);
+               if (!attrib)
+                       attrib |= const_le32_to_cpu(FILE_ATTR_NORMAL);
+       } else
+               errno = EINVAL;
+       return (attrib);
+}
+
+/*
+ *             Set the ntfs attributes on an inode
+ *     The attribute is expected according to cpu endianness
+ *
+ *     Returns 0 if successful
+ *             -1 if failed (errno to tell why)
+ */
+
+int ntfs_set_inode_attributes(ntfs_inode *ni, u32 attrib)
+{
+       le32 settable;
+       ATTR_FLAGS dirflags;
+       int res;
+
+       res = -1;
+       if (ni) {
+               settable = FILE_ATTR_SETTABLE;
+               res = 0;
+               if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
+                       /*
+                        * Accept changing compression for a directory
+                        * and set index root accordingly
+                        */
+                       settable |= FILE_ATTR_COMPRESSED;
+                       if ((ni->flags ^ cpu_to_le32(attrib))
+                                    & FILE_ATTR_COMPRESSED) {
+                               if (ni->flags & FILE_ATTR_COMPRESSED)
+                                       dirflags = const_cpu_to_le16(0);
+                               else
+                                       dirflags = ATTR_IS_COMPRESSED;
+                               res = ntfs_attr_set_flags(ni, AT_INDEX_ROOT,
+                                       NTFS_INDEX_I30, 4, dirflags,
+                                       ATTR_COMPRESSION_MASK);
+                       }
+               }
+               if (!res) {
+                       ni->flags = (ni->flags & ~settable)
+                                | (cpu_to_le32(attrib) & settable);
+                       NInoFileNameSetDirty(ni);
+                       NInoSetDirty(ni);
+               }
+       } else
+               errno = EINVAL;
+       return (res);
+}
+
 /*
  *             Return the ntfs attribute into an extended attribute
  *     The attribute is expected according to cpu endianness
@@ -4136,8 +4205,6 @@ int ntfs_set_ntfs_attrib(ntfs_inode *ni,
                        const char *value, size_t size, int flags)
 {
        u32 attrib;
-       le32 settable;
-       ATTR_FLAGS dirflags;
        int res;
 
        res = -1;
@@ -4145,33 +4212,7 @@ int ntfs_set_ntfs_attrib(ntfs_inode *ni,
                if (!(flags & XATTR_CREATE)) {
                        /* copy to avoid alignment problems */
                        memcpy(&attrib,value,sizeof(FILE_ATTR_FLAGS));
-                       settable = FILE_ATTR_SETTABLE;
-                       res = 0;
-                       if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
-                               /*
-                                * Accept changing compression for a directory
-                                * and set index root accordingly
-                                */
-                               settable |= FILE_ATTR_COMPRESSED;
-                               if ((ni->flags ^ cpu_to_le32(attrib))
-                                            & FILE_ATTR_COMPRESSED) {
-                                       if (ni->flags & FILE_ATTR_COMPRESSED)
-                                               dirflags = const_cpu_to_le16(0);
-                                       else
-                                               dirflags = ATTR_IS_COMPRESSED;
-                                       res = ntfs_attr_set_flags(ni,
-                                               AT_INDEX_ROOT,
-                                               NTFS_INDEX_I30, 4,
-                                               dirflags,
-                                               ATTR_COMPRESSION_MASK);
-                               }
-                       }
-                       if (!res) {
-                               ni->flags = (ni->flags & ~settable)
-                                        | (cpu_to_le32(attrib) & settable);
-                               NInoFileNameSetDirty(ni);
-                               NInoSetDirty(ni);
-                       }
+                       res = ntfs_set_inode_attributes(ni, attrib);
                } else
                        errno = EEXIST;
        } else
@@ -4554,7 +4595,69 @@ static BOOL mergesecurityattr(ntfs_volume *vol, const char *oldattr,
        return (ok);
 }
 
-#if 0
+int ntfs_get_inode_security(ntfs_inode *ni, u32 selection,
+                           char *buf, u32 buflen, u32 *psize)
+{
+       int res;
+       char *attr;
+
+       res = 0;
+       if (ni) {
+               attr = getsecurityattr(ni->vol, ni);
+               if (attr) {
+                       if (feedsecurityattr(attr, selection,
+                                       buf, buflen, psize)) {
+                               if (test_nino_flag(ni, v3_Extensions)
+                                   && ni->security_id)
+                                       res = le32_to_cpu(
+                                               ni->security_id);
+                               else
+                                       res = -1;
+                       }
+                       free(attr);
+               }
+       } else
+               errno = EINVAL;
+       return (res);
+}
+
+int ntfs_set_inode_security(ntfs_inode *ni, u32 selection, const char *attr)
+{
+       const SECURITY_DESCRIPTOR_RELATIVE *phead;
+       int attrsz;
+       BOOL missing;
+       char *oldattr;
+       int res;
+
+       res = -1; /* default return */
+       if (ni) {
+               phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr;
+               attrsz = ntfs_attr_size(attr);
+               /* if selected, owner and group must be present or defaulted */
+               missing = ((selection & OWNER_SECURITY_INFORMATION)
+                               && !phead->owner
+                               && !(phead->control & SE_OWNER_DEFAULTED))
+                       || ((selection & GROUP_SECURITY_INFORMATION)
+                               && !phead->group
+                               && !(phead->control & SE_GROUP_DEFAULTED));
+               if (!missing
+                   && (phead->control & SE_SELF_RELATIVE)
+                   && ntfs_valid_descr(attr, attrsz)) {
+                       oldattr = getsecurityattr(ni->vol, ni);
+                       if (oldattr) {
+                               if (mergesecurityattr(ni->vol, oldattr, attr,
+                                                       selection, ni)) {
+                                       res = 0;
+                               }
+                               free(oldattr);
+                       }
+               } else
+                       errno = EINVAL;
+       } else
+               errno = EINVAL;
+       return (res);
+}
+
 /*
  *             Return the security descriptor of a file
  *     This is intended to be similar to GetFileSecurity() from Win32
@@ -4680,57 +4783,8 @@ int ntfs_set_file_security(struct SECURITY_API *scapi,
                errno = EINVAL;
        return (res);
 }
-#endif
-
-/* 
- * Set security data on a NTFS file given an inode
- *
- * Returns nonzero on success
- */
-int _ntfs_set_file_security(ntfs_volume *vol, ntfs_inode *ni,
-                           u32 selection, const char *attr)
-{
-       const SECURITY_DESCRIPTOR_RELATIVE *phead;
-       int attrsz;
-       BOOL missing;
-       char *oldattr;
-       int res;
-
-       res = 0; /* default return */
-       phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr;
-       attrsz = ntfs_attr_size(attr);
-       /* if selected, owner and group must be present or defaulted */
-       missing = ((selection & OWNER_SECURITY_INFORMATION)
-                       && !phead->owner
-                       && !(phead->control & SE_OWNER_DEFAULTED))
-               || ((selection & GROUP_SECURITY_INFORMATION)
-                       && !phead->group
-                       && !(phead->control & SE_GROUP_DEFAULTED));
-       if (!missing
-           && (phead->control & SE_SELF_RELATIVE)
-           && ntfs_valid_descr(attr, attrsz)) {
-               oldattr = getsecurityattr(vol, ni);
-               if (oldattr) {
-                       if (mergesecurityattr(
-                               vol,
-                               oldattr, attr,
-                               selection, ni)) {
-                               if (test_nino_flag(ni,
-                                           v3_Extensions))
-                                       res = le32_to_cpu(
-                                           ni->security_id);
-                               else
-                                       res = -1;
-                       }
-                       free(oldattr);
-               }
-       } else
-               errno = EINVAL;
-       return (res);
-}
 
 
-#if 0
 /*
  *             Return the attributes of a file
  *     This is intended to be similar to GetFileAttributes() from Win32
@@ -4754,14 +4808,7 @@ int ntfs_get_file_attributes(struct SECURITY_API *scapi, const char *path)
        if (scapi && (scapi->magic == MAGIC_API) && path) {
                ni = ntfs_pathname_to_inode(scapi->security.vol, NULL, path);
                if (ni) {
-                       attrib = le32_to_cpu(ni->flags);
-                       if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)
-                               attrib |= const_le32_to_cpu(FILE_ATTR_DIRECTORY);
-                       else
-                               attrib &= ~const_le32_to_cpu(FILE_ATTR_DIRECTORY);
-                       if (!attrib)
-                               attrib |= const_le32_to_cpu(FILE_ATTR_NORMAL);
-
+                       attrib = ntfs_get_inode_attributes(ni);
                        ntfs_inode_close(ni);
                } else
                        errno = ENOENT;
@@ -4793,91 +4840,24 @@ BOOL ntfs_set_file_attributes(struct SECURITY_API *scapi,
                const char *path, s32 attrib)
 {
        ntfs_inode *ni;
-       le32 settable;
-       ATTR_FLAGS dirflags;
        int res;
 
        res = 0; /* default return */
        if (scapi && (scapi->magic == MAGIC_API) && path) {
                ni = ntfs_pathname_to_inode(scapi->security.vol, NULL, path);
                if (ni) {
-                       settable = FILE_ATTR_SETTABLE;
-                       if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
-                               /*
-                                * Accept changing compression for a directory
-                                * and set index root accordingly
-                                */
-                               settable |= FILE_ATTR_COMPRESSED;
-                               if ((ni->flags ^ cpu_to_le32(attrib))
-                                            & FILE_ATTR_COMPRESSED) {
-                                       if (ni->flags & FILE_ATTR_COMPRESSED)
-                                               dirflags = const_cpu_to_le16(0);
-                                       else
-                                               dirflags = ATTR_IS_COMPRESSED;
-                                       res = ntfs_attr_set_flags(ni,
-                                               AT_INDEX_ROOT,
-                                               NTFS_INDEX_I30, 4,
-                                               dirflags,
-                                               ATTR_COMPRESSION_MASK);
-                               }
-                       }
-                       if (!res) {
-                               ni->flags = (ni->flags & ~settable)
-                                        | (cpu_to_le32(attrib) & settable);
-                               NInoSetDirty(ni);
-                               NInoFileNameSetDirty(ni);
-                       }
-                       if (!ntfs_inode_close(ni))
+                       /* Win32 convention here : -1 means successful */
+                       if (!ntfs_set_inode_attributes(ni, attrib))
                                res = -1;
+                       if (ntfs_inode_close(ni))
+                               res = 0;
                } else
                        errno = ENOENT;
        }
        return (res);
 }
-#endif
 
-/* 
- * Set attributes of a NTFS file given an inode
- *
- * Returns nonzero on success
- */
-int _ntfs_set_file_attributes(ntfs_inode *ni, s32 attrib)
-{
-       le32 settable = FILE_ATTR_SETTABLE;
-       ATTR_FLAGS dirflags;
-       int ret = 0;
 
-       if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) {
-               /*
-                * Accept changing compression for a directory
-                * and set index root accordingly
-                */
-               settable |= FILE_ATTR_COMPRESSED;
-               if ((ni->flags ^ cpu_to_le32(attrib))
-                            & FILE_ATTR_COMPRESSED) {
-                       if (ni->flags & FILE_ATTR_COMPRESSED)
-                               dirflags = const_cpu_to_le16(0);
-                       else
-                               dirflags = ATTR_IS_COMPRESSED;
-                       ret = ntfs_attr_set_flags(ni,
-                               AT_INDEX_ROOT,
-                               NTFS_INDEX_I30, 4,
-                               dirflags,
-                               ATTR_COMPRESSION_MASK);
-               }
-       }
-       if (ret == 0) {
-               ni->flags = (ni->flags & ~settable)
-                        | (cpu_to_le32(attrib) & settable);
-               NInoSetDirty(ni);
-               NInoFileNameSetDirty(ni);
-               ret = -1;
-       }
-       return ret;
-}
-
-
-#if 0
 BOOL ntfs_read_directory(struct SECURITY_API *scapi,
                const char *path, ntfs_filldir_t callback, void *context)
 {
@@ -5181,4 +5161,3 @@ BOOL ntfs_leave_file_security(struct SECURITY_API *scapi)
        return (ok);
 }
 
-#endif