Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1413999iob; Sat, 14 May 2022 08:21:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0Fk4IyI8L1cX6NaAShKTZrE1M45FFb0mYDE9SK9WGaaq607qjcre1aCRrryBEnNgiHW/w X-Received: by 2002:a17:907:7f25:b0:6f4:c553:c73c with SMTP id qf37-20020a1709077f2500b006f4c553c73cmr8687087ejc.119.1652541688176; Sat, 14 May 2022 08:21:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652541688; cv=none; d=google.com; s=arc-20160816; b=xdVsr3jY8zbZX8/Q8EdvSS48WThClmPmHu7tl7zABRwSwgDS+gj7gmEp40LwzszRZy PWPt2DRGhwu/IDF7FfivdE/I2cxn6DlXOkEDvzLPtQX/aM8d08sgmEgnN6pT6cATPbsh 3L3s8RECox5KAuWL6shuJEnnuP+KakS7YbbeHf5fO6RrdMg3bNib0AJFkXOaJUDK752G XZII8vaJ/t8y2XBFrzYT11HmnRSfKIV8EDSuDaKUNewMqvaQiEB7l+UNZxUGR22bJ9y0 0gQq+rgcWTsWOwwDv/XSkDs2v8oG6ZVksrolg4AisVaY2HUYy+Xx27Q/sCpMXHP2YRf8 ZIBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=eGUirbcs/qsK2zwrQvql6cEhY+nxxbJV18Rv1TT7Dfo=; b=0dM6yR5VdOxyz3voq0SuZ+Y0Wo5iLdUeytwwFsOXdSVMQD6TJj1ijoGIX5Fw0Zbkk4 1zLjRjUzFdTstcFTJSkOF2UUKc/gBMMoAONsh0Bfq4qNqP3hOD6P09QNMf8UzgeATXVB OzLYZ7RWNmosyEUELTQMroaIgsn3BRmYjJ4Edd0D7bykrIG5l4kW/jSoa4D5mO0a2UnL VYdpqniaHlq+OXwNZOGd5u2HFhkwmLSC9I6n4NZsybaekgUijlx8VmvXW/OKuXTAoPmg fC1npCHKrN69jZEgwJ6N1EktNLMC/aTK/L8lqa7e9pOtLMKqHDkxLLvget6rvyPs6/+n ue2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Eu9vinia; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oq19-20020a170906cc9300b006f4a597941csi4954792ejb.118.2022.05.14.08.20.53; Sat, 14 May 2022 08:21:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Eu9vinia; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233419AbiENOvO (ORCPT + 99 others); Sat, 14 May 2022 10:51:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233700AbiENOvD (ORCPT ); Sat, 14 May 2022 10:51:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 538423466D for ; Sat, 14 May 2022 07:50:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E36AD60F69 for ; Sat, 14 May 2022 14:50:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F057CC34117; Sat, 14 May 2022 14:50:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1652539843; bh=4H5VhiOzuNnAER7m+K0HBmbaoSkrPy7gUzvAMCNReZY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eu9viniaghDuWVypbYsj+6tty4DGUlf4dR3SWs5qTpInQ0saUnFE0/8hWD26NJYaS +6h3dNTNPmL2917+ZB7cR6E0/Kh0YxzInrp7w2lyc0+1PYMkemVQYhBSH5OeYrV/xL CWBymxxlC588ANYAeODzOX/38yK36S9/ERU0/YJU5NsvudZA08rzGyAxc7PtorimCZ 3B+QYb3TzVANqGBtDyMr5JwDxDhxEa3txevRsC96nHrxMUsjpPdUVBYm1GQN4YzC6a I7VhFNgZ7eq5DX/ltxEy5BJ0PDdP+2H+P+0V4+WgOy6eYpKCcy4eZ1Wkazcq7YzRbw nH+ZiHVlilPzg== From: trondmy@kernel.org To: Steve Dickson , "J.Bruce Fields" Cc: linux-nfs@vger.kernel.org Subject: [PATCH 1/6] libnfs4acl: Add helpers to set the dacl and sacl Date: Sat, 14 May 2022 10:44:31 -0400 Message-Id: <20220514144436.4298-2-trondmy@kernel.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220514144436.4298-1-trondmy@kernel.org> References: <20220514144436.4298-1-trondmy@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org From: Trond Myklebust Add helper functions to set the NFSv4.1 dacl and sacl attributes. Signed-off-by: Trond Myklebust --- include/libacl_nfs4.h | 9 +++++ libnfs4acl/Makefile | 2 + libnfs4acl/nfs4_getacl.c | 83 ++++++++++++++++++++++++++++++++++++++++ libnfs4acl/nfs4_setacl.c | 49 ++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 libnfs4acl/nfs4_getacl.c create mode 100644 libnfs4acl/nfs4_setacl.c diff --git a/include/libacl_nfs4.h b/include/libacl_nfs4.h index d3786c3fabdc..76bbe90af54d 100644 --- a/include/libacl_nfs4.h +++ b/include/libacl_nfs4.h @@ -123,6 +123,8 @@ /* NFS4 acl xattr name */ #define ACL_NFS4_XATTR "system.nfs4_acl" +#define DACL_NFS4_XATTR "system.nfs4_dacl" +#define SACL_NFS4_XATTR "system.nfs4_sacl" /* Macro for finding empty tailqs */ #define TAILQ_IS_EMPTY(head) (head.tqh_first == NULL) @@ -152,6 +154,13 @@ TAILQ_HEAD(ace_container_list_head, ace_container); /**** Public functions ****/ +extern struct nfs4_acl * nfs4_getacl(const char *path); +extern struct nfs4_acl * nfs4_getdacl(const char *path); +extern struct nfs4_acl * nfs4_getsacl(const char *path); +extern int nfs4_setacl(const char *path, struct nfs4_acl *acl); +extern int nfs4_setdacl(const char *path, struct nfs4_acl *acl); +extern int nfs4_setsacl(const char *path, struct nfs4_acl *acl); + /** Manipulation functions **/ extern int acl_nfs4_set_who(struct nfs4_ace*, int, char*); extern struct nfs4_acl * acl_nfs4_copy_acl(struct nfs4_acl *); diff --git a/libnfs4acl/Makefile b/libnfs4acl/Makefile index a598d4ee141f..556b59535e26 100644 --- a/libnfs4acl/Makefile +++ b/libnfs4acl/Makefile @@ -92,6 +92,8 @@ LIBACL_NFS4_CFILES = \ nfs4_get_ace_access.c \ nfs4_get_ace_flags.c \ nfs4_get_ace_type.c \ + nfs4_getacl.c \ + nfs4_setacl.c \ nfs4_insert_file_aces.c \ nfs4_insert_string_aces.c \ nfs4_free_acl.c \ diff --git a/libnfs4acl/nfs4_getacl.c b/libnfs4acl/nfs4_getacl.c new file mode 100644 index 000000000000..753ba9167459 --- /dev/null +++ b/libnfs4acl/nfs4_getacl.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022, Trond Myklebust + * + * This code is free software; you can redistribute it and/or modify + * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE + * version 2.1 as published by the Free Software Foundation. + * + * This code 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 LESSER GENERAL PUBLIC LICENSE for more details. + */ + +#include +#include +#ifdef HAVE_ATTR_XATTR_H +# include +#else +# ifdef HAVE_SYS_XATTR_H +# include +# endif +#endif +#include +#include "libacl_nfs4.h" + +/* returns a newly-allocated struct nfs4_acl or NULL on error. */ +static struct nfs4_acl *nfs4_getacl_byname(const char *path, + const char *xattr_name) +{ + struct nfs4_acl *acl; + struct stat st; + void *buf; + ssize_t ret; + u32 iflags = NFS4_ACL_ISFILE; + + if (path == NULL || *path == 0) { + errno = EFAULT; + return NULL; + } + + /* find necessary buffer size */ + ret = getxattr(path, xattr_name, NULL, 0); + if (ret == -1) + goto err; + + buf = malloc(ret); + if (!buf) + goto err; + + /* reconstruct the ACL */ + ret = getxattr(path, xattr_name, buf, ret); + if (ret == -1) + goto err_free; + + ret = stat(path, &st); + if (ret == -1) + goto err_free; + + if (S_ISDIR(st.st_mode)) + iflags = NFS4_ACL_ISDIR; + + acl = acl_nfs4_xattr_load(buf, ret, iflags); + + free(buf); + return acl; +err_free: + free(buf); +err: + return NULL; +} + +struct nfs4_acl *nfs4_getacl(const char *path) +{ + return nfs4_getacl_byname(path, ACL_NFS4_XATTR); +} +struct nfs4_acl *nfs4_getdacl(const char *path) +{ + return nfs4_getacl_byname(path, DACL_NFS4_XATTR); +} +struct nfs4_acl *nfs4_getsacl(const char *path) +{ + return nfs4_getacl_byname(path, SACL_NFS4_XATTR); +} diff --git a/libnfs4acl/nfs4_setacl.c b/libnfs4acl/nfs4_setacl.c new file mode 100644 index 000000000000..298365ec67c5 --- /dev/null +++ b/libnfs4acl/nfs4_setacl.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022, Trond Myklebust + * + * This code is free software; you can redistribute it and/or modify + * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE + * version 2.1 as published by the Free Software Foundation. + * + * This code 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 LESSER GENERAL PUBLIC LICENSE for more details. + */ + +#include +#include +#ifdef HAVE_ATTR_XATTR_H +# include +#else +# ifdef HAVE_SYS_XATTR_H +# include +# endif +#endif +#include "libacl_nfs4.h" + +static int nfs4_setacl_byname(const char *path, const char *xattr_name, + struct nfs4_acl *acl) +{ + char *xdrbuf = NULL; + int ret; + + ret = acl_nfs4_xattr_pack(acl, &xdrbuf); + if (ret != -1) + ret = setxattr(path, xattr_name, xdrbuf, ret, XATTR_REPLACE); + free(xdrbuf); + return ret; +} + +int nfs4_setacl(const char *path, struct nfs4_acl *acl) +{ + return nfs4_setacl_byname(path, ACL_NFS4_XATTR, acl); +} +int nfs4_setdacl(const char *path, struct nfs4_acl *acl) +{ + return nfs4_setacl_byname(path, DACL_NFS4_XATTR, acl); +} +int nfs4_setsacl(const char *path, struct nfs4_acl *acl) +{ + return nfs4_setacl_byname(path, SACL_NFS4_XATTR, acl); +} -- 2.36.1