00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00169 #include <unistd.h>
00170 #include <errno.h>
00171 #include <string.h>
00172 #include <fcntl.h>
00173 #include <stdio.h>
00174 #include <stdlib.h>
00175 
00176 #include "../config.h"
00177 #include "../interface.h"
00178 
00179 
00180 #define STOP(...)                                                       \
00181     do {                                                                \
00182         fprintf(stderr, __VA_ARGS__);                                       \
00183         fprintf(stderr, "\n%s (%d)\n"                                       \
00184                 "fsvs-chrooter (licensed under the GPLv3), (C) by Ph. Marek;"   \
00185                 " version " FSVS_VERSION "\n",                                                              \
00186         strerror(errno), errno);                                            \
00187         exit(errno);                                                        \
00188     } while (0)
00189 
00190 
00191 void open_keep_set(char *fn, char *env)
00192 {
00193     char stg[10];
00194     int hdl;
00195     int flags;
00196     int status;
00197 
00198 
00199     hdl=open(fn, O_RDONLY);
00200     if (hdl<0) STOP("Cannot open directory %s", fn);
00201 
00202     flags=fcntl(hdl, F_GETFD);
00203     if ( flags == -1 )
00204         STOP("Cannot get fd flags");
00205 
00206     flags &= ~FD_CLOEXEC;
00207     status=fcntl(hdl, F_SETFD, flags);
00208     if ( flags == -1 )
00209         STOP("Cannot set fd flags");
00210 
00211     sprintf(stg,"%d",hdl);
00212   setenv(env, stg, 1);
00213 }
00214 
00215 
00216 int main(int argc, char *args[])
00217 {
00218     errno=0;
00219 
00220     if ( getenv(CHROOTER_LIBS_ENV) == NULL)
00221         STOP("Please specify in %s which libraries should be preloaded.",
00222                 CHROOTER_LIBS_ENV);
00223 
00224     open_keep_set("/", CHROOTER_ROOT_ENV);
00225     open_keep_set(".", CHROOTER_CWD_ENV);
00226 
00227     if (chroot(CHROOTER_JAIL)==-1)
00228         STOP("Cannot do chroot(%s)", CHROOTER_JAIL);
00229 
00230     if (chdir("/") == -1)
00231         STOP("Cannot do chdir(/) call");
00232 
00233     execvp("fsvs",args);
00234     STOP("Executing fsvs in the chroot jail failed");
00235 
00236     return 0;
00237 }
00238