Fix botched merge of FQDNs in PROBLEMS
[emacs.git] / lib / euidaccess.c
blob4f512f5af5422b7c5401182e6176e7393c54e02e
1 /* euidaccess -- check if effective user id can access file
3 Copyright (C) 1990-1991, 1995, 1998, 2000, 2003-2006, 2008-2018 Free
4 Software Foundation, Inc.
6 This file is part of the GNU C Library.
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 /* Written by David MacKenzie and Torbjorn Granlund.
22 Adapted for GNU C library by Roland McGrath. */
24 #ifndef _LIBC
25 # include <config.h>
26 #endif
28 #include <fcntl.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 # include <io.h>
34 #else
35 # include "root-uid.h"
36 #endif
38 #if HAVE_LIBGEN_H
39 # include <libgen.h>
40 #endif
42 #include <errno.h>
43 #ifndef __set_errno
44 # define __set_errno(val) errno = (val)
45 #endif
47 #if defined EACCES && !defined EACCESS
48 # define EACCESS EACCES
49 #endif
51 #ifndef F_OK
52 # define F_OK 0
53 # define X_OK 1
54 # define W_OK 2
55 # define R_OK 4
56 #endif
59 #ifdef _LIBC
61 # define access __access
62 # define getuid __getuid
63 # define getgid __getgid
64 # define geteuid __geteuid
65 # define getegid __getegid
66 # define group_member __group_member
67 # define euidaccess __euidaccess
68 # undef stat
69 # define stat stat64
71 #endif
73 /* Return 0 if the user has permission of type MODE on FILE;
74 otherwise, return -1 and set 'errno'.
75 Like access, except that it uses the effective user and group
76 id's instead of the real ones, and it does not always check for read-only
77 file system, text busy, etc. */
79 int
80 euidaccess (const char *file, int mode)
82 #if HAVE_FACCESSAT /* glibc, AIX 7, Solaris 11, Cygwin 1.7 */
83 return faccessat (AT_FDCWD, file, mode, AT_EACCESS);
84 #elif defined EFF_ONLY_OK /* IRIX, OSF/1, Interix */
85 return access (file, mode | EFF_ONLY_OK);
86 #elif defined ACC_SELF /* AIX */
87 return accessx (file, mode, ACC_SELF);
88 #elif HAVE_EACCESS /* FreeBSD */
89 return eaccess (file, mode);
90 #elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ /* mingw */
91 return _access (file, mode);
92 #else /* Mac OS X, NetBSD, OpenBSD, HP-UX, Solaris, Cygwin, BeOS */
94 uid_t uid = getuid ();
95 gid_t gid = getgid ();
96 uid_t euid = geteuid ();
97 gid_t egid = getegid ();
98 struct stat stats;
100 # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
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. */
109 if (mode == F_OK)
110 return stat (file, &stats);
111 else
113 int result;
114 int saved_errno;
116 if (uid != euid)
117 setreuid (euid, uid);
118 if (gid != egid)
119 setregid (egid, gid);
121 result = access (file, mode);
122 saved_errno = errno;
124 /* Restore them. */
125 if (uid != euid)
126 setreuid (uid, euid);
127 if (gid != egid)
128 setregid (gid, egid);
130 errno = saved_errno;
131 return result;
134 # else
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. */
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);
145 if (stat (file, &stats) != 0)
146 return -1;
148 /* The super-user can read and write any file, and execute any file
149 that anyone can execute. */
150 if (euid == ROOT_UID
151 && ((mode & X_OK) == 0
152 || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
153 return 0;
155 /* Convert the mode to traditional form, clearing any bogus bits. */
156 if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0)
157 mode &= 7;
158 else
159 mode = ((mode & R_OK ? 4 : 0)
160 + (mode & W_OK ? 2 : 0)
161 + (mode & X_OK ? 1 : 0));
163 if (mode == 0)
164 return 0; /* The file exists. */
166 /* Convert the file's permission bits to traditional form. */
167 if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6)
168 && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3)
169 && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0))
170 granted = stats.st_mode;
171 else
172 granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0)
173 + (stats.st_mode & S_IWUSR ? 2 << 6 : 0)
174 + (stats.st_mode & S_IXUSR ? 1 << 6 : 0)
175 + (stats.st_mode & S_IRGRP ? 4 << 3 : 0)
176 + (stats.st_mode & S_IWGRP ? 2 << 3 : 0)
177 + (stats.st_mode & S_IXGRP ? 1 << 3 : 0)
178 + (stats.st_mode & S_IROTH ? 4 << 0 : 0)
179 + (stats.st_mode & S_IWOTH ? 2 << 0 : 0)
180 + (stats.st_mode & S_IXOTH ? 1 << 0 : 0));
182 if (euid == stats.st_uid)
183 granted >>= 6;
184 else if (egid == stats.st_gid || group_member (stats.st_gid))
185 granted >>= 3;
187 if ((mode & ~granted) == 0)
188 return 0;
189 __set_errno (EACCESS);
190 return -1;
192 # endif
193 #endif
195 #undef euidaccess
196 #ifdef weak_alias
197 weak_alias (__euidaccess, euidaccess)
198 #endif
200 #ifdef TEST
201 # include <error.h>
202 # include <stdio.h>
203 # include <stdlib.h>
206 main (int argc, char **argv)
208 char *file;
209 int mode;
210 int err;
212 if (argc < 3)
213 abort ();
214 file = argv[1];
215 mode = atoi (argv[2]);
217 err = euidaccess (file, mode);
218 printf ("%d\n", err);
219 if (err != 0)
220 error (0, errno, "%s", file);
221 exit (0);
223 #endif