4 Copyright (C) Simo Sorce 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 * Name: ldb_controls.c
27 * Component: ldb controls utility functions
29 * Description: helper functions for control modules
34 #include "ldb_private.h"
36 /* check if a control with the specified "oid" exist and return it */
37 /* returns NULL if not found */
38 struct ldb_control
*ldb_request_get_control(struct ldb_request
*req
, const char *oid
)
42 if (req
->controls
!= NULL
) {
43 for (i
= 0; req
->controls
[i
]; i
++) {
44 if (req
->controls
[i
]->oid
&& strcmp(oid
, req
->controls
[i
]->oid
) == 0) {
49 return req
->controls
[i
];
55 /* check if a control with the specified "oid" exist and return it */
56 /* returns NULL if not found */
57 struct ldb_control
*ldb_reply_get_control(struct ldb_reply
*rep
, const char *oid
)
61 if (rep
->controls
!= NULL
) {
62 for (i
= 0; rep
->controls
[i
]; i
++) {
63 if (rep
->controls
[i
]->oid
&& strcmp(oid
, rep
->controls
[i
]->oid
) == 0) {
68 return rep
->controls
[i
];
75 * Saves the current controls list into the "saver" (can also be NULL) and
76 * replace the one in "req" with a new one excluding the "exclude" control
77 * (if it is NULL then the list remains the same)
81 int ldb_save_controls(struct ldb_control
*exclude
, struct ldb_request
*req
, struct ldb_control
***saver
)
83 struct ldb_control
**lcs
, **lcs_old
;
86 lcs_old
= req
->controls
;
91 for (i
= 0; req
->controls
&& req
->controls
[i
]; i
++);
97 lcs
= talloc_array(req
, struct ldb_control
*, i
+ 1);
102 for (i
= 0, j
= 0; lcs_old
[i
]; i
++) {
103 if (exclude
== lcs_old
[i
]) continue;
109 req
->controls
= talloc_realloc(req
, lcs
, struct ldb_control
*, j
+ 1);
110 if (req
->controls
== NULL
) {
117 * Returns a list of controls, except the one specified with "exclude" (can
118 * also be NULL). Included controls become a child of returned list if they
119 * were children of "controls_in".
121 * Returns NULL on error (OOM) or an empty control list.
123 struct ldb_control
**ldb_controls_except_specified(struct ldb_control
**controls_in
,
125 struct ldb_control
*exclude
)
127 struct ldb_control
**lcs
= NULL
;
128 unsigned int i
, j
, n
;
130 for (i
= 0; controls_in
&& controls_in
[i
]; i
++);
136 for (i
= 0, j
= 0; controls_in
&& controls_in
[i
]; i
++) {
137 if (exclude
== controls_in
[i
]) continue;
140 /* Allocate here so if we remove the only
141 * control, or there were no controls, we
142 * don't allocate at all, and just return
144 lcs
= talloc_array(mem_ctx
, struct ldb_control
*,
151 lcs
[j
] = controls_in
[i
];
152 talloc_reparent(controls_in
, lcs
, lcs
[j
]);
158 lcs
= talloc_realloc(mem_ctx
, lcs
, struct ldb_control
*, j
+ 1);
164 /* check if there's any control marked as critical in the list */
165 /* return True if any, False if none */
166 int ldb_check_critical_controls(struct ldb_control
**controls
)
170 if (controls
== NULL
) {
174 for (i
= 0; controls
[i
]; i
++) {
175 if (controls
[i
]->critical
) {
183 int ldb_request_add_control(struct ldb_request
*req
, const char *oid
, bool critical
, void *data
)
186 struct ldb_control
**ctrls
;
187 struct ldb_control
*ctrl
;
189 for (n
=0; req
->controls
&& req
->controls
[n
];n
++) {
190 /* having two controls of the same OID makes no sense */
191 if (req
->controls
[n
]->oid
&& strcmp(oid
, req
->controls
[n
]->oid
) == 0) {
192 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
196 ctrls
= talloc_array(req
,
197 struct ldb_control
*,
199 if (!ctrls
) return LDB_ERR_OPERATIONS_ERROR
;
201 for (i
=0; i
<n
; i
++) {
202 ctrls
[i
] = req
->controls
[i
];
205 req
->controls
= ctrls
;
209 ctrl
= talloc(ctrls
, struct ldb_control
);
210 if (!ctrl
) return LDB_ERR_OPERATIONS_ERROR
;
212 ctrl
->oid
= talloc_strdup(ctrl
, oid
);
213 if (!ctrl
->oid
) return LDB_ERR_OPERATIONS_ERROR
;
214 ctrl
->critical
= critical
;
221 int ldb_reply_add_control(struct ldb_reply
*ares
, const char *oid
, bool critical
, void *data
)
224 struct ldb_control
**ctrls
;
225 struct ldb_control
*ctrl
;
227 for (n
=0; ares
->controls
&& ares
->controls
[n
];) {
228 /* having two controls of the same OID makes no sense */
229 if (ares
->controls
[n
]->oid
&& strcmp(oid
, ares
->controls
[n
]->oid
) == 0) {
230 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
235 ctrls
= talloc_realloc(ares
, ares
->controls
,
236 struct ldb_control
*,
238 if (!ctrls
) return LDB_ERR_OPERATIONS_ERROR
;
239 ares
->controls
= ctrls
;
243 ctrl
= talloc(ctrls
, struct ldb_control
);
244 if (!ctrl
) return LDB_ERR_OPERATIONS_ERROR
;
246 ctrl
->oid
= talloc_strdup(ctrl
, oid
);
247 if (!ctrl
->oid
) return LDB_ERR_OPERATIONS_ERROR
;
248 ctrl
->critical
= critical
;
255 /* Add a control to the request, replacing the old one if it is already in the request */
256 int ldb_request_replace_control(struct ldb_request
*req
, const char *oid
, bool critical
, void *data
)
261 ret
= ldb_request_add_control(req
, oid
, critical
, data
);
262 if (ret
!= LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
) {
266 for (n
=0; req
->controls
[n
];n
++) {
267 if (req
->controls
[n
]->oid
&& strcmp(oid
, req
->controls
[n
]->oid
) == 0) {
268 req
->controls
[n
]->critical
= critical
;
269 req
->controls
[n
]->data
= data
;
274 return LDB_ERR_OPERATIONS_ERROR
;
278 * Return a control as string
279 * the project (ie. name:value1:value2:...:valuen
280 * The string didn't include the criticity of the critical flag
282 char *ldb_control_to_string(TALLOC_CTX
*mem_ctx
, const struct ldb_control
*control
)
286 if (strcmp(control
->oid
, LDB_CONTROL_PAGED_RESULTS_OID
) == 0) {
287 struct ldb_paged_control
*rep_control
= talloc_get_type(control
->data
, struct ldb_paged_control
);
290 cookie
= ldb_base64_encode(mem_ctx
, rep_control
->cookie
, rep_control
->cookie_len
);
291 if (cookie
== NULL
) {
294 if (cookie
[0] != '\0') {
295 res
= talloc_asprintf(mem_ctx
, "%s:%d:%s",
296 LDB_CONTROL_PAGED_RESULTS_NAME
,
302 res
= talloc_asprintf(mem_ctx
, "%s:%d",
303 LDB_CONTROL_PAGED_RESULTS_NAME
,
309 if (strcmp(control
->oid
, LDB_CONTROL_VLV_RESP_OID
) == 0) {
310 struct ldb_vlv_resp_control
*rep_control
= talloc_get_type(control
->data
,
311 struct ldb_vlv_resp_control
);
315 cookie
= ldb_base64_encode(mem_ctx
,
316 (char *)rep_control
->contextId
,
317 rep_control
->ctxid_len
);
318 if (cookie
== NULL
) {
322 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%d:%d:%s",
323 LDB_CONTROL_VLV_RESP_NAME
,
325 rep_control
->targetPosition
,
326 rep_control
->contentCount
,
327 rep_control
->vlv_result
,
333 if (strcmp(control
->oid
, LDB_CONTROL_SORT_RESP_OID
) == 0) {
334 struct ldb_sort_resp_control
*rep_control
= talloc_get_type(control
->data
,
335 struct ldb_sort_resp_control
);
337 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%s",
338 LDB_CONTROL_SORT_RESP_NAME
,
341 rep_control
->attr_desc
);
346 if (strcmp(control
->oid
, LDB_CONTROL_ASQ_OID
) == 0) {
347 struct ldb_asq_control
*rep_control
= talloc_get_type(control
->data
,
348 struct ldb_asq_control
);
350 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d",
351 LDB_CONTROL_SORT_RESP_NAME
,
353 rep_control
->result
);
358 if (strcmp(control
->oid
, LDB_CONTROL_DIRSYNC_OID
) == 0) {
360 struct ldb_dirsync_control
*rep_control
= talloc_get_type(control
->data
,
361 struct ldb_dirsync_control
);
363 cookie
= ldb_base64_encode(mem_ctx
, rep_control
->cookie
,
364 rep_control
->cookie_len
);
365 if (cookie
== NULL
) {
368 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%d:%s",
369 LDB_CONTROL_DIRSYNC_NAME
,
372 rep_control
->max_attributes
,
378 if (strcmp(control
->oid
, LDB_CONTROL_DIRSYNC_EX_OID
) == 0) {
380 struct ldb_dirsync_control
*rep_control
= talloc_get_type(control
->data
,
381 struct ldb_dirsync_control
);
383 cookie
= ldb_base64_encode(mem_ctx
, rep_control
->cookie
,
384 rep_control
->cookie_len
);
385 if (cookie
== NULL
) {
388 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%d:%s",
389 LDB_CONTROL_DIRSYNC_EX_NAME
,
392 rep_control
->max_attributes
,
399 if (strcmp(control
->oid
, LDB_CONTROL_VERIFY_NAME_OID
) == 0) {
400 struct ldb_verify_name_control
*rep_control
= talloc_get_type(control
->data
, struct ldb_verify_name_control
);
402 if (rep_control
->gc
!= NULL
) {
403 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%s",
404 LDB_CONTROL_VERIFY_NAME_NAME
,
410 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d",
411 LDB_CONTROL_VERIFY_NAME_NAME
,
419 * From here we don't know the control
421 if (control
->data
== NULL
) {
423 * We don't know the control but there is no real data attached
424 * to it so we can represent it with local_oid:oid:criticity.
426 res
= talloc_asprintf(mem_ctx
, "local_oid:%s:%d",
430 res
= talloc_asprintf(mem_ctx
, "unknown oid:%s",
438 * A little trick to allow one to use constants defined in headers rather than
439 * hardwritten in the file.
440 * "sizeof" will return the \0 char as well so it will take the place of ":"
441 * in the length of the string.
443 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
445 /* Parse one string and return associated control if parsing is successful*/
446 struct ldb_control
*ldb_parse_control_from_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *control_strings
)
448 struct ldb_control
*ctrl
;
450 if (!(ctrl
= talloc(mem_ctx
, struct ldb_control
))) {
455 if (LDB_CONTROL_CMP(control_strings
,
456 LDB_CONTROL_VLV_REQ_NAME
) == 0) {
457 struct ldb_vlv_req_control
*control
;
461 int crit
, bc
, ac
, os
, cc
, ret
;
465 p
= &(control_strings
[sizeof(LDB_CONTROL_VLV_REQ_NAME
)]);
466 ret
= sscanf(p
, "%d:%d:%d:%d:%d:%1023[^$]", &crit
, &bc
, &ac
, &os
, &cc
, ctxid
);
467 /* We allow 2 ways to encode the GT_EQ case, because the
468 comparison string might contain null bytes or colons, which
469 would break sscanf (or indeed any parsing mechanism). */
471 ret
= sscanf(p
, "%d:%d:%d:>=%1023[^:]:%1023[^$]", &crit
, &bc
, &ac
, attr
, ctxid
);
475 ret
= sscanf(p
, "%d:%d:%d:base64>=%1023[^:]:%1023[^$]", &crit
, &bc
, &ac
, attr
, ctxid
);
476 len
= ldb_base64_decode(attr
);
482 if ((ret
< 4) || (crit
< 0) || (crit
> 1)) {
483 ldb_set_errstring(ldb
,
484 "invalid VLV control syntax\n"
485 " syntax: crit(b):bc(n):ac(n):"
486 "{os(n):cc(n)|>=val(s)|base64>=val(o)}[:ctxid(o)]\n"
487 " note: b = boolean, n = number, s = string, o = b64 binary blob");
491 ctrl
->oid
= LDB_CONTROL_VLV_REQ_OID
;
492 ctrl
->critical
= crit
;
493 if (!(control
= talloc(ctrl
,
494 struct ldb_vlv_req_control
))) {
499 control
->beforeCount
= bc
;
500 control
->afterCount
= ac
;
503 control
->match
.gtOrEq
.value
= talloc_strdup(control
, attr
);
504 control
->match
.gtOrEq
.value_len
= strlen(attr
);
507 control
->match
.byOffset
.offset
= os
;
508 control
->match
.byOffset
.contentCount
= cc
;
511 int len
= ldb_base64_decode(ctxid
);
513 ldb_set_errstring(ldb
,
514 "invalid VLV context_id\n");
518 control
->ctxid_len
= len
;
519 control
->contextId
= talloc_memdup(control
, ctxid
,
521 if (control
->contextId
== NULL
) {
527 control
->ctxid_len
= 0;
528 control
->contextId
= NULL
;
530 ctrl
->data
= control
;
535 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_DIRSYNC_NAME
) == 0) {
536 struct ldb_dirsync_control
*control
;
539 int crit
, max_attrs
, ret
;
542 cookie
= talloc_zero_array(ctrl
, char,
543 strlen(control_strings
) + 1);
544 if (cookie
== NULL
) {
550 p
= &(control_strings
[sizeof(LDB_CONTROL_DIRSYNC_NAME
)]);
551 ret
= sscanf(p
, "%d:%u:%d:%[^$]", &crit
, &flags
, &max_attrs
, cookie
);
553 if ((ret
< 3) || (crit
< 0) || (crit
> 1) || (max_attrs
< 0)) {
554 ldb_set_errstring(ldb
,
555 "invalid dirsync control syntax\n"
556 " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n"
557 " note: b = boolean, n = number, o = b64 binary blob");
562 /* w2k3 seems to ignore the parameter,
563 * but w2k sends a wrong cookie when this value is to small
564 * this would cause looping forever, while getting
565 * the same data and same cookie forever
567 if (max_attrs
== 0) max_attrs
= 0x0FFFFFFF;
569 ctrl
->oid
= LDB_CONTROL_DIRSYNC_OID
;
570 ctrl
->critical
= crit
;
571 control
= talloc(ctrl
, struct ldb_dirsync_control
);
572 if (control
== NULL
) {
577 control
->flags
= flags
;
578 control
->max_attributes
= max_attrs
;
580 int len
= ldb_base64_decode(cookie
);
582 ldb_set_errstring(ldb
,
583 "invalid dirsync cookie\n");
587 control
->cookie_len
= len
;
588 control
->cookie
= (char *)talloc_memdup(control
, cookie
, control
->cookie_len
);
589 if (control
->cookie
== NULL
) {
595 control
->cookie
= NULL
;
596 control
->cookie_len
= 0;
598 ctrl
->data
= control
;
603 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_DIRSYNC_EX_NAME
) == 0) {
604 struct ldb_dirsync_control
*control
;
607 int crit
, max_attrs
, ret
;
610 cookie
= talloc_zero_array(ctrl
, char,
611 strlen(control_strings
) + 1);
612 if (cookie
== NULL
) {
618 p
= &(control_strings
[sizeof(LDB_CONTROL_DIRSYNC_EX_NAME
)]);
619 ret
= sscanf(p
, "%d:%u:%d:%1023[^$]", &crit
, &flags
, &max_attrs
, cookie
);
621 if ((ret
< 3) || (crit
< 0) || (crit
> 1) || (max_attrs
< 0)) {
622 ldb_set_errstring(ldb
,
623 "invalid dirsync_ex control syntax\n"
624 " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n"
625 " note: b = boolean, n = number, o = b64 binary blob");
630 /* w2k3 seems to ignore the parameter,
631 * but w2k sends a wrong cookie when this value is to small
632 * this would cause looping forever, while getting
633 * the same data and same cookie forever
635 if (max_attrs
== 0) max_attrs
= 0x0FFFFFFF;
637 ctrl
->oid
= LDB_CONTROL_DIRSYNC_EX_OID
;
638 ctrl
->critical
= crit
;
639 control
= talloc(ctrl
, struct ldb_dirsync_control
);
640 if (control
== NULL
) {
645 control
->flags
= flags
;
646 control
->max_attributes
= max_attrs
;
648 int len
= ldb_base64_decode(cookie
);
650 ldb_set_errstring(ldb
,
651 "invalid dirsync_ex cookie"
652 " (probably too long)\n");
656 control
->cookie_len
= len
;
657 control
->cookie
= (char *)talloc_memdup(control
, cookie
, control
->cookie_len
);
658 if (control
->cookie
== NULL
) {
664 control
->cookie
= NULL
;
665 control
->cookie_len
= 0;
667 ctrl
->data
= control
;
673 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_ASQ_NAME
) == 0) {
674 struct ldb_asq_control
*control
;
680 p
= &(control_strings
[sizeof(LDB_CONTROL_ASQ_NAME
)]);
681 ret
= sscanf(p
, "%d:%255[^$]", &crit
, attr
);
682 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (attr
[0] == '\0')) {
683 ldb_set_errstring(ldb
,
684 "invalid asq control syntax\n"
685 " syntax: crit(b):attr(s)\n"
686 " note: b = boolean, s = string");
691 ctrl
->oid
= LDB_CONTROL_ASQ_OID
;
692 ctrl
->critical
= crit
;
693 control
= talloc(ctrl
, struct ldb_asq_control
);
694 if (control
== NULL
) {
699 control
->request
= 1;
700 control
->source_attribute
= talloc_strdup(control
, attr
);
701 control
->src_attr_len
= strlen(attr
);
702 ctrl
->data
= control
;
707 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_EXTENDED_DN_NAME
) == 0) {
708 struct ldb_extended_dn_control
*control
;
712 p
= &(control_strings
[sizeof(LDB_CONTROL_EXTENDED_DN_NAME
)]);
713 ret
= sscanf(p
, "%d:%d", &crit
, &type
);
714 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (type
< 0) || (type
> 1)) {
715 ret
= sscanf(p
, "%d", &crit
);
716 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
717 ldb_set_errstring(ldb
,
718 "invalid extended_dn control syntax\n"
719 " syntax: crit(b)[:type(i)]\n"
720 " note: b = boolean\n"
722 " valid values are: 0 - hexadecimal representation\n"
723 " 1 - normal string representation");
729 control
= talloc(ctrl
, struct ldb_extended_dn_control
);
730 if (control
== NULL
) {
735 control
->type
= type
;
738 ctrl
->oid
= LDB_CONTROL_EXTENDED_DN_OID
;
739 ctrl
->critical
= crit
;
740 ctrl
->data
= talloc_steal(ctrl
, control
);
745 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SD_FLAGS_NAME
) == 0) {
746 struct ldb_sd_flags_control
*control
;
749 unsigned secinfo_flags
;
751 p
= &(control_strings
[sizeof(LDB_CONTROL_SD_FLAGS_NAME
)]);
752 ret
= sscanf(p
, "%d:%u", &crit
, &secinfo_flags
);
753 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (secinfo_flags
> 0xF)) {
754 ldb_set_errstring(ldb
,
755 "invalid sd_flags control syntax\n"
756 " syntax: crit(b):secinfo_flags(n)\n"
757 " note: b = boolean, n = number");
762 ctrl
->oid
= LDB_CONTROL_SD_FLAGS_OID
;
763 ctrl
->critical
= crit
;
764 control
= talloc(ctrl
, struct ldb_sd_flags_control
);
765 if (control
== NULL
) {
771 control
->secinfo_flags
= secinfo_flags
;
772 ctrl
->data
= control
;
777 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SEARCH_OPTIONS_NAME
) == 0) {
778 struct ldb_search_options_control
*control
;
781 unsigned search_options
;
783 p
= &(control_strings
[sizeof(LDB_CONTROL_SEARCH_OPTIONS_NAME
)]);
784 ret
= sscanf(p
, "%d:%u", &crit
, &search_options
);
785 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (search_options
> 0xF)) {
786 ldb_set_errstring(ldb
,
787 "invalid search_options control syntax\n"
788 " syntax: crit(b):search_options(n)\n"
789 " note: b = boolean, n = number");
794 ctrl
->oid
= LDB_CONTROL_SEARCH_OPTIONS_OID
;
795 ctrl
->critical
= crit
;
796 control
= talloc(ctrl
, struct ldb_search_options_control
);
797 if (control
== NULL
) {
803 control
->search_options
= search_options
;
804 ctrl
->data
= control
;
809 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_BYPASS_OPERATIONAL_NAME
) == 0) {
813 p
= &(control_strings
[sizeof(LDB_CONTROL_BYPASS_OPERATIONAL_NAME
)]);
814 ret
= sscanf(p
, "%d", &crit
);
815 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
816 ldb_set_errstring(ldb
,
817 "invalid bypassopreational control syntax\n"
819 " note: b = boolean");
824 ctrl
->oid
= LDB_CONTROL_BYPASS_OPERATIONAL_OID
;
825 ctrl
->critical
= crit
;
831 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_RELAX_NAME
) == 0) {
835 p
= &(control_strings
[sizeof(LDB_CONTROL_RELAX_NAME
)]);
836 ret
= sscanf(p
, "%d", &crit
);
837 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
838 ldb_set_errstring(ldb
,
839 "invalid relax control syntax\n"
841 " note: b = boolean");
846 ctrl
->oid
= LDB_CONTROL_RELAX_OID
;
847 ctrl
->critical
= crit
;
853 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_RECALCULATE_SD_NAME
) == 0) {
857 p
= &(control_strings
[sizeof(LDB_CONTROL_RECALCULATE_SD_NAME
)]);
858 ret
= sscanf(p
, "%d", &crit
);
859 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
860 ldb_set_errstring(ldb
,
861 "invalid recalculate_sd control syntax\n"
863 " note: b = boolean");
868 ctrl
->oid
= LDB_CONTROL_RECALCULATE_SD_OID
;
869 ctrl
->critical
= crit
;
875 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_DOMAIN_SCOPE_NAME
) == 0) {
879 p
= &(control_strings
[sizeof(LDB_CONTROL_DOMAIN_SCOPE_NAME
)]);
880 ret
= sscanf(p
, "%d", &crit
);
881 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
882 ldb_set_errstring(ldb
,
883 "invalid domain_scope control syntax\n"
885 " note: b = boolean");
890 ctrl
->oid
= LDB_CONTROL_DOMAIN_SCOPE_OID
;
891 ctrl
->critical
= crit
;
897 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_PAGED_RESULTS_NAME
) == 0) {
898 struct ldb_paged_control
*control
;
904 p
= &(control_strings
[sizeof(LDB_CONTROL_PAGED_RESULTS_NAME
)]);
905 ret
= sscanf(p
, "%d:%d:%1023[^$]", &crit
, &size
, cookie
);
906 if ((ret
< 2) || (ret
> 3) || (crit
< 0) || (crit
> 1) ||
908 ldb_set_errstring(ldb
,
909 "invalid paged_results control syntax\n"
910 " syntax: crit(b):size(n)[:cookie(base64)]\n"
911 " note: b = boolean, n = number");
916 ctrl
->oid
= LDB_CONTROL_PAGED_RESULTS_OID
;
917 ctrl
->critical
= crit
;
918 control
= talloc(ctrl
, struct ldb_paged_control
);
919 if (control
== NULL
) {
925 control
->size
= size
;
926 if (cookie
[0] != '\0') {
927 int len
= ldb_base64_decode(cookie
);
929 ldb_set_errstring(ldb
,
930 "invalid paged_results cookie"
931 " (probably too long)\n");
935 control
->cookie_len
= len
;
936 control
->cookie
= talloc_memdup(control
, cookie
, control
->cookie_len
);
937 if (control
->cookie
== NULL
) {
943 control
->cookie
= NULL
;
944 control
->cookie_len
= 0;
946 ctrl
->data
= control
;
951 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SERVER_SORT_NAME
) == 0) {
952 struct ldb_server_sort_control
**control
;
960 p
= &(control_strings
[sizeof(LDB_CONTROL_SERVER_SORT_NAME
)]);
961 ret
= sscanf(p
, "%d:%d:%255[^:]:%127[^:]", &crit
, &rev
, attr
, rule
);
962 if ((ret
< 3) || (crit
< 0) || (crit
> 1) || (rev
< 0 ) || (rev
> 1) ||attr
[0] == '\0') {
963 ldb_set_errstring(ldb
,
964 "invalid server_sort control syntax\n"
965 " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n"
966 " note: b = boolean, s = string");
970 ctrl
->oid
= LDB_CONTROL_SERVER_SORT_OID
;
971 ctrl
->critical
= crit
;
972 control
= talloc_array(ctrl
, struct ldb_server_sort_control
*, 2);
973 if (control
== NULL
) {
979 control
[0] = talloc(control
, struct ldb_server_sort_control
);
980 if (control
[0] == NULL
) {
986 control
[0]->attributeName
= talloc_strdup(control
, attr
);
987 if (control
[0]->attributeName
== NULL
) {
994 control
[0]->orderingRule
= talloc_strdup(control
, rule
);
995 if (control
[0]->orderingRule
== NULL
) {
1001 control
[0]->orderingRule
= NULL
;
1003 control
[0]->reverse
= rev
;
1005 ctrl
->data
= control
;
1010 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_NOTIFICATION_NAME
) == 0) {
1014 p
= &(control_strings
[sizeof(LDB_CONTROL_NOTIFICATION_NAME
)]);
1015 ret
= sscanf(p
, "%d", &crit
);
1016 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1017 ldb_set_errstring(ldb
,
1018 "invalid notification control syntax\n"
1019 " syntax: crit(b)\n"
1020 " note: b = boolean");
1025 ctrl
->oid
= LDB_CONTROL_NOTIFICATION_OID
;
1026 ctrl
->critical
= crit
;
1032 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_TREE_DELETE_NAME
) == 0) {
1036 p
= &(control_strings
[sizeof(LDB_CONTROL_TREE_DELETE_NAME
)]);
1037 ret
= sscanf(p
, "%d", &crit
);
1038 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1039 ldb_set_errstring(ldb
,
1040 "invalid tree_delete control syntax\n"
1041 " syntax: crit(b)\n"
1042 " note: b = boolean");
1047 ctrl
->oid
= LDB_CONTROL_TREE_DELETE_OID
;
1048 ctrl
->critical
= crit
;
1054 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SHOW_DELETED_NAME
) == 0) {
1058 p
= &(control_strings
[sizeof(LDB_CONTROL_SHOW_DELETED_NAME
)]);
1059 ret
= sscanf(p
, "%d", &crit
);
1060 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1061 ldb_set_errstring(ldb
,
1062 "invalid show_deleted control syntax\n"
1063 " syntax: crit(b)\n"
1064 " note: b = boolean");
1069 ctrl
->oid
= LDB_CONTROL_SHOW_DELETED_OID
;
1070 ctrl
->critical
= crit
;
1076 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME
) == 0) {
1080 p
= &(control_strings
[sizeof(LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME
)]);
1081 ret
= sscanf(p
, "%d", &crit
);
1082 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1083 ldb_set_errstring(ldb
,
1084 "invalid show_deactivated_link control syntax\n"
1085 " syntax: crit(b)\n"
1086 " note: b = boolean");
1091 ctrl
->oid
= LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID
;
1092 ctrl
->critical
= crit
;
1098 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SHOW_RECYCLED_NAME
) == 0) {
1102 p
= &(control_strings
[sizeof(LDB_CONTROL_SHOW_RECYCLED_NAME
)]);
1103 ret
= sscanf(p
, "%d", &crit
);
1104 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1105 ldb_set_errstring(ldb
,
1106 "invalid show_recycled control syntax\n"
1107 " syntax: crit(b)\n"
1108 " note: b = boolean");
1113 ctrl
->oid
= LDB_CONTROL_SHOW_RECYCLED_OID
;
1114 ctrl
->critical
= crit
;
1120 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_PERMISSIVE_MODIFY_NAME
) == 0) {
1124 p
= &(control_strings
[sizeof(LDB_CONTROL_PERMISSIVE_MODIFY_NAME
)]);
1125 ret
= sscanf(p
, "%d", &crit
);
1126 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1127 ldb_set_errstring(ldb
,
1128 "invalid permissive_modify control syntax\n"
1129 " syntax: crit(b)\n"
1130 " note: b = boolean");
1135 ctrl
->oid
= LDB_CONTROL_PERMISSIVE_MODIFY_OID
;
1136 ctrl
->critical
= crit
;
1142 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_REVEAL_INTERNALS_NAME
) == 0) {
1146 p
= &(control_strings
[sizeof(LDB_CONTROL_REVEAL_INTERNALS_NAME
)]);
1147 ret
= sscanf(p
, "%d", &crit
);
1148 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1149 ldb_set_errstring(ldb
,
1150 "invalid reveal_internals control syntax\n"
1151 " syntax: crit(b)\n"
1152 " note: b = boolean");
1157 ctrl
->oid
= LDB_CONTROL_REVEAL_INTERNALS
;
1158 ctrl
->critical
= crit
;
1164 if (strncmp(control_strings
, "local_oid:", 10) == 0) {
1166 int crit
= 0, ret
= 0;
1170 p
= &(control_strings
[10]);
1171 ret
= sscanf(p
, "%255[^:]:%d", oid
, &crit
);
1173 if ((ret
!= 2) || strlen(oid
) == 0 || (crit
< 0) || (crit
> 1)) {
1174 ldb_set_errstring(ldb
,
1175 "invalid local_oid control syntax\n"
1176 " syntax: oid(s):crit(b)\n"
1177 " note: b = boolean, s = string");
1182 ctrl
->oid
= talloc_strdup(ctrl
, oid
);
1188 ctrl
->critical
= crit
;
1194 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_RODC_DCPROMO_NAME
) == 0) {
1198 p
= &(control_strings
[sizeof(LDB_CONTROL_RODC_DCPROMO_NAME
)]);
1199 ret
= sscanf(p
, "%d", &crit
);
1200 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1201 ldb_set_errstring(ldb
,
1202 "invalid rodc_join control syntax\n"
1203 " syntax: crit(b)\n"
1204 " note: b = boolean");
1209 ctrl
->oid
= LDB_CONTROL_RODC_DCPROMO_OID
;
1210 ctrl
->critical
= crit
;
1216 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_PROVISION_NAME
) == 0) {
1220 p
= &(control_strings
[sizeof(LDB_CONTROL_PROVISION_NAME
)]);
1221 ret
= sscanf(p
, "%d", &crit
);
1222 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1223 ldb_set_errstring(ldb
,
1224 "invalid provision control syntax\n"
1225 " syntax: crit(b)\n"
1226 " note: b = boolean");
1231 ctrl
->oid
= LDB_CONTROL_PROVISION_OID
;
1232 ctrl
->critical
= crit
;
1237 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_VERIFY_NAME_NAME
) == 0) {
1240 int crit
, flags
, ret
;
1241 struct ldb_verify_name_control
*control
;
1245 p
= &(control_strings
[sizeof(LDB_CONTROL_VERIFY_NAME_NAME
)]);
1246 ret
= sscanf(p
, "%d:%d:%1023[^$]", &crit
, &flags
, gc
);
1247 if ((ret
!= 3) || (crit
< 0) || (crit
> 1)) {
1248 ret
= sscanf(p
, "%d:%d", &crit
, &flags
);
1249 if ((ret
!= 2) || (crit
< 0) || (crit
> 1)) {
1250 ldb_set_errstring(ldb
,
1251 "invalid verify_name control syntax\n"
1252 " syntax: crit(b):flags(i)[:gc(s)]\n"
1253 " note: b = boolean"
1254 " note: i = integer"
1255 " note: s = string");
1261 ctrl
->oid
= LDB_CONTROL_VERIFY_NAME_OID
;
1262 ctrl
->critical
= crit
;
1263 control
= talloc(ctrl
, struct ldb_verify_name_control
);
1264 if (control
== NULL
) {
1270 control
->gc
= talloc_strdup(control
, gc
);
1271 if (control
->gc
== NULL
) {
1277 control
->gc_len
= strlen(gc
);
1278 control
->flags
= flags
;
1279 ctrl
->data
= control
;
1283 * When no matching control has been found.
1288 /* Parse controls from the format used on the command line and in ejs */
1289 struct ldb_control
**ldb_parse_control_strings(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char **control_strings
)
1292 struct ldb_control
**ctrl
;
1294 if (control_strings
== NULL
|| control_strings
[0] == NULL
)
1297 for (i
= 0; control_strings
[i
]; i
++);
1299 ctrl
= talloc_array(mem_ctx
, struct ldb_control
*, i
+ 1);
1301 ldb_reset_err_string(ldb
);
1302 for (i
= 0; control_strings
[i
]; i
++) {
1303 ctrl
[i
] = ldb_parse_control_from_string(ldb
, ctrl
, control_strings
[i
]);
1304 if (ctrl
[i
] == NULL
) {
1305 if (ldb_errstring(ldb
) == NULL
) {
1306 /* no controls matched, throw an error */
1307 ldb_asprintf_errstring(ldb
, "Invalid control name: '%s'", control_strings
[i
]);