diff mbox series

[cifs-utils,v1] add cifsfileinfo utility

Message ID 20180916175114.13269-1-aaptel@suse.com (mailing list archive)
State New, archived
Headers show
Series [cifs-utils,v1] add cifsfileinfo utility | expand

Commit Message

Aurélien Aptel Sept. 16, 2018, 5:51 p.m. UTC
Userspace helper to display SMB-specific file information using the
new CIFS_QUERY_INFO IOCTL.

Signed-off-by: Aurelien Aptel <aaptel@suse.com>
---
 Makefile.am      |   6 ++
 cifsfileinfo.c   | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cifsfileinfo.rst |  69 ++++++++++++++++++++
 configure.ac     |   6 ++
 4 files changed, 269 insertions(+)
 create mode 100644 cifsfileinfo.c
 create mode 100644 cifsfileinfo.rst

Comments

Ronnie Sahlberg Sept. 17, 2018, 10:50 p.m. UTC | #1
Nice,
but we should extend it so that it can print any of the QUERY_INFO levels.

I.e. smbstat secdesc <file>
or
smbstat file_ea_information <file>
etc.

And then we implement all the classes/types from smb2.


But I will need to update my patch and add two new fields to the structure.
We need to add an output_buffer_length from userspace with any
data we want to pass in the QUERY_INFO as the buffer field.
We need this so that we can implement QUERY_QUOTA.

But then, if we add the capability to pass data to the server as part of QUERY_INFO,
that is very similar to SET_INFO
so we should add a flags field to the data structure as well, MustBeZero for now
but we can add a SET_INFO flag later on and then use the same ioctl for both query and set
info.

What do you think?

regards
ronnie sahlberg



----- Original Message -----
From: "Aurelien Aptel" <aaptel@suse.com>
To: linux-cifs@vger.kernel.org
Cc: lsahlber@redhat.com, piastryyy@gmail.com, "Aurelien Aptel" <aaptel@suse.com>
Sent: Monday, 17 September, 2018 3:51:14 AM
Subject: [cifs-utils PATCH v1] add cifsfileinfo utility

Userspace helper to display SMB-specific file information using the
new CIFS_QUERY_INFO IOCTL.

Signed-off-by: Aurelien Aptel <aaptel@suse.com>
---
 Makefile.am      |   6 ++
 cifsfileinfo.c   | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cifsfileinfo.rst |  69 ++++++++++++++++++++
 configure.ac     |   6 ++
 4 files changed, 269 insertions(+)
 create mode 100644 cifsfileinfo.c
 create mode 100644 cifsfileinfo.rst

diff --git a/Makefile.am b/Makefile.am
index f37c9ae..4a407cf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -79,6 +79,12 @@ setcifsacl.rst: setcifsacl.rst.in
 	$(SED) 's,[@]pluginpath@,$(pluginpath),' $(srcdir)/$@.in > $@-t && mv $@-t $@
 endif
 
+if CONFIG_CIFSFILEINFO
+bin_PROGRAMS += cifsfileinfo
+cifsfileinfo_SOURCES = cifsfileinfo.c
+rst_man_pages += cifsfileinfo.1
+endif
+
 if CONFIG_PLUGIN
 plugindir = $(pkglibdir)
 plugin_PROGRAMS = idmapwb.so
diff --git a/cifsfileinfo.c b/cifsfileinfo.c
new file mode 100644
index 0000000..315dd00
--- /dev/null
+++ b/cifsfileinfo.c
@@ -0,0 +1,188 @@
+/*
+ * cifsfileinfo
+ *
+ * Copyright (C) Ronnie Sahlberg (lsahlberg@redhat.com) 2018
+ * Copyright (C) Aurelien Aptel (aaptel@suse.com) 2018
+ *
+ * Display SMB-specific file information using cifs IOCTL
+ *
+ * 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
+ * (at your option) any later version.
+ * This program 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <endian.h>
+#include <errno.h>
+#include <getopt.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#define CIFS_IOCTL_MAGIC 0xCF
+struct smb_query_info {
+	uint32_t input_buffer_length;
+	uint32_t info_type;
+	uint32_t file_info_class;
+	uint32_t additional_information;
+} __packed;
+
+#define CIFS_QUERY_INFO _IOWR(CIFS_IOCTL_MAGIC, 7, struct smb_query_info)
+#define INPUT_BUFFER_LENGTH 16384
+
+static void
+usage(char *name)
+{
+	fprintf(stderr, "Usage: %s <file>\n"
+		"  Prints file information for cifs file.\n",
+		name);
+	exit(1);
+}
+
+static void
+print_sid(unsigned char *sd)
+{
+	int i;
+	uint32_t subauth;
+	uint64_t idauth;
+
+	if (sd[0] != 1) {
+		fprintf(stderr, "Unknown SID revision\n");
+		return;
+	}
+
+	idauth = 0;
+	for (i = 0; i < 6; i++)
+		idauth = (idauth << 8) | sd[2 + i];
+
+	printf("S-1-%" PRIu64, idauth);
+	for (i = 0; i < sd[1]; i++) {
+		memcpy(&subauth, &sd[8 + 4 * i], 4);
+		subauth = le32toh(subauth);
+		printf("-%d", subauth);
+	}
+}
+
+static void
+print_acl(unsigned char *sd)
+{
+	int i, j, off;
+	uint16_t count, size;
+
+	if (sd[0] != 2) {
+		fprintf(stderr, "Unknown ACL revision\n");
+		return;
+	}
+
+	memcpy(&count, &sd[4], 2);
+	count = le16toh(count);
+	off = 8;
+	for (i = 0; i < count; i++) {
+		printf("Type:%02x Flags:%02x ", sd[off], sd[off + 1]);
+		memcpy(&size, &sd[off + 2], 2);
+		size = le16toh(size);
+
+		for (j = 0; j < size; j++)
+			printf("%02x", sd[off + 4 + j]);
+
+		off += size;
+		printf("\n");
+	}
+}
+
+static void
+print_sd(uint8_t *sd)
+{
+	int offset_owner, offset_group, offset_dacl;
+
+	printf("Revision:%d\n", sd[0]);
+	if (sd[0] != 1) {
+		fprintf(stderr, "Unknown SD revision\n");
+		exit(1);
+	}
+
+	printf("Control: %02x%02x\n", sd[2], sd[3]);
+
+	memcpy(&offset_owner, &sd[4], 4);
+	offset_owner = le32toh(offset_owner);
+	memcpy(&offset_group, &sd[8], 4);
+	offset_group = le32toh(offset_group);
+	memcpy(&offset_dacl, &sd[16], 4);
+	offset_dacl = le32toh(offset_dacl);
+
+	if (offset_owner) {
+		printf("Owner: ");
+		print_sid(&sd[offset_owner]);
+		printf("\n");
+	}
+	if (offset_group) {
+		printf("Group: ");
+		print_sid(&sd[offset_group]);
+		printf("\n");
+	}
+	if (offset_dacl) {
+		printf("DACL:\n");
+		print_acl(&sd[offset_dacl]);
+	}
+}
+
+
+int main(int argc, char *argv[])
+{
+	int c;
+	int f;
+	struct smb_query_info *qi;
+
+	while ((c = getopt_long(argc, argv, "v", NULL, NULL)) != -1) {
+		switch (c) {
+		case 'v':
+			printf("cifsfileinfo version %s\n", VERSION);
+			return 0;
+		default:
+			usage(argv[0]);
+		}
+	}
+
+	if (optind >= argc)
+		usage(argv[0]);
+
+	if ((f = open(argv[optind], O_RDONLY)) < 0) {
+		fprintf(stderr, "Failed to open %s\n", argv[optind]);
+		exit(1);
+	}
+
+
+	qi = malloc(sizeof(struct smb_query_info) + INPUT_BUFFER_LENGTH);
+	qi->info_type = 0x03;
+	qi->file_info_class = 0;
+	qi->additional_information = 0x00000007; /* Owner, Group, Dacl */
+	qi->input_buffer_length = INPUT_BUFFER_LENGTH;
+
+	if (ioctl(f, CIFS_QUERY_INFO, qi) < 0) {
+		fprintf(stderr, "ioctl failed with %d\n", errno);
+		exit(1);
+	}
+
+	print_sd((uint8_t *)(&qi[1]));
+
+	close(f);
+	return 0;
+}
diff --git a/cifsfileinfo.rst b/cifsfileinfo.rst
new file mode 100644
index 0000000..0851a6a
--- /dev/null
+++ b/cifsfileinfo.rst
@@ -0,0 +1,69 @@
+============
+cifsfileinfo
+============
+
+-----------------------------------------------------------------------------------------------------
+Userspace helper to display SMB-specific file information for the Linux SMB client file system (CIFS)
+-----------------------------------------------------------------------------------------------------
+:Manual section: 1
+
+********
+SYNOPSIS
+********
+
+  cifsfileinfo [-v] {file system object}
+
+***********
+DESCRIPTION
+***********
+
+This tool is part of the cifs-utils suite.
+
+``cifsfileinfo`` is a userspace helper program for the Linux SMB
+client file system (CIFS). It is intended to display SMB-specific file
+informations such as:
+
+- Revision
+- Control
+- Owner SID
+- Group SID
+- ACL
+- File types
+- File flags
+
+This tool works by making an CIFS_QUERY_INFO IOCTL call to the Linux
+SMB client which in turn issues a SMB Query Info request and returns
+the result. This differs from ``getcifsacl`` which uses extended file
+attributes.
+
+*******
+OPTIONS
+*******
+
+-v
+  Print version number and exit.
+
+
+*****
+NOTES
+*****
+
+Kernel support for cifsfileinfo utilities requires the CIFS_QUERY_INFO
+IOCTL which was initially introduced in the XXX kernel and is only
+implemented for mount points using SMB2 or above (see mount.cifs(8)
+``vers`` option).
+
+********
+SEE ALSO
+********
+
+mount.cifs(8), getcifsacl(1)
+
+******
+AUTHOR
+******
+
+Ronnie Sahlberg wrote the cifsfileinfo program.
+
+The Linux CIFS Mailing list is the preferred place to ask questions
+regarding these programs.
diff --git a/configure.ac b/configure.ac
index 8e3d6ce..01c4c2c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,6 +40,11 @@ AC_ARG_ENABLE(cifsacl,
 	enable_cifsacl=$enableval,
 	enable_cifsacl="maybe")
 
+AC_ARG_ENABLE(cifsfileinfo,
+	[AS_HELP_STRING([--enable-cifsfileinfo],[Create cifsfileinfo binary @<:@default=yes@:>@])],
+	enable_cifsfileinfo=$enableval,
+	enable_cifsfileinfo="maybe")
+
 AC_ARG_ENABLE(pam,
 	[AS_HELP_STRING([--enable-pam],[Create cifscreds PAM module @<:@default=yes@:>@])],
 	enable_pam=$enableval,
@@ -275,6 +280,7 @@ AM_CONDITIONAL(CONFIG_CIFSUPCALL, [test "$enable_cifsupcall" != "no"])
 AM_CONDITIONAL(CONFIG_CIFSCREDS, [test "$enable_cifscreds" != "no"])
 AM_CONDITIONAL(CONFIG_CIFSIDMAP, [test "$enable_cifsidmap" != "no"])
 AM_CONDITIONAL(CONFIG_CIFSACL, [test "$enable_cifsacl" != "no"])
+AM_CONDITIONAL(CONFIG_CIFSFILEINFO, [test "$enable_cifsfileinfo" != "no"])
 AM_CONDITIONAL(CONFIG_PAM, [test "$enable_pam" != "no"])
 AM_CONDITIONAL(CONFIG_PLUGIN, [test "$enable_cifsidmap" != "no" -o "$enable_cifsacl" != "no"])
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index f37c9ae..4a407cf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -79,6 +79,12 @@  setcifsacl.rst: setcifsacl.rst.in
 	$(SED) 's,[@]pluginpath@,$(pluginpath),' $(srcdir)/$@.in > $@-t && mv $@-t $@
 endif
 
+if CONFIG_CIFSFILEINFO
+bin_PROGRAMS += cifsfileinfo
+cifsfileinfo_SOURCES = cifsfileinfo.c
+rst_man_pages += cifsfileinfo.1
+endif
+
 if CONFIG_PLUGIN
 plugindir = $(pkglibdir)
 plugin_PROGRAMS = idmapwb.so
diff --git a/cifsfileinfo.c b/cifsfileinfo.c
new file mode 100644
index 0000000..315dd00
--- /dev/null
+++ b/cifsfileinfo.c
@@ -0,0 +1,188 @@ 
+/*
+ * cifsfileinfo
+ *
+ * Copyright (C) Ronnie Sahlberg (lsahlberg@redhat.com) 2018
+ * Copyright (C) Aurelien Aptel (aaptel@suse.com) 2018
+ *
+ * Display SMB-specific file information using cifs IOCTL
+ *
+ * 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
+ * (at your option) any later version.
+ * This program 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <endian.h>
+#include <errno.h>
+#include <getopt.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+#define CIFS_IOCTL_MAGIC 0xCF
+struct smb_query_info {
+	uint32_t input_buffer_length;
+	uint32_t info_type;
+	uint32_t file_info_class;
+	uint32_t additional_information;
+} __packed;
+
+#define CIFS_QUERY_INFO _IOWR(CIFS_IOCTL_MAGIC, 7, struct smb_query_info)
+#define INPUT_BUFFER_LENGTH 16384
+
+static void
+usage(char *name)
+{
+	fprintf(stderr, "Usage: %s <file>\n"
+		"  Prints file information for cifs file.\n",
+		name);
+	exit(1);
+}
+
+static void
+print_sid(unsigned char *sd)
+{
+	int i;
+	uint32_t subauth;
+	uint64_t idauth;
+
+	if (sd[0] != 1) {
+		fprintf(stderr, "Unknown SID revision\n");
+		return;
+	}
+
+	idauth = 0;
+	for (i = 0; i < 6; i++)
+		idauth = (idauth << 8) | sd[2 + i];
+
+	printf("S-1-%" PRIu64, idauth);
+	for (i = 0; i < sd[1]; i++) {
+		memcpy(&subauth, &sd[8 + 4 * i], 4);
+		subauth = le32toh(subauth);
+		printf("-%d", subauth);
+	}
+}
+
+static void
+print_acl(unsigned char *sd)
+{
+	int i, j, off;
+	uint16_t count, size;
+
+	if (sd[0] != 2) {
+		fprintf(stderr, "Unknown ACL revision\n");
+		return;
+	}
+
+	memcpy(&count, &sd[4], 2);
+	count = le16toh(count);
+	off = 8;
+	for (i = 0; i < count; i++) {
+		printf("Type:%02x Flags:%02x ", sd[off], sd[off + 1]);
+		memcpy(&size, &sd[off + 2], 2);
+		size = le16toh(size);
+
+		for (j = 0; j < size; j++)
+			printf("%02x", sd[off + 4 + j]);
+
+		off += size;
+		printf("\n");
+	}
+}
+
+static void
+print_sd(uint8_t *sd)
+{
+	int offset_owner, offset_group, offset_dacl;
+
+	printf("Revision:%d\n", sd[0]);
+	if (sd[0] != 1) {
+		fprintf(stderr, "Unknown SD revision\n");
+		exit(1);
+	}
+
+	printf("Control: %02x%02x\n", sd[2], sd[3]);
+
+	memcpy(&offset_owner, &sd[4], 4);
+	offset_owner = le32toh(offset_owner);
+	memcpy(&offset_group, &sd[8], 4);
+	offset_group = le32toh(offset_group);
+	memcpy(&offset_dacl, &sd[16], 4);
+	offset_dacl = le32toh(offset_dacl);
+
+	if (offset_owner) {
+		printf("Owner: ");
+		print_sid(&sd[offset_owner]);
+		printf("\n");
+	}
+	if (offset_group) {
+		printf("Group: ");
+		print_sid(&sd[offset_group]);
+		printf("\n");
+	}
+	if (offset_dacl) {
+		printf("DACL:\n");
+		print_acl(&sd[offset_dacl]);
+	}
+}
+
+
+int main(int argc, char *argv[])
+{
+	int c;
+	int f;
+	struct smb_query_info *qi;
+
+	while ((c = getopt_long(argc, argv, "v", NULL, NULL)) != -1) {
+		switch (c) {
+		case 'v':
+			printf("cifsfileinfo version %s\n", VERSION);
+			return 0;
+		default:
+			usage(argv[0]);
+		}
+	}
+
+	if (optind >= argc)
+		usage(argv[0]);
+
+	if ((f = open(argv[optind], O_RDONLY)) < 0) {
+		fprintf(stderr, "Failed to open %s\n", argv[optind]);
+		exit(1);
+	}
+
+
+	qi = malloc(sizeof(struct smb_query_info) + INPUT_BUFFER_LENGTH);
+	qi->info_type = 0x03;
+	qi->file_info_class = 0;
+	qi->additional_information = 0x00000007; /* Owner, Group, Dacl */
+	qi->input_buffer_length = INPUT_BUFFER_LENGTH;
+
+	if (ioctl(f, CIFS_QUERY_INFO, qi) < 0) {
+		fprintf(stderr, "ioctl failed with %d\n", errno);
+		exit(1);
+	}
+
+	print_sd((uint8_t *)(&qi[1]));
+
+	close(f);
+	return 0;
+}
diff --git a/cifsfileinfo.rst b/cifsfileinfo.rst
new file mode 100644
index 0000000..0851a6a
--- /dev/null
+++ b/cifsfileinfo.rst
@@ -0,0 +1,69 @@ 
+============
+cifsfileinfo
+============
+
+-----------------------------------------------------------------------------------------------------
+Userspace helper to display SMB-specific file information for the Linux SMB client file system (CIFS)
+-----------------------------------------------------------------------------------------------------
+:Manual section: 1
+
+********
+SYNOPSIS
+********
+
+  cifsfileinfo [-v] {file system object}
+
+***********
+DESCRIPTION
+***********
+
+This tool is part of the cifs-utils suite.
+
+``cifsfileinfo`` is a userspace helper program for the Linux SMB
+client file system (CIFS). It is intended to display SMB-specific file
+informations such as:
+
+- Revision
+- Control
+- Owner SID
+- Group SID
+- ACL
+- File types
+- File flags
+
+This tool works by making an CIFS_QUERY_INFO IOCTL call to the Linux
+SMB client which in turn issues a SMB Query Info request and returns
+the result. This differs from ``getcifsacl`` which uses extended file
+attributes.
+
+*******
+OPTIONS
+*******
+
+-v
+  Print version number and exit.
+
+
+*****
+NOTES
+*****
+
+Kernel support for cifsfileinfo utilities requires the CIFS_QUERY_INFO
+IOCTL which was initially introduced in the XXX kernel and is only
+implemented for mount points using SMB2 or above (see mount.cifs(8)
+``vers`` option).
+
+********
+SEE ALSO
+********
+
+mount.cifs(8), getcifsacl(1)
+
+******
+AUTHOR
+******
+
+Ronnie Sahlberg wrote the cifsfileinfo program.
+
+The Linux CIFS Mailing list is the preferred place to ask questions
+regarding these programs.
diff --git a/configure.ac b/configure.ac
index 8e3d6ce..01c4c2c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -40,6 +40,11 @@  AC_ARG_ENABLE(cifsacl,
 	enable_cifsacl=$enableval,
 	enable_cifsacl="maybe")
 
+AC_ARG_ENABLE(cifsfileinfo,
+	[AS_HELP_STRING([--enable-cifsfileinfo],[Create cifsfileinfo binary @<:@default=yes@:>@])],
+	enable_cifsfileinfo=$enableval,
+	enable_cifsfileinfo="maybe")
+
 AC_ARG_ENABLE(pam,
 	[AS_HELP_STRING([--enable-pam],[Create cifscreds PAM module @<:@default=yes@:>@])],
 	enable_pam=$enableval,
@@ -275,6 +280,7 @@  AM_CONDITIONAL(CONFIG_CIFSUPCALL, [test "$enable_cifsupcall" != "no"])
 AM_CONDITIONAL(CONFIG_CIFSCREDS, [test "$enable_cifscreds" != "no"])
 AM_CONDITIONAL(CONFIG_CIFSIDMAP, [test "$enable_cifsidmap" != "no"])
 AM_CONDITIONAL(CONFIG_CIFSACL, [test "$enable_cifsacl" != "no"])
+AM_CONDITIONAL(CONFIG_CIFSFILEINFO, [test "$enable_cifsfileinfo" != "no"])
 AM_CONDITIONAL(CONFIG_PAM, [test "$enable_pam" != "no"])
 AM_CONDITIONAL(CONFIG_PLUGIN, [test "$enable_cifsidmap" != "no" -o "$enable_cifsacl" != "no"])