Home | History | Annotate | Download | only in 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 // +build dragonfly freebsd !android,linux netbsd openbsd
      6 
      7 package user
      8 
      9 /*
     10 #include <unistd.h>
     11 #include <sys/types.h>
     12 #include <grp.h>
     13 
     14 static int mygetgrouplist(const char* user, gid_t group, gid_t* groups, int* ngroups) {
     15 	return getgrouplist(user, group, groups, ngroups);
     16 }
     17 */
     18 import "C"
     19 import (
     20 	"fmt"
     21 	"unsafe"
     22 )
     23 
     24 func getGroupList(name *C.char, userGID C.gid_t, gids *C.gid_t, n *C.int) C.int {
     25 	return C.mygetgrouplist(name, userGID, gids, n)
     26 }
     27 
     28 // groupRetry retries getGroupList with much larger size for n. The result is
     29 // stored in gids.
     30 func groupRetry(username string, name []byte, userGID C.gid_t, gids *[]C.gid_t, n *C.int) error {
     31 	// More than initial buffer, but now n contains the correct size.
     32 	if *n > maxGroups {
     33 		return fmt.Errorf("user: %q is a member of more than %d groups", username, maxGroups)
     34 	}
     35 	*gids = make([]C.gid_t, *n)
     36 	rv := getGroupList((*C.char)(unsafe.Pointer(&name[0])), userGID, &(*gids)[0], n)
     37 	if rv == -1 {
     38 		return fmt.Errorf("user: list groups for %s failed", username)
     39 	}
     40 	return nil
     41 }
     42