2 Unix SMB/CIFS implementation.
3 Infrastructure for async ldap client requests
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "tldap_util.h"
23 #include "../libcli/security/security.h"
25 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
26 DATA_BLOB **values, int *num_values)
28 struct tldap_attribute *attributes;
29 int i, num_attributes;
31 if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
35 for (i=0; i<num_attributes; i++) {
36 if (strequal(attribute, attributes[i].name)) {
40 if (i == num_attributes) {
43 *num_values = attributes[i].num_values;
44 *values = attributes[i].values;
48 bool tldap_get_single_valueblob(struct tldap_message *msg,
49 const char *attribute, DATA_BLOB *blob)
54 if (attribute == NULL) {
57 if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
60 if (num_values != 1) {
67 char *tldap_talloc_single_attribute(struct tldap_message *msg,
68 const char *attribute,
75 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
78 if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
80 &result, &len, false)) {
86 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
91 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
94 return sid_parse((char *)val.data, val.length, sid);
97 bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
102 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
105 return NT_STATUS_IS_OK(GUID_from_data_blob(&val, guid));
108 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
109 DATA_BLOB *newvals, int num_newvals)
111 int num_values = talloc_array_length(mod->values);
115 tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
116 num_values + num_newvals);
122 for (i=0; i<num_newvals; i++) {
123 mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
124 mod->values, newvals[i].data, newvals[i].length);
125 if (mod->values[i+num_values].data == NULL) {
128 mod->values[i+num_values].length = newvals[i].length;
130 mod->num_values = num_values + num_newvals;
134 bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx,
135 struct tldap_mod **pmods, int *pnum_mods,
136 int mod_op, const char *attrib,
137 DATA_BLOB *newvals, int num_newvals)
139 struct tldap_mod new_mod;
140 struct tldap_mod *mods = *pmods;
141 struct tldap_mod *mod = NULL;
145 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
151 num_mods = *pnum_mods;
153 for (i=0; i<num_mods; i++) {
154 if ((mods[i].mod_op == mod_op)
155 && strequal(mods[i].attribute, attrib)) {
162 new_mod.mod_op = mod_op;
163 new_mod.attribute = talloc_strdup(mods, attrib);
164 if (new_mod.attribute == NULL) {
167 new_mod.num_values = 0;
168 new_mod.values = NULL;
172 if ((num_newvals != 0)
173 && !tldap_add_blob_vals(mods, mod, newvals, num_newvals)) {
177 if ((i == num_mods) && (talloc_array_length(mods) < num_mods + 1)) {
178 mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
183 mods[num_mods] = *mod;
191 bool tldap_add_mod_str(TALLOC_CTX *mem_ctx,
192 struct tldap_mod **pmods, int *pnum_mods,
193 int mod_op, const char *attrib, const char *str)
198 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
199 strlen(str), &utf8.data, &utf8.length,
204 ret = tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods, mod_op, attrib,
206 TALLOC_FREE(utf8.data);
210 static bool tldap_make_mod_blob_int(struct tldap_message *existing,
212 struct tldap_mod **pmods, int *pnum_mods,
213 const char *attrib, DATA_BLOB newval,
214 int (*comparison)(const DATA_BLOB *d1,
215 const DATA_BLOB *d2))
218 DATA_BLOB *values = NULL;
219 DATA_BLOB oldval = data_blob_null;
221 if ((existing != NULL)
222 && tldap_entry_values(existing, attrib, &values, &num_values)) {
224 if (num_values > 1) {
225 /* can't change multivalue attributes atm */
228 if (num_values == 1) {
233 if ((oldval.data != NULL) && (newval.data != NULL)
234 && (comparison(&oldval, &newval) == 0)) {
235 /* Believe it or not, but LDAP will deny a delete and
236 an add at the same time if the values are the
238 DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
239 "changed.\n", attrib));
243 if (oldval.data != NULL) {
244 /* By deleting exactly the value we found in the entry this
245 * should be race-free in the sense that the LDAP-Server will
246 * deny the complete operation if somebody changed the
247 * attribute behind our back. */
248 /* This will also allow modifying single valued attributes in
249 * Novell NDS. In NDS you have to first remove attribute and
250 * then you could add new value */
252 DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
254 if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
256 attrib, &oldval, 1)) {
261 /* Regardless of the real operation (add or modify)
262 we add the new value here. We rely on deleting
263 the old value, should it exist. */
265 if (newval.data != NULL) {
266 DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
267 "%d\n", attrib, (int)newval.length));
268 if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
270 attrib, &newval, 1)) {
277 bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
278 struct tldap_mod **pmods, int *pnum_mods,
279 const char *attrib, DATA_BLOB newval)
281 return tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
282 attrib, newval, data_blob_cmp);
285 static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
291 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
292 d1->length, &s1, &s1len, false)) {
293 /* can't do much here */
296 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
297 d2->length, &s2, &s2len, false)) {
298 /* can't do much here */
302 ret = StrCaseCmp(s1, s2);
308 bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
309 struct tldap_mod **pmods, int *pnum_mods,
310 const char *attrib, const char *fmt, ...)
315 DATA_BLOB blob = data_blob_null;
318 newval = talloc_vasprintf(talloc_tos(), fmt, ap);
321 if (newval == NULL) {
325 blob.length = strlen(newval);
326 if (blob.length != 0) {
327 blob.data = CONST_DISCARD(uint8_t *, newval);
329 ret = tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
330 attrib, blob, compare_utf8_blobs);
335 const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
337 const char *ld_error = NULL;
340 ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
341 res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
342 tldap_err2string(rc),
343 ld_error ? ld_error : "unknown");
347 int tldap_search_va(struct tldap_context *ld, const char *base, int scope,
348 const char *attrs[], int num_attrs, int attrsonly,
349 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
350 const char *fmt, va_list ap)
355 filter = talloc_vasprintf(talloc_tos(), fmt, ap);
356 if (filter == NULL) {
357 return TLDAP_NO_MEMORY;
360 ret = tldap_search(ld, base, scope, filter,
361 attrs, num_attrs, attrsonly,
362 NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
363 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
369 int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
370 const char *attrs[], int num_attrs, int attrsonly,
371 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
372 const char *fmt, ...)
378 ret = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
379 mem_ctx, res, fmt, ap);
384 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
390 str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
392 DEBUG(10, ("Could not find attribute %s\n", attr));
395 result = strtoull(str, NULL, 10);
401 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
406 if (!tldap_pull_uint64(msg, attr, &result)) {
409 *presult = (uint32_t)result;
413 struct tldap_fetch_rootdse_state {
414 struct tldap_context *ld;
415 struct tldap_message *rootdse;
418 static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
420 struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
421 struct tevent_context *ev,
422 struct tldap_context *ld)
424 struct tevent_req *req, *subreq;
425 struct tldap_fetch_rootdse_state *state;
426 static const char *attrs[2] = { "*", "+" };
428 req = tevent_req_create(mem_ctx, &state,
429 struct tldap_fetch_rootdse_state);
434 state->rootdse = NULL;
436 subreq = tldap_search_send(
437 mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
438 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
439 if (tevent_req_nomem(subreq, req)) {
440 return tevent_req_post(req, ev);
442 tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
446 static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
448 struct tevent_req *req = tevent_req_callback_data(
449 subreq, struct tevent_req);
450 struct tldap_fetch_rootdse_state *state = tevent_req_data(
451 req, struct tldap_fetch_rootdse_state);
452 struct tldap_message *msg;
455 rc = tldap_search_recv(subreq, state, &msg);
456 if (rc != TLDAP_SUCCESS) {
458 tevent_req_error(req, rc);
462 switch (tldap_msg_type(msg)) {
463 case TLDAP_RES_SEARCH_ENTRY:
464 if (state->rootdse != NULL) {
467 state->rootdse = msg;
469 case TLDAP_RES_SEARCH_RESULT:
471 if (state->rootdse == NULL) {
474 tevent_req_done(req);
482 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
486 int tldap_fetch_rootdse_recv(struct tevent_req *req)
488 struct tldap_fetch_rootdse_state *state = tevent_req_data(
489 req, struct tldap_fetch_rootdse_state);
493 if (tevent_req_is_ldap_error(req, &err)) {
496 /* Trigger parsing the dn, just to make sure it's ok */
497 if (!tldap_entry_dn(state->rootdse, &dn)) {
498 return TLDAP_DECODING_ERROR;
500 if (!tldap_context_setattr(state->ld, "tldap:rootdse",
502 return TLDAP_NO_MEMORY;
507 int tldap_fetch_rootdse(struct tldap_context *ld)
509 TALLOC_CTX *frame = talloc_stackframe();
510 struct tevent_context *ev;
511 struct tevent_req *req;
514 ev = event_context_init(frame);
516 result = TLDAP_NO_MEMORY;
520 req = tldap_fetch_rootdse_send(frame, ev, ld);
522 result = TLDAP_NO_MEMORY;
526 if (!tevent_req_poll(req, ev)) {
527 result = TLDAP_OPERATIONS_ERROR;
531 result = tldap_fetch_rootdse_recv(req);
537 struct tldap_message *tldap_rootdse(struct tldap_context *ld)
539 return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
540 struct tldap_message);
543 bool tldap_entry_has_attrvalue(struct tldap_message *msg,
544 const char *attribute,
545 const DATA_BLOB blob)
550 if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
553 for (i=0; i<num_values; i++) {
554 if (data_blob_cmp(&values[i], &blob) == 0) {
561 bool tldap_supports_control(struct tldap_context *ld, const char *oid)
563 struct tldap_message *rootdse = tldap_rootdse(ld);
565 if (rootdse == NULL) {
568 return tldap_entry_has_attrvalue(rootdse, "supportedControl",
569 data_blob_const(oid, strlen(oid)));
572 struct tldap_control *tldap_add_control(TALLOC_CTX *mem_ctx,
573 struct tldap_control *ctrls,
575 struct tldap_control *ctrl)
577 struct tldap_control *result;
579 result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
580 if (result == NULL) {
583 memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
584 result[num_ctrls] = *ctrl;
589 * Find a control returned by the server
591 struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
594 struct tldap_control *controls;
597 tldap_msg_sctrls(msg, &num_controls, &controls);
599 for (i=0; i<num_controls; i++) {
600 if (strcmp(controls[i].oid, oid) == 0) {
607 struct tldap_search_paged_state {
608 struct tevent_context *ev;
609 struct tldap_context *ld;
616 struct tldap_control *sctrls;
618 struct tldap_control *cctrls;
625 struct asn1_data *asn1;
627 struct tldap_message *result;
630 static struct tevent_req *tldap_ship_paged_search(
632 struct tldap_search_paged_state *state)
634 struct tldap_control *pgctrl;
635 struct asn1_data *asn1;
637 asn1 = asn1_init(state);
641 asn1_push_tag(asn1, ASN1_SEQUENCE(0));
642 asn1_write_Integer(asn1, state->page_size);
643 asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length);
645 if (asn1->has_error) {
651 pgctrl = &state->sctrls[state->num_sctrls-1];
652 pgctrl->oid = TLDAP_CONTROL_PAGEDRESULTS;
653 pgctrl->critical = true;
654 if (!asn1_blob(state->asn1, &pgctrl->value)) {
658 return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
659 state->scope, state->filter, state->attrs,
660 state->num_attrs, state->attrsonly,
661 state->sctrls, state->num_sctrls,
662 state->cctrls, state->num_cctrls,
663 state->timelimit, state->sizelimit,
667 static void tldap_search_paged_done(struct tevent_req *subreq);
669 struct tevent_req *tldap_search_paged_send(TALLOC_CTX *mem_ctx,
670 struct tevent_context *ev,
671 struct tldap_context *ld,
672 const char *base, int scope,
677 struct tldap_control *sctrls,
679 struct tldap_control *cctrls,
686 struct tevent_req *req, *subreq;
687 struct tldap_search_paged_state *state;
688 struct tldap_control empty_control;
690 req = tevent_req_create(mem_ctx, &state,
691 struct tldap_search_paged_state);
698 state->filter = filter;
699 state->scope = scope;
700 state->attrs = attrs;
701 state->num_attrs = num_attrs;
702 state->attrsonly = attrsonly;
703 state->cctrls = cctrls;
704 state->num_cctrls = num_cctrls;
705 state->timelimit = timelimit;
706 state->sizelimit = sizelimit;
707 state->deref = deref;
709 state->page_size = page_size;
711 state->cookie = data_blob_null;
713 ZERO_STRUCT(empty_control);
715 state->sctrls = tldap_add_control(state, sctrls, num_sctrls,
717 if (tevent_req_nomem(state->sctrls, req)) {
718 return tevent_req_post(req, ev);
720 state->num_sctrls = num_sctrls+1;
722 subreq = tldap_ship_paged_search(state, state);
723 if (tevent_req_nomem(subreq, req)) {
724 return tevent_req_post(req, ev);
726 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
731 static void tldap_search_paged_done(struct tevent_req *subreq)
733 struct tevent_req *req = tevent_req_callback_data(
734 subreq, struct tevent_req);
735 struct tldap_search_paged_state *state = tevent_req_data(
736 req, struct tldap_search_paged_state);
737 struct asn1_data *asn1;
738 struct tldap_control *pgctrl;
741 rc = tldap_search_recv(subreq, state, &state->result);
742 if (rc != TLDAP_SUCCESS) {
744 tevent_req_error(req, rc);
748 TALLOC_FREE(state->asn1);
750 switch (tldap_msg_type(state->result)) {
751 case TLDAP_RES_SEARCH_ENTRY:
752 case TLDAP_RES_SEARCH_REFERENCE:
753 tevent_req_notify_callback(req);
755 case TLDAP_RES_SEARCH_RESULT:
759 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
765 /* We've finished one paged search, fire the next */
767 pgctrl = tldap_msg_findcontrol(state->result,
768 TLDAP_CONTROL_PAGEDRESULTS);
769 if (pgctrl == NULL) {
770 /* RFC2696 requires the server to return the control */
771 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
775 TALLOC_FREE(state->cookie.data);
777 asn1 = asn1_init(talloc_tos());
779 tevent_req_error(req, TLDAP_NO_MEMORY);
783 asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
784 asn1_start_tag(asn1, ASN1_SEQUENCE(0));
785 asn1_read_Integer(asn1, &size);
786 asn1_read_OctetString(asn1, state, &state->cookie);
788 if (asn1->has_error) {
789 tevent_req_error(req, TLDAP_DECODING_ERROR);
794 if (state->cookie.length == 0) {
795 /* We're done, no cookie anymore */
796 tevent_req_done(req);
800 TALLOC_FREE(state->result);
802 subreq = tldap_ship_paged_search(state, state);
803 if (tevent_req_nomem(subreq, req)) {
806 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
809 int tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
810 struct tldap_message **pmsg)
812 struct tldap_search_paged_state *state = tevent_req_data(
813 req, struct tldap_search_paged_state);
816 if (!tevent_req_is_in_progress(req)
817 && tevent_req_is_ldap_error(req, &err)) {
820 if (tevent_req_is_in_progress(req)) {
821 switch (tldap_msg_type(state->result)) {
822 case TLDAP_RES_SEARCH_ENTRY:
823 case TLDAP_RES_SEARCH_REFERENCE:
826 return TLDAP_PROTOCOL_ERROR;
829 *pmsg = talloc_move(mem_ctx, &state->result);
830 return TLDAP_SUCCESS;