模拟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\n", real);
printf("有效用户ID: %4d\n", effective);
printf("保存的设置用户ID: %4d\n", saved);
}
main函数是TLPI第9章习题第1道的运行结果,然后模拟了一遍功能,后面几道也很简单就能做出来了。以后忘记的话看遍代码就能很快记起来了。
模拟Linux修改实际、有效和保存设置标识的更多相关文章
- Windows上模拟Linux环境的软件Cygwin
Windows上模拟Linux环境的软件Cygwin 2010-10-11 15:19 我要评论(0) 字号:T|T Cygwin是一个用于在Windows上 模拟Linux环境的软件.它可 ...
- 以Qemu模拟Linux,学习Linux内核
文章名称:以Qemu模拟Linux,学习Linux内核作 者:five_cent文章地址:http://www.cnblogs.com/senix/archive/2013/02/21/29 ...
- Linux - 修改系统的max open files、max user processes (附ulimit的使用方法)
目录 1 问题说明 2 修改max open files 3 修改max user processes 4 附录: ulimit命令说明 1 问题说明 Linux 系统默认的max open file ...
- Linux - 修改系统的max open files、max user processes(附ulimit的使用方法)【转载】
Linux - 修改系统的max open files.max user processes(附ulimit的使用方法)目录 1 问题说明2 修改max open files3 修改max user ...
- 转: 深入理解Linux修改hostname
from: http://www.cnblogs.com/kerrycode/p/3595724.html 写的相当详细!!! 深入理解Linux修改hostname 2014-03-12 10:17 ...
- linux修改主机名的方法
linux修改主机名的方法 用hostname命令可以临时修改机器名,但机器重新启动之后就会恢复原来的值. #hostname //查看机器名#hostname -i //查看本机器名对应的ip ...
- linux修改网卡名称
本文转载自江一<linux修改网卡名称> 终端输入:vi /etc/udev/rules.d/70-persistent-net.rules 出现以下文件 # This file was ...
- Linux修改系统以及pip更新源
Linux修改系统以及pip更新源 时间:2015-08-01来源:csdn 作者:henulwj 修改系统更新源 你是否跟我一样在刚看时接触Linux时被系统更新源问题搞得晕头转向,不同的Linux ...
- linux修改挂载目录
linux修改挂载目录 修改扩展磁盘默认的挂载点/home到/data [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用%% 挂载点 /de ...
随机推荐
- 学会使用postman模拟http请求(转)
原文链接:http://www.cnblogs.com/mafly/p/postman.html 这个东西关键时刻还是有点用的.因为有些时候你和前端工作不协调的话,自己动手,丰衣足食.确定自己没问题是 ...
- 初次使用VCS仿真软件
由于刚开始接触VCS,对于VCS不是太了解,在网上找了很多的资料终于遇到了一个相对比较初级的入门资料,这个资料是以一个简单的4位加法器为例来介绍vcs的用法的,比较好入门,这个文章的地址如下: htt ...
- MoreEffectiveC++Item35(效率)(条款16-24)
条款16 谨记80-20法则 条款17 考虑使用 lazy evaluation(缓释评估) 条款18 分期摊还预期的计算成本 条款19 了解临时对象的来源 条款20 协助完成"返回值的优化 ...
- core文件介绍
原文链接:http://team.eyou.com/?p=27 如有侵犯您的版权,请联系windeal12@qq.com linux下,产生core文件,和不产生core文件的条件: 当我们的程序崩溃 ...
- L159
Waves are the children of the struggle between ocean and atmosphere, the ongoing signatures of infin ...
- Linux之LVM设备的管理
LVM可以理解为可扩展的设备:在设备空间不足的时候,保证其在原始数据不变的情况下增大设备的存储大小.那么,要达到这种效果,我们得把可用设备先比变为物理卷,再把物理卷处理为物理卷组,最后成为LVM逻辑卷 ...
- PHP sessions that have already been started
In my PHP code, if a session has already started, and I try to start a new one, I get the following ...
- asp.net string有多行文字
用如下格式设置
- html与css与JavaScript的关系
“HTML是网页的结构,CSS是网页的外观,而JavaScript是页面的行为.” 1)HTML—Hypertext Markup Language. 超文本标记语言.用来描述网页的语言. <h ...
- 深度学习(七十)darknet 实现编写mobilenet源码
一.添加一个新的网络层 (1)parse.c文件中函数string_to_layer_type,添加网络层类型解析: if (strcmp(type, "[depthwise_convolu ...