Blame lib/chown.c

Packit 8f70b4
/* provide consistent interface to chown for systems that don't interpret
Packit 8f70b4
   an ID of -1 as meaning "don't change the corresponding ID".
Packit 8f70b4
Packit 8f70b4
   Copyright (C) 1997, 2004-2007, 2009-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
/* written by Jim Meyering */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
#include <fcntl.h>
Packit 8f70b4
#include <stdbool.h>
Packit 8f70b4
#include <string.h>
Packit 8f70b4
#include <sys/stat.h>
Packit 8f70b4
Packit 8f70b4
#if !HAVE_CHOWN
Packit 8f70b4
Packit 8f70b4
/* Simple stub that always fails with ENOSYS, for mingw.  */
Packit 8f70b4
int
Packit 8f70b4
chown (const char *file _GL_UNUSED, uid_t uid _GL_UNUSED,
Packit 8f70b4
       gid_t gid _GL_UNUSED)
Packit 8f70b4
{
Packit 8f70b4
  errno = ENOSYS;
Packit 8f70b4
  return -1;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#else /* HAVE_CHOWN */
Packit 8f70b4
Packit 8f70b4
/* Below we refer to the system's chown().  */
Packit 8f70b4
# undef chown
Packit 8f70b4
Packit 8f70b4
/* Provide a more-closely POSIX-conforming version of chown on
Packit 8f70b4
   systems with one or both of the following problems:
Packit 8f70b4
   - chown doesn't treat an ID of -1 as meaning
Packit 8f70b4
   "don't change the corresponding ID".
Packit 8f70b4
   - chown doesn't dereference symlinks.  */
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
rpl_chown (const char *file, uid_t uid, gid_t gid)
Packit 8f70b4
{
Packit 8f70b4
  struct stat st;
Packit 8f70b4
  bool stat_valid = false;
Packit 8f70b4
  int result;
Packit 8f70b4
Packit 8f70b4
# if CHOWN_CHANGE_TIME_BUG
Packit 8f70b4
  if (gid != (gid_t) -1 || uid != (uid_t) -1)
Packit 8f70b4
    {
Packit 8f70b4
      if (stat (file, &st))
Packit 8f70b4
        return -1;
Packit 8f70b4
      stat_valid = true;
Packit 8f70b4
    }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
# if CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE
Packit 8f70b4
  if (gid == (gid_t) -1 || uid == (uid_t) -1)
Packit 8f70b4
    {
Packit 8f70b4
      /* Stat file to get id(s) that should remain unchanged.  */
Packit 8f70b4
      if (!stat_valid && stat (file, &st))
Packit 8f70b4
        return -1;
Packit 8f70b4
      if (gid == (gid_t) -1)
Packit 8f70b4
        gid = st.st_gid;
Packit 8f70b4
      if (uid == (uid_t) -1)
Packit 8f70b4
        uid = st.st_uid;
Packit 8f70b4
    }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
# if CHOWN_MODIFIES_SYMLINK
Packit 8f70b4
  {
Packit 8f70b4
    /* Handle the case in which the system-supplied chown function
Packit 8f70b4
       does *not* follow symlinks.  Instead, it changes permissions
Packit 8f70b4
       on the symlink itself.  To work around that, we open the
Packit 8f70b4
       file (but this can fail due to lack of read or write permission) and
Packit 8f70b4
       use fchown on the resulting descriptor.  */
Packit 8f70b4
    int open_flags = O_NONBLOCK | O_NOCTTY;
Packit 8f70b4
    int fd = open (file, O_RDONLY | open_flags);
Packit 8f70b4
    if (0 <= fd
Packit 8f70b4
        || (errno == EACCES
Packit 8f70b4
            && 0 <= (fd = open (file, O_WRONLY | open_flags))))
Packit 8f70b4
      {
Packit 8f70b4
        int saved_errno;
Packit 8f70b4
        bool fchown_socket_failure;
Packit 8f70b4
Packit 8f70b4
        result = fchown (fd, uid, gid);
Packit 8f70b4
        saved_errno = errno;
Packit 8f70b4
Packit 8f70b4
        /* POSIX says fchown can fail with errno == EINVAL on sockets
Packit 8f70b4
           and pipes, so fall back on chown in that case.  */
Packit 8f70b4
        fchown_socket_failure =
Packit 8f70b4
          (result != 0 && saved_errno == EINVAL
Packit 8f70b4
           && fstat (fd, &st) == 0
Packit 8f70b4
           && (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)));
Packit 8f70b4
Packit 8f70b4
        close (fd);
Packit 8f70b4
Packit 8f70b4
        if (! fchown_socket_failure)
Packit 8f70b4
          {
Packit 8f70b4
            errno = saved_errno;
Packit 8f70b4
            return result;
Packit 8f70b4
          }
Packit 8f70b4
      }
Packit 8f70b4
    else if (errno != EACCES)
Packit 8f70b4
      return -1;
Packit 8f70b4
  }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
# if CHOWN_TRAILING_SLASH_BUG
Packit 8f70b4
  if (!stat_valid)
Packit 8f70b4
    {
Packit 8f70b4
      size_t len = strlen (file);
Packit 8f70b4
      if (len && file[len - 1] == '/' && stat (file, &st))
Packit 8f70b4
        return -1;
Packit 8f70b4
    }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
  result = chown (file, uid, gid);
Packit 8f70b4
Packit 8f70b4
# if CHOWN_CHANGE_TIME_BUG
Packit 8f70b4
  if (result == 0 && stat_valid
Packit 8f70b4
      && (uid == st.st_uid || uid == (uid_t) -1)
Packit 8f70b4
      && (gid == st.st_gid || gid == (gid_t) -1))
Packit 8f70b4
    {
Packit 8f70b4
      /* No change in ownership, but at least one argument was not -1,
Packit 8f70b4
         so we are required to update ctime.  Since chown succeeded,
Packit 8f70b4
         we assume that chmod will do likewise.  Fortunately, on all
Packit 8f70b4
         known systems where a 'no-op' chown skips the ctime update, a
Packit 8f70b4
         'no-op' chmod still does the trick.  */
Packit 8f70b4
      result = chmod (file, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO
Packit 8f70b4
                                          | S_ISUID | S_ISGID | S_ISVTX));
Packit 8f70b4
    }
Packit 8f70b4
# endif
Packit 8f70b4
Packit 8f70b4
  return result;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#endif /* HAVE_CHOWN */