14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 78462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * Glenn Fowler <gsf@research.att.com> * 184887Schin * David Korn <dgk@research.att.com> * 194887Schin * Phong Vo <kpv@research.att.com> * 204887Schin * * 214887Schin ***********************************************************************/ 224887Schin #pragma prototyped 234887Schin /* 244887Schin * access() euid/egid implementation 254887Schin */ 264887Schin 274887Schin #include <ast.h> 284887Schin #include <errno.h> 294887Schin #include <ls.h> 304887Schin 314887Schin #include "FEATURE/eaccess" 324887Schin 334887Schin #if _lib_eaccess 344887Schin 354887Schin NoN(eaccess) 364887Schin 374887Schin #else 384887Schin 394887Schin #if defined(__EXPORT__) 404887Schin #define extern __EXPORT__ 414887Schin #endif 424887Schin 434887Schin extern int 444887Schin eaccess(const char* path, register int flags) 454887Schin { 464887Schin #ifdef EFF_ONLY_OK 474887Schin return access(path, flags|EFF_ONLY_OK); 484887Schin #else 494887Schin #if _lib_euidaccess 504887Schin return euidaccess(path, flags); 514887Schin #else 524887Schin register int mode; 534887Schin struct stat st; 544887Schin 554887Schin static int init; 564887Schin static uid_t ruid; 574887Schin static uid_t euid; 584887Schin static gid_t rgid; 594887Schin static gid_t egid; 604887Schin 614887Schin if (!init) 624887Schin { 634887Schin ruid = getuid(); 644887Schin euid = geteuid(); 654887Schin rgid = getgid(); 664887Schin egid = getegid(); 674887Schin init = (ruid == euid && rgid == egid) ? 1 : -1; 684887Schin } 694887Schin if (init > 0 || flags == F_OK) 704887Schin return access(path, flags); 714887Schin if (stat(path, &st)) 724887Schin return -1; 734887Schin mode = 0; 744887Schin if (euid == 0) 754887Schin { 764887Schin if (!S_ISREG(st.st_mode) || !(flags & X_OK) || (st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))) 774887Schin return 0; 784887Schin goto nope; 794887Schin } 804887Schin else if (euid == st.st_uid) 814887Schin { 824887Schin if (flags & R_OK) 834887Schin mode |= S_IRUSR; 844887Schin if (flags & W_OK) 854887Schin mode |= S_IWUSR; 864887Schin if (flags & X_OK) 874887Schin mode |= S_IXUSR; 884887Schin } 894887Schin else if (egid == st.st_gid) 904887Schin { 914887Schin #if _lib_getgroups 924887Schin setgroup: 934887Schin #endif 944887Schin if (flags & R_OK) 954887Schin mode |= S_IRGRP; 964887Schin if (flags & W_OK) 974887Schin mode |= S_IWGRP; 984887Schin if (flags & X_OK) 994887Schin mode |= S_IXGRP; 1004887Schin } 1014887Schin else 1024887Schin { 1034887Schin #if _lib_getgroups 1044887Schin register int n; 1054887Schin 1064887Schin static int ngroups = -2; 1074887Schin static gid_t* groups; 1084887Schin 1094887Schin if (ngroups == -2) 1104887Schin { 1114887Schin if ((ngroups = getgroups(0, (gid_t*)0)) <= 0) 1124887Schin ngroups = NGROUPS_MAX; 1134887Schin if (!(groups = newof(0, gid_t, ngroups + 1, 0))) 1144887Schin ngroups = -1; 1154887Schin else 1164887Schin ngroups = getgroups(ngroups, groups); 1174887Schin } 1184887Schin n = ngroups; 1194887Schin while (--n >= 0) 1204887Schin if (groups[n] == st.st_gid) 1214887Schin goto setgroup; 1224887Schin #endif 1234887Schin if (flags & R_OK) 1244887Schin mode |= S_IROTH; 1254887Schin if (flags & W_OK) 1264887Schin mode |= S_IWOTH; 1274887Schin if (flags & X_OK) 1284887Schin mode |= S_IXOTH; 1294887Schin } 1304887Schin if ((st.st_mode & mode) == mode) 1314887Schin return 0; 1324887Schin nope: 1334887Schin errno = EACCES; 1344887Schin return -1; 1354887Schin #endif 1364887Schin #endif 1374887Schin } 1384887Schin 1394887Schin #endif 140