478481b422cde37b9e8a832fc3eef77905b21548
[samba.git] / source4 / libcli / ldap / ldap_controls.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    LDAP protocol helper functions for SAMBA
4    
5    Copyright (C) Simo Sorce 2005
6     
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 3 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19    
20 */
21
22 #include "includes.h"
23 #include "../lib/util/asn1.h"
24 #include "libcli/ldap/libcli_ldap.h"
25 #include "libcli/ldap/ldap_proto.h"
26 #include "dsdb/samdb/samdb.h"
27
28 static bool decode_server_sort_response(void *mem_ctx, DATA_BLOB in, void *_out)
29 {
30         void **out = (void **)_out;
31         DATA_BLOB attr;
32         struct asn1_data *data = asn1_init(mem_ctx);
33         struct ldb_sort_resp_control *lsrc;
34
35         if (!data) return false;
36
37         if (!asn1_load(data, in)) {
38                 return false;
39         }
40
41         lsrc = talloc(mem_ctx, struct ldb_sort_resp_control);
42         if (!lsrc) {
43                 return false;
44         }
45
46         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
47                 return false;
48         }
49
50         if (!asn1_read_enumerated(data, &(lsrc->result))) {
51                 return false;
52         }
53
54         lsrc->attr_desc = NULL;
55         if (asn1_peek_tag(data, ASN1_OCTET_STRING)) {
56                 if (!asn1_read_OctetString(data, mem_ctx, &attr)) {
57                         return false;
58                 }
59                 lsrc->attr_desc = talloc_strndup(lsrc, (const char *)attr.data, attr.length);
60                 if (!lsrc->attr_desc) {
61                         return false;
62                 }
63         }
64
65         if (!asn1_end_tag(data)) {
66                 return false;
67         }
68
69         *out = lsrc;
70
71         return true;
72 }
73
74 static bool decode_server_sort_request(void *mem_ctx, DATA_BLOB in, void *_out)
75 {
76         void **out = (void **)_out;
77         DATA_BLOB attr;
78         DATA_BLOB rule;
79         struct asn1_data *data = asn1_init(mem_ctx);
80         struct ldb_server_sort_control **lssc;
81         int num;
82
83         if (!data) return false;
84
85         if (!asn1_load(data, in)) {
86                 return false;
87         }
88
89         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
90                 return false;
91         }
92
93         lssc = NULL;
94
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);
97                 if (!lssc) {
98                         return false;
99                 }
100                 lssc[num] = talloc_zero(lssc, struct ldb_server_sort_control);
101                 if (!lssc[num]) {
102                         return false;
103                 }
104
105                 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
106                         return false;
107                 }
108
109                 if (!asn1_read_OctetString(data, mem_ctx, &attr)) {
110                         return false;
111                 }
112
113                 lssc[num]->attributeName = talloc_strndup(lssc[num], (const char *)attr.data, attr.length);
114                 if (!lssc [num]->attributeName) {
115                         return false;
116                 }
117         
118                 if (asn1_peek_tag(data, ASN1_OCTET_STRING)) {
119                         if (!asn1_read_OctetString(data, mem_ctx, &rule)) {
120                                 return false;
121                         }
122                         lssc[num]->orderingRule = talloc_strndup(lssc[num], (const char *)rule.data, rule.length);
123                         if (!lssc[num]->orderingRule) {
124                                 return false;
125                         }
126                 }
127
128                 if (asn1_peek_tag(data, ASN1_CONTEXT_SIMPLE(1))) {
129                         bool reverse;
130                         if (!asn1_read_BOOLEAN_context(data, &reverse, 1)) {
131                         return false;
132                         }
133                         lssc[num]->reverse = reverse;
134                 }
135         
136                 if (!asn1_end_tag(data)) {
137                         return false;
138                 }
139         }
140
141         if (lssc != NULL) {
142                 lssc[num] = NULL;
143         }
144
145         if (!asn1_end_tag(data)) {
146                 return false;
147         }
148
149         *out = lssc;
150
151         return true;
152 }
153
154 static bool decode_extended_dn_request(void *mem_ctx, DATA_BLOB in, void *_out)
155 {
156         void **out = (void **)_out;
157         struct asn1_data *data;
158         struct ldb_extended_dn_control *ledc;
159
160         /* The content of this control is optional */
161         if (in.length == 0) {
162                 *out = NULL;
163                 return true;
164         }
165
166         data = asn1_init(mem_ctx);
167         if (!data) return false;
168
169         if (!asn1_load(data, in)) {
170                 return false;
171         }
172
173         ledc = talloc(mem_ctx, struct ldb_extended_dn_control);
174         if (!ledc) {
175                 return false;
176         }
177
178         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
179                 return false;
180         }
181
182         if (!asn1_read_Integer(data, &(ledc->type))) {
183                 return false;
184         }
185         
186         if (!asn1_end_tag(data)) {
187                 return false;
188         }
189
190         *out = ledc;
191
192         return true;
193 }
194
195 static bool decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void *_out)
196 {
197         void **out = (void **)_out;
198         struct asn1_data *data = asn1_init(mem_ctx);
199         struct ldb_sd_flags_control *lsdfc;
200
201         if (!data) return false;
202
203         if (!asn1_load(data, in)) {
204                 return false;
205         }
206
207         lsdfc = talloc(mem_ctx, struct ldb_sd_flags_control);
208         if (!lsdfc) {
209                 return false;
210         }
211
212         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
213                 return false;
214         }
215
216         if (!asn1_read_Integer(data, (int *) &(lsdfc->secinfo_flags))) {
217                 return false;
218         }
219
220         if (!asn1_end_tag(data)) {
221                 return false;
222         }
223
224         *out = lsdfc;
225
226         return true;
227 }
228
229 static bool decode_search_options_request(void *mem_ctx, DATA_BLOB in, void *_out)
230 {
231         void **out = (void **)_out;
232         struct asn1_data *data = asn1_init(mem_ctx);
233         struct ldb_search_options_control *lsoc;
234
235         if (!data) return false;
236
237         if (!asn1_load(data, in)) {
238                 return false;
239         }
240
241         lsoc = talloc(mem_ctx, struct ldb_search_options_control);
242         if (!lsoc) {
243                 return false;
244         }
245
246         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
247                 return false;
248         }
249
250         if (!asn1_read_Integer(data, (int *) &(lsoc->search_options))) {
251                 return false;
252         }
253
254         if (!asn1_end_tag(data)) {
255                 return false;
256         }
257
258         *out = lsoc;
259
260         return true;
261 }
262
263 static bool decode_paged_results_request(void *mem_ctx, DATA_BLOB in, void *_out)
264 {
265         void **out = (void **)_out;
266         DATA_BLOB cookie;
267         struct asn1_data *data = asn1_init(mem_ctx);
268         struct ldb_paged_control *lprc;
269
270         if (!data) return false;
271
272         if (!asn1_load(data, in)) {
273                 return false;
274         }
275
276         lprc = talloc(mem_ctx, struct ldb_paged_control);
277         if (!lprc) {
278                 return false;
279         }
280
281         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
282                 return false;
283         }
284
285         if (!asn1_read_Integer(data, &(lprc->size))) {
286                 return false;
287         }
288         
289         if (!asn1_read_OctetString(data, mem_ctx, &cookie)) {
290                 return false;
291         }
292         lprc->cookie_len = cookie.length;
293         if (lprc->cookie_len) {
294                 lprc->cookie = talloc_memdup(lprc, cookie.data, cookie.length);
295
296                 if (!(lprc->cookie)) {
297                         return false;
298                 }
299         } else {
300                 lprc->cookie = NULL;
301         }
302
303         if (!asn1_end_tag(data)) {
304                 return false;
305         }
306
307         *out = lprc;
308
309         return true;
310 }
311
312 static bool decode_dirsync_request(void *mem_ctx, DATA_BLOB in, void *_out)
313 {
314         void **out = (void **)_out;
315         DATA_BLOB cookie;
316         struct asn1_data *data = asn1_init(mem_ctx);
317         struct ldb_dirsync_control *ldc;
318
319         if (!data) return false;
320
321         if (!asn1_load(data, in)) {
322                 return false;
323         }
324
325         ldc = talloc(mem_ctx, struct ldb_dirsync_control);
326         if (!ldc) {
327                 return false;
328         }
329
330         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
331                 return false;
332         }
333
334         if (!asn1_read_Integer(data, &(ldc->flags))) {
335                 return false;
336         }
337         
338         if (!asn1_read_Integer(data, &(ldc->max_attributes))) {
339                 return false;
340         }
341         
342         if (!asn1_read_OctetString(data, mem_ctx, &cookie)) {
343                 return false;
344         }
345         ldc->cookie_len = cookie.length;
346         if (ldc->cookie_len) {
347                 ldc->cookie = talloc_memdup(ldc, cookie.data, cookie.length);
348
349                 if (!(ldc->cookie)) {
350                         return false;
351                 }
352         } else {
353                 ldc->cookie = NULL;
354         }
355
356         if (!asn1_end_tag(data)) {
357                 return false;
358         }
359
360         *out = ldc;
361
362         return true;
363 }
364
365 /* seem that this controls has 2 forms one in case it is used with
366  * a Search Request and another when used ina Search Response
367  */
368 static bool decode_asq_control(void *mem_ctx, DATA_BLOB in, void *_out)
369 {
370         void **out = (void **)_out;
371         DATA_BLOB source_attribute;
372         struct asn1_data *data = asn1_init(mem_ctx);
373         struct ldb_asq_control *lac;
374
375         if (!data) return false;
376
377         if (!asn1_load(data, in)) {
378                 return false;
379         }
380
381         lac = talloc(mem_ctx, struct ldb_asq_control);
382         if (!lac) {
383                 return false;
384         }
385
386         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
387                 return false;
388         }
389
390         if (asn1_peek_tag(data, ASN1_OCTET_STRING)) {
391
392                 if (!asn1_read_OctetString(data, mem_ctx, &source_attribute)) {
393                         return false;
394                 }
395                 lac->src_attr_len = source_attribute.length;
396                 if (lac->src_attr_len) {
397                         lac->source_attribute = talloc_strndup(lac, (const char *)source_attribute.data, source_attribute.length);
398
399                         if (!(lac->source_attribute)) {
400                                 return false;
401                         }
402                 } else {
403                         lac->source_attribute = NULL;
404                 }
405
406                 lac->request = 1;
407
408         } else if (asn1_peek_tag(data, ASN1_ENUMERATED)) {
409
410                 if (!asn1_read_enumerated(data, &(lac->result))) {
411                         return false;
412                 }
413
414                 lac->request = 0;
415
416         } else {
417                 return false;
418         }
419
420         if (!asn1_end_tag(data)) {
421                 return false;
422         }
423
424         *out = lac;
425
426         return true;
427 }
428
429 static bool decode_vlv_request(void *mem_ctx, DATA_BLOB in, void *_out)
430 {
431         void **out = (void **)_out;
432         DATA_BLOB assertion_value, context_id;
433         struct asn1_data *data = asn1_init(mem_ctx);
434         struct ldb_vlv_req_control *lvrc;
435
436         if (!data) return false;
437
438         if (!asn1_load(data, in)) {
439                 return false;
440         }
441
442         lvrc = talloc(mem_ctx, struct ldb_vlv_req_control);
443         if (!lvrc) {
444                 return false;
445         }
446
447         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
448                 return false;
449         }
450
451         if (!asn1_read_Integer(data, &(lvrc->beforeCount))) {
452                 return false;
453         }
454         
455         if (!asn1_read_Integer(data, &(lvrc->afterCount))) {
456                 return false;
457         }
458
459         if (asn1_peek_tag(data, ASN1_CONTEXT(0))) {
460
461                 lvrc->type = 0;
462                 
463                 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) {
464                         return false;
465                 }
466
467                 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
468                         return false;
469                 }
470
471                 if (!asn1_read_Integer(data, &(lvrc->match.byOffset.offset))) {
472                         return false;
473                 }
474
475                 if (!asn1_read_Integer(data, &(lvrc->match.byOffset.contentCount))) {
476                         return false;
477                 }
478
479                 if (!asn1_end_tag(data)) { /*SEQUENCE*/
480                         return false;
481                 }
482
483                 if (!asn1_end_tag(data)) { /*CONTEXT*/
484                         return false;
485                 }
486
487         } else {
488
489                 lvrc->type = 1;
490
491                 if (!asn1_start_tag(data, ASN1_CONTEXT(1))) {
492                         return false;
493                 }
494
495                 if (!asn1_read_OctetString(data, mem_ctx, &assertion_value)) {
496                         return false;
497                 }
498                 lvrc->match.gtOrEq.value_len = assertion_value.length;
499                 if (lvrc->match.gtOrEq.value_len) {
500                         lvrc->match.gtOrEq.value = talloc_memdup(lvrc, assertion_value.data, assertion_value.length);
501
502                         if (!(lvrc->match.gtOrEq.value)) {
503                                 return false;
504                         }
505                 } else {
506                         lvrc->match.gtOrEq.value = NULL;
507                 }
508
509                 if (!asn1_end_tag(data)) { /*CONTEXT*/
510                         return false;
511                 }
512         }
513
514         if (asn1_peek_tag(data, ASN1_OCTET_STRING)) {
515                 if (!asn1_read_OctetString(data, mem_ctx, &context_id)) {
516                         return false;
517                 }
518                 lvrc->ctxid_len = context_id.length;
519                 if (lvrc->ctxid_len) {
520                         lvrc->contextId = talloc_memdup(lvrc, context_id.data, context_id.length);
521
522                         if (!(lvrc->contextId)) {
523                                 return false;
524                         }
525                 } else {
526                         lvrc->contextId = NULL;
527                 }
528         } else {
529                 lvrc->contextId = NULL;
530                 lvrc->ctxid_len = 0;
531         }
532
533         if (!asn1_end_tag(data)) {
534                 return false;
535         }
536
537         *out = lvrc;
538
539         return true;
540 }
541
542 static bool decode_vlv_response(void *mem_ctx, DATA_BLOB in, void *_out)
543 {
544         void **out = (void **)_out;
545         DATA_BLOB context_id;
546         struct asn1_data *data = asn1_init(mem_ctx);
547         struct ldb_vlv_resp_control *lvrc;
548
549         if (!data) return false;
550
551         if (!asn1_load(data, in)) {
552                 return false;
553         }
554
555         lvrc = talloc(mem_ctx, struct ldb_vlv_resp_control);
556         if (!lvrc) {
557                 return false;
558         }
559
560         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
561                 return false;
562         }
563
564         if (!asn1_read_Integer(data, &(lvrc->targetPosition))) {
565                 return false;
566         }
567         
568         if (!asn1_read_Integer(data, &(lvrc->contentCount))) {
569                 return false;
570         }
571         
572         if (!asn1_read_enumerated(data, &(lvrc->vlv_result))) {
573                 return false;
574         }
575
576         if (asn1_peek_tag(data, ASN1_OCTET_STRING)) {
577                 if (!asn1_read_OctetString(data, mem_ctx, &context_id)) {
578                         return false;
579                 }
580                 lvrc->contextId = talloc_strndup(lvrc, (const char *)context_id.data, context_id.length);
581                 if (!lvrc->contextId) {
582                         return false;
583                 }
584                 lvrc->ctxid_len = context_id.length;
585         } else {
586                 lvrc->contextId = NULL;
587                 lvrc->ctxid_len = 0;
588         }
589
590         if (!asn1_end_tag(data)) {
591                 return false;
592         }
593
594         *out = lvrc;
595
596         return true;
597 }
598
599 static bool encode_server_sort_response(void *mem_ctx, void *in, DATA_BLOB *out)
600 {
601         struct ldb_sort_resp_control *lsrc = talloc_get_type(in, struct ldb_sort_resp_control);
602         struct asn1_data *data = asn1_init(mem_ctx);
603
604         if (!data) return false;
605
606         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
607                 return false;
608         }
609
610         if (!asn1_write_enumerated(data, lsrc->result)) {
611                 return false;
612         }
613
614         if (lsrc->attr_desc) {
615                 if (!asn1_write_OctetString(data, lsrc->attr_desc, strlen(lsrc->attr_desc))) {
616                         return false;
617                 }
618         }
619
620         if (!asn1_pop_tag(data)) {
621                 return false;
622         }
623
624         *out = data_blob_talloc(mem_ctx, data->data, data->length);
625         if (out->data == NULL) {
626                 return false;
627         }
628         talloc_free(data);
629
630         return true;
631 }
632
633 static bool encode_server_sort_request(void *mem_ctx, void *in, DATA_BLOB *out)
634 {
635         struct ldb_server_sort_control **lssc = talloc_get_type(in, struct ldb_server_sort_control *);
636         struct asn1_data *data = asn1_init(mem_ctx);
637         int num;
638
639         if (!data) return false;
640
641         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
642                 return false;
643         }
644
645         /*
646           RFC2891 section 1.1:
647             SortKeyList ::= SEQUENCE OF SEQUENCE {
648               attributeType   AttributeDescription,
649               orderingRule    [0] MatchingRuleId OPTIONAL,
650               reverseOrder    [1] BOOLEAN DEFAULT FALSE }
651         */
652         for (num = 0; lssc[num]; num++) {
653                 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
654                         return false;
655                 }
656                 
657                 if (!asn1_write_OctetString(data, lssc[num]->attributeName, strlen(lssc[num]->attributeName))) {
658                         return false;
659                 }
660
661                 if (lssc[num]->orderingRule) {
662                         if (!asn1_write_OctetString(data, lssc[num]->orderingRule, strlen(lssc[num]->orderingRule))) {
663                                 return false;
664                         }
665                 }
666
667                 if (lssc[num]->reverse) {
668                         if (!asn1_write_BOOLEAN_context(data, lssc[num]->reverse, 1)) {
669                                 return false;
670                         }
671                 }
672
673                 if (!asn1_pop_tag(data)) {
674                         return false;
675                 }
676         }
677
678         if (!asn1_pop_tag(data)) {
679                 return false;
680         }
681
682         *out = data_blob_talloc(mem_ctx, data->data, data->length);
683         if (out->data == NULL) {
684                 return false;
685         }
686         talloc_free(data);
687
688         return true;
689 }
690
691 static bool encode_extended_dn_request(void *mem_ctx, void *in, DATA_BLOB *out)
692 {
693         struct ldb_extended_dn_control *ledc = talloc_get_type(in, struct ldb_extended_dn_control);
694         struct asn1_data *data;
695
696         if (!in) {
697                 *out = data_blob(NULL, 0);
698                 return true;
699         }
700
701         data = asn1_init(mem_ctx);
702
703         if (!data) return false;
704
705         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
706                 return false;
707         }
708
709         if (!asn1_write_Integer(data, ledc->type)) {
710                 return false;
711         }
712
713         if (!asn1_pop_tag(data)) {
714                 return false;
715         }
716
717         *out = data_blob_talloc(mem_ctx, data->data, data->length);
718         if (out->data == NULL) {
719                 return false;
720         }
721         talloc_free(data);
722
723         return true;
724 }
725
726 static bool encode_sd_flags_request(void *mem_ctx, void *in, DATA_BLOB *out)
727 {
728         struct ldb_sd_flags_control *lsdfc = talloc_get_type(in, struct ldb_sd_flags_control);
729         struct asn1_data *data = asn1_init(mem_ctx);
730
731         if (!data) return false;
732
733         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
734                 return false;
735         }
736
737         if (!asn1_write_Integer(data, lsdfc->secinfo_flags)) {
738                 return false;
739         }
740
741         if (!asn1_pop_tag(data)) {
742                 return false;
743         }
744
745         *out = data_blob_talloc(mem_ctx, data->data, data->length);
746         if (out->data == NULL) {
747                 return false;
748         }
749         talloc_free(data);
750
751         return true;
752 }
753
754 static bool encode_search_options_request(void *mem_ctx, void *in, DATA_BLOB *out)
755 {
756         struct ldb_search_options_control *lsoc = talloc_get_type(in, struct ldb_search_options_control);
757         struct asn1_data *data = asn1_init(mem_ctx);
758
759         if (!data) return false;
760
761         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
762                 return false;
763         }
764
765         if (!asn1_write_Integer(data, lsoc->search_options)) {
766                 return false;
767         }
768
769         if (!asn1_pop_tag(data)) {
770                 return false;
771         }
772
773         *out = data_blob_talloc(mem_ctx, data->data, data->length);
774         if (out->data == NULL) {
775                 return false;
776         }
777         talloc_free(data);
778
779         return true;
780 }
781
782 static bool encode_paged_results_request(void *mem_ctx, void *in, DATA_BLOB *out)
783 {
784         struct ldb_paged_control *lprc = talloc_get_type(in, struct ldb_paged_control);
785         struct asn1_data *data = asn1_init(mem_ctx);
786
787         if (!data) return false;
788
789         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
790                 return false;
791         }
792
793         if (!asn1_write_Integer(data, lprc->size)) {
794                 return false;
795         }
796
797         if (!asn1_write_OctetString(data, lprc->cookie, lprc->cookie_len)) {
798                 return false;
799         }       
800
801         if (!asn1_pop_tag(data)) {
802                 return false;
803         }
804
805         *out = data_blob_talloc(mem_ctx, data->data, data->length);
806         if (out->data == NULL) {
807                 return false;
808         }
809         talloc_free(data);
810
811         return true;
812 }
813
814 /* seem that this controls has 2 forms one in case it is used with
815  * a Search Request and another when used ina Search Response
816  */
817 static bool encode_asq_control(void *mem_ctx, void *in, DATA_BLOB *out)
818 {
819         struct ldb_asq_control *lac = talloc_get_type(in, struct ldb_asq_control);
820         struct asn1_data *data = asn1_init(mem_ctx);
821
822         if (!data) return false;
823
824         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
825                 return false;
826         }
827
828         if (lac->request) {
829
830                 if (!asn1_write_OctetString(data, lac->source_attribute, lac->src_attr_len)) {
831                         return false;
832                 }
833         } else {
834                 if (!asn1_write_enumerated(data, lac->result)) {
835                         return false;
836                 }
837         }
838
839         if (!asn1_pop_tag(data)) {
840                 return false;
841         }
842
843         *out = data_blob_talloc(mem_ctx, data->data, data->length);
844         if (out->data == NULL) {
845                 return false;
846         }
847         talloc_free(data);
848
849         return true;
850 }
851
852 static bool encode_dirsync_request(void *mem_ctx, void *in, DATA_BLOB *out)
853 {
854         struct ldb_dirsync_control *ldc = talloc_get_type(in, struct ldb_dirsync_control);
855         struct asn1_data *data = asn1_init(mem_ctx);
856
857         if (!data) return false;
858
859         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
860                 return false;
861         }
862
863         if (!asn1_write_Integer(data, ldc->flags)) {
864                 return false;
865         }
866
867         if (!asn1_write_Integer(data, ldc->max_attributes)) {
868                 return false;
869         }
870
871         if (!asn1_write_OctetString(data, ldc->cookie, ldc->cookie_len)) {
872                 return false;
873         }       
874
875         if (!asn1_pop_tag(data)) {
876                 return false;
877         }
878
879         *out = data_blob_talloc(mem_ctx, data->data, data->length);
880         if (out->data == NULL) {
881                 return false;
882         }
883         talloc_free(data);
884
885         return true;
886 }
887
888 static bool encode_vlv_request(void *mem_ctx, void *in, DATA_BLOB *out)
889 {
890         struct ldb_vlv_req_control *lvrc = talloc_get_type(in, struct ldb_vlv_req_control);
891         struct asn1_data *data = asn1_init(mem_ctx);
892
893         if (!data) return false;
894
895         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
896                 return false;
897         }
898
899         if (!asn1_write_Integer(data, lvrc->beforeCount)) {
900                 return false;
901         }
902
903         if (!asn1_write_Integer(data, lvrc->afterCount)) {
904                 return false;
905         }
906
907         if (lvrc->type == 0) {
908                 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) {
909                         return false;
910                 }
911                 
912                 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
913                         return false;
914                 }
915                 
916                 if (!asn1_write_Integer(data, lvrc->match.byOffset.offset)) {
917                         return false;
918                 }
919
920                 if (!asn1_write_Integer(data, lvrc->match.byOffset.contentCount)) {
921                         return false;
922                 }
923
924                 if (!asn1_pop_tag(data)) { /*SEQUENCE*/
925                         return false;
926                 }
927
928                 if (!asn1_pop_tag(data)) { /*CONTEXT*/
929                         return false;
930                 }
931         } else {
932                 if (!asn1_push_tag(data, ASN1_CONTEXT(1))) {
933                         return false;
934                 }
935                 
936                 if (!asn1_write_OctetString(data, lvrc->match.gtOrEq.value, lvrc->match.gtOrEq.value_len)) {
937                         return false;
938                 }
939
940                 if (!asn1_pop_tag(data)) { /*CONTEXT*/
941                         return false;
942                 }
943         }
944
945         if (lvrc->ctxid_len) {
946                 if (!asn1_write_OctetString(data, lvrc->contextId, lvrc->ctxid_len)) {
947                         return false;
948                 }
949         }
950
951         if (!asn1_pop_tag(data)) {
952                 return false;
953         }
954
955         *out = data_blob_talloc(mem_ctx, data->data, data->length);
956         if (out->data == NULL) {
957                 return false;
958         }
959         talloc_free(data);
960
961         return true;
962 }
963
964 static bool encode_vlv_response(void *mem_ctx, void *in, DATA_BLOB *out)
965 {
966         struct ldb_vlv_resp_control *lvrc = talloc_get_type(in, struct ldb_vlv_resp_control);
967         struct asn1_data *data = asn1_init(mem_ctx);
968
969         if (!data) return false;
970
971         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
972                 return false;
973         }
974
975         if (!asn1_write_Integer(data, lvrc->targetPosition)) {
976                 return false;
977         }
978
979         if (!asn1_write_Integer(data, lvrc->contentCount)) {
980                 return false;
981         }
982
983         if (!asn1_write_enumerated(data, lvrc->vlv_result)) {
984                 return false;
985         }
986
987         if (lvrc->ctxid_len) {
988                 if (!asn1_write_OctetString(data, lvrc->contextId, lvrc->ctxid_len)) {
989                         return false;
990                 }
991         }
992
993         if (!asn1_pop_tag(data)) {
994                 return false;
995         }
996
997         *out = data_blob_talloc(mem_ctx, data->data, data->length);
998         if (out->data == NULL) {
999                 return false;
1000         }
1001         talloc_free(data);
1002
1003         return true;
1004 }
1005
1006 static bool encode_openldap_dereference(void *mem_ctx, void *in, DATA_BLOB *out)
1007 {
1008         struct dsdb_openldap_dereference_control *control = talloc_get_type(in, struct dsdb_openldap_dereference_control);
1009         int i,j;
1010         struct asn1_data *data = asn1_init(mem_ctx);
1011
1012         if (!data) return false;
1013         
1014         if (!control) return false;
1015         
1016         if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
1017                 return false;
1018         }
1019         
1020         for (i=0; control->dereference && control->dereference[i]; i++) {
1021                 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
1022                         return false;
1023                 }
1024                 if (!asn1_write_OctetString(data, control->dereference[i]->source_attribute, strlen(control->dereference[i]->source_attribute))) {
1025                         return false;
1026                 }
1027                 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) {
1028                         return false;
1029                 }
1030                 for (j=0; control->dereference && control->dereference[i]->dereference_attribute[j]; j++) {
1031                         if (!asn1_write_OctetString(data, control->dereference[i]->dereference_attribute[j], 
1032                                                     strlen(control->dereference[i]->dereference_attribute[j]))) {
1033                                 return false;
1034                         }
1035                 }
1036                 
1037                 asn1_pop_tag(data);
1038                 asn1_pop_tag(data);
1039         }
1040         asn1_pop_tag(data);
1041
1042         *out = data_blob_talloc(mem_ctx, data->data, data->length);
1043         if (out->data == NULL) {
1044                 return false;
1045         }
1046         talloc_free(data);
1047         return true;
1048 }
1049
1050 static bool decode_openldap_dereference(void *mem_ctx, DATA_BLOB in, void *_out)
1051 {
1052         void **out = (void **)_out;
1053         struct asn1_data *data = asn1_init(mem_ctx);
1054         struct dsdb_openldap_dereference_result_control *control;
1055         struct dsdb_openldap_dereference_result **r = NULL;
1056         int i = 0;
1057         if (!data) return false;
1058
1059         control = talloc(mem_ctx, struct dsdb_openldap_dereference_result_control);
1060         if (!control) return false;
1061
1062         if (!asn1_load(data, in)) {
1063                 return false;
1064         }
1065
1066         control = talloc(mem_ctx, struct dsdb_openldap_dereference_result_control);
1067         if (!control) {
1068                 return false;
1069         }
1070
1071         if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
1072                 return false;
1073         }
1074
1075         while (asn1_tag_remaining(data) > 0) {                                  
1076                 r = talloc_realloc(control, r, struct dsdb_openldap_dereference_result *, i + 2);
1077                 if (!r) {
1078                         return false;
1079                 }
1080                 r[i] = talloc_zero(r, struct dsdb_openldap_dereference_result);
1081                 if (!r[i]) {
1082                         return false;
1083                 }
1084
1085                 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) {
1086                         return false;
1087                 }
1088                 
1089                 asn1_read_OctetString_talloc(r[i], data, &r[i]->source_attribute);
1090                 asn1_read_OctetString_talloc(r[i], data, &r[i]->dereferenced_dn);
1091                 if (asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1092                         if (!asn1_start_tag(data, ASN1_CONTEXT(0))) {
1093                                 return false;
1094                         }
1095                         
1096                         ldap_decode_attribs_bare(r, data, &r[i]->attributes,
1097                                                  &r[i]->num_attributes);
1098                         
1099                         if (!asn1_end_tag(data)) {
1100                                 return false;
1101                         }
1102                 }
1103                 if (!asn1_end_tag(data)) {
1104                         return false;
1105                 }
1106                 i++;
1107                 r[i] = NULL;
1108         }
1109
1110         if (!asn1_end_tag(data)) {
1111                 return false;
1112         }
1113
1114         control->attributes = r;
1115         *out = control;
1116
1117         return true;
1118 }
1119
1120 static bool encode_flag_request(void *mem_ctx, void *in, DATA_BLOB *out)
1121 {
1122         if (in) {
1123                 return false;
1124         }
1125
1126         *out = data_blob(NULL, 0);
1127         return true;
1128 }
1129
1130 static bool decode_flag_request(void *mem_ctx, DATA_BLOB in, void *_out)
1131 {
1132         if (in.length != 0) {
1133                 return false;
1134         }
1135
1136         return true;
1137 }
1138
1139 static const struct ldap_control_handler ldap_known_controls[] = {
1140         { LDB_CONTROL_PAGED_RESULTS_OID, decode_paged_results_request, encode_paged_results_request },
1141         { LDB_CONTROL_SD_FLAGS_OID, decode_sd_flags_request, encode_sd_flags_request },
1142         { LDB_CONTROL_DOMAIN_SCOPE_OID, decode_flag_request, encode_flag_request },
1143         { LDB_CONTROL_SEARCH_OPTIONS_OID, decode_search_options_request, encode_search_options_request },
1144         { LDB_CONTROL_NOTIFICATION_OID, decode_flag_request, encode_flag_request },
1145         { LDB_CONTROL_TREE_DELETE_OID, decode_flag_request, encode_flag_request },
1146         { LDB_CONTROL_SHOW_DELETED_OID, decode_flag_request, encode_flag_request },
1147         { LDB_CONTROL_SHOW_RECYCLED_OID, decode_flag_request, encode_flag_request },
1148         { LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID, decode_flag_request, encode_flag_request },
1149         { LDB_CONTROL_EXTENDED_DN_OID, decode_extended_dn_request, encode_extended_dn_request },
1150         { LDB_CONTROL_SERVER_SORT_OID, decode_server_sort_request, encode_server_sort_request },
1151         { LDB_CONTROL_SORT_RESP_OID, decode_server_sort_response, encode_server_sort_response },
1152         { LDB_CONTROL_ASQ_OID, decode_asq_control, encode_asq_control },
1153         { LDB_CONTROL_DIRSYNC_OID, decode_dirsync_request, encode_dirsync_request },
1154         { LDB_CONTROL_VLV_REQ_OID, decode_vlv_request, encode_vlv_request },
1155         { LDB_CONTROL_VLV_RESP_OID, decode_vlv_response, encode_vlv_response },
1156         { LDB_CONTROL_PERMISSIVE_MODIFY_OID, decode_flag_request, encode_flag_request },
1157         { LDB_CONTROL_SERVER_LAZY_COMMIT, decode_flag_request, encode_flag_request },
1158         { LDB_CONTROL_RODC_DCPROMO_OID, decode_flag_request, encode_flag_request },
1159         { LDB_CONTROL_RELAX_OID, decode_flag_request, encode_flag_request },
1160         { DSDB_OPENLDAP_DEREFERENCE_CONTROL, decode_openldap_dereference, encode_openldap_dereference },
1161
1162 /* DSDB_CONTROL_CURRENT_PARTITION_OID is internal only, and has no network representation */
1163         { DSDB_CONTROL_CURRENT_PARTITION_OID, NULL, NULL },
1164 /* DSDB_CONTROL_REPLICATED_UPDATE_OID is internal only, and has no network representation */
1165         { DSDB_CONTROL_REPLICATED_UPDATE_OID, NULL, NULL },
1166 /* DSDB_CONTROL_DN_STORAGE_FORMAT_OID is internal only, and has no network representation */
1167         { DSDB_CONTROL_DN_STORAGE_FORMAT_OID, NULL, NULL },
1168 /* LDB_CONTROL_RECALCULATE_SD_OID is internal only, and has no network representation */
1169         { LDB_CONTROL_RECALCULATE_SD_OID, NULL, NULL },
1170 /* LDB_CONTROL_REVEAL_INTERNALS is internal only, and has no network representation */
1171         { LDB_CONTROL_REVEAL_INTERNALS, NULL, NULL },
1172 /* LDB_CONTROL_AS_SYSTEM_OID is internal only, and has no network representation */
1173         { LDB_CONTROL_AS_SYSTEM_OID, NULL, NULL },
1174 /* DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID is internal only, and has no network representation */
1175         { DSDB_CONTROL_PASSWORD_CHANGE_STATUS_OID, NULL, NULL },
1176 /* DSDB_CONTROL_PASSWORD_HASH_VALUES_OID is internal only, and has no network representation */
1177         { DSDB_CONTROL_PASSWORD_HASH_VALUES_OID, NULL, NULL },
1178 /* DSDB_CONTROL_PASSWORD_CHANGE_OID is internal only, and has no network representation */
1179         { DSDB_CONTROL_PASSWORD_CHANGE_OID, NULL, NULL },
1180 /* DSDB_CONTROL_APPLY_LINKS is internal only, and has no network representation */
1181         { DSDB_CONTROL_APPLY_LINKS, NULL, NULL },
1182 /* DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID is internal only, and has no network representation */
1183         { DSDB_CONTROL_BYPASS_PASSWORD_HASH_OID, NULL, NULL },
1184 /* LDB_CONTROL_BYPASS_OPERATIONAL_OID is internal only, and has no network representation */
1185         { LDB_CONTROL_BYPASS_OPERATIONAL_OID, NULL, NULL },
1186 /* DSDB_CONTROL_CHANGEREPLMETADATA_OID is internal only, and has no network representation */
1187         { DSDB_CONTROL_CHANGEREPLMETADATA_OID, NULL, NULL },
1188 /* DSDB_CONTROL_SEARCH_APPLY_ACCESS is internal only, and has no network representation */
1189         { DSDB_CONTROL_SEARCH_APPLY_ACCESS, NULL, NULL },
1190 /* DSDB_EXTENDED_REPLICATED_OBJECTS_OID is internal only, and has no network representation */
1191         { DSDB_EXTENDED_REPLICATED_OBJECTS_OID, NULL, NULL },
1192 /* DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID is internal only, and has no network representation */
1193         { DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, NULL, NULL },
1194 /* DSDB_EXTENDED_ALLOCATE_RID_POOL is internal only, and has no network representation */
1195         { DSDB_EXTENDED_ALLOCATE_RID_POOL, NULL, NULL },
1196         { NULL, NULL, NULL }
1197 };
1198
1199 const struct ldap_control_handler *samba_ldap_control_handlers(void)
1200 {
1201         return ldap_known_controls;
1202 }
1203