37
37
#include <stdlib.h>
38
38
#include <string.h>
39
39
#include <unistd.h>
40
#include <sys/types.h>
41
44
/* conf.h is admin-configured by the setup process.
42
45
* It defines jail_base.
48
#define ARG_JAILPATH 2
56
49
/* Returns TRUE if the given uid is allowed to execute trampoline.
57
50
* Only root or the web server should be allowed to execute.
58
51
* 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);
76
119
int main(int argc, char* const argv[])
128
char canonical_jailpath[PATH_MAX];
81
130
/* Disallow execution from all users but the whitelisted ones, and root */
82
131
if (!uid_allowed(getuid()))
88
137
/* Args check and usage */
91
fprintf(stderr, "usage: %s <uid> <jail> <cwd> <program> [args...]\n",
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;
96
158
/* Disallow suiding to the root user */
97
uid = atoi(argv[ARG_UID]);
100
161
fprintf(stderr, "cannot set up a jail as root\n");
108
* Begin with jail_base
165
/* Jail path must be an absolute path,
166
* and it must begin with 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)))
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)))
115
175
fprintf(stderr, "bad jail path: %s\n", jailpath);
119
179
/* chroot into the jail.
120
180
* Henceforth this process, and its children, cannot see anything above
122
if (chroot(jailpath))
181
* canoncial_jailpath. */
182
if (chroot(canonical_jailpath))
124
184
perror("could not chroot");
128
188
/* chdir into the specified working directory */
129
if (chdir(argv[ARG_CWD]))
131
191
perror("could not chdir");
144
209
/* exec (replace this process with the a new instance of the target
145
210
* program). Pass along all the arguments.
146
211
* Note that for script execution, the "program" will be the interpreter,
147
212
* and the first argument will be the script. */
148
execv(argv[ARG_PROG], argv + ARG_PROG);
215
/* XXX if (daemon_mode) use syslog? */
150
216
/* nb exec won't return unless there was an error */
151
217
perror("could not exec");