From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id BDD301F47C for ; Tue, 3 Jan 2023 14:09:51 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pChz4-0002MU-Pc; Tue, 03 Jan 2023 09:09:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pChz3-0002Jd-7X for bug-gnulib@gnu.org; Tue, 03 Jan 2023 09:09:41 -0500 Received: from haproxy.adestotech.com ([217.163.77.122]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pChz0-0003zH-AV for bug-gnulib@gnu.org; Tue, 03 Jan 2023 09:09:40 -0500 Received: from skynet19.adestotech.com (unknown [192.168.129.19]) by haproxy.adestotech.com (Postfix) with ESMTP id ED43EA146E; Tue, 3 Jan 2023 14:09:31 +0000 (GMT) From: Ondrej Valousek To: bug-gnulib@gnu.org, kdudka@redhat.com Cc: Ondrej Valousek Subject: [PATCH] Use xattr (Linux) in copy-acl.c Date: Tue, 3 Jan 2023 15:08:17 +0100 Message-Id: <20230103140816.1121699-1-ondrej.valousek.xm@renesas.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=217.163.77.122; envelope-from=ondrej.valousek.xm@renesas.com; helo=haproxy.adestotech.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) AC_FROM_MANY_DOTS=1, BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org The following patch is aiming to improve & simplify ACL handling in copy-acl.c Changes/Benefits: * we no longer try to decode ACLs, instead we just copy the whole xattr with ACLs making the code simpler and faster * side effect is support for NFSv4 acls Disadvantages: * it pulls in dependency on libattr. We still need dependency on libacl because of set-acl.c which would have to be cared of separately. The idea I have here is to confirm if it's safe to replace set_acl() with chmod_or_fchmod() at least in Linux. It probably has something to do with ACL inheritance. Anyhow - with this patch copy_acl() does NOT honor inherited ACLs. It copies ACLs to dest file exactly - i.e. the same way the old code worked. * we can't do any ACLs conversions, but these are not (AFAIK) being done anyway in the old code (the old code only worked with Posix) The goal is for "cp -p" to preserve ACLs regardless of the type. I tried to experiment with variety of filesystems (posix and nfs) and it seems to me to work exactly the way the old code worked (plus handy support for NFSv4). Ondrej --- lib/copy-acl.c | 31 ++++++++++++++++++++++++++++++- m4/xattr.m4 | 43 +++++++++++++++++++++++++++++++++++++++++++ modules/acl | 1 + 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 m4/xattr.m4 diff --git a/lib/copy-acl.c b/lib/copy-acl.c index 5fc42b7f6..341d1f605 100644 --- a/lib/copy-acl.c +++ b/lib/copy-acl.c @@ -29,6 +29,20 @@ #define _(msgid) gettext (msgid) +#if USE_XATTR + +# include + +static int +copy_attr_permissions (const char *name, struct error_context *ctx) +{ + int action = attr_copy_action (name, ctx); + return action == ATTR_ACTION_PERMISSIONS; +} + +#endif /* USE_XATTR */ + + /* Copy access control lists from one file to another. If SOURCE_DESC is a valid file descriptor, use file descriptor operations, else use filename based operations on SRC_NAME. Likewise for DEST_DESC and @@ -43,7 +57,22 @@ int copy_acl (const char *src_name, int source_desc, const char *dst_name, int dest_desc, mode_t mode) { - int ret = qcopy_acl (src_name, source_desc, dst_name, dest_desc, mode); + int ret; +#ifdef USE_XATTR + ret = chmod_or_fchmod (dst_name, dest_desc, mode); + /* Rather than fiddling with acls one by one, we just copy the whole ACL xattrs + * (Posix or NFSv4). Of course, that won't address ACLs conversion + * (i.e. posix <-> nfs4) but we can't do it anyway, so for now, we don't care + */ + if(ret == 0) + ret = source_desc <= 0 && dest_desc <= 0 + ? attr_copy_file (src_name, dst_name, copy_attr_permissions, NULL) + : attr_copy_fd (src_name, source_desc, dst_name, dest_desc, copy_attr_permissions, NULL); +#else + /* no XATTR, so we proceed the old dusty way */ + ret = qcopy_acl (src_name, source_desc, dst_name, dest_desc, mode); +#endif + switch (ret) { case -2: diff --git a/m4/xattr.m4 b/m4/xattr.m4 new file mode 100644 index 000000000..5f9248939 --- /dev/null +++ b/m4/xattr.m4 @@ -0,0 +1,43 @@ +# xattr.m4 - check for Extended Attributes (Linux) +# serial 4 + +# Copyright (C) 2003-2021 Free Software Foundation, Inc. +# This file is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# Originally written by Andreas Gruenbacher. + +AC_DEFUN([gl_FUNC_XATTR], +[ + AC_ARG_ENABLE([xattr], + AS_HELP_STRING([--disable-xattr], + [do not support extended attributes]), + [use_xattr=$enableval], [use_xattr=yes]) + + LIB_XATTR= + AC_SUBST([LIB_XATTR]) + + if test "$use_xattr" = "yes"; then + AC_CHECK_HEADERS([attr/error_context.h attr/libattr.h]) + use_xattr=no + if test $ac_cv_header_attr_libattr_h = yes \ + && test $ac_cv_header_attr_error_context_h = yes; then + xattr_saved_LIBS=$LIBS + AC_SEARCH_LIBS([attr_copy_file], [attr], + [test "$ac_cv_search_attr_copy_file" = "none required" || + LIB_XATTR=$ac_cv_search_attr_copy_file]) + AC_CHECK_FUNCS([attr_copy_file]) + LIBS=$xattr_saved_LIBS + if test $ac_cv_func_attr_copy_file = yes; then + use_xattr=yes + fi + fi + if test $use_xattr = no; then + AC_MSG_WARN([libattr development library was not found or not usable.]) + AC_MSG_WARN([AC_PACKAGE_NAME will be built without xattr support.]) + fi + fi + AC_DEFINE_UNQUOTED([USE_XATTR], [`test $use_xattr != yes; echo $?`], + [Define if you want extended attribute support.]) +]) diff --git a/modules/acl b/modules/acl index 1a3a14e6c..ca2239823 100644 --- a/modules/acl +++ b/modules/acl @@ -4,6 +4,7 @@ Access control lists of files, with diagnostics. (Unportable.) Files: lib/copy-acl.c lib/set-acl.c +m4/xattr.m4 Depends-on: error -- 2.38.1