Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/user/getgrouplist_darwin.go

Documentation: os/user

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build cgo && !osusergo
     6  // +build cgo,!osusergo
     7  
     8  package user
     9  
    10  /*
    11  #include <unistd.h>
    12  #include <sys/types.h>
    13  #include <stdlib.h>
    14  
    15  static int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) {
    16  	int* buf = malloc(*ngroups * sizeof(int));
    17  	int rv = getgrouplist(user, (int) group, buf, ngroups);
    18  	int i;
    19  	if (rv == 0) {
    20  		for (i = 0; i < *ngroups; i++) {
    21  			groups[i] = (gid_t) buf[i];
    22  		}
    23  	}
    24  	free(buf);
    25  	return rv;
    26  }
    27  */
    28  import "C"
    29  import (
    30  	"fmt"
    31  	"unsafe"
    32  )
    33  
    34  func getGroupList(name *C.char, userGID C.gid_t, gids *C.gid_t, n *C.int) C.int {
    35  	return C.mygetgrouplist(name, userGID, gids, n)
    36  }
    37  
    38  // groupRetry retries getGroupList with an increasingly large size for n. The
    39  // result is stored in gids.
    40  func groupRetry(username string, name []byte, userGID C.gid_t, gids *[]C.gid_t, n *C.int) error {
    41  	*n = C.int(256 * 2)
    42  	for {
    43  		*gids = make([]C.gid_t, *n)
    44  		rv := getGroupList((*C.char)(unsafe.Pointer(&name[0])), userGID, &(*gids)[0], n)
    45  		if rv >= 0 {
    46  			// n is set correctly
    47  			break
    48  		}
    49  		if *n > maxGroups {
    50  			return fmt.Errorf("user: %q is a member of more than %d groups", username, maxGroups)
    51  		}
    52  		*n = *n * C.int(2)
    53  	}
    54  	return nil
    55  }
    56  

View as plain text