1 /* Check if effective user id can access file
2 Copyright (C) 1990-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
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
45 #ifndef _POSIX_VERSION
50 #endif /* not POSIX_VERSION */
57 # define __set_errno(val) errno = (val)
60 #if defined EACCES && !defined EACCESS
61 # define EACCESS EACCES
71 #if !defined S_IROTH && defined R_OK
74 #if !defined S_IWOTH && defined W_OK
77 #if !defined S_IXOTH && defined X_OK
84 # define group_member __group_member
85 # define euidaccess __euidaccess
89 /* The user's real user id. */
92 /* The user's real group id. */
95 /* The user's effective user id. */
98 /* The user's effective group id. */
101 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
104 # ifdef HAVE_GETGROUPS
107 # define group_member(gid) 0
113 /* Return 0 if the user has permission of type MODE on file PATH;
114 otherwise, return -1 and set `errno' to EACCESS.
115 Like access, except that it uses the effective user and group
116 id's instead of the real ones, and it does not check for read-only
117 filesystem, text busy, etc. */
120 euidaccess (const char *path
, int mode
)
122 struct __stat64_t64 stats
;
138 if (uid
== euid
&& gid
== egid
)
139 /* If we are not set-uid or set-gid, access does the same. */
140 return access (path
, mode
);
143 if (__stat64_time64 (path
, &stats
))
146 mode
&= (X_OK
| W_OK
| R_OK
); /* Clear any bogus bits. */
147 #if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH
148 ?error Oops
, portability assumptions incorrect
.
152 return 0; /* The file exists. */
155 /* Now we need the IDs. */
159 if (__getuid () == euid
&& __getgid () == egid
)
160 /* If we are not set-uid or set-gid, access does the same. */
161 return __access (path
, mode
);
164 /* The super-user can read and write any file, and execute any file
165 that anyone can execute. */
166 if (euid
== 0 && ((mode
& X_OK
) == 0
167 || (stats
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))))
170 if (euid
== stats
.st_uid
)
171 granted
= (unsigned int) (stats
.st_mode
& (mode
<< 6)) >> 6;
172 else if (egid
== stats
.st_gid
|| group_member (stats
.st_gid
))
173 granted
= (unsigned int) (stats
.st_mode
& (mode
<< 3)) >> 3;
175 granted
= (stats
.st_mode
& mode
);
176 /* XXX Add support for ACLs. */
179 __set_errno (EACCESS
);
185 weak_alias (__euidaccess
, euidaccess
)
186 weak_alias (__euidaccess
, eaccess
)
197 main (int argc
, char **argv
)
203 program_name
= argv
[0];
207 mode
= atoi (argv
[2]);
209 err
= euidaccess (file
, mode
);
210 printf ("%d\n", err
);
212 error (0, errno
, "%s", file
);