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