Import upstream version 1.28
[debian/tar] / gnu / euidaccess.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* euidaccess -- check if effective user id can access file
4
5    Copyright (C) 1990-1991, 1995, 1998, 2000, 2003-2006, 2008-2014 Free
6    Software Foundation, Inc.
7
8    This file is part of the GNU C Library.
9
10    This program is free software: you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 /* Written by David MacKenzie and Torbjorn Granlund.
24    Adapted for GNU C library by Roland McGrath.  */
25
26 #ifndef _LIBC
27 # include <config.h>
28 #endif
29
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "root-uid.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 #endif
71
72 /* Return 0 if the user has permission of type MODE on FILE;
73    otherwise, return -1 and set 'errno'.
74    Like access, except that it uses the effective user and group
75    id's instead of the real ones, and it does not always check for read-only
76    file system, text busy, etc.  */
77
78 int
79 euidaccess (const char *file, int mode)
80 {
81 #if HAVE_FACCESSAT                   /* glibc, AIX 7, Solaris 11, Cygwin 1.7 */
82   return faccessat (AT_FDCWD, file, mode, AT_EACCESS);
83 #elif defined EFF_ONLY_OK               /* IRIX, OSF/1, Interix */
84   return access (file, mode | EFF_ONLY_OK);
85 #elif defined ACC_SELF                  /* AIX */
86   return accessx (file, mode, ACC_SELF);
87 #elif HAVE_EACCESS                      /* FreeBSD */
88   return eaccess (file, mode);
89 #else       /* Mac OS X, NetBSD, OpenBSD, HP-UX, Solaris, Cygwin, mingw, BeOS */
90
91   uid_t uid = getuid ();
92   gid_t gid = getgid ();
93   uid_t euid = geteuid ();
94   gid_t egid = getegid ();
95   struct stat stats;
96
97 # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
98
99   /* Define PREFER_NONREENTRANT_EUIDACCESS if you prefer euidaccess to
100      return the correct result even if this would make it
101      nonreentrant.  Define this only if your entire application is
102      safe even if the uid or gid might temporarily change.  If your
103      application uses signal handlers or threads it is probably not
104      safe.  */
105
106   if (mode == F_OK)
107     return stat (file, &stats);
108   else
109     {
110       int result;
111       int saved_errno;
112
113       if (uid != euid)
114         setreuid (euid, uid);
115       if (gid != egid)
116         setregid (egid, gid);
117
118       result = access (file, mode);
119       saved_errno = errno;
120
121       /* Restore them.  */
122       if (uid != euid)
123         setreuid (uid, euid);
124       if (gid != egid)
125         setregid (gid, egid);
126
127       errno = saved_errno;
128       return result;
129     }
130
131 # else
132
133   /* The following code assumes the traditional Unix model, and is not
134      correct on systems that have ACLs or the like.  However, it's
135      better than nothing, and it is reentrant.  */
136
137   unsigned int granted;
138   if (uid == euid && gid == egid)
139     /* If we are not set-uid or set-gid, access does the same.  */
140     return access (file, mode);
141
142   if (stat (file, &stats) != 0)
143     return -1;
144
145   /* The super-user can read and write any file, and execute any file
146      that anyone can execute.  */
147   if (euid == ROOT_UID
148       && ((mode & X_OK) == 0
149           || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
150     return 0;
151
152   /* Convert the mode to traditional form, clearing any bogus bits.  */
153   if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0)
154     mode &= 7;
155   else
156     mode = ((mode & R_OK ? 4 : 0)
157             + (mode & W_OK ? 2 : 0)
158             + (mode & X_OK ? 1 : 0));
159
160   if (mode == 0)
161     return 0;                   /* The file exists.  */
162
163   /* Convert the file's permission bits to traditional form.  */
164   if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6)
165       && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3)
166       && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0))
167     granted = stats.st_mode;
168   else
169     granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0)
170                + (stats.st_mode & S_IWUSR ? 2 << 6 : 0)
171                + (stats.st_mode & S_IXUSR ? 1 << 6 : 0)
172                + (stats.st_mode & S_IRGRP ? 4 << 3 : 0)
173                + (stats.st_mode & S_IWGRP ? 2 << 3 : 0)
174                + (stats.st_mode & S_IXGRP ? 1 << 3 : 0)
175                + (stats.st_mode & S_IROTH ? 4 << 0 : 0)
176                + (stats.st_mode & S_IWOTH ? 2 << 0 : 0)
177                + (stats.st_mode & S_IXOTH ? 1 << 0 : 0));
178
179   if (euid == stats.st_uid)
180     granted >>= 6;
181   else if (egid == stats.st_gid || group_member (stats.st_gid))
182     granted >>= 3;
183
184   if ((mode & ~granted) == 0)
185     return 0;
186   __set_errno (EACCESS);
187   return -1;
188
189 # endif
190 #endif
191 }
192 #undef euidaccess
193 #ifdef weak_alias
194 weak_alias (__euidaccess, euidaccess)
195 #endif
196 \f
197 #ifdef TEST
198 # include <error.h>
199 # include <stdio.h>
200 # include <stdlib.h>
201
202 char *program_name;
203
204 int
205 main (int argc, char **argv)
206 {
207   char *file;
208   int mode;
209   int err;
210
211   program_name = argv[0];
212   if (argc < 3)
213     abort ();
214   file = argv[1];
215   mode = atoi (argv[2]);
216
217   err = euidaccess (file, mode);
218   printf ("%d\n", err);
219   if (err != 0)
220     error (0, errno, "%s", file);
221   exit (0);
222 }
223 #endif