diff mbox series

[4/6] nfs4_getacl: Add support for the --dacl and --sacl options

Message ID 20220514144436.4298-5-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_getfacl/nfs4_getfacl.c | 72 +++++++++++++++++++++++++++++++++----
 1 file changed, 65 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/nfs4_getfacl/nfs4_getfacl.c b/nfs4_getfacl/nfs4_getfacl.c
index 1222dd907c9e..954cf7edb19a 100644
--- a/nfs4_getfacl/nfs4_getfacl.c
+++ b/nfs4_getfacl/nfs4_getfacl.c
@@ -42,15 +42,30 @@ 
 #include <ftw.h>
 #include <getopt.h>
 
+#define OPT_DACL	0x98
+#define OPT_SACL	0x99
+
 static void usage(int);
 static void more_help();
 static char *execname;
-static void print_acl_from_path();
+static void print_acl_from_path(const char *, enum acl_type);
 static int ignore_comment = 0;
 
-static int recursive(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
+static int print_acl(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
+{
+	print_acl_from_path(fpath, ACL_TYPE_ACL);
+	return 0;
+}
+
+static int print_dacl(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
 {
-	print_acl_from_path(fpath);
+	print_acl_from_path(fpath, ACL_TYPE_DACL);
+	return 0;
+}
+
+static int print_sacl(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
+{
+	print_acl_from_path(fpath, ACL_TYPE_SACL);
 	return 0;
 }
 
@@ -59,6 +74,8 @@  static struct option long_options[] = {
         {"help",         0, 0, 'h' },
         {"recursive",     0, 0, 'R' },
         {"omit-header",  0, 0, 'c'},
+        {"dacl",         0, 0, OPT_DACL},
+        {"sacl",         0, 0, OPT_SACL},
         { NULL,          0, 0, 0,  },
 };
 
@@ -66,6 +83,9 @@  int main(int argc, char **argv)
 {
 	int opt, res = 1;
         int do_recursive = 0;
+	int (*recursive)(const char *fpath, const struct stat *sb,
+			 int tflag, struct FTW *ftwbuf) = print_acl;
+	enum acl_type type = ACL_TYPE_ACL;
 	
 	execname = basename(argv[0]);
 
@@ -88,6 +108,14 @@  int main(int argc, char **argv)
 			case 'c':
 				ignore_comment = 1;
 				break;
+			case OPT_DACL:
+				type = ACL_TYPE_DACL;
+				recursive = print_dacl;
+				break;
+			case OPT_SACL:
+				type = ACL_TYPE_SACL;
+				recursive = print_sacl;
+				break;
 			case 'h':
 				usage(1);
 				res = 0;
@@ -111,23 +139,51 @@  int main(int argc, char **argv)
 				printf("Invalid filename: %s\n", argv[optind]);
 		}
 		else
-			print_acl_from_path(argv[optind]);
+			print_acl_from_path(argv[optind], type);
 		res = 0;
 	}
 out:
 	return res;
 }
 
-static void print_acl_from_path(const char *fpath)
+static void print_acl_from_path(const char *fpath, enum acl_type type)
 {
 	struct nfs4_acl *acl;
-	acl = nfs4_acl_for_path(fpath);
+
+	switch (type) {
+	case ACL_TYPE_ACL:
+		acl = nfs4_getacl(fpath);
+		break;
+	case ACL_TYPE_DACL:
+		acl = nfs4_getdacl(fpath);
+		break;
+	case ACL_TYPE_SACL:
+		acl = nfs4_getsacl(fpath);
+		break;
+	}
+
 	if (acl != NULL) {
 		if (ignore_comment == 0)
 			printf("# file: %s\n", fpath);
 		nfs4_print_acl(stdout, acl);
 		printf("\n");
 		nfs4_free_acl(acl);
+	} else {
+		switch (errno) {
+		case ENODATA:
+			fprintf(stderr,"Attribute not found on file: %s\n",
+				fpath);
+			break;
+		case EREMOTEIO:
+			fprintf(stderr,"An NFS server error occurred.\n");
+			break;
+		case EOPNOTSUPP:
+                        fprintf(stderr,"Operation to request attribute not "
+				       "supported: %s\n", fpath);
+			break;
+		default:
+			perror("Failed operation");
+		}
 	}
 }
 
@@ -142,7 +198,9 @@  static void usage(int label)
 	"   -H, --more-help	 display ACL format information\n"
 	"   -h, --help		 display this help text\n"
 	"   -R, --recursive	 recurse into subdirectories\n"
-	"   -c, --omit-header	 Do not display the comment header (Do not print filename)\n";
+	"   -c, --omit-header	 Do not display the comment header (Do not print filename)\n"
+	"       --dacl           display the NFSv4.1 dacl\n"
+	"       --sacl           display the NFSv4.1 sacl\n";
 
 	fprintf(stderr, gfusage, execname);
 }