gpt4 book ai didi

c - setgid() : Operation not permitted

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:26 24 4
gpt4 key购买 nike

我的任务是为系统上的所有用户列出用户所属的所有组。这个想法是通过 /etc/passwd 并为每个用户打印其组。

[编辑] 成功了:

if( getgrouplist(passwd->pw_name, passwd->pw_gid,
groups, &ngroups) < 0)
error_fatal ("getgrouplist ()");

但我仍然很好奇它不起作用的原因。

输出:

User root is a member of: root
User daemon is a member of: root
setgid(): Operation not permitted

代码:

while ((passwd = getpwent ()) != NULL) {
uid = passwd->pw_uid;
gid = passwd->pw_gid;

if (setgid(gid) < 0)
error_fatal ("setgid()");

if (setuid(uid) < 0)
error_fatal ("setuid()");

if((ngroups = getgroups (0, NULL)) < 0)
error_fatal ("getgroups ()");

if((groups = (gid_t *) malloc (sizeof (gid_t) * ngroups)) < 0)
error_fatal ("malloc ()");

if (getgroups (ngroups, groups) < 0)
error_fatal ("getgroups ()");

printf ("User %s is a member of: ", passwd->pw_name);
for (i = 0; i < ngroups; i++) {
gid = groups[i];
if((group = getgrgid (gid)) == NULL)
error_fatal ("getgrgid ()");
printf ("%s ", group->gr_name);
}
putchar ('\n');
}

有什么想法吗?

最佳答案

一旦您的程序调用setuid() 切换到除root 以外的其他用户,您的程序就放弃了切换用户的权限,因此后续调用将失败。

关于c - setgid() : Operation not permitted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23592338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com