From 09537c955f5c2262023cf6148ca95fe75c80b8cc Mon Sep 17 00:00:00 2001 From: hanez Date: Wed, 18 Jun 2025 23:42:00 +0200 Subject: [PATCH] Added all this in pure Lua. ;) --- Makefile | 2 + README.md | 6 +- lua/checkgroup.lua | 79 ++++++++++++++++++++++++++ lua/chkgrp-native-min-extended.lua | 89 ++++++++++++++++++++++++++++++ lua/test.lua | 26 +++++++++ 5 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 lua/checkgroup.lua create mode 100755 lua/chkgrp-native-min-extended.lua create mode 100755 lua/test.lua diff --git a/Makefile b/Makefile index f6bb51d..162f0f5 100644 --- a/Makefile +++ b/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 diff --git a/README.md b/README.md index 54b6892..7704915 100644 --- a/README.md +++ b/README.md @@ -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!... diff --git a/lua/checkgroup.lua b/lua/checkgroup.lua new file mode 100644 index 0000000..a8ee0db --- /dev/null +++ b/lua/checkgroup.lua @@ -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 - 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 + diff --git a/lua/chkgrp-native-min-extended.lua b/lua/chkgrp-native-min-extended.lua new file mode 100755 index 0000000..6b5f22f --- /dev/null +++ b/lua/chkgrp-native-min-extended.lua @@ -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 - 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 + diff --git a/lua/test.lua b/lua/test.lua new file mode 100755 index 0000000..0bfb5e8 --- /dev/null +++ b/lua/test.lua @@ -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 - 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 +