minor cosmetical changes
[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
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29 #include "compat.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <pwd.h>
34 #include <grp.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <stdbool.h>
38 #include <stdlib.h>
39
40 #include "linuxcaps.h"
41 #include "vserver.h"
42
43 int main (int argc, char *argv[])
44 {
45         if (argc < 3){
46                 fprintf (stderr,"capchroot version %s\n",VERSION);
47                 fprintf (stderr
48                         ,"capchroot --nochroot directory [ --suid user ] command argument\n"
49                          "\n"
50                          "--nochroot remove the CAP_SYS_CHROOT capability\n"
51                          "           after the chroot system call.\n"
52                          "--suid switch to a different user (in the vserver context)\n"
53                          "       before executing the command.\n");
54         }else{
55                 const char *uid = NULL;
56                 bool nochroot = false;
57                 int dir;
58                 for (dir=1; dir<argc; dir++){
59                         const char *arg = argv[dir];
60                         if (arg[0] != '-' && arg[1] != '-'){
61                                 break;
62                         }else if (strcmp(arg,"--nochroot")==0){
63                                 nochroot = true;
64                         }else if (strcmp(arg,"--suid")==0){
65                                 dir++;
66                                 uid = argv[dir];
67                         }
68                         
69                 }
70                 // We resolve the UID before doing the chroot.
71                 // If we do the getpwnam after the chroot, we will end
72                 // up loading shared object from the vserver.
73                 // This is causing two kind of problem: Incompatibilities
74                 // and also a security flaw. The shared objects in the vserver
75                 // may be tweaked to get control of the root server ...
76                 getpwnam ("root");
77                 if (vc_chrootsafe (argv[dir]) == -1){
78                         fprintf (stderr,"Can't chroot to directory %s (%s)\n",argv[dir]
79                                 ,strerror(errno));
80                 }else{
81                         struct passwd *p = NULL;
82                         int cmd          = dir + 1;
83
84                         if (nochroot){
85                                 vc_new_s_context (-2,1<<CAP_SYS_CHROOT,0);
86                         }
87
88                         if (uid != NULL && strcmp(uid,"root")!=0){
89                                 p = getpwnam(uid);
90                                 if (p == NULL){
91                                         fprintf (stderr,"User not found: %s\n",uid);
92                                         exit (-1);
93                                 }
94                         }
95                         if (p != NULL) {
96                                 setgroups (0,NULL);
97                                 setgid(p->pw_gid);
98                                 setuid(p->pw_uid);
99                         }
100                         if (cmd >= argc){
101                                 fprintf (stderr,"capchroot: No command to execute, do nothing\n");
102                         }else{
103                                 execvp (argv[cmd],argv+cmd);
104                                 fprintf (stderr,"Can't execute %s (%s)\n",argv[cmd]
105                                         ,strerror(errno));
106                         }
107                 }
108         }
109         return -1;
110 }
111
112