1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/* access.c -- access testing and path searching ($Revision: 1.2 $) */
#define REQUIRE_STAT 1
#define REQUIRE_PARAM 1
#include "es.h"
#include "prim.h"
#define READ 4
#define WRITE 2
#define EXEC 1
#define USER 6
#define GROUP 3
#define OTHER 0
/* ingroupset -- determine whether gid lies in the user's set of groups */
static Boolean ingroupset(int gid) {
#ifdef NGROUPS
int i;
static int ngroups;
static gidset_t gidset[NGROUPS];
static Boolean initialized = FALSE;
if (!initialized) {
initialized = TRUE;
ngroups = getgroups(NGROUPS, gidset);
}
for (i = 0; i < ngroups; i++)
if (gid == gidset[i])
return TRUE;
#endif
return FALSE;
}
static int testperm(struct stat *stat, int perm) {
int mask;
static int uid, gid;
static Boolean initialized = FALSE;
if (perm == 0)
return 0;
if (!initialized) {
initialized = TRUE;
uid = geteuid();
gid = getegid();
}
mask = (uid == 0)
? (perm << USER) | (perm << GROUP) | (perm << OTHER)
: (perm <<
((uid == stat->st_uid)
? USER
: ((gid == stat->st_gid || ingroupset(stat->st_gid))
? GROUP
: OTHER)));
return (stat->st_mode & mask) ? 0 : EACCES;
}
static int testfile(char *path, int perm, int type) {
struct stat st;
#ifdef S_IFLNK
if (type == S_IFLNK) {
if (lstat(path, &st) == -1)
return errno;
} else
#endif
if (stat(path, &st) == -1)
return errno;
if (type != 0 && (st.st_mode & S_IFMT) != type)
return EACCES; /* what is an appropriate return value? */
return testperm(&st, perm);
}
static char *pathcat(char *prefix, char *suffix) {
char *s;
size_t plen, slen, len;
static char *pathbuf = NULL;
static size_t pathlen = 0;
if (*prefix == '\0')
return suffix;
if (*suffix == '\0')
return prefix;
plen = strlen(prefix);
slen = strlen(suffix);
len = plen + slen + 2; /* one for '/', one for '\0' */
if (pathlen < len) {
pathlen = len;
pathbuf = erealloc(pathbuf, pathlen);
}
memcpy(pathbuf, prefix, plen);
s = pathbuf + plen;
if (s[-1] != '/')
*s++ = '/';
memcpy(s, suffix, slen + 1);
return pathbuf;
}
PRIM(access) {
int c, perm = 0, type = 0, estatus = ENOENT;
Boolean first = FALSE, exception = FALSE;
char *suffix = NULL;
List *lp;
const char * const usage = "access [-n name] [-1e] [-rwx] [-fdcblsp] path ...";
gcdisable();
esoptbegin(list, "$&access", usage);
while ((c = esopt("bcdefln:prswx1")) != EOF)
switch (c) {
case 'n': suffix = getstr(esoptarg()); break;
case '1': first = TRUE; break;
case 'e': exception = TRUE; break;
case 'r': perm |= READ; break;
case 'w': perm |= WRITE; break;
case 'x': perm |= EXEC; break;
case 'f': type = S_IFREG; break;
case 'd': type = S_IFDIR; break;
case 'c': type = S_IFCHR; break;
case 'b': type = S_IFBLK; break;
#ifdef S_IFLNK
case 'l': type = S_IFLNK; break;
#endif
#ifdef S_IFSOCK
case 's': type = S_IFSOCK; break;
#endif
#ifdef S_IFIFO
case 'p': type = S_IFIFO; break;
#endif
default:
esoptend();
fail("$&access", "access -%c is not supported on this system", c);
}
list = esoptend();
for (lp = NULL; list != NULL; list = list->next) {
int error;
char *name;
name = getstr(list->term);
if (suffix != NULL)
name = pathcat(name, suffix);
error = testfile(name, perm, type);
if (first) {
if (error == 0) {
Ref(List *, result,
mklist(mkstr(suffix == NULL
? name
: gcdup(name)),
NULL));
gcenable();
RefReturn(result);
} else if (error != ENOENT)
estatus = error;
} else
lp = mklist(mkstr(error == 0 ? "0" : esstrerror(error)),
lp);
}
if (first && exception) {
gcenable();
if (suffix)
fail("$&access", "%s: %s", suffix, esstrerror(estatus));
else
fail("$&access", "%s", esstrerror(estatus));
}
Ref(List *, result, reverse(lp));
gcenable();
RefReturn(result);
}
extern Dict *initprims_access(Dict *primdict) {
X(access);
return primdict;
}
extern char *checkexecutable(char *file) {
int err = testfile(file, EXEC, S_IFREG);
return err == 0 ? NULL : esstrerror(err);
}
|