作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个在嵌入式Linux系统上运行的C++程序。
该程序由用户“myuser”启动,并使用“uid”来管理权限。
ls -l /bin/myprog
-rwsr-sr-x 1 root root 757328 May 7 12:55 myprog
/*************************************************************
* HEADER FILE
*************************************************************/
#ifndef _PRIVILEGE_H
#define _PRIVILEGE_H
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
class CPrivilege
{
private:
uid_t _ruid;
uid_t _euid;
uid_t _suid;
gid_t _rgid;
gid_t _egid;
gid_t _sgid;
int _status;
int getPrivilege(uid_t&,uid_t&,uid_t&,gid_t&,gid_t&,gid_t&);
public :
CPrivilege();
int elevate();
int down();
int print();
int status();
};
#endif
/*************************************************************
* CPP FILE
*************************************************************/
#include "_privilege.h"
CPrivilege::CPrivilege()
{
_status = getPrivilege(_ruid,_euid,_suid,_rgid,_egid,_sgid);
}
int CPrivilege::status()
{
return _status;
}
int CPrivilege::getPrivilege(uid_t& ruid, uid_t& euid, uid_t& suid, gid_t& rgid, gid_t& egid, gid_t& sgid)
{
if (getresuid(&ruid, &euid, &suid) == -1) {
printf("getresuid error\n");
return EXIT_FAILURE;
}
if (getresgid(&rgid, &egid, &sgid) == -1) {
printf("getresgid error\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int CPrivilege::elevate()
{
/* Switch to target user. effective uid gets the saved uid. */
if (seteuid((uid_t)_suid) == -1) {
printf("seteuid error\n");
return (_status = EXIT_FAILURE);
}
/* Switch to target group. effective gid gets the saved gid. */
if (setegid((gid_t)_sgid) == -1) {
printf("setegid error\n");
return (_status = EXIT_FAILURE);
}
return (_status = EXIT_SUCCESS);
}
int CPrivilege::down()
{
/* Switch to target user. effective uid gets the real uid. */
if (seteuid((uid_t)_ruid) == -1) {
printf("seteuid error\n");
return (_status = EXIT_FAILURE);
}
/* Switch to target group. effective gid gets the real gid. */
if (setegid((gid_t)_rgid) == -1) {
printf("setegid error\n");
return (_status = EXIT_FAILURE);
}
return (_status = EXIT_SUCCESS);
}
int CPrivilege::print()
{
uid_t ruid;
uid_t euid;
uid_t suid;
gid_t rgid;
gid_t egid;
gid_t sgid;
printf("status = %d\n", _status);
getPrivilege(ruid,euid,suid,rgid,egid,sgid);
printf("ruid = %d, euid = %d, suid = %d\n", ruid, euid, suid);
printf("rgid = %d, egid = %d, sgid = %d\n", rgid, egid, sgid);
}
/*************************************************************
* MAIN FILE
*************************************************************/
#include "_privilege.h"
#include <string>
int main(int argc, char *argv[])
{
CPrivilege priv;
priv.down();
priv.print();
// [...) Program stuff with user privilege
std::string system_cmd;
int ret;
system_cmd = "/root/test_script";
priv.elevate();
priv.print();
FILE *pf;
pf = fopen(system_cmd.c_str(),"r");
if (pf != NULL)
{
printf("Root privilege\n\n");
fclose(pf);
}
else
printf("User privilege\n\n");
system(system_cmd.c_str());
priv.down();
priv.print();
// [...) Other Program stuff with user privilege
return 0;
}
/*************************************************************
* SHELL FILE
*************************************************************/
#!/bin/sh
echo 'HELLO WORLD !'
status = 0
ruid = 0, euid = 0, suid = 0
rgid = 0, egid = 0, sgid = 0
status = 0
ruid = 0, euid = 0, suid = 0
rgid = 0, egid = 0, sgid = 0
Root privilege
HELLO WORLD !
status = 0
ruid = 0, euid = 0, suid = 0
rgid = 0, egid = 0, sgid = 0
status = 0
ruid = 1009, euid = 1009, suid = 0
rgid = 1013, egid = 1013, sgid = 0
status = 0
ruid = 1009, euid = 0, suid = 0
rgid = 1013, egid = 0, sgid = 0
Root privilege
sh: /root/test_script: Permission denied
status = 0
ruid = 1009, euid = 1009, suid = 0
rgid = 1013, egid = 1013, sgid = 0
最佳答案
从Bash手册中:
If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, [...] and the effective user id is set to the real user id. If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.
bash -p /root/test_script
。
关于c++ - 如何从C++将根euid传递给shell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61660356/
我是一名优秀的程序员,十分优秀!