home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / lib / eaccess.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-04  |  3.9 KB  |  168 lines

  1. /* eaccess -- check if effective user id can access file
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  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.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie and Torbjorn Granlund. */
  19.  
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22.  
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26.  
  27. #ifdef _POSIX_VERSION
  28. #include <limits.h>
  29. #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
  30. #undef NGROUPS_MAX
  31. #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
  32. #endif /* NGROUPS_MAX */
  33.  
  34. #else /* not _POSIX_VERSION */
  35. uid_t geteuid ();
  36. gid_t getegid ();
  37. #include <sys/param.h>
  38. #if !defined(NGROUPS_MAX) && defined(NGROUPS)
  39. #define NGROUPS_MAX NGROUPS
  40. #endif /* not NGROUPS_MAX and NGROUPS */
  41. #endif /* not POSIX_VERSION */
  42.  
  43. #include <errno.h>
  44. #ifndef STDC_HEADERS
  45. extern int errno;
  46. #endif
  47.  
  48. #if defined(EACCES) && !defined(EACCESS)
  49. #define EACCESS EACCES
  50. #endif
  51.  
  52. #ifndef F_OK
  53. #define F_OK 0
  54. #define X_OK 1
  55. #define W_OK 2
  56. #define R_OK 4
  57. #endif
  58.  
  59. int eaccess_stat ();
  60.  
  61. /* The user's effective user id. */
  62. static uid_t euid;
  63.  
  64. /* The user's effective group id. */
  65. static gid_t egid;
  66.  
  67. #ifdef NGROUPS_MAX
  68. char *xmalloc ();
  69. static int in_group ();
  70.  
  71. /* Array of group id's that the user is in. */
  72. static GETGROUPS_T *groups = 0;
  73.  
  74. /* The number of valid elements in `groups'. */
  75. static int ngroups;
  76. #endif
  77.  
  78. /* Nonzero if the other static variables have valid values. */
  79. static int initialized = 0;
  80.  
  81. /* Return 0 if the user has permission of type MODE on file PATH;
  82.    otherwise, return -1 and set `errno' to EACCESS.
  83.    Like access, except that it uses the effective user and group
  84.    id's instead of the real ones, and it does not check for read-only
  85.    filesystem, text busy, etc. */
  86.  
  87. int
  88. eaccess (path, mode)
  89.      char *path;
  90.      int mode;
  91. {
  92.   struct stat stats;
  93.  
  94.   if (stat (path, &stats))
  95.     return -1;
  96.  
  97.   return eaccess_stat (&stats, mode);
  98. }
  99.  
  100. /* Like eaccess, except that a pointer to a filled-in stat structure
  101.    describing the file is provided instead of a filename. */
  102.  
  103. int
  104. eaccess_stat (statp, mode)
  105.      struct stat *statp;
  106.      int mode;
  107. {
  108.   int granted;
  109.  
  110.   mode &= (X_OK | W_OK | R_OK);    /* Clear any bogus bits. */
  111.  
  112.   if (mode == F_OK)
  113.     return 0;            /* The file exists. */
  114.  
  115.   if (initialized == 0)
  116.     {
  117.       initialized = 1;
  118.       euid = geteuid ();
  119.       egid = getegid ();
  120. #ifdef NGROUPS_MAX
  121.       groups = (GETGROUPS_T *)
  122.         xmalloc ((unsigned) NGROUPS_MAX * sizeof (GETGROUPS_T));
  123.       ngroups = getgroups ((int) NGROUPS_MAX, groups);
  124. #endif
  125.     }
  126.   
  127.   /* The super-user can read and write any file, and execute any file
  128.      that anyone can execute. */
  129.   if (euid == 0 && ((mode & X_OK) == 0 || (statp->st_mode & 0111)))
  130.     return 0;
  131.   if (euid == statp->st_uid)
  132.     granted = (unsigned) (statp->st_mode & (mode << 6)) >> 6;
  133.   else if (egid == statp->st_gid
  134. #ifdef NGROUPS_MAX
  135.        || in_group (statp->st_gid)
  136. #endif
  137.        )
  138.     granted = (unsigned) (statp->st_mode & (mode << 3)) >> 3;
  139.   else
  140.     granted = (statp->st_mode & mode);
  141.   if (granted == mode)
  142.     return 0;
  143.   errno = EACCESS;
  144.   return -1;
  145. }
  146.  
  147. #ifdef NGROUPS_MAX
  148. static int
  149. in_group (gid)
  150.      GETGROUPS_T gid;
  151. {
  152.   int i;
  153.  
  154.   for (i = 0; i < ngroups; i++)
  155.     if (gid == groups[i])
  156.       return 1;
  157.   return 0;
  158. }
  159. #endif
  160.  
  161. #ifdef TEST
  162. main (argc, argv)
  163.      char **argv;
  164. {
  165.   printf ("%d\n", eaccess (argv[1], atoi (argv[2])));
  166. }
  167. #endif
  168.