From nobody Fri Nov 25 07:50:47 2022 X-Original-To: dev-commits-src-main@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 4NJRqq3JJlz4jBfb; Fri, 25 Nov 2022 07:50: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 4NJRqq2qVzz3FtS; Fri, 25 Nov 2022 07:50:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1669362647; 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=Fbjx/tBqMpk5NYViKgjMr1b73YmMqOfRPMKMUWrleKs=; b=Vx2plCeiY8aSpDgTeDtDmLuDKfjpoq5FxIMfoUB+67+mG6W2+7YK8Nxsy/zOb8a6XRjV0O MzawDnorDbJ8a1rqa9aAD0NHg7b09pQS70L0YZcY5ZxkRfeT9L5Pd90IZrEl6+qiUkdvuL 001K+v53XTtBx7HzLoi3QZK5vrU6gHetjDdbZkKm1SJb1lURX8zaSVfmZHXjt2FRhqdOXF Sx/NE6VRSeHhRQrUtIJ2IIpJ39SdJ5pQrBfC6ZAPKoe/jdX95H/ULvyoA45gTv7HJzo1NW lfB3Qwu1cab8vtqgMCikB4AHoaUz5B0pPNLHbp4yyxTTA3/+ym9Bdvh7nksrrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1669362647; 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=Fbjx/tBqMpk5NYViKgjMr1b73YmMqOfRPMKMUWrleKs=; b=twN3I5IWWZ8JX1V8vfAG6yZvTRpHil8b+s7KfGp1QYBGmkhoEND9W/YAc31ym5mLtBFCHl sz2W2vIUNXGj2/Wmr/I8CTTbf2jj0FBskEueK6I7pG3BM7Lamku47kF1QydYygKwGH61zD nkr6tiHVT+PQQ3CXIU/3nP5EKIX2IHXzciLqCQxhji6iBAlUlbkQQmiqM6OHEnpt00wnaX s1JjuV8gqUoMyea13TOAziKWIIpS+ipb9y+/z4yELMb31aFeRk+uLtYSjDKs5uYAK/sf+S 1XcA/m4dOtjSk+mBCtq2GCxTJ0zxUlYhNPC3lXjCiLywWXlNjP0iqVsnjaYW+A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1669362647; a=rsa-sha256; cv=none; b=Na/5wzGzf+wKV+3CzFBTG4jrOxquniuGUQXe6Xa2xBdQK22P0hUFHA2uNxJ32p8M5Ug3ja ReO2jhOUUexj/Qz6ZEm22NdCFCKVeZyZoy3HWef/L1o2oGS09YqLLhSLOIcsh9VCX24mhQ AQiijGy5ceTjynI8BgiDFBWrBhk4oX4x0Z/VX6waxrCzvMeUA1b3kbGiFM4coiy9lbyG+l V5NI3QX8eS+JduVgVyGt+ce/v89l2aJqqfsuYPE75fMdxO8odP8ty7baBtKeNFKkyG3SVy ZNjC4/QojYcT5GeIaWS81MyK3u+gQiiA48vJr3rDzbBYfPCaxqt/rYED5SqZfg== 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 4NJRqq1Z5FzY1X; Fri, 25 Nov 2022 07:50: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 2AP7olI3039736; Fri, 25 Nov 2022 07:50:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2AP7olPT039735; Fri, 25 Nov 2022 07:50:47 GMT (envelope-from git) Date: Fri, 25 Nov 2022 07:50:47 GMT Message-Id: <202211250750.2AP7olPT039735@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: a1ab15abe2e2 - main - flua: add a chown(2) binding List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: a1ab15abe2e2a1ed92cbad056df71afff7e8794e Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=a1ab15abe2e2a1ed92cbad056df71afff7e8794e commit a1ab15abe2e2a1ed92cbad056df71afff7e8794e Author: Baptiste Daroussin AuthorDate: 2022-11-24 08:16:05 +0000 Commit: Baptiste Daroussin CommitDate: 2022-11-25 07:49:58 +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 --- 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