From patchwork Sun Sep 16 17:51:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Aur=C3=A9lien_Aptel?= X-Patchwork-Id: 970336 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-cifs-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.com Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42CxdT10lbz9sBv for ; Mon, 17 Sep 2018 03:51:25 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728300AbeIPXPE (ORCPT ); Sun, 16 Sep 2018 19:15:04 -0400 Received: from mx2.suse.de ([195.135.220.15]:54952 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728239AbeIPXPE (ORCPT ); Sun, 16 Sep 2018 19:15:04 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id D3020AE6C; Sun, 16 Sep 2018 17:51:20 +0000 (UTC) From: Aurelien Aptel To: linux-cifs@vger.kernel.org Cc: lsahlber@redhat.com, piastryyy@gmail.com, Aurelien Aptel Subject: [cifs-utils PATCH v1] add cifsfileinfo utility Date: Sun, 16 Sep 2018 10:51:14 -0700 Message-Id: <20180916175114.13269-1-aaptel@suse.com> X-Mailer: git-send-email 2.13.7 In-Reply-To: <875zz58hcm.fsf@suse.com> References: <875zz58hcm.fsf@suse.com> Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org Userspace helper to display SMB-specific file information using the new CIFS_QUERY_INFO IOCTL. Signed-off-by: Aurelien Aptel Signed-off-by: Aurelien Aptel --- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 \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"])