macOS上的getgroups()不返回所有补充组,而是仅返回主要组

问题描述 投票:1回答:1

'id'命令的确给了我所有的补充组,但是以下代码只是返回该用途所属的主要组:

gid_size = getgroups(0, NULL); //1 is getting returned here
grouplist = malloc(gid_size * sizeof(gid_t));
getgroups(gid_size, grouplist); //Even with gid_size>1, only primary groups is returned.

我的问题是,是否还有其他替代方法可以在macOS上获取所有补充组。

c macos darwin usergroups
1个回答
0
投票

首先应调用initgroups(),应该最多可以再建立16个组,但是这也不理想。

initgroups(3) man page

进程不应使用initgroups(3)中的组ID号来确定用户的组成员身份。从getgroups(2)获得的列表可能只是用户组成员身份的部分列表。成员资格检查应使用getgroups()mbr_gid_to_uuid(3)mbr_uid_to_uuid(3)功能。

因此,如果您要检查用户是否是特定组的成员,则可以使用mbr_check_membership(3)功能。

请注意,mbr_*()并基于非常快速的检查,看起来它使用了未记录的source code of the id command is available

id这是getgrouplist_2()函数的一种变体,但没有16组限制。

我不认为此函数在任何公共头文件中都声明,looks like使用原型

getgrouplist()
© www.soinside.com 2019 - 2024. All rights reserved.