1 /* euidaccess -- check if effective user id can access file
2 Copyright (C) 1990, 1991, 1995, 1998, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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)
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.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by David MacKenzie and Torbjorn Granlund.
20 Adapted for GNU C library by Roland McGrath. */
26 #include <sys/types.h>
31 # define S_IXUSR S_IEXEC
34 # define S_IXGRP (S_IEXEC >> 3)
37 # define S_IXOTH (S_IEXEC >> 6)
41 #if defined (HAVE_UNISTD_H) || defined (_LIBC)
47 # if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
49 # define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
50 # endif /* NGROUPS_MAX */
52 #else /* not _POSIX_VERSION */
57 # include <sys/param.h>
58 # if !defined(NGROUPS_MAX) && defined(NGROUPS)
59 # define NGROUPS_MAX NGROUPS
60 # endif /* not NGROUPS_MAX and NGROUPS */
61 #endif /* not POSIX_VERSION */
68 # define __set_errno(val) errno = (val)
71 #if defined(EACCES) && !defined(EACCESS)
72 # define EACCESS EACCES
82 #if !defined (S_IROTH) && defined (R_OK)
86 #if !defined (S_IWOTH) && defined (W_OK)
90 #if !defined (S_IXOTH) && defined (X_OK)
96 # define group_member __group_member
97 # define euidaccess __euidaccess
101 /* The user's real user id. */
104 /* The user's real group id. */
110 # define group_member(gid) 0
115 /* The user's effective user id. */
118 /* The user's effective group id. */
121 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
125 /* Return 0 if the user has permission of type MODE on file PATH;
126 otherwise, return -1 and set `errno' to EACCESS.
127 Like access, except that it uses the effective user and group
128 id's instead of the real ones, and it does not check for read-only
129 filesystem, text busy, etc. */
132 euidaccess (const char *path, int mode)
138 if (! __libc_enable_secure)
139 /* If we are not set-uid or set-gid, access does the same. */
140 return __access (path, mode);
151 if (uid == euid && gid == egid)
152 /* If we are not set-uid or set-gid, access does the same. */
153 return access (path, mode);
156 if (stat (path, &stats))
159 mode &= (X_OK | W_OK | R_OK); /* Clear any bogus bits. */
160 #if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH
161 ?error Oops, portability assumptions incorrect.
165 return 0; /* The file exists. */
168 /* Now we need the IDs. */
177 /* The super-user can read and write any file, and execute any file
178 that anyone can execute. */
179 if (euid == 0 && ((mode & X_OK) == 0
180 || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
183 if (euid == stats.st_uid)
184 granted = (unsigned) (stats.st_mode & (mode << 6)) >> 6;
185 else if (egid == stats.st_gid || group_member (stats.st_gid))
186 granted = (unsigned) (stats.st_mode & (mode << 3)) >> 3;
188 granted = (stats.st_mode & mode);
191 __set_errno (EACCESS);
196 weak_alias (__euidaccess, euidaccess)
215 program_name = argv[0];
219 mode = atoi (argv[2]);
221 err = euidaccess (file, mode);
222 printf ("%d\n", err);
224 error (0, errno, "%s", file);