You will want to use setuid which can be found in unistd.h so it is available on POSIX systems (so any linux/unix falvour should be ok)
From the man page
If the process has appropriate privileges, setuid() shall set the real user ID, effective user ID, and the saved set-user-ID of the calling process to uid.
If the process does not have appropriate privileges, but uid is equal to the real user ID or the saved set-user-ID, setuid() shall set the effective user ID to uid; the real user ID and saved set-user-ID shall remain unchanged.
The setuid() function shall not affect the supplementary group list in any way.
Call the su binary (such as through system() or a combination of fork() and one of the exec functions), which knows about all the intricacies of changing credentials, both Unix-generically and on your particular OS. The terminal is shared and continues to belong to the original user.
Some of these intricacies not handled by a simple setuid() call are groups, account disabling and resource limits.
If this is just for fun, setuid() preceded by setgid() and initgroups() will probably be sufficient for you.
You have to find out the UID using getpwuid(). Afterwords you have to set the UID in your program with setuid() followed by the application/function you intent to run with these new rights, e.g. the shell. To start an application with the current UID use system().
发布评论
评论(3)
您将需要使用 setuid,它可以在
unistd.h
中找到,因此它可以在 POSIX 系统上使用(因此任何 linux/unix favour 都应该没问题)从手册页
这是一个 示例
You will want to use setuid which can be found in
unistd.h
so it is available on POSIX systems (so any linux/unix falvour should be ok)From the man page
here is an example
调用
su
二进制文件(例如通过system()
或fork()
和exec
之一的组合> 函数),它知道更改凭据的所有复杂性,无论是通用 Unix 还是在您的特定操作系统上。终端被共享并继续属于原始用户。其中一些复杂的问题无法通过简单的
setuid()
调用来处理,包括组、帐户禁用和资源限制。如果这只是为了好玩,
setuid()
前面的setgid()
和initgroups()
对您来说可能就足够了。Call the
su
binary (such as throughsystem()
or a combination offork()
and one of theexec
functions), which knows about all the intricacies of changing credentials, both Unix-generically and on your particular OS. The terminal is shared and continues to belong to the original user.Some of these intricacies not handled by a simple
setuid()
call are groups, account disabling and resource limits.If this is just for fun,
setuid()
preceded bysetgid()
andinitgroups()
will probably be sufficient for you.您必须使用 getpwuid() 找出 UID。之后,您必须在程序中使用 setuid() 后跟 application/ 设置 UID您打算使用这些新权限运行的功能,例如 shell。要使用当前 UID 启动应用程序,请使用 system() 。
You have to find out the UID using getpwuid(). Afterwords you have to set the UID in your program with setuid() followed by the application/function you intent to run with these new rights, e.g. the shell. To start an application with the current UID use system().