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,
372 * From here we don't know the control
374 if (control->data == NULL) {
376 * We don't know the control but there is no real data attached to it
377 * so we can represent it with local_oid:oid:criticity
379 res = talloc_asprintf(mem_ctx, "local_oid:%s:%d",
385 res = talloc_asprintf(mem_ctx, "unknown oid:%s",
392 * A little trick to allow to use constants defined in headers rather than
393 * hardwritten in the file hardwritten in the file
394 * sizeof will return the \0 char as well so it will take the place of ":" in the
395 * length of the string
397 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
399 /* Parse one string and return associated control if parsing is successful*/
400 struct ldb_control *ldb_parse_control_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *control_strings)
402 struct ldb_control *ctrl;
403 char *error_string = NULL;
405 if (!(ctrl = talloc(mem_ctx, struct ldb_control))) {
410 if (LDB_CONTROL_CMP(control_strings,
411 LDB_CONTROL_VLV_REQ_NAME) == 0) {
412 struct ldb_vlv_req_control *control;
416 int crit, bc, ac, os, cc, ret;
420 p = &(control_strings[sizeof(LDB_CONTROL_VLV_REQ_NAME)]);
421 ret = sscanf(p, "%d:%d:%d:%d:%d:%1023[^$]", &crit, &bc, &ac, &os, &cc, ctxid);
423 ret = sscanf(p, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit, &bc, &ac, attr, ctxid);
426 if ((ret < 4) || (crit < 0) || (crit > 1)) {
427 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
428 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):bc(n):ac(n):<os(n):cc(n)|attr(s)>[:ctxid(o)]\n");
429 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number, s = string, o = b64 binary blob");
430 ldb_set_errstring(ldb, error_string);
431 talloc_free(error_string);
434 ctrl->oid = LDB_CONTROL_VLV_REQ_OID;
435 ctrl->critical = crit;
436 if (!(control = talloc(ctrl,
437 struct ldb_vlv_req_control))) {
441 control->beforeCount = bc;
442 control->afterCount = ac;
445 control->match.gtOrEq.value = talloc_strdup(control, attr);
446 control->match.gtOrEq.value_len = strlen(attr);
449 control->match.byOffset.offset = os;
450 control->match.byOffset.contentCount = cc;
453 control->ctxid_len = ldb_base64_decode(ctxid);
454 control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
456 control->ctxid_len = 0;
457 control->contextId = NULL;
459 ctrl->data = control;
464 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DIRSYNC_NAME) == 0) {
465 struct ldb_dirsync_control *control;
468 int crit, flags, max_attrs, ret;
471 p = &(control_strings[sizeof(LDB_CONTROL_DIRSYNC_NAME)]);
472 ret = sscanf(p, "%d:%d:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
474 if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
475 error_string = talloc_asprintf(mem_ctx, "invalid dirsync control syntax\n");
476 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
477 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number, o = b64 binary blob");
478 ldb_set_errstring(ldb, error_string);
479 talloc_free(error_string);
483 /* w2k3 seems to ignore the parameter,
484 * but w2k sends a wrong cookie when this value is to small
485 * this would cause looping forever, while getting
486 * the same data and same cookie forever
488 if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
490 ctrl->oid = LDB_CONTROL_DIRSYNC_OID;
491 ctrl->critical = crit;
492 control = talloc(ctrl, struct ldb_dirsync_control);
493 control->flags = flags;
494 control->max_attributes = max_attrs;
496 control->cookie_len = ldb_base64_decode(cookie);
497 control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
499 control->cookie = NULL;
500 control->cookie_len = 0;
502 ctrl->data = control;
507 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_ASQ_NAME) == 0) {
508 struct ldb_asq_control *control;
514 p = &(control_strings[sizeof(LDB_CONTROL_ASQ_NAME)]);
515 ret = sscanf(p, "%d:%255[^$]", &crit, attr);
516 if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
517 error_string = talloc_asprintf(mem_ctx, "invalid asq control syntax\n");
518 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):attr(s)\n");
519 error_string = talloc_asprintf_append(error_string, " note: b = boolean, s = string");
520 ldb_set_errstring(ldb, error_string);
521 talloc_free(error_string);
525 ctrl->oid = LDB_CONTROL_ASQ_OID;
526 ctrl->critical = crit;
527 control = talloc(ctrl, struct ldb_asq_control);
528 control->request = 1;
529 control->source_attribute = talloc_strdup(control, attr);
530 control->src_attr_len = strlen(attr);
531 ctrl->data = control;
536 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_EXTENDED_DN_NAME) == 0) {
537 struct ldb_extended_dn_control *control;
541 p = &(control_strings[sizeof(LDB_CONTROL_EXTENDED_DN_NAME)]);
542 ret = sscanf(p, "%d:%d", &crit, &type);
543 if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
544 ret = sscanf(p, "%d", &crit);
545 if ((ret != 1) || (crit < 0) || (crit > 1)) {
546 error_string = talloc_asprintf(mem_ctx, "invalid extended_dn control syntax\n");
547 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)[:type(i)]\n");
548 error_string = talloc_asprintf_append(error_string, " note: b = boolean\n");
549 error_string = talloc_asprintf_append(error_string, " i = integer\n");
550 error_string = talloc_asprintf_append(error_string, " valid values are: 0 - hexadecimal representation\n");
551 error_string = talloc_asprintf_append(error_string, " 1 - normal string representation");
552 ldb_set_errstring(ldb, error_string);
553 talloc_free(error_string);
558 control = talloc(ctrl, struct ldb_extended_dn_control);
559 control->type = type;
562 ctrl->oid = LDB_CONTROL_EXTENDED_DN_OID;
563 ctrl->critical = crit;
564 ctrl->data = talloc_steal(ctrl, control);
569 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SD_FLAGS_NAME) == 0) {
570 struct ldb_sd_flags_control *control;
573 unsigned secinfo_flags;
575 p = &(control_strings[sizeof(LDB_CONTROL_SD_FLAGS_NAME)]);
576 ret = sscanf(p, "%d:%u", &crit, &secinfo_flags);
577 if ((ret != 2) || (crit < 0) || (crit > 1) || (secinfo_flags < 0) || (secinfo_flags > 0xF)) {
578 error_string = talloc_asprintf(mem_ctx, "invalid sd_flags control syntax\n");
579 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):secinfo_flags(n)\n");
580 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number");
581 ldb_set_errstring(ldb, error_string);
582 talloc_free(error_string);
586 ctrl->oid = LDB_CONTROL_SD_FLAGS_OID;
587 ctrl->critical = crit;
588 control = talloc(ctrl, struct ldb_sd_flags_control);
589 control->secinfo_flags = secinfo_flags;
590 ctrl->data = control;
595 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SEARCH_OPTIONS_NAME) == 0) {
596 struct ldb_search_options_control *control;
599 unsigned search_options;
601 p = &(control_strings[sizeof(LDB_CONTROL_SEARCH_OPTIONS_NAME)]);
602 ret = sscanf(p, "%d:%u", &crit, &search_options);
603 if ((ret != 2) || (crit < 0) || (crit > 1) || (search_options < 0) || (search_options > 0xF)) {
604 error_string = talloc_asprintf(mem_ctx, "invalid search_options control syntax\n");
605 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):search_options(n)\n");
606 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number");
607 ldb_set_errstring(ldb, error_string);
608 talloc_free(error_string);
612 ctrl->oid = LDB_CONTROL_SEARCH_OPTIONS_OID;
613 ctrl->critical = crit;
614 control = talloc(ctrl, struct ldb_search_options_control);
615 control->search_options = search_options;
616 ctrl->data = control;
621 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_BYPASS_OPERATIONAL_NAME) == 0) {
625 p = &(control_strings[sizeof(LDB_CONTROL_BYPASS_OPERATIONAL_NAME)]);
626 ret = sscanf(p, "%d", &crit);
627 if ((ret != 1) || (crit < 0) || (crit > 1)) {
628 error_string = talloc_asprintf(mem_ctx, "invalid bypassopreational control syntax\n");
629 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
630 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
631 ldb_set_errstring(ldb, error_string);
632 talloc_free(error_string);
636 ctrl->oid = LDB_CONTROL_BYPASS_OPERATIONAL_OID;
637 ctrl->critical = crit;
643 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RELAX_NAME) == 0) {
647 p = &(control_strings[sizeof(LDB_CONTROL_RELAX_NAME)]);
648 ret = sscanf(p, "%d", &crit);
649 if ((ret != 1) || (crit < 0) || (crit > 1)) {
650 error_string = talloc_asprintf(mem_ctx, "invalid relax control syntax\n");
651 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
652 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
653 ldb_set_errstring(ldb, error_string);
654 talloc_free(error_string);
658 ctrl->oid = LDB_CONTROL_RELAX_OID;
659 ctrl->critical = crit;
665 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RECALCULATE_SD_NAME) == 0) {
669 p = &(control_strings[sizeof(LDB_CONTROL_RECALCULATE_SD_NAME)]);
670 ret = sscanf(p, "%d", &crit);
671 if ((ret != 1) || (crit < 0) || (crit > 1)) {
672 error_string = talloc_asprintf(mem_ctx, "invalid recalculate_sd control syntax\n");
673 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
674 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
675 ldb_set_errstring(ldb, error_string);
676 talloc_free(error_string);
680 ctrl->oid = LDB_CONTROL_RECALCULATE_SD_OID;
681 ctrl->critical = crit;
687 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DOMAIN_SCOPE_NAME) == 0) {
691 p = &(control_strings[sizeof(LDB_CONTROL_DOMAIN_SCOPE_NAME)]);
692 ret = sscanf(p, "%d", &crit);
693 if ((ret != 1) || (crit < 0) || (crit > 1)) {
694 error_string = talloc_asprintf(mem_ctx, "invalid domain_scope control syntax\n");
695 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
696 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
697 ldb_set_errstring(ldb, error_string);
698 talloc_free(error_string);
702 ctrl->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
703 ctrl->critical = crit;
709 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PAGED_RESULTS_NAME) == 0) {
710 struct ldb_paged_control *control;
714 p = &(control_strings[sizeof(LDB_CONTROL_PAGED_RESULTS_NAME)]);
715 ret = sscanf(p, "%d:%d", &crit, &size);
716 if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
717 error_string = talloc_asprintf(mem_ctx, "invalid paged_results control syntax\n");
718 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):size(n)\n");
719 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number");
720 ldb_set_errstring(ldb, error_string);
721 talloc_free(error_string);
725 ctrl->oid = LDB_CONTROL_PAGED_RESULTS_OID;
726 ctrl->critical = crit;
727 control = talloc(ctrl, struct ldb_paged_control);
728 control->size = size;
729 control->cookie = NULL;
730 control->cookie_len = 0;
731 ctrl->data = control;
736 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SERVER_SORT_NAME) == 0) {
737 struct ldb_server_sort_control **control;
745 p = &(control_strings[sizeof(LDB_CONTROL_SERVER_SORT_NAME)]);
746 ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
747 if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
748 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
749 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
750 error_string = talloc_asprintf_append(error_string, " note: b = boolean, s = string");
751 ldb_set_errstring(ldb, error_string);
752 talloc_free(error_string);
755 ctrl->oid = LDB_CONTROL_SERVER_SORT_OID;
756 ctrl->critical = crit;
757 control = talloc_array(ctrl, struct ldb_server_sort_control *, 2);
758 control[0] = talloc(control, struct ldb_server_sort_control);
759 control[0]->attributeName = talloc_strdup(control, attr);
761 control[0]->orderingRule = talloc_strdup(control, rule);
763 control[0]->orderingRule = NULL;
764 control[0]->reverse = rev;
766 ctrl->data = control;
771 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_NOTIFICATION_NAME) == 0) {
775 p = &(control_strings[sizeof(LDB_CONTROL_NOTIFICATION_NAME)]);
776 ret = sscanf(p, "%d", &crit);
777 if ((ret != 1) || (crit < 0) || (crit > 1)) {
778 error_string = talloc_asprintf(mem_ctx, "invalid notification control syntax\n");
779 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
780 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
781 ldb_set_errstring(ldb, error_string);
782 talloc_free(error_string);
786 ctrl->oid = LDB_CONTROL_NOTIFICATION_OID;
787 ctrl->critical = crit;
793 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_TREE_DELETE_NAME) == 0) {
797 p = &(control_strings[sizeof(LDB_CONTROL_TREE_DELETE_NAME)]);
798 ret = sscanf(p, "%d", &crit);
799 if ((ret != 1) || (crit < 0) || (crit > 1)) {
800 error_string = talloc_asprintf(mem_ctx, "invalid tree_delete control syntax\n");
801 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
802 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
803 ldb_set_errstring(ldb, error_string);
804 talloc_free(error_string);
808 ctrl->oid = LDB_CONTROL_TREE_DELETE_OID;
809 ctrl->critical = crit;
815 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DELETED_NAME) == 0) {
819 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DELETED_NAME)]);
820 ret = sscanf(p, "%d", &crit);
821 if ((ret != 1) || (crit < 0) || (crit > 1)) {
822 error_string = talloc_asprintf(mem_ctx, "invalid show_deleted control syntax\n");
823 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
824 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
825 ldb_set_errstring(ldb, error_string);
826 talloc_free(error_string);
830 ctrl->oid = LDB_CONTROL_SHOW_DELETED_OID;
831 ctrl->critical = crit;
837 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME) == 0) {
841 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME)]);
842 ret = sscanf(p, "%d", &crit);
843 if ((ret != 1) || (crit < 0) || (crit > 1)) {
844 error_string = talloc_asprintf(mem_ctx, "invalid show_deactivated_link control syntax\n");
845 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
846 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
847 ldb_set_errstring(ldb, error_string);
848 talloc_free(error_string);
852 ctrl->oid = LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID;
853 ctrl->critical = crit;
859 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_RECYCLED_NAME) == 0) {
863 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_RECYCLED_NAME)]);
864 ret = sscanf(p, "%d", &crit);
865 if ((ret != 1) || (crit < 0) || (crit > 1)) {
866 error_string = talloc_asprintf(mem_ctx, "invalid show_recycled control syntax\n");
867 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
868 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
869 ldb_set_errstring(ldb, error_string);
870 talloc_free(error_string);
874 ctrl->oid = LDB_CONTROL_SHOW_RECYCLED_OID;
875 ctrl->critical = crit;
881 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PERMISSIVE_MODIFY_NAME) == 0) {
885 p = &(control_strings[sizeof(LDB_CONTROL_PERMISSIVE_MODIFY_NAME)]);
886 ret = sscanf(p, "%d", &crit);
887 if ((ret != 1) || (crit < 0) || (crit > 1)) {
888 error_string = talloc_asprintf(mem_ctx, "invalid permissive_modify control syntax\n");
889 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
890 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
891 ldb_set_errstring(ldb, error_string);
892 talloc_free(error_string);
896 ctrl->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
897 ctrl->critical = crit;
903 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_REVEAL_INTERNALS_NAME) == 0) {
907 p = &(control_strings[sizeof(LDB_CONTROL_REVEAL_INTERNALS_NAME)]);
908 ret = sscanf(p, "%d", &crit);
909 if ((ret != 1) || (crit < 0) || (crit > 1)) {
910 error_string = talloc_asprintf(mem_ctx, "invalid reveal_internals control syntax\n");
911 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
912 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
913 ldb_set_errstring(ldb, error_string);
914 talloc_free(error_string);
918 ctrl->oid = LDB_CONTROL_REVEAL_INTERNALS;
919 ctrl->critical = crit;
925 if (strncmp(control_strings, "local_oid:", 10) == 0) {
927 int crit = 0, ret = 0;
931 p = &(control_strings[10]);
932 ret = sscanf(p, "%64[^:]:%d", oid, &crit);
934 if ((ret != 2) || strlen(oid) == 0 || (crit < 0) || (crit > 1)) {
935 error_string = talloc_asprintf(mem_ctx, "invalid local_oid control syntax\n");
936 error_string = talloc_asprintf_append(error_string, " syntax: oid(s):crit(b)\n");
937 error_string = talloc_asprintf_append(error_string, " note: b = boolean, s = string");
938 ldb_set_errstring(ldb, error_string);
939 talloc_free(error_string);
943 ctrl->oid = talloc_strdup(ctrl, oid);
948 ctrl->critical = crit;
954 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RODC_DCPROMO_NAME) == 0) {
958 p = &(control_strings[sizeof(LDB_CONTROL_RODC_DCPROMO_NAME)]);
959 ret = sscanf(p, "%d", &crit);
960 if ((ret != 1) || (crit < 0) || (crit > 1)) {
961 error_string = talloc_asprintf(mem_ctx, "invalid rodc_join control syntax\n");
962 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
963 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
964 ldb_set_errstring(ldb, error_string);
965 talloc_free(error_string);
969 ctrl->oid = LDB_CONTROL_RODC_DCPROMO_OID;
970 ctrl->critical = crit;
976 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PROVISION_NAME) == 0) {
980 p = &(control_strings[sizeof(LDB_CONTROL_PROVISION_NAME)]);
981 ret = sscanf(p, "%d", &crit);
982 if ((ret != 1) || (crit < 0) || (crit > 1)) {
983 error_string = talloc_asprintf(mem_ctx, "invalid provision control syntax\n");
984 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
985 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
986 ldb_set_errstring(ldb, error_string);
987 talloc_free(error_string);
991 ctrl->oid = LDB_CONTROL_PROVISION_OID;
992 ctrl->critical = crit;
998 * When no matching control has been found.
1004 * A little trick to allow to use constants defined in headers rather than
1005 * hardwritten in the file hardwritten in the file
1006 * sizeof will return the \0 char as well so it will take the place of ":" in the
1007 * length of the string
1009 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
1011 /* Parse controls from the format used on the command line and in ejs */
1012 struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char **control_strings)
1015 struct ldb_control **ctrl;
1017 if (control_strings == NULL || control_strings[0] == NULL)
1020 for (i = 0; control_strings[i]; i++);
1022 ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
1024 ldb_reset_err_string(ldb);
1025 for (i = 0; control_strings[i]; i++) {
1026 ctrl[i] = ldb_parse_control_from_string(ldb, ctrl, control_strings[i]);
1027 if (ctrl[i] == NULL) {
1028 if( ldb_errstring == NULL ) {
1029 /* no controls matched, throw an error */
1030 ldb_asprintf_errstring(ldb, "Invalid control name: '%s'", control_strings[i]);