Changeset View
Changeset View
Standalone View
Standalone View
lib/libcasper/services/cap_fileargs/cap_fileargs.c
Show First 20 Lines • Show All 487 Lines • ▼ Show 20 Lines | if (cap_rights_limit(fd, caprightsp) < 0 && errno != ENOSYS) { | ||||
errno = serrno; | errno = serrno; | ||||
return (-1); | return (-1); | ||||
} | } | ||||
} | } | ||||
return (fd); | return (fd); | ||||
} | } | ||||
static void | static int | ||||
fileargs_add_cache(nvlist_t *nvlout, const nvlist_t *limits, | fileargs_add_cache(nvlist_t *nvlout, const nvlist_t *limits, | ||||
const char *curent_name) | const char *curent_name) | ||||
{ | { | ||||
int type, i, fd; | int type, i, fd; | ||||
void *cookie; | void *cookie; | ||||
nvlist_t *new; | nvlist_t *new; | ||||
const char *fname; | const char *fname; | ||||
struct stat sb; | struct stat sb; | ||||
fd = -1; | |||||
if ((capflags & O_CREAT) != 0) { | if ((capflags & O_CREAT) != 0) { | ||||
allcached = true; | allcached = true; | ||||
return; | return (0); | ||||
} | } | ||||
cookie = cacheposition; | cookie = cacheposition; | ||||
for (i = 0; i < CACHE_SIZE + 1; i++) { | for (i = 0; i < CACHE_SIZE + 1; i++) { | ||||
fname = nvlist_next(limits, &type, &cookie); | fname = nvlist_next(limits, &type, &cookie); | ||||
if (fname == NULL) { | if (fname == NULL) { | ||||
cacheposition = NULL; | cacheposition = NULL; | ||||
lastname = NULL; | lastname = NULL; | ||||
allcached = true; | allcached = true; | ||||
return; | return (0); | ||||
} | } | ||||
/* We doing that to catch next element name. */ | /* We doing that to catch next element name. */ | ||||
if (i == CACHE_SIZE) { | if (i == CACHE_SIZE) { | ||||
break; | break; | ||||
} | } | ||||
if (type != NV_TYPE_NULL || | if (type != NV_TYPE_NULL || | ||||
(curent_name != NULL && strcmp(fname, curent_name) == 0)) { | (curent_name != NULL && strcmp(fname, curent_name) == 0)) { | ||||
Show All 17 Lines | if ((allowed_operations & FA_LSTAT) != 0) { | ||||
i--; | i--; | ||||
nvlist_destroy(new); | nvlist_destroy(new); | ||||
continue; | continue; | ||||
} | } | ||||
nvlist_add_binary(new, "stat", &sb, sizeof(sb)); | nvlist_add_binary(new, "stat", &sb, sizeof(sb)); | ||||
} | } | ||||
nvlist_add_nvlist(nvlout, fname, new); | nvlist_add_nvlist(nvlout, fname, new); | ||||
if (nvlist_error(nvlout) != 0) { | |||||
if (fd >= 0) | |||||
close(fd); | |||||
nvlist_destroy(new); | |||||
return (nvlist_error(nvlout)); | |||||
} | } | ||||
} | |||||
cacheposition = cookie; | cacheposition = cookie; | ||||
lastname = fname; | lastname = fname; | ||||
return (0); | |||||
} | } | ||||
static bool | static bool | ||||
fileargs_allowed(const nvlist_t *limits, const nvlist_t *request, int operation) | fileargs_allowed(const nvlist_t *limits, const nvlist_t *request, int operation) | ||||
{ | { | ||||
const char *name; | const char *name; | ||||
if ((allowed_operations & operation) == 0) | if ((allowed_operations & operation) == 0) | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | fileargs_command_lstat(const nvlist_t *limits, nvlist_t *nvlin, | ||||
error = lstat(name, &sb); | error = lstat(name, &sb); | ||||
if (error < 0) | if (error < 0) | ||||
return (errno); | return (errno); | ||||
if (!allcached && (lastname == NULL || | if (!allcached && (lastname == NULL || | ||||
strcmp(name, lastname) == 0)) { | strcmp(name, lastname) == 0)) { | ||||
nvlist_add_string(nvlout, "cmd", "cache"); | nvlist_add_string(nvlout, "cmd", "cache"); | ||||
fileargs_add_cache(nvlout, limits, name); | error = fileargs_add_cache(nvlout, limits, name); | ||||
if (error != 0) | |||||
return (error); | |||||
} else { | } else { | ||||
nvlist_add_string(nvlout, "cmd", "lstat"); | nvlist_add_string(nvlout, "cmd", "lstat"); | ||||
} | } | ||||
nvlist_add_binary(nvlout, "stat", &sb, sizeof(sb)); | nvlist_add_binary(nvlout, "stat", &sb, sizeof(sb)); | ||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
fileargs_command_open(const nvlist_t *limits, nvlist_t *nvlin, | fileargs_command_open(const nvlist_t *limits, nvlist_t *nvlin, | ||||
nvlist_t *nvlout) | nvlist_t *nvlout) | ||||
{ | { | ||||
int fd; | int error, fd; | ||||
const char *name; | const char *name; | ||||
if (limits == NULL) | if (limits == NULL) | ||||
return (ENOTCAPABLE); | return (ENOTCAPABLE); | ||||
if (!fileargs_allowed(limits, nvlin, FA_OPEN)) | if (!fileargs_allowed(limits, nvlin, FA_OPEN)) | ||||
return (ENOTCAPABLE); | return (ENOTCAPABLE); | ||||
name = nvlist_get_string(nvlin, "name"); | name = nvlist_get_string(nvlin, "name"); | ||||
fd = open_file(name); | fd = open_file(name); | ||||
if (fd < 0) | if (fd < 0) | ||||
return (errno); | return (errno); | ||||
if (!allcached && (lastname == NULL || | if (!allcached && (lastname == NULL || | ||||
strcmp(name, lastname) == 0)) { | strcmp(name, lastname) == 0)) { | ||||
nvlist_add_string(nvlout, "cmd", "cache"); | nvlist_add_string(nvlout, "cmd", "cache"); | ||||
fileargs_add_cache(nvlout, limits, name); | error = fileargs_add_cache(nvlout, limits, name); | ||||
if (error != 0) { | |||||
close(fd); | |||||
return (error); | |||||
} | |||||
} else { | } else { | ||||
nvlist_add_string(nvlout, "cmd", "open"); | nvlist_add_string(nvlout, "cmd", "open"); | ||||
} | } | ||||
nvlist_move_descriptor(nvlout, "fd", fd); | nvlist_move_descriptor(nvlout, "fd", fd); | ||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
Show All 15 Lines |