s3:libsmb: remove unused 'inbuf' variable
[Samba/gebeck_regimport.git] / lib / ldb / common / ldb_controls.c
blob7ce4fc34af62f43d91ba7d2470b4c661a17b6206
1 /*
2 ldb database library
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
8 ** under the LGPL
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
31 * Author: Simo Sorce
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)
40 unsigned int i;
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) {
45 break;
49 return req->controls[i];
52 return NULL;
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)
59 unsigned int i;
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) {
64 break;
68 return rep->controls[i];
71 return NULL;
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)
79 * Returns 0 on error.
81 int ldb_save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver)
83 struct ldb_control **lcs, **lcs_old;
84 unsigned int i, j;
86 lcs_old = req->controls;
87 if (saver != NULL) {
88 *saver = lcs_old;
91 for (i = 0; req->controls && req->controls[i]; i++);
92 if (i == 0) {
93 req->controls = NULL;
94 return 1;
97 lcs = talloc_array(req, struct ldb_control *, i + 1);
98 if (!lcs) {
99 return 0;
102 for (i = 0, j = 0; lcs_old[i]; i++) {
103 if (exclude == lcs_old[i]) continue;
104 lcs[j] = lcs_old[i];
105 j++;
107 lcs[j] = NULL;
109 req->controls = talloc_realloc(req, lcs, struct ldb_control *, j + 1);
110 if (req->controls == NULL) {
111 return 0;
113 return 1;
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,
124 TALLOC_CTX *mem_ctx,
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++);
131 if (i == 0) {
132 return NULL;
134 n = i;
136 for (i = 0, j = 0; controls_in && controls_in[i]; i++) {
137 if (exclude == controls_in[i]) continue;
139 if (!lcs) {
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
143 * NULL */
144 lcs = talloc_array(mem_ctx, struct ldb_control *,
145 n + 1);
146 if (!lcs) {
147 return NULL;
151 lcs[j] = controls_in[i];
152 talloc_reparent(controls_in, lcs, lcs[j]);
153 j++;
155 if (lcs) {
156 lcs[j] = NULL;
158 lcs = talloc_realloc(mem_ctx, lcs, struct ldb_control *, j + 1);
161 return lcs;
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)
168 unsigned int i;
170 if (controls == NULL) {
171 return 0;
174 for (i = 0; controls[i]; i++) {
175 if (controls[i]->critical) {
176 return 1;
180 return 0;
183 int ldb_request_add_control(struct ldb_request *req, const char *oid, bool critical, void *data)
185 unsigned int i, n;
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 *,
198 n + 2);
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;
206 ctrls[n] = NULL;
207 ctrls[n+1] = NULL;
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;
215 ctrl->data = data;
217 ctrls[n] = ctrl;
218 return LDB_SUCCESS;
221 int ldb_reply_add_control(struct ldb_reply *ares, const char *oid, bool critical, void *data)
223 unsigned n;
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;
232 n++;
235 ctrls = talloc_realloc(ares, ares->controls,
236 struct ldb_control *,
237 n + 2);
238 if (!ctrls) return LDB_ERR_OPERATIONS_ERROR;
239 ares->controls = ctrls;
240 ctrls[n] = NULL;
241 ctrls[n+1] = NULL;
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;
249 ctrl->data = data;
251 ctrls[n] = ctrl;
252 return LDB_SUCCESS;
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)
258 unsigned int n;
259 int ret;
261 ret = ldb_request_add_control(req, oid, critical, data);
262 if (ret != LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
263 return ret;
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;
270 return LDB_SUCCESS;
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)
284 char *res = NULL;
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);
288 char *cookie;
290 cookie = ldb_base64_encode(mem_ctx, rep_control->cookie, rep_control->cookie_len);
291 if (cookie == NULL) {
292 return NULL;
294 if (cookie[0] != '\0') {
295 res = talloc_asprintf(mem_ctx, "%s:%d:%s",
296 LDB_CONTROL_PAGED_RESULTS_NAME,
297 control->critical,
298 cookie);
300 talloc_free(cookie);
301 } else {
302 res = talloc_asprintf(mem_ctx, "%s:%d",
303 LDB_CONTROL_PAGED_RESULTS_NAME,
304 control->critical);
306 return res;
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,
315 control->critical,
316 rep_control->targetPosition,
317 rep_control->contentCount,
318 rep_control->vlv_result,
319 rep_control->ctxid_len,
320 rep_control->contextId);
322 return res;
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,
331 control->critical,
332 rep_control->result,
333 rep_control->attr_desc);
335 return res;
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,
344 control->critical,
345 rep_control->result);
347 return res;
350 if (strcmp(control->oid, LDB_CONTROL_DIRSYNC_OID) == 0) {
351 char *cookie;
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) {
358 return NULL;
360 res = talloc_asprintf(mem_ctx, "%s:%d:%d:%d:%s",
361 LDB_CONTROL_DIRSYNC_NAME,
362 control->critical,
363 rep_control->flags,
364 rep_control->max_attributes,
365 cookie);
367 talloc_free(cookie);
368 return res;
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
377 * to it so we can represent it with local_oid:oid:criticity.
379 res = talloc_asprintf(mem_ctx, "local_oid:%s:%d",
380 control->oid,
381 control->critical);
382 } else {
383 res = talloc_asprintf(mem_ctx, "unknown oid:%s",
384 control->oid);
386 return res;
391 * A little trick to allow to use constants defined in headers rather than
392 * hardwritten in the file.
393 * "sizeof" will return the \0 char as well so it will take the place of ":"
394 * in the length of the string.
396 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
398 /* Parse one string and return associated control if parsing is successful*/
399 struct ldb_control *ldb_parse_control_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *control_strings)
401 struct ldb_control *ctrl;
402 char *error_string = NULL;
404 if (!(ctrl = talloc(mem_ctx, struct ldb_control))) {
405 ldb_oom(ldb);
406 return NULL;
409 if (LDB_CONTROL_CMP(control_strings,
410 LDB_CONTROL_VLV_REQ_NAME) == 0) {
411 struct ldb_vlv_req_control *control;
412 const char *p;
413 char attr[1024];
414 char ctxid[1024];
415 int crit, bc, ac, os, cc, ret;
417 attr[0] = '\0';
418 ctxid[0] = '\0';
419 p = &(control_strings[sizeof(LDB_CONTROL_VLV_REQ_NAME)]);
420 ret = sscanf(p, "%d:%d:%d:%d:%d:%1023[^$]", &crit, &bc, &ac, &os, &cc, ctxid);
421 if (ret < 5) {
422 ret = sscanf(p, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit, &bc, &ac, attr, ctxid);
425 if ((ret < 4) || (crit < 0) || (crit > 1)) {
426 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
427 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):bc(n):ac(n):<os(n):cc(n)|attr(s)>[:ctxid(o)]\n");
428 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number, s = string, o = b64 binary blob");
429 ldb_set_errstring(ldb, error_string);
430 talloc_free(error_string);
431 talloc_free(ctrl);
432 return NULL;
434 ctrl->oid = LDB_CONTROL_VLV_REQ_OID;
435 ctrl->critical = crit;
436 if (!(control = talloc(ctrl,
437 struct ldb_vlv_req_control))) {
438 ldb_oom(ldb);
439 talloc_free(ctrl);
440 return NULL;
442 control->beforeCount = bc;
443 control->afterCount = ac;
444 if (attr[0]) {
445 control->type = 1;
446 control->match.gtOrEq.value = talloc_strdup(control, attr);
447 control->match.gtOrEq.value_len = strlen(attr);
448 } else {
449 control->type = 0;
450 control->match.byOffset.offset = os;
451 control->match.byOffset.contentCount = cc;
453 if (ctxid[0]) {
454 control->ctxid_len = ldb_base64_decode(ctxid);
455 control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
456 } else {
457 control->ctxid_len = 0;
458 control->contextId = NULL;
460 ctrl->data = control;
462 return ctrl;
465 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DIRSYNC_NAME) == 0) {
466 struct ldb_dirsync_control *control;
467 const char *p;
468 char cookie[1024];
469 int crit, max_attrs, ret;
470 uint32_t flags;
472 cookie[0] = '\0';
473 p = &(control_strings[sizeof(LDB_CONTROL_DIRSYNC_NAME)]);
474 ret = sscanf(p, "%d:%u:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
476 if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
477 error_string = talloc_asprintf(mem_ctx, "invalid dirsync control syntax\n");
478 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
479 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number, o = b64 binary blob");
480 ldb_set_errstring(ldb, error_string);
481 talloc_free(error_string);
482 talloc_free(ctrl);
483 return NULL;
486 /* w2k3 seems to ignore the parameter,
487 * but w2k sends a wrong cookie when this value is to small
488 * this would cause looping forever, while getting
489 * the same data and same cookie forever
491 if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
493 ctrl->oid = LDB_CONTROL_DIRSYNC_OID;
494 ctrl->critical = crit;
495 control = talloc(ctrl, struct ldb_dirsync_control);
496 control->flags = flags;
497 control->max_attributes = max_attrs;
498 if (*cookie) {
499 control->cookie_len = ldb_base64_decode(cookie);
500 control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
501 } else {
502 control->cookie = NULL;
503 control->cookie_len = 0;
505 ctrl->data = control;
507 return ctrl;
510 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_ASQ_NAME) == 0) {
511 struct ldb_asq_control *control;
512 const char *p;
513 char attr[256];
514 int crit, ret;
516 attr[0] = '\0';
517 p = &(control_strings[sizeof(LDB_CONTROL_ASQ_NAME)]);
518 ret = sscanf(p, "%d:%255[^$]", &crit, attr);
519 if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
520 error_string = talloc_asprintf(mem_ctx, "invalid asq control syntax\n");
521 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):attr(s)\n");
522 error_string = talloc_asprintf_append(error_string, " note: b = boolean, s = string");
523 ldb_set_errstring(ldb, error_string);
524 talloc_free(error_string);
525 talloc_free(ctrl);
526 return NULL;
529 ctrl->oid = LDB_CONTROL_ASQ_OID;
530 ctrl->critical = crit;
531 control = talloc(ctrl, struct ldb_asq_control);
532 control->request = 1;
533 control->source_attribute = talloc_strdup(control, attr);
534 control->src_attr_len = strlen(attr);
535 ctrl->data = control;
537 return ctrl;
540 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_EXTENDED_DN_NAME) == 0) {
541 struct ldb_extended_dn_control *control;
542 const char *p;
543 int crit, type, ret;
545 p = &(control_strings[sizeof(LDB_CONTROL_EXTENDED_DN_NAME)]);
546 ret = sscanf(p, "%d:%d", &crit, &type);
547 if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
548 ret = sscanf(p, "%d", &crit);
549 if ((ret != 1) || (crit < 0) || (crit > 1)) {
550 error_string = talloc_asprintf(mem_ctx, "invalid extended_dn control syntax\n");
551 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)[:type(i)]\n");
552 error_string = talloc_asprintf_append(error_string, " note: b = boolean\n");
553 error_string = talloc_asprintf_append(error_string, " i = integer\n");
554 error_string = talloc_asprintf_append(error_string, " valid values are: 0 - hexadecimal representation\n");
555 error_string = talloc_asprintf_append(error_string, " 1 - normal string representation");
556 ldb_set_errstring(ldb, error_string);
557 talloc_free(error_string);
558 talloc_free(ctrl);
559 return NULL;
561 control = NULL;
562 } else {
563 control = talloc(ctrl, struct ldb_extended_dn_control);
564 control->type = type;
567 ctrl->oid = LDB_CONTROL_EXTENDED_DN_OID;
568 ctrl->critical = crit;
569 ctrl->data = talloc_steal(ctrl, control);
571 return ctrl;
574 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SD_FLAGS_NAME) == 0) {
575 struct ldb_sd_flags_control *control;
576 const char *p;
577 int crit, ret;
578 unsigned secinfo_flags;
580 p = &(control_strings[sizeof(LDB_CONTROL_SD_FLAGS_NAME)]);
581 ret = sscanf(p, "%d:%u", &crit, &secinfo_flags);
582 if ((ret != 2) || (crit < 0) || (crit > 1) || (secinfo_flags < 0) || (secinfo_flags > 0xF)) {
583 error_string = talloc_asprintf(mem_ctx, "invalid sd_flags control syntax\n");
584 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):secinfo_flags(n)\n");
585 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number");
586 ldb_set_errstring(ldb, error_string);
587 talloc_free(error_string);
588 talloc_free(ctrl);
589 return NULL;
592 ctrl->oid = LDB_CONTROL_SD_FLAGS_OID;
593 ctrl->critical = crit;
594 control = talloc(ctrl, struct ldb_sd_flags_control);
595 control->secinfo_flags = secinfo_flags;
596 ctrl->data = control;
598 return ctrl;
601 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SEARCH_OPTIONS_NAME) == 0) {
602 struct ldb_search_options_control *control;
603 const char *p;
604 int crit, ret;
605 unsigned search_options;
607 p = &(control_strings[sizeof(LDB_CONTROL_SEARCH_OPTIONS_NAME)]);
608 ret = sscanf(p, "%d:%u", &crit, &search_options);
609 if ((ret != 2) || (crit < 0) || (crit > 1) || (search_options < 0) || (search_options > 0xF)) {
610 error_string = talloc_asprintf(mem_ctx, "invalid search_options control syntax\n");
611 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):search_options(n)\n");
612 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number");
613 ldb_set_errstring(ldb, error_string);
614 talloc_free(error_string);
615 talloc_free(ctrl);
616 return NULL;
619 ctrl->oid = LDB_CONTROL_SEARCH_OPTIONS_OID;
620 ctrl->critical = crit;
621 control = talloc(ctrl, struct ldb_search_options_control);
622 control->search_options = search_options;
623 ctrl->data = control;
625 return ctrl;
628 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_BYPASS_OPERATIONAL_NAME) == 0) {
629 const char *p;
630 int crit, ret;
632 p = &(control_strings[sizeof(LDB_CONTROL_BYPASS_OPERATIONAL_NAME)]);
633 ret = sscanf(p, "%d", &crit);
634 if ((ret != 1) || (crit < 0) || (crit > 1)) {
635 error_string = talloc_asprintf(mem_ctx, "invalid bypassopreational control syntax\n");
636 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
637 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
638 ldb_set_errstring(ldb, error_string);
639 talloc_free(error_string);
640 talloc_free(ctrl);
641 return NULL;
644 ctrl->oid = LDB_CONTROL_BYPASS_OPERATIONAL_OID;
645 ctrl->critical = crit;
646 ctrl->data = NULL;
648 return ctrl;
651 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RELAX_NAME) == 0) {
652 const char *p;
653 int crit, ret;
655 p = &(control_strings[sizeof(LDB_CONTROL_RELAX_NAME)]);
656 ret = sscanf(p, "%d", &crit);
657 if ((ret != 1) || (crit < 0) || (crit > 1)) {
658 error_string = talloc_asprintf(mem_ctx, "invalid relax control syntax\n");
659 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
660 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
661 ldb_set_errstring(ldb, error_string);
662 talloc_free(error_string);
663 talloc_free(ctrl);
664 return NULL;
667 ctrl->oid = LDB_CONTROL_RELAX_OID;
668 ctrl->critical = crit;
669 ctrl->data = NULL;
671 return ctrl;
674 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RECALCULATE_SD_NAME) == 0) {
675 const char *p;
676 int crit, ret;
678 p = &(control_strings[sizeof(LDB_CONTROL_RECALCULATE_SD_NAME)]);
679 ret = sscanf(p, "%d", &crit);
680 if ((ret != 1) || (crit < 0) || (crit > 1)) {
681 error_string = talloc_asprintf(mem_ctx, "invalid recalculate_sd control syntax\n");
682 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
683 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
684 ldb_set_errstring(ldb, error_string);
685 talloc_free(error_string);
686 talloc_free(ctrl);
687 return NULL;
690 ctrl->oid = LDB_CONTROL_RECALCULATE_SD_OID;
691 ctrl->critical = crit;
692 ctrl->data = NULL;
694 return ctrl;
697 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DOMAIN_SCOPE_NAME) == 0) {
698 const char *p;
699 int crit, ret;
701 p = &(control_strings[sizeof(LDB_CONTROL_DOMAIN_SCOPE_NAME)]);
702 ret = sscanf(p, "%d", &crit);
703 if ((ret != 1) || (crit < 0) || (crit > 1)) {
704 error_string = talloc_asprintf(mem_ctx, "invalid domain_scope control syntax\n");
705 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
706 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
707 ldb_set_errstring(ldb, error_string);
708 talloc_free(error_string);
709 talloc_free(ctrl);
710 return NULL;
713 ctrl->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
714 ctrl->critical = crit;
715 ctrl->data = NULL;
717 return ctrl;
720 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PAGED_RESULTS_NAME) == 0) {
721 struct ldb_paged_control *control;
722 const char *p;
723 int crit, size, ret;
725 p = &(control_strings[sizeof(LDB_CONTROL_PAGED_RESULTS_NAME)]);
726 ret = sscanf(p, "%d:%d", &crit, &size);
727 if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
728 error_string = talloc_asprintf(mem_ctx, "invalid paged_results control syntax\n");
729 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):size(n)\n");
730 error_string = talloc_asprintf_append(error_string, " note: b = boolean, n = number");
731 ldb_set_errstring(ldb, error_string);
732 talloc_free(error_string);
733 talloc_free(ctrl);
734 return NULL;
737 ctrl->oid = LDB_CONTROL_PAGED_RESULTS_OID;
738 ctrl->critical = crit;
739 control = talloc(ctrl, struct ldb_paged_control);
740 control->size = size;
741 control->cookie = NULL;
742 control->cookie_len = 0;
743 ctrl->data = control;
745 return ctrl;
748 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SERVER_SORT_NAME) == 0) {
749 struct ldb_server_sort_control **control;
750 const char *p;
751 char attr[256];
752 char rule[128];
753 int crit, rev, ret;
755 attr[0] = '\0';
756 rule[0] = '\0';
757 p = &(control_strings[sizeof(LDB_CONTROL_SERVER_SORT_NAME)]);
758 ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
759 if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
760 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
761 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
762 error_string = talloc_asprintf_append(error_string, " note: b = boolean, s = string");
763 ldb_set_errstring(ldb, error_string);
764 talloc_free(error_string);
765 talloc_free(ctrl);
766 return NULL;
768 ctrl->oid = LDB_CONTROL_SERVER_SORT_OID;
769 ctrl->critical = crit;
770 control = talloc_array(ctrl, struct ldb_server_sort_control *, 2);
771 control[0] = talloc(control, struct ldb_server_sort_control);
772 control[0]->attributeName = talloc_strdup(control, attr);
773 if (rule[0])
774 control[0]->orderingRule = talloc_strdup(control, rule);
775 else
776 control[0]->orderingRule = NULL;
777 control[0]->reverse = rev;
778 control[1] = NULL;
779 ctrl->data = control;
781 return ctrl;
784 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_NOTIFICATION_NAME) == 0) {
785 const char *p;
786 int crit, ret;
788 p = &(control_strings[sizeof(LDB_CONTROL_NOTIFICATION_NAME)]);
789 ret = sscanf(p, "%d", &crit);
790 if ((ret != 1) || (crit < 0) || (crit > 1)) {
791 error_string = talloc_asprintf(mem_ctx, "invalid notification control syntax\n");
792 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
793 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
794 ldb_set_errstring(ldb, error_string);
795 talloc_free(error_string);
796 talloc_free(ctrl);
797 return NULL;
800 ctrl->oid = LDB_CONTROL_NOTIFICATION_OID;
801 ctrl->critical = crit;
802 ctrl->data = NULL;
804 return ctrl;
807 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_TREE_DELETE_NAME) == 0) {
808 const char *p;
809 int crit, ret;
811 p = &(control_strings[sizeof(LDB_CONTROL_TREE_DELETE_NAME)]);
812 ret = sscanf(p, "%d", &crit);
813 if ((ret != 1) || (crit < 0) || (crit > 1)) {
814 error_string = talloc_asprintf(mem_ctx, "invalid tree_delete control syntax\n");
815 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
816 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
817 ldb_set_errstring(ldb, error_string);
818 talloc_free(error_string);
819 talloc_free(ctrl);
820 return NULL;
823 ctrl->oid = LDB_CONTROL_TREE_DELETE_OID;
824 ctrl->critical = crit;
825 ctrl->data = NULL;
827 return ctrl;
830 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DELETED_NAME) == 0) {
831 const char *p;
832 int crit, ret;
834 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DELETED_NAME)]);
835 ret = sscanf(p, "%d", &crit);
836 if ((ret != 1) || (crit < 0) || (crit > 1)) {
837 error_string = talloc_asprintf(mem_ctx, "invalid show_deleted control syntax\n");
838 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
839 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
840 ldb_set_errstring(ldb, error_string);
841 talloc_free(error_string);
842 talloc_free(ctrl);
843 return NULL;
846 ctrl->oid = LDB_CONTROL_SHOW_DELETED_OID;
847 ctrl->critical = crit;
848 ctrl->data = NULL;
850 return ctrl;
853 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME) == 0) {
854 const char *p;
855 int crit, ret;
857 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME)]);
858 ret = sscanf(p, "%d", &crit);
859 if ((ret != 1) || (crit < 0) || (crit > 1)) {
860 error_string = talloc_asprintf(mem_ctx, "invalid show_deactivated_link control syntax\n");
861 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
862 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
863 ldb_set_errstring(ldb, error_string);
864 talloc_free(error_string);
865 talloc_free(ctrl);
866 return NULL;
869 ctrl->oid = LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID;
870 ctrl->critical = crit;
871 ctrl->data = NULL;
873 return ctrl;
876 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_RECYCLED_NAME) == 0) {
877 const char *p;
878 int crit, ret;
880 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_RECYCLED_NAME)]);
881 ret = sscanf(p, "%d", &crit);
882 if ((ret != 1) || (crit < 0) || (crit > 1)) {
883 error_string = talloc_asprintf(mem_ctx, "invalid show_recycled control syntax\n");
884 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
885 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
886 ldb_set_errstring(ldb, error_string);
887 talloc_free(error_string);
888 talloc_free(ctrl);
889 return NULL;
892 ctrl->oid = LDB_CONTROL_SHOW_RECYCLED_OID;
893 ctrl->critical = crit;
894 ctrl->data = NULL;
896 return ctrl;
899 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PERMISSIVE_MODIFY_NAME) == 0) {
900 const char *p;
901 int crit, ret;
903 p = &(control_strings[sizeof(LDB_CONTROL_PERMISSIVE_MODIFY_NAME)]);
904 ret = sscanf(p, "%d", &crit);
905 if ((ret != 1) || (crit < 0) || (crit > 1)) {
906 error_string = talloc_asprintf(mem_ctx, "invalid permissive_modify control syntax\n");
907 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
908 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
909 ldb_set_errstring(ldb, error_string);
910 talloc_free(error_string);
911 talloc_free(ctrl);
912 return NULL;
915 ctrl->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
916 ctrl->critical = crit;
917 ctrl->data = NULL;
919 return ctrl;
922 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_REVEAL_INTERNALS_NAME) == 0) {
923 const char *p;
924 int crit, ret;
926 p = &(control_strings[sizeof(LDB_CONTROL_REVEAL_INTERNALS_NAME)]);
927 ret = sscanf(p, "%d", &crit);
928 if ((ret != 1) || (crit < 0) || (crit > 1)) {
929 error_string = talloc_asprintf(mem_ctx, "invalid reveal_internals control syntax\n");
930 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
931 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
932 ldb_set_errstring(ldb, error_string);
933 talloc_free(error_string);
934 talloc_free(ctrl);
935 return NULL;
938 ctrl->oid = LDB_CONTROL_REVEAL_INTERNALS;
939 ctrl->critical = crit;
940 ctrl->data = NULL;
942 return ctrl;
945 if (strncmp(control_strings, "local_oid:", 10) == 0) {
946 const char *p;
947 int crit = 0, ret = 0;
948 char oid[256];
950 oid[0] = '\0';
951 p = &(control_strings[10]);
952 ret = sscanf(p, "%255[^:]:%d", oid, &crit);
954 if ((ret != 2) || strlen(oid) == 0 || (crit < 0) || (crit > 1)) {
955 error_string = talloc_asprintf(mem_ctx, "invalid local_oid control syntax\n");
956 error_string = talloc_asprintf_append(error_string, " syntax: oid(s):crit(b)\n");
957 error_string = talloc_asprintf_append(error_string, " note: b = boolean, s = string");
958 ldb_set_errstring(ldb, error_string);
959 talloc_free(error_string);
960 talloc_free(ctrl);
961 return NULL;
964 ctrl->oid = talloc_strdup(ctrl, oid);
965 if (!ctrl->oid) {
966 ldb_oom(ldb);
967 talloc_free(ctrl);
968 return NULL;
970 ctrl->critical = crit;
971 ctrl->data = NULL;
973 return ctrl;
976 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RODC_DCPROMO_NAME) == 0) {
977 const char *p;
978 int crit, ret;
980 p = &(control_strings[sizeof(LDB_CONTROL_RODC_DCPROMO_NAME)]);
981 ret = sscanf(p, "%d", &crit);
982 if ((ret != 1) || (crit < 0) || (crit > 1)) {
983 error_string = talloc_asprintf(mem_ctx, "invalid rodc_join 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);
988 talloc_free(ctrl);
989 return NULL;
992 ctrl->oid = LDB_CONTROL_RODC_DCPROMO_OID;
993 ctrl->critical = crit;
994 ctrl->data = NULL;
996 return ctrl;
999 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PROVISION_NAME) == 0) {
1000 const char *p;
1001 int crit, ret;
1003 p = &(control_strings[sizeof(LDB_CONTROL_PROVISION_NAME)]);
1004 ret = sscanf(p, "%d", &crit);
1005 if ((ret != 1) || (crit < 0) || (crit > 1)) {
1006 error_string = talloc_asprintf(mem_ctx, "invalid provision control syntax\n");
1007 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
1008 error_string = talloc_asprintf_append(error_string, " note: b = boolean");
1009 ldb_set_errstring(ldb, error_string);
1010 talloc_free(error_string);
1011 talloc_free(ctrl);
1012 return NULL;
1015 ctrl->oid = LDB_CONTROL_PROVISION_OID;
1016 ctrl->critical = crit;
1017 ctrl->data = NULL;
1019 return ctrl;
1022 * When no matching control has been found.
1024 return NULL;
1027 /* Parse controls from the format used on the command line and in ejs */
1028 struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char **control_strings)
1030 unsigned int i;
1031 struct ldb_control **ctrl;
1033 if (control_strings == NULL || control_strings[0] == NULL)
1034 return NULL;
1036 for (i = 0; control_strings[i]; i++);
1038 ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
1040 ldb_reset_err_string(ldb);
1041 for (i = 0; control_strings[i]; i++) {
1042 ctrl[i] = ldb_parse_control_from_string(ldb, ctrl, control_strings[i]);
1043 if (ctrl[i] == NULL) {
1044 if (ldb_errstring(ldb) == NULL) {
1045 /* no controls matched, throw an error */
1046 ldb_asprintf_errstring(ldb, "Invalid control name: '%s'", control_strings[i]);
1048 talloc_free(ctrl);
1049 return NULL;
1053 ctrl[i] = NULL;
1055 return ctrl;