(_dl_map_objet_deps): Call _dl_catch_error correctly.
[glibc.git] / sysdeps / posix / euidaccess.c
blobed595821547aacab025b44bc4c9cf2a0af2e9f65
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. */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 #ifdef S_IEXEC
31 #ifndef S_IXUSR
32 #define S_IXUSR S_IEXEC
33 #endif
34 #ifndef S_IXGRP
35 #define S_IXGRP (S_IEXEC >> 3)
36 #endif
37 #ifndef S_IXOTH
38 #define S_IXOTH (S_IEXEC >> 6)
39 #endif
40 #endif /* S_IEXEC */
42 #if defined (HAVE_UNISTD_H) || defined (_LIBC)
43 #include <unistd.h>
44 #endif
46 #ifdef _POSIX_VERSION
47 #include <limits.h>
48 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
49 #undef NGROUPS_MAX
50 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
51 #endif /* NGROUPS_MAX */
53 #else /* not _POSIX_VERSION */
54 uid_t getuid ();
55 gid_t getgid ();
56 uid_t geteuid ();
57 gid_t getegid ();
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 */
64 #include <errno.h>
65 #ifndef errno
66 extern int errno;
67 #endif
68 #ifndef __set_errno
69 #define __set_errno(val) errno = (val)
70 #endif
72 #if defined(EACCES) && !defined(EACCESS)
73 #define EACCESS EACCES
74 #endif
76 #ifndef F_OK
77 #define F_OK 0
78 #define X_OK 1
79 #define W_OK 2
80 #define R_OK 4
81 #endif
83 #if !defined (S_IROTH) && defined (R_OK)
84 # define S_IROTH R_OK
85 #endif
86 #if !defined (S_IWOTH) && defined (W_OK)
87 # define S_IWOTH W_OK
88 #endif
89 #if !defined (S_IXOTH) && defined (X_OK)
90 # define S_IXOTH X_OK
91 #endif
94 #ifdef _LIBC
96 #define group_member __group_member
97 #define euidaccess __euidaccess
99 #else
101 /* The user's real user id. */
102 static uid_t uid;
104 /* The user's real group id. */
105 static gid_t gid;
107 #ifdef HAVE_GETGROUPS
108 int group_member ();
109 #else
110 #define group_member(gid) 0
111 #endif
113 #endif
115 /* The user's effective user id. */
116 static uid_t euid;
118 /* The user's effective group id. */
119 static gid_t egid;
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)
133 const char *path;
134 int mode;
136 struct stat stats;
137 int granted;
139 #ifdef _LIBC
140 if (! __libc_enable_secure)
141 /* If we are not set-uid or set-gid, access does the same. */
142 return access (path, mode);
143 #else
144 if (have_ids == 0)
146 have_ids = 1;
147 uid = getuid ();
148 gid = getgid ();
149 euid = geteuid ();
150 egid = getegid ();
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);
156 #endif
158 if (stat (path, &stats))
159 return -1;
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.
164 #endif
166 if (mode == F_OK)
167 return 0; /* The file exists. */
169 #ifdef _LIBC
170 /* Now we need the IDs. */
171 if (have_ids == 0)
173 have_ids = 1;
174 euid = geteuid ();
175 egid = getegid ();
177 #endif
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))))
183 return 0;
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;
189 else
190 granted = (stats.st_mode & mode);
191 if (granted == mode)
192 return 0;
193 __set_errno (EACCESS);
194 return -1;
196 #undef euidaccess
197 #ifdef weak_alias
198 weak_alias (__euidaccess, euidaccess)
199 #endif
201 #ifdef TEST
202 #include <stdio.h>
203 #include <errno.h>
204 #include "error.h"
206 char *program_name;
209 main (argc, argv)
210 int argc;
211 char **argv;
213 char *file;
214 int mode;
215 int err;
217 program_name = argv[0];
218 if (argc < 3)
219 abort ();
220 file = argv[1];
221 mode = atoi (argv[2]);
223 err = euidaccess (file, mode);
224 printf ("%d\n", err);
225 if (err != 0)
226 error (0, errno, "%s", file);
227 exit (0);
229 #endif