Move the euidaccess() declaration to <unistd.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, 2006, 2008
4    Free 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 3 of the License, or
11    (at your option) 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
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* Written by David MacKenzie and Torbjorn Granlund.
22    Adapted for GNU C library by Roland McGrath.  */
23
24 #ifndef _LIBC
25 # include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
32 #if HAVE_LIBGEN_H
33 # include <libgen.h>
34 #endif
35
36 #include <errno.h>
37 #ifndef __set_errno
38 # define __set_errno(val) errno = (val)
39 #endif
40
41 #if defined EACCES && !defined EACCESS
42 # define EACCESS EACCES
43 #endif
44
45 #ifndef F_OK
46 # define F_OK 0
47 # define X_OK 1
48 # define W_OK 2
49 # define R_OK 4
50 #endif
51
52
53 #ifdef _LIBC
54
55 # define access __access
56 # define getuid __getuid
57 # define getgid __getgid
58 # define geteuid __geteuid
59 # define getegid __getegid
60 # define group_member __group_member
61 # define euidaccess __euidaccess
62 # undef stat
63 # define stat stat64
64
65 #else
66
67 # include "group-member.h"
68
69 #endif
70
71 /* Return 0 if the user has permission of type MODE on FILE;
72    otherwise, return -1 and set `errno'.
73    Like access, except that it uses the effective user and group
74    id's instead of the real ones, and it does not always check for read-only
75    file system, text busy, etc.  */
76
77 int
78 euidaccess (const char *file, int mode)
79 {
80 #if defined EFF_ONLY_OK
81   return access (file, mode | EFF_ONLY_OK);
82 #elif defined ACC_SELF
83   return accessx (file, mode, ACC_SELF);
84 #elif HAVE_EACCESS
85   return eaccess (file, mode);
86 #else
87
88   uid_t uid = getuid ();
89   gid_t gid = getgid ();
90   uid_t euid = geteuid ();
91   gid_t egid = getegid ();
92   struct stat stats;
93
94 # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
95
96   /* Define PREFER_NONREENTRANT_EUIDACCESS if you prefer euidaccess to
97      return the correct result even if this would make it
98      nonreentrant.  Define this only if your entire application is
99      safe even if the uid or gid might temporarily change.  If your
100      application uses signal handlers or threads it is probably not
101      safe.  */
102
103   if (mode == F_OK)
104     return stat (file, &stats);
105   else
106     {
107       int result;
108       int saved_errno;
109
110       if (uid != euid)
111         setreuid (euid, uid);
112       if (gid != egid)
113         setregid (egid, gid);
114
115       result = access (file, mode);
116       saved_errno = errno;
117
118       /* Restore them.  */
119       if (uid != euid)
120         setreuid (uid, euid);
121       if (gid != egid)
122         setregid (gid, egid);
123
124       errno = saved_errno;
125       return result;
126     }
127
128 # else
129
130   /* The following code assumes the traditional Unix model, and is not
131      correct on systems that have ACLs or the like.  However, it's
132      better than nothing, and it is reentrant.  */
133
134   unsigned int granted;
135   if (uid == euid && gid == egid)
136     /* If we are not set-uid or set-gid, access does the same.  */
137     return access (file, mode);
138
139   if (stat (file, &stats) != 0)
140     return -1;
141
142   /* The super-user can read and write any file, and execute any file
143      that anyone can execute.  */
144   if (euid == 0 && ((mode & X_OK) == 0
145                     || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
146     return 0;
147
148   /* Convert the mode to traditional form, clearing any bogus bits.  */
149   if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0)
150     mode &= 7;
151   else
152     mode = ((mode & R_OK ? 4 : 0)
153             + (mode & W_OK ? 2 : 0)
154             + (mode & X_OK ? 1 : 0));
155
156   if (mode == 0)
157     return 0;                   /* The file exists.  */
158
159   /* Convert the file's permission bits to traditional form.  */
160   if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6)
161       && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3)
162       && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0))
163     granted = stats.st_mode;
164   else
165     granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0)
166                + (stats.st_mode & S_IWUSR ? 2 << 6 : 0)
167                + (stats.st_mode & S_IXUSR ? 1 << 6 : 0)
168                + (stats.st_mode & S_IRGRP ? 4 << 3 : 0)
169                + (stats.st_mode & S_IWGRP ? 2 << 3 : 0)
170                + (stats.st_mode & S_IXGRP ? 1 << 3 : 0)
171                + (stats.st_mode & S_IROTH ? 4 << 0 : 0)
172                + (stats.st_mode & S_IWOTH ? 2 << 0 : 0)
173                + (stats.st_mode & S_IXOTH ? 1 << 0 : 0));
174
175   if (euid == stats.st_uid)
176     granted >>= 6;
177   else if (egid == stats.st_gid || group_member (stats.st_gid))
178     granted >>= 3;
179
180   if ((mode & ~granted) == 0)
181     return 0;
182   __set_errno (EACCESS);
183   return -1;
184
185 # endif
186 #endif
187 }
188 #undef euidaccess
189 #ifdef weak_alias
190 weak_alias (__euidaccess, euidaccess)
191 #endif
192 \f
193 #ifdef TEST
194 # include <error.h>
195 # include <stdio.h>
196 # include <stdlib.h>
197
198 char *program_name;
199
200 int
201 main (int argc, char **argv)
202 {
203   char *file;
204   int mode;
205   int err;
206
207   program_name = argv[0];
208   if (argc < 3)
209     abort ();
210   file = argv[1];
211   mode = atoi (argv[2]);
212
213   err = euidaccess (file, mode);
214   printf ("%d\n", err);
215   if (err != 0)
216     error (0, errno, "%s", file);
217   exit (0);
218 }
219 #endif