| 1 | /* |
| 2 | Authors: |
| 3 | Pavel Březina <[email protected]> |
| 4 | |
| 5 | Copyright (C) 2018 Red Hat |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <libgen.h> |
| 25 | #include <limits.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | #include "common/common.h" |
| 32 | #include "lib/util/file.h" |
| 33 | |
| 34 | static bool |
| 35 | file_check_type(struct stat *statbuf, |
| 36 | const char *name, |
| 37 | mode_t mode) |
| 38 | { |
| 39 | mode_t exp_type = mode & S_IFMT; |
| 40 | mode_t real_type; |
| 41 | |
| 42 | if (statbuf == NULL) { |
| 43 | ERROR("Internal error: stat cannot be NULL!"); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | real_type = statbuf->st_mode & S_IFMT; |
| 48 | if (exp_type != (real_type)) { |
| 49 | switch (exp_type) { |
| 50 | case S_IFDIR: |
| 51 | ERROR("[%s] is not a directory!", name); |
| 52 | break; |
| 53 | case S_IFREG: |
| 54 | ERROR("[%s] is not a regular file!", name); |
| 55 | break; |
| 56 | case S_IFLNK: |
| 57 | ERROR("[%s] is not a symbolic link!", name); |
| 58 | break; |
| 59 | default: |
| 60 | ERROR("[%s] has wrong type [%.7o], expected [%.7o]!", |
| 61 | name, real_type, exp_type); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | static bool |
| 72 | file_check_mode(struct stat *statbuf, |
| 73 | const char *name, |
| 74 | uid_t uid, |
| 75 | gid_t gid, |
| 76 | mode_t mode) |
| 77 | { |
| 78 | mode_t exp_perm = mode & ALLPERMS; |
| 79 | bool bret; |
| 80 | |
| 81 | bret = file_check_type(statbuf, name, mode); |
| 82 | if (!bret) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (exp_perm != (statbuf->st_mode & ALLPERMS)) { |
| 87 | ERROR("[%s] has wrong mode [%.4o], expected [%.4o]!", |
| 88 | name, statbuf->st_mode & ALLPERMS, exp_perm); |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if (uid != (uid_t)(-1) && statbuf->st_uid != uid) { |
| 93 | ERROR("[%s] has wrong owner [%u], expected [%u]!", |
| 94 | name, statbuf->st_uid, uid); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | if (gid != (gid_t)(-1) && statbuf->st_gid != gid) { |
| 99 | ERROR("[%s] has wrong group [%u], expected [%u]!", |
| 100 | name, statbuf->st_gid, gid); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | static errno_t |
| 108 | file_check_attributes(const char *filepath, |
| 109 | uid_t uid, |
| 110 | gid_t gid, |
| 111 | mode_t mode, |
| 112 | bool *_result) |
| 113 | { |
| 114 | struct stat statbuf; |
| 115 | errno_t ret; |
| 116 | |
| 117 | ret = lstat(filepath, &statbuf); |
| 118 | if (ret == -1) { |
| 119 | ret = errno; |
| 120 | if (ret == ENOENT) { |
| 121 | ERROR("[%s] does not exist!", filepath); |
| 122 | *_result = false; |
| 123 | return EOK; |
| 124 | } |
| 125 | |
| 126 | ERROR("Unable to stat [%s] [%d]: %s", filepath, ret, strerror(ret)); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | *_result = file_check_mode(&statbuf, filepath, uid, gid, mode); |
| 131 | |
| 132 | return EOK; |
| 133 | } |
| 134 | |
| 135 | errno_t |
| 136 | file_is_regular(const char *filepath, |
| 137 | uid_t uid, |
| 138 | gid_t gid, |
| 139 | mode_t access_mode, |
| 140 | bool *_result) |
| 141 | { |
| 142 | return file_check_attributes(filepath, uid, gid, S_IFREG | access_mode, |
| 143 | _result); |
| 144 | } |
| 145 | |
| 146 | errno_t |
| 147 | file_links_to(const char *linkpath, |
| 148 | const char *destpath, |
| 149 | bool *_result) |
| 150 | { |
| 151 | char linkbuf[PATH_MAX + 1]; |
| 152 | ssize_t len; |
| 153 | errno_t ret; |
| 154 | |
| 155 | ret = file_check_attributes(linkpath, (uid_t)-1, (gid_t)-1, |
| 156 | S_IFLNK | ACCESSPERMS, _result); |
| 157 | if (ret != EOK || *_result == false) { |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | len = readlink(linkpath, linkbuf, PATH_MAX + 1); |
| 162 | if (len == -1) { |
| 163 | ret = errno; |
| 164 | ERROR("Unable to read link destination [%s] [%d]: %s", |
| 165 | linkpath, ret, strerror(ret)); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | if (strncmp(linkbuf, destpath, len) != 0) { |
| 170 | ERROR("Link [%s] does not point to [%s]", linkpath, destpath); |
| 171 | *_result = false; |
| 172 | return EOK; |
| 173 | } |
| 174 | |
| 175 | *_result = true; |
| 176 | return EOK; |
| 177 | } |
| 178 | |
| 179 | errno_t |
| 180 | file_does_not_link_to(const char *linkpath, |
| 181 | const char *destpath, |
| 182 | bool error_mode, |
| 183 | bool *_result) |
| 184 | { |
| 185 | char linkbuf[PATH_MAX + 1]; |
| 186 | struct stat statbuf; |
| 187 | ssize_t len; |
| 188 | errno_t ret; |
| 189 | |
| 190 | ret = lstat(linkpath, &statbuf); |
| 191 | if (ret == -1) { |
| 192 | ret = errno; |
| 193 | |
| 194 | if (ret == ENOENT) { |
| 195 | *_result = true; |
| 196 | return EOK; |
| 197 | } |
| 198 | |
| 199 | ERROR("Unable to stat [%s] [%d]: %s", linkpath, ret, strerror(ret)); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | if (!S_ISLNK(statbuf.st_mode)) { |
| 204 | *_result = true; |
| 205 | return EOK; |
| 206 | } |
| 207 | |
| 208 | len = readlink(linkpath, linkbuf, PATH_MAX + 1); |
| 209 | if (len == -1) { |
| 210 | ret = errno; |
| 211 | ERROR("Unable to read link destination [%s] [%d]: %s", |
| 212 | linkpath, ret, strerror(ret)); |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | if (strncmp(linkbuf, destpath, len) == 0) { |
| 217 | if (error_mode) { |
| 218 | ERROR("Link [%s] points to [%s]", linkpath, destpath); |
| 219 | } else { |
| 220 | INFO("Link [%s] points to [%s]", linkpath, destpath); |
| 221 | } |
| 222 | *_result = false; |
| 223 | return EOK; |
| 224 | } |
| 225 | |
| 226 | *_result = true; |
| 227 | return EOK; |
| 228 | } |
| 229 | |
| 230 | errno_t |
| 231 | file_check_access(const char *path, int mode) |
| 232 | { |
| 233 | errno = 0; |
| 234 | if (access(path, mode) == 0) { |
| 235 | return EOK; |
| 236 | } |
| 237 | |
| 238 | /* ENOENT is returned if a file is missing. */ |
| 239 | return errno; |
| 240 | } |
| 241 | |
| 242 | errno_t |
| 243 | file_exists(const char *path) |
| 244 | { |
| 245 | return file_check_access(path, F_OK); |
| 246 | } |
| 247 | |
| 248 | const char * |
| 249 | file_get_basename(const char *filepath) |
| 250 | { |
| 251 | const char *filename; |
| 252 | |
| 253 | if (filepath == NULL) { |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | filename = strrchr(filepath, '/'); |
| 258 | if (filename == NULL) { |
| 259 | /* There is no slash. */ |
| 260 | return filepath; |
| 261 | } |
| 262 | |
| 263 | if (filename[0] == '\0' || filename[1] == '\0') { |
| 264 | /* There is a slash but no file name. */ |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | return filename + 1; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | char * |
| 273 | file_get_parent_directory(const char *filepath) |
| 274 | { |
| 275 | char *copy = NULL; |
| 276 | char *out = NULL; |
| 277 | char *dir; |
| 278 | errno_t ret; |
| 279 | |
| 280 | if (filepath == NULL) { |
| 281 | ERROR("Internal error: filepath cannot be NULL!"); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | copy = strdup(filepath); |
| 286 | if (copy == NULL) { |
| 287 | ret = ENOMEM; |
| 288 | goto done; |
| 289 | } |
| 290 | |
| 291 | /* Function dirname() may modify input argument, |
| 292 | * it may also return pointer to static memory. */ |
| 293 | dir = dirname(copy); |
| 294 | if (dir == NULL) { |
| 295 | ret = ENOTDIR; |
| 296 | goto done; |
| 297 | } |
| 298 | |
| 299 | out = strdup(dir); |
| 300 | if (out == NULL) { |
| 301 | ret = ENOMEM; |
| 302 | goto done; |
| 303 | } |
| 304 | |
| 305 | ret = EOK; |
| 306 | |
| 307 | done: |
| 308 | if (copy != NULL) { |
| 309 | free(copy); |
| 310 | } |
| 311 | |
| 312 | if (ret != EOK) { |
| 313 | ERROR("Unable to get parent directory of [%s] [%d]: %s", |
| 314 | filepath, ret, strerror(ret)); |
| 315 | } |
| 316 | |
| 317 | return out; |
| 318 | } |
| 319 | |
| 320 | errno_t |
| 321 | file_make_path(const char *path, mode_t mode) |
| 322 | { |
| 323 | char *parent; |
| 324 | errno_t ret; |
| 325 | |
| 326 | ret = file_exists(path); |
| 327 | if (ret != ENOENT) { |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | parent = file_get_parent_directory(path); |
| 332 | if (parent != NULL) { |
| 333 | ret = file_make_path(parent, mode); |
| 334 | free(parent); |
| 335 | if (ret != EOK) { |
| 336 | return ret; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | ret = mkdir(path, mode); |
| 341 | if (ret != 0) { |
| 342 | return errno; |
| 343 | } |
| 344 | |
| 345 | return EOK; |
| 346 | |
| 347 | } |
| 348 | |
| 349 | errno_t |
| 350 | file_mktmp_for(const char *path, mode_t mode, char **_tmpfile) |
| 351 | { |
| 352 | mode_t oldmask; |
| 353 | char *tmpfile; |
| 354 | errno_t ret; |
| 355 | int fd; |
| 356 | |
| 357 | oldmask = umask(~mode & ALLPERMS); |
| 358 | |
| 359 | tmpfile = format("%s.XXXXXX", path); |
| 360 | if (tmpfile == NULL) { |
| 361 | ret = ENOMEM; |
| 362 | goto done; |
| 363 | } |
| 364 | |
| 365 | fd = mkstemp(tmpfile); |
| 366 | if (fd == -1) { |
| 367 | ret = errno; |
| 368 | /* To silence static analyzers that assumes that errno can be 0 here. */ |
| 369 | if (ret == EOK) { |
| 370 | ret = EINVAL; |
| 371 | } |
| 372 | goto done; |
| 373 | } |
| 374 | |
| 375 | close(fd); |
| 376 | |
| 377 | *_tmpfile = tmpfile; |
| 378 | |
| 379 | ret = EOK; |
| 380 | |
| 381 | done: |
| 382 | if (ret != EOK) { |
| 383 | free(tmpfile); |
| 384 | } |
| 385 | |
| 386 | umask(oldmask); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static errno_t |
| 392 | file_mktmp_at(const char *path, const char *name, mode_t mode, char **_tmpfile) |
| 393 | { |
| 394 | char *fullpath; |
| 395 | errno_t ret; |
| 396 | |
| 397 | fullpath = format("%s/%s", path, name); |
| 398 | if (fullpath == NULL) { |
| 399 | return ENOMEM; |
| 400 | } |
| 401 | |
| 402 | ret = file_mktmp_for(fullpath, mode, _tmpfile); |
| 403 | free(fullpath); |
| 404 | |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | errno_t |
| 409 | file_mktmp_copy(const char *source, |
| 410 | const char *destdir, |
| 411 | const char *destname, |
| 412 | mode_t dir_mode, |
| 413 | char **_tmpfile) |
| 414 | { |
| 415 | const char *tmpname; |
| 416 | char *tmpfile; |
| 417 | errno_t ret; |
| 418 | |
| 419 | ret = file_make_path(destdir, dir_mode); |
| 420 | if (ret != EOK) { |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | ret = file_mktmp_at(destdir, destname, 0600, &tmpfile); |
| 425 | if (ret != EOK) { |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | tmpname = file_get_basename(tmpfile); |
| 430 | if (tmpname == NULL) { |
| 431 | ret = EINVAL; |
| 432 | goto done; |
| 433 | } |
| 434 | |
| 435 | ret = file_copy(source, destdir, tmpname, dir_mode); |
| 436 | if (ret != EOK) { |
| 437 | goto done; |
| 438 | } |
| 439 | |
| 440 | *_tmpfile = tmpfile; |
| 441 | |
| 442 | ret = EOK; |
| 443 | |
| 444 | done: |
| 445 | if (ret != EOK) { |
| 446 | unlink(tmpfile); |
| 447 | free(tmpfile); |
| 448 | } |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | errno_t |
| 454 | file_copy(const char *source, |
| 455 | const char *destdir, |
| 456 | const char *destname, |
| 457 | mode_t dir_mode) |
| 458 | { |
| 459 | struct stat statbuf; |
| 460 | FILE *fsource = NULL; |
| 461 | FILE *fdest = NULL; |
| 462 | size_t bytes_written; |
| 463 | size_t bytes_read; |
| 464 | mode_t oldmask; |
| 465 | char *destpath; |
| 466 | char buf[32]; |
| 467 | errno_t ret; |
| 468 | |
| 469 | ret = file_make_path(destdir, dir_mode); |
| 470 | if (ret != EOK) { |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | destpath = format("%s/%s", destdir, destname); |
| 475 | if (destpath == NULL) { |
| 476 | return ENOMEM; |
| 477 | } |
| 478 | |
| 479 | /* Temporary umask before we change the owner and permissions. */ |
| 480 | oldmask = umask(0177); |
| 481 | |
| 482 | fsource = fopen(source, "r"); |
| 483 | if (fsource == NULL) { |
| 484 | ret = errno; |
| 485 | goto done; |
| 486 | } |
| 487 | |
| 488 | ret = fstat(fileno(fsource), &statbuf); |
| 489 | if (ret == -1) { |
| 490 | ret = errno; |
| 491 | goto done; |
| 492 | } |
| 493 | |
| 494 | fdest = fopen(destpath, "w"); |
| 495 | if (fdest == NULL) { |
| 496 | ret = errno; |
| 497 | goto done; |
| 498 | } |
| 499 | |
| 500 | do { |
| 501 | bytes_read = fread(buf, sizeof(char), sizeof(buf), fsource); |
| 502 | if (bytes_read != sizeof(buf)) { |
| 503 | if (ferror(fsource) != 0) { |
| 504 | ret = EIO; |
| 505 | goto done; |
| 506 | } |
| 507 | /* eof not error */ |
| 508 | } |
| 509 | |
| 510 | bytes_written = fwrite(buf, sizeof(char), bytes_read, fdest); |
| 511 | if (bytes_written != bytes_read) { |
| 512 | if (ferror(fdest) != 0) { |
| 513 | ret = EIO; |
| 514 | goto done; |
| 515 | } |
| 516 | } |
| 517 | } while (!feof(fsource)); |
| 518 | |
| 519 | /* Restore original owner and mode. Errors here are not fatal, since we |
| 520 | * have the original content already stored and owned by root. */ |
| 521 | ret = fchmod(fileno(fdest), statbuf.st_mode & ALLPERMS); |
| 522 | if (ret != 0) { |
| 523 | ret = errno; |
| 524 | WARN("Unable to chmod file [%s] [%d]: %s", |
| 525 | destpath, ret, strerror(ret)); |
| 526 | } |
| 527 | |
| 528 | ret = fchown(fileno(fdest), statbuf.st_uid, statbuf.st_gid); |
| 529 | if (ret != 0) { |
| 530 | ret = errno; |
| 531 | WARN("Unable to chown file [%s] [%d]: %s", |
| 532 | destpath, ret, strerror(ret)); |
| 533 | } |
| 534 | |
| 535 | ret = EOK; |
| 536 | |
| 537 | done: |
| 538 | free(destpath); |
| 539 | umask(oldmask); |
| 540 | if (fsource != NULL) { |
| 541 | fclose(fsource); |
| 542 | } |
| 543 | |
| 544 | if (fdest != NULL) { |
| 545 | fclose(fdest); |
| 546 | } |
| 547 | |
| 548 | return ret; |
| 549 | } |