Use a consistent style for including <config.h>.
[gnulib.git] / lib / euidaccess.c
1 /* euidaccess -- check if effective user id can access file
2
3    Copyright (C) 1990, 1991, 1995, 1998, 2000, 2003, 2004, 2005 Free
4    Software Foundation, Inc.
5
6    This file is part of the GNU C Library.
7
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)
11    any later version.
12
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.
17
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.  */
21
22 /* Written by David MacKenzie and Torbjorn Granlund.
23    Adapted for GNU C library by Roland McGrath.  */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #ifndef _LIBC
30 # include "euidaccess.h"
31 #endif
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #if HAVE_UNISTD_H || defined _LIBC
37 # include <unistd.h>
38 #endif
39
40 #if HAVE_LIBGEN_H
41 # include <libgen.h>
42 #endif
43
44 #include <errno.h>
45 #ifndef __set_errno
46 # define __set_errno(val) errno = (val)
47 #endif
48
49 #if defined EACCES && !defined EACCESS
50 # define EACCESS EACCES
51 #endif
52
53 #ifndef F_OK
54 # define F_OK 0
55 # define X_OK 1
56 # define W_OK 2
57 # define R_OK 4
58 #endif
59
60
61 #ifdef _LIBC
62
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
70 # undef stat
71 # define stat stat64
72
73 #else
74
75 # include "group-member.h"
76 # include "stat-macros.h"
77
78 #endif
79
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.  */
85
86 int
87 euidaccess (const char *file, int mode)
88 {
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);
93 #elif HAVE_EACCESS
94   return eaccess (file, mode);
95 #else
96
97   uid_t uid = getuid ();
98   gid_t gid = getgid ();
99   uid_t euid = geteuid ();
100   gid_t egid = getegid ();
101   struct stat stats;
102
103 # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
104
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
110      safe.  */
111
112   if (mode == F_OK)
113     return stat (file, &stats);
114   else
115     {
116       int result;
117       int saved_errno;
118
119       if (uid != euid)
120         setreuid (euid, uid);
121       if (gid != egid)
122         setregid (egid, gid);
123
124       result = access (file, mode);
125       saved_errno = errno;
126
127       /* Restore them.  */
128       if (uid != euid)
129         setreuid (uid, euid);
130       if (gid != egid)
131         setregid (gid, egid);
132
133       errno = saved_errno;
134       return result;
135     }
136
137 # else
138
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.  */
142
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);
147
148   if (stat (file, &stats) != 0)
149     return -1;
150
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))))
155     return 0;
156
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)
159     mode &= 7;
160   else
161     mode = ((mode & R_OK ? 4 : 0)
162             + (mode & W_OK ? 2 : 0)
163             + (mode & X_OK ? 1 : 0));
164
165   if (mode == 0)
166     return 0;                   /* The file exists.  */
167
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;
173   else
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));
183
184   if (euid == stats.st_uid)
185     granted >>= 6;
186   else if (egid == stats.st_gid || group_member (stats.st_gid))
187     granted >>= 3;
188
189   if ((mode & ~granted) == 0)
190     return 0;
191   __set_errno (EACCESS);
192   return -1;
193
194 # endif
195 #endif
196 }
197 #undef euidaccess
198 #ifdef weak_alias
199 weak_alias (__euidaccess, euidaccess)
200 #endif
201 \f
202 #ifdef TEST
203 # include <error.h>
204 # include <stdio.h>
205 # include <stdlib.h>
206
207 char *program_name;
208
209 int
210 main (int argc, char **argv)
211 {
212   char *file;
213   int mode;
214   int err;
215
216   program_name = argv[0];
217   if (argc < 3)
218     abort ();
219   file = argv[1];
220   mode = atoi (argv[2]);
221
222   err = euidaccess (file, mode);
223   printf ("%d\n", err);
224   if (err != 0)
225     error (0, errno, "%s", file);
226   exit (0);
227 }
228 #endif