Blame sysdeps/unix/sysv/linux/faccessat.c

Packit 6c4009
/* Test for access to file, relative to open directory.  Linux version.
Packit 6c4009
   Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <alloca.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
faccessat (int fd, const char *file, int mode, int flag)
Packit 6c4009
{
Packit 6c4009
  if (flag & ~(AT_SYMLINK_NOFOLLOW | AT_EACCESS))
Packit 6c4009
    return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL);
Packit 6c4009
Packit 6c4009
  if ((flag == 0 || ((flag & ~AT_EACCESS) == 0 && ! __libc_enable_secure)))
Packit 6c4009
    return INLINE_SYSCALL (faccessat, 3, fd, file, mode);
Packit 6c4009
Packit 6c4009
  struct stat64 stats;
Packit 6c4009
  if (__fxstatat64 (_STAT_VER, fd, file, &stats, flag & AT_SYMLINK_NOFOLLOW))
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  mode &= (X_OK | W_OK | R_OK);	/* Clear any bogus bits. */
Packit 6c4009
#if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH
Packit 6c4009
# error Oops, portability assumptions incorrect.
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (mode == F_OK)
Packit 6c4009
    return 0;			/* The file exists. */
Packit 6c4009
Packit 6c4009
  uid_t uid = (flag & AT_EACCESS) ? __geteuid () : __getuid ();
Packit 6c4009
Packit 6c4009
  /* The super-user can read and write any file, and execute any file
Packit 6c4009
     that anyone can execute. */
Packit 6c4009
  if (uid == 0 && ((mode & X_OK) == 0
Packit 6c4009
		   || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  int granted = (uid == stats.st_uid
Packit 6c4009
		 ? (unsigned int) (stats.st_mode & (mode << 6)) >> 6
Packit 6c4009
		 : (stats.st_gid == ((flag & AT_EACCESS)
Packit 6c4009
				     ? __getegid () : __getgid ())
Packit 6c4009
		    || __group_member (stats.st_gid))
Packit 6c4009
		 ? (unsigned int) (stats.st_mode & (mode << 3)) >> 3
Packit 6c4009
		 : (stats.st_mode & mode));
Packit 6c4009
Packit 6c4009
  if (granted == mode)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  return INLINE_SYSCALL_ERROR_RETURN_VALUE (EACCES);
Packit 6c4009
}