openat: don’t close (-1)
[gnulib.git] / lib / dirchownmod.c
blobd5f7b5432abeec6c0e45da102a1c7da0a1eeeddf
1 /* Change the ownership and mode bits of a directory.
3 Copyright (C) 2006-2024 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 #include <config.h>
22 #include "dirchownmod.h"
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
29 #include "stat-macros.h"
31 #ifndef HAVE_FCHMOD
32 # define HAVE_FCHMOD 0
33 # undef fchmod
34 # define fchmod(fd, mode) (-1)
35 #endif
37 #ifndef HAVE_FCHOWN
38 # define HAVE_FCHOWN 0
39 # undef fchown
40 # define fchown(fd, owner, group) (-1)
41 #endif
43 /* Change the ownership and mode bits of a directory. If FD is
44 nonnegative, it should be a file descriptor associated with the
45 directory; close it before returning. DIR is the name of the
46 directory.
48 If MKDIR_MODE is not (mode_t) -1, mkdir (DIR, MKDIR_MODE) has just
49 been executed successfully with umask zero, so DIR should be a
50 directory (not a symbolic link).
52 First, set the file's owner to OWNER and group to GROUP, but leave
53 the owner alone if OWNER is (uid_t) -1, and similarly for GROUP.
55 Then, set the file's mode bits to MODE, except preserve any of the
56 bits that correspond to zero bits in MODE_BITS. In other words,
57 MODE_BITS is a mask that specifies which of the file's mode bits
58 should be set or cleared. MODE should be a subset of MODE_BITS,
59 which in turn should be a subset of CHMOD_MODE_BITS.
61 This implementation assumes the current umask is zero.
63 Return 0 if successful, -1 (setting errno) otherwise. Unsuccessful
64 calls may do the chown but not the chmod. */
66 int
67 dirchownmod (int fd, char const *dir, mode_t mkdir_mode,
68 uid_t owner, gid_t group,
69 mode_t mode, mode_t mode_bits)
71 struct stat st;
72 int result = (fd < 0 ? stat (dir, &st) : fstat (fd, &st));
74 if (result == 0)
76 mode_t dir_mode = st.st_mode;
78 /* Check whether DIR is a directory. If FD is nonnegative, this
79 check avoids changing the ownership and mode bits of the
80 wrong file in many cases. This doesn't fix all the race
81 conditions, but it is better than nothing. */
82 if (! S_ISDIR (dir_mode))
84 errno = ENOTDIR;
85 result = -1;
87 else
89 /* If at least one of the S_IXUGO bits are set, chown might
90 clear the S_ISUID and S_SGID bits. Keep track of any
91 file mode bits whose values are indeterminate due to this
92 issue. */
93 mode_t indeterminate = 0;
95 /* On some systems, chown clears S_ISUID and S_ISGID, so do
96 chown before chmod. On older System V hosts, ordinary
97 users can give their files away via chown; don't worry
98 about that here, since users shouldn't do that. */
100 if ((owner != (uid_t) -1 && owner != st.st_uid)
101 || (group != (gid_t) -1 && group != st.st_gid))
103 result = (HAVE_FCHOWN && 0 <= fd
104 ? fchown (fd, owner, group)
105 : mkdir_mode != (mode_t) -1
106 ? lchown (dir, owner, group)
107 : chown (dir, owner, group));
109 /* Either the user cares about an indeterminate bit and
110 it'll be set properly by chmod below, or the user
111 doesn't care and it's OK to use the bit's pre-chown
112 value. So there's no need to re-stat DIR here. */
114 if (result == 0 && (dir_mode & S_IXUGO))
115 indeterminate = dir_mode & (S_ISUID | S_ISGID);
118 /* If the file mode bits might not be right, use chmod to
119 change them. Don't change bits the user doesn't care
120 about. */
121 if (result == 0 && (((dir_mode ^ mode) | indeterminate) & mode_bits))
123 mode_t chmod_mode =
124 mode | (dir_mode & CHMOD_MODE_BITS & ~mode_bits);
125 result = (HAVE_FCHMOD && 0 <= fd
126 ? fchmod (fd, chmod_mode)
127 : mkdir_mode != (mode_t) -1
128 ? lchmod (dir, chmod_mode)
129 : chmod (dir, chmod_mode));
134 if (0 <= fd)
136 if (result == 0)
137 result = close (fd);
138 else
140 int e = errno;
141 close (fd);
142 errno = e;
146 return result;