]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
nss/libsubid: simplify the ranges variable for list_owner_ranges 340/head
authorSerge Hallyn <serge@hallyn.com>
Sat, 22 May 2021 17:16:50 +0000 (12:16 -0500)
committerSerge Hallyn <serge@hallyn.com>
Sat, 22 May 2021 22:59:57 +0000 (17:59 -0500)
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>
configure.ac
lib/prototypes.h
lib/subordinateio.c
lib/subordinateio.h
libsubid/api.c
libsubid/subid.h
src/list_subid_ranges.c
tests/libsubid/04_nss/libsubid_zzz.c

index 7f7e87841f5792f2a491dadf5a751426c9b09627..010072c7ff3f6ab733fef3b51a2cb11c50111663 100644 (file)
@@ -1,6 +1,6 @@
 dnl Process this file with autoconf to produce a configure script.
 AC_PREREQ([2.69])
-m4_define([libsubid_abi_major], 2)
+m4_define([libsubid_abi_major], 3)
 m4_define([libsubid_abi_minor], 0)
 m4_define([libsubid_abi_micro], 0)
 m4_define([libsubid_abi], [libsubid_abi_major.libsubid_abi_minor.libsubid_abi_micro])
index 077cd3cbeb05705978657784fbcbda21100327f1..388a98f889005839045407aea8cbaa651a64a283 100644 (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.
index f8dd6691056dd0fed3fb41a938769ca3bd538068..a5214849699ab4b9f312908efe550cdaaec0222e 100644 (file)
 #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;
index 1a8f4d5ed575b3d9bc392627beec98ac937e8940..5644d051f5941e06de3f1c0f4dd8497e1934f117 100644 (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);
index 1383a9487072790c855273936a17aba5044f8eb7..ec781eec0bfbc761f62a15fd8467892335738774 100644 (file)
@@ -66,17 +66,17 @@ bool libsubid_init(const char *progname, FILE * logfd)
 }
 
 static
-int get_subid_ranges(const char *owner, enum subid_type id_type, struct subid_range ***ranges)
+int get_subid_ranges(const char *owner, enum subid_type id_type, struct subid_range **ranges)
 {
        return list_owner_ranges(owner, id_type, ranges);
 }
 
-int get_subuid_ranges(const char *owner, struct subid_range ***ranges)
+int get_subuid_ranges(const char *owner, struct subid_range **ranges)
 {
        return get_subid_ranges(owner, ID_TYPE_UID, ranges);
 }
 
-int get_subgid_ranges(const char *owner, struct subid_range ***ranges)
+int get_subgid_ranges(const char *owner, struct subid_range **ranges)
 {
        return get_subid_ranges(owner, ID_TYPE_GID, ranges);
 }
index 62972a439df3ab3943a0f016ea9021cd48d43df6..5fef2572db500ad56b71e830d4b054533a7ebac5 100644 (file)
@@ -50,32 +50,27 @@ bool libsubid_init(const char *progname, FILE *logfd);
  * get_subuid_ranges: return a list of UID ranges for a user
  *
  * @owner: username being queried
- * @ranges: a pointer to a subordinate range ** in which the result will be
- *          returned.
+ * @ranges: a pointer to an array of subid_range structs in which the result
+ *          will be returned.
+ *
+ * The caller must free(ranges) when done.
  *
  * returns: number of ranges found, ir < 0 on error.
  */
-int get_subuid_ranges(const char *owner, struct subid_range ***ranges);
+int get_subuid_ranges(const char *owner, struct subid_range **ranges);
 
 /*
  * get_subgid_ranges: return a list of GID ranges for a user
  *
  * @owner: username being queried
- * @ranges: a pointer to a subordinate range ** in which the result will be
- *          returned.
+ * @ranges: a pointer to an array of subid_range structs in which the result
+ *          will be returned.
  *
- * returns: number of ranges found, ir < 0 on error.
- */
-int get_subgid_ranges(const char *owner, struct subid_range ***ranges);
-
-/*
- * subid_free_ranges: free an array of subordinate_ranges returned by either
- *                    get_subuid_ranges() or get_subgid_ranges().
+ * The caller must free(ranges) when done.
  *
- * @ranges: the ranges to free
- * @count: the number of ranges in @ranges
+ * returns: number of ranges found, ir < 0 on error.
  */
-void subid_free_ranges(struct subid_range **ranges, int count);
+int get_subgid_ranges(const char *owner, struct subid_range **ranges);
 
 /*
  * get_subuid_owners: return a list of uids to which the given uid has been
index 34abd82ca41eb979a4f1db9a0456ee618f71206e..f649a0027034f8a5a9f2ec2c824eb505d4239528 100644 (file)
@@ -17,7 +17,7 @@ void usage(void)
 int main(int argc, char *argv[])
 {
        int i, count=0;
-       struct subid_range **ranges;
+       struct subid_range *ranges;
        const char *owner;
 
        Prog = Basename (argv[0]);
@@ -39,8 +39,7 @@ int main(int argc, char *argv[])
        }
        for (i = 0; i < count; i++) {
                printf("%d: %s %lu %lu\n", i, owner,
-                       ranges[i]->start, ranges[i]->count);
+                       ranges[i].start, ranges[i].count);
        }
-       subid_free_ranges(ranges, count);
        return 0;
 }
index d21372781f4ef6f9bc5a1a6929574ff1bc805357..d9ca949f98a4239f84b23ed5e0bcf87a1fdc82f7 100644 (file)
@@ -101,9 +101,9 @@ enum subid_status shadow_subid_find_subid_owners(unsigned long id, enum subid_ty
        return SUBID_STATUS_SUCCESS;
 }
 
-enum subid_status shadow_subid_list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_range ***in_ranges, int *count)
+enum subid_status shadow_subid_list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_range **in_ranges, int *count)
 {
-       struct subid_range **ranges;
+       struct subid_range *ranges;
 
        *count = 0;
        if (strcmp(owner, "error") == 0)
@@ -113,7 +113,7 @@ enum subid_status shadow_subid_list_owner_ranges(const char *owner, enum subid_t
        if (strcmp(owner, "conn") == 0)
                return SUBID_STATUS_ERROR_CONN;
 
-       *ranges = NULL;
+       *in_ranges = NULL;
        if (strcmp(owner, "user1") != 0 && strcmp(owner, "ubuntu") != 0 &&
            strcmp(owner, "group1") != 0)
                return SUBID_STATUS_SUCCESS;
@@ -121,21 +121,15 @@ enum subid_status shadow_subid_list_owner_ranges(const char *owner, enum subid_t
                return SUBID_STATUS_SUCCESS;
        if (id_type == ID_TYPE_UID && strcmp(owner, "group1") == 0)
                return SUBID_STATUS_SUCCESS;
-       ranges = (struct subid_range **)malloc(sizeof(struct subid_range *));
+       ranges = (struct subid_range *)malloc(sizeof(struct subid_range));
        if (!*ranges)
                return SUBID_STATUS_ERROR;
-       ranges[0] = (struct subid_range *)malloc(sizeof(struct subid_range));
-       if (!ranges[0]) {
-               free(*ranges);
-               *ranges = NULL;
-               return SUBID_STATUS_ERROR;
-       }
        if (strcmp(owner, "user1") == 0 || strcmp(owner, "group1") == 0) {
-               ranges[0]->start = 100000;
-               ranges[0]->count = 65536;
+               ranges[0].start = 100000;
+               ranges[0].count = 65536;
        } else {
-               ranges[0]->start = 200000;
-               ranges[0]->count = 100000;
+               ranges[0].start = 200000;
+               ranges[0].count = 100000;
        }
 
        *count = 1;