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