/* This chroot command does very little. Once the chroot system call is executed, it (option) remove the CAP_SYS_CHROOT capability. Then it executes its argument */ #include #include #include #include #include #include #include "vutil.h" #include #include "old_syscall.p" static int my_chroot(const char *dir) { int ret = -1; if (has_chrootsafe()){ ret = call_chrootsafe(dir); }else{ // fprintf (stderr,"Kernel do not support chrootsafe(), using chroot()\n"); ret = chroot (dir); } return ret; } int main (int argc, char *argv[]) { if (argc < 3){ fprintf (stderr,"capchroot version %s\n",VERSION); fprintf (stderr ,"capchroot [--nochroot] [--suid user] directory command argument\n" "\n" "--nochroot remove the CAP_SYS_CHROOT capability\n" " after the chroot system call.\n" "--suid user switch to a different user (in the vserver context)\n" " before executing the command.\n"); } else { const char *uid = NULL; bool nochroot = false; int dir; for (dir=1; dirpw_gid) == -1){ fprintf(stderr,"initgroups error: %s\n" ,strerror(errno)) ; exit(-1) ; } if (setgid(p->pw_gid) == -1){ fprintf(stderr,"setgid error: %s\n" ,strerror(errno)) ; exit(-1) ; } if (setuid(p->pw_uid) == -1){ fprintf(stderr,"setuid error: %s\n" ,strerror(errno)) ; exit(-1) ; } setenv("HOME", p->pw_dir, 1) ; setenv("SHELL", p->pw_shell, 1) ; setenv("USER", p->pw_name, 1) ; setenv("LOGNAME", p->pw_name, 1) ; } int cmd = dir + 1; if (cmd >= argc){ fprintf (stderr,"capchroot: No command to execute, do nothing\n"); }else{ #if 0 for (int i=cmd; i