LDB:common - Change counters to "unsigned" where appropriate
[ira/wip.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 (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 (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 (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 (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 /* Parse controls from the format used on the command line and in ejs */
238
239 struct ldb_control **ldb_parse_control_strings(struct ldb_context *ldb, void *mem_ctx, const char **control_strings)
240 {
241         unsigned int i;
242         struct ldb_control **ctrl;
243
244         char *error_string = NULL;
245
246         if (control_strings == NULL || control_strings[0] == NULL)
247                 return NULL;
248
249         for (i = 0; control_strings[i]; i++);
250
251         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
252
253         for (i = 0; control_strings[i]; i++) {
254                 if (strncmp(control_strings[i], "vlv:", 4) == 0) {
255                         struct ldb_vlv_req_control *control;
256                         const char *p;
257                         char attr[1024];
258                         char ctxid[1024];
259                         int crit, bc, ac, os, cc, ret;
260
261                         attr[0] = '\0';
262                         ctxid[0] = '\0';
263                         p = &(control_strings[i][4]);
264                         ret = sscanf(p, "%d:%d:%d:%d:%d:%1023[^$]", &crit, &bc, &ac, &os, &cc, ctxid);
265                         if (ret < 5) {
266                                 ret = sscanf(p, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit, &bc, &ac, attr, ctxid);
267                         }
268                                
269                         if ((ret < 4) || (crit < 0) || (crit > 1)) {
270                                 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
271                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):bc(n):ac(n):<os(n):cc(n)|attr(s)>[:ctxid(o)]\n");
272                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, s = string, o = b64 binary blob");
273                                 ldb_set_errstring(ldb, error_string);
274                                 talloc_free(error_string);
275                                 return NULL;
276                         }
277                         if (!(ctrl[i] = talloc(ctrl, struct ldb_control))) {
278                                 ldb_oom(ldb);
279                                 return NULL;
280                         }
281                         ctrl[i]->oid = LDB_CONTROL_VLV_REQ_OID;
282                         ctrl[i]->critical = crit;
283                         if (!(control = talloc(ctrl[i],
284                                                struct ldb_vlv_req_control))) {
285                                 ldb_oom(ldb);
286                                 return NULL;
287                         }
288                         control->beforeCount = bc;
289                         control->afterCount = ac;
290                         if (attr[0]) {
291                                 control->type = 1;
292                                 control->match.gtOrEq.value = talloc_strdup(control, attr);
293                                 control->match.gtOrEq.value_len = strlen(attr);
294                         } else {
295                                 control->type = 0;
296                                 control->match.byOffset.offset = os;
297                                 control->match.byOffset.contentCount = cc;
298                         }
299                         if (ctxid[0]) {
300                                 control->ctxid_len = ldb_base64_decode(ctxid);
301                                 control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
302                         } else {
303                                 control->ctxid_len = 0;
304                                 control->contextId = NULL;
305                         }
306                         ctrl[i]->data = control;
307
308                         continue;
309                 }
310
311                 if (strncmp(control_strings[i], "dirsync:", 8) == 0) {
312                         struct ldb_dirsync_control *control;
313                         const char *p;
314                         char cookie[1024];
315                         int crit, flags, max_attrs, ret;
316                        
317                         cookie[0] = '\0';
318                         p = &(control_strings[i][8]);
319                         ret = sscanf(p, "%d:%d:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
320
321                         if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
322                                 error_string = talloc_asprintf(mem_ctx, "invalid dirsync control syntax\n");
323                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
324                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number, o = b64 binary blob");
325                                 ldb_set_errstring(ldb, error_string);
326                                 talloc_free(error_string);
327                                 return NULL;
328                         }
329
330                         /* w2k3 seems to ignore the parameter,
331                          * but w2k sends a wrong cookie when this value is to small
332                          * this would cause looping forever, while getting
333                          * the same data and same cookie forever
334                          */
335                         if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
336
337                         ctrl[i] = talloc(ctrl, struct ldb_control);
338                         ctrl[i]->oid = LDB_CONTROL_DIRSYNC_OID;
339                         ctrl[i]->critical = crit;
340                         control = talloc(ctrl[i], struct ldb_dirsync_control);
341                         control->flags = flags;
342                         control->max_attributes = max_attrs;
343                         if (*cookie) {
344                                 control->cookie_len = ldb_base64_decode(cookie);
345                                 control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
346                         } else {
347                                 control->cookie = NULL;
348                                 control->cookie_len = 0;
349                         }
350                         ctrl[i]->data = control;
351
352                         continue;
353                 }
354
355                 if (strncmp(control_strings[i], "asq:", 4) == 0) {
356                         struct ldb_asq_control *control;
357                         const char *p;
358                         char attr[256];
359                         int crit, ret;
360
361                         attr[0] = '\0';
362                         p = &(control_strings[i][4]);
363                         ret = sscanf(p, "%d:%255[^$]", &crit, attr);
364                         if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
365                                 error_string = talloc_asprintf(mem_ctx, "invalid asq control syntax\n");
366                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):attr(s)\n");
367                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
368                                 ldb_set_errstring(ldb, error_string);
369                                 talloc_free(error_string);
370                                 return NULL;
371                         }
372
373                         ctrl[i] = talloc(ctrl, struct ldb_control);
374                         if (!ctrl[i]) {
375                                 ldb_oom(ldb);
376                                 return NULL;
377                         }
378                         ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
379                         ctrl[i]->critical = crit;
380                         control = talloc(ctrl[i], struct ldb_asq_control);
381                         control->request = 1;
382                         control->source_attribute = talloc_strdup(control, attr);
383                         control->src_attr_len = strlen(attr);
384                         ctrl[i]->data = control;
385
386                         continue;
387                 }
388
389                 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
390                         struct ldb_extended_dn_control *control;
391                         const char *p;
392                         int crit, type, ret;
393
394                         p = &(control_strings[i][12]);
395                         ret = sscanf(p, "%d:%d", &crit, &type);
396                         if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
397                                 ret = sscanf(p, "%d", &crit);
398                                 if ((ret != 1) || (crit < 0) || (crit > 1)) {
399                                         error_string = talloc_asprintf(mem_ctx, "invalid extended_dn control syntax\n");
400                                         error_string = talloc_asprintf_append(error_string, " syntax: crit(b)[:type(i)]\n");
401                                         error_string = talloc_asprintf_append(error_string, "   note: b = boolean\n");
402                                         error_string = talloc_asprintf_append(error_string, "         i = integer\n");
403                                         error_string = talloc_asprintf_append(error_string, "   valid values are: 0 - hexadecimal representation\n");
404                                         error_string = talloc_asprintf_append(error_string, "                     1 - normal string representation");
405                                         ldb_set_errstring(ldb, error_string);
406                                         talloc_free(error_string);
407                                         return NULL;
408                                 }
409                                 control = NULL;
410                         } else {
411                                 control = talloc(ctrl, struct ldb_extended_dn_control);
412                                 control->type = type;
413                         }
414
415                         ctrl[i] = talloc(ctrl, struct ldb_control);
416                         if (!ctrl[i]) {
417                                 ldb_oom(ldb);
418                                 return NULL;
419                         }
420                         ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
421                         ctrl[i]->critical = crit;
422                         ctrl[i]->data = talloc_steal(ctrl[i], control);
423
424                         continue;
425                 }
426
427                 if (strncmp(control_strings[i], "sd_flags:", 9) == 0) {
428                         struct ldb_sd_flags_control *control;
429                         const char *p;
430                         int crit, ret;
431                         unsigned secinfo_flags;
432
433                         p = &(control_strings[i][9]);
434                         ret = sscanf(p, "%d:%u", &crit, &secinfo_flags);
435                         if ((ret != 2) || (crit < 0) || (crit > 1) || (secinfo_flags < 0) || (secinfo_flags > 0xF)) {
436                                 error_string = talloc_asprintf(mem_ctx, "invalid sd_flags control syntax\n");
437                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):secinfo_flags(n)\n");
438                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
439                                 ldb_set_errstring(ldb, error_string);
440                                 talloc_free(error_string);
441                                 return NULL;
442                         }
443
444                         ctrl[i] = talloc(ctrl, struct ldb_control);
445                         if (!ctrl[i]) {
446                                 ldb_oom(ldb);
447                                 return NULL;
448                         }
449                         ctrl[i]->oid = LDB_CONTROL_SD_FLAGS_OID;
450                         ctrl[i]->critical = crit;
451                         control = talloc(ctrl[i], struct ldb_sd_flags_control);
452                         control->secinfo_flags = secinfo_flags;
453                         ctrl[i]->data = control;
454
455                         continue;
456                 }
457
458                 if (strncmp(control_strings[i], "search_options:", 15) == 0) {
459                         struct ldb_search_options_control *control;
460                         const char *p;
461                         int crit, ret;
462                         unsigned search_options;
463
464                         p = &(control_strings[i][15]);
465                         ret = sscanf(p, "%d:%u", &crit, &search_options);
466                         if ((ret != 2) || (crit < 0) || (crit > 1) || (search_options < 0) || (search_options > 0xF)) {
467                                 error_string = talloc_asprintf(mem_ctx, "invalid search_options control syntax\n");
468                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):search_options(n)\n");
469                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
470                                 ldb_set_errstring(ldb, error_string);
471                                 talloc_free(error_string);
472                                 return NULL;
473                         }
474
475                         ctrl[i] = talloc(ctrl, struct ldb_control);
476                         if (!ctrl[i]) {
477                                 ldb_oom(ldb);
478                                 return NULL;
479                         }
480                         ctrl[i]->oid = LDB_CONTROL_SEARCH_OPTIONS_OID;
481                         ctrl[i]->critical = crit;
482                         control = talloc(ctrl[i], struct ldb_search_options_control);
483                         control->search_options = search_options;
484                         ctrl[i]->data = control;
485
486                         continue;
487                 }
488
489                 if (strncmp(control_strings[i], "relax:", 6) == 0) {
490                         const char *p;
491                         int crit, ret;
492
493                         p = &(control_strings[i][6]);
494                         ret = sscanf(p, "%d", &crit);
495                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
496                                 error_string = talloc_asprintf(mem_ctx, "invalid relax control syntax\n");
497                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
498                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
499                                 ldb_set_errstring(ldb, error_string);
500                                 talloc_free(error_string);
501                                 return NULL;
502                         }
503
504                         ctrl[i] = talloc(ctrl, struct ldb_control);
505                         if (!ctrl[i]) {
506                                 ldb_oom(ldb);
507                                 return NULL;
508                         }
509                         ctrl[i]->oid = LDB_CONTROL_RELAX_OID;
510                         ctrl[i]->critical = crit;
511                         ctrl[i]->data = NULL;
512
513                         continue;
514                 }
515
516                 if (strncmp(control_strings[i], "recalculate_sd:", 15) == 0) {
517                         const char *p;
518                         int crit, ret;
519
520                         p = &(control_strings[i][15]);
521                         ret = sscanf(p, "%d", &crit);
522                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
523                                 error_string = talloc_asprintf(mem_ctx, "invalid recalculate_sd control syntax\n");
524                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
525                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
526                                 ldb_set_errstring(ldb, error_string);
527                                 talloc_free(error_string);
528                                 return NULL;
529                         }
530
531                         ctrl[i] = talloc(ctrl, struct ldb_control);
532                         if (!ctrl[i]) {
533                                 ldb_oom(ldb);
534                                 return NULL;
535                         }
536                         ctrl[i]->oid = LDB_CONTROL_RECALCULATE_SD_OID;
537                         ctrl[i]->critical = crit;
538                         ctrl[i]->data = NULL;
539
540                         continue;
541                 }
542
543                 if (strncmp(control_strings[i], "domain_scope:", 13) == 0) {
544                         const char *p;
545                         int crit, ret;
546
547                         p = &(control_strings[i][13]);
548                         ret = sscanf(p, "%d", &crit);
549                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
550                                 error_string = talloc_asprintf(mem_ctx, "invalid domain_scope control syntax\n");
551                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
552                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
553                                 ldb_set_errstring(ldb, error_string);
554                                 talloc_free(error_string);
555                                 return NULL;
556                         }
557
558                         ctrl[i] = talloc(ctrl, struct ldb_control);
559                         if (!ctrl[i]) {
560                                 ldb_oom(ldb);
561                                 return NULL;
562                         }
563                         ctrl[i]->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
564                         ctrl[i]->critical = crit;
565                         ctrl[i]->data = NULL;
566
567                         continue;
568                 }
569
570                 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
571                         struct ldb_paged_control *control;
572                         const char *p;
573                         int crit, size, ret;
574                        
575                         p = &(control_strings[i][14]);
576                         ret = sscanf(p, "%d:%d", &crit, &size);
577
578                         if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
579                                 error_string = talloc_asprintf(mem_ctx, "invalid paged_results control syntax\n");
580                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):size(n)\n");
581                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, n = number");
582                                 ldb_set_errstring(ldb, error_string);
583                                 talloc_free(error_string);
584                                 return NULL;
585                         }
586
587                         ctrl[i] = talloc(ctrl, struct ldb_control);
588                         if (!ctrl[i]) {
589                                 ldb_oom(ldb);
590                                 return NULL;
591                         }
592                         ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
593                         ctrl[i]->critical = crit;
594                         control = talloc(ctrl[i], struct ldb_paged_control);
595                         control->size = size;
596                         control->cookie = NULL;
597                         control->cookie_len = 0;
598                         ctrl[i]->data = control;
599
600                         continue;
601                 }
602
603                 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
604                         struct ldb_server_sort_control **control;
605                         const char *p;
606                         char attr[256];
607                         char rule[128];
608                         int crit, rev, ret;
609
610                         attr[0] = '\0';
611                         rule[0] = '\0';
612                         p = &(control_strings[i][12]);
613                         ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
614                         if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
615                                 error_string = talloc_asprintf(mem_ctx, "invalid server_sort control syntax\n");
616                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
617                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean, s = string");
618                                 ldb_set_errstring(ldb, error_string);
619                                 talloc_free(error_string);
620                                 return NULL;
621                         }
622                         ctrl[i] = talloc(ctrl, struct ldb_control);
623                         if (!ctrl[i]) {
624                                 ldb_oom(ldb);
625                                 return NULL;
626                         }
627                         ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
628                         ctrl[i]->critical = crit;
629                         control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
630                         control[0] = talloc(control, struct ldb_server_sort_control);
631                         control[0]->attributeName = talloc_strdup(control, attr);
632                         if (rule[0])
633                                 control[0]->orderingRule = talloc_strdup(control, rule);
634                         else
635                                 control[0]->orderingRule = NULL;
636                         control[0]->reverse = rev;
637                         control[1] = NULL;
638                         ctrl[i]->data = control;
639
640                         continue;
641                 }
642
643                 if (strncmp(control_strings[i], "notification:", 13) == 0) {
644                         const char *p;
645                         int crit, ret;
646
647                         p = &(control_strings[i][13]);
648                         ret = sscanf(p, "%d", &crit);
649                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
650                                 error_string = talloc_asprintf(mem_ctx, "invalid notification control syntax\n");
651                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
652                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
653                                 ldb_set_errstring(ldb, error_string);
654                                 talloc_free(error_string);
655                                 return NULL;
656                         }
657
658                         ctrl[i] = talloc(ctrl, struct ldb_control);
659                         if (!ctrl[i]) {
660                                 ldb_oom(ldb);
661                                 return NULL;
662                         }
663                         ctrl[i]->oid = LDB_CONTROL_NOTIFICATION_OID;
664                         ctrl[i]->critical = crit;
665                         ctrl[i]->data = NULL;
666
667                         continue;
668                 }
669
670                 if (strncmp(control_strings[i], "show_deleted:", 13) == 0) {
671                         const char *p;
672                         int crit, ret;
673
674                         p = &(control_strings[i][13]);
675                         ret = sscanf(p, "%d", &crit);
676                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
677                                 error_string = talloc_asprintf(mem_ctx, "invalid show_deleted control syntax\n");
678                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
679                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
680                                 ldb_set_errstring(ldb, error_string);
681                                 talloc_free(error_string);
682                                 return NULL;
683                         }
684
685                         ctrl[i] = talloc(ctrl, struct ldb_control);
686                         if (!ctrl[i]) {
687                                 ldb_oom(ldb);
688                                 return NULL;
689                         }
690                         ctrl[i]->oid = LDB_CONTROL_SHOW_DELETED_OID;
691                         ctrl[i]->critical = crit;
692                         ctrl[i]->data = NULL;
693
694                         continue;
695                 }
696
697                 if (strncmp(control_strings[i], "show_deactivated_link:", 22) == 0) {
698                         const char *p;
699                         int crit, ret;
700
701                         p = &(control_strings[i][22]);
702                         ret = sscanf(p, "%d", &crit);
703                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
704                                 error_string = talloc_asprintf(mem_ctx, "invalid show_deactivated_link control syntax\n");
705                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
706                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
707                                 ldb_set_errstring(ldb, error_string);
708                                 talloc_free(error_string);
709                                 return NULL;
710                         }
711
712                         ctrl[i] = talloc(ctrl, struct ldb_control);
713                         if (!ctrl[i]) {
714                                 ldb_oom(ldb);
715                                 return NULL;
716                         }
717                         ctrl[i]->oid = LDB_CONTROL_SHOW_DEACTIVATED_LINK_OID;
718                         ctrl[i]->critical = crit;
719                         ctrl[i]->data = NULL;
720
721                         continue;
722                 }
723
724                 if (strncmp(control_strings[i], "show_recycled:", 14) == 0) {
725                         const char *p;
726                         int crit, ret;
727
728                         p = &(control_strings[i][14]);
729                         ret = sscanf(p, "%d", &crit);
730                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
731                                 error_string = talloc_asprintf(mem_ctx, "invalid show_recycled control syntax\n");
732                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
733                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
734                                 ldb_set_errstring(ldb, error_string);
735                                 talloc_free(error_string);
736                                 return NULL;
737                         }
738
739                         ctrl[i] = talloc(ctrl, struct ldb_control);
740                         if (!ctrl[i]) {
741                                 ldb_oom(ldb);
742                                 return NULL;
743                         }
744                         ctrl[i]->oid = LDB_CONTROL_SHOW_RECYCLED_OID;
745                         ctrl[i]->critical = crit;
746                         ctrl[i]->data = NULL;
747
748                         continue;
749                 }
750
751                 if (strncmp(control_strings[i], "permissive_modify:", 18) == 0) {
752                         const char *p;
753                         int crit, ret;
754
755                         p = &(control_strings[i][18]);
756                         ret = sscanf(p, "%d", &crit);
757                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
758                                 error_string = talloc_asprintf(mem_ctx, "invalid permissive_modify control syntax\n");
759                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
760                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
761                                 ldb_set_errstring(ldb, error_string);
762                                 talloc_free(error_string);
763                                 return NULL;
764                         }
765
766                         ctrl[i] = talloc(ctrl, struct ldb_control);
767                         if (!ctrl[i]) {
768                                 ldb_oom(ldb);
769                                 return NULL;
770                         }
771                         ctrl[i]->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
772                         ctrl[i]->critical = crit;
773                         ctrl[i]->data = NULL;
774
775                         continue;
776                 }
777
778                 if (strncmp(control_strings[i], "reveal_internals:", 17) == 0) {
779                         const char *p;
780                         int crit, ret;
781
782                         p = &(control_strings[i][17]);
783                         ret = sscanf(p, "%d", &crit);
784                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
785                                 error_string = talloc_asprintf(mem_ctx, "invalid reveal_internals control syntax\n");
786                                 error_string = talloc_asprintf_append(error_string, " syntax: crit(b)\n");
787                                 error_string = talloc_asprintf_append(error_string, "   note: b = boolean");
788                                 ldb_set_errstring(ldb, error_string);
789                                 talloc_free(error_string);
790                                 return NULL;
791                         }
792
793                         ctrl[i] = talloc(ctrl, struct ldb_control);
794                         if (!ctrl[i]) {
795                                 ldb_oom(ldb);
796                                 return NULL;
797                         }
798                         ctrl[i]->oid = LDB_CONTROL_REVEAL_INTERNALS;
799                         ctrl[i]->critical = crit;
800                         ctrl[i]->data = NULL;
801
802                         continue;
803                 }
804
805                 /* no controls matched, throw an error */
806                 ldb_asprintf_errstring(ldb, "Invalid control name: '%s'", control_strings[i]);
807                 return NULL;
808         }
809
810         ctrl[i] = NULL;
811
812         return ctrl;
813 }
814
815