2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2013-2015 Todd C. Miller <Todd.Miller@sudo.ws>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
21 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
26 #include <sys/types.h>
32 #define DEFAULT_TEXT_DOMAIN "sudo"
33 #include "sudo_gettext.h" /* must be included before sudo_compat.h */
35 #include "sudo_compat.h"
36 #include "sudo_fatal.h"
37 #include "sudo_debug.h"
38 #include "sudo_util.h"
41 * Parse a comma-separated list of gids into an allocated array of GETGROUPS_T.
42 * If a pointer to the base gid is specified, it is stored as the first element
44 * Returns the number of gids in the allocated array.
47 sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp)
51 const char *cp = gidstr;
54 debug_decl(sudo_parse_gids, SUDO_DEBUG_UTIL)
62 } while (*cp != '\0');
64 /* Base gid is optional. */
67 /* Allocate and fill in array. */
69 gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T));
71 sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
76 gids[ngids++] = *basegid;
79 gids[ngids] = (GETGROUPS_T) sudo_strtoid(cp, ",", &ep, &errstr);
81 sudo_warnx(U_("%s: %s"), cp, U_(errstr));
85 if (basegid == NULL || gids[ngids] != *basegid)
88 } while (*ep != '\0');
91 debug_return_int(ngids);