37
37
#include <stdlib.h>
38
38
#include <string.h>
39
39
#include <unistd.h>
40
#include <sys/types.h>
44
41
/* conf.h is admin-configured by the setup process.
45
42
* It defines jail_base.
48
#define ARG_JAILPATH 2
49
56
/* Returns TRUE if the given uid is allowed to execute trampoline.
50
57
* Only root or the web server should be allowed to execute.
51
58
* This is determined by the whitelist allowed_uids in conf.h.
69
/* Turn the process into a daemon using the standard
76
/* already a daemon */
77
if ( getppid() == 1 ) return;
79
/* Fork off the parent process */
84
/* If we got a good PID, then we can exit the parent process. */
89
/* At this point we are executing as the child process */
91
/* Change the file mode mask */
94
/* Create a new SID for the child process */
100
/* Change the current working directory. This prevents the current
101
directory from being locked; hence not being able to remove it. */
102
if ((chdir("/")) < 0) {
106
/* Redirect standard files to /dev/null */
107
freopen( "/dev/null", "r", stdin);
108
freopen( "/dev/null", "w", stdout);
109
freopen( "/dev/null", "w", stderr);
112
static void usage(const char* nm)
115
"usage: %s [-d] <uid> <jail> <cwd> <program> [args...]\n", nm);
119
76
int main(int argc, char* const argv[])
128
char canonical_jailpath[PATH_MAX];
130
81
/* Disallow execution from all users but the whitelisted ones, and root */
131
82
if (!uid_allowed(getuid()))
137
88
/* Args check and usage */
143
if (strcmp(argv[arg_num], "-d") == 0)
152
uid = atoi(argv[arg_num++]);
153
jailpath = argv[arg_num++];
154
work_dir = argv[arg_num++];
155
prog = argv[arg_num];
156
args = argv + arg_num;
91
fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
158
96
/* Disallow suiding to the root user */
97
uid = atoi(argv[ARG_UID]);
161
100
fprintf(stderr, "cannot set up a jail as root\n");
165
/* Jail path must be an absolute path,
166
* and it must begin with jail_base.
108
* Begin with jail_base
168
if (norm(canonical_jailpath, PATH_MAX, jailpath) != 0)
170
fprintf(stderr, "bad jail path: %s\n", jailpath);
173
if (strncmp(canonical_jailpath, jail_base, strlen(jail_base)))
110
jailpath = argv[ARG_JAILPATH];
111
if (strlen(jailpath) < 1 || jailpath[0] != '/'
112
|| strstr(jailpath, "/..")
113
|| strncmp(jailpath, jail_base, strlen(jail_base)))
175
115
fprintf(stderr, "bad jail path: %s\n", jailpath);
179
119
/* chroot into the jail.
180
120
* Henceforth this process, and its children, cannot see anything above
181
* canoncial_jailpath. */
182
if (chroot(canonical_jailpath))
122
if (chroot(jailpath))
184
124
perror("could not chroot");
188
128
/* chdir into the specified working directory */
129
if (chdir(argv[ARG_CWD]))
191
131
perror("could not chdir");
209
144
/* exec (replace this process with the a new instance of the target
210
145
* program). Pass along all the arguments.
211
146
* Note that for script execution, the "program" will be the interpreter,
212
147
* and the first argument will be the script. */
148
execv(argv[ARG_PROG], argv + ARG_PROG);
215
/* XXX if (daemon_mode) use syslog? */
216
150
/* nb exec won't return unless there was an error */
217
151
perror("could not exec");