~zeller-benjamin/ubuntu-sdk-tools/cmakeworkaround

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2016 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Benjamin Zeller <benjamin.zeller@canonical.com>
 */
package ubuntu_sdk_tools

/*
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <shadow.h>

char *getGrpMember (struct group *grp, size_t index)
{
    if (grp == NULL) return NULL;
    if (grp->gr_mem[index] == NULL)
    	    return NULL;
    return grp->gr_mem[index];
}

*/
import "C"
import (
	"unsafe"
	"fmt"
	"syscall"
)

type Passwd struct {
	Uid uint32
	Gid uint32
	Dir string
	Shell string
	LoginName string
}

func Getpwnam(username string) (*Passwd, error) {
	var pwd C.struct_passwd
	var result *C.struct_passwd

	bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
	if bufSize <= 0 || bufSize > 1<<20 {
		return nil, fmt.Errorf("unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
	}

	buf := C.malloc(C.size_t(bufSize))
	defer C.free(buf)
	var rv C.int

	nameC := C.CString(username)
	defer C.free(unsafe.Pointer(nameC))

	rv, err := C.getpwnam_r(nameC,
		&pwd,
		(*C.char)(buf),
		C.size_t(bufSize),
		&result)
	if rv != 0 {
		return nil, fmt.Errorf("error while looking up username %s: %s", username, syscall.Errno(rv))
	}
	if result == nil {
		if err != nil {
			return nil, err
		}
		return nil, fmt.Errorf("Unknown error while getting the user entry for %s", username)
	}

	return &Passwd{
		Uid: uint32(pwd.pw_uid),
		Gid: uint32(pwd.pw_uid),
		Dir: C.GoString(pwd.pw_dir),
		Shell: C.GoString(pwd.pw_shell),
		LoginName: C.GoString(pwd.pw_name)}, nil
}

type GroupEntry struct {
	Gid uint32
	Name string
	Members []string
}

func GetGroups() ([]GroupEntry, error){
	var grp C.struct_group
	var result *C.struct_group
	allGroups := make([]GroupEntry, 0)

	//_SC_GETGR_R_SIZE_MAX
	bufSize := C.sysconf(C._SC_GETGR_R_SIZE_MAX)
	if bufSize <= 0 || bufSize > 1<<20 {
		return nil, fmt.Errorf("unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
	}
	buf := C.malloc(C.size_t(bufSize))
	defer C.free(buf)

	for {
		result = nil
		res, err := C.getgrent_r(&grp, (*C.char)(buf), C.size_t(bufSize), &result)
		if res != 0 {
			if err != nil {
				return nil, err
			}
			break
		}

		currEntry := GroupEntry{}
		currEntry.Gid = uint32(result.gr_gid)
		currEntry.Name = C.GoString(result.gr_name)

		idx := 0
		for {
			cMember := C.getGrpMember(result, C.size_t(idx))
			if cMember == nil {
				break
			}
			currEntry.Members = append(currEntry.Members, C.GoString(cMember))
			idx++
		}
		allGroups = append(allGroups, currEntry)
	}

	return allGroups, nil
}

type SPasswd struct {
	Sp_namp string   /* Login name */
	Sp_pwdp string   /* Encrypted password */
}

func Getspnam (username string) (*SPasswd, error) {
	var spwd C.struct_spwd
	var result *C.struct_spwd

	bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
	if bufSize <= 0 || bufSize > 1<<20 {
		return nil, fmt.Errorf("unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
	}

	buf := C.malloc(C.size_t(bufSize))
	defer C.free(buf)
	var rv C.int

	nameC := C.CString(username)
	defer C.free(unsafe.Pointer(nameC))

	rv, err := C.getspnam_r(nameC,
		&spwd,
		(*C.char)(buf),
		C.size_t(bufSize),
		&result)
	if rv != 0 {
		return nil, fmt.Errorf("error while looking up passwd for %s: %s", username, syscall.Errno(rv))
	}
	if result == nil {
		if err != nil {
			return nil, err
		}
		return nil, fmt.Errorf("Unknown error while getting the password entry for %s", username)
	}

	return &SPasswd{
		Sp_namp:C.GoString(result.sp_namp),
		Sp_pwdp:C.GoString(result.sp_pwdp),
	}, nil
}