| 1 |
/* A more POSIX-compliant chown |
| 2 |
|
| 3 |
Copyright (C) 1997, 2004-2007, 2009-2026 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* written by Jim Meyering */ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
/* Specification. */ |
| 23 |
#include <unistd.h> |
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
|
| 30 |
#include "issymlink.h" |
| 31 |
#include "stat-time.h" |
| 32 |
|
| 33 |
#ifndef CHOWN_CHANGE_TIME_BUG |
| 34 |
# define CHOWN_CHANGE_TIME_BUG 0 |
| 35 |
#endif |
| 36 |
#ifndef CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE |
| 37 |
# define CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE 0 |
| 38 |
#endif |
| 39 |
#ifndef CHOWN_MODIFIES_SYMLINK |
| 40 |
# define CHOWN_MODIFIES_SYMLINK 0 |
| 41 |
#endif |
| 42 |
#ifndef CHOWN_TRAILING_SLASH_BUG |
| 43 |
# define CHOWN_TRAILING_SLASH_BUG 0 |
| 44 |
#endif |
| 45 |
|
| 46 |
/* Gnulib target platforms lacking utimensat do not need it, |
| 47 |
because in practice the bug it works around does not occur. */ |
| 48 |
#if !HAVE_UTIMENSAT |
| 49 |
# undef utimensat |
| 50 |
# define utimensat(fd, file, times, flag) \ |
| 51 |
((void) (fd), (void) (file), (void) (times), (void) (flag), \ |
| 52 |
0) |
| 53 |
#endif |
| 54 |
|
| 55 |
#if !HAVE_CHOWN |
| 56 |
|
| 57 |
/* Simple stub that always fails with ENOSYS, for mingw. */ |
| 58 |
int |
| 59 |
chown (_GL_UNUSED const char *file, _GL_UNUSED uid_t owner, |
| 60 |
_GL_UNUSED gid_t group) |
| 61 |
{ |
| 62 |
errno = ENOSYS; |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
#else /* HAVE_CHOWN */ |
| 67 |
|
| 68 |
/* Below we refer to the system's function. */ |
| 69 |
# undef chown |
| 70 |
|
| 71 |
/* Provide a more-closely POSIX-conforming version. */ |
| 72 |
|
| 73 |
int |
| 74 |
rpl_chown (const char *file, uid_t owner, gid_t group) |
| 75 |
{ |
| 76 |
/* In some very old platforms, the system-supplied function |
| 77 |
does not follow symlinks. |
| 78 |
If the file is a symlink, open the file (following symlinks), and |
| 79 |
fchown the resulting descriptor. Although the open might fail |
| 80 |
due to lack of permissions, it's the best we can easily do. */ |
| 81 |
if (CHOWN_MODIFIES_SYMLINK && 0 < issymlink (file)) |
| 82 |
{ |
| 83 |
int open_flags = O_NONBLOCK | O_NOCTTY | O_CLOEXEC; |
| 84 |
int fd = open (file, O_RDONLY | open_flags); |
| 85 |
if (fd < 0 |
| 86 |
&& (errno != EACCES |
| 87 |
|| ((fd = open (file, O_WRONLY | open_flags)) < 0)) |
| 88 |
&& (errno != EACCES || O_EXEC == O_RDONLY |
| 89 |
|| ((fd = open (file, O_EXEC | open_flags)) < 0)) |
| 90 |
&& (errno != EACCES || O_SEARCH == O_RDONLY || O_SEARCH == O_EXEC |
| 91 |
|| ((fd = open (file, O_SEARCH | open_flags)) < 0))) |
| 92 |
return fd; |
| 93 |
|
| 94 |
int r = fchown (fd, owner, group); |
| 95 |
int err = errno; |
| 96 |
close (fd); |
| 97 |
errno = err; |
| 98 |
return r; |
| 99 |
} |
| 100 |
|
| 101 |
struct stat st; |
| 102 |
gid_t no_gid = -1; |
| 103 |
uid_t no_uid = -1; |
| 104 |
bool gid_noop = group == no_gid; |
| 105 |
bool uid_noop = owner == no_uid; |
| 106 |
bool change_time_check = CHOWN_CHANGE_TIME_BUG && !(gid_noop & uid_noop); |
| 107 |
bool negative_one_check = (CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE |
| 108 |
&& (gid_noop | uid_noop)); |
| 109 |
if (change_time_check | negative_one_check |
| 110 |
|| (CHOWN_TRAILING_SLASH_BUG |
| 111 |
&& file[0] && file[strlen (file) - 1] == '/')) |
| 112 |
{ |
| 113 |
int r = stat (file, &st); |
| 114 |
|
| 115 |
/* EOVERFLOW means the file exists, which is all that the |
| 116 |
trailing slash check needs. */ |
| 117 |
if (r < 0 && (change_time_check | negative_one_check |
| 118 |
|| errno != EOVERFLOW)) |
| 119 |
return r; |
| 120 |
} |
| 121 |
|
| 122 |
gid_t uid = (CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE && uid_noop |
| 123 |
? st.st_uid : owner); |
| 124 |
gid_t gid = (CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE && gid_noop |
| 125 |
? st.st_gid : group); |
| 126 |
int result = chown (file, uid, gid); |
| 127 |
|
| 128 |
/* If no change in ownership, but at least one argument was not -1, |
| 129 |
update ctime indirectly via a no-change update to atime and mtime. |
| 130 |
Do not use UTIME_NOW or UTIME_OMIT as they might run into bugs |
| 131 |
on some platforms. Do not communicate any failure to the caller |
| 132 |
as that would be worse than communicating the ownership change. */ |
| 133 |
if (result == 0 && change_time_check |
| 134 |
&& (((uid == st.st_uid) | uid_noop) |
| 135 |
& ((gid == st.st_gid) | gid_noop))) |
| 136 |
{ |
| 137 |
struct timespec times[2]; |
| 138 |
times[0] = get_stat_atime (&st); |
| 139 |
times[1] = get_stat_mtime (&st); |
| 140 |
utimensat (AT_FDCWD, file, times, 0); |
| 141 |
} |
| 142 |
|
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
#endif /* HAVE_CHOWN */ |