1 /* Check if effective user id can access file
2 Copyright (C) 1990, 1991, 1995, 1996, 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 /* Written by David MacKenzie and Torbjorn Granlund.
21 Adapted for GNU C library by Roland McGrath. */
27 #include <sys/types.h>
32 #define S_IXUSR S_IEXEC
35 #define S_IXGRP (S_IEXEC >> 3)
38 #define S_IXOTH (S_IEXEC >> 6)
42 #if defined (HAVE_UNISTD_H) || defined (_LIBC)
48 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
50 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
51 #endif /* NGROUPS_MAX */
53 #else /* not _POSIX_VERSION */
58 #include <sys/param.h>
59 #if !defined(NGROUPS_MAX) && defined(NGROUPS)
60 #define NGROUPS_MAX NGROUPS
61 #endif /* not NGROUPS_MAX and NGROUPS */
62 #endif /* not POSIX_VERSION */
69 #define __set_errno(val) errno = (val)
72 #if defined(EACCES) && !defined(EACCESS)
73 #define EACCESS EACCES
83 #if !defined (S_IROTH) && defined (R_OK)
86 #if !defined (S_IWOTH) && defined (W_OK)
89 #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. */
107 #ifdef HAVE_GETGROUPS
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. */
122 static int have_ids
= 0;
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 (path
, mode
)
140 if (! __libc_enable_secure
)
141 /* If we are not set-uid or set-gid, access does the same. */
142 return access (path
, mode
);
153 if (uid
== euid
&& gid
== egid
)
154 /* If we are not set-uid or set-gid, access does the same. */
155 return access (path
, mode
);
158 if (stat (path
, &stats
))
161 mode
&= (X_OK
| W_OK
| R_OK
); /* Clear any bogus bits. */
162 #if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH
163 ?error Oops
, portability assumptions incorrect
.
167 return 0; /* The file exists. */
170 /* Now we need the IDs. */
179 /* The super-user can read and write any file, and execute any file
180 that anyone can execute. */
181 if (euid
== 0 && ((mode
& X_OK
) == 0
182 || (stats
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))))
185 if (euid
== stats
.st_uid
)
186 granted
= (unsigned) (stats
.st_mode
& (mode
<< 6)) >> 6;
187 else if (egid
== stats
.st_gid
|| group_member (stats
.st_gid
))
188 granted
= (unsigned) (stats
.st_mode
& (mode
<< 3)) >> 3;
190 granted
= (stats
.st_mode
& mode
);
193 __set_errno (EACCESS
);
198 weak_alias (__euidaccess
, euidaccess
)
217 program_name
= argv
[0];
221 mode
= atoi (argv
[2]);
223 err
= euidaccess (file
, mode
);
224 printf ("%d\n", err
);
226 error (0, errno
, "%s", file
);