summaryrefslogtreecommitdiff
path: root/source3/lib/tldap_util.c
blob: 5f85e7a113214abccaef15b69d51c622a75f0c20 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
   Unix SMB/CIFS implementation.
   Infrastructure for async ldap client requests
   Copyright (C) Volker Lendecke 2009

   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/>.
*/

#include "includes.h"

bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
			int *num_values, DATA_BLOB **values)
{
	struct tldap_attribute *attributes;
	int i, num_attributes;

	if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
		return false;
	}

	for (i=0; i<num_attributes; i++) {
		if (strequal(attribute, attributes[i].name)) {
			break;
		}
	}
	if (i == num_attributes) {
		return false;
	}
	*num_values = attributes[i].num_values;
	*values = attributes[i].values;
	return true;
}

bool tldap_get_single_valueblob(struct tldap_message *msg,
				const char *attribute, DATA_BLOB *blob)
{
	int num_values;
	DATA_BLOB *values;

	if (attribute == NULL) {
		return NULL;
	}
	if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
		return NULL;
	}
	if (num_values != 1) {
		return NULL;
	}
	*blob = values[0];
	return true;
}

char *tldap_talloc_single_attribute(struct tldap_message *msg,
				    const char *attribute,
				    TALLOC_CTX *mem_ctx)
{
	DATA_BLOB val;
	char *result;
	size_t len;

	if (!tldap_get_single_valueblob(msg, attribute, &val)) {
		return false;
	}
	if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
				   val.data, val.length,
				   &result, &len, false)) {
		return NULL;
	}
	return result;
}

bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
		       struct dom_sid *sid)
{
	DATA_BLOB val;

	if (!tldap_get_single_valueblob(msg, attribute, &val)) {
		return false;
	}
	return sid_parse((char *)val.data, val.length, sid);
}

static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
				int num_newvals, DATA_BLOB *newvals)
{
	int num_values = talloc_array_length(mod->values);
	int i;
	DATA_BLOB *tmp;

	tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
			     num_values + num_newvals);
	if (tmp == NULL) {
		return false;
	}
	mod->values = tmp;

	for (i=0; i<num_newvals; i++) {
		mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
			mod->values, newvals[i].data, newvals[i].length);
		if (mod->values[i+num_values].data == NULL) {
			return false;
		}
		mod->values[i+num_values].length = newvals[i].length;
	}
	mod->num_values = num_values + num_newvals;
	return true;
}

bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
			 int mod_op, const char *attrib,
			 int num_newvals, DATA_BLOB *newvals)
{
	struct tldap_mod new_mod;
	struct tldap_mod *mods = *pmods;
	struct tldap_mod *mod = NULL;
	int i, num_mods;

	if (mods == NULL) {
		mods = talloc_array(mem_ctx, struct tldap_mod, 0);
	}
	if (mods == NULL) {
		return false;
	}

	num_mods = talloc_array_length(mods);

	for (i=0; i<num_mods; i++) {
		if ((mods[i].mod_op == mod_op)
		    && strequal(mods[i].attribute, attrib)) {
			mod = &mods[i];
			break;
		}
	}

	if (mod == NULL) {
		new_mod.mod_op = mod_op;
		new_mod.attribute = talloc_strdup(mods, attrib);
		if (new_mod.attribute == NULL) {
			return false;
		}
		new_mod.num_values = 0;
		new_mod.values = NULL;
		mod = &new_mod;
	}

	if ((num_newvals != 0)
	    && !tldap_add_blob_vals(mods, mod, num_newvals, newvals)) {
		return false;
	}

	if (i == num_mods) {
		mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
				      num_mods+1);
		if (mods == NULL) {
			return false;
		}
		mods[num_mods] = *mod;
	}

	*pmods = mods;
	return true;
}

bool tldap_add_mod_str(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
		       int mod_op, const char *attrib, const char *str)
{
	DATA_BLOB utf8;
	bool ret;

	if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
				   strlen(str), &utf8.data, &utf8.length,
				   false)) {
		return false;
	}

	ret = tldap_add_mod_blobs(mem_ctx, pmods, mod_op, attrib, 1, &utf8);
	TALLOC_FREE(utf8.data);
	return ret;
}

static bool tldap_make_mod_blob_int(struct tldap_message *existing,
				    TALLOC_CTX *mem_ctx,
				    int *pnum_mods, struct tldap_mod **pmods,
				    const char *attrib, DATA_BLOB newval,
				    int (*comparison)(const DATA_BLOB *d1,
						      const DATA_BLOB *d2))
{
	int num_values = 0;
	DATA_BLOB *values = NULL;
	DATA_BLOB oldval = data_blob_null;

	if ((existing != NULL)
	    && tldap_entry_values(existing, attrib, &num_values, &values)) {

		if (num_values > 1) {
			/* can't change multivalue attributes atm */
			return false;
		}
		if (num_values == 1) {
			oldval = values[0];
		}
	}

	if ((oldval.data != NULL) && (newval.data != NULL)
	    && (comparison(&oldval, &newval) == 0)) {
		/* Believe it or not, but LDAP will deny a delete and
		   an add at the same time if the values are the
		   same... */
		DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
			  "changed.\n", attrib));
		return true;
	}

	if (oldval.data != NULL) {
		/* By deleting exactly the value we found in the entry this
		 * should be race-free in the sense that the LDAP-Server will
		 * deny the complete operation if somebody changed the
		 * attribute behind our back. */
		/* This will also allow modifying single valued attributes in
		 * Novell NDS. In NDS you have to first remove attribute and
		 * then you could add new value */

		DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
			   attrib));
		if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_DELETE,
					 attrib, 1, &oldval)) {
			return false;
		}
	}

	/* Regardless of the real operation (add or modify)
	   we add the new value here. We rely on deleting
	   the old value, should it exist. */

	if (newval.data != NULL) {
		DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
			   "%d\n", attrib, (int)newval.length));
	        if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_ADD,
					 attrib, 1, &newval)) {
			return false;
		}
	}
	*pnum_mods = talloc_array_length(*pmods);
	return true;
}

bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
			 int *pnum_mods, struct tldap_mod **pmods,
			 const char *attrib, DATA_BLOB newval)
{
	return tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
				       attrib, newval, data_blob_cmp);
}

static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
{
	char *s1, *s2;
	size_t s1len, s2len;
	int ret;

	if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
				   d1->length, &s1, &s1len, false)) {
		/* can't do much here */
		return 0;
	}
	if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
				   d2->length, &s2, &s2len, false)) {
		/* can't do much here */
		TALLOC_FREE(s1);
		return 0;
	}
	ret = StrCaseCmp(s1, s2);
	TALLOC_FREE(s2);
	TALLOC_FREE(s1);
	return ret;
}

bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
			int *pnum_mods, struct tldap_mod **pmods,
			const char *attrib, const char *fmt, ...)
{
	va_list ap;
	char *newval;
	bool ret;
	DATA_BLOB blob = data_blob_null;

	va_start(ap, fmt);
	newval = talloc_vasprintf(talloc_tos(), fmt, ap);
	va_end(ap);

	if (newval == NULL) {
		return false;
	}

	blob.length = strlen(newval);
	if (blob.length != 0) {
		blob.data = CONST_DISCARD(uint8_t *, newval);
	}
	ret = tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
				      attrib, blob, compare_utf8_blobs);
	TALLOC_FREE(newval);
	return ret;
}

const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
{
	const char *ld_error = NULL;
	char *res;

	ld_error = tldap_ctx_diagnosticmessage(ld);
	res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
			      tldap_err2string(rc),
			      ld_error ? ld_error : "unknown");
	return res;
}

int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
		     const char *attrs[], int num_attrs, int attrsonly,
		     TALLOC_CTX *mem_ctx, struct tldap_message ***res,
		     const char *fmt, ...)
{
	va_list ap;
	char *filter;
	int ret;

	va_start(ap, fmt);
	filter = talloc_vasprintf(talloc_tos(), fmt, ap);
	va_end(ap);

	if (filter == NULL) {
		return TLDAP_NO_MEMORY;
	}
	ret = tldap_search(ld, base, scope, filter,
			   attrs, num_attrs, attrsonly,
			   NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
			   0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
			   mem_ctx, res, NULL);
	TALLOC_FREE(filter);
	return ret;
}

bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
		       uint64_t *presult)
{
	char *str;
	uint64_t result;

	str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
	if (str == NULL) {
		DEBUG(10, ("Could not find attribute %s\n", attr));
		return false;
	}
	result = strtoull(str, NULL, 10);
	TALLOC_FREE(str);
	*presult = result;
	return true;
}

bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
		       uint32_t *presult)
{
	uint64_t result;

	if (!tldap_pull_uint64(msg, attr, &result)) {
		return false;
	}
	*presult = (uint32_t)result;
	return true;
}

struct tldap_fetch_rootdse_state {
	struct tldap_context *ld;
	struct tldap_message *rootdse;
};

static void tldap_fetch_rootdse_done(struct tevent_req *subreq);

struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
					    struct tevent_context *ev,
					    struct tldap_context *ld)
{
	struct tevent_req *req, *subreq;
	struct tldap_fetch_rootdse_state *state;
	static const char *attrs[2] = { "*", "+" };

	req = tevent_req_create(mem_ctx, &state,
				struct tldap_fetch_rootdse_state);
	if (req == NULL) {
		return NULL;
	}
	state->ld = ld;
	state->rootdse = NULL;

	subreq = tldap_search_send(
		mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
		attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
	return req;
}

static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct tldap_fetch_rootdse_state *state = tevent_req_data(
		req, struct tldap_fetch_rootdse_state);
	struct tldap_message *msg;
	int rc;

	rc = tldap_search_recv(subreq, state, &msg);
	if (rc != TLDAP_SUCCESS) {
		TALLOC_FREE(subreq);
		tevent_req_error(req, rc);
		return;
	}

	switch (tldap_msg_type(msg)) {
	case TLDAP_RES_SEARCH_ENTRY:
		if (state->rootdse != NULL) {
			goto protocol_error;
		}
		state->rootdse = msg;
		break;
	case TLDAP_RES_SEARCH_RESULT:
		TALLOC_FREE(subreq);
		if (state->rootdse == NULL) {
			goto protocol_error;
		}
		tevent_req_done(req);
		break;
	default:
		goto protocol_error;
	}
	tevent_req_done(req);
	return;

protocol_error:
	tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
	return;
}

int tldap_fetch_rootdse_recv(struct tevent_req *req)
{
	struct tldap_fetch_rootdse_state *state = tevent_req_data(
		req, struct tldap_fetch_rootdse_state);
	int err;
	char *dn;

	if (tevent_req_is_ldap_error(req, &err)) {
		return err;
	}
	/* Trigger parsing the dn, just to make sure it's ok */
	if (!tldap_entry_dn(state->rootdse, &dn)) {
		return TLDAP_DECODING_ERROR;
	}
	if (!tldap_context_setattr(state->ld, "tldap:rootdse",
				   &state->rootdse)) {
		return TLDAP_NO_MEMORY;
	}
	return 0;
}

int tldap_fetch_rootdse(struct tldap_context *ld)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct tevent_context *ev;
	struct tevent_req *req;
	int result;

	ev = event_context_init(frame);
	if (ev == NULL) {
		result = TLDAP_NO_MEMORY;
		goto fail;
	}

	req = tldap_fetch_rootdse_send(frame, ev, ld);
	if (req == NULL) {
		result = TLDAP_NO_MEMORY;
		goto fail;
	}

	if (!tevent_req_poll(req, ev)) {
		result = TLDAP_OPERATIONS_ERROR;
		goto fail;
	}

	result = tldap_fetch_rootdse_recv(req);
 fail:
	TALLOC_FREE(frame);
	return result;
}

struct tldap_message *tldap_rootdse(struct tldap_context *ld)
{
	return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
			       struct tldap_message);
}

bool tldap_entry_has_attrvalue(struct tldap_message *msg,
			       const char *attribute,
			       const DATA_BLOB blob)
{
	int i, num_values;
	DATA_BLOB *values;

	if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
		return false;
	}
	for (i=0; i<num_values; i++) {
		if (data_blob_cmp(&values[i], &blob) == 0) {
			return true;
		}
	}
	return false;
}

bool tldap_supports_control(struct tldap_context *ld, const char *oid)
{
	struct tldap_message *rootdse = tldap_rootdse(ld);

	if (rootdse == NULL) {
		return false;
	}
	return tldap_entry_has_attrvalue(rootdse, "supportedControl",
					 data_blob_const(oid, strlen(oid)));
}