Added all this in pure Lua. ;)
This commit is contained in:
parent
c6dd59e392
commit
09537c955f
5 changed files with 201 additions and 1 deletions
2
Makefile
2
Makefile
|
|
@ -18,6 +18,7 @@ install:
|
|||
install -g 0 -o 0 -m 0655 ./chkgrp-native /usr/bin/
|
||||
install -g 0 -o 0 -m 0655 ./chkgrp-native-min /usr/bin/
|
||||
install -g 0 -o 0 -m 0655 ./chkgrp-native-min-extended /usr/bin/
|
||||
install -g 0 -o 0 -m 0655 ./lua/chkgrp-native-min-extended.lua /usr/bin/
|
||||
|
||||
uninstall:
|
||||
rm -f /usr/bin/chkgrp
|
||||
|
|
@ -25,4 +26,5 @@ uninstall:
|
|||
rm -f /usr/bin/chkgrp-native
|
||||
rm -f /usr/bin/chkgrp-native-min
|
||||
rm -f /usr/bin/chkgrp-native-min-extended
|
||||
rm -f /usr/bin/chkgrp-native-min-extended.lua
|
||||
|
||||
|
|
|
|||
|
|
@ -2,5 +2,9 @@
|
|||
|
||||
chkgrp is a set of programs to verify if a user is a member of a group on a UNIX based system. This code is so simple that it describes itself very good.
|
||||
|
||||
Mor information will follow...
|
||||
More information will follow... Actually there are multiple programs with some different behavior. I will write more about this, but for now I will not.
|
||||
|
||||
# Why?
|
||||
|
||||
I am releasing this code because I like it and use it in some situations. If you have a usecase, fine! If not, fine too!...
|
||||
|
||||
|
|
|
|||
79
lua/checkgroup.lua
Normal file
79
lua/checkgroup.lua
Normal 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
|
||||
|
||||
89
lua/chkgrp-native-min-extended.lua
Executable file
89
lua/chkgrp-native-min-extended.lua
Executable 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
26
lua/test.lua
Executable 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
|
||||
|
||||
Reference in a new issue