s3-pylibsmb: Add get_oplock_break
[Samba/gebeck_regimport.git] / lib / ldb / common / ldb_controls.c
blob097ae20ece75073871f2ab9aa7cbc6e496cea2db
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;
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,
377 control->critical,
378 rep_control->flags,
379 rep_control->gc);
381 } else {
382 res = talloc_asprintf(mem_ctx, "%s:%d:%d",
383 LDB_CONTROL_VERIFY_NAME_NAME,
384 control->critical,
385 rep_control->flags);
387 return res;
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",
399 control->oid,
400 control->critical);
401 } else {
402 res = talloc_asprintf(mem_ctx, "unknown oid:%s",
403 control->oid);
405 return res;
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))) {
424 ldb_oom(ldb);
425 return NULL;
428 if (LDB_CONTROL_CMP(control_strings,
429 LDB_CONTROL_VLV_REQ_NAME) == 0) {
430 struct ldb_vlv_req_control *control;
431 const char *p;
432 char attr[1024];
433 char ctxid[1024];
434 int crit, bc, ac, os, cc, ret;
436 attr[0] = '\0';
437 ctxid[0] = '\0';
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);
440 if (ret < 5) {
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);
450 talloc_free(ctrl);
451 return NULL;
453 ctrl->oid = LDB_CONTROL_VLV_REQ_OID;
454 ctrl->critical = crit;
455 if (!(control = talloc(ctrl,
456 struct ldb_vlv_req_control))) {
457 ldb_oom(ldb);
458 talloc_free(ctrl);
459 return NULL;
461 control->beforeCount = bc;
462 control->afterCount = ac;
463 if (attr[0]) {
464 control->type = 1;
465 control->match.gtOrEq.value = talloc_strdup(control, attr);
466 control->match.gtOrEq.value_len = strlen(attr);
467 } else {
468 control->type = 0;
469 control->match.byOffset.offset = os;
470 control->match.byOffset.contentCount = cc;
472 if (ctxid[0]) {
473 control->ctxid_len = ldb_base64_decode(ctxid);
474 control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
475 } else {
476 control->ctxid_len = 0;
477 control->contextId = NULL;
479 ctrl->data = control;
481 return ctrl;
484 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DIRSYNC_NAME) == 0) {
485 struct ldb_dirsync_control *control;
486 const char *p;
487 char cookie[1024];
488 int crit, max_attrs, ret;
489 uint32_t flags;
491 cookie[0] = '\0';
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);
501 talloc_free(ctrl);
502 return NULL;
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;
517 if (*cookie) {
518 control->cookie_len = ldb_base64_decode(cookie);
519 control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
520 } else {
521 control->cookie = NULL;
522 control->cookie_len = 0;
524 ctrl->data = control;
526 return ctrl;
529 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_ASQ_NAME) == 0) {
530 struct ldb_asq_control *control;
531 const char *p;
532 char attr[256];
533 int crit, ret;
535 attr[0] = '\0';
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);
544 talloc_free(ctrl);
545 return NULL;
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;
556 return ctrl;
559 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_EXTENDED_DN_NAME) == 0) {
560 struct ldb_extended_dn_control *control;
561 const char *p;
562 int crit, type, ret;
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);
577 talloc_free(ctrl);
578 return NULL;
580 control = NULL;
581 } else {
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);
590 return ctrl;
593 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SD_FLAGS_NAME) == 0) {
594 struct ldb_sd_flags_control *control;
595 const char *p;
596 int crit, ret;
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);
607 talloc_free(ctrl);
608 return NULL;
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;
617 return ctrl;
620 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SEARCH_OPTIONS_NAME) == 0) {
621 struct ldb_search_options_control *control;
622 const char *p;
623 int crit, ret;
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);
634 talloc_free(ctrl);
635 return NULL;
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;
644 return ctrl;
647 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_BYPASS_OPERATIONAL_NAME) == 0) {
648 const char *p;
649 int crit, ret;
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);
659 talloc_free(ctrl);
660 return NULL;
663 ctrl->oid = LDB_CONTROL_BYPASS_OPERATIONAL_OID;
664 ctrl->critical = crit;
665 ctrl->data = NULL;
667 return ctrl;
670 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RELAX_NAME) == 0) {
671 const char *p;
672 int crit, ret;
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);
682 talloc_free(ctrl);
683 return NULL;
686 ctrl->oid = LDB_CONTROL_RELAX_OID;
687 ctrl->critical = crit;
688 ctrl->data = NULL;
690 return ctrl;
693 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RECALCULATE_SD_NAME) == 0) {
694 const char *p;
695 int crit, ret;
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);
705 talloc_free(ctrl);
706 return NULL;
709 ctrl->oid = LDB_CONTROL_RECALCULATE_SD_OID;
710 ctrl->critical = crit;
711 ctrl->data = NULL;
713 return ctrl;
716 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DOMAIN_SCOPE_NAME) == 0) {
717 const char *p;
718 int crit, ret;
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);
728 talloc_free(ctrl);
729 return NULL;
732 ctrl->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
733 ctrl->critical = crit;
734 ctrl->data = NULL;
736 return ctrl;
739 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PAGED_RESULTS_NAME) == 0) {
740 struct ldb_paged_control *control;
741 const char *p;
742 int crit, size, ret;
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);
752 talloc_free(ctrl);
753 return NULL;
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;
764 return ctrl;
767 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SERVER_SORT_NAME) == 0) {
768 struct ldb_server_sort_control **control;
769 const char *p;
770 char attr[256];
771 char rule[128];
772 int crit, rev, ret;
774 attr[0] = '\0';
775 rule[0] = '\0';
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);
784 talloc_free(ctrl);
785 return NULL;
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);
792 if (rule[0])
793 control[0]->orderingRule = talloc_strdup(control, rule);
794 else
795 control[0]->orderingRule = NULL;
796 control[0]->reverse = rev;
797 control[1] = NULL;
798 ctrl->data = control;
800 return ctrl;
803 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_NOTIFICATION_NAME) == 0) {
804 const char *p;
805 int crit, ret;
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);
815 talloc_free(ctrl);
816 return NULL;
819 ctrl->oid = LDB_CONTROL_NOTIFICATION_OID;
820 ctrl->critical = crit;
821 ctrl->data = NULL;
823 return ctrl;
826 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_TREE_DELETE_NAME) == 0) {
827 const char *p;
828 int crit, ret;
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);
838 talloc_free(ctrl);
839 return NULL;
842 ctrl->oid = LDB_CONTROL_TREE_DELETE_OID;
843 ctrl->critical = crit;
844 ctrl->data = NULL;
846 return ctrl;
849 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DELETED_NAME) == 0) {
850 const char *p;
851 int crit, ret;
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);
861 talloc_free(ctrl);
862 return NULL;
865 ctrl->oid = LDB_CONTROL_SHOW_DELETED_OID;
866 ctrl->critical = crit;
867 ctrl->data = NULL;
869 return ctrl;
872 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME) == 0) {
873 const char *p;
874 int crit, ret;
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);
884 talloc_free(ctrl);
885 return NULL;
888 ctrl->oid = LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID;
889 ctrl->critical = crit;
890 ctrl->data = NULL;
892 return ctrl;
895 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_RECYCLED_NAME) == 0) {
896 const char *p;
897 int crit, ret;
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);
907 talloc_free(ctrl);
908 return NULL;
911 ctrl->oid = LDB_CONTROL_SHOW_RECYCLED_OID;
912 ctrl->critical = crit;
913 ctrl->data = NULL;
915 return ctrl;
918 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PERMISSIVE_MODIFY_NAME) == 0) {
919 const char *p;
920 int crit, ret;
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);
930 talloc_free(ctrl);
931 return NULL;
934 ctrl->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
935 ctrl->critical = crit;
936 ctrl->data = NULL;
938 return ctrl;
941 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_REVEAL_INTERNALS_NAME) == 0) {
942 const char *p;
943 int crit, ret;
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);
953 talloc_free(ctrl);
954 return NULL;
957 ctrl->oid = LDB_CONTROL_REVEAL_INTERNALS;
958 ctrl->critical = crit;
959 ctrl->data = NULL;
961 return ctrl;
964 if (strncmp(control_strings, "local_oid:", 10) == 0) {
965 const char *p;
966 int crit = 0, ret = 0;
967 char oid[256];
969 oid[0] = '\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);
979 talloc_free(ctrl);
980 return NULL;
983 ctrl->oid = talloc_strdup(ctrl, oid);
984 if (!ctrl->oid) {
985 ldb_oom(ldb);
986 talloc_free(ctrl);
987 return NULL;
989 ctrl->critical = crit;
990 ctrl->data = NULL;
992 return ctrl;
995 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RODC_DCPROMO_NAME) == 0) {
996 const char *p;
997 int crit, ret;
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);
1007 talloc_free(ctrl);
1008 return NULL;
1011 ctrl->oid = LDB_CONTROL_RODC_DCPROMO_OID;
1012 ctrl->critical = crit;
1013 ctrl->data = NULL;
1015 return ctrl;
1018 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PROVISION_NAME) == 0) {
1019 const char *p;
1020 int crit, ret;
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);
1030 talloc_free(ctrl);
1031 return NULL;
1034 ctrl->oid = LDB_CONTROL_PROVISION_OID;
1035 ctrl->critical = crit;
1036 ctrl->data = NULL;
1038 return ctrl;
1040 if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_VERIFY_NAME_NAME) == 0) {
1041 const char *p;
1042 char gc[1024];
1043 int crit, flags, ret;
1044 struct ldb_verify_name_control *control;
1046 gc[0] = '\0';
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);
1060 talloc_free(ctrl);
1061 return NULL;
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;
1072 return ctrl;
1075 * When no matching control has been found.
1077 return NULL;
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)
1083 unsigned int i;
1084 struct ldb_control **ctrl;
1086 if (control_strings == NULL || control_strings[0] == NULL)
1087 return 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]);
1101 talloc_free(ctrl);
1102 return NULL;
1106 ctrl[i] = NULL;
1108 return ctrl;