summaryrefslogtreecommitdiffstats
path: root/nsswitch/libwbclient/wbc_sid_sssd.c
blob: 28d181c56641698a07d7734d40de190988d027d9 (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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
   Unix SMB/CIFS implementation.

   Winbind client API - SSSD version

   Copyright (C) Sumit Bose <sbose@redhat.com> 2014

   This library 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 3 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Required Headers */

#include <sss_nss_idmap.h>

#include "replace.h"
#include "libwbclient.h"
#include "../winbind_client.h"
#include "wbc_sssd_internal.h"

static int sss_id_type_to_wbcSidType(enum sss_id_type sss_type,
                                     enum wbcSidType *name_type)
{
	switch (sss_type) {
	case SSS_ID_TYPE_NOT_SPECIFIED:
		*name_type = WBC_SID_NAME_USE_NONE;
		break;
	case SSS_ID_TYPE_UID:
	case SSS_ID_TYPE_BOTH:
		*name_type = WBC_SID_NAME_USER;
		break;
	case SSS_ID_TYPE_GID:
		*name_type = WBC_SID_NAME_DOM_GRP;
		break;
	default:
		return EINVAL;
	}

	return 0;
};

/* Convert a domain and name to SID */
wbcErr wbcLookupName(const char *domain,
		     const char *name,
		     struct wbcDomainSid *sid,
		     enum wbcSidType *name_type)
{
	char *fq_name = NULL;
	char *str_sid;
	enum sss_id_type type;
	int ret;
	wbcErr wbc_status;

	ret = asprintf(&fq_name, "%s@%s", name, domain);
	if (ret == -1) {
		return WBC_ERR_NO_MEMORY;
	}

	ret = sss_nss_getsidbyname(fq_name, &str_sid, &type);
	free(fq_name);
	if (ret != 0) {
		return WBC_ERR_UNKNOWN_FAILURE;
	}

	ret = sss_id_type_to_wbcSidType(type, name_type);
	if (ret != 0) {
		return WBC_ERR_UNKNOWN_FAILURE;
	}

	wbc_status = wbcStringToSid(str_sid, sid);
	free(str_sid);
	if (!WBC_ERROR_IS_OK(wbc_status)) {
		return wbc_status;
	}

	return WBC_ERR_SUCCESS;
}


/* Convert a SID to a domain and name */
wbcErr wbcLookupSid(const struct wbcDomainSid *sid,
		    char **pdomain,
		    char **pname,
		    enum wbcSidType *pname_type)
{
	char *str_sid;
	char *fq_name = NULL;
	enum sss_id_type type;
	int ret;
	char *p;
	wbcErr wbc_status;

	wbc_status = wbcSidToString(sid, &str_sid);
	if (!WBC_ERROR_IS_OK(wbc_status)) {
		return wbc_status;
	}

	ret = sss_nss_getnamebysid(str_sid, &fq_name, &type);
	wbcFreeMemory(str_sid);
	if (ret != 0) {
		return WBC_ERR_UNKNOWN_FAILURE;
	}

	ret = sss_id_type_to_wbcSidType(type, pname_type);
	if (ret != 0) {
		wbc_status = WBC_ERR_UNKNOWN_FAILURE;
		goto done;
	}

	/* FIXME: it would be nice to have a sss_nss_getnamebysid() call which
	* return name and domain separately. */
	p = strchr(fq_name, '@');
	if (p == NULL) {
		wbc_status = WBC_ERR_UNKNOWN_FAILURE;
		goto done;
	}

	*p = '\0';
	*pname = wbcStrDup(fq_name);
	if (*pname == NULL) {
		wbc_status = WBC_ERR_NO_MEMORY;
		goto done;
	}

	*pdomain = wbcStrDup(p + 1);
	if (*pdomain == NULL) {
		wbc_status = WBC_ERR_NO_MEMORY;
		goto done;
	}

	wbc_status = WBC_ERR_SUCCESS;
done:
	free(fq_name);
	return wbc_status;
}

wbcErr wbcLookupSids(const struct wbcDomainSid *sids, int num_sids,
		     struct wbcDomainInfo **pdomains, int *pnum_domains,
		     struct wbcTranslatedName **pnames)
{
	WBC_SSSD_NOT_IMPLEMENTED;
}

/* Translate a collection of RIDs within a domain to names */

wbcErr wbcLookupRids(struct wbcDomainSid *dom_sid,
		     int num_rids,
		     uint32_t *rids,
		     const char **pp_domain_name,
		     const char ***pnames,
		     enum wbcSidType **ptypes)
{
	struct wbcDomainSid obj_sid;
	size_t c;
	wbcErr err;
	char *domain;
	char *name;
	enum wbcSidType type;
	const char **names = NULL;
	enum wbcSidType *types = NULL;

	memset(&obj_sid, 0, sizeof(obj_sid));

	obj_sid.sid_rev_num = dom_sid->sid_rev_num;
	obj_sid.num_auths = dom_sid->num_auths + 1;
	for (c = 0; c < 6; c++) {
		obj_sid.id_auth[c] = dom_sid->id_auth[c];
	}
	for (c = 0; c < WBC_MAXSUBAUTHS; c++) {
		obj_sid.sub_auths[c] = dom_sid->sub_auths[c];
	}

	names = wbcAllocateStringArray(num_rids + 1);
	if (names == NULL) {
		err = WBC_ERR_NO_MEMORY;
		goto done;
	}

	types = wbcAllocateMemory(num_rids + 1, sizeof(enum wbcSidType), NULL);
	if (types == NULL) {
		err = WBC_ERR_NO_MEMORY;
		goto done;
	}

	for (c = 0; c < num_rids; c++) {
		obj_sid.sub_auths[obj_sid.num_auths - 1] = rids[c];

		err = wbcLookupSid(&obj_sid, &domain, &name, &type);
		if (err != WBC_ERR_SUCCESS) {
			goto done;
		}

		names[c] = strdup(name);
		wbcFreeMemory(name);
		if (names[c] == NULL) {
			err = WBC_ERR_NO_MEMORY;
			goto done;
		}
		types[c] = type;

		if (c == 0) {
			*pp_domain_name = domain;
		} else {
			wbcFreeMemory(domain);
		}
	}

	*pnames = names;
	*ptypes = types;

	err = WBC_ERR_SUCCESS;

done:
	if (err != WBC_ERR_SUCCESS) {
		wbcFreeMemory(types);
		wbcFreeMemory(names);
	}

	return err;
}

/* Get the groups a user belongs to */
wbcErr wbcLookupUserSids(const struct wbcDomainSid *user_sid,
			 bool domain_groups_only,
			 uint32_t *num_sids,
			 struct wbcDomainSid **_sids)
{
	WBC_SSSD_NOT_IMPLEMENTED;
}

/* Get alias membership for sids */
wbcErr wbcGetSidAliases(const struct wbcDomainSid *dom_sid,
			struct wbcDomainSid *sids,
			uint32_t num_sids,
			uint32_t **alias_rids,
			uint32_t *num_alias_rids)
{
	WBC_SSSD_NOT_IMPLEMENTED;
}


/* Lists Users */
wbcErr wbcListUsers(const char *domain_name,
		    uint32_t *_num_users,
		    const char ***_users)
{
	WBC_SSSD_NOT_IMPLEMENTED;
}

/* Lists Groups */
wbcErr wbcListGroups(const char *domain_name,
		     uint32_t *_num_groups,
		     const char ***_groups)
{
	WBC_SSSD_NOT_IMPLEMENTED;
}

wbcErr wbcGetDisplayName(const struct wbcDomainSid *sid,
			 char **pdomain,
			 char **pfullname,
			 enum wbcSidType *pname_type)
{
	WBC_SSSD_NOT_IMPLEMENTED;
}