From: trondmy@kernel.org
To: Steve Dickson <SteveD@redhat.com>,
"J.Bruce Fields" <bfields@fieldses.org>
Cc: linux-nfs@vger.kernel.org
Subject: [PATCH 1/6] libnfs4acl: Add helpers to set the dacl and sacl
Date: Sat, 14 May 2022 10:44:31 -0400 [thread overview]
Message-ID: <20220514144436.4298-2-trondmy@kernel.org> (raw)
In-Reply-To: <20220514144436.4298-1-trondmy@kernel.org>
From: Trond Myklebust <trond.myklebust@hammerspace.com>
Add helper functions to set the NFSv4.1 dacl and sacl attributes.
Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
---
include/libacl_nfs4.h | 9 +++++
libnfs4acl/Makefile | 2 +
libnfs4acl/nfs4_getacl.c | 83 ++++++++++++++++++++++++++++++++++++++++
libnfs4acl/nfs4_setacl.c | 49 ++++++++++++++++++++++++
4 files changed, 143 insertions(+)
create mode 100644 libnfs4acl/nfs4_getacl.c
create mode 100644 libnfs4acl/nfs4_setacl.c
diff --git a/include/libacl_nfs4.h b/include/libacl_nfs4.h
index d3786c3fabdc..76bbe90af54d 100644
--- a/include/libacl_nfs4.h
+++ b/include/libacl_nfs4.h
@@ -123,6 +123,8 @@
/* NFS4 acl xattr name */
#define ACL_NFS4_XATTR "system.nfs4_acl"
+#define DACL_NFS4_XATTR "system.nfs4_dacl"
+#define SACL_NFS4_XATTR "system.nfs4_sacl"
/* Macro for finding empty tailqs */
#define TAILQ_IS_EMPTY(head) (head.tqh_first == NULL)
@@ -152,6 +154,13 @@ TAILQ_HEAD(ace_container_list_head, ace_container);
/**** Public functions ****/
+extern struct nfs4_acl * nfs4_getacl(const char *path);
+extern struct nfs4_acl * nfs4_getdacl(const char *path);
+extern struct nfs4_acl * nfs4_getsacl(const char *path);
+extern int nfs4_setacl(const char *path, struct nfs4_acl *acl);
+extern int nfs4_setdacl(const char *path, struct nfs4_acl *acl);
+extern int nfs4_setsacl(const char *path, struct nfs4_acl *acl);
+
/** Manipulation functions **/
extern int acl_nfs4_set_who(struct nfs4_ace*, int, char*);
extern struct nfs4_acl * acl_nfs4_copy_acl(struct nfs4_acl *);
diff --git a/libnfs4acl/Makefile b/libnfs4acl/Makefile
index a598d4ee141f..556b59535e26 100644
--- a/libnfs4acl/Makefile
+++ b/libnfs4acl/Makefile
@@ -92,6 +92,8 @@ LIBACL_NFS4_CFILES = \
nfs4_get_ace_access.c \
nfs4_get_ace_flags.c \
nfs4_get_ace_type.c \
+ nfs4_getacl.c \
+ nfs4_setacl.c \
nfs4_insert_file_aces.c \
nfs4_insert_string_aces.c \
nfs4_free_acl.c \
diff --git a/libnfs4acl/nfs4_getacl.c b/libnfs4acl/nfs4_getacl.c
new file mode 100644
index 000000000000..753ba9167459
--- /dev/null
+++ b/libnfs4acl/nfs4_getacl.c
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2022, Trond Myklebust <trond.myklebust@hammerspace.com>
+ *
+ * This code is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This code 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.
+ */
+
+#include <sys/types.h>
+#include <config.h>
+#ifdef HAVE_ATTR_XATTR_H
+# include <attr/xattr.h>
+#else
+# ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+# endif
+#endif
+#include <sys/stat.h>
+#include "libacl_nfs4.h"
+
+/* returns a newly-allocated struct nfs4_acl or NULL on error. */
+static struct nfs4_acl *nfs4_getacl_byname(const char *path,
+ const char *xattr_name)
+{
+ struct nfs4_acl *acl;
+ struct stat st;
+ void *buf;
+ ssize_t ret;
+ u32 iflags = NFS4_ACL_ISFILE;
+
+ if (path == NULL || *path == 0) {
+ errno = EFAULT;
+ return NULL;
+ }
+
+ /* find necessary buffer size */
+ ret = getxattr(path, xattr_name, NULL, 0);
+ if (ret == -1)
+ goto err;
+
+ buf = malloc(ret);
+ if (!buf)
+ goto err;
+
+ /* reconstruct the ACL */
+ ret = getxattr(path, xattr_name, buf, ret);
+ if (ret == -1)
+ goto err_free;
+
+ ret = stat(path, &st);
+ if (ret == -1)
+ goto err_free;
+
+ if (S_ISDIR(st.st_mode))
+ iflags = NFS4_ACL_ISDIR;
+
+ acl = acl_nfs4_xattr_load(buf, ret, iflags);
+
+ free(buf);
+ return acl;
+err_free:
+ free(buf);
+err:
+ return NULL;
+}
+
+struct nfs4_acl *nfs4_getacl(const char *path)
+{
+ return nfs4_getacl_byname(path, ACL_NFS4_XATTR);
+}
+struct nfs4_acl *nfs4_getdacl(const char *path)
+{
+ return nfs4_getacl_byname(path, DACL_NFS4_XATTR);
+}
+struct nfs4_acl *nfs4_getsacl(const char *path)
+{
+ return nfs4_getacl_byname(path, SACL_NFS4_XATTR);
+}
diff --git a/libnfs4acl/nfs4_setacl.c b/libnfs4acl/nfs4_setacl.c
new file mode 100644
index 000000000000..298365ec67c5
--- /dev/null
+++ b/libnfs4acl/nfs4_setacl.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2022, Trond Myklebust <trond.myklebust@hammerspace.com>
+ *
+ * This code is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This code 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.
+ */
+
+#include <sys/types.h>
+#include <config.h>
+#ifdef HAVE_ATTR_XATTR_H
+# include <attr/xattr.h>
+#else
+# ifdef HAVE_SYS_XATTR_H
+# include <sys/xattr.h>
+# endif
+#endif
+#include "libacl_nfs4.h"
+
+static int nfs4_setacl_byname(const char *path, const char *xattr_name,
+ struct nfs4_acl *acl)
+{
+ char *xdrbuf = NULL;
+ int ret;
+
+ ret = acl_nfs4_xattr_pack(acl, &xdrbuf);
+ if (ret != -1)
+ ret = setxattr(path, xattr_name, xdrbuf, ret, XATTR_REPLACE);
+ free(xdrbuf);
+ return ret;
+}
+
+int nfs4_setacl(const char *path, struct nfs4_acl *acl)
+{
+ return nfs4_setacl_byname(path, ACL_NFS4_XATTR, acl);
+}
+int nfs4_setdacl(const char *path, struct nfs4_acl *acl)
+{
+ return nfs4_setacl_byname(path, DACL_NFS4_XATTR, acl);
+}
+int nfs4_setsacl(const char *path, struct nfs4_acl *acl)
+{
+ return nfs4_setacl_byname(path, SACL_NFS4_XATTR, acl);
+}
--
2.36.1
next prev parent reply other threads:[~2022-05-14 14:51 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-14 14:44 [PATCH 0/6] Allow nfs4-acl-tools to access 'dacl' and 'sacl' trondmy
2022-05-14 14:44 ` trondmy [this message]
2022-05-14 14:44 ` [PATCH 2/6] libnfs4acl: Add support for the NFS4.1 ACE_INHERITED_ACE flag trondmy
2022-05-14 14:44 ` [PATCH 3/6] The NFSv41 DACL and SACL prepend an extra field to the acl trondmy
2022-05-14 14:44 ` [PATCH 4/6] nfs4_getacl: Add support for the --dacl and --sacl options trondmy
2022-05-14 14:44 ` [PATCH 5/6] nfs4_setacl: " trondmy
2022-05-14 14:44 ` [PATCH 6/6] Edit manpages to document the new --dacl, --sacl and inheritance features trondmy
2022-05-15 1:59 ` [PATCH 0/6] Allow nfs4-acl-tools to access 'dacl' and 'sacl' J.Bruce Fields
2022-05-15 3:23 ` Trond Myklebust
2022-05-19 13:47 ` Steve Dickson
2022-05-19 13:53 ` bfields
2022-05-19 18:52 ` Steve Dickson
2022-05-19 19:01 ` bfields
2022-06-21 13:43 ` Steve Dickson
2022-06-21 13:58 ` J.Bruce Fields
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220514144436.4298-2-trondmy@kernel.org \
--to=trondmy@kernel.org \
--cc=SteveD@redhat.com \
--cc=bfields@fieldses.org \
--cc=linux-nfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).