1 /* euidaccess -- check if effective user id can access file
3 Copyright (C) 1990, 1991, 1995, 1998, 2000, 2003, 2004, 2005 Free
4 Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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>
43 # define __set_errno(val) errno = (val)
46 #if defined EACCES && !defined EACCESS
47 # define EACCESS EACCES
60 # define access __access
61 # define getuid __getuid
62 # define getgid __getgid
63 # define geteuid __geteuid
64 # define getegid __getegid
65 # define group_member __group_member
66 # define euidaccess __euidaccess
72 # include "group-member.h"
73 # include "stat-macros.h"
77 /* Return 0 if the user has permission of type MODE on FILE;
78 otherwise, return -1 and set `errno'.
79 Like access, except that it uses the effective user and group
80 id's instead of the real ones, and it does not always check for read-only
81 file system, text busy, etc. */
84 euidaccess (const char *file, int mode)
86 #if defined EFF_ONLY_OK
87 return access (file, mode | EFF_ONLY_OK);
88 #elif defined ACC_SELF
89 return accessx (file, mode, ACC_SELF);
91 return eaccess (file, mode);
94 uid_t uid = getuid ();
95 gid_t gid = getgid ();
96 uid_t euid = geteuid ();
97 gid_t egid = getegid ();
100 # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
102 /* Define PREFER_NONREENTRANT_EUIDACCESS if you prefer euidaccess to
103 return the correct result even if this would make it
104 nonreentrant. Define this only if your entire application is
105 safe even if the uid or gid might temporarily change. If your
106 application uses signal handlers or threads it is probably not
110 return stat (file, &stats);
117 setreuid (euid, uid);
119 setregid (egid, gid);
121 result = access (file, mode);
126 setreuid (uid, euid);
128 setregid (gid, egid);
136 /* The following code assumes the traditional Unix model, and is not
137 correct on systems that have ACLs or the like. However, it's
138 better than nothing, and it is reentrant. */
140 unsigned int granted;
141 if (uid == euid && gid == egid)
142 /* If we are not set-uid or set-gid, access does the same. */
143 return access (file, mode);
145 if (stat (file, &stats) != 0)
148 /* The super-user can read and write any file, and execute any file
149 that anyone can execute. */
150 if (euid == 0 && ((mode & X_OK) == 0
151 || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
154 /* Convert the mode to traditional form, clearing any bogus bits. */
155 if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0)
158 mode = ((mode & R_OK ? 4 : 0)
159 + (mode & W_OK ? 2 : 0)
160 + (mode & X_OK ? 1 : 0));
163 return 0; /* The file exists. */
165 /* Convert the file's permission bits to traditional form. */
166 if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6)
167 && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3)
168 && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0))
169 granted = stats.st_mode;
171 granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0)
172 + (stats.st_mode & S_IWUSR ? 2 << 6 : 0)
173 + (stats.st_mode & S_IXUSR ? 1 << 6 : 0)
174 + (stats.st_mode & S_IRGRP ? 4 << 3 : 0)
175 + (stats.st_mode & S_IWGRP ? 2 << 3 : 0)
176 + (stats.st_mode & S_IXGRP ? 1 << 3 : 0)
177 + (stats.st_mode & S_IROTH ? 4 << 0 : 0)
178 + (stats.st_mode & S_IWOTH ? 2 << 0 : 0)
179 + (stats.st_mode & S_IXOTH ? 1 << 0 : 0));
181 if (euid == stats.st_uid)
183 else if (egid == stats.st_gid || group_member (stats.st_gid))
186 if ((mode & ~granted) == 0)
188 __set_errno (EACCESS);
196 weak_alias (__euidaccess, euidaccess)
207 main (int argc, char **argv)
213 program_name = argv[0];
217 mode = atoi (argv[2]);
219 err = euidaccess (file, mode);
220 printf ("%d\n", err);
222 error (0, errno, "%s", file);