分类: LINUX
2015-04-15 19:40:56
SETUID(2) Linux Programmer’s Manual SETUID(2)
NAME
setuid - set user identity
//setuid-设置用户身份
SYNOPSIS
#include
//包含头文件
#include
//包含头文件
int setuid(uid_t uid);
//返回整型,uid_t就是用户ID的专用类型
DESCRIPTION
setuid() sets the effective user ID of the current process. If the effective UID of the caller is root, the real UID and saved set-user-ID are also set.
//setuid()设置当前进程的有效用户ID。如果调用者的有效UID是root,真正的UID和保存设置用户ID也设置。
Under Linux, setuid() is implemented like the POSIX version with the _POSIX_SAVED_IDS feature. This allows a set-user-ID (other than root) program to drop all of its user privileges, do some un-privileged work, and then re-engage the original effective user ID in a secure manner.
//在Linux下,setuid()实现像有着_POSIX_SAVED_IDS特性的POSIX的版本。这允许set-user-ID(除了根)计划放弃所有的用户权限,
做一些un-privileged工作,然后以一种安全的方式重新接入原有的有效用户ID
If the user is root or the program is set-user-ID-root, special care must be taken. The setuid() function checks the effective user ID of the caller and if it is the superuser, all process related user ID’s are set to uid. After this has occurred, it is impossible for the program to regain root privileges.
//如果用户是根或程序set-user-ID-root,必须要注意。setuid()函数检查调用者的有效用户ID,如果是超级用户,所有进程相关的用户ID都要设置uid。在这发生后,程序不可能恢复root特权。
Thus, a set-user-ID-root program wishing to temporarily drop root privileges, assume the identity of a non-root user, and then regain root privileges afterwards cannot use setuid(). You can accomplish this with the (non-POSIX, BSD) call seteuid().
//因此,set-user-ID-root计划希望临时下将root特权,假设一个非root用户的身份,然后重新获得root权限之后不能使用setuid()。
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and
errno is set appropriately.
//成功,则返回0。错误,返回1, errno设置适当。
ERRORS
EAGAIN The uid does not match the current uid and uid brings
process over it’s NPROC rlimit.
EPERM The user is not privileged (Linux: does not have the
CAP_SETUID capability) and uid does not match the real
UID or saved set-user-ID of the calling process.
CONFORMING TO
SVr4, POSIX.1-2001. Not quite compatible with the 4.4BSD
call, which sets all of the real, saved, and effective user
IDs.
LINUX-SPECIFIC REMARKS
Linux has the concept of filesystem user ID, normally equal to
the effective user ID. The setuid() call also sets the
filesystem user ID of the current process. See setfsuid(2).
If uid is different from the old effective uid, the process
will be forbidden from leaving core dumps.
SEE ALSO
getuid(2), seteuid(2), setfsuid(2), setreuid(2), capabili-
ties(7)
Linux 2.6.6 2004-05-27 SETUID(2)
(END)
#include
#include
#include
//包含头文件
int main() //主函数
{
if(!setuid(1234)) //如果非setuid(1234)
{
printf("setuid successfully!\n"); //打印setuid成功!
}
else //否则
{
printf("setuid error!"); //打印setuid错误!
perror("setuid"); //将输入的一些信息和errno所对应的错误一起输出
}
return 0;
}
Introductions:
setuid函数设置实际用户ID和有效用户ID。Linux的setuid函数和Unix中的setuid函数的行为是不同的。在Linux中, setuid(uid)函数的执行步骤为:(1)如果由普通用户调用,将当前进程的有效ID设置为uid. (2)如果由有效用户ID符为0的进程调用,则将真实,有效和已保存用户ID都设置为uid.
在Unix中.setuid(uid)函数的行为为: (1)如果进程没有特权,且uid等于实际用户ID或已保存用户ID,则将有效的用户ID设置为uid.否则返回错误.(2)如果进程是有超级用户特权,则将真实、有效和已保存用户表示符都设置为uid.如果两个条件都不满足,则设置errno为EPERM。