Annotation of embedaddon/sudo/common/gidlist.c, revision 1.1.1.2

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"
1.1.1.2 ! misho      33: #include "gettext.h"           /* must be included before missing.h */
1.1       misho      34: 
                     35: #include "missing.h"
                     36: #include "alloc.h"
                     37: #include "fatal.h"
                     38: #include "sudo_debug.h"
1.1.1.2 ! misho      39: #include "sudo_util.h"
1.1       misho      40: 
                     41: /*
                     42:  * Parse a comma-separated list of gids into an allocated array of GETGROUPS_T.
                     43:  * If a pointer to the base gid is specified, it is stored as the first element
                     44:  * in the array.
                     45:  * Returns the number of gids in the allocated array.
                     46:  * Calls fatalx() on error.
                     47:  */
                     48: int
                     49: parse_gid_list(const char *gidstr, const gid_t *basegid, GETGROUPS_T **gidsp)
                     50: {
                     51:     int ngids = 0;
                     52:     GETGROUPS_T *gids;
                     53:     const char *cp = gidstr;
                     54:     const char *errstr;
                     55:     char *ep;
                     56:     debug_decl(atoid, SUDO_DEBUG_UTIL)
                     57: 
                     58:     /* Count groups. */
                     59:     if (*cp != '\0') {
                     60:        ngids++;
                     61:        do {
                     62:            if (*cp++ == ',')
                     63:                ngids++;
                     64:        } while (*cp != '\0');
                     65:     }
                     66:     /* Base gid is optional. */
                     67:     if (basegid != NULL)
                     68:        ngids++;
                     69:     /* Allocate and fill in array. */
                     70:     if (ngids != 0) {
                     71:        gids = emalloc2(ngids, sizeof(GETGROUPS_T));
                     72:        ngids = 0;
                     73:        if (basegid != NULL)
                     74:            gids[ngids++] = *basegid;
                     75:        cp = gidstr;
                     76:        do {
                     77:            gids[ngids] = (GETGROUPS_T) atoid(cp, ",", &ep, &errstr);
                     78:            if (errstr != NULL) {
1.1.1.2 ! misho      79:                warningx(U_("%s: %s"), cp, U_(errstr));
1.1       misho      80:                free(gids);
                     81:                debug_return_int(-1);
                     82:            }
                     83:            if (basegid == NULL || gids[ngids] != *basegid)
                     84:                ngids++;
                     85:            cp = ep + 1;
                     86:        } while (*ep != '\0');
                     87:        *gidsp = gids;
                     88:     }
                     89:     debug_return_int(ngids);
                     90: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>