Annotation of embedaddon/sudo/common/gidlist.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (c) 2013 Todd C. Miller <Todd.Miller@courtesan.com>
! 3: *
! 4: * Permission to use, copy, modify, and distribute this software for any
! 5: * purpose with or without fee is hereby granted, provided that the above
! 6: * copyright notice and this permission notice appear in all copies.
! 7: *
! 8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 15: */
! 16:
! 17: #include <config.h>
! 18:
! 19: #include <sys/types.h>
! 20:
! 21: #include <stdio.h>
! 22: #ifdef STDC_HEADERS
! 23: # include <stdlib.h>
! 24: # include <stddef.h>
! 25: #else
! 26: # ifdef HAVE_STDLIB_H
! 27: # include <stdlib.h>
! 28: # endif
! 29: #endif /* STDC_HEADERS */
! 30: #include <grp.h>
! 31:
! 32: #define DEFAULT_TEXT_DOMAIN "sudo"
! 33: #include "gettext.h"
! 34:
! 35: #include "missing.h"
! 36: #include "alloc.h"
! 37: #include "fatal.h"
! 38: #include "sudo_debug.h"
! 39:
! 40: extern id_t atoid(const char *p, const char *sep, char **endp, const char **errstr);
! 41:
! 42: /*
! 43: * Parse a comma-separated list of gids into an allocated array of GETGROUPS_T.
! 44: * If a pointer to the base gid is specified, it is stored as the first element
! 45: * in the array.
! 46: * Returns the number of gids in the allocated array.
! 47: * Calls fatalx() on error.
! 48: */
! 49: int
! 50: parse_gid_list(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp)
! 51: {
! 52: int ngids = 0;
! 53: GETGROUPS_T *gids;
! 54: const char *cp = gidstr;
! 55: const char *errstr;
! 56: char *ep;
! 57: debug_decl(atoid, SUDO_DEBUG_UTIL)
! 58:
! 59: /* Count groups. */
! 60: if (*cp != '\0') {
! 61: ngids++;
! 62: do {
! 63: if (*cp++ == ',')
! 64: ngids++;
! 65: } while (*cp != '\0');
! 66: }
! 67: /* Base gid is optional. */
! 68: if (basegid != NULL)
! 69: ngids++;
! 70: /* Allocate and fill in array. */
! 71: if (ngids != 0) {
! 72: gids = emalloc2(ngids, sizeof(GETGROUPS_T));
! 73: ngids = 0;
! 74: if (basegid != NULL)
! 75: gids[ngids++] = *basegid;
! 76: cp = gidstr;
! 77: do {
! 78: gids[ngids] = (GETGROUPS_T) atoid(cp, ",", &ep, &errstr);
! 79: if (errstr != NULL) {
! 80: warningx(_("%s: %s"), cp, _(errstr));
! 81: free(gids);
! 82: debug_return_int(-1);
! 83: }
! 84: if (basegid == NULL || gids[ngids] != *basegid)
! 85: ngids++;
! 86: cp = ep + 1;
! 87: } while (*ep != '\0');
! 88: *gidsp = gids;
! 89: }
! 90: debug_return_int(ngids);
! 91: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>