3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on capchroot.cc by Jacques Gelinas
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 This chroot command does very little. Once the chroot
22 system call is executed, it (option) remove the CAP_SYS_CHROOT
23 capability. Then it executes its argument
39 #include "linuxcaps.h"
43 static int my_chroot(const char *dir)
46 if (has_chrootsafe()){
47 ret = call_chrootsafe(dir);
49 //fprintf (stderr,"Kernel do not support chrootsafe(), using chroot()\n");
55 int main (int argc, char *argv[])
58 fprintf (stderr,"capchroot version %s\n",VERSION);
60 ,"capchroot --nochroot directory [ --suid user ] command argument\n"
62 "--nochroot remove the CAP_SYS_CHROOT capability\n"
63 " after the chroot system call.\n"
64 "--suid switch to a different user (in the vserver context)\n"
65 " before executing the command.\n");
67 const char *uid = NULL;
68 bool nochroot = false;
70 for (dir=1; dir<argc; dir++){
71 const char *arg = argv[dir];
72 if (arg[0] != '-' && arg[1] != '-'){
74 }else if (strcmp(arg,"--nochroot")==0){
76 }else if (strcmp(arg,"--suid")==0){
82 // We resolve the UID before doing the chroot.
83 // If we do the getpwnam after the chroot, we will end
84 // up loading shared object from the vserver.
85 // This is causing two kind of problem: Incompatibilities
86 // and also a security flaw. The shared objects in the vserver
87 // may be tweaked to get control of the root server ...
89 if (my_chroot (argv[dir]) == -1){
90 fprintf (stderr,"Can't chroot to directory %s (%s)\n",argv[dir]
93 struct passwd *p = NULL;
97 call_new_s_context (0,NULL,1<<CAP_SYS_CHROOT,0);
100 if (uid != NULL && strcmp(uid,"root")!=0){
103 fprintf (stderr,"User not found: %s\n",uid);
113 fprintf (stderr,"capchroot: No command to execute, do nothing\n");
115 execvp (argv[cmd],argv+cmd);
116 fprintf (stderr,"Can't execute %s (%s)\n",argv[cmd]