aboutsummaryrefslogtreecommitdiffstats
path: root/idmapwb.c
blob: 5e8422b188e9356fe2a083dfb2788336af58c9fd (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Winbind ID Mapping Plugin
 * Copyright (C) 2012 Jeff Layton (jlayton@samba.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdint.h>
#include <endian.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <wbclient.h>
#include <limits.h>

#include "cifsidmap.h"

static const char **plugin_errmsg;

/*
 * Winbind keeps wbcDomainSid fields in host-endian. Copy fields from the
 * csid to the wsid, while converting the subauthority fields from LE.
 */
static void
csid_to_wsid(struct wbcDomainSid *wsid, const struct cifs_sid *csid)
{
	int i;
	uint8_t num_subauth = (csid->num_subauth <= WBC_MAXSUBAUTHS) ?
				csid->num_subauth : WBC_MAXSUBAUTHS;

	wsid->sid_rev_num = csid->revision;
	wsid->num_auths = num_subauth;
	for (i = 0; i < NUM_AUTHS; i++)
		wsid->id_auth[i] = csid->authority[i];
	for (i = 0; i < num_subauth; i++)
		wsid->sub_auths[i] = le32toh(csid->sub_auth[i]);
}

/*
 * Winbind keeps wbcDomainSid fields in host-endian. Copy fields from the
 * wsid to the csid, while converting the subauthority fields to LE.
 */
static void
wsid_to_csid(struct cifs_sid *csid, struct wbcDomainSid *wsid)
{
	int i;
	uint8_t num_subauth = (wsid->num_auths <= SID_MAX_SUB_AUTHORITIES) ?
				wsid->num_auths : SID_MAX_SUB_AUTHORITIES;

	csid->revision = wsid->sid_rev_num;
	csid->num_subauth = num_subauth;
	for (i = 0; i < NUM_AUTHS; i++)
		csid->authority[i] = wsid->id_auth[i];
	for (i = 0; i < num_subauth; i++)
		csid->sub_auth[i] = htole32(wsid->sub_auths[i]);
}

int
cifs_idmap_sid_to_str(void *handle __attribute__ ((unused)),
			const struct cifs_sid *csid, char **string)
{
	int rc;
	wbcErr wbcrc;
	char *domain = NULL;
	char *name = NULL;
	enum wbcSidType sntype;
	struct wbcDomainSid wsid;
	size_t len;

	csid_to_wsid(&wsid, csid);

	wbcrc = wbcLookupSid(&wsid, &domain, &name, &sntype);
	if (!WBC_ERROR_IS_OK(wbcrc)) {
		*plugin_errmsg = wbcErrorString(wbcrc);
		return -EIO;
	}

	/* +1 for '\\' and +1 for NULL terminator */
	len = strlen(domain) + 1 + strlen(name) + 1;

	*string = malloc(len);
	if (!*string) {
		*plugin_errmsg = "Unable to allocate memory";
		rc = -ENOMEM;
		goto out;
	}

	rc = snprintf(*string, len, "%s\\%s", domain, name);
	if (rc >= (long)len) {
		free(*string);
		*plugin_errmsg = "Resulting string was truncated";
		*string = NULL;
		rc = -EIO;
	} else {
		rc = 0;
	}
out:
	wbcFreeMemory(domain);
	wbcFreeMemory(name);
	return rc;
}

int
cifs_idmap_str_to_sid(void *handle __attribute__ ((unused)),
			const char *orig, struct cifs_sid *csid)
{
	wbcErr wbcrc;
	char *name, *domain, *sidstr;
	enum wbcSidType type;
	struct wbcDomainSid wsid;

	sidstr = strdup(orig);
	if (!sidstr) {
		*plugin_errmsg = "Unable to copy string";
		return -ENOMEM;
	}

	name = strchr(sidstr, '\\');
	if (!name) {
		/* might be a raw string representation of SID */
		wbcrc = wbcStringToSid(sidstr, &wsid);
		if (WBC_ERROR_IS_OK(wbcrc))
			goto convert_sid;

		domain = "";
		name = sidstr;
	} else {
		domain = sidstr;
		*name = '\0';
		++name;
	}

	wbcrc = wbcLookupName(domain, name, &wsid, &type);
	/* FIXME: map these to better POSIX error codes? */
	if (!WBC_ERROR_IS_OK(wbcrc)) {
		*plugin_errmsg = wbcErrorString(wbcrc);
		free(sidstr);
		return -EIO;
	}

convert_sid:
	wsid_to_csid(csid, &wsid);
	free(sidstr);
	return 0;
}

static void
wuxid_to_cuxid(struct cifs_uxid *cuxid, const struct wbcUnixId *wuxid)
{
	switch(wuxid->type) {
	case WBC_ID_TYPE_UID:
		cuxid->id.uid = wuxid->id.uid;
		cuxid->type = CIFS_UXID_TYPE_UID;
		break;
	case WBC_ID_TYPE_GID:
		cuxid->id.gid = wuxid->id.gid;
		cuxid->type = CIFS_UXID_TYPE_GID;
		break;
#ifdef HAVE_WBC_ID_TYPE_BOTH
	case WBC_ID_TYPE_BOTH:
		cuxid->id.uid = wuxid->id.uid;
		cuxid->type = CIFS_UXID_TYPE_BOTH;
		break;
#endif /* HAVE_WBC_ID_TYPE_BOTH */
	default:
		cuxid->type = CIFS_UXID_TYPE_UNKNOWN;
	}
}

int
cifs_idmap_sids_to_ids(void *handle __attribute__((unused)),
			const struct cifs_sid *csid, size_t num,
			struct cifs_uxid *cuxid)
{
	int ret;
	unsigned int i;
	wbcErr wbcret;
	struct wbcDomainSid *wsid;
	struct wbcUnixId *wuxid;

	if (num > UINT_MAX) {
		*plugin_errmsg = "num is too large.";
		return -EINVAL;
	}

	wsid = calloc(num, sizeof(*wsid));
	if (!wsid) {
		*plugin_errmsg = "Unable to allocate memory.";
		return -ENOMEM;
	}

	wuxid = calloc(num, sizeof(*wuxid));
	if (!wuxid) {
		*plugin_errmsg = "Unable to allocate memory.";
		ret = -ENOMEM;
		goto out;
	}

	for (i = 0; i < num; ++i)
		csid_to_wsid(&wsid[i], &csid[i]);

	/*
	 * Winbind does not set an error message in the event that some
	 * mappings fail. So, we preemptively do it here, just in case.
	 */
	*plugin_errmsg = "Some IDs could not be mapped.";

	wbcret = wbcSidsToUnixIds(wsid, num, wuxid);
	if (!WBC_ERROR_IS_OK(wbcret)) {
		*plugin_errmsg = wbcErrorString(wbcret);
		ret = -EIO;
		goto out;
	}

	ret = 0;
	for (i = 0; i < num; ++i)
		wuxid_to_cuxid(&cuxid[i], &wuxid[i]);
out:
	free(wuxid);
	free(wsid);
	return ret;
}

int
cifs_idmap_ids_to_sids(void *handle __attribute__((unused)),
			const struct cifs_uxid *cuxid, size_t num,
			struct cifs_sid *csid)
{
	int ret = -EIO;
	wbcErr wbcrc;
	size_t i;
	struct wbcDomainSid wsid;

	for (i = 0; i < num; ++i) {
		switch(cuxid[i].type) {
		case CIFS_UXID_TYPE_UID:
			wbcrc = wbcUidToSid(cuxid[i].id.uid, &wsid);
			break;
		case CIFS_UXID_TYPE_GID:
			wbcrc = wbcGidToSid(cuxid[i].id.gid, &wsid);
			break;
		case CIFS_UXID_TYPE_BOTH:
			/*
			 * In the BOTH case, prefer a user type first and fall
			 * back to a group if that doesn't map.
			 */
			wbcrc = wbcUidToSid(cuxid[i].id.uid, &wsid);
			if (WBC_ERROR_IS_OK(wbcrc))
				break;
			wbcrc = wbcGidToSid(cuxid[i].id.gid, &wsid);
			break;
		default:
			csid[i].revision = 0;
			*plugin_errmsg = "Invalid CIFS_UXID_TYPE value";
			continue;
		}

		if (WBC_ERROR_IS_OK(wbcrc)) {
			ret = 0;
			wsid_to_csid(&csid[i], &wsid);
		} else {
			csid[i].revision = 0;
			*plugin_errmsg = wbcErrorString(wbcrc);
		}
	}
	return ret;
}

/*
 * For the winbind plugin, we don't need to do anything special on
 * init or exit
 */
int
cifs_idmap_init_plugin(void **handle __attribute__((unused)), const char **errmsg)
{
	plugin_errmsg = errmsg;
	return 0;
}

void
cifs_idmap_exit_plugin(void *handle __attribute__((unused)))
{
	return;
}