1 /* euidaccess -- check if effective user id can access file
3 Copyright (C) 1990, 1991, 1995, 1998, 2000, 2003 Free Software
6 This file is part of the GNU C Library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Written by David MacKenzie and Torbjorn Granlund.
23 Adapted for GNU C library by Roland McGrath. */
30 # include "euidaccess.h"
33 #include <sys/types.h>
38 # define S_IXUSR S_IEXEC
41 # define S_IXGRP (S_IEXEC >> 3)
44 # define S_IXOTH (S_IEXEC >> 6)
48 #if defined (HAVE_UNISTD_H) || defined (_LIBC)
54 # if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
56 # define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
57 # endif /* NGROUPS_MAX */
59 #else /* not _POSIX_VERSION */
64 # include <sys/param.h>
65 # if !defined(NGROUPS_MAX) && defined(NGROUPS)
66 # define NGROUPS_MAX NGROUPS
67 # endif /* not NGROUPS_MAX and NGROUPS */
68 #endif /* not POSIX_VERSION */
75 # define __set_errno(val) errno = (val)
78 #if defined(EACCES) && !defined(EACCESS)
79 # define EACCESS EACCES
89 #if !defined (S_IROTH) && defined (R_OK)
93 #if !defined (S_IWOTH) && defined (W_OK)
97 #if !defined (S_IXOTH) && defined (X_OK)
103 # define group_member __group_member
104 # define euidaccess __euidaccess
108 /* The user's real user id. */
111 /* The user's real group id. */
117 # define group_member(gid) 0
122 /* The user's effective user id. */
125 /* The user's effective group id. */
128 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
132 /* Return 0 if the user has permission of type MODE on file PATH;
133 otherwise, return -1 and set `errno' to EACCESS.
134 Like access, except that it uses the effective user and group
135 id's instead of the real ones, and it does not check for read-only
136 filesystem, text busy, etc. */
139 euidaccess (const char *path, int mode)
145 if (! __libc_enable_secure)
146 /* If we are not set-uid or set-gid, access does the same. */
147 return __access (path, mode);
158 if (uid == euid && gid == egid)
159 /* If we are not set-uid or set-gid, access does the same. */
160 return access (path, mode);
163 if (stat (path, &stats))
166 mode &= (X_OK | W_OK | R_OK); /* Clear any bogus bits. */
167 #if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH
168 ?error Oops, portability assumptions incorrect.
172 return 0; /* The file exists. */
175 /* Now we need the IDs. */
184 /* The super-user can read and write any file, and execute any file
185 that anyone can execute. */
186 if (euid == 0 && ((mode & X_OK) == 0
187 || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
190 if (euid == stats.st_uid)
191 granted = (unsigned) (stats.st_mode & (mode << 6)) >> 6;
192 else if (egid == stats.st_gid || group_member (stats.st_gid))
193 granted = (unsigned) (stats.st_mode & (mode << 3)) >> 3;
195 granted = (stats.st_mode & mode);
198 __set_errno (EACCESS);
203 weak_alias (__euidaccess, euidaccess)
214 main (int argc, char **argv)
220 program_name = argv[0];
224 mode = atoi (argv[2]);
226 err = euidaccess (file, mode);
227 printf ("%d\n", err);
229 error (0, errno, "%s", file);