BeOS portability.
[gnulib.git] / lib / dirchownmod.c
1 /* Change the ownership and mode bits of a directory.
2
3    Copyright (C) 2006 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "dirchownmod.h"
26
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #include "lchmod.h"
34 #include "stat-macros.h"
35
36 #ifndef O_DIRECTORY
37 # define O_DIRECTORY 0
38 #endif
39 #ifndef O_NOFOLLOW
40 # define O_NOFOLLOW 0
41 #endif
42
43 #ifndef HAVE_FCHMOD
44 # define HAVE_FCHMOD 0
45 # undef fchmod
46 # define fchmod(fd, mode) (-1)
47 #endif
48
49 /* Change the ownership and mode bits of the directory DIR.
50
51    If MKDIR_MODE is not (mode_t) -1, mkdir (DIR, MKDIR_MODE) has just
52    been executed successfully with umask zero, so DIR should be a
53    directory (not a symbolic link).
54
55    First, set the file's owner to OWNER and group to GROUP, but leave
56    the owner alone if OWNER is (uid_t) -1, and similarly for GROUP.
57
58    Then, set the file's mode bits to MODE, except preserve any of the
59    bits that correspond to zero bits in MODE_BITS.  In other words,
60    MODE_BITS is a mask that specifies which of the file's mode bits
61    should be set or cleared.  MODE should be a subset of MODE_BITS,
62    which in turn should be a subset of CHMOD_MODE_BITS.
63
64    This implementation assumes the current umask is zero.
65
66    Return 0 if successful, -1 (setting errno) otherwise.  Unsuccessful
67    calls may do the chown but not the chmod.  */
68
69 int
70 dirchownmod (char const *dir, mode_t mkdir_mode,
71              uid_t owner, gid_t group,
72              mode_t mode, mode_t mode_bits)
73 {
74   struct stat st;
75   int result;
76
77   /* Manipulate DIR via a file descriptor if possible, to avoid some races.  */
78   int open_flags = O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NOFOLLOW | O_NONBLOCK;
79   int fd = open (dir, open_flags);
80
81   /* Fail if the directory is unreadable, the directory previously
82      existed or was created without read permission.  Otherwise, get
83      the file's status.  */
84   if (0 <= fd)
85     result = fstat (fd, &st);
86   else if (errno != EACCES
87            || (mkdir_mode != (mode_t) -1 && mkdir_mode & S_IRUSR))
88     return fd;
89   else
90     result = stat (dir, &st);
91
92   if (result == 0)
93     {
94       mode_t dir_mode = st.st_mode;
95
96       /* Check whether DIR is a directory.  If FD is nonnegative, this
97          check avoids changing the ownership and mode bits of the
98          wrong file in many cases.  This doesn't fix all the race
99          conditions, but it is better than nothing.  */
100       if (! S_ISDIR (dir_mode))
101         {
102           errno = ENOTDIR;
103           result = -1;
104         }
105       else
106         {
107           /* If at least one of the S_IXUGO bits are set, chown might
108              clear the S_ISUID and S_SGID bits.  Keep track of any
109              file mode bits whose values are indeterminate due to this
110              issue.  */
111           mode_t indeterminate = 0;
112
113           /* On some systems, chown clears S_ISUID and S_ISGID, so do
114              chown before chmod.  On older System V hosts, ordinary
115              users can give their files away via chown; don't worry
116              about that here, since users shouldn't do that.  */
117
118           if ((owner != (uid_t) -1 && owner != st.st_uid)
119               || (group != (gid_t) -1 && group != st.st_gid))
120             {
121               result = (0 <= fd
122                         ? fchown (fd, owner, group)
123                         : mkdir_mode != (mode_t) -1
124                         ? lchown (dir, owner, group)
125                         : chown (dir, owner, group));
126
127               /* Either the user cares about an indeterminate bit and
128                  it'll be set properly by chmod below, or the user
129                  doesn't care and it's OK to use the bit's pre-chown
130                  value.  So there's no need to re-stat DIR here.  */
131
132               if (result == 0 && (dir_mode & S_IXUGO))
133                 indeterminate = dir_mode & (S_ISUID | S_ISGID);
134             }
135
136           /* If the file mode bits might not be right, use chmod to
137              change them.  Don't change bits the user doesn't care
138              about.  */
139           if (result == 0 && (((dir_mode ^ mode) | indeterminate) & mode_bits))
140             {
141               mode_t chmod_mode =
142                 mode | (dir_mode & CHMOD_MODE_BITS & ~mode_bits);
143               result = (HAVE_FCHMOD && 0 <= fd
144                         ? fchmod (fd, chmod_mode)
145                         : mkdir_mode != (mode_t) -1
146                         ? lchmod (dir, chmod_mode)
147                         : chmod (dir, chmod_mode));
148             }
149         }
150     }
151
152   if (0 <= fd)
153     {
154       if (result == 0)
155         result = close (fd);
156       else
157         {
158           int e = errno;
159           close (fd);
160           errno = e;
161         }
162     }
163
164   return result;
165 }