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-2010 Free Software Foundation, Inc.
5
6   This program is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   This program 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 General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://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 <stdbool.h>
29#include <string.h>
30#include <sys/stat.h>
31
32#if !HAVE_CHOWN
33
34/* Simple stub that always fails with ENOSYS, for mingw.  */
35int
36chown (const char *file _GL_UNUSED, uid_t uid _GL_UNUSED,
37       gid_t gid _GL_UNUSED)
38{
39  errno = ENOSYS;
40  return -1;
41}
42
43#else /* HAVE_CHOWN */
44
45/* Below we refer to the system's chown().  */
46# undef chown
47
48/* The results of open() in this file are not used with fchdir,
49   therefore save some unnecessary work in fchdir.c.  */
50# undef open
51# undef close
52
53/* Provide a more-closely POSIX-conforming version of chown on
54   systems with one or both of the following problems:
55   - chown doesn't treat an ID of -1 as meaning
56   `don't change the corresponding ID'.
57   - chown doesn't dereference symlinks.  */
58
59int
60rpl_chown (const char *file, uid_t uid, gid_t gid)
61{
62  struct stat st;
63  bool stat_valid = false;
64  int result;
65
66# if CHOWN_CHANGE_TIME_BUG
67  if (gid != (gid_t) -1 || uid != (uid_t) -1)
68    {
69      if (stat (file, &st))
70        return -1;
71      stat_valid = true;
72    }
73# endif
74
75# if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
76  if (gid == (gid_t) -1 || uid == (uid_t) -1)
77    {
78      /* Stat file to get id(s) that should remain unchanged.  */
79      if (!stat_valid && stat (file, &st))
80        return -1;
81      if (gid == (gid_t) -1)
82        gid = st.st_gid;
83      if (uid == (uid_t) -1)
84        uid = st.st_uid;
85    }
86# endif
87
88# if CHOWN_MODIFIES_SYMLINK
89  {
90    /* Handle the case in which the system-supplied chown function
91       does *not* follow symlinks.  Instead, it changes permissions
92       on the symlink itself.  To work around that, we open the
93       file (but this can fail due to lack of read or write permission) and
94       use fchown on the resulting descriptor.  */
95    int open_flags = O_NONBLOCK | O_NOCTTY;
96    int fd = open (file, O_RDONLY | open_flags);
97    if (0 <= fd
98        || (errno == EACCES
99            && 0 <= (fd = open (file, O_WRONLY | open_flags))))
100      {
101        int saved_errno;
102        bool fchown_socket_failure;
103
104        result = fchown (fd, uid, gid);
105        saved_errno = errno;
106
107        /* POSIX says fchown can fail with errno == EINVAL on sockets
108           and pipes, so fall back on chown in that case.  */
109        fchown_socket_failure =
110          (result != 0 && saved_errno == EINVAL
111           && fstat (fd, &st) == 0
112           && (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)));
113
114        close (fd);
115
116        if (! fchown_socket_failure)
117          {
118            errno = saved_errno;
119            return result;
120          }
121      }
122    else if (errno != EACCES)
123      return -1;
124  }
125# endif
126
127# if CHOWN_TRAILING_SLASH_BUG
128  if (!stat_valid)
129    {
130      size_t len = strlen (file);
131      if (len && file[len - 1] == '/' && stat (file, &st))
132        return -1;
133    }
134# endif
135
136  result = chown (file, uid, gid);
137
138# if CHOWN_CHANGE_TIME_BUG
139  if (result == 0 && stat_valid
140      && (uid == st.st_uid || uid == (uid_t) -1)
141      && (gid == st.st_gid || gid == (gid_t) -1))
142    {
143      /* No change in ownership, but at least one argument was not -1,
144         so we are required to update ctime.  Since chown succeeded,
145         we assume that chmod will do likewise.  Fortunately, on all
146         known systems where a 'no-op' chown skips the ctime update, a
147         'no-op' chmod still does the trick.  */
148      result = chmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
149                                          | S_ISUID | S_ISGID | S_ISVTX));
150    }
151# endif
152
153  return result;
154}
155
156#endif /* HAVE_CHOWN */
157