1 
2 /* Check if effective user id can access file
3    Copyright (C) 1990, 91, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20 
21 /* Written by David MacKenzie and Torbjorn Granlund.
22    Adapted for GNU C library by Roland McGrath. Adapted further
23    for use with xisp by Dimitrios P. Bouras <dbouras@hol.gr> */
24 
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 
28 #ifdef S_IEXEC
29 #ifndef S_IXUSR
30 #define S_IXUSR S_IEXEC
31 #endif
32 #ifndef S_IXGRP
33 #define S_IXGRP (S_IEXEC >> 3)
34 #endif
35 #ifndef S_IXOTH
36 #define S_IXOTH (S_IEXEC >> 6)
37 #endif
38 #endif /* S_IEXEC */
39 
40 #include <unistd.h>
41 
42 uid_t getuid ();
43 gid_t getgid ();
44 uid_t geteuid ();
45 gid_t getegid ();
46 #include <sys/param.h>
47 #if !defined(NGROUPS_MAX) && defined(NGROUPS)
48 #define NGROUPS_MAX NGROUPS
49 #endif /* not NGROUPS_MAX and NGROUPS */
50 
51 #include <errno.h>
52 #ifndef errno
53 extern int errno;
54 #endif
55 #ifndef __set_errno
56 #define __set_errno(val) errno = (val)
57 #endif
58 
59 #if defined(EACCES) && !defined(EACCESS)
60 #define EACCESS EACCES
61 #endif
62 
63 #ifndef F_OK
64 #define F_OK 0
65 #define X_OK 1
66 #define W_OK 2
67 #define R_OK 4
68 #endif
69 
70 #if !defined (S_IROTH) && defined (R_OK)
71 # define S_IROTH R_OK
72 #endif
73 #if !defined (S_IWOTH) && defined (W_OK)
74 # define S_IWOTH W_OK
75 #endif
76 #if !defined (S_IXOTH) && defined (X_OK)
77 # define S_IXOTH X_OK
78 #endif
79 
80 #include <alloca.h>
81 #ifndef NGROUPS_MAX
82 #define NGROUPS_MAX	16	/* First guess.  */
83 #endif
group_member(gid)84 int group_member (gid)
85      gid_t gid;
86 {
87   int n, size;
88   gid_t *groups;
89 
90   size = NGROUPS_MAX;
91   do
92     {
93       groups = alloca (size * sizeof *groups);
94       n = getgroups (size, groups);
95       size *= 2;
96     } while (n == size / 2);
97 
98   while (n > 0)
99     if (groups[n--] == gid)
100       return 1;
101 
102   return 0;
103 }
104 
105 /* The user's real user id. */
106 static uid_t uid;
107 
108 /* The user's real group id. */
109 static gid_t gid;
110 
111 /* The user's effective user id. */
112 static uid_t euid;
113 
114 /* The user's effective group id. */
115 static gid_t egid;
116 
117 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
118 static int have_ids;
119 
120 /* Return 0 if the user has permission of type MODE on file PATH;
121    otherwise, return -1 and set `errno' to EACCESS.
122    Like access, except that it uses the effective user and group
123    id's instead of the real ones, and it does not check for read-only
124    filesystem, text busy, etc. */
125 
126 int
xisp_euidaccess(path,mode)127 xisp_euidaccess (path, mode)
128      const char *path;
129      int mode;
130 {
131   struct stat stats;
132   int granted;
133 
134   if (have_ids == 0)
135     {
136       have_ids = 1;
137       uid = getuid ();
138       gid = getgid ();
139       euid = geteuid ();
140       egid = getegid ();
141     }
142 
143   if (uid == euid && gid == egid)
144     /* If we are not set-uid or set-gid, access does the same.  */
145     return access (path, mode);
146 
147   if (stat (path, &stats))
148     return -1;
149 
150   mode &= (X_OK | W_OK | R_OK);	/* Clear any bogus bits. */
151 #if R_OK != S_IROTH || W_OK != S_IWOTH || X_OK != S_IXOTH
152   #error Oops, portability assumptions incorrect.
153 #endif
154 
155   if (mode == F_OK)
156     return 0;			/* The file exists. */
157 
158   /* The super-user can read and write any file, and execute any file
159      that anyone can execute. */
160   if (euid == 0 && ((mode & X_OK) == 0
161 		    || (stats.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
162     return 0;
163 
164   if (euid == stats.st_uid)
165     granted = (unsigned) (stats.st_mode & (mode << 6)) >> 6;
166   else if (egid == stats.st_gid || group_member (stats.st_gid))
167     granted = (unsigned) (stats.st_mode & (mode << 3)) >> 3;
168   else
169     granted = (stats.st_mode & mode);
170   if (granted == mode)
171     return 0;
172   __set_errno (EACCESS);
173   return -1;
174 }
175 
176