1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <platform.h>
#include <files_copy.h>
#include <files_names.h>
#include <files_interfaces.h>
#include <instrumentation.h>
#include <policy.h>
#include <files_lib.h>
#include <file_lib.h>
#include <string_lib.h>
#include <acl_tools.h>
bool CopyRegularFileDiskPerms(const char *source, const char *destination,
int mode)
{
assert(source != NULL);
assert(destination != NULL);
int sd = safe_open(source, O_RDONLY | O_BINARY);
if (sd == -1)
{
Log(LOG_LEVEL_INFO, "Can't copy '%s' (open: %s)",
source, GetErrorStr());
return false;
}
/* unlink() + safe_open(O_CREAT|O_EXCL) to avoid
symlink attacks and races. */
unlink(destination);
int dd = safe_open_create_perms(destination,
O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
mode);
if (dd == -1)
{
Log(LOG_LEVEL_INFO,
"Unable to open destination file while copying '%s' to '%s'"
" (open: %s)", source, destination, GetErrorStr());
close(sd);
return false;
}
/* We need to stat the file to get the block size of the source file */
struct stat statbuf;
if (fstat(sd, &statbuf) == -1)
{
Log(LOG_LEVEL_INFO, "Can't copy '%s' (fstat: %s)",
source, GetErrorStr());
close(sd);
close(dd);
return false;
}
size_t total_bytes_written;
bool last_write_was_hole;
bool ret = FileSparseCopy(sd, source, dd, destination,
ST_BLKSIZE(statbuf),
&total_bytes_written, &last_write_was_hole);
if (!ret)
{
unlink(destination);
close(sd);
close(dd);
return false;
}
bool do_sync = false;
ret = FileSparseClose(dd, destination, do_sync,
total_bytes_written, last_write_was_hole);
if (!ret)
{
unlink(destination);
}
close(sd);
close(dd);
return ret;
}
bool CopyRegularFileDisk(const char *source, const char *destination)
{
assert(source != NULL);
assert(destination != NULL);
bool ok1 = false, ok2 = false; /* initialize before the goto end; */
int sd = safe_open(source, O_RDONLY | O_BINARY);
if (sd == -1)
{
Log(LOG_LEVEL_INFO, "Can't copy '%s' (open: %s)",
source, GetErrorStr());
goto end;
}
/* We need to stat the file to get the right source permissions. */
struct stat statbuf;
if (fstat(sd, &statbuf) == -1)
{
Log(LOG_LEVEL_INFO, "Can't copy '%s' (fstat: %s)",
source, GetErrorStr());
goto end;
}
/* unlink() + safe_open(O_CREAT|O_EXCL) to avoid
symlink attacks and races. */
unlink(destination);
int dd = safe_open_create_perms(destination,
O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
statbuf.st_mode);
if (dd == -1)
{
Log(LOG_LEVEL_INFO,
"Unable to open destination file while copying '%s' to '%s'"
" (open: %s)", source, destination, GetErrorStr());
goto end;
}
size_t total_bytes_written;
bool last_write_was_hole;
ok1 = FileSparseCopy(sd, source, dd, destination,
ST_BLKSIZE(statbuf),
&total_bytes_written, &last_write_was_hole);
bool do_sync = false;
ok2 = FileSparseClose(dd, destination, do_sync,
total_bytes_written, last_write_was_hole);
if (!ok1 || !ok2)
{
unlink(destination);
}
end:
if (sd != -1)
{
close(sd);
}
return ok1 && ok2;
}
bool CopyFilePermissionsDisk(const char *source, const char *destination)
{
struct stat statbuf;
if (stat(source, &statbuf) == -1)
{
Log(LOG_LEVEL_INFO, "Can't copy permissions '%s'. (stat: %s)", source, GetErrorStr());
return false;
}
if (safe_chmod(destination, statbuf.st_mode) != 0)
{
Log(LOG_LEVEL_INFO, "Can't copy permissions '%s'. (chmod: %s)", source, GetErrorStr());
return false;
}
if (safe_chown(destination, statbuf.st_uid, statbuf.st_gid) != 0)
{
Log(LOG_LEVEL_INFO, "Can't copy permissions '%s'. (chown: %s)", source, GetErrorStr());
return false;
}
if (!CopyFileExtendedAttributesDisk(source, destination, NULL))
{
return false;
}
return true;
}
bool CopyFileExtendedAttributesDisk(const char *source, const char *destination, bool *change)
{
#if defined(WITH_XATTR)
// Extended attributes include both POSIX ACLs and SELinux contexts.
ssize_t attr_raw_names_size;
char attr_raw_names[CF_BUFSIZE];
attr_raw_names_size = llistxattr(source, attr_raw_names, sizeof(attr_raw_names));
if (attr_raw_names_size < 0)
{
if (errno == ENOTSUP || errno == ENODATA)
{
if (change != NULL)
{
*change = false;
}
return true;
}
else
{
Log(LOG_LEVEL_ERR, "Can't copy extended attributes from '%s' to '%s'. (llistxattr: %s)",
source, destination, GetErrorStr());
return false;
}
}
int pos;
for (pos = 0; pos < attr_raw_names_size;)
{
const char *current = attr_raw_names + pos;
pos += strlen(current) + 1;
char src_data[CF_BUFSIZE];
int src_datasize = lgetxattr(source, current, src_data, sizeof(src_data));
if (src_datasize < 0)
{
if (errno == ENOTSUP)
{
continue;
}
else
{
Log(LOG_LEVEL_ERR, "Can't copy extended attributes from '%s' to '%s'. (lgetxattr: %s: %s)",
source, destination, GetErrorStr(), current);
return false;
}
}
char dst_data[CF_BUFSIZE];
int dst_datasize = lgetxattr(destination, current, dst_data, sizeof(dst_data));
if ((dst_datasize < 0) && (errno == ENOTSUP))
{
continue;
}
else if ((src_datasize == dst_datasize) &&
(memcmp(src_data, dst_data, src_datasize) == 0))
{
/* The value is the same, no need to overwrite it. */
continue;
}
int ret = lsetxattr(destination, current, src_data, src_datasize, 0);
if (ret < 0)
{
if (errno == ENOTSUP)
{
continue;
}
else
{
Log(LOG_LEVEL_ERR, "Can't copy extended attributes from '%s' to '%s'. (lsetxattr: %s: %s)",
source, destination, GetErrorStr(), current);
return false;
}
}
if (change != NULL)
{
*change = true;
}
}
#else // !WITH_XATTR
// ACLs are included in extended attributes, but fall back to CopyACLs if xattr is not available.
if (!CopyACLs(source, destination, change))
{
return false;
}
#endif
return true;
}
|