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