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