00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org> 00002 // 00003 // This program is free software; you can redistribute it and/or 00004 // modify it under the terms of the GNU General Public License 00005 // as published by the Free Software Foundation; either version 2 00006 // of the License, or (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 00016 00017 #include <unistd.h> 00018 #include <errno.h> 00019 00020 #include "chroot.h" 00021 #include "misc.h" 00022 #include "open.h" 00023 00024 /* go to <dir> in <root> as root 00025 ** going into the chroot before doing chdir(dir) prevents symlink attacks 00026 ** and hence is safer */ 00027 int chroot_mkdirp(const char *root, const char *dir, mode_t mode) 00028 { 00029 int orig_root, new_root; 00030 int errno_orig; 00031 00032 if ((orig_root = open_read("/")) == -1) 00033 return -1; 00034 00035 if (chdir(root) == -1) 00036 return -1; 00037 00038 if ((new_root = open_read(".")) == -1) 00039 return -1; 00040 00041 /* check cwdfd */ 00042 if (chroot_fd(new_root) == -1) 00043 return -1; 00044 00045 /* now create the dir in the chroot */ 00046 if (mkdirp(dir, mode) == -1) 00047 goto err; 00048 00049 /* break out of the chroot */ 00050 chroot_fd(orig_root); 00051 00052 return 0; 00053 00054 err: 00055 errno_orig = errno; 00056 chroot_fd(orig_root); 00057 errno = errno_orig; 00058 return -1; 00059 }