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>
36 #if HAVE_UNISTD_H || defined _LIBC
46 # define __set_errno(val) errno = (val)
49 #if defined EACCES && !defined EACCESS
50 # define EACCESS EACCES
63 # define access __access
64 # define getuid __getuid
65 # define getgid __getgid
66 # define geteuid __geteuid
67 # define getegid __getegid
68 # define group_member __group_member
69 # define euidaccess __euidaccess
75 # include "group-member.h"
76 # include "stat-macros.h"
80 /* Return 0 if the user has permission of type MODE on FILE;
81 otherwise, return -1 and set `errno'.
82 Like access, except that it uses the effective user and group
83 id's instead of the real ones, and it does not always check for read-only
84 file system, text busy, etc. */
87 euidaccess (const char *file, int mode)
89 #if defined EFF_ONLY_OK
90 return access (file, mode | EFF_ONLY_OK);
91 #elif defined ACC_SELF
92 return accessx (file, mode, ACC_SELF);
94 return eaccess (file, mode);
97 uid_t uid = getuid ();
98 gid_t gid = getgid ();
99 uid_t euid = geteuid ();
100 gid_t egid = getegid ();
103 # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
105 /* Define PREFER_NONREENTRANT_EUIDACCESS if you prefer euidaccess to
106 return the correct result even if this would make it
107 nonreentrant. Define this only if your entire application is
108 safe even if the uid or gid might temporarily change. If your
109 application uses signal handlers or threads it is probably not
113 return stat (file, &stats);
120 setreuid (euid, uid);
122 setregid (egid, gid);
124 result = access (file, mode);
129 setreuid (uid, euid);
131 setregid (gid, egid);
139 /* The following code assumes the traditional Unix model, and is not
140 correct on systems that have ACLs or the like. However, it's
141 better than nothing, and it is reentrant. */
143 unsigned int granted;
144 if (uid == euid && gid == egid)
145 /* If we are not set-uid or set-gid, access does the same. */
146 return access (file, mode);
148 if (stat (file, &stats) != 0)
151 /* The super-user can read and write any file, and execute any file
152 that anyone can execute. */
153 if (euid == 0 && ((mode & X_OK) == 0
154 || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
157 /* Convert the mode to traditional form, clearing any bogus bits. */
158 if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0)
161 mode = ((mode & R_OK ? 4 : 0)
162 + (mode & W_OK ? 2 : 0)
163 + (mode & X_OK ? 1 : 0));
166 return 0; /* The file exists. */
168 /* Convert the file's permission bits to traditional form. */
169 if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6)
170 && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3)
171 && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0))
172 granted = stats.st_mode;
174 granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0)
175 + (stats.st_mode & S_IWUSR ? 2 << 6 : 0)
176 + (stats.st_mode & S_IXUSR ? 1 << 6 : 0)
177 + (stats.st_mode & S_IRGRP ? 4 << 3 : 0)
178 + (stats.st_mode & S_IWGRP ? 2 << 3 : 0)
179 + (stats.st_mode & S_IXGRP ? 1 << 3 : 0)
180 + (stats.st_mode & S_IROTH ? 4 << 0 : 0)
181 + (stats.st_mode & S_IWOTH ? 2 << 0 : 0)
182 + (stats.st_mode & S_IXOTH ? 1 << 0 : 0));
184 if (euid == stats.st_uid)
186 else if (egid == stats.st_gid || group_member (stats.st_gid))
189 if ((mode & ~granted) == 0)
191 __set_errno (EACCESS);
199 weak_alias (__euidaccess, euidaccess)
210 main (int argc, char **argv)
216 program_name = argv[0];
220 mode = atoi (argv[2]);
222 err = euidaccess (file, mode);
223 printf ("%d\n", err);
225 error (0, errno, "%s", file);