2 Check access to a file by user name.
4 Copyright (C) 1992 Ian Lance Taylor
6 This file is part of the Taylor UUCP package.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 The author of the program may be contacted at ian@airs.com or
23 c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
36 #if GETGRENT_DECLARATION_OK
38 extern struct group *getgrent ();
41 #endif /* HAVE_GETGRENT */
43 #if GETPWNAM_DECLARATION_OK
45 extern struct passwd *getpwnam ();
49 /* Do access(2) on a stat structure, except that the user name is
50 provided. If the user name in zuser is NULL, require the file to
51 be accessible to the world. Return TRUE if access is permitted,
52 FALSE otherwise. This does not log an error message. */
55 fsuser_access (q, imode, zuser)
60 static char *zuser_hold;
61 static uid_t iuid_hold;
62 static gid_t igid_hold;
63 static int cgroups_hold;
64 static gid_t *paigroups_hold;
72 /* We keep static variables around for the last user we did, to
73 avoid looking up a user multiple times. */
74 if (zuser_hold == NULL || strcmp (zuser_hold, zuser) != 0)
78 if (zuser_hold != NULL)
80 ubuffree (zuser_hold);
83 xfree ((pointer) paigroups_hold);
84 paigroups_hold = NULL;
87 qpwd = getpwnam ((char *) zuser);
90 /* Check this as a remote request. */
99 zuser_hold = zbufcpy (zuser);
101 iuid_hold = qpwd->pw_uid;
102 igid_hold = qpwd->pw_gid;
105 /* Get the list of groups for this user. This is
106 definitely more appropriate for BSD than for System
107 V. It may just be a waste of time, and perhaps it
108 should be configurable. */
110 while ((qg = getgrent ()) != NULL)
114 if (qg->gr_gid == igid_hold)
116 for (pz = (const char **) qg->gr_mem; *pz != NULL; pz++)
118 if ((*pz)[0] == *zuser
119 && strcmp (*pz, zuser) == 0)
121 paigroups_hold = ((gid_t *)
123 ((pointer) paigroups_hold,
125 * sizeof (gid_t)))));
126 paigroups_hold[cgroups_hold] = qg->gr_gid;
139 /* Now do the actual access check. */
143 /* The superuser can do anything. */
147 /* If this is the uid we're running under, there's no point to
148 checking access further, because when we actually try the
149 operation the system will do the checking for us. */
150 if (iuid_hold == geteuid ())
160 if (iuid_hold == q->st_uid)
171 if (igid_hold == q->st_gid)
177 for (i = 0; i < cgroups_hold; i++)
179 if (paigroups_hold[i] == q->st_gid)
197 if ((imode & R_OK) != 0)
199 if ((imode & W_OK) != 0)
201 if ((imode & X_OK) != 0)
204 return (q->st_mode & iand) == iand;