Initial revision
[pspp] / lib / euidaccess.c
1 /* euidaccess -- 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 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #ifdef _POSIX_VERSION
32 #include <limits.h>
33 #if !defined(NGROUPS_MAX) || NGROUPS_MAX < 1
34 #undef NGROUPS_MAX
35 #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
36 #endif /* NGROUPS_MAX */
37
38 #else /* not _POSIX_VERSION */
39 uid_t getuid ();
40 gid_t getgid ();
41 uid_t geteuid ();
42 gid_t getegid ();
43 #include <sys/param.h>
44 #if !defined(NGROUPS_MAX) && defined(NGROUPS)
45 #define NGROUPS_MAX NGROUPS
46 #endif /* not NGROUPS_MAX and NGROUPS */
47 #endif /* not POSIX_VERSION */
48
49 #include <errno.h>
50 #ifndef STDC_HEADERS
51 extern int errno;
52 #endif
53
54 #if defined(EACCES) && !defined(EACCESS)
55 #define EACCESS EACCES
56 #endif
57
58 #ifndef F_OK
59 #define F_OK 0
60 #define X_OK 1
61 #define W_OK 2
62 #define R_OK 4
63 #endif
64
65 #include "safe-stat.h"
66
67 /* The user's real user id. */
68 static uid_t uid;
69
70 /* The user's real group id. */
71 static gid_t gid;
72
73 /* The user's effective user id. */
74 static uid_t euid;
75
76 /* The user's effective group id. */
77 static gid_t egid;
78
79 int group_member ();
80
81 /* Nonzero if UID, GID, EUID, and EGID have valid values. */
82 static int have_ids = 0;
83
84 /* Like euidaccess, except that a pointer to a filled-in stat structure
85    describing the file is provided instead of a filename.
86    Because this function is almost guaranteed to fail on systems that
87    use ACLs, a third argument *PATH may be used.  If it is non-NULL,
88    it is assumed to be the name of the file corresponding to STATP.
89    Then, if the user is not running set-uid or set-gid, use access
90    instead of attempting a manual and non-portable comparison.  */
91
92 int
93 eaccess_stat (statp, mode, path)
94      struct stat *statp;
95      int mode;
96      char *path;
97 {
98   int granted;
99
100   mode &= (X_OK | W_OK | R_OK); /* Clear any bogus bits. */
101
102   if (mode == F_OK)
103     return 0;                   /* The file exists. */
104
105   if (have_ids == 0)
106     {
107       have_ids = 1;
108       uid = getuid ();
109       gid = getgid ();
110       euid = geteuid ();
111       egid = getegid ();
112     }
113
114   if (path && uid == euid && gid == egid)
115     {
116       return access (path, mode);
117     }
118
119   /* The super-user can read and write any file, and execute any file
120      that anyone can execute. */
121   if (euid == 0 && ((mode & X_OK) == 0
122                     || (statp->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))))
123     return 0;
124
125   if (euid == statp->st_uid)
126     granted = (unsigned) (statp->st_mode & (mode << 6)) >> 6;
127   else if (egid == statp->st_gid
128 #ifdef HAVE_GETGROUPS
129            || group_member (statp->st_gid)
130 #endif
131            )
132     granted = (unsigned) (statp->st_mode & (mode << 3)) >> 3;
133   else
134     granted = (statp->st_mode & mode);
135   if (granted == mode)
136     return 0;
137   errno = EACCESS;
138   return -1;
139 }
140
141 /* Return 0 if the user has permission of type MODE on file PATH;
142    otherwise, return -1 and set `errno' to EACCESS.
143    Like access, except that it uses the effective user and group
144    id's instead of the real ones, and it does not check for read-only
145    filesystem, text busy, etc. */
146
147 int
148 euidaccess (path, mode)
149      char *path;
150      int mode;
151 {
152   struct stat stats;
153
154   if (have_ids == 0)
155     {
156       have_ids = 1;
157       uid = getuid ();
158       gid = getgid ();
159       euid = geteuid ();
160       egid = getegid ();
161     }
162
163   if (uid == euid && gid == egid)
164     {
165       return access (path, mode);
166     }
167
168   if (SAFE_STAT (path, &stats))
169     return -1;
170
171   return eaccess_stat (&stats, mode, path);
172 }
173
174 #ifdef TEST
175 #include <stdio.h>
176 #include <errno.h>
177
178 void error ();
179
180 char *program_name;
181
182 int
183 main (argc, argv)
184      int argc;
185      char **argv;
186 {
187   char *file;
188   int mode;
189   int err;
190
191   program_name = argv[0];
192   if (argc < 3)
193     abort ();
194   file = argv[1];
195   mode = atoi (argv[2]);
196
197   err = euidaccess (file, mode);
198   printf ("%d\n", err);
199   if (err != 0)
200     error (0, errno, "%s", file);
201   exit (0);
202 }
203 #endif