chroot/chroot_mkdirp.c

Go to the documentation of this file.
00001 // Copyright 2006 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (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
00015 // Free Software Foundation, Inc.,
00016 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 
00018 #include <unistd.h>
00019 #include <errno.h>
00020 
00021 #include "chroot.h"
00022 #include "misc.h"
00023 #include "open.h"
00024 
00025 /* go to <dir> in <root> as root
00026 ** going into the chroot before doing chdir(dir) prevents symlink attacks
00027 ** and hence is safer */
00028 int chroot_mkdirp(const char *root, const char *dir, mode_t mode)
00029 {
00030         int orig_root, new_root;
00031         int errno_orig;
00032         
00033         if ((orig_root = open_read("/")) == -1)
00034                 return -1;
00035         
00036         if (chdir(root) == -1)
00037                 return -1;
00038         
00039         if ((new_root = open_read(".")) == -1)
00040                 return -1;
00041         
00042         /* check cwdfd */
00043         if (chroot_fd(new_root) == -1)
00044                 return -1;
00045         
00046         /* now create the dir in the chroot */
00047         if (mkdirp(dir, mode) == -1)
00048                 goto err;
00049         
00050         /* break out of the chroot */
00051         chroot_fd(orig_root);
00052         
00053         return 0;
00054         
00055 err:
00056         errno_orig = errno;
00057         chroot_fd(orig_root);
00058         errno = errno_orig;
00059         return -1;
00060 }

Generated on Sun Dec 3 17:45:53 2006 for lucid by  doxygen 1.5.1