summaryrefslogtreecommitdiff
path: root/getgr/getgr.go
blob: 4dd6f21e742d707d748f363b9ef57b8345d4b0af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2015-2016 Luke Shumaker <lukeshu@sbcglobal.net>.
//
// This is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this manual; if not, see
// <http://www.gnu.org/licenses/>.

// Package getgr provides an interface to query the POSIX group
// database.
//
// BUG(lukeshu) This package should be renamed to "nss" and expanded.
package getgr

import (
	"unsafe"

	"golang.org/x/sys/unix"
)

//#define _POSIX_SOURCE // for getgrnam_r(3) in grp.h
//#include <stdlib.h>   // for free(3)
//#include <unistd.h>   // for sysconf(3)
//#include <grp.h>      // for getgrnam_r(3)
//static char *strary(char **ary, unsigned int n) { return ary[n]; }
import "C"

type Gid uint

type Group struct {
	Name   string
	Passwd string
	Gid    Gid
	Mem    []string
}

func strary(c **C.char) (g []string) {
	g = make([]string, 0)
	for i := C.uint(0); true; i++ {
		cstr := C.strary(c, i)
		if cstr == nil {
			return
		}
		g = append(g, C.GoString(cstr))
	}
	panic("not reached")
}

// A wrapper around C getgrnam_r
func ByName(name string) (*Group, error) {
	nameC := C.CString(name)
	defer C.free(unsafe.Pointer(nameC))

	buflen := C.sysconf(C._SC_GETGR_R_SIZE_MAX)
	if buflen < 1 {
		buflen = 1024
	}
	buf := make([]byte, buflen)
	var grp C.struct_group
	var ret *C.struct_group
	for {
		success, errno := C.getgrnam_r(nameC, &grp, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(buflen), &ret)
		if success >= 0 {
			break
		}
		if errno == unix.ERANGE {
			buflen += 256
			buf = make([]byte, buflen)
		} else {
			return nil, errno
		}
	}
	if ret == nil {
		return nil, nil
	}
	return &Group{
		Name:   C.GoString(ret.gr_name),
		Passwd: C.GoString(ret.gr_passwd),
		Gid:    Gid(ret.gr_gid),
		Mem:    strary(ret.gr_mem),
	}, nil
}

// A wrapper around C getgrgid_r
func ByGid(gid int32) (*Group, error) {
	gidC := C.__gid_t(gid)

	buflen := C.sysconf(C._SC_GETGR_R_SIZE_MAX)
	if buflen < 1 {
		buflen = 1024
	}
	buf := make([]byte, buflen)
	var grp C.struct_group
	var ret *C.struct_group
	for {
		success, errno := C.getgrgid_r(gidC, &grp, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(buflen), &ret)
		if success >= 0 {
			break
		}
		if errno == unix.ERANGE {
			buflen += 256
			buf = make([]byte, buflen)
		} else {
			return nil, errno
		}
	}
	if ret == nil {
		return nil, nil
	}
	return &Group{
		Name:   C.GoString(ret.gr_name),
		Passwd: C.GoString(ret.gr_passwd),
		Gid:    Gid(ret.gr_gid),
		Mem:    strary(ret.gr_mem),
	}, nil
}