我的任务是为系统上的所有用户列出用户所属的所有组。其想法是通过/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');
}有什么想法吗?
发布于 2014-05-11 12:29:31
当您的程序调用setuid()以切换到root以外的其他用户时,您的程序已经放弃了切换用户的权限,因此随后的调用将失败。
https://stackoverflow.com/questions/23592338
复制相似问题