35 #include <sys/types.h> 47 #define kMAXPATHLEN 4096 49 int assertdir(
const std::string &path, uid_t u, gid_t
g,
unsigned int mode);
50 int changeown(
const std::string &path, uid_t u, gid_t
g);
52 int loginuser(
const std::string &home,
const std::string &user, uid_t u, gid_t
g);
53 int mvfile(
const std::string &from,
const std::string &to, uid_t u, gid_t
g,
unsigned int mode);
54 int completercfile(
const std::string &rcfile,
const std::string &sessdir,
55 const std::string &stag,
const std::string &adminpath);
57 const std::string &creds,
const std::string &dsrcs,
58 const std::string &ddir,
const std::string &ddiro,
59 const std::string &ord,
const std::string &stag);
61 const std::string &logfile,
const std::string &rcfile);
64 void start_ps(
int argc,
char **argv);
75 va_start(ap,
va_(fmt));
76 vsnprintf(buf,
sizeof(buf), fmt, ap);
80 fprintf(
gLogger,
"proofexecv: %s\n", buf);
82 fprintf(stderr,
"proofexecv: %s\n", buf);
90 int main(
int argc,
char **argv)
95 Info(
"argc=%d: at least 2 additional argument (the process type and debug level) are required - exit",
99 if ((
gType = atoi(argv[1])) < 0) {
100 Info(
"ERROR: invalid process type %d (must be > 0) - exit",
gType);
109 }
else if (
gType == 20) {
114 Info(
"ERROR: process type %d not yet implemented",
gType);
128 Info(
"argc=%d: at least 5 additional arguments required - exit", argc);
140 std::string sockpath = argv[3];
141 rpdunix *uconn =
new rpdunix(sockpath.c_str());
142 if (!uconn || (uconn && !uconn->isvalid(0))) {
143 Info(
"ERROR: failure calling back parent on '%s'", sockpath.c_str());
150 if ((rcc = uconn->recvdesc(fd)) != 0) {
151 Info(
"ERROR: failure receiving open descriptor from parent (errno: %d)", -rcc);
160 if (dup2(fd, STDIN_FILENO) != 0)
161 Info(
"WARNING: failure duplicating STDIN (errno: %d)", errno);
162 if (dup2(fd, STDOUT_FILENO) != 0)
163 Info(
"WARNING: failure duplicating STDOUT (errno: %d)", errno);
167 char **argvv =
new char *[na + 1];
173 argvv[ka] = argv[ia];
179 execv(argv[4], argvv);
182 Info(
"ERROR: returned from execv: bad, bad sign !!!");
193 Info(
"argc=%d: at least 5 additional arguments required - exit", argc);
211 std::string errlog(argv[6]);
212 if (!(
gLogger = fopen(errlog.c_str(),
"a"))) {
213 Info(
"FATAL: could not open '%s' for error logging - errno: %d",
214 errlog.c_str(), (int) errno);
220 snprintf(spid, 20,
"%d", (
int)getpid());
223 std::string user = argv[3];
224 struct passwd *pw = getpwnam(user.c_str());
226 Info(
"ERROR: could noy get identity info for '%s' - errno: %d", user.c_str(), (int) errno);
232 std::string::size_type loc = 0;
236 std::string sessdir(argv[4]), logfile(argv[4]), tenvfile, trcfile;
239 if ((loc = sessdir.rfind(
'/')) != std::string::npos) sessdir.erase(loc, std::string::npos);
244 if ((loc = sessdir.rfind(
'/')) != std::string::npos) sessdir.erase(loc, std::string::npos);
246 if ((loc = tenvfile.rfind(
"<pid>")) != std::string::npos) tenvfile.erase(loc, std::string::npos);
249 trcfile +=
".rootrc";
252 if ((loc = sessdir.find(
"<pid>")) != std::string::npos) sessdir.replace(loc, 5, spid);
253 if (
assertdir(sessdir, uid, gid, 0755) != 0) {
254 Info(
"ERROR: could not assert dir '%s'", sessdir.c_str());
257 Info(
"session dir: %s", sessdir.c_str());
260 while ((loc = logfile.find(
"<pid>")) != std::string::npos) { logfile.replace(loc, 5, spid); }
261 std::string stag(logfile), envfile(logfile), userdir(logfile), rcfile(logfile);
267 if (
assertdir(userdir, uid, gid, 0755) != 0) {
268 Info(
"ERROR: could not assert dir '%s'", userdir.c_str());
273 if ((loc = stag.rfind(
'/')) != std::string::npos) stag.erase(0, loc);
274 if ((loc = stag.find(
'-')) != std::string::npos) loc = stag.find(
'-', loc+1);
275 if (loc != std::string::npos) stag.erase(0, loc+1);
276 Info(
"session tag: %s", stag.c_str());
279 std::string sockpath = argv[5];
280 rpdunix *uconn =
new rpdunix(sockpath.c_str());
281 if (!uconn || (uconn && !uconn->isvalid(0))) {
282 Info(
"ERROR: failure calling back parent on '%s'", sockpath.c_str());
283 if (uconn)
delete uconn;
289 if ((rcc = uconn->send((
int) getpid())) != 0) {
290 Info(
"ERROR: failure sending pid to parent (errno: %d)", -rcc);
297 if ((rcc = uconn->recv(msg)) != 0) {
298 Info(
"ERROR: failure receiving admin path and executable from parent (errno: %d)", -rcc);
303 std::string srvadmin, adminpath, pspath;
304 msg >> srvadmin >> adminpath >> pspath >> ppid;
305 Info(
"srv admin path: %s", srvadmin.c_str());
306 Info(
"partial admin path: %s", adminpath.c_str());
307 Info(
"executable: %s", pspath.c_str());
308 Info(
"parent pid: %d", ppid);
312 if ((rcc = uconn->recv(msg)) != 0) {
313 Info(
"ERROR: failure receiving information about dataset and data dir(s) from parent (errno: %d)", -rcc);
318 std::string
group, creds, ord, datadir, ddiropts, datasetsrcs;
319 msg >> euid >> group >> creds >> ord >> datadir >> ddiropts >> datasetsrcs;
320 Info(
"euid at startup: %d", euid);
321 Info(
"group, ord: %s, %s", group.c_str(), ord.c_str());
322 Info(
"datadir: %s", datadir.c_str());
323 Info(
"datasetsrcs: %s", datasetsrcs.c_str());
326 if (
setownerships(euid, user, group, creds, datasetsrcs, datadir, ddiropts,
328 Info(
"ERROR: problems setting relevant user ownerships");
334 if (
mvfile(tenvfile, envfile, uid, gid, 0644) != 0) {
335 Info(
"ERROR: problems renaming '%s' to '%s' (errno: %d)",
336 tenvfile.c_str(), envfile.c_str(), errno);
341 if (
mvfile(trcfile, rcfile, uid, gid, 0644) != 0) {
342 Info(
"ERROR: problems renaming '%s' to '%s' (errno: %d)",
343 trcfile.c_str(), rcfile.c_str(), errno);
350 Info(
"ERROR: problems completing '%s'", rcfile.c_str());
356 Info(
"ERROR: problems setting environment from '%s'", envfile.c_str());
363 Info(
"ERROR: problems exporting file descriptor");
370 if (
loginuser(userdir, user, uid, gid) != 0) {
371 Info(
"ERROR: problems login user '%s' in", user.c_str());
378 Info(
"ERROR: problems redirecting logs to '%s'", logfile.c_str());
384 char *argvv[6] = {0};
387 if (adminpath.length() > 0) {
389 int len = srvadmin.length() + strlen(
"xpdpath:") + 1;
390 sxpd =
new char[len];
391 snprintf(sxpd, len,
"xpdpath:%s", adminpath.c_str());
403 argvv[0] = (
char *) pspath.c_str();
404 argvv[1] = (
char *)((
gType == 0) ?
"proofslave" :
"proofserv");
405 argvv[2] = (
char *)
"xpd";
406 argvv[3] = (
char *)sxpd;
407 argvv[4] = (
char *)slog;
413 sigaddset(&myset, SIGUSR1);
414 sigaddset(&myset, SIGUSR2);
415 pthread_sigmask(SIG_UNBLOCK, &myset, 0);
417 Info(
"%d: uid: %d, euid: %d", (
int)getpid(), getuid(), geteuid());
418 Info(
"argvv: '%s' '%s' '%s' '%s' '%s'", argvv[0], argvv[1], argvv[2], argvv[3], argvv[4]);
421 execv(pspath.c_str(), argvv);
424 Info(
"ERROR: returned from execv: bad, bad sign !!!");
431 int loginuser(
const std::string &home,
const std::string &user, uid_t uid, gid_t gid)
433 if (chdir(home.c_str()) != 0) {
434 Info(
"loginuser: ERROR: can't change directory to %s, euid: %d, uid: %d; errno: %d",
435 home.c_str(), geteuid(), getuid(), errno);
440 size_t len = home.length() + 8;
441 char *
h =
new char[len];
442 snprintf(h, len,
"HOME=%s", home.c_str());
444 if (
gDebug > 0)
Info(
"loginuser: set '%s'", h);
447 char *u =
new char[len];
448 snprintf(u, len,
"USER=%s", user.c_str());
450 if (
gDebug > 0)
Info(
"loginuser: set '%s'", u);
454 if (geteuid() != uid) {
455 rpdprivguard pguard((uid_t)0, (gid_t)0);
456 if (rpdbadpguard(pguard, uid)) {
457 Info(
"loginuser: ERROR: could not get required privileges");
460 initgroups(user.c_str(), gid);
465 Info(
"loginuser: acquiring target user identity (%d,%d)", uid, gid);
466 if (rpdpriv::changeperm(uid, gid) != 0) {
467 Info(
"loginuser: ERROR: can't acquire '%s' identity", user.c_str());
480 int assertdir(
const std::string &path, uid_t u, gid_t
g,
unsigned int mode)
482 if (path.length() <= 0)
return -1;
484 rpdprivguard pguard((uid_t)0, (gid_t)0);
485 if (rpdbadpguard(pguard, u)) {
486 Info(
"assertdir: ERROR: could not get privileges (errno: %d)", errno);
491 if (mkdir(path.c_str(), mode) != 0 && (errno != EEXIST)) {
492 Info(
"assertdir: ERROR: unable to create path: %s (errno: %d)", path.c_str(), errno);
496 if (chown(path.c_str(), u,
g) == -1) {
497 Info(
"assertdir: ERROR: unable to set ownership on path: %s (errno: %d)", path.c_str(), errno);
510 int mvfile(
const std::string &from,
const std::string &to, uid_t u, gid_t
g,
unsigned int mode)
512 if (from.length() <= 0 || to.length() <= 0)
return -1;
514 rpdprivguard pguard((uid_t)0, (gid_t)0);
515 if (rpdbadpguard(pguard, u)) {
516 Info(
"mvfile: ERROR: could not get privileges (errno: %d)", errno);
521 if (rename(from.c_str(), to.c_str()) != 0) {
522 Info(
"mvfile: ERROR: unable to rename '%s' to '%s' (errno: %d)", from.c_str(), to.c_str(), errno);
527 if (chmod(to.c_str(), mode) == -1) {
528 Info(
"mvfile: ERROR: unable to set mode %o on path: %s (errno: %d)", mode, to.c_str(), errno);
533 if (chown(to.c_str(), u,
g) == -1) {
534 Info(
"mvfile: ERROR: unable to set ownership on path: %s (errno: %d)", to.c_str(), errno);
546 const std::string &stag,
const std::string &adminpath)
548 FILE *frc = fopen(rcfile.c_str(),
"a");
550 Info(
"completercfile: ERROR: unable to open rc file: '%s' (errno: %d)", rcfile.c_str(), errno);
554 fprintf(frc,
"# The session working dir\n");
555 fprintf(frc,
"ProofServ.SessionDir: %s\n", sessdir.c_str());
557 fprintf(frc,
"# Session tag\n");
558 fprintf(frc,
"ProofServ.SessionTag: %s\n", stag.c_str());
560 fprintf(frc,
"# Admin path\n");
561 fprintf(frc,
"ProofServ.AdminPath: %s%d.status\n", adminpath.c_str(), (int)getpid());
573 const std::string &logfile,
const std::string &rcfile)
575 if (envfile.length() <= 0)
return -1;
580 len = logfile.length() + strlen(
"ROOTPROOFLOGFILE") + 4;
581 h =
new char[len + 1];
582 snprintf(h, len + 1,
"ROOTPROOFLOGFILE=%s", logfile.c_str());
585 Info(
"setproofservenv: set '%s'", h);
587 len = rcfile.length() + strlen(
"ROOTRCFILE") + 4;
588 h =
new char[len + 1];
589 snprintf(h, len + 1,
"ROOTRCFILE=%s", rcfile.c_str());
592 Info(
"setproofservenv: set '%s'", h);
594 std::fstream fin(envfile.c_str(), std::ios::in);
596 Info(
"setproofservenv: ERROR: unable to open env file: %s (errno: %d)", envfile.c_str(), errno);
602 std::getline(fin, line);
603 if (line[line.length()-1] ==
'\n') line.erase(line.length()-1);
604 if (line.length() > 0) {
605 h =
new char[line.length() + 1];
606 snprintf(h, line.length()+1,
"%s", line.c_str());
609 Info(
"setproofservenv: set '%s'", h);
626 if (!conn || (conn && !conn->isvalid(0))) {
627 Info(
"exportsock: ERROR: connection is %s", (conn ?
"invalid" :
"undefined"));
632 int d = conn->exportfd();
635 if (d == 0 || d == 1 || d == 2) {
638 while (natt > 0 && (fd = dup(d)) <= 2) {
639 if (fd >= 0 && fd != d)
close(fd);
643 if (natt <= 0 && fd <= 2) {
644 Info(
"exportsock: ERROR: no free filedescriptor!");
656 char *rootopensock =
new char[33];
657 snprintf(rootopensock, 33,
"ROOTOPENSOCK=%d", d);
658 putenv(rootopensock);
671 Info(
"redirectoutput: enter: %s", logfile.c_str());
673 if (logfile.length() <= 0) {
674 Info(
"redirectoutput: ERROR: logfile path undefined");
679 Info(
"redirectoutput: reopen %s", logfile.c_str());
680 FILE *
flog = freopen(logfile.c_str(),
"a", stdout);
682 Info(
"redirectoutput: ERROR: could not freopen stdout (errno: %d)", errno);
687 Info(
"redirectoutput: dup2 ...");
688 if ((dup2(fileno(stdout), fileno(stderr))) < 0) {
689 Info(
"redirectoutput: ERROR: could not redirect stderr (errno: %d)", errno);
698 int len = strlen(
"ROOTPROOFDONOTREDIR=2");
699 char *notredir =
new char[len + 1];
700 snprintf(notredir, len+1,
"ROOTPROOFDONOTREDIR=2");
704 Info(
"redirectoutput: done!");
714 const std::string &creds,
const std::string &dsrcs,
715 const std::string &ddir,
const std::string &ddiro,
716 const std::string &ord,
const std::string &stag)
719 struct passwd *pwad, *pwus;
720 if (!(pwad = getpwuid(euid))) {
721 Info(
"setownerships: ERROR: problems getting 'struct passwd' for" 722 " uid: %d (errno: %d)", euid, (
int)errno);
725 if (!(pwus = getpwnam(us.c_str()))) {
726 Info(
"setownerships: ERROR: problems getting 'struct passwd' for" 727 " user: '%s' (errno: %d)", us.c_str(), (int)errno);
733 if (dsrcs.length() > 0) {
734 std::string dsrc(dsrcs);
735 std::string::size_type loc = dsrcs.find(
',', 0);
737 if (loc != std::string::npos) dsrc.erase(loc, std::string::npos);
738 if (dsrc.length() > 0) {
745 Info(
"setownerships: ERROR: problems asserting '%s' in mode 0755" 746 " (errno: %d)", d.c_str(), (int)errno);
749 Info(
"setownerships: ERROR: problems asserting '%s' in mode 0777" 750 " (errno: %d)", d.c_str(), (int)errno);
753 dsrc.assign(dsrcs, loc + 1, dsrcs.length() - loc);
755 }
while ((loc = dsrcs.find(
',', loc)) != std::string::npos);
760 if (ddir.length() > 0 && ord.length() > 0 && stag.length() > 0) {
761 std::string dgr(ddir);
762 dgr +=
"/"; dgr +=
gr;
765 unsigned int mode = 0755;
766 if (ddiro.find(
'g') != std::string::npos) mode = 0775;
767 if (ddiro.find(
'a') != std::string::npos ||
768 ddiro.find(
'o') != std::string::npos) mode = 0777;
769 std::string dus(dgr);
770 dus +=
"/"; dus +=
us;
772 dus +=
"/"; dus += ord;
774 dus +=
"/"; dus += stag;
779 Info(
"setownerships: ERROR: problems asserting '%s' in mode %o" 780 " (errno: %d)", dus.c_str(), mode, (int)errno);
782 Info(
"setownerships: ERROR: problems asserting '%s' in mode 0777" 783 " (errno: %d)", dgr.c_str(), (int)errno);
788 if (creds.length() > 0) {
790 Info(
"setownerships: ERROR: problems changing owenership of '%s'", creds.c_str());
806 if (path.length() <= 0)
return -1;
809 DIR *dir = opendir(path.c_str());
812 std::string proot(path);
813 if (!(proot.rfind(
'/') != proot.length() - 1)) proot +=
"/";
815 struct dirent *ent = 0;
816 while ((ent = readdir(dir))) {
817 if (ent->d_name[0] ==
'.' || !strcmp(ent->d_name,
".."))
continue;
818 std::string fn(proot);
823 Info(
"changeown: ERROR: problems changing recursively ownership of '%s'",
834 if (errno != 0 && (errno != ENOTDIR)) {
835 Info(
"changeown: ERROR: problems opening '%s' (errno: %d)",
836 path.c_str(), (int)errno);
840 rpdprivguard pguard((uid_t)0, (gid_t)0);
841 if (rpdbadpguard(pguard, u)) {
842 Info(
"changeown: ERROR: could not get privileges (errno: %d)", errno);
846 if (chown(path.c_str(), u,
g) == -1) {
847 Info(
"changeown: ERROR: cannot set user ownership on path '%s' (errno: %d)",
848 path.c_str(), errno);
void start_rootd(int argc, char **argv)
Process a request to start a rootd server.
void Info(const char *va_(fmt),...)
Write info message to syslog.
static constexpr double us
void start_ps(int argc, char **argv)
Process a request to start a proofserv process.
int setownerships(int euid, const std::string &us, const std::string &gr, const std::string &creds, const std::string &dsrcs, const std::string &ddir, const std::string &ddiro, const std::string &ord, const std::string &stag)
Set user ownerships on some critical files or directories.
int redirectoutput(const std::string &logfile)
Redirect stdout to 'logfile' On success return 0.
int setproofservenv(const std::string &envfile, const std::string &logfile, const std::string &rcfile)
Initialize the environment following the content of 'envfile'.
int loginuser(const std::string &home, const std::string &user, uid_t u, gid_t g)
Login the user in its space.
int exportsock(rpdunix *conn)
Export the descriptor of 'conn' so that it can used in the execv application.
int mvfile(const std::string &from, const std::string &to, uid_t u, gid_t g, unsigned int mode)
Move file form 'from' to 'to', making sure that it is owned by the entity described by {u...
int assertdir(const std::string &path, uid_t u, gid_t g, unsigned int mode)
Make sure that 'path' exists, it is owned by the entity described by {u,g} and its mode is 'mode'...
int changeown(const std::string &path, uid_t u, gid_t g)
Change the ownership of 'path' to the entity described by {u,g}.
int completercfile(const std::string &rcfile, const std::string &sessdir, const std::string &stag, const std::string &adminpath)
Finalize the rc file with the missing pieces.
int main(int argc, char **argv)
Program executed via system starting proofserv instances.
static constexpr double g