From nobody Wed Jan 25 08:51:47 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4P1yJ34QW2z30ykm; Wed, 25 Jan 2023 08:51:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4P1yJ33xndz3hRN; Wed, 25 Jan 2023 08:51:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674636707; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ScFMG3cyjRjWZjiwdZXB4QFTLtYzSUD1C7v7qFSmTyY=; b=sQQ9cFP1/bGYNjEMstu6IDwbC9rryF/uMS0noR6Qr2mzWF+MefAQKSAVdG2b1kI+JLjcwk 9c7dXQKT867km5pIwv6lpM3Fx30FZVspFfu78C4x5EHfJOtZKoU7riAUy6+va857NhOYUE 3Jv2Y55YXN8SkImkUEChzvQpysrda8L4ty9Sl+guLsUdidJeaMoFc8bIriKmvhcrJFfVaH M00NJVZbA+LqV54rhYbXx1PhZxVCVskwfEJLfHh0nv2EK3QZnqstfRvHh8h64RSssEIP2G isHIXru6qG+q2uhnh4PbpwNF+p9KRGPhHfvJJhWtqHN25EgU4CgpJnrikjL+rA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674636707; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ScFMG3cyjRjWZjiwdZXB4QFTLtYzSUD1C7v7qFSmTyY=; b=NkAJGD+CCyszMYMLukiveAPKwJUt4Ms36WG7GF8T4S3+ax2sbqYOpjNEgQR5jbaCIKlrKo yYxGSfea2afWifwrT8GAcxk7caQG8oVcJEdn9C6JpbBrfgY8KkjfHzABBkCg3NTdFV9egz hh3jxmc9nUdc6+6jCPuGAEri/+Dxi5AotA6A6I8AwMYk+pBTs+uzF3WlpvrmolOYLEm1as b2prwfeCwc+tg+hTau+p0IHGIKglj6CFpmnU+pAyYAc2Wh6nMLLK08nrMb1vHWh66iKKTi dMgKihLtZV1S7/3RIuI7NLiYB/BUT6XcFTLu+elFZ1ogObTxQOeBzaef5IFEhg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674636707; a=rsa-sha256; cv=none; b=wFDBTjibDeBssooBpwu54lcOJR7IQcwSDVOpldPOAWcbfPd3eR909UiIWNfSVSJ3w89ijT 6F0063RRSH10LhxZLw39MP9SQI7t94OpfmJa5nhPG/feHg+uxeWz/aPYbpLiXprbdPuPh/ tTMU2BoDeL1X+f5cypcMnqVw+BdF0Jzmcswy6+qffir9apI4mW6NdorjMdt9hYWa/DQgqJ hUMyu5p17ia/zZ/o5zjh6DtWce/ZbQifRhibk8sknyVDCNNGIgF7DMEGGazEtVCFGYY16x kl4xb2p9cOv/p+8Ckoe7ithuA0k3GAkc1zQiJpPkYWbxv2VGOlMAMhHxbHZyFg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4P1yJ330JVzjZS; Wed, 25 Jan 2023 08:51:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30P8plKY010805; Wed, 25 Jan 2023 08:51:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30P8plBd010804; Wed, 25 Jan 2023 08:51:47 GMT (envelope-from git) Date: Wed, 25 Jan 2023 08:51:47 GMT Message-Id: <202301250851.30P8plBd010804@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Baptiste Daroussin Subject: git: c69a31795b0e - stable/13 - flua: add a chown(2) binding List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: c69a31795b0ec00525d37ef0f0a9828a56ecf991 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=c69a31795b0ec00525d37ef0f0a9828a56ecf991 commit c69a31795b0ec00525d37ef0f0a9828a56ecf991 Author: Baptiste Daroussin AuthorDate: 2022-11-24 08:16:05 +0000 Commit: Baptiste Daroussin CommitDate: 2023-01-25 08:51:23 +0000 flua: add a chown(2) binding The main difference with the chown in luaposix, is that it checks and reports if a user or a group do exist when a string is passed as arguments Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D37479 (cherry picked from commit a1ab15abe2e2a1ed92cbad056df71afff7e8794e) --- libexec/flua/modules/lposix.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c index adf3a7bb9a1f..88687be2788c 100644 --- a/libexec/flua/modules/lposix.c +++ b/libexec/flua/modules/lposix.c @@ -30,6 +30,8 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include #include #include @@ -63,6 +65,62 @@ lua_chmod(lua_State *L) return 1; } +static int +lua_chown(lua_State *L) +{ + int n; + const char *path; + uid_t owner = (uid_t) -1; + gid_t group = (gid_t) -1; + + n = lua_gettop(L); + luaL_argcheck(L, n > 1, n, + "chown takes at least two arguments"); + path = luaL_checkstring(L, 1); + if (lua_isinteger(L, 2)) + owner = (uid_t) lua_tointeger(L, 2); + else if (lua_isstring(L, 2)) { + struct passwd *p = getpwnam(lua_tostring(L, 2)); + if (p != NULL) + owner = p->pw_uid; + else + return (luaL_argerror(L, 2, + lua_pushfstring(L, "unknown user %s", + lua_tostring(L, 2)))); + } else if (!lua_isnoneornil(L, 2)) { + const char *type = luaL_typename(L, 2); + return (luaL_argerror(L, 2, + lua_pushfstring(L, "integer or string expected, got %s", + type))); + } + + if (lua_isinteger(L, 3)) + group = (gid_t) lua_tointeger(L, 3); + else if (lua_isstring(L, 3)) { + struct group *g = getgrnam(lua_tostring(L, 3)); + if (g != NULL) + group = g->gr_gid; + else + return (luaL_argerror(L, 3, + lua_pushfstring(L, "unknown user %s", + lua_tostring(L, 3)))); + } else if (!lua_isnoneornil(L, 3)) { + const char *type = luaL_typename(L, 3); + return (luaL_argerror(L, 3, + lua_pushfstring(L, "integer or string expected, got %s", + type))); + } + + if (chown(path, owner, group) == -1) { + lua_pushnil(L); + lua_pushstring(L, strerror(errno)); + lua_pushinteger(L, errno); + return (3); + } + lua_pushinteger(L, 0); + return (1); +} + static int lua_getpid(lua_State *L) { @@ -82,6 +140,7 @@ static const struct luaL_Reg sys_statlib[] = { static const struct luaL_Reg unistdlib[] = { REG_SIMPLE(getpid), + REG_SIMPLE(chown), { NULL, NULL }, }; #undef REG_SIMPLE