46
/* Returns TRUE if the given uid is allowed to execute trampoline.
47
* Only root or the web server should be allowed to execute.
48
* This is determined by the whitelist allowed_uids in conf.h.
50
int uid_allowed(int uid)
53
/* root is always allowed to execute trampoline */
56
/* loop over all allowed_uids */
57
for (i=(sizeof(allowed_uids)/sizeof(*allowed_uids))-1; i>=0; i--)
59
if (allowed_uids[i] == uid)
62
/* default to disallowing */
66
/* Turn the process into a daemon using the standard
73
/* already a daemon */
74
if ( getppid() == 1 ) return;
76
/* Fork off the parent process */
81
/* If we got a good PID, then we can exit the parent process. */
86
/* At this point we are executing as the child process */
88
/* Change the file mode mask */
91
/* Create a new SID for the child process */
97
/* Change the current working directory. This prevents the current
98
directory from being locked; hence not being able to remove it. */
99
if ((chdir("/")) < 0) {
103
/* Redirect standard files to /dev/null */
104
freopen( "/dev/null", "r", stdin);
105
freopen( "/dev/null", "w", stdout);
106
freopen( "/dev/null", "w", stderr);
109
static void usage(const char* nm)
112
"usage: %s [-d] <uid> <jail> <cwd> <program> [args...]\n", nm);
48
#define ARG_JAILPATH 2
116
56
int main(int argc, char* const argv[])
126
/* Disallow execution from all users but the whitelisted ones, and root */
127
if (!uid_allowed(getuid()))
129
fprintf(stderr, "only the web server may execute trampoline\n");
133
60
/* Args check and usage */
139
if (strcmp(argv[arg_num], "-d") == 0)
148
uid = atoi(argv[arg_num++]);
149
jailpath = argv[arg_num++];
150
work_dir = argv[arg_num++];
151
prog = argv[arg_num];
152
args = argv + arg_num;
63
fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
154
68
/* Disallow suiding to the root user */
69
uid = atoi(argv[ARG_UID]);
157
72
fprintf(stderr, "cannot set up a jail as root\n");
205
116
/* exec (replace this process with the a new instance of the target
206
117
* program). Pass along all the arguments.
207
118
* Note that for script execution, the "program" will be the interpreter,
208
119
* and the first argument will be the script. */
120
execv(argv[ARG_PROG], argv + ARG_PROG);
211
/* XXX if (daemon_mode) use syslog? */
212
122
/* nb exec won't return unless there was an error */
213
123
perror("could not exec");