2 Unix SMB/CIFS mplementation.
3 LDAP protocol helper functions for SAMBA
5 Copyright (C) Simo Sorce 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/util/asn_1.h"
25 #include "libcli/ldap/ldap.h"
26 #include "lib/ldb/include/ldb.h"
28 struct control_handler {
30 BOOL (*decode)(void *mem_ctx, DATA_BLOB in, void **out);
31 BOOL (*encode)(void *mem_ctx, void *in, DATA_BLOB *out);
34 static BOOL decode_server_sort_response(void *mem_ctx, DATA_BLOB in, void **out)
37 struct asn1_data data;
38 struct ldb_sort_resp_control *lsrc;
40 if (!asn1_load(&data, in)) {
44 lsrc = talloc(mem_ctx, struct ldb_sort_resp_control);
49 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
53 if (!asn1_read_enumerated(&data, &(lsrc->result))) {
57 lsrc->attr_desc = NULL;
58 if (asn1_peek_tag(&data, ASN1_OCTET_STRING)) {
59 if (!asn1_read_OctetString(&data, &attr)) {
62 lsrc->attr_desc = talloc_strndup(lsrc, (const char *)attr.data, attr.length);
63 if (!lsrc->attr_desc) {
68 if (!asn1_end_tag(&data)) {
77 static BOOL decode_server_sort_request(void *mem_ctx, DATA_BLOB in, void **out)
81 struct asn1_data data;
82 struct ldb_server_sort_control **lssc;
85 if (!asn1_load(&data, in)) {
89 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
95 for (num = 0; asn1_peek_tag(&data, ASN1_SEQUENCE(0)); num++) {
96 lssc = talloc_realloc(mem_ctx, lssc, struct ldb_server_sort_control *, num + 2);
100 lssc[num] = talloc_zero(lssc, struct ldb_server_sort_control);
105 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
109 if (!asn1_read_OctetString(&data, &attr)) {
113 lssc[num]->attributeName = talloc_strndup(lssc[num], (const char *)attr.data, attr.length);
114 if (!lssc [num]->attributeName) {
118 if (asn1_peek_tag(&data, ASN1_OCTET_STRING)) {
119 if (!asn1_read_OctetString(&data, &rule)) {
122 lssc[num]->orderingRule = talloc_strndup(lssc[num], (const char *)rule.data, rule.length);
123 if (!lssc[num]->orderingRule) {
128 if (asn1_peek_tag(&data, ASN1_BOOLEAN)) {
130 if (!asn1_read_BOOLEAN(&data, &reverse)) {
133 lssc[num]->reverse = reverse;
136 if (!asn1_end_tag(&data)) {
145 if (!asn1_end_tag(&data)) {
154 static BOOL decode_extended_dn_request(void *mem_ctx, DATA_BLOB in, void **out)
156 struct asn1_data data;
157 struct ldb_extended_dn_control *ledc;
159 if (!asn1_load(&data, in)) {
163 ledc = talloc(mem_ctx, struct ldb_extended_dn_control);
168 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
172 if (!asn1_read_Integer(&data, &(ledc->type))) {
176 if (!asn1_end_tag(&data)) {
185 static BOOL decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void **out)
187 struct asn1_data data;
188 struct ldb_sd_flags_control *lsdfc;
190 if (!asn1_load(&data, in)) {
194 lsdfc = talloc(mem_ctx, struct ldb_sd_flags_control);
199 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
203 if (!asn1_read_Integer(&data, &(lsdfc->secinfo_flags))) {
207 if (!asn1_end_tag(&data)) {
216 static BOOL decode_search_options_request(void *mem_ctx, DATA_BLOB in, void **out)
218 struct asn1_data data;
219 struct ldb_search_options_control *lsoc;
221 if (!asn1_load(&data, in)) {
225 lsoc = talloc(mem_ctx, struct ldb_search_options_control);
230 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
234 if (!asn1_read_Integer(&data, &(lsoc->search_options))) {
238 if (!asn1_end_tag(&data)) {
247 static BOOL decode_paged_results_request(void *mem_ctx, DATA_BLOB in, void **out)
250 struct asn1_data data;
251 struct ldb_paged_control *lprc;
253 if (!asn1_load(&data, in)) {
257 lprc = talloc(mem_ctx, struct ldb_paged_control);
262 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
266 if (!asn1_read_Integer(&data, &(lprc->size))) {
270 if (!asn1_read_OctetString(&data, &cookie)) {
273 lprc->cookie_len = cookie.length;
274 if (lprc->cookie_len) {
275 lprc->cookie = talloc_memdup(lprc, cookie.data, cookie.length);
277 if (!(lprc->cookie)) {
284 if (!asn1_end_tag(&data)) {
293 static BOOL decode_dirsync_request(void *mem_ctx, DATA_BLOB in, void **out)
296 struct asn1_data data;
297 struct ldb_dirsync_control *ldc;
299 if (!asn1_load(&data, in)) {
303 ldc = talloc(mem_ctx, struct ldb_dirsync_control);
308 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
312 if (!asn1_read_Integer(&data, &(ldc->flags))) {
316 if (!asn1_read_Integer(&data, &(ldc->max_attributes))) {
320 if (!asn1_read_OctetString(&data, &cookie)) {
323 ldc->cookie_len = cookie.length;
324 if (ldc->cookie_len) {
325 ldc->cookie = talloc_memdup(ldc, cookie.data, cookie.length);
327 if (!(ldc->cookie)) {
334 if (!asn1_end_tag(&data)) {
343 /* seem that this controls has 2 forms one in case it is used with
344 * a Search Request and another when used ina Search Response
346 static BOOL decode_asq_control(void *mem_ctx, DATA_BLOB in, void **out)
348 DATA_BLOB source_attribute;
349 struct asn1_data data;
350 struct ldb_asq_control *lac;
352 if (!asn1_load(&data, in)) {
356 lac = talloc(mem_ctx, struct ldb_asq_control);
361 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
365 if (asn1_peek_tag(&data, ASN1_OCTET_STRING)) {
367 if (!asn1_read_OctetString(&data, &source_attribute)) {
370 lac->src_attr_len = source_attribute.length;
371 if (lac->src_attr_len) {
372 lac->source_attribute = talloc_strndup(lac, (const char *)source_attribute.data, source_attribute.length);
374 if (!(lac->source_attribute)) {
378 lac->source_attribute = NULL;
383 } else if (asn1_peek_tag(&data, ASN1_ENUMERATED)) {
385 if (!asn1_read_enumerated(&data, &(lac->result))) {
395 if (!asn1_end_tag(&data)) {
404 static BOOL decode_domain_scope_request(void *mem_ctx, DATA_BLOB in, void **out)
406 if (in.length != 0) {
413 static BOOL decode_notification_request(void *mem_ctx, DATA_BLOB in, void **out)
415 if (in.length != 0) {
422 static BOOL decode_show_deleted_request(void *mem_ctx, DATA_BLOB in, void **out)
424 if (in.length != 0) {
431 static BOOL decode_manageDSAIT_request(void *mem_ctx, DATA_BLOB in, void **out)
433 if (in.length != 0) {
440 static BOOL decode_vlv_request(void *mem_ctx, DATA_BLOB in, void **out)
442 DATA_BLOB assertion_value, context_id;
443 struct asn1_data data;
444 struct ldb_vlv_req_control *lvrc;
446 if (!asn1_load(&data, in)) {
450 lvrc = talloc(mem_ctx, struct ldb_vlv_req_control);
455 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
459 if (!asn1_read_Integer(&data, &(lvrc->beforeCount))) {
463 if (!asn1_read_Integer(&data, &(lvrc->afterCount))) {
467 if (asn1_peek_tag(&data, ASN1_CONTEXT(0))) {
471 if (!asn1_start_tag(&data, ASN1_CONTEXT(0))) {
475 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
479 if (!asn1_read_Integer(&data, &(lvrc->match.byOffset.offset))) {
483 if (!asn1_read_Integer(&data, &(lvrc->match.byOffset.contentCount))) {
487 if (!asn1_end_tag(&data)) { /*SEQUENCE*/
491 if (!asn1_end_tag(&data)) { /*CONTEXT*/
499 if (!asn1_start_tag(&data, ASN1_CONTEXT(1))) {
503 if (!asn1_read_OctetString(&data, &assertion_value)) {
506 lvrc->match.gtOrEq.value_len = assertion_value.length;
507 if (lvrc->match.gtOrEq.value_len) {
508 lvrc->match.gtOrEq.value = talloc_memdup(lvrc, assertion_value.data, assertion_value.length);
510 if (!(lvrc->match.gtOrEq.value)) {
514 lvrc->match.gtOrEq.value = NULL;
517 if (!asn1_end_tag(&data)) { /*CONTEXT*/
522 if (asn1_peek_tag(&data, ASN1_OCTET_STRING)) {
523 if (!asn1_read_OctetString(&data, &context_id)) {
526 lvrc->ctxid_len = context_id.length;
527 if (lvrc->ctxid_len) {
528 lvrc->contextId = talloc_memdup(lvrc, context_id.data, context_id.length);
530 if (!(lvrc->contextId)) {
534 lvrc->contextId = NULL;
537 lvrc->contextId = NULL;
541 if (!asn1_end_tag(&data)) {
550 static BOOL decode_vlv_response(void *mem_ctx, DATA_BLOB in, void **out)
552 DATA_BLOB context_id;
553 struct asn1_data data;
554 struct ldb_vlv_resp_control *lvrc;
556 if (!asn1_load(&data, in)) {
560 lvrc = talloc(mem_ctx, struct ldb_vlv_resp_control);
565 if (!asn1_start_tag(&data, ASN1_SEQUENCE(0))) {
569 if (!asn1_read_Integer(&data, &(lvrc->targetPosition))) {
573 if (!asn1_read_Integer(&data, &(lvrc->contentCount))) {
577 if (!asn1_read_enumerated(&data, &(lvrc->vlv_result))) {
581 if (asn1_peek_tag(&data, ASN1_OCTET_STRING)) {
582 if (!asn1_read_OctetString(&data, &context_id)) {
585 lvrc->contextId = talloc_strndup(lvrc, (const char *)context_id.data, context_id.length);
586 if (!lvrc->contextId) {
589 lvrc->ctxid_len = context_id.length;
591 lvrc->contextId = NULL;
595 if (!asn1_end_tag(&data)) {
604 static BOOL encode_server_sort_response(void *mem_ctx, void *in, DATA_BLOB *out)
606 struct ldb_sort_resp_control *lsrc = talloc_get_type(in, struct ldb_sort_resp_control);
607 struct asn1_data data;
611 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
615 if (!asn1_write_enumerated(&data, lsrc->result)) {
619 if (lsrc->attr_desc) {
620 if (!asn1_write_OctetString(&data, lsrc->attr_desc, strlen(lsrc->attr_desc))) {
625 if (!asn1_pop_tag(&data)) {
629 *out = data_blob_talloc(mem_ctx, data.data, data.length);
630 if (out->data == NULL) {
637 static BOOL encode_server_sort_request(void *mem_ctx, void *in, DATA_BLOB *out)
639 struct ldb_server_sort_control **lssc = talloc_get_type(in, struct ldb_server_sort_control *);
640 struct asn1_data data;
645 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
649 for (num = 0; lssc[num]; num++) {
650 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
654 if (!asn1_write_OctetString(&data, lssc[num]->attributeName, strlen(lssc[num]->attributeName))) {
658 if (lssc[num]->orderingRule) {
659 if (!asn1_write_OctetString(&data, lssc[num]->orderingRule, strlen(lssc[num]->orderingRule))) {
664 if (lssc[num]->reverse) {
665 if (!asn1_write_BOOLEAN(&data, lssc[num]->reverse)) {
670 if (!asn1_pop_tag(&data)) {
675 if (!asn1_pop_tag(&data)) {
679 *out = data_blob_talloc(mem_ctx, data.data, data.length);
680 if (out->data == NULL) {
687 static BOOL encode_extended_dn_request(void *mem_ctx, void *in, DATA_BLOB *out)
689 struct ldb_extended_dn_control *ledc = talloc_get_type(in, struct ldb_extended_dn_control);
690 struct asn1_data data;
694 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
698 if (!asn1_write_Integer(&data, ledc->type)) {
702 if (!asn1_pop_tag(&data)) {
706 *out = data_blob_talloc(mem_ctx, data.data, data.length);
707 if (out->data == NULL) {
714 static BOOL encode_sd_flags_request(void *mem_ctx, void *in, DATA_BLOB *out)
716 struct ldb_sd_flags_control *lsdfc = talloc_get_type(in, struct ldb_sd_flags_control);
717 struct asn1_data data;
721 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
725 if (!asn1_write_Integer(&data, lsdfc->secinfo_flags)) {
729 if (!asn1_pop_tag(&data)) {
733 *out = data_blob_talloc(mem_ctx, data.data, data.length);
734 if (out->data == NULL) {
741 static BOOL encode_search_options_request(void *mem_ctx, void *in, DATA_BLOB *out)
743 struct ldb_search_options_control *lsoc = talloc_get_type(in, struct ldb_search_options_control);
744 struct asn1_data data;
748 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
752 if (!asn1_write_Integer(&data, lsoc->search_options)) {
756 if (!asn1_pop_tag(&data)) {
760 *out = data_blob_talloc(mem_ctx, data.data, data.length);
761 if (out->data == NULL) {
768 static BOOL encode_paged_results_request(void *mem_ctx, void *in, DATA_BLOB *out)
770 struct ldb_paged_control *lprc = talloc_get_type(in, struct ldb_paged_control);
771 struct asn1_data data;
775 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
779 if (!asn1_write_Integer(&data, lprc->size)) {
783 if (!asn1_write_OctetString(&data, lprc->cookie, lprc->cookie_len)) {
787 if (!asn1_pop_tag(&data)) {
791 *out = data_blob_talloc(mem_ctx, data.data, data.length);
792 if (out->data == NULL) {
799 /* seem that this controls has 2 forms one in case it is used with
800 * a Search Request and another when used ina Search Response
802 static BOOL encode_asq_control(void *mem_ctx, void *in, DATA_BLOB *out)
804 struct ldb_asq_control *lac = talloc_get_type(in, struct ldb_asq_control);
805 struct asn1_data data;
809 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
815 if (!asn1_write_OctetString(&data, lac->source_attribute, lac->src_attr_len)) {
819 if (!asn1_write_enumerated(&data, lac->result)) {
824 if (!asn1_pop_tag(&data)) {
828 *out = data_blob_talloc(mem_ctx, data.data, data.length);
829 if (out->data == NULL) {
836 static BOOL encode_dirsync_request(void *mem_ctx, void *in, DATA_BLOB *out)
838 struct ldb_dirsync_control *ldc = talloc_get_type(in, struct ldb_dirsync_control);
839 struct asn1_data data;
843 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
847 if (!asn1_write_Integer(&data, ldc->flags)) {
851 if (!asn1_write_Integer(&data, ldc->max_attributes)) {
855 if (!asn1_write_OctetString(&data, ldc->cookie, ldc->cookie_len)) {
859 if (!asn1_pop_tag(&data)) {
863 *out = data_blob_talloc(mem_ctx, data.data, data.length);
864 if (out->data == NULL) {
871 static BOOL encode_domain_scope_request(void *mem_ctx, void *in, DATA_BLOB *out)
877 *out = data_blob(NULL, 0);
881 static BOOL encode_notification_request(void *mem_ctx, void *in, DATA_BLOB *out)
887 *out = data_blob(NULL, 0);
891 static BOOL encode_show_deleted_request(void *mem_ctx, void *in, DATA_BLOB *out)
897 *out = data_blob(NULL, 0);
901 static BOOL encode_manageDSAIT_request(void *mem_ctx, void *in, DATA_BLOB *out)
907 *out = data_blob(NULL, 0);
911 static BOOL encode_vlv_request(void *mem_ctx, void *in, DATA_BLOB *out)
913 struct ldb_vlv_req_control *lvrc = talloc_get_type(in, struct ldb_vlv_req_control);
914 struct asn1_data data;
918 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
922 if (!asn1_write_Integer(&data, lvrc->beforeCount)) {
926 if (!asn1_write_Integer(&data, lvrc->afterCount)) {
930 if (lvrc->type == 0) {
931 if (!asn1_push_tag(&data, ASN1_CONTEXT(0))) {
935 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
939 if (!asn1_write_Integer(&data, lvrc->match.byOffset.offset)) {
943 if (!asn1_write_Integer(&data, lvrc->match.byOffset.contentCount)) {
947 if (!asn1_pop_tag(&data)) { /*SEQUENCE*/
951 if (!asn1_pop_tag(&data)) { /*CONTEXT*/
955 if (!asn1_push_tag(&data, ASN1_CONTEXT(1))) {
959 if (!asn1_write_OctetString(&data, lvrc->match.gtOrEq.value, lvrc->match.gtOrEq.value_len)) {
963 if (!asn1_pop_tag(&data)) { /*CONTEXT*/
968 if (lvrc->ctxid_len) {
969 if (!asn1_write_OctetString(&data, lvrc->contextId, lvrc->ctxid_len)) {
974 if (!asn1_pop_tag(&data)) {
978 *out = data_blob_talloc(mem_ctx, data.data, data.length);
979 if (out->data == NULL) {
986 static BOOL encode_vlv_response(void *mem_ctx, void *in, DATA_BLOB *out)
988 struct ldb_vlv_resp_control *lvrc = talloc_get_type(in, struct ldb_vlv_resp_control);
989 struct asn1_data data;
993 if (!asn1_push_tag(&data, ASN1_SEQUENCE(0))) {
997 if (!asn1_write_Integer(&data, lvrc->targetPosition)) {
1001 if (!asn1_write_Integer(&data, lvrc->contentCount)) {
1005 if (!asn1_write_enumerated(&data, lvrc->vlv_result)) {
1009 if (lvrc->ctxid_len) {
1010 if (!asn1_write_OctetString(&data, lvrc->contextId, lvrc->ctxid_len)) {
1015 if (!asn1_pop_tag(&data)) {
1019 *out = data_blob_talloc(mem_ctx, data.data, data.length);
1020 if (out->data == NULL) {
1027 struct control_handler ldap_known_controls[] = {
1028 { "1.2.840.113556.1.4.319", decode_paged_results_request, encode_paged_results_request },
1029 { "1.2.840.113556.1.4.529", decode_extended_dn_request, encode_extended_dn_request },
1030 { "1.2.840.113556.1.4.473", decode_server_sort_request, encode_server_sort_request },
1031 { "1.2.840.113556.1.4.474", decode_server_sort_response, encode_server_sort_response },
1032 { "1.2.840.113556.1.4.1504", decode_asq_control, encode_asq_control },
1033 { "1.2.840.113556.1.4.841", decode_dirsync_request, encode_dirsync_request },
1034 { "1.2.840.113556.1.4.528", decode_notification_request, encode_notification_request },
1035 { "1.2.840.113556.1.4.417", decode_show_deleted_request, encode_show_deleted_request },
1036 { "1.2.840.113556.1.4.801", decode_sd_flags_request, encode_sd_flags_request },
1037 { "1.2.840.113556.1.4.1339", decode_domain_scope_request, encode_domain_scope_request },
1038 { "1.2.840.113556.1.4.1340", decode_search_options_request, encode_search_options_request },
1039 { "2.16.840.1.113730.3.4.2", decode_manageDSAIT_request, encode_manageDSAIT_request },
1040 { "2.16.840.1.113730.3.4.9", decode_vlv_request, encode_vlv_request },
1041 { "2.16.840.1.113730.3.4.10", decode_vlv_response, encode_vlv_response },
1042 { NULL, NULL, NULL }
1045 BOOL ldap_decode_control(void *mem_ctx, struct asn1_data *data, struct ldb_control *ctrl)
1051 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
1055 if (!asn1_read_OctetString(data, &oid)) {
1058 ctrl->oid = talloc_strndup(mem_ctx, (char *)oid.data, oid.length);
1063 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
1065 if (!asn1_read_BOOLEAN(data, &critical)) {
1068 ctrl->critical = critical;
1070 ctrl->critical = False;
1075 if (!asn1_peek_tag(data, ASN1_OCTET_STRING)) {
1079 if (!asn1_read_OctetString(data, &value)) {
1083 for (i = 0; ldap_known_controls[i].oid != NULL; i++) {
1084 if (strcmp(ldap_known_controls[i].oid, ctrl->oid) == 0) {
1085 if (!ldap_known_controls[i].decode(mem_ctx, value, &ctrl->data)) {
1091 if (ldap_known_controls[i].oid == NULL) {
1096 if (!asn1_end_tag(data)) {
1103 BOOL ldap_encode_control(void *mem_ctx, struct asn1_data *data, struct ldb_control *ctrl)
1108 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
1112 if (!asn1_write_OctetString(data, ctrl->oid, strlen(ctrl->oid))) {
1116 if (ctrl->critical) {
1117 if (!asn1_write_BOOLEAN(data, ctrl->critical)) {
1126 for (i = 0; ldap_known_controls[i].oid != NULL; i++) {
1127 if (strcmp(ldap_known_controls[i].oid, ctrl->oid) == 0) {
1128 if (!ldap_known_controls[i].encode(mem_ctx, ctrl->data, &value)) {
1134 if (ldap_known_controls[i].oid == NULL) {
1138 if (!asn1_write_OctetString(data, value.data, value.length)) {
1143 if (!asn1_pop_tag(data)) {