Merged with chkgrp and renamed to chkusr.

This commit is contained in:
Johannes Findeisen 2025-06-27 00:57:42 +02:00
commit 8644ccbdc5
12 changed files with 779 additions and 22 deletions

79
lua/checkgroup.lua Normal file
View file

@ -0,0 +1,79 @@
-- This file is part of chkgrp that is a program that checks if a user is a
-- member of a group.
--
-- Author: Johannes Findeisen <you@hanez.org> - 2025
-- Homepage: https://git.xw3.org/hanez/chkgrp
-- License: Apache-2.0 (see LICENSE)
local checkgroup = {}
local MAX_NAME = 256
local function read_passwd_gid(username)
local f = io.open("/etc/passwd", "r")
if not f then return nil end
for line in f:lines() do
local fields = {}
for field in string.gmatch(line, "([^:]+)") do
table.insert(fields, field)
end
if fields[1] == username then
f:close()
return tonumber(fields[4])
end
end
f:close()
return nil
end
local function check_group_membership(username, groupname, user_gid)
local f = io.open("/etc/group", "r")
if not f then return nil end
for line in f:lines() do
local fields = {}
for field in string.gmatch(line, "([^:]+)") do
table.insert(fields, field)
end
if fields[1] == groupname then
local group_gid = tonumber(fields[3])
if group_gid == user_gid then
f:close()
return true
end
for member in string.gmatch(fields[4] or "", "[^,]+") do
if member == username then
f:close()
return true
end
end
f:close()
return false
end
end
f:close()
return nil
end
function checkgroup.is_user_in_group(username, groupname)
if not username or not groupname then
return nil
end
if #username > MAX_NAME or #groupname > MAX_NAME then
return nil
end
local gid = read_passwd_gid(username)
if not gid then return nil end
return check_group_membership(username, groupname, gid)
end
return checkgroup

View file

@ -0,0 +1,89 @@
#!/usr/bin/env lua
-- This file is part of chkgrp that is a program that checks if a user is a
-- member of a group.
--
-- Author: Johannes Findeisen <you@hanez.org> - 2025
-- Homepage: https://git.xw3.org/hanez/chkgrp
-- License: Apache-2.0 (see LICENSE)
local MAX_NAME = 256
local function read_passwd_gid(username)
local f = io.open("/etc/passwd", "r")
if not f then return nil end
for line in f:lines() do
local fields = {}
for field in string.gmatch(line, "([^:]+)") do
table.insert(fields, field)
end
if fields[1] == username then
f:close()
return tonumber(fields[4]) -- GID is 4th field
end
end
f:close()
return nil
end
local function check_group_membership(username, groupname, user_gid)
local f = io.open("/etc/group", "r")
if not f then return nil end
for line in f:lines() do
local fields = {}
for field in string.gmatch(line, "([^:]+)") do
table.insert(fields, field)
end
if fields[1] == groupname then
local group_gid = tonumber(fields[3])
if group_gid == user_gid then
f:close()
return true
end
for member in string.gmatch(fields[4] or "", "[^,]+") do
if member == username then
f:close()
return true
end
end
f:close()
return false
end
end
f:close()
return nil
end
local username = arg[1]
local groupname = arg[2]
if not username or not groupname then
os.exit(2)
end
if #username > MAX_NAME or #groupname > MAX_NAME then
os.exit(2)
end
local user_gid = read_passwd_gid(username)
if not user_gid then
os.exit(2)
end
local result = check_group_membership(username, groupname, user_gid)
if result == nil then
os.exit(2)
elseif result == true then
print("Yes")
os.exit(0)
else
print("No")
os.exit(1)
end

26
lua/test.lua Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env lua
-- This file is part of chkgrp that is a program that checks if a user is a
-- member of a group.
--
-- Author: Johannes Findeisen <you@hanez.org> - 2025
-- Homepage: https://git.xw3.org/hanez/chkgrp
-- License: Apache-2.0 (see LICENSE)
local checkgroup = require("checkgroup")
local user = arg[1]
local group = arg[2]
local result = checkgroup.is_user_in_group(user, group)
if result == true then
print("Yes")
os.exit(0)
elseif result == false then
print("No")
os.exit(1)
else
os.exit(2)
end