1 /* euidaccess -- check if effective user id can access file
2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie and Torbjorn Granlund. */
24 #include <sys/types.h>
29 #define S_IXUSR S_IEXEC
32 #define S_IXGRP (S_IEXEC >> 3)
35 #define S_IXOTH (S_IEXEC >> 6)
45 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
47 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
48 #endif /* NGROUPS_MAX */
50 #else /* not _POSIX_VERSION */
55 #include <sys/param.h>
56 #if !defined(NGROUPS_MAX) && defined(NGROUPS)
57 #define NGROUPS_MAX NGROUPS
58 #endif /* not NGROUPS_MAX and NGROUPS */
59 #endif /* not POSIX_VERSION */
66 #if defined(EACCES) && !defined(EACCESS)
67 #define EACCESS EACCES
77 /* The user's real user id. */
80 /* The user's real group id. */
83 /* The user's effective user id. */
86 /* The user's effective group id. */
91 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
92 static int have_ids = 0;
94 /* Like euidaccess, except that a pointer to a filled-in stat structure
95 describing the file is provided instead of a filename.
96 Because this function is almost guaranteed to fail on systems that
97 use ACLs, a third argument *PATH may be used. If it is non-NULL,
98 it is assumed to be the name of the file corresponding to STATP.
99 Then, if the user is not running set-uid or set-gid, use access
100 instead of attempting a manual and non-portable comparison. */
103 eaccess_stat (statp, mode, path)
104 const struct stat *statp;
110 mode &= (X_OK | W_OK | R_OK); /* Clear any bogus bits. */
113 return 0; /* The file exists. */
124 if (path && uid == euid && gid == egid)
126 return access (path, mode);
129 /* The super-user can read and write any file, and execute any file
130 that anyone can execute. */
131 if (euid == 0 && ((mode & X_OK) == 0
132 || (statp->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
135 if (euid == statp->st_uid)
136 granted = (unsigned) (statp->st_mode & (mode << 6)) >> 6;
137 else if (egid == statp->st_gid
138 #ifdef HAVE_GETGROUPS
139 || group_member (statp->st_gid)
142 granted = (unsigned) (statp->st_mode & (mode << 3)) >> 3;
144 granted = (statp->st_mode & mode);
151 /* Return 0 if the user has permission of type MODE on file PATH;
152 otherwise, return -1 and set `errno' to EACCESS.
153 Like access, except that it uses the effective user and group
154 id's instead of the real ones, and it does not check for read-only
155 filesystem, text busy, etc. */
158 euidaccess (path, mode)
173 if (uid == euid && gid == egid)
175 return access (path, mode);
178 if (stat (path, &stats))
181 return eaccess_stat (&stats, mode, path);
201 program_name = argv[0];
205 mode = atoi (argv[2]);
207 err = euidaccess (file, mode);
208 printf ("%d\n", err);
210 error (0, errno, "%s", file);