2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1991 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 2001, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2003 - 2006, Tomasz Kłoczko
|
|
|
|
* Copyright (c) 2007 - 2008, Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 3. The name of the copyright holders or contributors may not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2009-04-28 01:39:18 +05:30
|
|
|
#include <assert.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2007-12-26 19:24:23 +05:30
|
|
|
#include <sys/time.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#endif
|
2009-04-25 05:11:28 +05:30
|
|
|
static /*@null@*/const char *src_orig;
|
|
|
|
static /*@null@*/const char *dst_orig;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
struct link_name {
|
|
|
|
dev_t ln_dev;
|
|
|
|
ino_t ln_ino;
|
2009-04-25 05:11:28 +05:30
|
|
|
nlink_t ln_count;
|
2007-10-07 17:15:23 +05:30
|
|
|
char *ln_name;
|
2009-04-25 05:11:28 +05:30
|
|
|
/*@dependent@*/struct link_name *ln_next;
|
2007-10-07 17:14:02 +05:30
|
|
|
};
|
2009-04-25 05:11:28 +05:30
|
|
|
static /*@exposed@*/struct link_name *links;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_entry (const char *src, const char *dst,
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid);
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_dir (const char *src, const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid);
|
2007-12-28 04:53:51 +05:30
|
|
|
#ifdef S_IFLNK
|
2009-05-10 23:51:58 +05:30
|
|
|
static char *readlink_malloc (const char *filename);
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_symlink (const char *src, const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid);
|
2007-12-28 04:53:51 +05:30
|
|
|
#endif
|
|
|
|
static int copy_hardlink (const char *src, const char *dst,
|
|
|
|
struct link_name *lp);
|
2008-01-06 17:32:24 +05:30
|
|
|
static int copy_special (const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid);
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_file (const char *src, const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid);
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef WITH_SELINUX
|
2007-12-28 05:02:47 +05:30
|
|
|
/*
|
|
|
|
* selinux_file_context - Set the security context before any file or
|
|
|
|
* directory creation.
|
|
|
|
*
|
|
|
|
* selinux_file_context () should be called before any creation of file,
|
|
|
|
* symlink, directory, ...
|
|
|
|
*
|
2009-04-11 21:04:10 +05:30
|
|
|
* Callers may have to Reset SELinux to create files with default
|
|
|
|
* contexts:
|
|
|
|
* setfscreatecon (NULL);
|
2007-12-28 05:02:47 +05:30
|
|
|
*/
|
2009-04-11 21:04:10 +05:30
|
|
|
int selinux_file_context (const char *dst_name)
|
2007-10-07 17:16:07 +05:30
|
|
|
{
|
2008-05-26 05:15:21 +05:30
|
|
|
static bool selinux_checked = false;
|
|
|
|
static bool selinux_enabled;
|
2007-10-07 17:16:07 +05:30
|
|
|
security_context_t scontext = NULL;
|
|
|
|
|
2008-05-26 05:15:21 +05:30
|
|
|
if (!selinux_checked) {
|
2007-10-07 17:16:07 +05:30
|
|
|
selinux_enabled = is_selinux_enabled () > 0;
|
2008-05-26 05:15:21 +05:30
|
|
|
selinux_checked = true;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2008-05-26 05:15:21 +05:30
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
if (selinux_enabled) {
|
2007-12-28 05:10:00 +05:30
|
|
|
/* Get the default security context for this file */
|
|
|
|
if (matchpathcon (dst_name, 0, &scontext) < 0) {
|
2007-12-28 05:33:26 +05:30
|
|
|
if (security_getenforce () != 0) {
|
2007-10-07 17:16:07 +05:30
|
|
|
return 1;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Set the security context for the next created file */
|
|
|
|
if (setfscreatecon (scontext) < 0) {
|
2007-12-28 05:33:26 +05:30
|
|
|
if (security_getenforce () != 0) {
|
2007-10-07 17:16:07 +05:30
|
|
|
return 1;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
freecon (scontext);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2007-12-28 05:53:33 +05:30
|
|
|
* remove_link - delete a link from the linked list
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2009-04-25 05:11:28 +05:30
|
|
|
static void remove_link (/*@only@*/struct link_name *ln)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct link_name *lp;
|
|
|
|
|
|
|
|
if (links == ln) {
|
|
|
|
links = ln->ln_next;
|
|
|
|
free (ln->ln_name);
|
|
|
|
free (ln);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-28 05:33:26 +05:30
|
|
|
for (lp = links; NULL !=lp; lp = lp->ln_next) {
|
2007-12-28 05:10:00 +05:30
|
|
|
if (lp->ln_next == ln) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-28 05:33:26 +05:30
|
|
|
if (NULL == lp) {
|
2009-04-25 05:11:28 +05:30
|
|
|
free (ln->ln_name);
|
|
|
|
free (ln);
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
lp->ln_next = lp->ln_next->ln_next;
|
|
|
|
free (ln->ln_name);
|
|
|
|
free (ln);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_link - see if a file is really a link
|
|
|
|
*/
|
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
static /*@exposed@*/ /*@null@*/struct link_name *check_link (const char *name, const struct stat *sb)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
struct link_name *lp;
|
2008-06-14 01:40:53 +05:30
|
|
|
size_t src_len;
|
|
|
|
size_t dst_len;
|
|
|
|
size_t name_len;
|
|
|
|
size_t len;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
/* copy_tree () must be the entry point */
|
|
|
|
assert (NULL != src_orig);
|
|
|
|
assert (NULL != dst_orig);
|
|
|
|
|
|
|
|
for (lp = links; NULL != lp; lp = lp->ln_next) {
|
2007-12-28 05:33:26 +05:30
|
|
|
if ((lp->ln_dev == sb->st_dev) && (lp->ln_ino == sb->st_ino)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return lp;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-28 05:10:00 +05:30
|
|
|
if (sb->st_nlink == 1) {
|
2007-12-28 05:11:36 +05:30
|
|
|
return NULL;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
lp = (struct link_name *) xmalloc (sizeof *lp);
|
|
|
|
src_len = strlen (src_orig);
|
|
|
|
dst_len = strlen (dst_orig);
|
|
|
|
name_len = strlen (name);
|
|
|
|
lp->ln_dev = sb->st_dev;
|
|
|
|
lp->ln_ino = sb->st_ino;
|
|
|
|
lp->ln_count = sb->st_nlink;
|
|
|
|
len = name_len - src_len + dst_len + 1;
|
2008-06-14 01:40:53 +05:30
|
|
|
lp->ln_name = (char *) xmalloc (len);
|
2007-10-07 17:15:23 +05:30
|
|
|
snprintf (lp->ln_name, len, "%s%s", dst_orig, name + src_len);
|
2007-10-07 17:14:02 +05:30
|
|
|
lp->ln_next = links;
|
|
|
|
links = lp;
|
|
|
|
|
2007-12-28 05:11:36 +05:30
|
|
|
return NULL;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy_tree - copy files in a directory tree
|
|
|
|
*
|
|
|
|
* copy_tree() walks a directory tree and copies ordinary files
|
|
|
|
* as it goes.
|
|
|
|
*/
|
2008-01-01 20:01:00 +05:30
|
|
|
int copy_tree (const char *src_root, const char *dst_root,
|
|
|
|
long int uid, long int gid)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
int err = 0;
|
2008-05-26 05:15:21 +05:30
|
|
|
bool set_orig = false;
|
2007-10-07 17:15:23 +05:30
|
|
|
struct DIRECT *ent;
|
|
|
|
DIR *dir;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Make certain both directories exist. This routine is called
|
|
|
|
* after the home directory is created, or recursively after the
|
|
|
|
* target is created. It assumes the target directory exists.
|
|
|
|
*/
|
|
|
|
|
2007-12-28 05:33:26 +05:30
|
|
|
if ( (access (src_root, F_OK) != 0)
|
|
|
|
|| (access (dst_root, F_OK) != 0)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the source directory and read each entry. Every file
|
|
|
|
* entry in the directory is copied with the UID and GID set
|
|
|
|
* to the provided values. As an added security feature only
|
|
|
|
* regular files (and directories ...) are copied, and no file
|
|
|
|
* is made set-ID.
|
|
|
|
*/
|
2007-12-28 05:00:36 +05:30
|
|
|
dir = opendir (src_root);
|
2007-12-28 05:10:00 +05:30
|
|
|
if (NULL == dir) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-26 05:15:21 +05:30
|
|
|
if (src_orig == NULL) {
|
2007-10-07 17:14:02 +05:30
|
|
|
src_orig = src_root;
|
|
|
|
dst_orig = dst_root;
|
2008-05-26 05:15:21 +05:30
|
|
|
set_orig = true;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 05:38:16 +05:30
|
|
|
while ((0 == err) && (ent = readdir (dir)) != NULL) {
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Skip the "." and ".." entries
|
|
|
|
*/
|
2007-12-28 05:53:33 +05:30
|
|
|
if ((strcmp (ent->d_name, ".") != 0) &&
|
|
|
|
(strcmp (ent->d_name, "..") != 0)) {
|
2009-05-10 19:19:03 +05:30
|
|
|
char *src_name;
|
|
|
|
char *dst_name;
|
|
|
|
size_t src_len = strlen (ent->d_name) + 2;
|
|
|
|
size_t dst_len = strlen (ent->d_name) + 2;
|
|
|
|
src_len += strlen (src_root);
|
|
|
|
dst_len += strlen (dst_root);
|
|
|
|
|
|
|
|
src_name = (char *) malloc (src_len);
|
|
|
|
dst_name = (char *) malloc (dst_len);
|
|
|
|
|
|
|
|
if ((NULL == src_name) || (NULL == dst_name)) {
|
2007-12-28 05:53:33 +05:30
|
|
|
err = -1;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Build the filename for both the source and
|
|
|
|
* the destination files.
|
|
|
|
*/
|
2009-05-10 19:19:03 +05:30
|
|
|
snprintf (src_name, src_len, "%s/%s",
|
2007-12-28 05:53:33 +05:30
|
|
|
src_root, ent->d_name);
|
2009-05-10 19:19:03 +05:30
|
|
|
snprintf (dst_name, dst_len, "%s/%s",
|
2007-12-28 05:53:33 +05:30
|
|
|
dst_root, ent->d_name);
|
|
|
|
|
|
|
|
err = copy_entry (src_name, dst_name, uid, gid);
|
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
if (NULL != src_name) {
|
|
|
|
free (src_name);
|
|
|
|
}
|
|
|
|
if (NULL != dst_name) {
|
|
|
|
free (dst_name);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2008-05-24 20:49:02 +05:30
|
|
|
(void) closedir (dir);
|
2007-12-28 04:53:51 +05:30
|
|
|
|
|
|
|
if (set_orig) {
|
2008-05-26 05:15:21 +05:30
|
|
|
src_orig = NULL;
|
|
|
|
dst_orig = NULL;
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2009-04-11 21:04:10 +05:30
|
|
|
|
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
/* Reset SELinux to create files with default contexts */
|
|
|
|
setfscreatecon (NULL);
|
|
|
|
#endif
|
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
/* FIXME: with the call to remove_link, we could also check that
|
|
|
|
* no links remain in links.
|
|
|
|
* assert (NULL == links); */
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_entry - copy the entry of a directory
|
|
|
|
*
|
|
|
|
* Copy the entry src to dst.
|
|
|
|
* Depending on the type of entry, this function will forward the
|
|
|
|
* request to copy_dir(), copy_symlink(), copy_hardlink(),
|
|
|
|
* copy_special(), or copy_file().
|
|
|
|
*
|
|
|
|
* The access and modification time will not be modified.
|
|
|
|
*
|
|
|
|
* The permissions will be set to uid/gid.
|
|
|
|
*
|
|
|
|
* If uid (resp. gid) is equal to -1, the user (resp. group) will
|
|
|
|
* not be modified.
|
|
|
|
*/
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_entry (const char *src, const char *dst,
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct stat sb;
|
|
|
|
struct link_name *lp;
|
|
|
|
struct timeval mt[2];
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
if (LSTAT (src, &sb) == -1) {
|
|
|
|
/* If we cannot stat the file, do not care. */
|
|
|
|
} else {
|
2008-09-07 05:35:38 +05:30
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_ATIM
|
2007-12-26 19:24:23 +05:30
|
|
|
mt[0].tv_sec = sb.st_atim.tv_sec;
|
|
|
|
mt[0].tv_usec = sb.st_atim.tv_nsec / 1000;
|
|
|
|
#else
|
|
|
|
mt[0].tv_sec = sb.st_atime;
|
2008-08-31 00:01:21 +05:30
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_ATIMENSEC
|
|
|
|
mt[0].tv_usec = sb.st_atimensec / 1000;
|
|
|
|
#else
|
|
|
|
mt[0].tv_usec = 0;
|
2008-09-07 05:35:38 +05:30
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_MTIM
|
|
|
|
mt[1].tv_sec = sb.st_mtim.tv_sec;
|
|
|
|
mt[1].tv_usec = sb.st_mtim.tv_nsec / 1000;
|
|
|
|
#else
|
|
|
|
mt[1].tv_sec = sb.st_mtime;
|
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_MTIMENSEC
|
|
|
|
mt[1].tv_usec = sb.st_mtimensec / 1000;
|
|
|
|
#else
|
2008-08-31 00:01:21 +05:30
|
|
|
mt[1].tv_usec = 0;
|
|
|
|
#endif
|
2007-12-26 19:24:23 +05:30
|
|
|
#endif
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (S_ISDIR (sb.st_mode)) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = copy_dir (src, dst, &sb, mt, uid, gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef S_IFLNK
|
|
|
|
/*
|
|
|
|
* Copy any symbolic links
|
|
|
|
*/
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
else if (S_ISLNK (sb.st_mode)) {
|
|
|
|
err = copy_symlink (src, dst, &sb, mt, uid, gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if this is a previously copied link
|
|
|
|
*/
|
|
|
|
|
2007-12-28 05:33:26 +05:30
|
|
|
else if ((lp = check_link (src, &sb)) != NULL) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = copy_hardlink (src, dst, lp);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with FIFOs and special files. The user really
|
|
|
|
* shouldn't have any of these, but it seems like it
|
|
|
|
* would be nice to copy everything ...
|
|
|
|
*/
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
else if (!S_ISREG (sb.st_mode)) {
|
2008-01-06 17:32:24 +05:30
|
|
|
err = copy_special (dst, &sb, mt, uid, gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the new file and copy the contents. The new
|
|
|
|
* file will be owned by the provided UID and GID values.
|
|
|
|
*/
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
else {
|
|
|
|
err = copy_file (src, dst, &sb, mt, uid, gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_dir - copy a directory
|
|
|
|
*
|
|
|
|
* Copy a directory (recursively) from src to dst.
|
|
|
|
*
|
|
|
|
* statp, mt, uid, gid are used to set the access and modification and the
|
|
|
|
* access rights.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_dir (const char *src, const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new target directory, make it owned by
|
|
|
|
* the user and then recursively copy that directory.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef WITH_SELINUX
|
2007-12-28 04:53:51 +05:30
|
|
|
selinux_file_context (dst);
|
2007-10-07 17:16:07 +05:30
|
|
|
#endif
|
2007-12-28 05:33:26 +05:30
|
|
|
if ( (mkdir (dst, statp->st_mode) != 0)
|
|
|
|
|| (chown (dst,
|
2008-01-01 20:01:00 +05:30
|
|
|
(uid == - 1) ? statp->st_uid : (uid_t) uid,
|
|
|
|
(gid == - 1) ? statp->st_gid : (gid_t) gid) != 0)
|
2007-12-28 05:33:26 +05:30
|
|
|
|| (chmod (dst, statp->st_mode) != 0)
|
|
|
|
|| (copy_tree (src, dst, uid, gid) != 0)
|
|
|
|
|| (utimes (dst, mt) != 0)) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = -1;
|
|
|
|
}
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef S_IFLNK
|
2009-05-10 19:19:03 +05:30
|
|
|
/*
|
|
|
|
* readlink_malloc - wrapper for readlink
|
|
|
|
*
|
|
|
|
* return NULL on error.
|
|
|
|
* The return string shall be freed by the caller.
|
|
|
|
*/
|
2009-05-10 23:51:58 +05:30
|
|
|
static char *readlink_malloc (const char *filename)
|
2009-05-10 19:19:03 +05:30
|
|
|
{
|
|
|
|
size_t size = 1024;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ssize_t nchars;
|
|
|
|
char *buffer = (char *) malloc (size);
|
|
|
|
if (NULL == buffer) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nchars = readlink (filename, buffer, size);
|
|
|
|
|
|
|
|
if (nchars < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (size_t) nchars < size) { /* The buffer was large enough */
|
|
|
|
/* readlink does not nul-terminate */
|
|
|
|
buffer[nchars] = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try again with a bigger buffer */
|
|
|
|
free (buffer);
|
|
|
|
size *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_symlink - copy a symlink
|
|
|
|
*
|
|
|
|
* Copy a symlink from src to dst.
|
|
|
|
*
|
|
|
|
* statp, mt, uid, gid are used to set the access and modification and the
|
|
|
|
* access rights.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_symlink (const char *src, const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
2009-05-10 19:19:03 +05:30
|
|
|
char *oldlink;
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
/* copy_tree () must be the entry point */
|
|
|
|
assert (NULL != src_orig);
|
|
|
|
assert (NULL != dst_orig);
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
/*
|
|
|
|
* Get the name of the file which the link points
|
|
|
|
* to. If that name begins with the original
|
|
|
|
* source directory name, that part of the link
|
|
|
|
* name will be replaced with the original
|
|
|
|
* destination directory name.
|
|
|
|
*/
|
|
|
|
|
2009-05-10 19:19:03 +05:30
|
|
|
oldlink = readlink_malloc (src);
|
|
|
|
if (NULL == oldlink) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
|
|
|
|
/* If src was a link to an entry of the src_orig directory itself,
|
|
|
|
* create a link to the corresponding entry in the dst_orig
|
|
|
|
* directory.
|
2009-05-22 15:46:12 +05:30
|
|
|
* FIXME: This may change a relative link to an absolute link
|
2009-05-10 19:19:03 +05:30
|
|
|
*/
|
2008-05-24 20:49:02 +05:30
|
|
|
if (strncmp (oldlink, src_orig, strlen (src_orig)) == 0) {
|
2009-05-10 19:19:03 +05:30
|
|
|
size_t len = strlen (dst_orig) + strlen (oldlink) - strlen (src_orig) + 1;
|
|
|
|
char *dummy = (char *) malloc (len);
|
|
|
|
snprintf (dummy, len, "%s%s",
|
2007-12-28 04:53:51 +05:30
|
|
|
dst_orig,
|
|
|
|
oldlink + strlen (src_orig));
|
2009-05-10 19:19:03 +05:30
|
|
|
free (oldlink);
|
|
|
|
oldlink = dummy;
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
selinux_file_context (dst);
|
|
|
|
#endif
|
2007-12-28 05:33:26 +05:30
|
|
|
if ( (symlink (oldlink, dst) != 0)
|
|
|
|
|| (lchown (dst,
|
2008-01-01 20:01:00 +05:30
|
|
|
(uid == -1) ? statp->st_uid : (uid_t) uid,
|
|
|
|
(gid == -1) ? statp->st_gid : (gid_t) gid) != 0)) {
|
2009-05-10 19:19:03 +05:30
|
|
|
free (oldlink);
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
free (oldlink);
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2008-08-30 23:59:55 +05:30
|
|
|
#ifdef HAVE_LUTIMES
|
2007-12-28 04:53:51 +05:30
|
|
|
/* 2007-10-18: We don't care about
|
|
|
|
* exit status of lutimes because
|
|
|
|
* it returns ENOSYS on many system
|
|
|
|
* - not implemented
|
|
|
|
*/
|
|
|
|
lutimes (dst, mt);
|
2008-08-30 23:59:55 +05:30
|
|
|
#endif
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2009-05-10 19:19:03 +05:30
|
|
|
return 0;
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_hardlink - copy a hardlink
|
|
|
|
*
|
|
|
|
* Copy a hardlink from src to dst.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_hardlink (const char *src, const char *dst,
|
|
|
|
struct link_name *lp)
|
|
|
|
{
|
|
|
|
/* TODO: selinux needed? */
|
|
|
|
|
2007-12-28 05:33:26 +05:30
|
|
|
if (link (lp->ln_name, dst) != 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2009-04-25 05:11:28 +05:30
|
|
|
|
2009-05-22 15:46:12 +05:30
|
|
|
/* FIXME: why is it unlinked? This is a copy, not a move */
|
2007-12-28 05:33:26 +05:30
|
|
|
if (unlink (src) != 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2007-12-28 05:34:46 +05:30
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
/* FIXME: idem, although it may never be used again */
|
2007-12-28 05:53:33 +05:30
|
|
|
/* If the file could be unlinked, decrement the links counter,
|
|
|
|
* and delete the file if it was the last reference */
|
2007-12-28 05:34:46 +05:30
|
|
|
lp->ln_count--;
|
|
|
|
if (lp->ln_count <= 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
remove_link (lp);
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_special - copy a special file
|
|
|
|
*
|
|
|
|
* Copy a special file from src to dst.
|
|
|
|
*
|
|
|
|
* statp, mt, uid, gid are used to set the access and modification and the
|
|
|
|
* access rights.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2008-01-06 17:32:24 +05:30
|
|
|
static int copy_special (const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
selinux_file_context (dst);
|
|
|
|
#endif
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2007-12-28 05:33:26 +05:30
|
|
|
if ( (mknod (dst, statp->st_mode & ~07777, statp->st_rdev) != 0)
|
|
|
|
|| (chown (dst,
|
2008-01-01 20:01:00 +05:30
|
|
|
(uid == -1) ? statp->st_uid : (uid_t) uid,
|
|
|
|
(gid == -1) ? statp->st_gid : (gid_t) gid) != 0)
|
2007-12-28 05:33:26 +05:30
|
|
|
|| (chmod (dst, statp->st_mode & 07777) != 0)
|
|
|
|
|| (utimes (dst, mt) != 0)) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_file - copy a file
|
|
|
|
*
|
|
|
|
* Copy a file from src to dst.
|
|
|
|
*
|
|
|
|
* statp, mt, uid, gid are used to set the access and modification and the
|
|
|
|
* access rights.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2007-12-28 04:53:51 +05:30
|
|
|
static int copy_file (const char *src, const char *dst,
|
2008-06-14 01:40:53 +05:30
|
|
|
const struct stat *statp, const struct timeval mt[],
|
2008-01-01 20:01:00 +05:30
|
|
|
long int uid, long int gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
int ifd;
|
|
|
|
int ofd;
|
|
|
|
char buf[1024];
|
2008-06-14 01:40:53 +05:30
|
|
|
ssize_t cnt;
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2007-12-28 05:00:36 +05:30
|
|
|
ifd = open (src, O_RDONLY);
|
|
|
|
if (ifd < 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
selinux_file_context (dst);
|
|
|
|
#endif
|
2009-04-25 05:11:28 +05:30
|
|
|
ofd = open (dst, O_WRONLY | O_CREAT | O_TRUNC, statp->st_mode & 07777);
|
2007-12-28 05:33:26 +05:30
|
|
|
if ( (ofd < 0)
|
2009-04-25 05:11:28 +05:30
|
|
|
|| (fchown (ofd,
|
|
|
|
(uid == -1) ? statp->st_uid : (uid_t) uid,
|
|
|
|
(gid == -1) ? statp->st_gid : (gid_t) gid) != 0)
|
|
|
|
|| (fchmod (ofd, statp->st_mode & 07777) != 0)) {
|
2008-05-24 20:49:02 +05:30
|
|
|
(void) close (ifd);
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
while ((cnt = read (ifd, buf, sizeof buf)) > 0) {
|
2008-06-14 01:40:53 +05:30
|
|
|
if (write (ofd, buf, (size_t)cnt) != cnt) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
2007-12-26 19:24:23 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2008-05-24 20:49:02 +05:30
|
|
|
(void) close (ifd);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-08-30 23:59:55 +05:30
|
|
|
#ifdef HAVE_FUTIMES
|
2007-12-28 04:53:51 +05:30
|
|
|
if (futimes (ofd, mt) != 0) {
|
|
|
|
return -1;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2008-08-30 23:59:55 +05:30
|
|
|
#endif
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
if (close (ofd) != 0) {
|
|
|
|
return -1;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2008-09-07 06:21:17 +05:30
|
|
|
#ifndef HAVE_FUTIMES
|
|
|
|
if (utimes(dst, mt) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
return err;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-05-24 21:05:15 +05:30
|
|
|
* remove_tree - delete a directory tree
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
|
|
|
* remove_tree() walks a directory tree and deletes all the files
|
|
|
|
* and directories.
|
2008-05-24 21:05:15 +05:30
|
|
|
* At the end, it deletes the root directory itself.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
int remove_tree (const char *root)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2009-05-10 19:19:03 +05:30
|
|
|
char *new_name = NULL;
|
2007-10-07 17:15:23 +05:30
|
|
|
int err = 0;
|
|
|
|
struct DIRECT *ent;
|
|
|
|
struct stat sb;
|
|
|
|
DIR *dir;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the source directory and read each entry. Every file
|
|
|
|
* entry in the directory is copied with the UID and GID set
|
|
|
|
* to the provided values. As an added security feature only
|
|
|
|
* regular files (and directories ...) are copied, and no file
|
|
|
|
* is made set-ID.
|
|
|
|
*/
|
2007-12-28 05:00:36 +05:30
|
|
|
dir = opendir (root);
|
2007-12-28 05:10:00 +05:30
|
|
|
if (NULL == dir) {
|
2007-10-07 17:17:45 +05:30
|
|
|
return -1;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
while ((ent = readdir (dir))) {
|
2009-05-10 19:19:03 +05:30
|
|
|
size_t new_len = strlen (root) + strlen (ent->d_name) + 2;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip the "." and ".." entries
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strcmp (ent->d_name, ".") == 0 ||
|
2007-12-28 05:10:00 +05:30
|
|
|
strcmp (ent->d_name, "..") == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Make the filename for the current entry.
|
|
|
|
*/
|
|
|
|
|
2009-05-10 19:19:03 +05:30
|
|
|
if (NULL != new_name) {
|
|
|
|
free (new_name);
|
|
|
|
}
|
|
|
|
new_name = (char *) malloc (new_len);
|
|
|
|
if (NULL == new_name) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = -1;
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
snprintf (new_name, new_len, "%s/%s", root, ent->d_name);
|
2007-12-28 05:10:00 +05:30
|
|
|
if (LSTAT (new_name, &sb) == -1) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (S_ISDIR (sb.st_mode)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Recursively delete this directory.
|
|
|
|
*/
|
2008-05-24 20:49:02 +05:30
|
|
|
if (remove_tree (new_name) != 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = -1;
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
}
|
2008-05-24 20:49:02 +05:30
|
|
|
} else {
|
2008-05-24 21:05:15 +05:30
|
|
|
/*
|
|
|
|
* Delete the file.
|
|
|
|
*/
|
2008-05-24 20:49:02 +05:30
|
|
|
if (unlink (new_name) != 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = -1;
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
if (NULL != new_name) {
|
|
|
|
free (new_name);
|
|
|
|
}
|
2008-05-24 20:49:02 +05:30
|
|
|
(void) closedir (dir);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-24 21:05:15 +05:30
|
|
|
if (0 == err) {
|
|
|
|
if (rmdir (root) != 0) {
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
return err;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 05:10:00 +05:30
|
|
|
|