euidaccess.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* euidaccess -- check if effective user id can access file
  2. Copyright (C) 1990-1991, 1995, 1998, 2000, 2003-2006, 2008-2017 Free
  3. Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  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 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* Written by David MacKenzie and Torbjorn Granlund.
  16. Adapted for GNU C library by Roland McGrath. */
  17. #ifndef _LIBC
  18. # include <config.h>
  19. #endif
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #include "root-uid.h"
  25. #if HAVE_LIBGEN_H
  26. # include <libgen.h>
  27. #endif
  28. #include <errno.h>
  29. #ifndef __set_errno
  30. # define __set_errno(val) errno = (val)
  31. #endif
  32. #if defined EACCES && !defined EACCESS
  33. # define EACCESS EACCES
  34. #endif
  35. #ifndef F_OK
  36. # define F_OK 0
  37. # define X_OK 1
  38. # define W_OK 2
  39. # define R_OK 4
  40. #endif
  41. #ifdef _LIBC
  42. # define access __access
  43. # define getuid __getuid
  44. # define getgid __getgid
  45. # define geteuid __geteuid
  46. # define getegid __getegid
  47. # define group_member __group_member
  48. # define euidaccess __euidaccess
  49. # undef stat
  50. # define stat stat64
  51. #endif
  52. /* Return 0 if the user has permission of type MODE on FILE;
  53. otherwise, return -1 and set 'errno'.
  54. Like access, except that it uses the effective user and group
  55. id's instead of the real ones, and it does not always check for read-only
  56. file system, text busy, etc. */
  57. int
  58. euidaccess (const char *file, int mode)
  59. {
  60. #if HAVE_FACCESSAT /* glibc, AIX 7, Solaris 11, Cygwin 1.7 */
  61. return faccessat (AT_FDCWD, file, mode, AT_EACCESS);
  62. #elif defined EFF_ONLY_OK /* IRIX, OSF/1, Interix */
  63. return access (file, mode | EFF_ONLY_OK);
  64. #elif defined ACC_SELF /* AIX */
  65. return accessx (file, mode, ACC_SELF);
  66. #elif HAVE_EACCESS /* FreeBSD */
  67. return eaccess (file, mode);
  68. #else /* Mac OS X, NetBSD, OpenBSD, HP-UX, Solaris, Cygwin, mingw, BeOS */
  69. uid_t uid = getuid ();
  70. gid_t gid = getgid ();
  71. uid_t euid = geteuid ();
  72. gid_t egid = getegid ();
  73. struct stat stats;
  74. # if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
  75. /* Define PREFER_NONREENTRANT_EUIDACCESS if you prefer euidaccess to
  76. return the correct result even if this would make it
  77. nonreentrant. Define this only if your entire application is
  78. safe even if the uid or gid might temporarily change. If your
  79. application uses signal handlers or threads it is probably not
  80. safe. */
  81. if (mode == F_OK)
  82. return stat (file, &stats);
  83. else
  84. {
  85. int result;
  86. int saved_errno;
  87. if (uid != euid)
  88. setreuid (euid, uid);
  89. if (gid != egid)
  90. setregid (egid, gid);
  91. result = access (file, mode);
  92. saved_errno = errno;
  93. /* Restore them. */
  94. if (uid != euid)
  95. setreuid (uid, euid);
  96. if (gid != egid)
  97. setregid (gid, egid);
  98. errno = saved_errno;
  99. return result;
  100. }
  101. # else
  102. /* The following code assumes the traditional Unix model, and is not
  103. correct on systems that have ACLs or the like. However, it's
  104. better than nothing, and it is reentrant. */
  105. unsigned int granted;
  106. if (uid == euid && gid == egid)
  107. /* If we are not set-uid or set-gid, access does the same. */
  108. return access (file, mode);
  109. if (stat (file, &stats) != 0)
  110. return -1;
  111. /* The super-user can read and write any file, and execute any file
  112. that anyone can execute. */
  113. if (euid == ROOT_UID
  114. && ((mode & X_OK) == 0
  115. || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
  116. return 0;
  117. /* Convert the mode to traditional form, clearing any bogus bits. */
  118. if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0)
  119. mode &= 7;
  120. else
  121. mode = ((mode & R_OK ? 4 : 0)
  122. + (mode & W_OK ? 2 : 0)
  123. + (mode & X_OK ? 1 : 0));
  124. if (mode == 0)
  125. return 0; /* The file exists. */
  126. /* Convert the file's permission bits to traditional form. */
  127. if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6)
  128. && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3)
  129. && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0))
  130. granted = stats.st_mode;
  131. else
  132. granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0)
  133. + (stats.st_mode & S_IWUSR ? 2 << 6 : 0)
  134. + (stats.st_mode & S_IXUSR ? 1 << 6 : 0)
  135. + (stats.st_mode & S_IRGRP ? 4 << 3 : 0)
  136. + (stats.st_mode & S_IWGRP ? 2 << 3 : 0)
  137. + (stats.st_mode & S_IXGRP ? 1 << 3 : 0)
  138. + (stats.st_mode & S_IROTH ? 4 << 0 : 0)
  139. + (stats.st_mode & S_IWOTH ? 2 << 0 : 0)
  140. + (stats.st_mode & S_IXOTH ? 1 << 0 : 0));
  141. if (euid == stats.st_uid)
  142. granted >>= 6;
  143. else if (egid == stats.st_gid || group_member (stats.st_gid))
  144. granted >>= 3;
  145. if ((mode & ~granted) == 0)
  146. return 0;
  147. __set_errno (EACCESS);
  148. return -1;
  149. # endif
  150. #endif
  151. }
  152. #undef euidaccess
  153. #ifdef weak_alias
  154. weak_alias (__euidaccess, euidaccess)
  155. #endif
  156. #ifdef TEST
  157. # include <error.h>
  158. # include <stdio.h>
  159. # include <stdlib.h>
  160. int
  161. main (int argc, char **argv)
  162. {
  163. char *file;
  164. int mode;
  165. int err;
  166. if (argc < 3)
  167. abort ();
  168. file = argv[1];
  169. mode = atoi (argv[2]);
  170. err = euidaccess (file, mode);
  171. printf ("%d\n", err);
  172. if (err != 0)
  173. error (0, errno, "%s", file);
  174. exit (0);
  175. }
  176. #endif