繁体   English   中英

setgid():不允许操作

[英]setgid(): Operation not permitted

我的任务是为系统上的所有用户列出用户所属的所有组。 这个想法是通过/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用户以外的其他用户,您的程序就放弃了切换用户的权限,因此后续调用将失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM