Define S_IXUSR, S_IXGRP, and S_IXOTH in terms of _IEXEC if they're not
[gnulib.git] / lib / euidaccess.c
1 /* euidaccess -- check if effective user id can access file
2    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Written by David MacKenzie and Torbjorn Granlund. */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #ifdef S_IEXEC
28 #ifndef S_IXUSR
29 #define S_IXUSR S_IEXEC
30 #endif
31 #ifndef S_IXGRP
32 #define S_IXGRP (S_IEXEC >> 3)
33 #endif
34 #ifndef S_IXOTH
35 #define S_IXOTH (S_IEXEC >> 6)
36 #endif
37 #endif /* S_IEXEC */
38
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42
43 #ifdef _POSIX_VERSION
44 #include <limits.h>
45 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
46 #undef NGROUPS_MAX
47 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
48 #endif /* NGROUPS_MAX */
49
50 #else /* not _POSIX_VERSION */
51 uid_t getuid ();
52 gid_t getgid ();
53 uid_t geteuid ();
54 gid_t getegid ();
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 */
60
61 #include <errno.h>
62 #ifndef STDC_HEADERS
63 extern int errno;
64 #endif
65
66 #if defined(EACCES) && !defined(EACCESS)
67 #define EACCESS EACCES
68 #endif
69
70 #ifndef F_OK
71 #define F_OK 0
72 #define X_OK 1
73 #define W_OK 2
74 #define R_OK 4
75 #endif
76
77 #include "safe-stat.h"
78
79 /* The user's real user id. */
80 static uid_t uid;
81
82 /* The user's real group id. */
83 static gid_t gid;
84
85 /* The user's effective user id. */
86 static uid_t euid;
87
88 /* The user's effective group id. */
89 static gid_t egid;
90
91 int group_member ();
92
93 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
94 static int have_ids = 0;
95
96 /* Like euidaccess, except that a pointer to a filled-in stat structure
97    describing the file is provided instead of a filename.
98    Because this function is almost guaranteed to fail on systems that
99    use ACLs, a third argument *PATH may be used.  If it is non-NULL,
100    it is assumed to be the name of the file corresponding to STATP.
101    Then, if the user is not running set-uid or set-gid, use access
102    instead of attempting a manual and non-portable comparison.  */
103
104 int
105 eaccess_stat (statp, mode, path)
106      struct stat *statp;
107      int mode;
108      char *path;
109 {
110   int granted;
111
112   mode &= (X_OK | W_OK | R_OK); /* Clear any bogus bits. */
113
114   if (mode == F_OK)
115     return 0;                   /* The file exists. */
116
117   if (have_ids == 0)
118     {
119       have_ids = 1;
120       uid = getuid ();
121       gid = getgid ();
122       euid = geteuid ();
123       egid = getegid ();
124     }
125
126   if (path && uid == euid && gid == egid)
127     {
128       return access (path, mode);
129     }
130
131   /* The super-user can read and write any file, and execute any file
132      that anyone can execute. */
133   if (euid == 0 && ((mode & X_OK) == 0
134                     || (statp->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
135     return 0;
136
137   if (euid == statp->st_uid)
138     granted = (unsigned) (statp->st_mode & (mode << 6)) >> 6;
139   else if (egid == statp->st_gid
140 #ifdef HAVE_GETGROUPS
141            || group_member (statp->st_gid)
142 #endif
143            )
144     granted = (unsigned) (statp->st_mode & (mode << 3)) >> 3;
145   else
146     granted = (statp->st_mode & mode);
147   if (granted == mode)
148     return 0;
149   errno = EACCESS;
150   return -1;
151 }
152
153 /* Return 0 if the user has permission of type MODE on file PATH;
154    otherwise, return -1 and set `errno' to EACCESS.
155    Like access, except that it uses the effective user and group
156    id's instead of the real ones, and it does not check for read-only
157    filesystem, text busy, etc. */
158
159 int
160 euidaccess (path, mode)
161      char *path;
162      int mode;
163 {
164   struct stat stats;
165
166   if (have_ids == 0)
167     {
168       have_ids = 1;
169       uid = getuid ();
170       gid = getgid ();
171       euid = geteuid ();
172       egid = getegid ();
173     }
174
175   if (uid == euid && gid == egid)
176     {
177       return access (path, mode);
178     }
179
180   if (SAFE_STAT (path, &stats))
181     return -1;
182
183   return eaccess_stat (&stats, mode, path);
184 }
185
186 #ifdef TEST
187 #include <stdio.h>
188 #include <errno.h>
189
190 void error ();
191
192 char *program_name;
193
194 int
195 main (argc, argv)
196      int argc;
197      char **argv;
198 {
199   char *file;
200   int mode;
201   int err;
202
203   program_name = argv[0];
204   if (argc < 3)
205     abort ();
206   file = argv[1];
207   mode = atoi (argv[2]);
208
209   err = euidaccess (file, mode);
210   printf ("%d\n", err);
211   if (err != 0)
212     error (0, errno, "%s", file);
213   exit (0);
214 }
215 #endif