]> granicus.if.org Git - sudo/blob - lib/util/gidlist.c
Add SPDX-License-Identifier to files.
[sudo] / lib / util / gidlist.c
1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2013-2015 Todd C. Miller <Todd.Miller@sudo.ws>
5  *
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.
9  *
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.
17  */
18
19 /*
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
22  */
23
24 #include <config.h>
25
26 #include <sys/types.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <grp.h>
31
32 #define DEFAULT_TEXT_DOMAIN     "sudo"
33 #include "sudo_gettext.h"       /* must be included before sudo_compat.h */
34
35 #include "sudo_compat.h"
36 #include "sudo_fatal.h"
37 #include "sudo_debug.h"
38 #include "sudo_util.h"
39
40 /*
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
43  * in the array.
44  * Returns the number of gids in the allocated array.
45  */
46 int
47 sudo_parse_gids_v1(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp)
48 {
49     int ngids = 0;
50     GETGROUPS_T *gids;
51     const char *cp = gidstr;
52     const char *errstr;
53     char *ep;
54     debug_decl(sudo_parse_gids, SUDO_DEBUG_UTIL)
55
56     /* Count groups. */
57     if (*cp != '\0') {
58         ngids++;
59         do {
60             if (*cp++ == ',')
61                 ngids++;
62         } while (*cp != '\0');
63     }
64     /* Base gid is optional. */
65     if (basegid != NULL)
66         ngids++;
67     /* Allocate and fill in array. */
68     if (ngids != 0) {
69         gids = reallocarray(NULL, ngids, sizeof(GETGROUPS_T));
70         if (gids == NULL) {
71             sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
72             debug_return_int(-1);
73         }
74         ngids = 0;
75         if (basegid != NULL)
76             gids[ngids++] = *basegid;
77         cp = gidstr;
78         do {
79             gids[ngids] = (GETGROUPS_T) sudo_strtoid(cp, ",", &ep, &errstr);
80             if (errstr != NULL) {
81                 sudo_warnx(U_("%s: %s"), cp, U_(errstr));
82                 free(gids);
83                 debug_return_int(-1);
84             }
85             if (basegid == NULL || gids[ngids] != *basegid)
86                 ngids++;
87             cp = ep + 1;
88         } while (*ep != '\0');
89         *gidsp = gids;
90     }
91     debug_return_int(ngids);
92 }