Sync with the GNU C library.
[gnulib.git] / lib / euidaccess.c
1 /* euidaccess -- check if effective user id can access file
2    Copyright (C) 1990, 1991, 1995, 1998, 2000 Free Software Foundation, Inc.
3
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB.  If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place -
19 Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* Written by David MacKenzie and Torbjorn Granlund.
22    Adapted for GNU C library by Roland McGrath.  */
23
24 #if HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #ifdef S_IEXEC
32 # ifndef S_IXUSR
33 #  define S_IXUSR S_IEXEC
34 # endif
35 # ifndef S_IXGRP
36 #  define S_IXGRP (S_IEXEC >> 3)
37 # endif
38 # ifndef S_IXOTH
39 #  define S_IXOTH (S_IEXEC >> 6)
40 # endif
41 #endif /* S_IEXEC */
42
43 #if defined (HAVE_UNISTD_H) || defined (_LIBC)
44 # include <unistd.h>
45 #endif
46
47 #ifdef _POSIX_VERSION
48 # include <limits.h>
49 # if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
50 #  undef NGROUPS_MAX
51 #  define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
52 # endif /* NGROUPS_MAX */
53
54 #else /* not _POSIX_VERSION */
55 uid_t getuid ();
56 gid_t getgid ();
57 uid_t geteuid ();
58 gid_t getegid ();
59 # include <sys/param.h>
60 # if !defined(NGROUPS_MAX) && defined(NGROUPS)
61 #  define NGROUPS_MAX NGROUPS
62 # endif /* not NGROUPS_MAX and NGROUPS */
63 #endif /* not POSIX_VERSION */
64
65 #include <errno.h>
66 #ifndef errno
67 extern int errno;
68 #endif
69 #ifndef __set_errno
70 # define __set_errno(val) errno = (val)
71 #endif
72
73 #if defined(EACCES) && !defined(EACCESS)
74 # define EACCESS EACCES
75 #endif
76
77 #ifndef F_OK
78 # define F_OK 0
79 # define X_OK 1
80 # define W_OK 2
81 # define R_OK 4
82 #endif
83
84 #if !defined (S_IROTH) && defined (R_OK)
85 # define S_IROTH R_OK
86 #endif
87
88 #if !defined (S_IWOTH) && defined (W_OK)
89 # define S_IWOTH W_OK
90 #endif
91
92 #if !defined (S_IXOTH) && defined (X_OK)
93 # define S_IXOTH X_OK
94 #endif
95
96 #ifdef _LIBC
97
98 # define group_member __group_member
99 # define euidaccess __euidaccess
100
101 #else
102
103 /* The user's real user id. */
104 static uid_t uid;
105
106 /* The user's real group id. */
107 static gid_t gid;
108
109 # if HAVE_GETGROUPS
110 int group_member ();
111 # else
112 #  define group_member(gid)     0
113 # endif
114
115 #endif
116
117 /* The user's effective user id. */
118 static uid_t euid;
119
120 /* The user's effective group id. */
121 static gid_t egid;
122
123 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
124 static int have_ids;
125
126
127 /* Return 0 if the user has permission of type MODE on file PATH;
128    otherwise, return -1 and set `errno' to EACCESS.
129    Like access, except that it uses the effective user and group
130    id's instead of the real ones, and it does not check for read-only
131    filesystem, text busy, etc. */
132
133 int
134 euidaccess (const char *path, int mode)
135 {
136   struct stat stats;
137   int granted;
138
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)
145     {
146       have_ids = 1;
147       uid = getuid ();
148       gid = getgid ();
149       euid = geteuid ();
150       egid = getegid ();
151     }
152
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
157
158   if (stat (path, &stats))
159     return -1;
160
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
165
166   if (mode == F_OK)
167     return 0;                   /* The file exists. */
168
169 #ifdef  _LIBC
170   /* Now we need the IDs.  */
171   if (have_ids == 0)
172     {
173       have_ids = 1;
174       euid = __geteuid ();
175       egid = __getegid ();
176     }
177 #endif
178
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;
184
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;
195 }
196 #undef euidaccess
197 #ifdef weak_alias
198 weak_alias (__euidaccess, euidaccess)
199 #endif
200 \f
201 #ifdef TEST
202 # include <stdio.h>
203 # include <errno.h>
204 # include "error.h"
205
206 char *program_name;
207
208 int
209 main (argc, argv)
210      int argc;
211      char **argv;
212 {
213   char *file;
214   int mode;
215   int err;
216
217   program_name = argv[0];
218   if (argc < 3)
219     abort ();
220   file = argv[1];
221   mode = atoi (argv[2]);
222
223   err = euidaccess (file, mode);
224   printf ("%d\n", err);
225   if (err != 0)
226     error (0, errno, "%s", file);
227   exit (0);
228 }
229 #endif