6ce08652a7a5039d10e4e37060900cdac5cc7147
[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 /* saves the current controls list into the "saver" and replace the one in req with a new one excluding
75 the "exclude" control */
76 /* returns 0 on error */
77 int save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver)
78 {
79         struct ldb_control **lcs;
80         unsigned int i, j;
81
82         *saver = req->controls;
83         for (i = 0; req->controls[i]; i++);
84         if (i == 1) {
85                 req->controls = NULL;
86                 return 1;
87         }
88
89         lcs = talloc_array(req, struct ldb_control *, i);
90         if (!lcs) {
91                 return 0;
92         }
93
94         for (i = 0, j = 0; (*saver)[i]; i++) {
95                 if (exclude == (*saver)[i]) continue;
96                 lcs[j] = (*saver)[i];
97                 j++;
98         }
99         lcs[j] = NULL;
100
101         req->controls = lcs;
102         return 1;
103 }
104
105 /* Returns a list of controls, except the one specified.  Included
106  * controls become a child of returned list if they were children of
107  * controls_in */
108 struct ldb_control **controls_except_specified(struct ldb_control **controls_in, 
109                                                TALLOC_CTX *mem_ctx, 
110                                                struct ldb_control *exclude)
111 {
112         struct ldb_control **lcs = NULL;
113         unsigned int i, j;
114
115         for (i = 0; controls_in && controls_in[i]; i++);
116
117         if (i == 0) {
118                 return NULL;
119         }
120
121         for (i = 0, j = 0; controls_in && controls_in[i]; i++) {
122                 if (exclude == controls_in[i]) continue;
123
124                 if (!lcs) {
125                         /* Allocate here so if we remove the only
126                          * control, or there were no controls, we
127                          * don't allocate at all, and just return
128                          * NULL */
129                         lcs = talloc_array(mem_ctx, struct ldb_control *, i);
130                         if (!lcs) {
131                                 return NULL;
132                         }
133                 }
134
135                 lcs[j] = controls_in[i];
136                 talloc_reparent(controls_in, lcs, lcs[j]);
137                 j++;
138         }
139         if (lcs) {
140                 lcs[j] = NULL;
141         }
142
143         return lcs;
144 }
145
146 /* check if there's any control marked as critical in the list */
147 /* return True if any, False if none */
148 int check_critical_controls(struct ldb_control **controls)
149 {
150         unsigned int i;
151
152         if (controls == NULL) {
153                 return 0;
154         }
155
156         for (i = 0; controls[i]; i++) {
157                 if (controls[i]->critical) {
158                         return 1;
159                 }
160         }
161
162         return 0;
163 }
164
165 int ldb_request_add_control(struct ldb_request *req, const char *oid, bool critical, void *data)
166 {
167         unsigned int i, n;
168         struct ldb_control **ctrls;
169         struct ldb_control *ctrl;
170
171         for (n=0; req->controls && req->controls[n];n++) { 
172                 /* having two controls of the same OID makes no sense */
173                 if (req->controls[n]->oid && strcmp(oid, req->controls[n]->oid) == 0) {
174                         return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
175                 }
176         }
177
178         ctrls = talloc_array(req,
179                                struct ldb_control *,
180                                n + 2);
181         if (!ctrls) return LDB_ERR_OPERATIONS_ERROR;
182
183         for (i=0; i<n; i++) {
184                 ctrls[i] = req->controls[i];
185         }
186
187         req->controls = ctrls;
188         ctrls[n] = NULL;
189         ctrls[n+1] = NULL;
190
191         ctrl = talloc(ctrls, struct ldb_control);
192         if (!ctrl) return LDB_ERR_OPERATIONS_ERROR;
193
194         ctrl->oid       = talloc_strdup(ctrl, oid);
195         if (!ctrl->oid) return LDB_ERR_OPERATIONS_ERROR;
196         ctrl->critical  = critical;
197         ctrl->data      = data;
198
199         ctrls[n] = ctrl;
200         return LDB_SUCCESS;
201 }
202
203 int ldb_reply_add_control(struct ldb_reply *ares, const char *oid, bool critical, void *data)
204 {
205         unsigned n;
206         struct ldb_control **ctrls;
207         struct ldb_control *ctrl;
208
209         for (n=0; ares->controls && ares->controls[n];) { 
210                 /* having two controls of the same OID makes no sense */
211                 if (ares->controls[n]->oid && strcmp(oid, ares->controls[n]->oid) == 0) {
212                         return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
213                 }
214                 n++; 
215         }
216
217         ctrls = talloc_realloc(ares, ares->controls,
218                                struct ldb_control *,
219                                n + 2);
220         if (!ctrls) return LDB_ERR_OPERATIONS_ERROR;
221         ares->controls = ctrls;
222         ctrls[n] = NULL;
223         ctrls[n+1] = NULL;
224
225         ctrl = talloc(ctrls, struct ldb_control);
226         if (!ctrl) return LDB_ERR_OPERATIONS_ERROR;
227
228         ctrl->oid       = talloc_strdup(ctrl, oid);
229         if (!ctrl->oid) return LDB_ERR_OPERATIONS_ERROR;
230         ctrl->critical  = critical;
231         ctrl->data      = data;
232
233         ctrls[n] = ctrl;
234         return LDB_SUCCESS;
235 }
236
237 /* Add a control to the request, replacing the old one if it is already in the request */
238 int ldb_request_replace_control(struct ldb_request *req, const char *oid, bool critical, void *data)
239 {
240         unsigned int n;
241         int ret;
242
243         ret = ldb_request_add_control(req, oid, critical, data);
244         if (ret != LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
245                 return ret;
246         }
247
248         for (n=0; req->controls[n];n++) {
249                 if (req->controls[n]->oid && strcmp(oid, req->controls[n]->oid) == 0) {
250                         req->controls[n]->critical = critical;
251                         req->controls[n]->data = data;
252                         return LDB_SUCCESS;
253                 }
254         }
255
256         return LDB_ERR_OPERATIONS_ERROR;
257 }
258
259 /* Parse controls from the format used on the command line and in ejs */
260
261 struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char **control_strings)
262 {
263         unsigned int i;
264         struct ldb_control **ctrl;
265
266         char *error_string = NULL;
267
268         if (control_strings == NULL || control_strings[0] == NULL)
269                 return NULL;
270
271         for (i = 0; control_strings[i]; i++);
272
273         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
274
275         for (i = 0; control_strings[i]; i++) {
276                 if (strncmp(control_strings[i], "vlv:", 4) == 0) {
277                         struct ldb_vlv_req_control *control;
278                         const char *p;
279                         char attr[1024];
280                         char ctxid[1024];
281                         int crit, bc, ac, os, cc, ret;
282
283                         attr[0] = '\0';
284                         ctxid[0] = '\0';
285                         p = &(control_strings[i][4]);
286                         ret = sscanf(p, "%d:%d:%d:%d:%d:%1023[^$]", &crit, &bc, &ac, &os, &cc, ctxid);
287                         if (ret < 5) {
288                                 ret = sscanf(p, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit, &bc, &ac, attr, ctxid);
289                         }
290                                
291                         if ((ret < 4) || (crit < 0) || (crit > 1)) {
292                                 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
293                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):bc(n):ac(n):<os(n):cc(n)|attr(s)>[:ctxid(o)]\n");
294                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, s = string, o = b64 binary blob");
295                                 ldb_set_errstring(ldb, error_string);
296                                 talloc_free(error_string);
297                                 return NULL;
298                         }
299                         if (!(ctrl[i] = talloc(ctrl, struct ldb_control))) {
300                                 ldb_oom(ldb);
301                                 return NULL;
302                         }
303                         ctrl[i]->oid = LDB_CONTROL_VLV_REQ_OID;
304                         ctrl[i]->critical = crit;
305                         if (!(control = talloc(ctrl[i],
306                                                struct ldb_vlv_req_control))) {
307                                 ldb_oom(ldb);
308                                 return NULL;
309                         }
310                         control->beforeCount = bc;
311                         control->afterCount = ac;
312                         if (attr[0]) {
313                                 control->type = 1;
314                                 control->match.gtOrEq.value = talloc_strdup(control, attr);
315                                 control->match.gtOrEq.value_len = strlen(attr);
316                         } else {
317                                 control->type = 0;
318                                 control->match.byOffset.offset = os;
319                                 control->match.byOffset.contentCount = cc;
320                         }
321                         if (ctxid[0]) {
322                                 control->ctxid_len = ldb_base64_decode(ctxid);
323                                 control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
324                         } else {
325                                 control->ctxid_len = 0;
326                                 control->contextId = NULL;
327                         }
328                         ctrl[i]->data = control;
329
330                         continue;
331                 }
332
333                 if (strncmp(control_strings[i], "dirsync:", 8) == 0) {
334                         struct ldb_dirsync_control *control;
335                         const char *p;
336                         char cookie[1024];
337                         int crit, flags, max_attrs, ret;
338                        
339                         cookie[0] = '\0';
340                         p = &(control_strings[i][8]);
341                         ret = sscanf(p, "%d:%d:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
342
343                         if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
344                                 error_string = talloc_asprintf(mem_ctx, "invalid dirsync control syntax\n");
345                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
346                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, o = b64 binary blob");
347                                 ldb_set_errstring(ldb, error_string);
348                                 talloc_free(error_string);
349                                 return NULL;
350                         }
351
352                         /* w2k3 seems to ignore the parameter,
353                          * but w2k sends a wrong cookie when this value is to small
354                          * this would cause looping forever, while getting
355                          * the same data and same cookie forever
356                          */
357                         if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
358
359                         ctrl[i] = talloc(ctrl, struct ldb_control);
360                         ctrl[i]->oid = LDB_CONTROL_DIRSYNC_OID;
361                         ctrl[i]->critical = crit;
362                         control = talloc(ctrl[i], struct ldb_dirsync_control);
363                         control->flags = flags;
364                         control->max_attributes = max_attrs;
365                         if (*cookie) {
366                                 control->cookie_len = ldb_base64_decode(cookie);
367                                 control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
368                         } else {
369                                 control->cookie = NULL;
370                                 control->cookie_len = 0;
371                         }
372                         ctrl[i]->data = control;
373
374                         continue;
375                 }
376
377                 if (strncmp(control_strings[i], "asq:", 4) == 0) {
378                         struct ldb_asq_control *control;
379                         const char *p;
380                         char attr[256];
381                         int crit, ret;
382
383                         attr[0] = '\0';
384                         p = &(control_strings[i][4]);
385                         ret = sscanf(p, "%d:%255[^$]", &crit, attr);
386                         if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
387                                 error_string = talloc_asprintf(mem_ctx, "invalid asq control syntax\n");
388                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):attr(s)\n");
389                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
390                                 ldb_set_errstring(ldb, error_string);
391                                 talloc_free(error_string);
392                                 return NULL;
393                         }
394
395                         ctrl[i] = talloc(ctrl, struct ldb_control);
396                         if (!ctrl[i]) {
397                                 ldb_oom(ldb);
398                                 return NULL;
399                         }
400                         ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
401                         ctrl[i]->critical = crit;
402                         control = talloc(ctrl[i], struct ldb_asq_control);
403                         control->request = 1;
404                         control->source_attribute = talloc_strdup(control, attr);
405                         control->src_attr_len = strlen(attr);
406                         ctrl[i]->data = control;
407
408                         continue;
409                 }
410
411                 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
412                         struct ldb_extended_dn_control *control;
413                         const char *p;
414                         int crit, type, ret;
415
416                         p = &(control_strings[i][12]);
417                         ret = sscanf(p, "%d:%d", &crit, &type);
418                         if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
419                                 ret = sscanf(p, "%d", &crit);
420                                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
421                                         error_string = talloc_asprintf(mem_ctx, "invalid extended_dn control syntax\n");
422                                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)[:type(i)]\n");
423                                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean\n");
424                                         error_string = talloc_asprintf_append(error_string, "         i = integer\n");
425                                         error_string = talloc_asprintf_append(error_string, "   valid values are: 0 - hexadecimal representation\n");
426                                         error_string = talloc_asprintf_append(error_string, "                     1 - normal string representation");
427                                         ldb_set_errstring(ldb, error_string);
428                                         talloc_free(error_string);
429                                         return NULL;
430                                 }
431                                 control = NULL;
432                         } else {
433                                 control = talloc(ctrl, struct ldb_extended_dn_control);
434                                 control->type = type;
435                         }
436
437                         ctrl[i] = talloc(ctrl, struct ldb_control);
438                         if (!ctrl[i]) {
439                                 ldb_oom(ldb);
440                                 return NULL;
441                         }
442                         ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
443                         ctrl[i]->critical = crit;
444                         ctrl[i]->data = talloc_steal(ctrl[i], control);
445
446                         continue;
447                 }
448
449                 if (strncmp(control_strings[i], "sd_flags:", 9) == 0) {
450                         struct ldb_sd_flags_control *control;
451                         const char *p;
452                         int crit, ret;
453                         unsigned secinfo_flags;
454
455                         p = &(control_strings[i][9]);
456                         ret = sscanf(p, "%d:%u", &crit, &secinfo_flags);
457                         if ((ret != 2) || (crit < 0) || (crit > 1) || (secinfo_flags < 0) || (secinfo_flags > 0xF)) {
458                                 error_string = talloc_asprintf(mem_ctx, "invalid sd_flags control syntax\n");
459                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):secinfo_flags(n)\n");
460                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
461                                 ldb_set_errstring(ldb, error_string);
462                                 talloc_free(error_string);
463                                 return NULL;
464                         }
465
466                         ctrl[i] = talloc(ctrl, struct ldb_control);
467                         if (!ctrl[i]) {
468                                 ldb_oom(ldb);
469                                 return NULL;
470                         }
471                         ctrl[i]->oid = LDB_CONTROL_SD_FLAGS_OID;
472                         ctrl[i]->critical = crit;
473                         control = talloc(ctrl[i], struct ldb_sd_flags_control);
474                         control->secinfo_flags = secinfo_flags;
475                         ctrl[i]->data = control;
476
477                         continue;
478                 }
479
480                 if (strncmp(control_strings[i], "search_options:", 15) == 0) {
481                         struct ldb_search_options_control *control;
482                         const char *p;
483                         int crit, ret;
484                         unsigned search_options;
485
486                         p = &(control_strings[i][15]);
487                         ret = sscanf(p, "%d:%u", &crit, &search_options);
488                         if ((ret != 2) || (crit < 0) || (crit > 1) || (search_options < 0) || (search_options > 0xF)) {
489                                 error_string = talloc_asprintf(mem_ctx, "invalid search_options control syntax\n");
490                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):search_options(n)\n");
491                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
492                                 ldb_set_errstring(ldb, error_string);
493                                 talloc_free(error_string);
494                                 return NULL;
495                         }
496
497                         ctrl[i] = talloc(ctrl, struct ldb_control);
498                         if (!ctrl[i]) {
499                                 ldb_oom(ldb);
500                                 return NULL;
501                         }
502                         ctrl[i]->oid = LDB_CONTROL_SEARCH_OPTIONS_OID;
503                         ctrl[i]->critical = crit;
504                         control = talloc(ctrl[i], struct ldb_search_options_control);
505                         control->search_options = search_options;
506                         ctrl[i]->data = control;
507
508                         continue;
509                 }
510
511                 if (strncmp(control_strings[i], "bypassoperational:", 18) == 0) {
512                         const char *p;
513                         int crit, ret;
514
515                         p = &(control_strings[i][18]);
516                         ret = sscanf(p, "%d", &crit);
517                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
518                                 error_string = talloc_asprintf(mem_ctx, "invalid bypassopreational control syntax\n");
519                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
520                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
521                                 ldb_set_errstring(ldb, error_string);
522                                 talloc_free(error_string);
523                                 return NULL;
524                         }
525
526                         ctrl[i] = talloc(ctrl, struct ldb_control);
527                         if (!ctrl[i]) {
528                                 ldb_oom(ldb);
529                                 return NULL;
530                         }
531                         ctrl[i]->oid = LDB_CONTROL_BYPASS_OPERATIONAL_OID;
532                         ctrl[i]->critical = crit;
533                         ctrl[i]->data = NULL;
534
535                         continue;
536                 }
537
538                 if (strncmp(control_strings[i], "relax:", 6) == 0) {
539                         const char *p;
540                         int crit, ret;
541
542                         p = &(control_strings[i][6]);
543                         ret = sscanf(p, "%d", &crit);
544                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
545                                 error_string = talloc_asprintf(mem_ctx, "invalid relax control syntax\n");
546                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
547                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
548                                 ldb_set_errstring(ldb, error_string);
549                                 talloc_free(error_string);
550                                 return NULL;
551                         }
552
553                         ctrl[i] = talloc(ctrl, struct ldb_control);
554                         if (!ctrl[i]) {
555                                 ldb_oom(ldb);
556                                 return NULL;
557                         }
558                         ctrl[i]->oid = LDB_CONTROL_RELAX_OID;
559                         ctrl[i]->critical = crit;
560                         ctrl[i]->data = NULL;
561
562                         continue;
563                 }
564
565                 if (strncmp(control_strings[i], "recalculate_sd:", 15) == 0) {
566                         const char *p;
567                         int crit, ret;
568
569                         p = &(control_strings[i][15]);
570                         ret = sscanf(p, "%d", &crit);
571                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
572                                 error_string = talloc_asprintf(mem_ctx, "invalid recalculate_sd control syntax\n");
573                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
574                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
575                                 ldb_set_errstring(ldb, error_string);
576                                 talloc_free(error_string);
577                                 return NULL;
578                         }
579
580                         ctrl[i] = talloc(ctrl, struct ldb_control);
581                         if (!ctrl[i]) {
582                                 ldb_oom(ldb);
583                                 return NULL;
584                         }
585                         ctrl[i]->oid = LDB_CONTROL_RECALCULATE_SD_OID;
586                         ctrl[i]->critical = crit;
587                         ctrl[i]->data = NULL;
588
589                         continue;
590                 }
591
592                 if (strncmp(control_strings[i], "domain_scope:", 13) == 0) {
593                         const char *p;
594                         int crit, ret;
595
596                         p = &(control_strings[i][13]);
597                         ret = sscanf(p, "%d", &crit);
598                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
599                                 error_string = talloc_asprintf(mem_ctx, "invalid domain_scope control syntax\n");
600                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
601                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
602                                 ldb_set_errstring(ldb, error_string);
603                                 talloc_free(error_string);
604                                 return NULL;
605                         }
606
607                         ctrl[i] = talloc(ctrl, struct ldb_control);
608                         if (!ctrl[i]) {
609                                 ldb_oom(ldb);
610                                 return NULL;
611                         }
612                         ctrl[i]->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
613                         ctrl[i]->critical = crit;
614                         ctrl[i]->data = NULL;
615
616                         continue;
617                 }
618
619                 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
620                         struct ldb_paged_control *control;
621                         const char *p;
622                         int crit, size, ret;
623                        
624                         p = &(control_strings[i][14]);
625                         ret = sscanf(p, "%d:%d", &crit, &size);
626
627                         if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
628                                 error_string = talloc_asprintf(mem_ctx, "invalid paged_results control syntax\n");
629                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):size(n)\n");
630                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
631                                 ldb_set_errstring(ldb, error_string);
632                                 talloc_free(error_string);
633                                 return NULL;
634                         }
635
636                         ctrl[i] = talloc(ctrl, struct ldb_control);
637                         if (!ctrl[i]) {
638                                 ldb_oom(ldb);
639                                 return NULL;
640                         }
641                         ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
642                         ctrl[i]->critical = crit;
643                         control = talloc(ctrl[i], struct ldb_paged_control);
644                         control->size = size;
645                         control->cookie = NULL;
646                         control->cookie_len = 0;
647                         ctrl[i]->data = control;
648
649                         continue;
650                 }
651
652                 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
653                         struct ldb_server_sort_control **control;
654                         const char *p;
655                         char attr[256];
656                         char rule[128];
657                         int crit, rev, ret;
658
659                         attr[0] = '\0';
660                         rule[0] = '\0';
661                         p = &(control_strings[i][12]);
662                         ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
663                         if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
664                                 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
665                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
666                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
667                                 ldb_set_errstring(ldb, error_string);
668                                 talloc_free(error_string);
669                                 return NULL;
670                         }
671                         ctrl[i] = talloc(ctrl, struct ldb_control);
672                         if (!ctrl[i]) {
673                                 ldb_oom(ldb);
674                                 return NULL;
675                         }
676                         ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
677                         ctrl[i]->critical = crit;
678                         control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
679                         control[0] = talloc(control, struct ldb_server_sort_control);
680                         control[0]->attributeName = talloc_strdup(control, attr);
681                         if (rule[0])
682                                 control[0]->orderingRule = talloc_strdup(control, rule);
683                         else
684                                 control[0]->orderingRule = NULL;
685                         control[0]->reverse = rev;
686                         control[1] = NULL;
687                         ctrl[i]->data = control;
688
689                         continue;
690                 }
691
692                 if (strncmp(control_strings[i], "notification:", 13) == 0) {
693                         const char *p;
694                         int crit, ret;
695
696                         p = &(control_strings[i][13]);
697                         ret = sscanf(p, "%d", &crit);
698                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
699                                 error_string = talloc_asprintf(mem_ctx, "invalid notification control syntax\n");
700                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
701                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
702                                 ldb_set_errstring(ldb, error_string);
703                                 talloc_free(error_string);
704                                 return NULL;
705                         }
706
707                         ctrl[i] = talloc(ctrl, struct ldb_control);
708                         if (!ctrl[i]) {
709                                 ldb_oom(ldb);
710                                 return NULL;
711                         }
712                         ctrl[i]->oid = LDB_CONTROL_NOTIFICATION_OID;
713                         ctrl[i]->critical = crit;
714                         ctrl[i]->data = NULL;
715
716                         continue;
717                 }
718
719                 if (strncmp(control_strings[i], "tree_delete:", 12) == 0) {
720                         const char *p;
721                         int crit, ret;
722
723                         p = &(control_strings[i][12]);
724                         ret = sscanf(p, "%d", &crit);
725                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
726                                 error_string = talloc_asprintf(mem_ctx, "invalid tree_delete control syntax\n");
727                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
728                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
729                                 ldb_set_errstring(ldb, error_string);
730                                 talloc_free(error_string);
731                                 return NULL;
732                         }
733
734                         ctrl[i] = talloc(ctrl, struct ldb_control);
735                         if (!ctrl[i]) {
736                                 ldb_oom(ldb);
737                                 return NULL;
738                         }
739                         ctrl[i]->oid = LDB_CONTROL_TREE_DELETE_OID;
740                         ctrl[i]->critical = crit;
741                         ctrl[i]->data = NULL;
742
743                         continue;
744                 }
745
746                 if (strncmp(control_strings[i], "show_deleted:", 13) == 0) {
747                         const char *p;
748                         int crit, ret;
749
750                         p = &(control_strings[i][13]);
751                         ret = sscanf(p, "%d", &crit);
752                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
753                                 error_string = talloc_asprintf(mem_ctx, "invalid show_deleted control syntax\n");
754                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
755                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
756                                 ldb_set_errstring(ldb, error_string);
757                                 talloc_free(error_string);
758                                 return NULL;
759                         }
760
761                         ctrl[i] = talloc(ctrl, struct ldb_control);
762                         if (!ctrl[i]) {
763                                 ldb_oom(ldb);
764                                 return NULL;
765                         }
766                         ctrl[i]->oid = LDB_CONTROL_SHOW_DELETED_OID;
767                         ctrl[i]->critical = crit;
768                         ctrl[i]->data = NULL;
769
770                         continue;
771                 }
772
773                 if (strncmp(control_strings[i], "show_deactivated_link:", 22) == 0) {
774                         const char *p;
775                         int crit, ret;
776
777                         p = &(control_strings[i][22]);
778                         ret = sscanf(p, "%d", &crit);
779                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
780                                 error_string = talloc_asprintf(mem_ctx, "invalid show_deactivated_link control syntax\n");
781                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
782                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
783                                 ldb_set_errstring(ldb, error_string);
784                                 talloc_free(error_string);
785                                 return NULL;
786                         }
787
788                         ctrl[i] = talloc(ctrl, struct ldb_control);
789                         if (!ctrl[i]) {
790                                 ldb_oom(ldb);
791                                 return NULL;
792                         }
793                         ctrl[i]->oid = LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID;
794                         ctrl[i]->critical = crit;
795                         ctrl[i]->data = NULL;
796
797                         continue;
798                 }
799
800                 if (strncmp(control_strings[i], "show_recycled:", 14) == 0) {
801                         const char *p;
802                         int crit, ret;
803
804                         p = &(control_strings[i][14]);
805                         ret = sscanf(p, "%d", &crit);
806                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
807                                 error_string = talloc_asprintf(mem_ctx, "invalid show_recycled control syntax\n");
808                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
809                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
810                                 ldb_set_errstring(ldb, error_string);
811                                 talloc_free(error_string);
812                                 return NULL;
813                         }
814
815                         ctrl[i] = talloc(ctrl, struct ldb_control);
816                         if (!ctrl[i]) {
817                                 ldb_oom(ldb);
818                                 return NULL;
819                         }
820                         ctrl[i]->oid = LDB_CONTROL_SHOW_RECYCLED_OID;
821                         ctrl[i]->critical = crit;
822                         ctrl[i]->data = NULL;
823
824                         continue;
825                 }
826
827                 if (strncmp(control_strings[i], "permissive_modify:", 18) == 0) {
828                         const char *p;
829                         int crit, ret;
830
831                         p = &(control_strings[i][18]);
832                         ret = sscanf(p, "%d", &crit);
833                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
834                                 error_string = talloc_asprintf(mem_ctx, "invalid permissive_modify control syntax\n");
835                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
836                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
837                                 ldb_set_errstring(ldb, error_string);
838                                 talloc_free(error_string);
839                                 return NULL;
840                         }
841
842                         ctrl[i] = talloc(ctrl, struct ldb_control);
843                         if (!ctrl[i]) {
844                                 ldb_oom(ldb);
845                                 return NULL;
846                         }
847                         ctrl[i]->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
848                         ctrl[i]->critical = crit;
849                         ctrl[i]->data = NULL;
850
851                         continue;
852                 }
853
854                 if (strncmp(control_strings[i], "reveal_internals:", 17) == 0) {
855                         const char *p;
856                         int crit, ret;
857
858                         p = &(control_strings[i][17]);
859                         ret = sscanf(p, "%d", &crit);
860                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
861                                 error_string = talloc_asprintf(mem_ctx, "invalid reveal_internals control syntax\n");
862                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
863                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
864                                 ldb_set_errstring(ldb, error_string);
865                                 talloc_free(error_string);
866                                 return NULL;
867                         }
868
869                         ctrl[i] = talloc(ctrl, struct ldb_control);
870                         if (!ctrl[i]) {
871                                 ldb_oom(ldb);
872                                 return NULL;
873                         }
874                         ctrl[i]->oid = LDB_CONTROL_REVEAL_INTERNALS;
875                         ctrl[i]->critical = crit;
876                         ctrl[i]->data = NULL;
877
878                         continue;
879                 }
880
881                 if (strncmp(control_strings[i], "local_oid:", 10) == 0) {
882                         const char *p;
883                         int crit = 0, ret = 0;
884                         char oid[256];
885
886                         oid[0] = '\0';
887                         p = &(control_strings[i][10]);
888                         ret = sscanf(p, "%64[^:]:%d", oid, &crit);
889
890                         if ((ret != 2) || strlen(oid) == 0 || (crit < 0) || (crit > 1)) {
891                                 error_string = talloc_asprintf(mem_ctx, "invalid local_oid control syntax\n");
892                                 error_string = talloc_asprintf_append(error_string, " syntax: oid(s):crit(b)\n");
893                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
894                                 ldb_set_errstring(ldb, error_string);
895                                 talloc_free(error_string);
896                                 return NULL;
897                         }
898
899                         ctrl[i] = talloc(ctrl, struct ldb_control);
900                         if (!ctrl[i]) {
901                                 ldb_oom(ldb);
902                                 return NULL;
903                         }
904                         ctrl[i]->oid = talloc_strdup(ctrl[i], oid);
905                         if (!ctrl[i]->oid) {
906                                 ldb_oom(ldb);
907                                 return NULL;
908                         }
909                         ctrl[i]->critical = crit;
910                         ctrl[i]->data = NULL;
911
912                         continue;
913                 }
914
915                 if (strncmp(control_strings[i], "rodc_join:", 10) == 0) {
916                         const char *p;
917                         int crit, ret;
918
919                         p = &(control_strings[i][10]);
920                         ret = sscanf(p, "%d", &crit);
921                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
922                                 error_string = talloc_asprintf(mem_ctx, "invalid rodc_join control syntax\n");
923                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
924                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
925                                 ldb_set_errstring(ldb, error_string);
926                                 talloc_free(error_string);
927                                 return NULL;
928                         }
929
930                         ctrl[i] = talloc(ctrl, struct ldb_control);
931                         if (!ctrl[i]) {
932                                 ldb_oom(ldb);
933                                 return NULL;
934                         }
935                         ctrl[i]->oid = LDB_CONTROL_RODC_DCPROMO_OID;
936                         ctrl[i]->critical = crit;
937                         ctrl[i]->data = NULL;
938
939                         continue;
940                 }
941
942                 /* no controls matched, throw an error */
943                 ldb_asprintf_errstring(ldb, "Invalid control name: '%s'", control_strings[i]);
944                 return NULL;
945         }
946
947         ctrl[i] = NULL;
948
949         return ctrl;
950 }
951
952