nss/libsubid: simplify the ranges variable for list_owner_ranges

Following alexey-tikhonov's suggestion.

Since we've dropped the 'owner' field in the data returned for
get_subid_ranges, we can just return a single allocated array of
simple structs.  This means we can return a ** instead of ***, and
we can get rid of the subid_free_ranges() helper, since the caller
can just free() the returned data.

Signed-off-by: Serge Hallyn <serge@hallyn.com>
This commit is contained in:
Serge Hallyn
2021-05-22 12:16:50 -05:00
parent 322db32971
commit 3d670ba7ed
8 changed files with 39 additions and 74 deletions

View File

@@ -300,16 +300,15 @@ struct subid_nss_ops {
*
* @owner - string representing username being queried
* @id_type - subuid or subgid
* @ranges - pointer to an array of struct subid_range pointers, or
* NULL. The returned array of struct subid_range and its
* members must be freed by the caller.
* @ranges - pointer to an array of struct subid_range, or NULL. The
* returned array must be freed by the caller.
* @count - pointer to an integer into which the number of returned ranges
* is written.
* returns success if the module was able to determine an answer,
* else an error status.
*/
enum subid_status (*list_owner_ranges)(const char *owner, enum subid_type id_type, struct subid_range ***ranges, int *count);
enum subid_status (*list_owner_ranges)(const char *owner, enum subid_type id_type, struct subid_range **ranges, int *count);
/*
* nss_find_subid_owners: find uids who own a given subuid or subgid.

View File

@@ -17,23 +17,6 @@
#include <ctype.h>
#include <fcntl.h>
/* subid_free_ranges: free a subid_range
*
* @ranges: an array of subid_ranges to free
* @count: number of items in the array
*
* The subid_range is a subordinate_range without the owner field,
* defined in subid.h
*/
void subid_free_ranges(struct subid_range **ranges, int count)
{
int i;
for (i = 0; i < count; i++)
free(ranges[i]);
free(ranges);
}
/*
* subordinate_dup: create a duplicate range
*
@@ -326,26 +309,21 @@ static bool have_range(struct commonio_db *db,
return false;
}
static bool append_range(struct subid_range ***ranges, const struct subordinate_range *new, int n)
static bool append_range(struct subid_range **ranges, const struct subordinate_range *new, int n)
{
struct subid_range *tmp;
if (!*ranges) {
*ranges = malloc(sizeof(struct subid_range *));
*ranges = malloc(sizeof(struct subid_range));
if (!*ranges)
return false;
} else {
struct subid_range **new;
new = realloc(*ranges, (n + 1) * (sizeof(struct subid_range *)));
if (!new)
struct subid_range *alloced;
alloced = realloc(*ranges, (n + 1) * (sizeof(struct subid_range)));
if (!alloced)
return false;
*ranges = new;
*ranges = alloced;
}
(*ranges)[n] = NULL;
tmp = malloc(sizeof(*tmp));
if (!tmp)
return false;
memcpy(tmp, new, sizeof(*tmp));
(*ranges)[n] = tmp;
(*ranges)[n].start = new->start;
(*ranges)[n].count = new->count;
return true;
}
@@ -804,10 +782,10 @@ gid_t sub_gid_find_free_range(gid_t min, gid_t max, unsigned long count)
*
* The caller must free the subordinate range list.
*/
int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_range ***in_ranges)
int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_range **in_ranges)
{
// TODO - need to handle owner being either uid or username
struct subid_range **ranges = NULL;
struct subid_range *ranges = NULL;
const struct subordinate_range *range;
struct commonio_db *db;
enum subid_status status;
@@ -845,7 +823,7 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
while ((range = commonio_next(db)) != NULL) {
if (0 == strcmp(range->owner, owner)) {
if (!append_range(&ranges, range, count++)) {
subid_free_ranges(ranges, count-1);
free(ranges);
ranges = NULL;
count = -1;
goto out;

View File

@@ -25,7 +25,7 @@ extern int sub_uid_unlock (void);
extern int sub_uid_add (const char *owner, uid_t start, unsigned long count);
extern int sub_uid_remove (const char *owner, uid_t start, unsigned long count);
extern uid_t sub_uid_find_free_range(uid_t min, uid_t max, unsigned long count);
extern int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_range ***ranges);
extern int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_range **ranges);
extern bool new_subid_range(struct subordinate_range *range, enum subid_type id_type, bool reuse);
extern bool release_subid_range(struct subordinate_range *range, enum subid_type id_type);
extern int find_subid_owners(unsigned long id, enum subid_type id_type, uid_t **uids);