1 /* provide consistent interface to chown for systems that don't interpret
2 an ID of -1 as meaning "don't change the corresponding ID".
3
4 Copyright (C) 1997, 2004-2007, 2009-2023 Free Software 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 #if !HAVE_CHOWN
32
33 /* Simple stub that always fails with ENOSYS, for mingw. */
34 int
35 chown (_GL_UNUSED const char *file, _GL_UNUSED uid_t uid,
36 _GL_UNUSED gid_t gid)
37 {
38 errno = ENOSYS;
39 return -1;
40 }
41
42 #else /* HAVE_CHOWN */
43
44 /* Below we refer to the system's chown(). */
45 # undef chown
46
47 /* Provide a more-closely POSIX-conforming version of chown on
48 systems with one or both of the following problems:
49 - chown doesn't treat an ID of -1 as meaning
50 "don't change the corresponding ID".
51 - chown doesn't dereference symlinks. */
52
53 int
54 rpl_chown (const char *file, uid_t uid, gid_t gid)
55 {
56 struct stat st;
57 bool stat_valid = false;
58 int result;
59
60 # if CHOWN_CHANGE_TIME_BUG
61 if (gid != (gid_t) -1 || uid != (uid_t) -1)
62 {
63 if (stat (file, &st))
64 return -1;
65 stat_valid = true;
66 }
67 # endif
68
69 # if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
70 if (gid == (gid_t) -1 || uid == (uid_t) -1)
71 {
72 /* Stat file to get id(s) that should remain unchanged. */
73 if (!stat_valid && stat (file, &st))
74 return -1;
75 if (gid == (gid_t) -1)
76 gid = st.st_gid;
77 if (uid == (uid_t) -1)
78 uid = st.st_uid;
79 }
80 # endif
81
82 # if CHOWN_MODIFIES_SYMLINK
83 {
84 /* Handle the case in which the system-supplied chown function
85 does *not* follow symlinks. Instead, it changes permissions
86 on the symlink itself. To work around that, we open the
87 file (but this can fail due to lack of read or write permission) and
88 use fchown on the resulting descriptor. */
89 int open_flags = O_NONBLOCK | O_NOCTTY | O_CLOEXEC;
90 int fd = open (file, O_RDONLY | open_flags);
91 if (0 <= fd
92 || (errno == EACCES
93 && 0 <= (fd = open (file, O_WRONLY | open_flags))))
94 {
95 int saved_errno;
96 bool fchown_socket_failure;
97
98 result = fchown (fd, uid, gid);
99 saved_errno = errno;
100
101 /* POSIX says fchown can fail with errno == EINVAL on sockets
102 and pipes, so fall back on chown in that case. */
103 fchown_socket_failure =
104 (result != 0 && saved_errno == EINVAL
105 && fstat (fd, &st) == 0
106 && (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)));
107
108 close (fd);
109
110 if (! fchown_socket_failure)
111 {
112 errno = saved_errno;
113 return result;
114 }
115 }
116 else if (errno != EACCES)
117 return -1;
118 }
119 # endif
120
121 # if CHOWN_TRAILING_SLASH_BUG
122 if (!stat_valid)
123 {
124 size_t len = strlen (file);
125 if (len && file[len - 1] == '/' && stat (file, &st))
126 return -1;
127 }
128 # endif
129
130 result = chown (file, uid, gid);
131
132 # if CHOWN_CHANGE_TIME_BUG
133 if (result == 0 && stat_valid
134 && (uid == st.st_uid || uid == (uid_t) -1)
135 && (gid == st.st_gid || gid == (gid_t) -1))
136 {
137 /* No change in ownership, but at least one argument was not -1,
138 so we are required to update ctime. Since chown succeeded,
139 we assume that chmod will do likewise. Fortunately, on all
140 known systems where a 'no-op' chown skips the ctime update, a
141 'no-op' chmod still does the trick. */
142 result = chmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
143 | S_ISUID | S_ISGID | S_ISVTX));
144 }
145 # endif
146
147 return result;
148 }
149
150 #endif /* HAVE_CHOWN */