af056d09a5ace940f9baf891dd93c35f7691b8c0
[nivanova/samba-autobuild/.git] / 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         if (strcmp(control->oid, LDB_CONTROL_DIRSYNC_EX_OID) == 0) {
371                 char *cookie;
372                 struct ldb_dirsync_control *rep_control = talloc_get_type(control->data,
373                                                                 struct ldb_dirsync_control);
374
375                 cookie = ldb_base64_encode(mem_ctx, rep_control->cookie,
376                                 rep_control->cookie_len);
377                 if (cookie == NULL) {
378                         return NULL;
379                 }
380                 res = talloc_asprintf(mem_ctx, "%s:%d:%d:%d:%s",
381                                         LDB_CONTROL_DIRSYNC_EX_NAME,
382                                         control->critical,
383                                         rep_control->flags,
384                                         rep_control->max_attributes,
385                                         cookie);
386
387                 talloc_free(cookie);
388                 return res;
389         }
390
391         if (strcmp(control->oid, LDB_CONTROL_VERIFY_NAME_OID) == 0) {
392                 struct ldb_verify_name_control *rep_control = talloc_get_type(control->data, struct ldb_verify_name_control);
393
394                 if (rep_control->gc != NULL) {
395                         res = talloc_asprintf(mem_ctx, "%s:%d:%d:%s",
396                                                 LDB_CONTROL_VERIFY_NAME_NAME,
397                                                 control->critical,
398                                                 rep_control->flags,
399                                                 rep_control->gc);
400
401                 } else {
402                         res = talloc_asprintf(mem_ctx, "%s:%d:%d",
403                                                 LDB_CONTROL_VERIFY_NAME_NAME,
404                                                 control->critical,
405                                                 rep_control->flags);
406                 }
407                 return res;
408         }
409
410         /*
411          * From here we don't know the control
412          */
413         if (control->data == NULL) {
414                 /*
415                  * We don't know the control but there is no real data attached
416                  * to it so we can represent it with local_oid:oid:criticity.
417                  */
418                 res = talloc_asprintf(mem_ctx, "local_oid:%s:%d",
419                                         control->oid,
420                                         control->critical);
421         } else {
422                 res = talloc_asprintf(mem_ctx, "unknown oid:%s",
423                                         control->oid);
424         }
425         return res;
426 }
427
428
429 /*
430  * A little trick to allow one to use constants defined in headers rather than
431  * hardwritten in the file.
432  * "sizeof" will return the \0 char as well so it will take the place of ":"
433  * in the length of the string.
434  */
435 #define LDB_CONTROL_CMP(control, NAME) strncmp(control, NAME ":", sizeof(NAME))
436
437 /* Parse one string and return associated control if parsing is successful*/
438 struct ldb_control *ldb_parse_control_from_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *control_strings)
439 {
440         struct ldb_control *ctrl;
441         char *error_string = NULL;
442
443         if (!(ctrl = talloc(mem_ctx, struct ldb_control))) {
444                 ldb_oom(ldb);
445                 return NULL;
446         }
447
448         if (LDB_CONTROL_CMP(control_strings,
449                                 LDB_CONTROL_VLV_REQ_NAME) == 0) {
450                 struct ldb_vlv_req_control *control;
451                 const char *p;
452                 char attr[1024];
453                 char ctxid[1024];
454                 int crit, bc, ac, os, cc, ret;
455
456                 attr[0] = '\0';
457                 ctxid[0] = '\0';
458                 p = &(control_strings[sizeof(LDB_CONTROL_VLV_REQ_NAME)]);
459                 ret = sscanf(p, "%d:%d:%d:%d:%d:%1023[^$]", &crit, &bc, &ac, &os, &cc, ctxid);
460                 if (ret < 5) {
461                         ret = sscanf(p, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit, &bc, &ac, attr, ctxid);
462                 }
463                         
464                 if ((ret < 4) || (crit < 0) || (crit > 1)) {
465                         error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
466                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):bc(n):ac(n):<os(n):cc(n)|attr(s)>[:ctxid(o)]\n");
467                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, s = string, o = b64 binary blob");
468                         ldb_set_errstring(ldb, error_string);
469                         talloc_free(error_string);
470                         talloc_free(ctrl);
471                         return NULL;
472                 }
473                 ctrl->oid = LDB_CONTROL_VLV_REQ_OID;
474                 ctrl->critical = crit;
475                 if (!(control = talloc(ctrl,
476                                         struct ldb_vlv_req_control))) {
477                         ldb_oom(ldb);
478                         talloc_free(ctrl);
479                         return NULL;
480                 }
481                 control->beforeCount = bc;
482                 control->afterCount = ac;
483                 if (attr[0]) {
484                         control->type = 1;
485                         control->match.gtOrEq.value = talloc_strdup(control, attr);
486                         control->match.gtOrEq.value_len = strlen(attr);
487                 } else {
488                         control->type = 0;
489                         control->match.byOffset.offset = os;
490                         control->match.byOffset.contentCount = cc;
491                 }
492                 if (ctxid[0]) {
493                         control->ctxid_len = ldb_base64_decode(ctxid);
494                         control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
495                 } else {
496                         control->ctxid_len = 0;
497                         control->contextId = NULL;
498                 }
499                 ctrl->data = control;
500
501                 return ctrl;
502         }
503
504         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DIRSYNC_NAME) == 0) {
505                 struct ldb_dirsync_control *control;
506                 const char *p;
507                 char cookie[1024];
508                 int crit, max_attrs, ret;
509                 uint32_t flags;
510                 
511                 cookie[0] = '\0';
512                 p = &(control_strings[sizeof(LDB_CONTROL_DIRSYNC_NAME)]);
513                 ret = sscanf(p, "%d:%u:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
514
515                 if ((ret < 3) || (crit < 0) || (crit > 1) || (max_attrs < 0)) {
516                         error_string = talloc_asprintf(mem_ctx, "invalid dirsync control syntax\n");
517                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
518                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, o = b64 binary blob");
519                         ldb_set_errstring(ldb, error_string);
520                         talloc_free(error_string);
521                         talloc_free(ctrl);
522                         return NULL;
523                 }
524
525                 /* w2k3 seems to ignore the parameter,
526                  * but w2k sends a wrong cookie when this value is to small
527                  * this would cause looping forever, while getting
528                  * the same data and same cookie forever
529                  */
530                 if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
531
532                 ctrl->oid = LDB_CONTROL_DIRSYNC_OID;
533                 ctrl->critical = crit;
534                 control = talloc(ctrl, struct ldb_dirsync_control);
535                 control->flags = flags;
536                 control->max_attributes = max_attrs;
537                 if (*cookie) {
538                         control->cookie_len = ldb_base64_decode(cookie);
539                         control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
540                 } else {
541                         control->cookie = NULL;
542                         control->cookie_len = 0;
543                 }
544                 ctrl->data = control;
545
546                 return ctrl;
547         }
548         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DIRSYNC_EX_NAME) == 0) {
549                 struct ldb_dirsync_control *control;
550                 const char *p;
551                 char cookie[1024];
552                 int crit, max_attrs, ret;
553                 uint32_t flags;
554
555                 cookie[0] = '\0';
556                 p = &(control_strings[sizeof(LDB_CONTROL_DIRSYNC_EX_NAME)]);
557                 ret = sscanf(p, "%d:%u:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
558
559                 if ((ret < 3) || (crit < 0) || (crit > 1) || (max_attrs < 0)) {
560                         error_string = talloc_asprintf(mem_ctx, "invalid %s control syntax\n",
561                                                        LDB_CONTROL_DIRSYNC_EX_NAME);
562                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
563                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, o = b64 binary blob");
564                         ldb_set_errstring(ldb, error_string);
565                         talloc_free(error_string);
566                         talloc_free(ctrl);
567                         return NULL;
568                 }
569
570                 /* w2k3 seems to ignore the parameter,
571                  * but w2k sends a wrong cookie when this value is to small
572                  * this would cause looping forever, while getting
573                  * the same data and same cookie forever
574                  */
575                 if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
576
577                 ctrl->oid = LDB_CONTROL_DIRSYNC_EX_OID;
578                 ctrl->critical = crit;
579                 control = talloc(ctrl, struct ldb_dirsync_control);
580                 control->flags = flags;
581                 control->max_attributes = max_attrs;
582                 if (*cookie) {
583                         control->cookie_len = ldb_base64_decode(cookie);
584                         control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
585                 } else {
586                         control->cookie = NULL;
587                         control->cookie_len = 0;
588                 }
589                 ctrl->data = control;
590
591                 return ctrl;
592         }
593
594         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_ASQ_NAME) == 0) {
595                 struct ldb_asq_control *control;
596                 const char *p;
597                 char attr[256];
598                 int crit, ret;
599
600                 attr[0] = '\0';
601                 p = &(control_strings[sizeof(LDB_CONTROL_ASQ_NAME)]);
602                 ret = sscanf(p, "%d:%255[^$]", &crit, attr);
603                 if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
604                         error_string = talloc_asprintf(mem_ctx, "invalid asq control syntax\n");
605                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):attr(s)\n");
606                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
607                         ldb_set_errstring(ldb, error_string);
608                         talloc_free(error_string);
609                         talloc_free(ctrl);
610                         return NULL;
611                 }
612
613                 ctrl->oid = LDB_CONTROL_ASQ_OID;
614                 ctrl->critical = crit;
615                 control = talloc(ctrl, struct ldb_asq_control);
616                 control->request = 1;
617                 control->source_attribute = talloc_strdup(control, attr);
618                 control->src_attr_len = strlen(attr);
619                 ctrl->data = control;
620
621                 return ctrl;
622         }
623
624         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_EXTENDED_DN_NAME) == 0) {
625                 struct ldb_extended_dn_control *control;
626                 const char *p;
627                 int crit, type, ret;
628
629                 p = &(control_strings[sizeof(LDB_CONTROL_EXTENDED_DN_NAME)]);
630                 ret = sscanf(p, "%d:%d", &crit, &type);
631                 if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
632                         ret = sscanf(p, "%d", &crit);
633                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
634                                 error_string = talloc_asprintf(mem_ctx, "invalid extended_dn control syntax\n");
635                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)[:type(i)]\n");
636                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean\n");
637                                 error_string = talloc_asprintf_append(error_string, "         i = integer\n");
638                                 error_string = talloc_asprintf_append(error_string, "   valid values are: 0 - hexadecimal representation\n");
639                                 error_string = talloc_asprintf_append(error_string, "                     1 - normal string representation");
640                                 ldb_set_errstring(ldb, error_string);
641                                 talloc_free(error_string);
642                                 talloc_free(ctrl);
643                                 return NULL;
644                         }
645                         control = NULL;
646                 } else {
647                         control = talloc(ctrl, struct ldb_extended_dn_control);
648                         control->type = type;
649                 }
650
651                 ctrl->oid = LDB_CONTROL_EXTENDED_DN_OID;
652                 ctrl->critical = crit;
653                 ctrl->data = talloc_steal(ctrl, control);
654
655                 return ctrl;
656         }
657
658         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SD_FLAGS_NAME) == 0) {
659                 struct ldb_sd_flags_control *control;
660                 const char *p;
661                 int crit, ret;
662                 unsigned secinfo_flags;
663
664                 p = &(control_strings[sizeof(LDB_CONTROL_SD_FLAGS_NAME)]);
665                 ret = sscanf(p, "%d:%u", &crit, &secinfo_flags);
666                 if ((ret != 2) || (crit < 0) || (crit > 1) || (secinfo_flags > 0xF)) {
667                         error_string = talloc_asprintf(mem_ctx, "invalid sd_flags control syntax\n");
668                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):secinfo_flags(n)\n");
669                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
670                         ldb_set_errstring(ldb, error_string);
671                         talloc_free(error_string);
672                         talloc_free(ctrl);
673                         return NULL;
674                 }
675
676                 ctrl->oid = LDB_CONTROL_SD_FLAGS_OID;
677                 ctrl->critical = crit;
678                 control = talloc(ctrl, struct ldb_sd_flags_control);
679                 control->secinfo_flags = secinfo_flags;
680                 ctrl->data = control;
681
682                 return ctrl;
683         }
684
685         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SEARCH_OPTIONS_NAME) == 0) {
686                 struct ldb_search_options_control *control;
687                 const char *p;
688                 int crit, ret;
689                 unsigned search_options;
690
691                 p = &(control_strings[sizeof(LDB_CONTROL_SEARCH_OPTIONS_NAME)]);
692                 ret = sscanf(p, "%d:%u", &crit, &search_options);
693                 if ((ret != 2) || (crit < 0) || (crit > 1) || (search_options > 0xF)) {
694                         error_string = talloc_asprintf(mem_ctx, "invalid search_options control syntax\n");
695                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):search_options(n)\n");
696                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
697                         ldb_set_errstring(ldb, error_string);
698                         talloc_free(error_string);
699                         talloc_free(ctrl);
700                         return NULL;
701                 }
702
703                 ctrl->oid = LDB_CONTROL_SEARCH_OPTIONS_OID;
704                 ctrl->critical = crit;
705                 control = talloc(ctrl, struct ldb_search_options_control);
706                 control->search_options = search_options;
707                 ctrl->data = control;
708
709                 return ctrl;
710         }
711
712         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_BYPASS_OPERATIONAL_NAME) == 0) {
713                 const char *p;
714                 int crit, ret;
715
716                 p = &(control_strings[sizeof(LDB_CONTROL_BYPASS_OPERATIONAL_NAME)]);
717                 ret = sscanf(p, "%d", &crit);
718                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
719                         error_string = talloc_asprintf(mem_ctx, "invalid bypassopreational control syntax\n");
720                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
721                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
722                         ldb_set_errstring(ldb, error_string);
723                         talloc_free(error_string);
724                         talloc_free(ctrl);
725                         return NULL;
726                 }
727
728                 ctrl->oid = LDB_CONTROL_BYPASS_OPERATIONAL_OID;
729                 ctrl->critical = crit;
730                 ctrl->data = NULL;
731
732                 return ctrl;
733         }
734
735         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RELAX_NAME) == 0) {
736                 const char *p;
737                 int crit, ret;
738
739                 p = &(control_strings[sizeof(LDB_CONTROL_RELAX_NAME)]);
740                 ret = sscanf(p, "%d", &crit);
741                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
742                         error_string = talloc_asprintf(mem_ctx, "invalid relax control syntax\n");
743                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
744                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
745                         ldb_set_errstring(ldb, error_string);
746                         talloc_free(error_string);
747                         talloc_free(ctrl);
748                         return NULL;
749                 }
750
751                 ctrl->oid = LDB_CONTROL_RELAX_OID;
752                 ctrl->critical = crit;
753                 ctrl->data = NULL;
754
755                 return ctrl;
756         }
757
758         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RECALCULATE_SD_NAME) == 0) {
759                 const char *p;
760                 int crit, ret;
761
762                 p = &(control_strings[sizeof(LDB_CONTROL_RECALCULATE_SD_NAME)]);
763                 ret = sscanf(p, "%d", &crit);
764                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
765                         error_string = talloc_asprintf(mem_ctx, "invalid recalculate_sd control syntax\n");
766                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
767                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
768                         ldb_set_errstring(ldb, error_string);
769                         talloc_free(error_string);
770                         talloc_free(ctrl);
771                         return NULL;
772                 }
773
774                 ctrl->oid = LDB_CONTROL_RECALCULATE_SD_OID;
775                 ctrl->critical = crit;
776                 ctrl->data = NULL;
777
778                 return ctrl;
779         }
780
781         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_DOMAIN_SCOPE_NAME) == 0) {
782                 const char *p;
783                 int crit, ret;
784
785                 p = &(control_strings[sizeof(LDB_CONTROL_DOMAIN_SCOPE_NAME)]);
786                 ret = sscanf(p, "%d", &crit);
787                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
788                         error_string = talloc_asprintf(mem_ctx, "invalid domain_scope control syntax\n");
789                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
790                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
791                         ldb_set_errstring(ldb, error_string);
792                         talloc_free(error_string);
793                         talloc_free(ctrl);
794                         return NULL;
795                 }
796
797                 ctrl->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
798                 ctrl->critical = crit;
799                 ctrl->data = NULL;
800
801                 return ctrl;
802         }
803
804         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PAGED_RESULTS_NAME) == 0) {
805                 struct ldb_paged_control *control;
806                 const char *p;
807                 int crit, size, ret;
808                 
809                 p = &(control_strings[sizeof(LDB_CONTROL_PAGED_RESULTS_NAME)]);
810                 ret = sscanf(p, "%d:%d", &crit, &size);
811                 if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
812                         error_string = talloc_asprintf(mem_ctx, "invalid paged_results control syntax\n");
813                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):size(n)\n");
814                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
815                         ldb_set_errstring(ldb, error_string);
816                         talloc_free(error_string);
817                         talloc_free(ctrl);
818                         return NULL;
819                 }
820
821                 ctrl->oid = LDB_CONTROL_PAGED_RESULTS_OID;
822                 ctrl->critical = crit;
823                 control = talloc(ctrl, struct ldb_paged_control);
824                 control->size = size;
825                 control->cookie = NULL;
826                 control->cookie_len = 0;
827                 ctrl->data = control;
828
829                 return ctrl;
830         }
831
832         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SERVER_SORT_NAME) == 0) {
833                 struct ldb_server_sort_control **control;
834                 const char *p;
835                 char attr[256];
836                 char rule[128];
837                 int crit, rev, ret;
838
839                 attr[0] = '\0';
840                 rule[0] = '\0';
841                 p = &(control_strings[sizeof(LDB_CONTROL_SERVER_SORT_NAME)]);
842                 ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
843                 if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
844                         error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
845                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
846                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
847                         ldb_set_errstring(ldb, error_string);
848                         talloc_free(error_string);
849                         talloc_free(ctrl);
850                         return NULL;
851                 }
852                 ctrl->oid = LDB_CONTROL_SERVER_SORT_OID;
853                 ctrl->critical = crit;
854                 control = talloc_array(ctrl, struct ldb_server_sort_control *, 2);
855                 control[0] = talloc(control, struct ldb_server_sort_control);
856                 control[0]->attributeName = talloc_strdup(control, attr);
857                 if (rule[0])
858                         control[0]->orderingRule = talloc_strdup(control, rule);
859                 else
860                         control[0]->orderingRule = NULL;
861                 control[0]->reverse = rev;
862                 control[1] = NULL;
863                 ctrl->data = control;
864
865                 return ctrl;
866         }
867
868         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_NOTIFICATION_NAME) == 0) {
869                 const char *p;
870                 int crit, ret;
871
872                 p = &(control_strings[sizeof(LDB_CONTROL_NOTIFICATION_NAME)]);
873                 ret = sscanf(p, "%d", &crit);
874                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
875                         error_string = talloc_asprintf(mem_ctx, "invalid notification control syntax\n");
876                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
877                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
878                         ldb_set_errstring(ldb, error_string);
879                         talloc_free(error_string);
880                         talloc_free(ctrl);
881                         return NULL;
882                 }
883
884                 ctrl->oid = LDB_CONTROL_NOTIFICATION_OID;
885                 ctrl->critical = crit;
886                 ctrl->data = NULL;
887
888                 return ctrl;
889         }
890
891         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_TREE_DELETE_NAME) == 0) {
892                 const char *p;
893                 int crit, ret;
894
895                 p = &(control_strings[sizeof(LDB_CONTROL_TREE_DELETE_NAME)]);
896                 ret = sscanf(p, "%d", &crit);
897                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
898                         error_string = talloc_asprintf(mem_ctx, "invalid tree_delete control syntax\n");
899                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
900                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
901                         ldb_set_errstring(ldb, error_string);
902                         talloc_free(error_string);
903                         talloc_free(ctrl);
904                         return NULL;
905                 }
906
907                 ctrl->oid = LDB_CONTROL_TREE_DELETE_OID;
908                 ctrl->critical = crit;
909                 ctrl->data = NULL;
910
911                 return ctrl;
912         }
913
914         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DELETED_NAME) == 0) {
915                 const char *p;
916                 int crit, ret;
917
918                 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DELETED_NAME)]);
919                 ret = sscanf(p, "%d", &crit);
920                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
921                         error_string = talloc_asprintf(mem_ctx, "invalid show_deleted control syntax\n");
922                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
923                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
924                         ldb_set_errstring(ldb, error_string);
925                         talloc_free(error_string);
926                         talloc_free(ctrl);
927                         return NULL;
928                 }
929
930                 ctrl->oid = LDB_CONTROL_SHOW_DELETED_OID;
931                 ctrl->critical = crit;
932                 ctrl->data = NULL;
933
934                 return ctrl;
935         }
936
937         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME) == 0) {
938                 const char *p;
939                 int crit, ret;
940
941                 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_DEACTIVATED_LINK_NAME)]);
942                 ret = sscanf(p, "%d", &crit);
943                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
944                         error_string = talloc_asprintf(mem_ctx, "invalid show_deactivated_link control syntax\n");
945                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
946                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
947                         ldb_set_errstring(ldb, error_string);
948                         talloc_free(error_string);
949                         talloc_free(ctrl);
950                         return NULL;
951                 }
952
953                 ctrl->oid = LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID;
954                 ctrl->critical = crit;
955                 ctrl->data = NULL;
956
957                 return ctrl;
958         }
959
960         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_SHOW_RECYCLED_NAME) == 0) {
961                 const char *p;
962                 int crit, ret;
963
964                 p = &(control_strings[sizeof(LDB_CONTROL_SHOW_RECYCLED_NAME)]);
965                 ret = sscanf(p, "%d", &crit);
966                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
967                         error_string = talloc_asprintf(mem_ctx, "invalid show_recycled control syntax\n");
968                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
969                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
970                         ldb_set_errstring(ldb, error_string);
971                         talloc_free(error_string);
972                         talloc_free(ctrl);
973                         return NULL;
974                 }
975
976                 ctrl->oid = LDB_CONTROL_SHOW_RECYCLED_OID;
977                 ctrl->critical = crit;
978                 ctrl->data = NULL;
979
980                 return ctrl;
981         }
982
983         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PERMISSIVE_MODIFY_NAME) == 0) {
984                 const char *p;
985                 int crit, ret;
986
987                 p = &(control_strings[sizeof(LDB_CONTROL_PERMISSIVE_MODIFY_NAME)]);
988                 ret = sscanf(p, "%d", &crit);
989                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
990                         error_string = talloc_asprintf(mem_ctx, "invalid permissive_modify control syntax\n");
991                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
992                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
993                         ldb_set_errstring(ldb, error_string);
994                         talloc_free(error_string);
995                         talloc_free(ctrl);
996                         return NULL;
997                 }
998
999                 ctrl->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
1000                 ctrl->critical = crit;
1001                 ctrl->data = NULL;
1002
1003                 return ctrl;
1004         }
1005
1006         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_REVEAL_INTERNALS_NAME) == 0) {
1007                 const char *p;
1008                 int crit, ret;
1009
1010                 p = &(control_strings[sizeof(LDB_CONTROL_REVEAL_INTERNALS_NAME)]);
1011                 ret = sscanf(p, "%d", &crit);
1012                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
1013                         error_string = talloc_asprintf(mem_ctx, "invalid reveal_internals control syntax\n");
1014                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
1015                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
1016                         ldb_set_errstring(ldb, error_string);
1017                         talloc_free(error_string);
1018                         talloc_free(ctrl);
1019                         return NULL;
1020                 }
1021
1022                 ctrl->oid = LDB_CONTROL_REVEAL_INTERNALS;
1023                 ctrl->critical = crit;
1024                 ctrl->data = NULL;
1025
1026                 return ctrl;
1027         }
1028
1029         if (strncmp(control_strings, "local_oid:", 10) == 0) {
1030                 const char *p;
1031                 int crit = 0, ret = 0;
1032                 char oid[256];
1033
1034                 oid[0] = '\0';
1035                 p = &(control_strings[10]);
1036                 ret = sscanf(p, "%255[^:]:%d", oid, &crit);
1037
1038                 if ((ret != 2) || strlen(oid) == 0 || (crit < 0) || (crit > 1)) {
1039                         error_string = talloc_asprintf(mem_ctx, "invalid local_oid control syntax\n");
1040                         error_string = talloc_asprintf_append(error_string, " syntax: oid(s):crit(b)\n");
1041                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
1042                         ldb_set_errstring(ldb, error_string);
1043                         talloc_free(error_string);
1044                         talloc_free(ctrl);
1045                         return NULL;
1046                 }
1047
1048                 ctrl->oid = talloc_strdup(ctrl, oid);
1049                 if (!ctrl->oid) {
1050                         ldb_oom(ldb);
1051                         talloc_free(ctrl);
1052                         return NULL;
1053                 }
1054                 ctrl->critical = crit;
1055                 ctrl->data = NULL;
1056
1057                 return ctrl;
1058         }
1059
1060         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_RODC_DCPROMO_NAME) == 0) {
1061                 const char *p;
1062                 int crit, ret;
1063
1064                 p = &(control_strings[sizeof(LDB_CONTROL_RODC_DCPROMO_NAME)]);
1065                 ret = sscanf(p, "%d", &crit);
1066                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
1067                         error_string = talloc_asprintf(mem_ctx, "invalid rodc_join control syntax\n");
1068                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
1069                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
1070                         ldb_set_errstring(ldb, error_string);
1071                         talloc_free(error_string);
1072                         talloc_free(ctrl);
1073                         return NULL;
1074                 }
1075
1076                 ctrl->oid = LDB_CONTROL_RODC_DCPROMO_OID;
1077                 ctrl->critical = crit;
1078                 ctrl->data = NULL;
1079
1080                 return ctrl;
1081         }
1082
1083         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_PROVISION_NAME) == 0) {
1084                 const char *p;
1085                 int crit, ret;
1086
1087                 p = &(control_strings[sizeof(LDB_CONTROL_PROVISION_NAME)]);
1088                 ret = sscanf(p, "%d", &crit);
1089                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
1090                         error_string = talloc_asprintf(mem_ctx, "invalid provision control syntax\n");
1091                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
1092                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
1093                         ldb_set_errstring(ldb, error_string);
1094                         talloc_free(error_string);
1095                         talloc_free(ctrl);
1096                         return NULL;
1097                 }
1098
1099                 ctrl->oid = LDB_CONTROL_PROVISION_OID;
1100                 ctrl->critical = crit;
1101                 ctrl->data = NULL;
1102
1103                 return ctrl;
1104         }
1105         if (LDB_CONTROL_CMP(control_strings, LDB_CONTROL_VERIFY_NAME_NAME) == 0) {
1106                 const char *p;
1107                 char gc[1024];
1108                 int crit, flags, ret;
1109                 struct ldb_verify_name_control *control;
1110
1111                 gc[0] = '\0';
1112
1113                 p = &(control_strings[sizeof(LDB_CONTROL_VERIFY_NAME_NAME)]);
1114                 ret = sscanf(p, "%d:%d:%1023[^$]", &crit, &flags, gc);
1115                 if ((ret != 3) || (crit < 0) || (crit > 1)) {
1116                         ret = sscanf(p, "%d:%d", &crit, &flags);
1117                         if ((ret != 2) || (crit < 0) || (crit > 1)) {
1118                                 error_string = talloc_asprintf(mem_ctx, "invalid verify_name control syntax\n");
1119                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(i)[:gc(s)]\n");
1120                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
1121                                 error_string = talloc_asprintf_append(error_string, "   note: i = integer");
1122                                 error_string = talloc_asprintf_append(error_string, "   note: s = string");
1123                                 ldb_set_errstring(ldb, error_string);
1124                                 talloc_free(error_string);
1125                                 talloc_free(ctrl);
1126                                 return NULL;
1127                         }
1128                 }
1129
1130                 ctrl->oid = LDB_CONTROL_VERIFY_NAME_OID;
1131                 ctrl->critical = crit;
1132                 control = talloc(ctrl, struct ldb_verify_name_control);
1133                 control->gc = talloc_strdup(control, gc);
1134                 control->gc_len = strlen(gc);
1135                 control->flags = flags;
1136                 ctrl->data = control;
1137                 return ctrl;
1138         }
1139         /*
1140          * When no matching control has been found.
1141          */
1142         return NULL;
1143 }
1144
1145 /* Parse controls from the format used on the command line and in ejs */
1146 struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char **control_strings)
1147 {
1148         unsigned int i;
1149         struct ldb_control **ctrl;
1150
1151         if (control_strings == NULL || control_strings[0] == NULL)
1152                 return NULL;
1153
1154         for (i = 0; control_strings[i]; i++);
1155
1156         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
1157
1158         ldb_reset_err_string(ldb);
1159         for (i = 0; control_strings[i]; i++) {
1160                 ctrl[i] = ldb_parse_control_from_string(ldb, ctrl, control_strings[i]);
1161                 if (ctrl[i] == NULL) {
1162                         if (ldb_errstring(ldb) == NULL) {
1163                                 /* no controls matched, throw an error */
1164                                 ldb_asprintf_errstring(ldb, "Invalid control name: '%s'", control_strings[i]);
1165                         }
1166                         talloc_free(ctrl);
1167                         return NULL;
1168                 }
1169         }
1170
1171         ctrl[i] = NULL;
1172
1173         return ctrl;
1174 }
1175
1176