2006-07-06 11:04:46 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
|
|
|
//
|
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
2007-10-09 11:35:47 +00:00
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-07-06 11:04:46 +00:00
|
|
|
|
2006-06-05 04:30:16 +00:00
|
|
|
// switcher.C
|
|
|
|
//
|
|
|
|
// When run as
|
2006-06-17 11:51:48 +00:00
|
|
|
// switcher Path X1 ... Xn
|
|
|
|
// runs program at Path with args X1. ... Xn
|
2006-06-05 04:30:16 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2006-06-17 11:51:48 +00:00
|
|
|
#include <stdio.h>
|
2007-10-02 09:31:20 +00:00
|
|
|
#include <string.h>
|
2006-07-01 05:32:02 +00:00
|
|
|
#include <cerrno>
|
2006-07-06 11:04:46 +00:00
|
|
|
#include <sys/param.h> // for MAXPATHLEN
|
2007-10-02 09:31:20 +00:00
|
|
|
#include <pwd.h> // getpwuid
|
|
|
|
#include <grp.h>
|
2006-06-05 04:30:16 +00:00
|
|
|
|
2006-06-17 11:51:48 +00:00
|
|
|
int main(int argc, char** argv) {
|
2007-10-02 09:31:20 +00:00
|
|
|
passwd *pw;
|
|
|
|
group *grp;
|
|
|
|
char user_name[256], group_name[256];
|
|
|
|
|
2007-10-02 23:29:28 +00:00
|
|
|
strcpy(user_name, "boinc_project");
|
|
|
|
strcpy(group_name, "boinc_project");
|
2006-06-17 11:51:48 +00:00
|
|
|
|
2006-07-06 11:04:46 +00:00
|
|
|
#if 0 // For debugging only
|
|
|
|
char current_dir[MAXPATHLEN];
|
|
|
|
|
|
|
|
getcwd( current_dir, sizeof(current_dir));
|
|
|
|
fprintf(stderr, "current directory = %s\n", current_dir);
|
|
|
|
|
2006-06-17 11:51:48 +00:00
|
|
|
for (int i=0; i<argc; i++) {
|
|
|
|
fprintf(stderr, "switcher arg %d: %s\n", i, argv[i]);
|
|
|
|
}
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
|
|
|
|
2007-10-02 09:31:20 +00:00
|
|
|
#if 0 // For debugging only
|
|
|
|
// Allow debugging without running as user or group boinc_project
|
|
|
|
pw = getpwuid(getuid());
|
2007-10-02 23:29:28 +00:00
|
|
|
if (pw) strcpy(user_name, pw->pw_name);
|
2007-10-02 09:31:20 +00:00
|
|
|
grp = getgrgid(getgid());
|
2007-10-02 23:29:28 +00:00
|
|
|
if (grp) strcpy(group_name, grp->gr_gid);
|
2007-10-02 09:31:20 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// We are running setuid root, so setgid() sets real group ID,
|
|
|
|
// effective group ID and saved set_group-ID for this process
|
|
|
|
grp = getgrnam(group_name);
|
|
|
|
if (grp) setgid(grp->gr_gid);
|
|
|
|
|
|
|
|
// We are running setuid root, so setuid() sets real user ID,
|
|
|
|
// effective user ID and saved set_user-ID for this process
|
|
|
|
pw = getpwnam(user_name);
|
|
|
|
if (pw) setuid(pw->pw_uid);
|
|
|
|
|
2006-06-17 11:51:48 +00:00
|
|
|
execv(argv[1], argv+2);
|
2006-07-01 05:32:02 +00:00
|
|
|
|
|
|
|
// If we got here execv failed
|
|
|
|
fprintf(stderr, "Process creation (%s) failed: errno=%d\n", argv[1], errno);
|
|
|
|
|
2006-06-05 04:30:16 +00:00
|
|
|
}
|