s4:libcli/ldap Rename ldap.h to libcli_ldap.h
[garming/samba-autobuild/.git] / source4 / libcli / ldap / ldap_controls.c
index 930d97c40d0b2b3a0765c3a3fc27d5bfe596aeee..b0c024a361b609f734ba891f35a8a8e5f8a1b77c 100644 (file)
 
 #include "includes.h"
 #include "../lib/util/asn1.h"
-#include "libcli/ldap/ldap.h"
+#include "libcli/ldap/libcli_ldap.h"
 #include "lib/ldb/include/ldb.h"
 #include "libcli/ldap/ldap_proto.h"
+#include "dsdb/samdb/samdb.h"
 
-struct control_handler {
-       const char *oid;
-       bool (*decode)(void *mem_ctx, DATA_BLOB in, void **out);
-       bool (*encode)(void *mem_ctx, void *in, DATA_BLOB *out);
-};
-
-static bool decode_server_sort_response(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_server_sort_response(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        DATA_BLOB attr;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_sort_resp_control *lsrc;
@@ -76,8 +72,9 @@ static bool decode_server_sort_response(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_server_sort_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_server_sort_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        DATA_BLOB attr;
        DATA_BLOB rule;
        struct asn1_data *data = asn1_init(mem_ctx);
@@ -129,9 +126,9 @@ static bool decode_server_sort_request(void *mem_ctx, DATA_BLOB in, void **out)
                        }
                }
 
-               if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
+               if (asn1_peek_tag(data, ASN1_CONTEXT_SIMPLE(1))) {
                        bool reverse;
-                       if (!asn1_read_BOOLEAN(data, &reverse)) {
+                       if (!asn1_read_BOOLEAN_context(data, &reverse, 1)) {
                        return false;
                        }
                        lssc[num]->reverse = reverse;
@@ -155,8 +152,9 @@ static bool decode_server_sort_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_extended_dn_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_extended_dn_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        struct asn1_data *data;
        struct ldb_extended_dn_control *ledc;
 
@@ -195,8 +193,9 @@ static bool decode_extended_dn_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_sd_flags_control *lsdfc;
 
@@ -215,7 +214,7 @@ static bool decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void **out)
                return false;
        }
 
-       if (!asn1_read_Integer(data, &(lsdfc->secinfo_flags))) {
+       if (!asn1_read_Integer(data, (int *) &(lsdfc->secinfo_flags))) {
                return false;
        }
 
@@ -228,8 +227,9 @@ static bool decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_search_options_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_search_options_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_search_options_control *lsoc;
 
@@ -248,7 +248,7 @@ static bool decode_search_options_request(void *mem_ctx, DATA_BLOB in, void **ou
                return false;
        }
 
-       if (!asn1_read_Integer(data, &(lsoc->search_options))) {
+       if (!asn1_read_Integer(data, (int *) &(lsoc->search_options))) {
                return false;
        }
 
@@ -261,8 +261,9 @@ static bool decode_search_options_request(void *mem_ctx, DATA_BLOB in, void **ou
        return true;
 }
 
-static bool decode_paged_results_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_paged_results_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        DATA_BLOB cookie;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_paged_control *lprc;
@@ -309,8 +310,9 @@ static bool decode_paged_results_request(void *mem_ctx, DATA_BLOB in, void **out
        return true;
 }
 
-static bool decode_dirsync_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_dirsync_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        DATA_BLOB cookie;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_dirsync_control *ldc;
@@ -364,8 +366,9 @@ static bool decode_dirsync_request(void *mem_ctx, DATA_BLOB in, void **out)
 /* seem that this controls has 2 forms one in case it is used with
  * a Search Request and another when used ina Search Response
  */
-static bool decode_asq_control(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_asq_control(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        DATA_BLOB source_attribute;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_asq_control *lac;
@@ -424,7 +427,7 @@ static bool decode_asq_control(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_domain_scope_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_domain_scope_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
        if (in.length != 0) {
                return false;
@@ -433,7 +436,7 @@ static bool decode_domain_scope_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_notification_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_notification_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
        if (in.length != 0) {
                return false;
@@ -442,7 +445,7 @@ static bool decode_notification_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_show_deleted_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_show_deleted_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
        if (in.length != 0) {
                return false;
@@ -451,7 +454,7 @@ static bool decode_show_deleted_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_permissive_modify_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_show_recycled_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
        if (in.length != 0) {
                return false;
@@ -460,7 +463,7 @@ static bool decode_permissive_modify_request(void *mem_ctx, DATA_BLOB in, void *
        return true;
 }
 
-static bool decode_manageDSAIT_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_show_deactivated_link_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
        if (in.length != 0) {
                return false;
@@ -469,8 +472,27 @@ static bool decode_manageDSAIT_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_vlv_request(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_permissive_modify_request(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       if (in.length != 0) {
+               return false;
+       }
+
+       return true;
+}
+
+static bool decode_manageDSAIT_request(void *mem_ctx, DATA_BLOB in, void *_out)
+{
+       if (in.length != 0) {
+               return false;
+       }
+
+       return true;
+}
+
+static bool decode_vlv_request(void *mem_ctx, DATA_BLOB in, void *_out)
+{
+       void **out = (void **)_out;
        DATA_BLOB assertion_value, context_id;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_vlv_req_control *lvrc;
@@ -581,8 +603,9 @@ static bool decode_vlv_request(void *mem_ctx, DATA_BLOB in, void **out)
        return true;
 }
 
-static bool decode_vlv_response(void *mem_ctx, DATA_BLOB in, void **out)
+static bool decode_vlv_response(void *mem_ctx, DATA_BLOB in, void *_out)
 {
+       void **out = (void **)_out;
        DATA_BLOB context_id;
        struct asn1_data *data = asn1_init(mem_ctx);
        struct ldb_vlv_resp_control *lvrc;
@@ -683,6 +706,13 @@ static bool encode_server_sort_request(void *mem_ctx, void *in, DATA_BLOB *out)
                return false;
        }
 
+       /*
+         RFC2891 section 1.1:
+           SortKeyList ::= SEQUENCE OF SEQUENCE {
+             attributeType   AttributeDescription,
+             orderingRule    [0] MatchingRuleId OPTIONAL,
+             reverseOrder    [1] BOOLEAN DEFAULT FALSE }
+       */
        for (num = 0; lssc[num]; num++) {
                if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
                        return false;
@@ -699,7 +729,7 @@ static bool encode_server_sort_request(void *mem_ctx, void *in, DATA_BLOB *out)
                }
 
                if (lssc[num]->reverse) {
-                       if (!asn1_write_BOOLEAN(data, lssc[num]->reverse)) {
+                       if (!asn1_write_BOOLEAN_context(data, lssc[num]->reverse, 1)) {
                                return false;
                        }
                }
@@ -949,6 +979,26 @@ static bool encode_show_deleted_request(void *mem_ctx, void *in, DATA_BLOB *out)
        return true;
 }
 
+static bool encode_show_recycled_request(void *mem_ctx, void *in, DATA_BLOB *out)
+{
+       if (in) {
+               return false;
+       }
+
+       *out = data_blob(NULL, 0);
+       return true;
+}
+
+static bool encode_show_deactivated_link_request(void *mem_ctx, void *in, DATA_BLOB *out)
+{
+       if (in) {
+               return false;
+       }
+
+       *out = data_blob(NULL, 0);
+       return true;
+}
+
 static bool encode_permissive_modify_request(void *mem_ctx, void *in, DATA_BLOB *out)
 {
        if (in) {
@@ -1087,144 +1137,192 @@ static bool encode_vlv_response(void *mem_ctx, void *in, DATA_BLOB *out)
        return true;
 }
 
-struct control_handler ldap_known_controls[] = {
-       { "1.2.840.113556.1.4.319", decode_paged_results_request, encode_paged_results_request },
-       { "1.2.840.113556.1.4.529", decode_extended_dn_request, encode_extended_dn_request },
-       { "1.2.840.113556.1.4.473", decode_server_sort_request, encode_server_sort_request },
-       { "1.2.840.113556.1.4.474", decode_server_sort_response, encode_server_sort_response },
-       { "1.2.840.113556.1.4.1504", decode_asq_control, encode_asq_control },
-       { "1.2.840.113556.1.4.841", decode_dirsync_request, encode_dirsync_request },
-       { "1.2.840.113556.1.4.528", decode_notification_request, encode_notification_request },
-       { "1.2.840.113556.1.4.417", decode_show_deleted_request, encode_show_deleted_request },
-       { "1.2.840.113556.1.4.1413", decode_permissive_modify_request, encode_permissive_modify_request },
-       { "1.2.840.113556.1.4.801", decode_sd_flags_request, encode_sd_flags_request },
-       { "1.2.840.113556.1.4.1339", decode_domain_scope_request, encode_domain_scope_request },
-       { "1.2.840.113556.1.4.1340", decode_search_options_request, encode_search_options_request },
-       { "2.16.840.1.113730.3.4.2", decode_manageDSAIT_request, encode_manageDSAIT_request },
-       { "2.16.840.1.113730.3.4.9", decode_vlv_request, encode_vlv_request },
-       { "2.16.840.1.113730.3.4.10", decode_vlv_response, encode_vlv_response },
-/* DSDB_CONTROL_CURRENT_PARTITION_OID is internal only, and has no network representation */
-       { "1.3.6.1.4.1.7165.4.3.2", NULL, NULL },
-/* DSDB_EXTENDED_REPLICATED_OBJECTS_OID is internal only, and has no network representation */
-       { "1.3.6.1.4.1.7165.4.4.1", NULL, NULL },
-       { NULL, NULL, NULL }
-};
-
-bool ldap_decode_control_value(void *mem_ctx, DATA_BLOB value, struct ldb_control *ctrl)
+static bool encode_openldap_dereference(void *mem_ctx, void *in, DATA_BLOB *out)
 {
-       int i;
+       struct dsdb_openldap_dereference_control *control = talloc_get_type(in, struct dsdb_openldap_dereference_control);
+       int i,j;
+       struct asn1_data *data = asn1_init(mem_ctx);
 
-       for (i = 0; ldap_known_controls[i].oid != NULL; i++) {
-               if (strcmp(ldap_known_controls[i].oid, ctrl->oid) == 0) {
-                       if (!ldap_known_controls[i].decode || !ldap_known_controls[i].decode(mem_ctx, value, &ctrl->data)) {
+       if (!data) return false;
+       
+       if (!control) return false;
+       
+       if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
+               return false;
+       }
+       
+       for (i=0; control->dereference && control->dereference[i]; i++) {
+               if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
+                       return false;
+               }
+               if (!asn1_write_OctetString(data, control->dereference[i]->source_attribute, strlen(control->dereference[i]->source_attribute))) {
+                       return false;
+               }
+               if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
+                       return false;
+               }
+               for (j=0; control->dereference && control->dereference[i]->dereference_attribute[j]; j++) {
+                       if (!asn1_write_OctetString(data, control->dereference[i]->dereference_attribute[j], 
+                                                   strlen(control->dereference[i]->dereference_attribute[j]))) {
                                return false;
                        }
-                       break;
                }
+               
+               asn1_pop_tag(data);
+               asn1_pop_tag(data);
        }
-       if (ldap_known_controls[i].oid == NULL) {
+       asn1_pop_tag(data);
+
+       *out = data_blob_talloc(mem_ctx, data->data, data->length);
+       if (out->data == NULL) {
                return false;
        }
-
+       talloc_free(data);
        return true;
 }
 
-bool ldap_decode_control_wrapper(void *mem_ctx, struct asn1_data *data, struct ldb_control *ctrl, DATA_BLOB *value)
+static bool decode_openldap_dereference(void *mem_ctx, DATA_BLOB in, void *_out)
 {
-       DATA_BLOB oid;
+       void **out = (void **)_out;
+       struct asn1_data *data = asn1_init(mem_ctx);
+       struct dsdb_openldap_dereference_result_control *control;
+       struct dsdb_openldap_dereference_result **r = NULL;
+       int i = 0;
+       if (!data) return false;
 
-       if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
+       control = talloc(mem_ctx, struct dsdb_openldap_dereference_result_control);
+       if (!control) return false;
+
+       if (!asn1_load(data, in)) {
                return false;
        }
 
-       if (!asn1_read_OctetString(data, mem_ctx, &oid)) {
+       control = talloc(mem_ctx, struct dsdb_openldap_dereference_result_control);
+       if (!control) {
                return false;
        }
-       ctrl->oid = talloc_strndup(mem_ctx, (char *)oid.data, oid.length);
-       if (!ctrl->oid) {
+
+       if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
                return false;
        }
 
-       if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
-               bool critical;
-               if (!asn1_read_BOOLEAN(data, &critical)) {
+       while (asn1_tag_remaining(data) > 0) {                                  
+               r = talloc_realloc(control, r, struct dsdb_openldap_dereference_result *, i + 2);
+               if (!r) {
+                       return false;
+               }
+               r[i] = talloc_zero(r, struct dsdb_openldap_dereference_result);
+               if (!r[i]) {
                        return false;
                }
-               ctrl->critical = critical;
-       } else {
-               ctrl->critical = false;
-       }
-
-       ctrl->data = NULL;
-
-       if (!asn1_peek_tag(data, ASN1_OCTET_STRING)) {
-               *value = data_blob(NULL, 0);
-               goto end_tag;
-       }
 
-       if (!asn1_read_OctetString(data, mem_ctx, value)) {
-               return false;
+               if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
+                       return false;
+               }
+               
+               asn1_read_OctetString_talloc(r[i], data, &r[i]->source_attribute);
+               asn1_read_OctetString_talloc(r[i], data, &r[i]->dereferenced_dn);
+               if (asn1_peek_tag(data, ASN1_CONTEXT(0))) {
+                       if (!asn1_start_tag(data, ASN1_CONTEXT(0))) {
+                               return false;
+                       }
+                       
+                       ldap_decode_attribs_bare(r, data, &r[i]->attributes,
+                                                &r[i]->num_attributes);
+                       
+                       if (!asn1_end_tag(data)) {
+                               return false;
+                       }
+               }
+               if (!asn1_end_tag(data)) {
+                       return false;
+               }
+               i++;
+               r[i] = NULL;
        }
 
-end_tag:
        if (!asn1_end_tag(data)) {
                return false;
        }
 
+       control->attributes = r;
+       *out = control;
+
        return true;
 }
 
-bool ldap_encode_control(void *mem_ctx, struct asn1_data *data, struct ldb_control *ctrl)
+static bool encode_relax_request(void *mem_ctx, void *in, DATA_BLOB *out)
 {
-       DATA_BLOB value;
-       int i;
-
-       for (i = 0; ldap_known_controls[i].oid != NULL; i++) {
-               if (strcmp(ldap_known_controls[i].oid, ctrl->oid) == 0) {
-                       if (!ldap_known_controls[i].encode) {
-                               if (ctrl->critical) {
-                                       return false;
-                               } else {
-                                       /* not encoding this control */
-                                       return true;
-                               }
-                       }
-                       if (!ldap_known_controls[i].encode(mem_ctx, ctrl->data, &value)) {
-                               return false;
-                       }
-                       break;
-               }
-       }
-       if (ldap_known_controls[i].oid == NULL) {
+       if (in) {
                return false;
        }
 
-       if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
-               return false;
-       }
+       *out = data_blob(NULL, 0);
+       return true;
+}
 
-       if (!asn1_write_OctetString(data, ctrl->oid, strlen(ctrl->oid))) {
+static bool decode_relax_request(void *mem_ctx, DATA_BLOB in, void *_out)
+{
+       if (in.length != 0) {
                return false;
        }
 
-       if (ctrl->critical) {
-               if (!asn1_write_BOOLEAN(data, ctrl->critical)) {
-                       return false;
-               }
-       }
-
-       if (!ctrl->data) {
-               goto pop_tag;
-       }
+       return true;
+}
 
-       if (!asn1_write_OctetString(data, value.data, value.length)) {
+static bool encode_recalculate_sd_request(void *mem_ctx, void *in, DATA_BLOB *out)
+{
+       if (in) {
                return false;
        }
 
-pop_tag:
-       if (!asn1_pop_tag(data)) {
+       *out = data_blob(NULL, 0);
+       return true;
+}
+
+static bool decode_recalculate_sd_request(void *mem_ctx, DATA_BLOB in, void *_out)
+{
+       if (in.length != 0) {
                return false;
        }
 
        return true;
 }
+
+static const struct ldap_control_handler ldap_known_controls[] = {
+       { "1.2.840.113556.1.4.319", decode_paged_results_request, encode_paged_results_request },
+       { "1.2.840.113556.1.4.529", decode_extended_dn_request, encode_extended_dn_request },
+       { "1.2.840.113556.1.4.473", decode_server_sort_request, encode_server_sort_request },
+       { "1.2.840.113556.1.4.474", decode_server_sort_response, encode_server_sort_response },
+       { "1.2.840.113556.1.4.1504", decode_asq_control, encode_asq_control },
+       { "1.2.840.113556.1.4.841", decode_dirsync_request, encode_dirsync_request },
+       { "1.2.840.113556.1.4.528", decode_notification_request, encode_notification_request },
+       { "1.2.840.113556.1.4.417", decode_show_deleted_request, encode_show_deleted_request },
+       { "1.2.840.113556.1.4.2064", decode_show_recycled_request, encode_show_recycled_request },
+       { "1.2.840.113556.1.4.2065", decode_show_deactivated_link_request, encode_show_deactivated_link_request },
+       { "1.2.840.113556.1.4.1413", decode_permissive_modify_request, encode_permissive_modify_request },
+       { "1.2.840.113556.1.4.801", decode_sd_flags_request, encode_sd_flags_request },
+       { "1.2.840.113556.1.4.1339", decode_domain_scope_request, encode_domain_scope_request },
+       { "1.2.840.113556.1.4.1340", decode_search_options_request, encode_search_options_request },
+       { "2.16.840.1.113730.3.4.2", decode_manageDSAIT_request, encode_manageDSAIT_request },
+       { "2.16.840.1.113730.3.4.9", decode_vlv_request, encode_vlv_request },
+       { "2.16.840.1.113730.3.4.10", decode_vlv_response, encode_vlv_response },
+/* DSDB_CONTROL_CURRENT_PARTITION_OID is internal only, and has no network representation */
+       { "1.3.6.1.4.1.7165.4.3.2", NULL, NULL },
+/* DSDB_CONTROL_DN_STORAGE_FORMAT_OID is internal only, and has no network representation */
+       { "1.3.6.1.4.1.7165.4.3.4", NULL, NULL },
+/* LDB_CONTROL_AS_SYSTEM_OID is internal only, and has no network representation */
+       { "1.3.6.1.4.1.7165.4.3.7", NULL, NULL },
+/* DSDB_EXTENDED_REPLICATED_OBJECTS_OID is internal only, and has no network representation */
+       { "1.3.6.1.4.1.7165.4.4.1", NULL, NULL },
+       { LDB_CONTROL_RECALCULATE_SD_OID,  decode_recalculate_sd_request, encode_recalculate_sd_request},
+       { DSDB_OPENLDAP_DEREFERENCE_CONTROL, decode_openldap_dereference, encode_openldap_dereference},
+       { LDB_CONTROL_RELAX_OID, decode_relax_request, encode_relax_request },
+       { NULL, NULL, NULL }
+};
+
+const struct ldap_control_handler *samba_ldap_control_handlers(void)
+{
+       return ldap_known_controls;
+}
+