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