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
);
313 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%d:%d:%d:%s",
314 LDB_CONTROL_VLV_RESP_NAME
,
316 rep_control
->targetPosition
,
317 rep_control
->contentCount
,
318 rep_control
->vlv_result
,
319 rep_control
->ctxid_len
,
320 rep_control
->contextId
);
325 if (strcmp(control
->oid
, LDB_CONTROL_SORT_RESP_OID
) == 0) {
326 struct ldb_sort_resp_control
*rep_control
= talloc_get_type(control
->data
,
327 struct ldb_sort_resp_control
);
329 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%s",
330 LDB_CONTROL_SORT_RESP_NAME
,
333 rep_control
->attr_desc
);
338 if (strcmp(control
->oid
, LDB_CONTROL_ASQ_OID
) == 0) {
339 struct ldb_asq_control
*rep_control
= talloc_get_type(control
->data
,
340 struct ldb_asq_control
);
342 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d",
343 LDB_CONTROL_SORT_RESP_NAME
,
345 rep_control
->result
);
350 if (strcmp(control
->oid
, LDB_CONTROL_DIRSYNC_OID
) == 0) {
352 struct ldb_dirsync_control
*rep_control
= talloc_get_type(control
->data
,
353 struct ldb_dirsync_control
);
355 cookie
= ldb_base64_encode(mem_ctx
, rep_control
->cookie
,
356 rep_control
->cookie_len
);
357 if (cookie
== NULL
) {
360 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%d:%s",
361 LDB_CONTROL_DIRSYNC_NAME
,
364 rep_control
->max_attributes
,
371 if (strcmp(control
->oid
, LDB_CONTROL_VERIFY_NAME_OID
) == 0) {
372 struct ldb_verify_name_control
*rep_control
= talloc_get_type(control
->data
, struct ldb_verify_name_control
);
374 if (rep_control
->gc
!= NULL
) {
375 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d:%s",
376 LDB_CONTROL_VERIFY_NAME_NAME
,
382 res
= talloc_asprintf(mem_ctx
, "%s:%d:%d",
383 LDB_CONTROL_VERIFY_NAME_NAME
,
391 * From here we don't know the control
393 if (control
->data
== NULL
) {
395 * We don't know the control but there is no real data attached
396 * to it so we can represent it with local_oid:oid:criticity.
398 res
= talloc_asprintf(mem_ctx
, "local_oid:%s:%d",
402 res
= talloc_asprintf(mem_ctx
, "unknown oid:%s",
410 * A little trick to allow to use constants defined in headers rather than
411 * hardwritten in the file.
412 * "sizeof" will return the \0 char as well so it will take the place of ":"
413 * in the length of the string.
415 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
417 /* Parse one string and return associated control if parsing is successful*/
418 struct ldb_control
*ldb_parse_control_from_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char *control_strings
)
420 struct ldb_control
*ctrl
;
421 char *error_string
= NULL
;
423 if (!(ctrl
= talloc(mem_ctx
, struct ldb_control
))) {
428 if (LDB_CONTROL_CMP(control_strings
,
429 LDB_CONTROL_VLV_REQ_NAME
) == 0) {
430 struct ldb_vlv_req_control
*control
;
434 int crit
, bc
, ac
, os
, cc
, ret
;
438 p
= &(control_strings
[sizeof(LDB_CONTROL_VLV_REQ_NAME
)]);
439 ret
= sscanf(p
, "%d:%d:%d:%d:%d:%1023[^$]", &crit
, &bc
, &ac
, &os
, &cc
, ctxid
);
441 ret
= sscanf(p
, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit
, &bc
, &ac
, attr
, ctxid
);
444 if ((ret
< 4) || (crit
< 0) || (crit
> 1)) {
445 error_string
= talloc_asprintf(mem_ctx
, "invalid server_sort control syntax\n");
446 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):bc(n):ac(n):<os(n):cc(n)|attr(s)>[:ctxid(o)]\n");
447 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, n = number, s = string, o = b64 binary blob");
448 ldb_set_errstring(ldb
, error_string
);
449 talloc_free(error_string
);
453 ctrl
->oid
= LDB_CONTROL_VLV_REQ_OID
;
454 ctrl
->critical
= crit
;
455 if (!(control
= talloc(ctrl
,
456 struct ldb_vlv_req_control
))) {
461 control
->beforeCount
= bc
;
462 control
->afterCount
= ac
;
465 control
->match
.gtOrEq
.value
= talloc_strdup(control
, attr
);
466 control
->match
.gtOrEq
.value_len
= strlen(attr
);
469 control
->match
.byOffset
.offset
= os
;
470 control
->match
.byOffset
.contentCount
= cc
;
473 control
->ctxid_len
= ldb_base64_decode(ctxid
);
474 control
->contextId
= (char *)talloc_memdup(control
, ctxid
, control
->ctxid_len
);
476 control
->ctxid_len
= 0;
477 control
->contextId
= NULL
;
479 ctrl
->data
= control
;
484 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_DIRSYNC_NAME
) == 0) {
485 struct ldb_dirsync_control
*control
;
488 int crit
, max_attrs
, ret
;
492 p
= &(control_strings
[sizeof(LDB_CONTROL_DIRSYNC_NAME
)]);
493 ret
= sscanf(p
, "%d:%u:%d:%1023[^$]", &crit
, &flags
, &max_attrs
, cookie
);
495 if ((ret
< 3) || (crit
< 0) || (crit
> 1) || (flags
< 0) || (max_attrs
< 0)) {
496 error_string
= talloc_asprintf(mem_ctx
, "invalid dirsync control syntax\n");
497 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
498 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, n = number, o = b64 binary blob");
499 ldb_set_errstring(ldb
, error_string
);
500 talloc_free(error_string
);
505 /* w2k3 seems to ignore the parameter,
506 * but w2k sends a wrong cookie when this value is to small
507 * this would cause looping forever, while getting
508 * the same data and same cookie forever
510 if (max_attrs
== 0) max_attrs
= 0x0FFFFFFF;
512 ctrl
->oid
= LDB_CONTROL_DIRSYNC_OID
;
513 ctrl
->critical
= crit
;
514 control
= talloc(ctrl
, struct ldb_dirsync_control
);
515 control
->flags
= flags
;
516 control
->max_attributes
= max_attrs
;
518 control
->cookie_len
= ldb_base64_decode(cookie
);
519 control
->cookie
= (char *)talloc_memdup(control
, cookie
, control
->cookie_len
);
521 control
->cookie
= NULL
;
522 control
->cookie_len
= 0;
524 ctrl
->data
= control
;
529 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_ASQ_NAME
) == 0) {
530 struct ldb_asq_control
*control
;
536 p
= &(control_strings
[sizeof(LDB_CONTROL_ASQ_NAME
)]);
537 ret
= sscanf(p
, "%d:%255[^$]", &crit
, attr
);
538 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (attr
[0] == '\0')) {
539 error_string
= talloc_asprintf(mem_ctx
, "invalid asq control syntax\n");
540 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):attr(s)\n");
541 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, s = string");
542 ldb_set_errstring(ldb
, error_string
);
543 talloc_free(error_string
);
548 ctrl
->oid
= LDB_CONTROL_ASQ_OID
;
549 ctrl
->critical
= crit
;
550 control
= talloc(ctrl
, struct ldb_asq_control
);
551 control
->request
= 1;
552 control
->source_attribute
= talloc_strdup(control
, attr
);
553 control
->src_attr_len
= strlen(attr
);
554 ctrl
->data
= control
;
559 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_EXTENDED_DN_NAME
) == 0) {
560 struct ldb_extended_dn_control
*control
;
564 p
= &(control_strings
[sizeof(LDB_CONTROL_EXTENDED_DN_NAME
)]);
565 ret
= sscanf(p
, "%d:%d", &crit
, &type
);
566 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (type
< 0) || (type
> 1)) {
567 ret
= sscanf(p
, "%d", &crit
);
568 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
569 error_string
= talloc_asprintf(mem_ctx
, "invalid extended_dn control syntax\n");
570 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)[:type(i)]\n");
571 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean\n");
572 error_string
= talloc_asprintf_append(error_string
, " i = integer\n");
573 error_string
= talloc_asprintf_append(error_string
, " valid values are: 0 - hexadecimal representation\n");
574 error_string
= talloc_asprintf_append(error_string
, " 1 - normal string representation");
575 ldb_set_errstring(ldb
, error_string
);
576 talloc_free(error_string
);
582 control
= talloc(ctrl
, struct ldb_extended_dn_control
);
583 control
->type
= type
;
586 ctrl
->oid
= LDB_CONTROL_EXTENDED_DN_OID
;
587 ctrl
->critical
= crit
;
588 ctrl
->data
= talloc_steal(ctrl
, control
);
593 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SD_FLAGS_NAME
) == 0) {
594 struct ldb_sd_flags_control
*control
;
597 unsigned secinfo_flags
;
599 p
= &(control_strings
[sizeof(LDB_CONTROL_SD_FLAGS_NAME
)]);
600 ret
= sscanf(p
, "%d:%u", &crit
, &secinfo_flags
);
601 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (secinfo_flags
< 0) || (secinfo_flags
> 0xF)) {
602 error_string
= talloc_asprintf(mem_ctx
, "invalid sd_flags control syntax\n");
603 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):secinfo_flags(n)\n");
604 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, n = number");
605 ldb_set_errstring(ldb
, error_string
);
606 talloc_free(error_string
);
611 ctrl
->oid
= LDB_CONTROL_SD_FLAGS_OID
;
612 ctrl
->critical
= crit
;
613 control
= talloc(ctrl
, struct ldb_sd_flags_control
);
614 control
->secinfo_flags
= secinfo_flags
;
615 ctrl
->data
= control
;
620 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SEARCH_OPTIONS_NAME
) == 0) {
621 struct ldb_search_options_control
*control
;
624 unsigned search_options
;
626 p
= &(control_strings
[sizeof(LDB_CONTROL_SEARCH_OPTIONS_NAME
)]);
627 ret
= sscanf(p
, "%d:%u", &crit
, &search_options
);
628 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (search_options
< 0) || (search_options
> 0xF)) {
629 error_string
= talloc_asprintf(mem_ctx
, "invalid search_options control syntax\n");
630 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):search_options(n)\n");
631 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, n = number");
632 ldb_set_errstring(ldb
, error_string
);
633 talloc_free(error_string
);
638 ctrl
->oid
= LDB_CONTROL_SEARCH_OPTIONS_OID
;
639 ctrl
->critical
= crit
;
640 control
= talloc(ctrl
, struct ldb_search_options_control
);
641 control
->search_options
= search_options
;
642 ctrl
->data
= control
;
647 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_BYPASS_OPERATIONAL_NAME
) == 0) {
651 p
= &(control_strings
[sizeof(LDB_CONTROL_BYPASS_OPERATIONAL_NAME
)]);
652 ret
= sscanf(p
, "%d", &crit
);
653 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
654 error_string
= talloc_asprintf(mem_ctx
, "invalid bypassopreational control syntax\n");
655 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
656 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
657 ldb_set_errstring(ldb
, error_string
);
658 talloc_free(error_string
);
663 ctrl
->oid
= LDB_CONTROL_BYPASS_OPERATIONAL_OID
;
664 ctrl
->critical
= crit
;
670 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_RELAX_NAME
) == 0) {
674 p
= &(control_strings
[sizeof(LDB_CONTROL_RELAX_NAME
)]);
675 ret
= sscanf(p
, "%d", &crit
);
676 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
677 error_string
= talloc_asprintf(mem_ctx
, "invalid relax control syntax\n");
678 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
679 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
680 ldb_set_errstring(ldb
, error_string
);
681 talloc_free(error_string
);
686 ctrl
->oid
= LDB_CONTROL_RELAX_OID
;
687 ctrl
->critical
= crit
;
693 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_RECALCULATE_SD_NAME
) == 0) {
697 p
= &(control_strings
[sizeof(LDB_CONTROL_RECALCULATE_SD_NAME
)]);
698 ret
= sscanf(p
, "%d", &crit
);
699 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
700 error_string
= talloc_asprintf(mem_ctx
, "invalid recalculate_sd control syntax\n");
701 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
702 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
703 ldb_set_errstring(ldb
, error_string
);
704 talloc_free(error_string
);
709 ctrl
->oid
= LDB_CONTROL_RECALCULATE_SD_OID
;
710 ctrl
->critical
= crit
;
716 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_DOMAIN_SCOPE_NAME
) == 0) {
720 p
= &(control_strings
[sizeof(LDB_CONTROL_DOMAIN_SCOPE_NAME
)]);
721 ret
= sscanf(p
, "%d", &crit
);
722 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
723 error_string
= talloc_asprintf(mem_ctx
, "invalid domain_scope control syntax\n");
724 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
725 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
726 ldb_set_errstring(ldb
, error_string
);
727 talloc_free(error_string
);
732 ctrl
->oid
= LDB_CONTROL_DOMAIN_SCOPE_OID
;
733 ctrl
->critical
= crit
;
739 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_PAGED_RESULTS_NAME
) == 0) {
740 struct ldb_paged_control
*control
;
744 p
= &(control_strings
[sizeof(LDB_CONTROL_PAGED_RESULTS_NAME
)]);
745 ret
= sscanf(p
, "%d:%d", &crit
, &size
);
746 if ((ret
!= 2) || (crit
< 0) || (crit
> 1) || (size
< 0)) {
747 error_string
= talloc_asprintf(mem_ctx
, "invalid paged_results control syntax\n");
748 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):size(n)\n");
749 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, n = number");
750 ldb_set_errstring(ldb
, error_string
);
751 talloc_free(error_string
);
756 ctrl
->oid
= LDB_CONTROL_PAGED_RESULTS_OID
;
757 ctrl
->critical
= crit
;
758 control
= talloc(ctrl
, struct ldb_paged_control
);
759 control
->size
= size
;
760 control
->cookie
= NULL
;
761 control
->cookie_len
= 0;
762 ctrl
->data
= control
;
767 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SERVER_SORT_NAME
) == 0) {
768 struct ldb_server_sort_control
**control
;
776 p
= &(control_strings
[sizeof(LDB_CONTROL_SERVER_SORT_NAME
)]);
777 ret
= sscanf(p
, "%d:%d:%255[^:]:%127[^:]", &crit
, &rev
, attr
, rule
);
778 if ((ret
< 3) || (crit
< 0) || (crit
> 1) || (rev
< 0 ) || (rev
> 1) ||attr
[0] == '\0') {
779 error_string
= talloc_asprintf(mem_ctx
, "invalid server_sort control syntax\n");
780 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
781 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, s = string");
782 ldb_set_errstring(ldb
, error_string
);
783 talloc_free(error_string
);
787 ctrl
->oid
= LDB_CONTROL_SERVER_SORT_OID
;
788 ctrl
->critical
= crit
;
789 control
= talloc_array(ctrl
, struct ldb_server_sort_control
*, 2);
790 control
[0] = talloc(control
, struct ldb_server_sort_control
);
791 control
[0]->attributeName
= talloc_strdup(control
, attr
);
793 control
[0]->orderingRule
= talloc_strdup(control
, rule
);
795 control
[0]->orderingRule
= NULL
;
796 control
[0]->reverse
= rev
;
798 ctrl
->data
= control
;
803 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_NOTIFICATION_NAME
) == 0) {
807 p
= &(control_strings
[sizeof(LDB_CONTROL_NOTIFICATION_NAME
)]);
808 ret
= sscanf(p
, "%d", &crit
);
809 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
810 error_string
= talloc_asprintf(mem_ctx
, "invalid notification control syntax\n");
811 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
812 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
813 ldb_set_errstring(ldb
, error_string
);
814 talloc_free(error_string
);
819 ctrl
->oid
= LDB_CONTROL_NOTIFICATION_OID
;
820 ctrl
->critical
= crit
;
826 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_TREE_DELETE_NAME
) == 0) {
830 p
= &(control_strings
[sizeof(LDB_CONTROL_TREE_DELETE_NAME
)]);
831 ret
= sscanf(p
, "%d", &crit
);
832 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
833 error_string
= talloc_asprintf(mem_ctx
, "invalid tree_delete control syntax\n");
834 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
835 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
836 ldb_set_errstring(ldb
, error_string
);
837 talloc_free(error_string
);
842 ctrl
->oid
= LDB_CONTROL_TREE_DELETE_OID
;
843 ctrl
->critical
= crit
;
849 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SHOW_DELETED_NAME
) == 0) {
853 p
= &(control_strings
[sizeof(LDB_CONTROL_SHOW_DELETED_NAME
)]);
854 ret
= sscanf(p
, "%d", &crit
);
855 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
856 error_string
= talloc_asprintf(mem_ctx
, "invalid show_deleted control syntax\n");
857 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
858 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
859 ldb_set_errstring(ldb
, error_string
);
860 talloc_free(error_string
);
865 ctrl
->oid
= LDB_CONTROL_SHOW_DELETED_OID
;
866 ctrl
->critical
= crit
;
872 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME
) == 0) {
876 p
= &(control_strings
[sizeof(LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME
)]);
877 ret
= sscanf(p
, "%d", &crit
);
878 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
879 error_string
= talloc_asprintf(mem_ctx
, "invalid show_deactivated_link control syntax\n");
880 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
881 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
882 ldb_set_errstring(ldb
, error_string
);
883 talloc_free(error_string
);
888 ctrl
->oid
= LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID
;
889 ctrl
->critical
= crit
;
895 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_SHOW_RECYCLED_NAME
) == 0) {
899 p
= &(control_strings
[sizeof(LDB_CONTROL_SHOW_RECYCLED_NAME
)]);
900 ret
= sscanf(p
, "%d", &crit
);
901 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
902 error_string
= talloc_asprintf(mem_ctx
, "invalid show_recycled control syntax\n");
903 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
904 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
905 ldb_set_errstring(ldb
, error_string
);
906 talloc_free(error_string
);
911 ctrl
->oid
= LDB_CONTROL_SHOW_RECYCLED_OID
;
912 ctrl
->critical
= crit
;
918 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_PERMISSIVE_MODIFY_NAME
) == 0) {
922 p
= &(control_strings
[sizeof(LDB_CONTROL_PERMISSIVE_MODIFY_NAME
)]);
923 ret
= sscanf(p
, "%d", &crit
);
924 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
925 error_string
= talloc_asprintf(mem_ctx
, "invalid permissive_modify control syntax\n");
926 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
927 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
928 ldb_set_errstring(ldb
, error_string
);
929 talloc_free(error_string
);
934 ctrl
->oid
= LDB_CONTROL_PERMISSIVE_MODIFY_OID
;
935 ctrl
->critical
= crit
;
941 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_REVEAL_INTERNALS_NAME
) == 0) {
945 p
= &(control_strings
[sizeof(LDB_CONTROL_REVEAL_INTERNALS_NAME
)]);
946 ret
= sscanf(p
, "%d", &crit
);
947 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
948 error_string
= talloc_asprintf(mem_ctx
, "invalid reveal_internals control syntax\n");
949 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
950 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
951 ldb_set_errstring(ldb
, error_string
);
952 talloc_free(error_string
);
957 ctrl
->oid
= LDB_CONTROL_REVEAL_INTERNALS
;
958 ctrl
->critical
= crit
;
964 if (strncmp(control_strings
, "local_oid:", 10) == 0) {
966 int crit
= 0, ret
= 0;
970 p
= &(control_strings
[10]);
971 ret
= sscanf(p
, "%255[^:]:%d", oid
, &crit
);
973 if ((ret
!= 2) || strlen(oid
) == 0 || (crit
< 0) || (crit
> 1)) {
974 error_string
= talloc_asprintf(mem_ctx
, "invalid local_oid control syntax\n");
975 error_string
= talloc_asprintf_append(error_string
, " syntax: oid(s):crit(b)\n");
976 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean, s = string");
977 ldb_set_errstring(ldb
, error_string
);
978 talloc_free(error_string
);
983 ctrl
->oid
= talloc_strdup(ctrl
, oid
);
989 ctrl
->critical
= crit
;
995 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_RODC_DCPROMO_NAME
) == 0) {
999 p
= &(control_strings
[sizeof(LDB_CONTROL_RODC_DCPROMO_NAME
)]);
1000 ret
= sscanf(p
, "%d", &crit
);
1001 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1002 error_string
= talloc_asprintf(mem_ctx
, "invalid rodc_join control syntax\n");
1003 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
1004 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
1005 ldb_set_errstring(ldb
, error_string
);
1006 talloc_free(error_string
);
1011 ctrl
->oid
= LDB_CONTROL_RODC_DCPROMO_OID
;
1012 ctrl
->critical
= crit
;
1018 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_PROVISION_NAME
) == 0) {
1022 p
= &(control_strings
[sizeof(LDB_CONTROL_PROVISION_NAME
)]);
1023 ret
= sscanf(p
, "%d", &crit
);
1024 if ((ret
!= 1) || (crit
< 0) || (crit
> 1)) {
1025 error_string
= talloc_asprintf(mem_ctx
, "invalid provision control syntax\n");
1026 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b)\n");
1027 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
1028 ldb_set_errstring(ldb
, error_string
);
1029 talloc_free(error_string
);
1034 ctrl
->oid
= LDB_CONTROL_PROVISION_OID
;
1035 ctrl
->critical
= crit
;
1040 if (LDB_CONTROL_CMP(control_strings
, LDB_CONTROL_VERIFY_NAME_NAME
) == 0) {
1043 int crit
, flags
, ret
;
1044 struct ldb_verify_name_control
*control
;
1048 p
= &(control_strings
[sizeof(LDB_CONTROL_VERIFY_NAME_NAME
)]);
1049 ret
= sscanf(p
, "%d:%d:%1023[^$]", &crit
, &flags
, gc
);
1050 if ((ret
!= 3) || (crit
< 0) || (crit
> 1)) {
1051 ret
= sscanf(p
, "%d:%d", &crit
, &flags
);
1052 if ((ret
!= 2) || (crit
< 0) || (crit
> 1)) {
1053 error_string
= talloc_asprintf(mem_ctx
, "invalid verify_name control syntax\n");
1054 error_string
= talloc_asprintf_append(error_string
, " syntax: crit(b):flags(i)[:gc(s)]\n");
1055 error_string
= talloc_asprintf_append(error_string
, " note: b = boolean");
1056 error_string
= talloc_asprintf_append(error_string
, " note: i = integer");
1057 error_string
= talloc_asprintf_append(error_string
, " note: s = string");
1058 ldb_set_errstring(ldb
, error_string
);
1059 talloc_free(error_string
);
1065 ctrl
->oid
= LDB_CONTROL_VERIFY_NAME_OID
;
1066 ctrl
->critical
= crit
;
1067 control
= talloc(ctrl
, struct ldb_verify_name_control
);
1068 control
->gc
= talloc_strdup(control
, gc
);
1069 control
->gc_len
= strlen(gc
);
1070 control
->flags
= flags
;
1071 ctrl
->data
= control
;
1075 * When no matching control has been found.
1080 /* Parse controls from the format used on the command line and in ejs */
1081 struct ldb_control
**ldb_parse_control_strings(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
, const char **control_strings
)
1084 struct ldb_control
**ctrl
;
1086 if (control_strings
== NULL
|| control_strings
[0] == NULL
)
1089 for (i
= 0; control_strings
[i
]; i
++);
1091 ctrl
= talloc_array(mem_ctx
, struct ldb_control
*, i
+ 1);
1093 ldb_reset_err_string(ldb
);
1094 for (i
= 0; control_strings
[i
]; i
++) {
1095 ctrl
[i
] = ldb_parse_control_from_string(ldb
, ctrl
, control_strings
[i
]);
1096 if (ctrl
[i
] == NULL
) {
1097 if (ldb_errstring(ldb
) == NULL
) {
1098 /* no controls matched, throw an error */
1099 ldb_asprintf_errstring(ldb
, "Invalid control name: '%s'", control_strings
[i
]);