initial checkin
[util-vserver.git] / util-vserver / src / capchroot.c
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on capchroot.cc by Jacques Gelinas
5 //  
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)
9 // any later version.
10 //  
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.
15 //  
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.
19
20 /*
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
24 */
25 #include <stdio.h>
26 #include <string.h>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdbool.h>
32 #include <stdlib.h>
33
34 #include "linuxcaps.h"
35 #include "vserver.h"
36
37 static int my_chroot(const char *dir)
38 {
39         int ret = -1;
40         if (has_chrootsafe()){
41                 ret = call_chrootsafe(dir);
42         }else{
43             //fprintf (stderr,"Kernel do not support chrootsafe(), using chroot()\n");
44                 ret = chroot (dir);
45         }
46         return ret;
47 }
48
49 int main (int argc, char *argv[])
50 {
51         if (argc < 3){
52                 fprintf (stderr,"capchroot version %s\n",VERSION);
53                 fprintf (stderr
54                         ,"capchroot --nochroot directory [ --suid user ] command argument\n"
55                          "\n"
56                          "--nochroot remove the CAP_SYS_CHROOT capability\n"
57                          "           after the chroot system call.\n"
58                          "--suid switch to a different user (in the vserver context)\n"
59                          "       before executing the command.\n");
60         }else{
61                 const char *uid = NULL;
62                 bool nochroot = false;
63                 int dir;
64                 for (dir=1; dir<argc; dir++){
65                         const char *arg = argv[dir];
66                         if (arg[0] != '-' && arg[1] != '-'){
67                                 break;
68                         }else if (strcmp(arg,"--nochroot")==0){
69                                 nochroot = true;
70                         }else if (strcmp(arg,"--suid")==0){
71                                 dir++;
72                                 uid = argv[dir];
73                         }
74                         
75                 }
76                 // We resolve the UID before doing the chroot.
77                 // If we do the getpwnam after the chroot, we will end
78                 // up loading shared object from the vserver.
79                 // This is causing two kind of problem: Incompatibilities
80                 // and also a security flaw. The shared objects in the vserver
81                 // may be tweaked to get control of the root server ...
82                 getpwnam ("root");
83                 if (my_chroot (argv[dir]) == -1){
84                         fprintf (stderr,"Can't chroot to directory %s (%s)\n",argv[dir]
85                                 ,strerror(errno));
86                 }else{
87                         struct passwd *p = NULL;
88                         int cmd          = dir + 1;
89
90                         if (nochroot){
91                                 call_new_s_context (0,NULL,1<<CAP_SYS_CHROOT,0);
92                         }
93
94                         if (uid != NULL && strcmp(uid,"root")!=0){
95                                 p = getpwnam(uid);
96                                 if (p == NULL){
97                                         fprintf (stderr,"User not found: %s\n",uid);
98                                         exit (-1);
99                                 }
100                         }
101                         if (p != NULL) {
102                                 setgroups (0,NULL);
103                                 setgid(p->pw_gid);
104                                 setuid(p->pw_uid);
105                         }
106                         if (cmd >= argc){
107                                 fprintf (stderr,"capchroot: No command to execute, do nothing\n");
108                         }else{
109                                 execvp (argv[cmd],argv+cmd);
110                                 fprintf (stderr,"Can't execute %s (%s)\n",argv[cmd]
111                                         ,strerror(errno));
112                         }
113                 }
114         }
115         return -1;
116 }
117
118