• 模拟Linux修改实际、有效和保存设置标识


    就是模拟setuid seteuid setreuid setresuid,感觉代码比书上大段的文字好记,就写成代码形式了。

    // setuid.cc: 模拟<unistd.h>中的设置用户ID的方法的作用
    #include <stdio.h>
    #include <errno.h>
    
    int real = 0;          // 实际用户ID
    int effective = 0;     // 有效用户ID
    int saved = 0;         // 保存的设置用户ID
    
    void showid();  // 打印所有ID
    
    inline bool hasPrivilege() { return effective == 0; }
    #define EINVAL_RETURN { errno = EINVAL; return -1; }
    #define EPERM_RETURN { errno = EPERM; return -1; }
    
    int setuid(int uid)  // 修改所有用户ID
    {
        if (uid < 0)
            EINVAL_RETURN;
        if (!hasPrivilege() && uid != real && uid != saved)
            EPERM_RETURN;
        if (hasPrivilege())
            real = effective = saved = uid;
        else
            effective = uid;
        return 0;
    }
    
    int seteuid(int euid)  // 修改有效用户ID
    {
        if (euid < 0)
            EINVAL_RETURN;
        if (!hasPrivilege() && euid != real && euid != saved)
            EPERM_RETURN;
        effective = euid;
        return 0;
    }
    
    int setreuid(int ruid, int euid)  // 修改实际/有效用户ID
    {
        if (ruid < -1 || euid < -1)
            EINVAL_RETURN;
        if (!hasPrivilege())
        {
            if (ruid != -1 && ruid != real && ruid != effective)
                EPERM_RETURN;
            if (euid != -1 && euid != real && euid != effective && euid != saved)
                EPERM_RETURN;
        }
        real = (ruid != -1) ? ruid : real;
        effective = (euid != -1) ? euid : effective;
        if (ruid != -1 || effective != real)
            saved = effective;
        return 0;
    }
    
    // 非SUSv3规范, 其他UNIX实现对其也鲜有支持
    int setresuid(int ruid, int euid, int suid)  // 修改实际/有效/保存用户ID
    {
        if (ruid < -1 || euid < -1 || suid < -1)
            EINVAL_RETURN;
        if (!hasPrivilege())
        {
            if (ruid != -1 && ruid != real && ruid != effective && ruid != saved)
                EPERM_RETURN;
            if (euid != -1 && euid != real && euid != effective && euid != saved)
                EPERM_RETURN;
            if (suid != -1 && suid != real && suid != effective && suid != saved)
                EPERM_RETURN;
        }
        real = (ruid != -1) ? ruid : real;
        effective = (euid != -1) ? euid : effective;
        saved = (suid != -1) ? suid : saved;
        return 0;
    }
    
    int main()
    {
        real = 1000;
        // 下面4句只能执行其中1句
    //    setuid(2000);
        setreuid(-1, 2000);
    //    seteuid(2000);
    //    setresuid(-1, 2000, 3000);
    
        showid();
        return 0;
    }
    
    void showid()
    {
        printf("实际用户ID:       %4d
    ", real);
        printf("有效用户ID:       %4d
    ", effective);
        printf("保存的设置用户ID: %4d
    ", saved);
    }
    

    main函数是TLPI第9章习题第1道的运行结果,然后模拟了一遍功能,后面几道也很简单就能做出来了。以后忘记的话看遍代码就能很快记起来了。

  • 相关阅读:
    南京的第一场雪 沧海
    再一则笑话家里闹鬼了 沧海
    简单需求描述(自己备用) 沧海
    为什么IIS无法启动,以前是可以了.而且WWW服务也启动不了提示:错误127,找不到指定程序. 沧海
    生活中有着太多的感动 沧海
    Enhancements 沧海
    ABAP SysteTransactions, Tables and Programs 沧海
    SAP ABAP OK Code Values 沧海
    Conversion of SAPSCRIPT to SMARTFORMS 沧海
    别人的面试记实转摘 沧海
  • 原文地址:https://www.cnblogs.com/Harley-Quinn/p/6676327.html
Copyright © 2020-2023  润新知