s4-dbcheck: support the 'none' option for prompts
[samba.git] / source4 / lib / ldb / common / ldb_controls.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005
5
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
9    
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.
14
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.
19
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/>.
22 */
23
24 /*
25  *  Name: ldb_controls.c
26  *
27  *  Component: ldb controls utility functions
28  *
29  *  Description: helper functions for control modules
30  *
31  *  Author: Simo Sorce
32  */
33
34 #include "ldb_private.h"
35
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)
39 {
40         unsigned int i;
41
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;
46                         }
47                 }
48
49                 return req->controls[i];
50         }
51
52         return NULL;
53 }
54
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)
58 {
59         unsigned int i;
60
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;
65                         }
66                 }
67
68                 return rep->controls[i];
69         }
70
71         return NULL;
72 }
73
74 /*
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)
78  *
79  * Returns 0 on error.
80  */
81 int ldb_save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver)
82 {
83         struct ldb_control **lcs, **lcs_old;
84         unsigned int i, j;
85
86         lcs_old = req->controls;
87         if (saver != NULL) {
88                 *saver = lcs_old;
89         }
90
91         for (i = 0; req->controls && req->controls[i]; i++);
92         if (i == 0) {
93                 req->controls = NULL;
94                 return 1;
95         }
96
97         lcs = talloc_array(req, struct ldb_control *, i + 1);
98         if (!lcs) {
99                 return 0;
100         }
101
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++;
106         }
107         lcs[j] = NULL;
108
109         req->controls = talloc_realloc(req, lcs, struct ldb_control *, j + 1);
110         if (req->controls == NULL) {
111                 return 0;
112         }
113         return 1;
114 }
115
116 /*
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".
120  *
121  * Returns NULL on error (OOM) or an empty control list.
122  */
123 struct ldb_control **ldb_controls_except_specified(struct ldb_control **controls_in, 
124                                                TALLOC_CTX *mem_ctx, 
125                                                struct ldb_control *exclude)
126 {
127         struct ldb_control **lcs = NULL;
128         unsigned int i, j, n;
129
130         for (i = 0; controls_in && controls_in[i]; i++);
131         if (i == 0) {
132                 return NULL;
133         }
134         n = i;
135
136         for (i = 0, j = 0; controls_in && controls_in[i]; i++) {
137                 if (exclude == controls_in[i]) continue;
138
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;
148                         }
149                 }
150
151                 lcs[j] = controls_in[i];
152                 talloc_reparent(controls_in, lcs, lcs[j]);
153                 j++;
154         }
155         if (lcs) {
156                 lcs[j] = NULL;
157
158                 lcs = talloc_realloc(mem_ctx, lcs, struct ldb_control *, j + 1);
159         }
160
161         return lcs;
162 }
163
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)
167 {
168         unsigned int i;
169
170         if (controls == NULL) {
171                 return 0;
172         }
173
174         for (i = 0; controls[i]; i++) {
175                 if (controls[i]->critical) {
176                         return 1;
177                 }
178         }
179
180         return 0;
181 }
182
183 int ldb_request_add_control(struct ldb_request *req, const char *oid, bool critical, void *data)
184 {
185         unsigned int i, n;
186         struct ldb_control **ctrls;
187         struct ldb_control *ctrl;
188
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;
193                 }
194         }
195
196         ctrls = talloc_array(req,
197                                struct ldb_control *,
198                                n + 2);
199         if (!ctrls) return LDB_ERR_OPERATIONS_ERROR;
200
201         for (i=0; i<n; i++) {
202                 ctrls[i] = req->controls[i];
203         }
204
205         req->controls = ctrls;
206         ctrls[n] = NULL;
207         ctrls[n+1] = NULL;
208
209         ctrl = talloc(ctrls, struct ldb_control);
210         if (!ctrl) return LDB_ERR_OPERATIONS_ERROR;
211
212         ctrl->oid       = talloc_strdup(ctrl, oid);
213         if (!ctrl->oid) return LDB_ERR_OPERATIONS_ERROR;
214         ctrl->critical  = critical;
215         ctrl->data      = data;
216
217         ctrls[n] = ctrl;
218         return LDB_SUCCESS;
219 }
220
221 int ldb_reply_add_control(struct ldb_reply *ares, const char *oid, bool critical, void *data)
222 {
223         unsigned n;
224         struct ldb_control **ctrls;
225         struct ldb_control *ctrl;
226
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;
231                 }
232                 n++; 
233         }
234
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;
242
243         ctrl = talloc(ctrls, struct ldb_control);
244         if (!ctrl) return LDB_ERR_OPERATIONS_ERROR;
245
246         ctrl->oid       = talloc_strdup(ctrl, oid);
247         if (!ctrl->oid) return LDB_ERR_OPERATIONS_ERROR;
248         ctrl->critical  = critical;
249         ctrl->data      = data;
250
251         ctrls[n] = ctrl;
252         return LDB_SUCCESS;
253 }
254
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)
257 {
258         unsigned int n;
259         int ret;
260
261         ret = ldb_request_add_control(req, oid, critical, data);
262         if (ret != LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
263                 return ret;
264         }
265
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;
271                 }
272         }
273
274         return LDB_ERR_OPERATIONS_ERROR;
275 }
276
277 /*
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
281  */
282 char *ldb_control_to_string(TALLOC_CTX *mem_ctx, const struct ldb_control *control)
283 {
284         char *res = NULL;
285
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;
289
290                 cookie = ldb_base64_encode(mem_ctx, rep_control->cookie, rep_control->cookie_len);
291                 if (cookie == NULL) {
292                         return NULL;
293                 }
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);
299
300                         talloc_free(cookie);
301                 } else {
302                         res = talloc_asprintf(mem_ctx, "%s:%d",
303                                                 LDB_CONTROL_PAGED_RESULTS_NAME,
304                                                 control->critical);
305                 }
306                 return res;
307         }
308
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);
312
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);
321
322                 return res;
323         }
324
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);
328
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);
334
335                 return res;
336         }
337
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);
341
342                 res = talloc_asprintf(mem_ctx, "%s:%d:%d",
343                                         LDB_CONTROL_SORT_RESP_NAME,
344                                         control->critical,
345                                         rep_control->result);
346
347                 return res;
348         }
349
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);
354
355                 cookie = ldb_base64_encode(mem_ctx, rep_control->cookie,
356                                 rep_control->cookie_len);
357                 if (cookie == NULL) {
358                         return NULL;
359                 }
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);
366
367                 talloc_free(cookie);
368                 return res;
369         }
370
371         /*
372          * From here we don't know the control
373          */
374         if (control->data == NULL) {
375                 /*
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
378                  */
379                 res = talloc_asprintf(mem_ctx, "local_oid:%s:%d",
380                                         control->oid,
381                                         control->critical);
382                 return res;
383         }
384
385                 res = talloc_asprintf(mem_ctx, "unknown oid:%s",
386                                         control->oid);
387         return res;
388 }
389
390
391 /*
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
396  */
397 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
398
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)
401 {
402         struct ldb_control *ctrl;
403         char *error_string = NULL;
404
405         if (!(ctrl = talloc(mem_ctx, struct ldb_control))) {
406                 ldb_oom(ldb);
407                 return NULL;
408         }
409
410         if (LDB_CONTROL_CMP(control_strings,
411                                 LDB_CONTROL_VLV_REQ_NAME) == 0) {
412                 struct ldb_vlv_req_control *control;
413                 const char *p;
414                 char attr[1024];
415                 char ctxid[1024];
416                 int crit, bc, ac, os, cc, ret;
417
418                 attr[0] = '\0';
419                 ctxid[0] = '\0';
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);
422                 if (ret < 5) {
423                         ret = sscanf(p, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit, &bc, &ac, attr, ctxid);
424                 }
425                         
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);
432                         return NULL;
433                 }
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                         return NULL;
440                 }
441                 control->beforeCount = bc;
442                 control->afterCount = ac;
443                 if (attr[0]) {
444                         control->type = 1;
445                         control->match.gtOrEq.value = talloc_strdup(control, attr);
446                         control->match.gtOrEq.value_len = strlen(attr);
447                 } else {
448                         control->type = 0;
449                         control->match.byOffset.offset = os;
450                         control->match.byOffset.contentCount = cc;
451                 }
452                 if (ctxid[0]) {
453                         control->ctxid_len = ldb_base64_decode(ctxid);
454                         control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
455                 } else {
456                         control->ctxid_len = 0;
457                         control->contextId = NULL;
458                 }
459                 ctrl->data = control;
460
461                 return ctrl;
462         }
463
464         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DIRSYNC_NAME) == 0) {
465                 struct ldb_dirsync_control *control;
466                 const char *p;
467                 char cookie[1024];
468                 int crit, max_attrs, ret;
469                 uint32_t flags;
470                 
471                 cookie[0] = '\0';
472                 p = &(control_strings[sizeof(LDB_CONTROL_DIRSYNC_NAME)]);
473                 ret = sscanf(p, "%d:%u:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
474
475                 if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
476                         error_string = talloc_asprintf(mem_ctx, "invalid dirsync control syntax\n");
477                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
478                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, o = b64 binary blob");
479                         ldb_set_errstring(ldb, error_string);
480                         talloc_free(error_string);
481                         return NULL;
482                 }
483
484                 /* w2k3 seems to ignore the parameter,
485                  * but w2k sends a wrong cookie when this value is to small
486                  * this would cause looping forever, while getting
487                  * the same data and same cookie forever
488                  */
489                 if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
490
491                 ctrl->oid = LDB_CONTROL_DIRSYNC_OID;
492                 ctrl->critical = crit;
493                 control = talloc(ctrl, struct ldb_dirsync_control);
494                 control->flags = flags;
495                 control->max_attributes = max_attrs;
496                 if (*cookie) {
497                         control->cookie_len = ldb_base64_decode(cookie);
498                         control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
499                 } else {
500                         control->cookie = NULL;
501                         control->cookie_len = 0;
502                 }
503                 ctrl->data = control;
504
505                 return ctrl;
506         }
507
508         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_ASQ_NAME) == 0) {
509                 struct ldb_asq_control *control;
510                 const char *p;
511                 char attr[256];
512                 int crit, ret;
513
514                 attr[0] = '\0';
515                 p = &(control_strings[sizeof(LDB_CONTROL_ASQ_NAME)]);
516                 ret = sscanf(p, "%d:%255[^$]", &crit, attr);
517                 if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
518                         error_string = talloc_asprintf(mem_ctx, "invalid asq control syntax\n");
519                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):attr(s)\n");
520                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
521                         ldb_set_errstring(ldb, error_string);
522                         talloc_free(error_string);
523                         return NULL;
524                 }
525
526                 ctrl->oid = LDB_CONTROL_ASQ_OID;
527                 ctrl->critical = crit;
528                 control = talloc(ctrl, struct ldb_asq_control);
529                 control->request = 1;
530                 control->source_attribute = talloc_strdup(control, attr);
531                 control->src_attr_len = strlen(attr);
532                 ctrl->data = control;
533
534                 return ctrl;
535         }
536
537         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_EXTENDED_DN_NAME) == 0) {
538                 struct ldb_extended_dn_control *control;
539                 const char *p;
540                 int crit, type, ret;
541
542                 p = &(control_strings[sizeof(LDB_CONTROL_EXTENDED_DN_NAME)]);
543                 ret = sscanf(p, "%d:%d", &crit, &type);
544                 if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
545                         ret = sscanf(p, "%d", &crit);
546                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
547                                 error_string = talloc_asprintf(mem_ctx, "invalid extended_dn control syntax\n");
548                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)[:type(i)]\n");
549                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean\n");
550                                 error_string = talloc_asprintf_append(error_string, "         i = integer\n");
551                                 error_string = talloc_asprintf_append(error_string, "   valid values are: 0 - hexadecimal representation\n");
552                                 error_string = talloc_asprintf_append(error_string, "                     1 - normal string representation");
553                                 ldb_set_errstring(ldb, error_string);
554                                 talloc_free(error_string);
555                                 return NULL;
556                         }
557                         control = NULL;
558                 } else {
559                         control = talloc(ctrl, struct ldb_extended_dn_control);
560                         control->type = type;
561                 }
562
563                 ctrl->oid = LDB_CONTROL_EXTENDED_DN_OID;
564                 ctrl->critical = crit;
565                 ctrl->data = talloc_steal(ctrl, control);
566
567                 return ctrl;
568         }
569
570         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SD_FLAGS_NAME) == 0) {
571                 struct ldb_sd_flags_control *control;
572                 const char *p;
573                 int crit, ret;
574                 unsigned secinfo_flags;
575
576                 p = &(control_strings[sizeof(LDB_CONTROL_SD_FLAGS_NAME)]);
577                 ret = sscanf(p, "%d:%u", &crit, &secinfo_flags);
578                 if ((ret != 2) || (crit < 0) || (crit > 1) || (secinfo_flags < 0) || (secinfo_flags > 0xF)) {
579                         error_string = talloc_asprintf(mem_ctx, "invalid sd_flags control syntax\n");
580                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):secinfo_flags(n)\n");
581                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
582                         ldb_set_errstring(ldb, error_string);
583                         talloc_free(error_string);
584                         return NULL;
585                 }
586
587                 ctrl->oid = LDB_CONTROL_SD_FLAGS_OID;
588                 ctrl->critical = crit;
589                 control = talloc(ctrl, struct ldb_sd_flags_control);
590                 control->secinfo_flags = secinfo_flags;
591                 ctrl->data = control;
592
593                 return ctrl;
594         }
595
596         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SEARCH_OPTIONS_NAME) == 0) {
597                 struct ldb_search_options_control *control;
598                 const char *p;
599                 int crit, ret;
600                 unsigned search_options;
601
602                 p = &(control_strings[sizeof(LDB_CONTROL_SEARCH_OPTIONS_NAME)]);
603                 ret = sscanf(p, "%d:%u", &crit, &search_options);
604                 if ((ret != 2) || (crit < 0) || (crit > 1) || (search_options < 0) || (search_options > 0xF)) {
605                         error_string = talloc_asprintf(mem_ctx, "invalid search_options control syntax\n");
606                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):search_options(n)\n");
607                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
608                         ldb_set_errstring(ldb, error_string);
609                         talloc_free(error_string);
610                         return NULL;
611                 }
612
613                 ctrl->oid = LDB_CONTROL_SEARCH_OPTIONS_OID;
614                 ctrl->critical = crit;
615                 control = talloc(ctrl, struct ldb_search_options_control);
616                 control->search_options = search_options;
617                 ctrl->data = control;
618
619                 return ctrl;
620         }
621
622         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_BYPASS_OPERATIONAL_NAME) == 0) {
623                 const char *p;
624                 int crit, ret;
625
626                 p = &(control_strings[sizeof(LDB_CONTROL_BYPASS_OPERATIONAL_NAME)]);
627                 ret = sscanf(p, "%d", &crit);
628                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
629                         error_string = talloc_asprintf(mem_ctx, "invalid bypassopreational control syntax\n");
630                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
631                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
632                         ldb_set_errstring(ldb, error_string);
633                         talloc_free(error_string);
634                         return NULL;
635                 }
636
637                 ctrl->oid = LDB_CONTROL_BYPASS_OPERATIONAL_OID;
638                 ctrl->critical = crit;
639                 ctrl->data = NULL;
640
641                 return ctrl;
642         }
643
644         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RELAX_NAME) == 0) {
645                 const char *p;
646                 int crit, ret;
647
648                 p = &(control_strings[sizeof(LDB_CONTROL_RELAX_NAME)]);
649                 ret = sscanf(p, "%d", &crit);
650                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
651                         error_string = talloc_asprintf(mem_ctx, "invalid relax control syntax\n");
652                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
653                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
654                         ldb_set_errstring(ldb, error_string);
655                         talloc_free(error_string);
656                         return NULL;
657                 }
658
659                 ctrl->oid = LDB_CONTROL_RELAX_OID;
660                 ctrl->critical = crit;
661                 ctrl->data = NULL;
662
663                 return ctrl;
664         }
665
666         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RECALCULATE_SD_NAME) == 0) {
667                 const char *p;
668                 int crit, ret;
669
670                 p = &(control_strings[sizeof(LDB_CONTROL_RECALCULATE_SD_NAME)]);
671                 ret = sscanf(p, "%d", &crit);
672                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
673                         error_string = talloc_asprintf(mem_ctx, "invalid recalculate_sd control syntax\n");
674                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
675                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
676                         ldb_set_errstring(ldb, error_string);
677                         talloc_free(error_string);
678                         return NULL;
679                 }
680
681                 ctrl->oid = LDB_CONTROL_RECALCULATE_SD_OID;
682                 ctrl->critical = crit;
683                 ctrl->data = NULL;
684
685                 return ctrl;
686         }
687
688         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DOMAIN_SCOPE_NAME) == 0) {
689                 const char *p;
690                 int crit, ret;
691
692                 p = &(control_strings[sizeof(LDB_CONTROL_DOMAIN_SCOPE_NAME)]);
693                 ret = sscanf(p, "%d", &crit);
694                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
695                         error_string = talloc_asprintf(mem_ctx, "invalid domain_scope control syntax\n");
696                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
697                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
698                         ldb_set_errstring(ldb, error_string);
699                         talloc_free(error_string);
700                         return NULL;
701                 }
702
703                 ctrl->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
704                 ctrl->critical = crit;
705                 ctrl->data = NULL;
706
707                 return ctrl;
708         }
709
710         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PAGED_RESULTS_NAME) == 0) {
711                 struct ldb_paged_control *control;
712                 const char *p;
713                 int crit, size, ret;
714                 
715                 p = &(control_strings[sizeof(LDB_CONTROL_PAGED_RESULTS_NAME)]);
716                 ret = sscanf(p, "%d:%d", &crit, &size);
717                 if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
718                         error_string = talloc_asprintf(mem_ctx, "invalid paged_results control syntax\n");
719                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):size(n)\n");
720                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
721                         ldb_set_errstring(ldb, error_string);
722                         talloc_free(error_string);
723                         return NULL;
724                 }
725
726                 ctrl->oid = LDB_CONTROL_PAGED_RESULTS_OID;
727                 ctrl->critical = crit;
728                 control = talloc(ctrl, struct ldb_paged_control);
729                 control->size = size;
730                 control->cookie = NULL;
731                 control->cookie_len = 0;
732                 ctrl->data = control;
733
734                 return ctrl;
735         }
736
737         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SERVER_SORT_NAME) == 0) {
738                 struct ldb_server_sort_control **control;
739                 const char *p;
740                 char attr[256];
741                 char rule[128];
742                 int crit, rev, ret;
743
744                 attr[0] = '\0';
745                 rule[0] = '\0';
746                 p = &(control_strings[sizeof(LDB_CONTROL_SERVER_SORT_NAME)]);
747                 ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
748                 if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
749                         error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
750                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
751                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
752                         ldb_set_errstring(ldb, error_string);
753                         talloc_free(error_string);
754                         return NULL;
755                 }
756                 ctrl->oid = LDB_CONTROL_SERVER_SORT_OID;
757                 ctrl->critical = crit;
758                 control = talloc_array(ctrl, struct ldb_server_sort_control *, 2);
759                 control[0] = talloc(control, struct ldb_server_sort_control);
760                 control[0]->attributeName = talloc_strdup(control, attr);
761                 if (rule[0])
762                         control[0]->orderingRule = talloc_strdup(control, rule);
763                 else
764                         control[0]->orderingRule = NULL;
765                 control[0]->reverse = rev;
766                 control[1] = NULL;
767                 ctrl->data = control;
768
769                 return ctrl;
770         }
771
772         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_NOTIFICATION_NAME) == 0) {
773                 const char *p;
774                 int crit, ret;
775
776                 p = &(control_strings[sizeof(LDB_CONTROL_NOTIFICATION_NAME)]);
777                 ret = sscanf(p, "%d", &crit);
778                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
779                         error_string = talloc_asprintf(mem_ctx, "invalid notification control syntax\n");
780                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
781                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
782                         ldb_set_errstring(ldb, error_string);
783                         talloc_free(error_string);
784                         return NULL;
785                 }
786
787                 ctrl->oid = LDB_CONTROL_NOTIFICATION_OID;
788                 ctrl->critical = crit;
789                 ctrl->data = NULL;
790
791                 return ctrl;
792         }
793
794         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_TREE_DELETE_NAME) == 0) {
795                 const char *p;
796                 int crit, ret;
797
798                 p = &(control_strings[sizeof(LDB_CONTROL_TREE_DELETE_NAME)]);
799                 ret = sscanf(p, "%d", &crit);
800                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
801                         error_string = talloc_asprintf(mem_ctx, "invalid tree_delete control syntax\n");
802                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
803                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
804                         ldb_set_errstring(ldb, error_string);
805                         talloc_free(error_string);
806                         return NULL;
807                 }
808
809                 ctrl->oid = LDB_CONTROL_TREE_DELETE_OID;
810                 ctrl->critical = crit;
811                 ctrl->data = NULL;
812
813                 return ctrl;
814         }
815
816         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DELETED_NAME) == 0) {
817                 const char *p;
818                 int crit, ret;
819
820                 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DELETED_NAME)]);
821                 ret = sscanf(p, "%d", &crit);
822                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
823                         error_string = talloc_asprintf(mem_ctx, "invalid show_deleted control syntax\n");
824                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
825                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
826                         ldb_set_errstring(ldb, error_string);
827                         talloc_free(error_string);
828                         return NULL;
829                 }
830
831                 ctrl->oid = LDB_CONTROL_SHOW_DELETED_OID;
832                 ctrl->critical = crit;
833                 ctrl->data = NULL;
834
835                 return ctrl;
836         }
837
838         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME) == 0) {
839                 const char *p;
840                 int crit, ret;
841
842                 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME)]);
843                 ret = sscanf(p, "%d", &crit);
844                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
845                         error_string = talloc_asprintf(mem_ctx, "invalid show_deactivated_link control syntax\n");
846                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
847                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
848                         ldb_set_errstring(ldb, error_string);
849                         talloc_free(error_string);
850                         return NULL;
851                 }
852
853                 ctrl->oid = LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID;
854                 ctrl->critical = crit;
855                 ctrl->data = NULL;
856
857                 return ctrl;
858         }
859
860         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_RECYCLED_NAME) == 0) {
861                 const char *p;
862                 int crit, ret;
863
864                 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_RECYCLED_NAME)]);
865                 ret = sscanf(p, "%d", &crit);
866                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
867                         error_string = talloc_asprintf(mem_ctx, "invalid show_recycled control syntax\n");
868                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
869                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
870                         ldb_set_errstring(ldb, error_string);
871                         talloc_free(error_string);
872                         return NULL;
873                 }
874
875                 ctrl->oid = LDB_CONTROL_SHOW_RECYCLED_OID;
876                 ctrl->critical = crit;
877                 ctrl->data = NULL;
878
879                 return ctrl;
880         }
881
882         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PERMISSIVE_MODIFY_NAME) == 0) {
883                 const char *p;
884                 int crit, ret;
885
886                 p = &(control_strings[sizeof(LDB_CONTROL_PERMISSIVE_MODIFY_NAME)]);
887                 ret = sscanf(p, "%d", &crit);
888                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
889                         error_string = talloc_asprintf(mem_ctx, "invalid permissive_modify control syntax\n");
890                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
891                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
892                         ldb_set_errstring(ldb, error_string);
893                         talloc_free(error_string);
894                         return NULL;
895                 }
896
897                 ctrl->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
898                 ctrl->critical = crit;
899                 ctrl->data = NULL;
900
901                 return ctrl;
902         }
903
904         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_REVEAL_INTERNALS_NAME) == 0) {
905                 const char *p;
906                 int crit, ret;
907
908                 p = &(control_strings[sizeof(LDB_CONTROL_REVEAL_INTERNALS_NAME)]);
909                 ret = sscanf(p, "%d", &crit);
910                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
911                         error_string = talloc_asprintf(mem_ctx, "invalid reveal_internals control syntax\n");
912                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
913                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
914                         ldb_set_errstring(ldb, error_string);
915                         talloc_free(error_string);
916                         return NULL;
917                 }
918
919                 ctrl->oid = LDB_CONTROL_REVEAL_INTERNALS;
920                 ctrl->critical = crit;
921                 ctrl->data = NULL;
922
923                 return ctrl;
924         }
925
926         if (strncmp(control_strings, "local_oid:", 10) == 0) {
927                 const char *p;
928                 int crit = 0, ret = 0;
929                 char oid[256];
930
931                 oid[0] = '\0';
932                 p = &(control_strings[10]);
933                 ret = sscanf(p, "%64[^:]:%d", oid, &crit);
934
935                 if ((ret != 2) || strlen(oid) == 0 || (crit < 0) || (crit > 1)) {
936                         error_string = talloc_asprintf(mem_ctx, "invalid local_oid control syntax\n");
937                         error_string = talloc_asprintf_append(error_string, " syntax: oid(s):crit(b)\n");
938                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
939                         ldb_set_errstring(ldb, error_string);
940                         talloc_free(error_string);
941                         return NULL;
942                 }
943
944                 ctrl->oid = talloc_strdup(ctrl, oid);
945                 if (!ctrl->oid) {
946                         ldb_oom(ldb);
947                         return NULL;
948                 }
949                 ctrl->critical = crit;
950                 ctrl->data = NULL;
951
952                 return ctrl;
953         }
954
955         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RODC_DCPROMO_NAME) == 0) {
956                 const char *p;
957                 int crit, ret;
958
959                 p = &(control_strings[sizeof(LDB_CONTROL_RODC_DCPROMO_NAME)]);
960                 ret = sscanf(p, "%d", &crit);
961                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
962                         error_string = talloc_asprintf(mem_ctx, "invalid rodc_join control syntax\n");
963                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
964                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
965                         ldb_set_errstring(ldb, error_string);
966                         talloc_free(error_string);
967                         return NULL;
968                 }
969
970                 ctrl->oid = LDB_CONTROL_RODC_DCPROMO_OID;
971                 ctrl->critical = crit;
972                 ctrl->data = NULL;
973
974                 return ctrl;
975         }
976
977         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PROVISION_NAME) == 0) {
978                 const char *p;
979                 int crit, ret;
980
981                 p = &(control_strings[sizeof(LDB_CONTROL_PROVISION_NAME)]);
982                 ret = sscanf(p, "%d", &crit);
983                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
984                         error_string = talloc_asprintf(mem_ctx, "invalid provision control syntax\n");
985                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
986                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
987                         ldb_set_errstring(ldb, error_string);
988                         talloc_free(error_string);
989                         return NULL;
990                 }
991
992                 ctrl->oid = LDB_CONTROL_PROVISION_OID;
993                 ctrl->critical = crit;
994                 ctrl->data = NULL;
995
996                 return ctrl;
997         }
998         /*
999          * When no matching control has been found.
1000          */
1001         return NULL;
1002 }
1003
1004 /*
1005  * A little trick to allow to use constants defined in headers rather than
1006  * hardwritten in the file hardwritten in the file
1007  * sizeof will return the \0 char as well so it will take the place of ":" in the
1008  * length of the string
1009  */
1010 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
1011
1012 /* Parse controls from the format used on the command line and in ejs */
1013 struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char **control_strings)
1014 {
1015         unsigned int i;
1016         struct ldb_control **ctrl;
1017
1018         if (control_strings == NULL || control_strings[0] == NULL)
1019                 return NULL;
1020
1021         for (i = 0; control_strings[i]; i++);
1022
1023         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
1024
1025         ldb_reset_err_string(ldb);
1026         for (i = 0; control_strings[i]; i++) {
1027                 ctrl[i] = ldb_parse_control_from_string(ldb, ctrl, control_strings[i]);
1028                 if (ctrl[i] == NULL) {
1029                         if( ldb_errstring == NULL ) {
1030                                 /* no controls matched, throw an error */
1031                                 ldb_asprintf_errstring(ldb, "Invalid control name: '%s'", control_strings[i]);
1032                         }
1033                         talloc_free(ctrl);
1034                         return NULL;
1035                 }
1036         }
1037
1038         ctrl[i] = NULL;
1039
1040         return ctrl;
1041 }
1042
1043