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