diff mbox series

[5/6] nfs4_setacl: Add support for the --dacl and --sacl options

Message ID 20220514144436.4298-6-trondmy@kernel.org (mailing list archive)
State New, archived
Headers show
Series Allow nfs4-acl-tools to access 'dacl' and 'sacl' | expand

Commit Message

Trond Myklebust May 14, 2022, 2:44 p.m. UTC
From: Trond Myklebust <trond.myklebust@hammerspace.com>

Add support for the NFSv4.1 dacl and sacl attributes.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
---
 nfs4_setfacl/nfs4_setfacl.c | 67 +++++++++++++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/nfs4_setfacl/nfs4_setfacl.c b/nfs4_setfacl/nfs4_setfacl.c
index d0485ad53024..e5816085c8b0 100644
--- a/nfs4_setfacl/nfs4_setfacl.c
+++ b/nfs4_setfacl/nfs4_setfacl.c
@@ -79,6 +79,9 @@ 
 #define EDITOR  		"vi"  /* <- evangelism! */
 #define u32 u_int32_t
 
+#define OPT_DACL	0x98
+#define OPT_SACL	0x99
+
 static int apply_action(const char *, const struct stat *, int, struct FTW *);
 static int do_apply_action(const char *, const struct stat *);
 static int open_editor(const char *);
@@ -110,6 +113,8 @@  static struct option long_options[] = {
 	{ "recursive",		0, 0, 'R' },
 	{ "physical",		0, 0, 'P' },
 	{ "logical",		0, 0, 'L' },
+	{ "dacl",		0, 0, OPT_DACL },
+	{ "sacl",		0, 0, OPT_SACL },
 	{ NULL,			0, 0, 0,  },
 };
 
@@ -124,6 +129,8 @@  static char *mod_string;
 static char *from_ace;
 static char *to_ace;
 
+static enum acl_type acl_type = ACL_TYPE_ACL;
+
 /* XXX: things we need to handle:
  *
  *  - we need some sort of 'purge' operation that completely clears an ACL.
@@ -272,6 +279,13 @@  int main(int argc, char **argv)
 				paths[numpaths++] = optarg;
 				break;
 
+			case OPT_DACL:
+				acl_type = ACL_TYPE_DACL;
+				break;
+			case OPT_SACL:
+				acl_type = ACL_TYPE_SACL;
+				break;
+
 			case 'h':
 			case '?':
 			default:
@@ -334,6 +348,50 @@  out:
 	return err;
 }
 
+static void nfs4_print_acl_error(const char *path)
+{
+	switch (errno) {
+	case ENODATA:
+		fprintf(stderr,"Attribute not found on file: %s\n", path);
+		break;
+	case EREMOTEIO:
+		fprintf(stderr,"An NFS server error occurred.\n");
+		break;
+	case EOPNOTSUPP:
+		fprintf(stderr,"Operation to request attribute not supported: "
+			       "%s\n", path);
+		break;
+	default:
+		perror("Failed operation");
+	}
+}
+
+static struct nfs4_acl *nfs4_retrieve_acl(const char *path,
+					  enum acl_type type)
+{
+	switch (type) {
+	case ACL_TYPE_DACL:
+		return nfs4_getdacl(path);
+	case ACL_TYPE_SACL:
+		return nfs4_getsacl(path);
+	default:
+		return nfs4_getacl(path);
+	}
+}
+
+static int nfs4_apply_acl(const char *path, struct nfs4_acl *acl,
+			  enum acl_type type)
+{
+	switch (type) {
+	case ACL_TYPE_DACL:
+		return nfs4_setdacl(path, acl);
+	case ACL_TYPE_SACL:
+		return nfs4_setsacl(path, acl);
+	default:
+		return nfs4_setacl(path, acl);
+	}
+}
+
 /* returns 0 on success, nonzero on failure */
 static int apply_action(const char *_path, const struct stat *stat, int flag, struct FTW *ftw)
 {
@@ -378,7 +436,7 @@  static int do_apply_action(const char *path, const struct stat *_st)
 	if (action == SUBSTITUTE_ACTION)
 		acl = nfs4_new_acl(S_ISDIR(st->st_mode));
 	else
-		acl = nfs4_acl_for_path(path);
+		acl = nfs4_retrieve_acl(path, acl_type);
 
 	if (acl == NULL) {
 		fprintf(stderr, "Failed to instantiate ACL.\n");
@@ -438,8 +496,11 @@  static int do_apply_action(const char *path, const struct stat *_st)
 	if (is_test) {
 		fprintf(stderr, "## Test mode only - the resulting ACL for \"%s\": \n", path);
 		nfs4_print_acl(stdout, acl);
-	} else
-		err = nfs4_set_acl(acl, path);
+	} else {
+		err = nfs4_apply_acl(path, acl, acl_type);
+		if (err == -1)
+			nfs4_print_acl_error(path);
+	}
 
 out:
 	nfs4_free_acl(acl);