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"
24 bool tldap_entry_values(struct tldap_message
*msg
, const char *attribute
,
25 int *num_values
, DATA_BLOB
**values
)
27 struct tldap_attribute
*attributes
;
28 int i
, num_attributes
;
30 if (!tldap_entry_attributes(msg
, &num_attributes
, &attributes
)) {
34 for (i
=0; i
<num_attributes
; i
++) {
35 if (strequal(attribute
, attributes
[i
].name
)) {
39 if (i
== num_attributes
) {
42 *num_values
= attributes
[i
].num_values
;
43 *values
= attributes
[i
].values
;
47 bool tldap_get_single_valueblob(struct tldap_message
*msg
,
48 const char *attribute
, DATA_BLOB
*blob
)
53 if (attribute
== NULL
) {
56 if (!tldap_entry_values(msg
, attribute
, &num_values
, &values
)) {
59 if (num_values
!= 1) {
66 char *tldap_talloc_single_attribute(struct tldap_message
*msg
,
67 const char *attribute
,
74 if (!tldap_get_single_valueblob(msg
, attribute
, &val
)) {
77 if (!convert_string_talloc(mem_ctx
, CH_UTF8
, CH_UNIX
,
79 &result
, &len
, false)) {
85 bool tldap_pull_binsid(struct tldap_message
*msg
, const char *attribute
,
90 if (!tldap_get_single_valueblob(msg
, attribute
, &val
)) {
93 return sid_parse((char *)val
.data
, val
.length
, sid
);
96 bool tldap_pull_guid(struct tldap_message
*msg
, const char *attribute
,
101 if (!tldap_get_single_valueblob(msg
, attribute
, &val
)) {
104 return NT_STATUS_IS_OK(GUID_from_data_blob(&val
, guid
));
107 static bool tldap_add_blob_vals(TALLOC_CTX
*mem_ctx
, struct tldap_mod
*mod
,
108 int num_newvals
, DATA_BLOB
*newvals
)
110 int num_values
= talloc_array_length(mod
->values
);
114 tmp
= talloc_realloc(mem_ctx
, mod
->values
, DATA_BLOB
,
115 num_values
+ num_newvals
);
121 for (i
=0; i
<num_newvals
; i
++) {
122 mod
->values
[i
+num_values
].data
= (uint8_t *)talloc_memdup(
123 mod
->values
, newvals
[i
].data
, newvals
[i
].length
);
124 if (mod
->values
[i
+num_values
].data
== NULL
) {
127 mod
->values
[i
+num_values
].length
= newvals
[i
].length
;
129 mod
->num_values
= num_values
+ num_newvals
;
133 bool tldap_add_mod_blobs(TALLOC_CTX
*mem_ctx
, struct tldap_mod
**pmods
,
134 int mod_op
, const char *attrib
,
135 int num_newvals
, DATA_BLOB
*newvals
)
137 struct tldap_mod new_mod
;
138 struct tldap_mod
*mods
= *pmods
;
139 struct tldap_mod
*mod
= NULL
;
143 mods
= talloc_array(mem_ctx
, struct tldap_mod
, 0);
149 num_mods
= talloc_array_length(mods
);
151 for (i
=0; i
<num_mods
; i
++) {
152 if ((mods
[i
].mod_op
== mod_op
)
153 && strequal(mods
[i
].attribute
, attrib
)) {
160 new_mod
.mod_op
= mod_op
;
161 new_mod
.attribute
= talloc_strdup(mods
, attrib
);
162 if (new_mod
.attribute
== NULL
) {
165 new_mod
.num_values
= 0;
166 new_mod
.values
= NULL
;
170 if ((num_newvals
!= 0)
171 && !tldap_add_blob_vals(mods
, mod
, num_newvals
, newvals
)) {
176 mods
= talloc_realloc(talloc_tos(), mods
, struct tldap_mod
,
181 mods
[num_mods
] = *mod
;
188 bool tldap_add_mod_str(TALLOC_CTX
*mem_ctx
, struct tldap_mod
**pmods
,
189 int mod_op
, const char *attrib
, const char *str
)
194 if (!convert_string_talloc(talloc_tos(), CH_UNIX
, CH_UTF8
, str
,
195 strlen(str
), &utf8
.data
, &utf8
.length
,
200 ret
= tldap_add_mod_blobs(mem_ctx
, pmods
, mod_op
, attrib
, 1, &utf8
);
201 TALLOC_FREE(utf8
.data
);
205 static bool tldap_make_mod_blob_int(struct tldap_message
*existing
,
207 int *pnum_mods
, struct tldap_mod
**pmods
,
208 const char *attrib
, DATA_BLOB newval
,
209 int (*comparison
)(const DATA_BLOB
*d1
,
210 const DATA_BLOB
*d2
))
213 DATA_BLOB
*values
= NULL
;
214 DATA_BLOB oldval
= data_blob_null
;
216 if ((existing
!= NULL
)
217 && tldap_entry_values(existing
, attrib
, &num_values
, &values
)) {
219 if (num_values
> 1) {
220 /* can't change multivalue attributes atm */
223 if (num_values
== 1) {
228 if ((oldval
.data
!= NULL
) && (newval
.data
!= NULL
)
229 && (comparison(&oldval
, &newval
) == 0)) {
230 /* Believe it or not, but LDAP will deny a delete and
231 an add at the same time if the values are the
233 DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
234 "changed.\n", attrib
));
238 if (oldval
.data
!= NULL
) {
239 /* By deleting exactly the value we found in the entry this
240 * should be race-free in the sense that the LDAP-Server will
241 * deny the complete operation if somebody changed the
242 * attribute behind our back. */
243 /* This will also allow modifying single valued attributes in
244 * Novell NDS. In NDS you have to first remove attribute and
245 * then you could add new value */
247 DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
249 if (!tldap_add_mod_blobs(mem_ctx
, pmods
, TLDAP_MOD_DELETE
,
250 attrib
, 1, &oldval
)) {
255 /* Regardless of the real operation (add or modify)
256 we add the new value here. We rely on deleting
257 the old value, should it exist. */
259 if (newval
.data
!= NULL
) {
260 DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
261 "%d\n", attrib
, (int)newval
.length
));
262 if (!tldap_add_mod_blobs(mem_ctx
, pmods
, TLDAP_MOD_ADD
,
263 attrib
, 1, &newval
)) {
267 *pnum_mods
= talloc_array_length(*pmods
);
271 bool tldap_make_mod_blob(struct tldap_message
*existing
, TALLOC_CTX
*mem_ctx
,
272 int *pnum_mods
, struct tldap_mod
**pmods
,
273 const char *attrib
, DATA_BLOB newval
)
275 return tldap_make_mod_blob_int(existing
, mem_ctx
, pnum_mods
, pmods
,
276 attrib
, newval
, data_blob_cmp
);
279 static int compare_utf8_blobs(const DATA_BLOB
*d1
, const DATA_BLOB
*d2
)
285 if (!convert_string_talloc(talloc_tos(), CH_UTF8
, CH_UNIX
, d1
->data
,
286 d1
->length
, &s1
, &s1len
, false)) {
287 /* can't do much here */
290 if (!convert_string_talloc(talloc_tos(), CH_UTF8
, CH_UNIX
, d2
->data
,
291 d2
->length
, &s2
, &s2len
, false)) {
292 /* can't do much here */
296 ret
= StrCaseCmp(s1
, s2
);
302 bool tldap_make_mod_fmt(struct tldap_message
*existing
, TALLOC_CTX
*mem_ctx
,
303 int *pnum_mods
, struct tldap_mod
**pmods
,
304 const char *attrib
, const char *fmt
, ...)
309 DATA_BLOB blob
= data_blob_null
;
312 newval
= talloc_vasprintf(talloc_tos(), fmt
, ap
);
315 if (newval
== NULL
) {
319 blob
.length
= strlen(newval
);
320 if (blob
.length
!= 0) {
321 blob
.data
= CONST_DISCARD(uint8_t *, newval
);
323 ret
= tldap_make_mod_blob_int(existing
, mem_ctx
, pnum_mods
, pmods
,
324 attrib
, blob
, compare_utf8_blobs
);
329 const char *tldap_errstr(TALLOC_CTX
*mem_ctx
, struct tldap_context
*ld
, int rc
)
331 const char *ld_error
= NULL
;
334 ld_error
= tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld
));
335 res
= talloc_asprintf(mem_ctx
, "LDAP error %d (%s), %s", rc
,
336 tldap_err2string(rc
),
337 ld_error
? ld_error
: "unknown");
341 int tldap_search_va(struct tldap_context
*ld
, const char *base
, int scope
,
342 const char *attrs
[], int num_attrs
, int attrsonly
,
343 TALLOC_CTX
*mem_ctx
, struct tldap_message
***res
,
344 const char *fmt
, va_list ap
)
349 filter
= talloc_vasprintf(talloc_tos(), fmt
, ap
);
350 if (filter
== NULL
) {
351 return TLDAP_NO_MEMORY
;
354 ret
= tldap_search(ld
, base
, scope
, filter
,
355 attrs
, num_attrs
, attrsonly
,
356 NULL
/*sctrls*/, 0, NULL
/*cctrls*/, 0,
357 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
363 int tldap_search_fmt(struct tldap_context
*ld
, const char *base
, int scope
,
364 const char *attrs
[], int num_attrs
, int attrsonly
,
365 TALLOC_CTX
*mem_ctx
, struct tldap_message
***res
,
366 const char *fmt
, ...)
372 ret
= tldap_search_va(ld
, base
, scope
, attrs
, num_attrs
, attrsonly
,
373 mem_ctx
, res
, fmt
, ap
);
378 bool tldap_pull_uint64(struct tldap_message
*msg
, const char *attr
,
384 str
= tldap_talloc_single_attribute(msg
, attr
, talloc_tos());
386 DEBUG(10, ("Could not find attribute %s\n", attr
));
389 result
= strtoull(str
, NULL
, 10);
395 bool tldap_pull_uint32(struct tldap_message
*msg
, const char *attr
,
400 if (!tldap_pull_uint64(msg
, attr
, &result
)) {
403 *presult
= (uint32_t)result
;
407 struct tldap_fetch_rootdse_state
{
408 struct tldap_context
*ld
;
409 struct tldap_message
*rootdse
;
412 static void tldap_fetch_rootdse_done(struct tevent_req
*subreq
);
414 struct tevent_req
*tldap_fetch_rootdse_send(TALLOC_CTX
*mem_ctx
,
415 struct tevent_context
*ev
,
416 struct tldap_context
*ld
)
418 struct tevent_req
*req
, *subreq
;
419 struct tldap_fetch_rootdse_state
*state
;
420 static const char *attrs
[2] = { "*", "+" };
422 req
= tevent_req_create(mem_ctx
, &state
,
423 struct tldap_fetch_rootdse_state
);
428 state
->rootdse
= NULL
;
430 subreq
= tldap_search_send(
431 mem_ctx
, ev
, ld
, "", TLDAP_SCOPE_BASE
, "(objectclass=*)",
432 attrs
, ARRAY_SIZE(attrs
), 0, NULL
, 0, NULL
, 0, 0, 0, 0);
433 if (tevent_req_nomem(subreq
, req
)) {
434 return tevent_req_post(req
, ev
);
436 tevent_req_set_callback(subreq
, tldap_fetch_rootdse_done
, req
);
440 static void tldap_fetch_rootdse_done(struct tevent_req
*subreq
)
442 struct tevent_req
*req
= tevent_req_callback_data(
443 subreq
, struct tevent_req
);
444 struct tldap_fetch_rootdse_state
*state
= tevent_req_data(
445 req
, struct tldap_fetch_rootdse_state
);
446 struct tldap_message
*msg
;
449 rc
= tldap_search_recv(subreq
, state
, &msg
);
450 if (rc
!= TLDAP_SUCCESS
) {
452 tevent_req_error(req
, rc
);
456 switch (tldap_msg_type(msg
)) {
457 case TLDAP_RES_SEARCH_ENTRY
:
458 if (state
->rootdse
!= NULL
) {
461 state
->rootdse
= msg
;
463 case TLDAP_RES_SEARCH_RESULT
:
465 if (state
->rootdse
== NULL
) {
468 tevent_req_done(req
);
476 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
480 int tldap_fetch_rootdse_recv(struct tevent_req
*req
)
482 struct tldap_fetch_rootdse_state
*state
= tevent_req_data(
483 req
, struct tldap_fetch_rootdse_state
);
487 if (tevent_req_is_ldap_error(req
, &err
)) {
490 /* Trigger parsing the dn, just to make sure it's ok */
491 if (!tldap_entry_dn(state
->rootdse
, &dn
)) {
492 return TLDAP_DECODING_ERROR
;
494 if (!tldap_context_setattr(state
->ld
, "tldap:rootdse",
496 return TLDAP_NO_MEMORY
;
501 int tldap_fetch_rootdse(struct tldap_context
*ld
)
503 TALLOC_CTX
*frame
= talloc_stackframe();
504 struct tevent_context
*ev
;
505 struct tevent_req
*req
;
508 ev
= event_context_init(frame
);
510 result
= TLDAP_NO_MEMORY
;
514 req
= tldap_fetch_rootdse_send(frame
, ev
, ld
);
516 result
= TLDAP_NO_MEMORY
;
520 if (!tevent_req_poll(req
, ev
)) {
521 result
= TLDAP_OPERATIONS_ERROR
;
525 result
= tldap_fetch_rootdse_recv(req
);
531 struct tldap_message
*tldap_rootdse(struct tldap_context
*ld
)
533 return talloc_get_type(tldap_context_getattr(ld
, "tldap:rootdse"),
534 struct tldap_message
);
537 bool tldap_entry_has_attrvalue(struct tldap_message
*msg
,
538 const char *attribute
,
539 const DATA_BLOB blob
)
544 if (!tldap_entry_values(msg
, attribute
, &num_values
, &values
)) {
547 for (i
=0; i
<num_values
; i
++) {
548 if (data_blob_cmp(&values
[i
], &blob
) == 0) {
555 bool tldap_supports_control(struct tldap_context
*ld
, const char *oid
)
557 struct tldap_message
*rootdse
= tldap_rootdse(ld
);
559 if (rootdse
== NULL
) {
562 return tldap_entry_has_attrvalue(rootdse
, "supportedControl",
563 data_blob_const(oid
, strlen(oid
)));
566 struct tldap_control
*tldap_add_control(TALLOC_CTX
*mem_ctx
,
567 struct tldap_control
*ctrls
,
569 struct tldap_control
*ctrl
)
571 struct tldap_control
*result
;
573 result
= talloc_array(mem_ctx
, struct tldap_control
, num_ctrls
+1);
574 if (result
== NULL
) {
577 memcpy(result
, ctrls
, sizeof(struct tldap_control
) * num_ctrls
);
578 result
[num_ctrls
] = *ctrl
;
583 * Find a control returned by the server
585 struct tldap_control
*tldap_msg_findcontrol(struct tldap_message
*msg
,
588 struct tldap_control
*controls
;
591 tldap_msg_sctrls(msg
, &num_controls
, &controls
);
593 for (i
=0; i
<num_controls
; i
++) {
594 if (strcmp(controls
[i
].oid
, oid
) == 0) {
601 struct tldap_search_paged_state
{
602 struct tevent_context
*ev
;
603 struct tldap_context
*ld
;
610 struct tldap_control
*sctrls
;
612 struct tldap_control
*cctrls
;
619 struct asn1_data
*asn1
;
621 struct tldap_message
*result
;
624 static struct tevent_req
*tldap_ship_paged_search(
626 struct tldap_search_paged_state
*state
)
628 struct tldap_control
*pgctrl
;
629 struct asn1_data
*asn1
;
631 asn1
= asn1_init(state
);
635 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
636 asn1_write_Integer(asn1
, state
->page_size
);
637 asn1_write_OctetString(asn1
, state
->cookie
.data
, state
->cookie
.length
);
639 if (asn1
->has_error
) {
645 pgctrl
= &state
->sctrls
[state
->num_sctrls
-1];
646 pgctrl
->oid
= TLDAP_CONTROL_PAGEDRESULTS
;
647 pgctrl
->critical
= true;
648 if (!asn1_blob(state
->asn1
, &pgctrl
->value
)) {
652 return tldap_search_send(mem_ctx
, state
->ev
, state
->ld
, state
->base
,
653 state
->scope
, state
->filter
, state
->attrs
,
654 state
->num_attrs
, state
->attrsonly
,
655 state
->sctrls
, state
->num_sctrls
,
656 state
->cctrls
, state
->num_cctrls
,
657 state
->timelimit
, state
->sizelimit
,
661 static void tldap_search_paged_done(struct tevent_req
*subreq
);
663 struct tevent_req
*tldap_search_paged_send(TALLOC_CTX
*mem_ctx
,
664 struct tevent_context
*ev
,
665 struct tldap_context
*ld
,
666 const char *base
, int scope
,
671 struct tldap_control
*sctrls
,
673 struct tldap_control
*cctrls
,
680 struct tevent_req
*req
, *subreq
;
681 struct tldap_search_paged_state
*state
;
682 struct tldap_control empty_control
;
684 req
= tevent_req_create(mem_ctx
, &state
,
685 struct tldap_search_paged_state
);
692 state
->filter
= filter
;
693 state
->scope
= scope
;
694 state
->attrs
= attrs
;
695 state
->num_attrs
= num_attrs
;
696 state
->attrsonly
= attrsonly
;
697 state
->cctrls
= cctrls
;
698 state
->num_cctrls
= num_cctrls
;
699 state
->timelimit
= timelimit
;
700 state
->sizelimit
= sizelimit
;
701 state
->deref
= deref
;
703 state
->page_size
= page_size
;
705 state
->cookie
= data_blob_null
;
707 ZERO_STRUCT(empty_control
);
709 state
->sctrls
= tldap_add_control(state
, sctrls
, num_sctrls
,
711 if (tevent_req_nomem(state
->sctrls
, req
)) {
712 return tevent_req_post(req
, ev
);
714 state
->num_sctrls
= num_sctrls
+1;
716 subreq
= tldap_ship_paged_search(state
, state
);
717 if (tevent_req_nomem(subreq
, req
)) {
718 return tevent_req_post(req
, ev
);
720 tevent_req_set_callback(subreq
, tldap_search_paged_done
, req
);
725 static void tldap_search_paged_done(struct tevent_req
*subreq
)
727 struct tevent_req
*req
= tevent_req_callback_data(
728 subreq
, struct tevent_req
);
729 struct tldap_search_paged_state
*state
= tevent_req_data(
730 req
, struct tldap_search_paged_state
);
731 struct asn1_data
*asn1
;
732 struct tldap_control
*pgctrl
;
735 rc
= tldap_search_recv(subreq
, state
, &state
->result
);
736 if (rc
!= TLDAP_SUCCESS
) {
738 tevent_req_error(req
, rc
);
742 TALLOC_FREE(state
->asn1
);
744 switch (tldap_msg_type(state
->result
)) {
745 case TLDAP_RES_SEARCH_ENTRY
:
746 case TLDAP_RES_SEARCH_REFERENCE
:
747 tevent_req_notify_callback(req
);
749 case TLDAP_RES_SEARCH_RESULT
:
753 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
759 /* We've finished one paged search, fire the next */
761 pgctrl
= tldap_msg_findcontrol(state
->result
,
762 TLDAP_CONTROL_PAGEDRESULTS
);
763 if (pgctrl
== NULL
) {
764 /* RFC2696 requires the server to return the control */
765 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
769 TALLOC_FREE(state
->cookie
.data
);
771 asn1
= asn1_init(talloc_tos());
773 tevent_req_error(req
, TLDAP_NO_MEMORY
);
777 asn1_load_nocopy(asn1
, pgctrl
->value
.data
, pgctrl
->value
.length
);
778 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
779 asn1_read_Integer(asn1
, &size
);
780 asn1_read_OctetString(asn1
, state
, &state
->cookie
);
782 if (asn1
->has_error
) {
783 tevent_req_error(req
, TLDAP_DECODING_ERROR
);
788 if (state
->cookie
.length
== 0) {
789 /* We're done, no cookie anymore */
790 tevent_req_done(req
);
794 TALLOC_FREE(state
->result
);
796 subreq
= tldap_ship_paged_search(state
, state
);
797 if (tevent_req_nomem(subreq
, req
)) {
800 tevent_req_set_callback(subreq
, tldap_search_paged_done
, req
);
803 int tldap_search_paged_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
804 struct tldap_message
**pmsg
)
806 struct tldap_search_paged_state
*state
= tevent_req_data(
807 req
, struct tldap_search_paged_state
);
810 if (!tevent_req_is_in_progress(req
)
811 && tevent_req_is_ldap_error(req
, &err
)) {
814 if (tevent_req_is_in_progress(req
)) {
815 switch (tldap_msg_type(state
->result
)) {
816 case TLDAP_RES_SEARCH_ENTRY
:
817 case TLDAP_RES_SEARCH_REFERENCE
:
820 return TLDAP_PROTOCOL_ERROR
;
823 *pmsg
= talloc_move(mem_ctx
, &state
->result
);
824 return TLDAP_SUCCESS
;