13 void chrootAndDrop(std::string
const& chrootdir
, std::string
const& username
)
17 std::cerr
<< "this programm has to be run as root in order to run in a chroot" << std::endl
;
21 struct passwd
*pw
= getpwnam(username
.c_str());
23 if(chroot(chrootdir
.c_str()))
25 std::cerr
<< "can't chroot to " << chrootdir
<< std::endl
;
28 cLog
.msg(Log::PRIO_NOTICE
) << "we are in chroot jail (" << chrootdir
<< ") now" << std::endl
;
30 if (initgroups(pw
->pw_name
, pw
->pw_gid
) || setgid(pw
->pw_gid
) || setuid(pw
->pw_uid
))
32 std::cerr
<< "can't drop to user " << username
<< " " << pw
->pw_uid
<< ":" << pw
->pw_gid
<< std::endl
;
35 cLog
.msg(Log::PRIO_NOTICE
) << "dropped user to " << username
<< " " << pw
->pw_uid
<< ":" << pw
->pw_gid
<< std::endl
;
39 std::cerr
<< "unknown user " << username
<< std::endl
;
54 // std::cout << "running in background now..." << std::endl;
57 // for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
58 for (fd
=0;fd
<=2;fd
++) // close all file descriptors
60 fd
=open("/dev/null",O_RDWR
); // stdin