skip membership if already in the primary group

This commit is contained in:
Motiejus Jakštys 2022-07-14 06:30:45 +03:00
parent 7a27c06f7b
commit d731fe377b

View File

@ -167,6 +167,11 @@ pub fn init(
for (groupmembers) |member_name| { for (groupmembers) |member_name| {
if (name2user.get(member_name)) |user_idx| { if (name2user.get(member_name)) |user_idx| {
// if user is in a group of their primary gid,
// skip the membersip.
if (users.items(.gid)[user_idx] == groups.items(.gid)[i])
continue;
members.len += 1; members.len += 1;
members[members.len - 1] = user_idx; members[members.len - 1] = user_idx;
try user2groups[user_idx].append(allocator, @intCast(u32, i)); try user2groups[user_idx].append(allocator, @intCast(u32, i));
@ -258,7 +263,10 @@ pub fn testCorpus(allocator: Allocator) !Corpus {
.shell = "/usr/sbin/nologin", .shell = "/usr/sbin/nologin",
} }; } };
var group0 = try Group.init(allocator, 0, "root", &[_][]const u8{}); // the "root" membership should be removed, because root's gid is this
// group's id. All the exported functions that return memberships (getgr*,
// initgroups_dyn) expect these users will not be returned.
var group0 = try Group.init(allocator, 0, "root", &[_][]const u8{"root"});
var group1 = try Group.init(allocator, 128, "vidmantas", &[_][]const u8{}); var group1 = try Group.init(allocator, 128, "vidmantas", &[_][]const u8{});
var group2 = try Group.init( var group2 = try Group.init(
allocator, allocator,