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 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
23 int *num_values, DATA_BLOB **values)
25 struct tldap_attribute *attributes;
26 int i, num_attributes;
28 if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
32 for (i=0; i<num_attributes; i++) {
33 if (strequal(attribute, attributes[i].name)) {
37 if (i == num_attributes) {
40 *num_values = attributes[i].num_values;
41 *values = attributes[i].values;
45 bool tldap_get_single_valueblob(struct tldap_message *msg,
46 const char *attribute, DATA_BLOB *blob)
51 if (attribute == NULL) {
54 if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
57 if (num_values != 1) {
64 char *tldap_talloc_single_attribute(struct tldap_message *msg,
65 const char *attribute,
72 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
75 if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
77 &result, &len, false)) {
83 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
88 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
91 return sid_parse((char *)val.data, val.length, sid);
94 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
95 int num_newvals, DATA_BLOB *newvals)
97 int num_values = talloc_array_length(mod->values);
101 tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
102 num_values + num_newvals);
108 for (i=0; i<num_newvals; i++) {
109 mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
110 mod->values, newvals[i].data, newvals[i].length);
111 if (mod->values[i+num_values].data == NULL) {
114 mod->values[i+num_values].length = newvals[i].length;
116 mod->num_values = num_values + num_newvals;
120 bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
121 int mod_op, const char *attrib,
122 int num_newvals, DATA_BLOB *newvals)
124 struct tldap_mod new_mod;
125 struct tldap_mod *mods = *pmods;
126 struct tldap_mod *mod = NULL;
130 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
136 num_mods = talloc_array_length(mods);
138 for (i=0; i<num_mods; i++) {
139 if ((mods[i].mod_op == mod_op)
140 && strequal(mods[i].attribute, attrib)) {
147 new_mod.mod_op = mod_op;
148 new_mod.attribute = talloc_strdup(mods, attrib);
149 if (new_mod.attribute == NULL) {
152 new_mod.num_values = 0;
153 new_mod.values = NULL;
157 if ((num_newvals != 0)
158 && !tldap_add_blob_vals(mods, mod, num_newvals, newvals)) {
163 mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
168 mods[num_mods] = *mod;
175 bool tldap_add_mod_str(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
176 int mod_op, const char *attrib, const char *str)
181 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
182 strlen(str), &utf8.data, &utf8.length,
187 ret = tldap_add_mod_blobs(mem_ctx, pmods, mod_op, attrib, 1, &utf8);
188 TALLOC_FREE(utf8.data);
192 static bool tldap_make_mod_blob_int(struct tldap_message *existing,
194 int *pnum_mods, struct tldap_mod **pmods,
195 const char *attrib, DATA_BLOB newval,
196 int (*comparison)(const DATA_BLOB *d1,
197 const DATA_BLOB *d2))
200 DATA_BLOB *values = NULL;
201 DATA_BLOB oldval = data_blob_null;
203 if ((existing != NULL)
204 && tldap_entry_values(existing, attrib, &num_values, &values)) {
206 if (num_values > 1) {
207 /* can't change multivalue attributes atm */
210 if (num_values == 1) {
215 if ((oldval.data != NULL) && (newval.data != NULL)
216 && (comparison(&oldval, &newval) == 0)) {
217 /* Believe it or not, but LDAP will deny a delete and
218 an add at the same time if the values are the
220 DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
221 "changed.\n", attrib));
225 if (oldval.data != NULL) {
226 /* By deleting exactly the value we found in the entry this
227 * should be race-free in the sense that the LDAP-Server will
228 * deny the complete operation if somebody changed the
229 * attribute behind our back. */
230 /* This will also allow modifying single valued attributes in
231 * Novell NDS. In NDS you have to first remove attribute and
232 * then you could add new value */
234 DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
236 if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_DELETE,
237 attrib, 1, &oldval)) {
242 /* Regardless of the real operation (add or modify)
243 we add the new value here. We rely on deleting
244 the old value, should it exist. */
246 if (newval.data != NULL) {
247 DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
248 "%d\n", attrib, (int)newval.length));
249 if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_ADD,
250 attrib, 1, &newval)) {
254 *pnum_mods = talloc_array_length(*pmods);
258 bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
259 int *pnum_mods, struct tldap_mod **pmods,
260 const char *attrib, DATA_BLOB newval)
262 return tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
263 attrib, newval, data_blob_cmp);
266 static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
272 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
273 d1->length, &s1, &s1len, false)) {
274 /* can't do much here */
277 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
278 d2->length, &s2, &s2len, false)) {
279 /* can't do much here */
283 ret = StrCaseCmp(s1, s2);
289 bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
290 int *pnum_mods, struct tldap_mod **pmods,
291 const char *attrib, const char *fmt, ...)
296 DATA_BLOB blob = data_blob_null;
299 newval = talloc_vasprintf(talloc_tos(), fmt, ap);
302 if (newval == NULL) {
306 blob.length = strlen(newval);
307 if (blob.length != 0) {
308 blob.data = CONST_DISCARD(uint8_t *, newval);
310 ret = tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
311 attrib, blob, compare_utf8_blobs);
316 const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
318 const char *ld_error = NULL;
321 ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
322 res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
323 tldap_err2string(rc),
324 ld_error ? ld_error : "unknown");
328 int tldap_search_va(struct tldap_context *ld, const char *base, int scope,
329 const char *attrs[], int num_attrs, int attrsonly,
330 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
331 const char *fmt, va_list ap)
336 filter = talloc_vasprintf(talloc_tos(), fmt, ap);
337 if (filter == NULL) {
338 return TLDAP_NO_MEMORY;
341 ret = tldap_search(ld, base, scope, filter,
342 attrs, num_attrs, attrsonly,
343 NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
344 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
350 int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
351 const char *attrs[], int num_attrs, int attrsonly,
352 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
353 const char *fmt, ...)
359 ret = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
360 mem_ctx, res, fmt, ap);
365 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
371 str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
373 DEBUG(10, ("Could not find attribute %s\n", attr));
376 result = strtoull(str, NULL, 10);
382 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
387 if (!tldap_pull_uint64(msg, attr, &result)) {
390 *presult = (uint32_t)result;
394 struct tldap_fetch_rootdse_state {
395 struct tldap_context *ld;
396 struct tldap_message *rootdse;
399 static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
401 struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
402 struct tevent_context *ev,
403 struct tldap_context *ld)
405 struct tevent_req *req, *subreq;
406 struct tldap_fetch_rootdse_state *state;
407 static const char *attrs[2] = { "*", "+" };
409 req = tevent_req_create(mem_ctx, &state,
410 struct tldap_fetch_rootdse_state);
415 state->rootdse = NULL;
417 subreq = tldap_search_send(
418 mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
419 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
420 if (tevent_req_nomem(subreq, req)) {
421 return tevent_req_post(req, ev);
423 tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
427 static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
429 struct tevent_req *req = tevent_req_callback_data(
430 subreq, struct tevent_req);
431 struct tldap_fetch_rootdse_state *state = tevent_req_data(
432 req, struct tldap_fetch_rootdse_state);
433 struct tldap_message *msg;
436 rc = tldap_search_recv(subreq, state, &msg);
437 if (rc != TLDAP_SUCCESS) {
439 tevent_req_error(req, rc);
443 switch (tldap_msg_type(msg)) {
444 case TLDAP_RES_SEARCH_ENTRY:
445 if (state->rootdse != NULL) {
448 state->rootdse = msg;
450 case TLDAP_RES_SEARCH_RESULT:
452 if (state->rootdse == NULL) {
455 tevent_req_done(req);
463 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
467 int tldap_fetch_rootdse_recv(struct tevent_req *req)
469 struct tldap_fetch_rootdse_state *state = tevent_req_data(
470 req, struct tldap_fetch_rootdse_state);
474 if (tevent_req_is_ldap_error(req, &err)) {
477 /* Trigger parsing the dn, just to make sure it's ok */
478 if (!tldap_entry_dn(state->rootdse, &dn)) {
479 return TLDAP_DECODING_ERROR;
481 if (!tldap_context_setattr(state->ld, "tldap:rootdse",
483 return TLDAP_NO_MEMORY;
488 int tldap_fetch_rootdse(struct tldap_context *ld)
490 TALLOC_CTX *frame = talloc_stackframe();
491 struct tevent_context *ev;
492 struct tevent_req *req;
495 ev = event_context_init(frame);
497 result = TLDAP_NO_MEMORY;
501 req = tldap_fetch_rootdse_send(frame, ev, ld);
503 result = TLDAP_NO_MEMORY;
507 if (!tevent_req_poll(req, ev)) {
508 result = TLDAP_OPERATIONS_ERROR;
512 result = tldap_fetch_rootdse_recv(req);
518 struct tldap_message *tldap_rootdse(struct tldap_context *ld)
520 return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
521 struct tldap_message);
524 bool tldap_entry_has_attrvalue(struct tldap_message *msg,
525 const char *attribute,
526 const DATA_BLOB blob)
531 if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
534 for (i=0; i<num_values; i++) {
535 if (data_blob_cmp(&values[i], &blob) == 0) {
542 bool tldap_supports_control(struct tldap_context *ld, const char *oid)
544 struct tldap_message *rootdse = tldap_rootdse(ld);
546 if (rootdse == NULL) {
549 return tldap_entry_has_attrvalue(rootdse, "supportedControl",
550 data_blob_const(oid, strlen(oid)));
553 struct tldap_control *tldap_add_control(TALLOC_CTX *mem_ctx,
554 struct tldap_control *ctrls,
556 struct tldap_control *ctrl)
558 struct tldap_control *result;
560 result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
561 if (result == NULL) {
564 memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
565 result[num_ctrls] = *ctrl;
570 * Find a control returned by the server
572 struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
575 struct tldap_control *controls;
578 tldap_msg_sctrls(msg, &num_controls, &controls);
580 for (i=0; i<num_controls; i++) {
581 if (strcmp(controls[i].oid, oid) == 0) {
588 struct tldap_search_paged_state {
589 struct tevent_context *ev;
590 struct tldap_context *ld;
597 struct tldap_control *sctrls;
599 struct tldap_control *cctrls;
606 struct asn1_data *asn1;
608 struct tldap_message *result;
611 static struct tevent_req *tldap_ship_paged_search(
613 struct tldap_search_paged_state *state)
615 struct tldap_control *pgctrl;
616 struct asn1_data *asn1;
618 asn1 = asn1_init(state);
622 asn1_push_tag(asn1, ASN1_SEQUENCE(0));
623 asn1_write_Integer(asn1, state->page_size);
624 asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length);
626 if (asn1->has_error) {
632 pgctrl = &state->sctrls[state->num_sctrls-1];
633 pgctrl->oid = TLDAP_CONTROL_PAGEDRESULTS;
634 pgctrl->critical = true;
635 if (!asn1_blob(state->asn1, &pgctrl->value)) {
639 return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
640 state->scope, state->filter, state->attrs,
641 state->num_attrs, state->attrsonly,
642 state->sctrls, state->num_sctrls,
643 state->cctrls, state->num_cctrls,
644 state->timelimit, state->sizelimit,
648 static void tldap_search_paged_done(struct tevent_req *subreq);
650 struct tevent_req *tldap_search_paged_send(TALLOC_CTX *mem_ctx,
651 struct tevent_context *ev,
652 struct tldap_context *ld,
653 const char *base, int scope,
658 struct tldap_control *sctrls,
660 struct tldap_control *cctrls,
667 struct tevent_req *req, *subreq;
668 struct tldap_search_paged_state *state;
669 struct tldap_control empty_control;
671 req = tevent_req_create(mem_ctx, &state,
672 struct tldap_search_paged_state);
679 state->filter = filter;
680 state->scope = scope;
681 state->attrs = attrs;
682 state->num_attrs = num_attrs;
683 state->attrsonly = attrsonly;
684 state->cctrls = cctrls;
685 state->num_cctrls = num_cctrls;
686 state->timelimit = timelimit;
687 state->sizelimit = sizelimit;
688 state->deref = deref;
690 state->page_size = page_size;
692 state->cookie = data_blob_null;
694 ZERO_STRUCT(empty_control);
696 state->sctrls = tldap_add_control(state, sctrls, num_sctrls,
698 if (tevent_req_nomem(state->sctrls, req)) {
699 return tevent_req_post(req, ev);
701 state->num_sctrls = num_sctrls+1;
703 subreq = tldap_ship_paged_search(state, state);
704 if (tevent_req_nomem(subreq, req)) {
705 return tevent_req_post(req, ev);
707 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
712 static void tldap_search_paged_done(struct tevent_req *subreq)
714 struct tevent_req *req = tevent_req_callback_data(
715 subreq, struct tevent_req);
716 struct tldap_search_paged_state *state = tevent_req_data(
717 req, struct tldap_search_paged_state);
718 struct asn1_data *asn1;
719 struct tldap_control *pgctrl;
722 rc = tldap_search_recv(subreq, state, &state->result);
723 if (rc != TLDAP_SUCCESS) {
725 tevent_req_error(req, rc);
729 TALLOC_FREE(state->asn1);
731 switch (tldap_msg_type(state->result)) {
732 case TLDAP_RES_SEARCH_ENTRY:
733 case TLDAP_RES_SEARCH_REFERENCE:
734 tevent_req_notify_callback(req);
736 case TLDAP_RES_SEARCH_RESULT:
740 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
746 /* We've finished one paged search, fire the next */
748 pgctrl = tldap_msg_findcontrol(state->result,
749 TLDAP_CONTROL_PAGEDRESULTS);
750 if (pgctrl == NULL) {
751 /* RFC2696 requires the server to return the control */
752 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
756 TALLOC_FREE(state->cookie.data);
758 asn1 = asn1_init(talloc_tos());
760 tevent_req_error(req, TLDAP_NO_MEMORY);
764 asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
765 asn1_start_tag(asn1, ASN1_SEQUENCE(0));
766 asn1_read_Integer(asn1, &size);
767 asn1_read_OctetString(asn1, state, &state->cookie);
769 if (asn1->has_error) {
770 tevent_req_error(req, TLDAP_DECODING_ERROR);
775 if (state->cookie.length == 0) {
776 /* We're done, no cookie anymore */
777 tevent_req_done(req);
781 TALLOC_FREE(state->result);
783 subreq = tldap_ship_paged_search(state, state);
784 if (tevent_req_nomem(subreq, req)) {
787 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
790 int tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
791 struct tldap_message **pmsg)
793 struct tldap_search_paged_state *state = tevent_req_data(
794 req, struct tldap_search_paged_state);
797 if (!tevent_req_is_in_progress(req)
798 && tevent_req_is_ldap_error(req, &err)) {
801 if (tevent_req_is_in_progress(req)) {
802 switch (tldap_msg_type(state->result)) {
803 case TLDAP_RES_SEARCH_ENTRY:
804 case TLDAP_RES_SEARCH_REFERENCE:
807 return TLDAP_PROTOCOL_ERROR;
810 *pmsg = talloc_move(mem_ctx, &state->result);
811 return TLDAP_SUCCESS;