bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] euidaccess fixes from coreutils


From: Paul Eggert
Subject: [Bug-gnulib] euidaccess fixes from coreutils
Date: Fri, 06 Aug 2004 16:48:40 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I installed this:

2004-08-06  Paul Eggert  <address@hidden>

        * modules/euidaccess: Add stat-macros.h.
        * lib/euidaccess.c [HAVE_LIBGEN_H]: Include <libgen.h>, for
        eaccess on Solaris and SVR4-like systems.
        (euidaccess): Use HAVE_EACCESS, not HAVE_DECL_EACCESS.
        [!defined LIBC]: Included group-member.h, stat-macros.h.
        (S_IXUSR, S_IXGRP, S_IXOTH, S_IROTH, S_IWOTH, S_IXOTH):
        Remove; now done by stat-macros.h.
        (NGROUPS_MAX, group_member): Remove; now done by group-member.h.
        No need to include <limits.h>.
        (errno): Remove decl; we now assume C89 or better.
        (access, getuid, getgid, geteuid, getegid, stat) [defined _LIBC]:
        New macros.
        (uid, gid, have_ids): Remove these static variables.
        They weren't accurate for programs that also invoked setreuid etc.
        (euidaccess) [defined EFF_ONLY_OK || defined ACC_SELF ||
        HAVE_DECL_EACCSS]: Use builtin substitutes.
        [defined _LIBC]: Ignore __libc_enable_secure; it's not a
        correct optimization for programs run as root that later
        invoke setreuid.
        [no builtin substitutes && HAVE_DECL_SETREGID &&
        PREFER_NONREENTRANT_EUIDACCESS]:
        Use setreuid+setregid to get the correct answer.
        [no builtin substitutes && ! (HAVE_DECL_SETREGID &&
        PREFER_NONREENTRANT_EUIDACCESS)]:
        Don't assume that the stat macros have their historical values,
        as POSIX doesn't require this.
        [defined TEST]: Include <stdlib.h>; don't include errno.h
        twice; include <error.h> rather than "error.h".
        * m4/euidaccess.m4 (gl_PREREQ_EUIDACCESS): Don't bother checking for
        unistd.h, as autoconf does this for us.  Check for libgen.h.
        Also look for eaccess within -lgen, which is where it is in
        Solaris.  Set LIB_EACCESS accordingly.
        (gl_FUNC_NONREENTRANT_EUIDACCESS): New macro.
        (gl_FUNC_EUIDACCESS): Use AC_CHECK_DECLS_ONCE, not AC_CHECK_DECLS.
        (gl_PREREQ_EUIDACCESS): Check for eaccess and setregid decls.
        Require AC_HEADER_STAT.

Index: modules/euidaccess
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/euidaccess,v
retrieving revision 1.5
diff -p -u -r1.5 euidaccess
--- modules/euidaccess  17 Aug 2003 14:34:12 -0000      1.5
+++ modules/euidaccess  6 Aug 2004 23:46:27 -0000
@@ -4,6 +4,7 @@ euidaccess() function: check effective u
 Files:
 lib/euidaccess.h
 lib/euidaccess.c
+lib/stat-macros.h
 m4/euidaccess.m4
 
 Depends-on:
Index: lib/euidaccess.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/euidaccess.c,v
retrieving revision 1.15
diff -p -u -r1.15 euidaccess.c
--- lib/euidaccess.c    9 Sep 2003 03:08:14 -0000       1.15
+++ lib/euidaccess.c    6 Aug 2004 23:40:36 -0000
@@ -1,7 +1,7 @@
 /* euidaccess -- check if effective user id can access file
 
-   Copyright (C) 1990, 1991, 1995, 1998, 2000, 2003 Free Software
-   Foundation, Inc.
+   Copyright (C) 1990, 1991, 1995, 1998, 2000, 2003, 2004 Free
+   Software Foundation, Inc.
 
    This file is part of the GNU C Library.
 
@@ -33,49 +33,27 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#ifdef S_IEXEC
-# ifndef S_IXUSR
-#  define S_IXUSR S_IEXEC
-# endif
-# ifndef S_IXGRP
-#  define S_IXGRP (S_IEXEC >> 3)
-# endif
-# ifndef S_IXOTH
-#  define S_IXOTH (S_IEXEC >> 6)
-# endif
-#endif /* S_IEXEC */
-
-#if defined (HAVE_UNISTD_H) || defined (_LIBC)
+#if HAVE_UNISTD_H || defined _LIBC
 # include <unistd.h>
 #endif
 
-#ifdef _POSIX_VERSION
-# include <limits.h>
-# if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
-#  undef NGROUPS_MAX
-#  define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
-# endif /* NGROUPS_MAX */
+#if HAVE_LIBGEN_H
+# include <libgen.h>
+#endif
 
-#else /* not _POSIX_VERSION */
+#ifndef _POSIX_VERSION
 uid_t getuid ();
 gid_t getgid ();
 uid_t geteuid ();
 gid_t getegid ();
-# include <sys/param.h>
-# if !defined(NGROUPS_MAX) && defined(NGROUPS)
-#  define NGROUPS_MAX NGROUPS
-# endif /* not NGROUPS_MAX and NGROUPS */
-#endif /* not POSIX_VERSION */
+#endif
 
 #include <errno.h>
-#ifndef errno
-extern int errno;
-#endif
 #ifndef __set_errno
 # define __set_errno(val) errno = (val)
 #endif
 
-#if defined(EACCES) && !defined(EACCESS)
+#if defined EACCES && !defined EACCESS
 # define EACCESS EACCES
 #endif
 
@@ -86,117 +64,142 @@ extern int errno;
 # define R_OK 4
 #endif
 
-#if !defined (S_IROTH) && defined (R_OK)
-# define S_IROTH R_OK
-#endif
-
-#if !defined (S_IWOTH) && defined (W_OK)
-# define S_IWOTH W_OK
-#endif
-
-#if !defined (S_IXOTH) && defined (X_OK)
-# define S_IXOTH X_OK
-#endif
 
 #ifdef _LIBC
 
+# define access __access
+# define getuid __getuid
+# define getgid __getgid
+# define geteuid __geteuid
+# define getegid __getegid
 # define group_member __group_member
 # define euidaccess __euidaccess
+# undef stat
+# define stat stat64
 
 #else
 
-/* The user's real user id. */
-static uid_t uid;
-
-/* The user's real group id. */
-static gid_t gid;
-
-# if HAVE_GETGROUPS
-int group_member ();
-# else
-#  define group_member(gid)    0
-# endif
+# include "group-member.h"
+# include "stat-macros.h"
 
 #endif
 
-/* The user's effective user id. */
-static uid_t euid;
-
-/* The user's effective group id. */
-static gid_t egid;
-
-/* Nonzero if UID, GID, EUID, and EGID have valid values. */
-static int have_ids;
-
-
 /* Return 0 if the user has permission of type MODE on file PATH;
-   otherwise, return -1 and set `errno' to EACCESS.
+   otherwise, return -1 and set `errno'.
    Like access, except that it uses the effective user and group
-   id's instead of the real ones, and it does not check for read-only
-   filesystem, text busy, etc. */
+   id's instead of the real ones, and it does not always check for read-only
+   file system, text busy, etc.  */
 
 int
 euidaccess (const char *path, int mode)
 {
+#if defined EFF_ONLY_OK
+  return access (path, mode | EFF_ONLY_OK);
+#elif defined ACC_SELF
+  return accessx (path, mode, ACC_SELF);
+#elif HAVE_EACCESS
+  return eaccess (path, mode);
+#else
+
+  uid_t uid = getuid ();
+  gid_t gid = getgid ();
+  uid_t euid = geteuid ();
+  gid_t egid = getegid ();
   struct stat stats;
-  int granted;
 
-#ifdef _LIBC
-  if (! __libc_enable_secure)
-    /* If we are not set-uid or set-gid, access does the same.  */
-    return __access (path, mode);
-#else
-  if (have_ids == 0)
+# if HAVE_DECL_SETREGID && PREFER_NONREENTRANT_EUIDACCESS
+
+  /* Define PREFER_NONREENTRANT_EUIDACCESS if you prefer euidaccess to
+     return the correct result even if this would make it
+     nonreentrant.  Define this only if your entire application is
+     safe even if the uid or gid might temporarily change.  If your
+     application uses signal handlers or threads it is probably not
+     safe.  */
+
+  if (mode == F_OK)
+    return stat (path, &stats);
+  else
     {
-      have_ids = 1;
-      uid = getuid ();
-      gid = getgid ();
-      euid = geteuid ();
-      egid = getegid ();
+      int result;
+      int saved_errno;
+      
+      if (uid != euid)
+       setreuid (euid, uid);
+      if (gid != egid)
+       setregid (egid, gid);
+
+      result = access (path, mode);
+      saved_errno = errno;
+      
+      /* Restore them.  */
+      if (uid != euid)
+       setreuid (uid, euid);
+      if (gid != egid)
+       setregid (gid, egid);
+
+      errno = saved_errno;
+      return result;
     }
 
+# else
+
+  /* The following code assumes the traditional Unix model, and is not
+     correct on systems that have ACLs or the like.  However, it's
+     better than nothing, and it is reentrant.  */
+
+  unsigned int granted;
   if (uid == euid && gid == egid)
     /* If we are not set-uid or set-gid, access does the same.  */
     return access (path, mode);
-#endif
 
   if (stat (path, &stats))
     return -1;
 
-  mode &= (X_OK | W_OK | R_OK);        /* Clear any bogus bits. */
-#if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH
-  ?error Oops, portability assumptions incorrect.
-#endif
-
-  if (mode == F_OK)
-    return 0;                  /* The file exists. */
-
-#ifdef _LIBC
-  /* Now we need the IDs.  */
-  if (have_ids == 0)
-    {
-      have_ids = 1;
-      euid = __geteuid ();
-      egid = __getegid ();
-    }
-#endif
-
   /* The super-user can read and write any file, and execute any file
-     that anyone can execute. */
+     that anyone can execute.  */
   if (euid == 0 && ((mode & X_OK) == 0
                    || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
     return 0;
 
+  /* Convert the mode to traditional form, clearing any bogus bits.  */
+  if (R_OK == 4 && W_OK == 2 && X_OK == 1 && F_OK == 0)
+    mode &= 7;
+  else
+    mode = ((mode & R_OK ? 4 : 0)
+           + (mode & W_OK ? 2 : 0)
+           + (mode & X_OK ? 1 : 0));
+
+  if (mode == 0)
+    return 0;                  /* The file exists.  */
+
+  /* Convert the file's permission bits to traditional form.  */
+  if (S_IRUSR == (4 << 6) && S_IWUSR == (2 << 6) && S_IXUSR == (1 << 6)
+      && S_IRGRP == (4 << 3) && S_IWGRP == (2 << 3) && S_IXGRP == (1 << 3)
+      && S_IROTH == (4 << 0) && S_IWOTH == (2 << 0) && S_IXOTH == (1 << 0))
+    granted = stats.st_mode;
+  else
+    granted = ((stats.st_mode & S_IRUSR ? 4 << 6 : 0)
+              + (stats.st_mode & S_IWUSR ? 2 << 6 : 0)
+              + (stats.st_mode & S_IXUSR ? 1 << 6 : 0)
+              + (stats.st_mode & S_IRGRP ? 4 << 3 : 0)
+              + (stats.st_mode & S_IWGRP ? 2 << 3 : 0)
+              + (stats.st_mode & S_IXGRP ? 1 << 3 : 0)
+              + (stats.st_mode & S_IROTH ? 4 << 0 : 0)
+              + (stats.st_mode & S_IWOTH ? 2 << 0 : 0)
+              + (stats.st_mode & S_IXOTH ? 1 << 0 : 0));
+
   if (euid == stats.st_uid)
-    granted = (unsigned) (stats.st_mode & (mode << 6)) >> 6;
+    granted >>= 6;
   else if (egid == stats.st_gid || group_member (stats.st_gid))
-    granted = (unsigned) (stats.st_mode & (mode << 3)) >> 3;
-  else
-    granted = (stats.st_mode & mode);
-  if (granted == mode)
+    granted >>= 3;
+
+  if ((mode & ~granted) == 0)
     return 0;
   __set_errno (EACCESS);
   return -1;
+
+# endif
+#endif
 }
 #undef euidaccess
 #ifdef weak_alias
@@ -204,9 +207,9 @@ weak_alias (__euidaccess, euidaccess)
 #endif
 
 #ifdef TEST
+# include <error.h>
 # include <stdio.h>
-# include <errno.h>
-# include "error.h"
+# include <stdlib.h>
 
 char *program_name;
 
Index: m4/euidaccess.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/euidaccess.m4,v
retrieving revision 1.2
diff -p -u -r1.2 euidaccess.m4
--- m4/euidaccess.m4    12 Aug 2003 23:39:50 -0000      1.2
+++ m4/euidaccess.m4    6 Aug 2004 23:40:36 -0000
@@ -1,17 +1,28 @@
-# euidaccess.m4 serial 2
-dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+# euidaccess.m4 serial 4
+dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
 dnl This file is free software, distributed under the terms of the GNU
 dnl General Public License.  As a special exception to the GNU General
 dnl Public License, this file may be distributed as part of a program
 dnl that contains a configuration script generated by Autoconf, under
 dnl the same distribution terms as the rest of that program.
 
+AC_DEFUN([gl_FUNC_NONREENTRANT_EUIDACCESS],
+[
+  AC_REQUIRE([gl_FUNC_EUIDACCESS])
+  AC_DEFINE([PREFER_NONREENTRANT_EUIDACCESS], 1,
+    [Define this if you prefer euidaccess to return the correct result
+     even if this would make it nonreentrant.  Define this only if your
+     entire application is safe even if the uid or gid might temporarily
+     change.  If your application uses signal handlers or threads it
+     is probably not safe.])
+])
+
 AC_DEFUN([gl_FUNC_EUIDACCESS],
 [
   dnl Persuade glibc <unistd.h> to declare euidaccess().
   AC_REQUIRE([AC_GNU_SOURCE])
 
-  AC_CHECK_DECLS([euidaccess])
+  AC_CHECK_DECLS_ONCE([euidaccess])
   AC_REPLACE_FUNCS(euidaccess)
   if test $ac_cv_func_euidaccess = no; then
     gl_PREREQ_EUIDACCESS
@@ -20,7 +31,21 @@ AC_DEFUN([gl_FUNC_EUIDACCESS],
 
 # Prerequisites of lib/euidaccess.c.
 AC_DEFUN([gl_PREREQ_EUIDACCESS], [
-  AC_CHECK_HEADERS_ONCE(unistd.h)
+  AC_CHECK_HEADERS_ONCE(libgen.h)
+  AC_CHECK_DECLS_ONCE(setregid)
   AC_REQUIRE([AC_FUNC_GETGROUPS])
+  AC_REQUIRE([AC_HEADER_STAT])
+
+  # Solaris 9 needs -lgen to get the eaccess function.
+  # Save and restore LIBS so -lgen isn't added to it.  Otherwise, *all*
+  # programs in the package would end up linked with that potentially-shared
+  # library, inducing unnecessary run-time overhead.
+  gl_saved_libs=$LIBS
+    AC_SEARCH_LIBS(eaccess, [gen],
+                   [test "$ac_cv_search_eaccess" = "none required" ||
+                    LIB_EACCESS=$ac_cv_search_eaccess])
+    AC_SUBST(LIB_EACCESS)
+    AC_CHECK_FUNCS(eaccess)
+  LIBS=$gl_saved_libs
 ])
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]