r19896: Fix klokwork id 2279
[kai/samba.git] / source3 / lib / ldb / tools / cmdline.c
1 /* 
2    ldb database library - command line handling for ldb tools
3
4    Copyright (C) Andrew Tridgell  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 2 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, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 #include "includes.h"
26 #include "ldb/include/includes.h"
27 #include "ldb/tools/cmdline.h"
28
29 #if (_SAMBA_BUILD_ >= 4)
30 #include "lib/cmdline/popt_common.h"
31 #include "lib/ldb/samba/ldif_handlers.h"
32 #include "auth/gensec/gensec.h"
33 #include "auth/auth.h"
34 #include "db_wrap.h"
35 #endif
36
37
38
39 /*
40   process command line options
41 */
42 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
43                                         void (*usage)(void))
44 {
45         static struct ldb_cmdline options; /* needs to be static for older compilers */
46         struct ldb_cmdline *ret=NULL;
47         poptContext pc;
48 #if (_SAMBA_BUILD_ >= 4)
49         int r;
50 #endif
51         int num_options = 0;
52         int opt;
53         int flags = 0;
54
55         struct poptOption popt_options[] = {
56                 POPT_AUTOHELP
57                 { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
58                 { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
59                 { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
60                 { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
61                 { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
62                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
63                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
64                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
65                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
66                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
67                 { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
68                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
69                 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
70                 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
71                 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
72                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
73                 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
74 #if (_SAMBA_BUILD_ >= 4)
75                 POPT_COMMON_SAMBA
76                 POPT_COMMON_CREDENTIALS
77                 POPT_COMMON_VERSION
78 #endif
79                 { NULL }
80         };
81
82         ldb_global_init();
83
84 #if (_SAMBA_BUILD_ >= 4)
85         r = ldb_register_samba_handlers(ldb);
86         if (r != 0) {
87                 goto failed;
88         }
89
90 #endif
91
92         ret = talloc_zero(ldb, struct ldb_cmdline);
93         if (ret == NULL) {
94                 ldb_oom(ldb);
95                 goto failed;
96         }
97
98         options = *ret;
99         
100         /* pull in URL */
101         options.url = getenv("LDB_URL");
102
103         /* and editor (used by ldbedit) */
104         options.editor = getenv("VISUAL");
105         if (!options.editor) {
106                 options.editor = getenv("EDITOR");
107         }
108         if (!options.editor) {
109                 options.editor = "vi";
110         }
111
112         options.scope = LDB_SCOPE_DEFAULT;
113
114         pc = poptGetContext(argv[0], argc, argv, popt_options, 
115                             POPT_CONTEXT_KEEP_FIRST);
116
117         while((opt = poptGetNextOpt(pc)) != -1) {
118                 switch (opt) {
119                 case 's': {
120                         const char *arg = poptGetOptArg(pc);
121                         if (strcmp(arg, "base") == 0) {
122                                 options.scope = LDB_SCOPE_BASE;
123                         } else if (strcmp(arg, "sub") == 0) {
124                                 options.scope = LDB_SCOPE_SUBTREE;
125                         } else if (strcmp(arg, "one") == 0) {
126                                 options.scope = LDB_SCOPE_ONELEVEL;
127                         } else {
128                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
129                                 goto failed;
130                         }
131                         break;
132                 }
133
134                 case 'v':
135                         options.verbose++;
136                         break;
137
138                 case 'o':
139                         options.options = talloc_realloc(ret, options.options, 
140                                                          const char *, num_options+3);
141                         if (options.options == NULL) {
142                                 ldb_oom(ldb);
143                                 goto failed;
144                         }
145                         options.options[num_options] = poptGetOptArg(pc);
146                         options.options[num_options+1] = NULL;
147                         num_options++;
148                         break;
149
150                 case 'c': {
151                         const char *cs = poptGetOptArg(pc);
152                         const char *p, *q;
153                         int cc;
154
155                         for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
156
157                         options.controls = talloc_array(ret, char *, cc + 1);
158                         if (options.controls == NULL) {
159                                 ldb_oom(ldb);
160                                 goto failed;
161                         }
162                         for (p = cs, cc = 0; p != NULL; cc++) {
163                                 const char *t;
164
165                                 t = strchr(p, ',');
166                                 if (t == NULL) {
167                                         options.controls[cc] = talloc_strdup(options.controls, p);
168                                         p = NULL;
169                                 } else {
170                                         options.controls[cc] = talloc_strndup(options.controls, p, t-p);
171                                         p = t + 1;
172                                 }
173                         }
174                         options.controls[cc] = NULL;
175
176                         break;    
177                 }
178                 default:
179                         fprintf(stderr, "Invalid option %s: %s\n", 
180                                 poptBadOption(pc, 0), poptStrerror(opt));
181                         if (usage) usage();
182                         goto failed;
183                 }
184         }
185
186         /* setup the remaining options for the main program to use */
187         options.argv = poptGetArgs(pc);
188         if (options.argv) {
189                 options.argv++;
190                 while (options.argv[options.argc]) options.argc++;
191         }
192
193         *ret = options;
194
195         /* all utils need some option */
196         if (ret->url == NULL) {
197                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
198                 if (usage) usage();
199                 goto failed;
200         }
201
202         if (strcmp(ret->url, "NONE") == 0) {
203                 return ret;
204         }
205
206         if (options.nosync) {
207                 flags |= LDB_FLG_NOSYNC;
208         }
209
210 #if (_SAMBA_BUILD_ >= 4)
211         /* Must be after we have processed command line options */
212         gensec_init(); 
213         
214         if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
215                 goto failed;
216         }
217         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
218                 goto failed;
219         }
220         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
221 #endif
222
223         /* now connect to the ldb */
224         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
225                 fprintf(stderr, "Failed to connect to %s - %s\n", 
226                         ret->url, ldb_errstring(ldb));
227                 goto failed;
228         }
229
230         return ret;
231
232 failed:
233         talloc_free(ret);
234         exit(1);
235         return NULL;
236 }
237
238 struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
239 {
240         int i;
241         struct ldb_control **ctrl;
242
243         if (control_strings == NULL || control_strings[0] == NULL)
244                 return NULL;
245
246         for (i = 0; control_strings[i]; i++);
247
248         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
249
250         for (i = 0; control_strings[i]; i++) {
251                 if (strncmp(control_strings[i], "vlv:", 4) == 0) {
252                         struct ldb_vlv_req_control *control;
253                         const char *p;
254                         char attr[1024];
255                         char ctxid[1024];
256                         int crit, bc, ac, os, cc, ret;
257
258                         attr[0] = '\0';
259                         ctxid[0] = '\0';
260                         p = &(control_strings[i][4]);
261                         ret = sscanf(p, "%d:%d:%d:%d:%d:%1023[^$]", &crit, &bc, &ac, &os, &cc, ctxid);
262                         if (ret < 5) {
263                                 ret = sscanf(p, "%d:%d:%d:%1023[^:]:%1023[^$]", &crit, &bc, &ac, attr, ctxid);
264                         }
265                                
266                         if ((ret < 4) || (crit < 0) || (crit > 1)) {
267                                 fprintf(stderr, "invalid server_sort control syntax\n");
268                                 fprintf(stderr, " syntax: crit(b):bc(n):ac(n):<os(n):cc(n)|attr(s)>[:ctxid(o)]\n");
269                                 fprintf(stderr, "   note: b = boolean, n = number, s = string, o = b64 binary blob\n");
270                                 return NULL;
271                         }
272                         if (!(ctrl[i] = talloc(ctrl, struct ldb_control))) {
273                                 fprintf(stderr, "talloc failed\n");
274                                 return NULL;
275                         }
276                         ctrl[i]->oid = LDB_CONTROL_VLV_REQ_OID;
277                         ctrl[i]->critical = crit;
278                         if (!(control = talloc(ctrl[i],
279                                                struct ldb_vlv_req_control))) {
280                                 fprintf(stderr, "talloc failed\n");
281                                 return NULL;
282                         }
283                         control->beforeCount = bc;
284                         control->afterCount = ac;
285                         if (attr[0]) {
286                                 control->type = 1;
287                                 control->match.gtOrEq.value = talloc_strdup(control, attr);
288                                 control->match.gtOrEq.value_len = strlen(attr);
289                         } else {
290                                 control->type = 0;
291                                 control->match.byOffset.offset = os;
292                                 control->match.byOffset.contentCount = cc;
293                         }
294                         if (ctxid[0]) {
295                                 control->ctxid_len = ldb_base64_decode(ctxid);
296                                 control->contextId = (char *)talloc_memdup(control, ctxid, control->ctxid_len);
297                         } else {
298                                 control->ctxid_len = 0;
299                                 control->contextId = NULL;
300                         }
301                         ctrl[i]->data = control;
302
303                         continue;
304                 }
305
306                 if (strncmp(control_strings[i], "dirsync:", 8) == 0) {
307                         struct ldb_dirsync_control *control;
308                         const char *p;
309                         char cookie[1024];
310                         int crit, flags, max_attrs, ret;
311                        
312                         cookie[0] = '\0';
313                         p = &(control_strings[i][8]);
314                         ret = sscanf(p, "%d:%d:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
315
316                         if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
317                                 fprintf(stderr, "invalid dirsync control syntax\n");
318                                 fprintf(stderr, " syntax: crit(b):flags(n):max_attrs(n)[:cookie(o)]\n");
319                                 fprintf(stderr, "   note: b = boolean, n = number, o = b64 binary blob\n");
320                                 return NULL;
321                         }
322
323                         /* w2k3 seems to ignore the parameter,
324                          * but w2k sends a wrong cookie when this value is to small
325                          * this would cause looping forever, while getting
326                          * the same data and same cookie forever
327                          */
328                         if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
329
330                         ctrl[i] = talloc(ctrl, struct ldb_control);
331                         ctrl[i]->oid = LDB_CONTROL_DIRSYNC_OID;
332                         ctrl[i]->critical = crit;
333                         control = talloc(ctrl[i], struct ldb_dirsync_control);
334                         control->flags = flags;
335                         control->max_attributes = max_attrs;
336                         if (*cookie) {
337                                 control->cookie_len = ldb_base64_decode(cookie);
338                                 control->cookie = (char *)talloc_memdup(control, cookie, control->cookie_len);
339                         } else {
340                                 control->cookie = NULL;
341                                 control->cookie_len = 0;
342                         }
343                         ctrl[i]->data = control;
344
345                         continue;
346                 }
347
348                 if (strncmp(control_strings[i], "asq:", 4) == 0) {
349                         struct ldb_asq_control *control;
350                         const char *p;
351                         char attr[256];
352                         int crit, ret;
353
354                         attr[0] = '\0';
355                         p = &(control_strings[i][4]);
356                         ret = sscanf(p, "%d:%255[^$]", &crit, attr);
357                         if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
358                                 fprintf(stderr, "invalid asq control syntax\n");
359                                 fprintf(stderr, " syntax: crit(b):attr(s)\n");
360                                 fprintf(stderr, "   note: b = boolean, s = string\n");
361                                 return NULL;
362                         }
363
364                         ctrl[i] = talloc(ctrl, struct ldb_control);
365                         ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
366                         ctrl[i]->critical = crit;
367                         control = talloc(ctrl[i], struct ldb_asq_control);
368                         control->request = 1;
369                         control->source_attribute = talloc_strdup(control, attr);
370                         control->src_attr_len = strlen(attr);
371                         ctrl[i]->data = control;
372
373                         continue;
374                 }
375
376                 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
377                         struct ldb_extended_dn_control *control;
378                         const char *p;
379                         int crit, type, ret;
380
381                         p = &(control_strings[i][12]);
382                         ret = sscanf(p, "%d:%d", &crit, &type);
383                         if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
384                                 fprintf(stderr, "invalid extended_dn control syntax\n");
385                                 fprintf(stderr, " syntax: crit(b):type(b)\n");
386                                 fprintf(stderr, "   note: b = boolean\n");
387                                 return NULL;
388                         }
389
390                         ctrl[i] = talloc(ctrl, struct ldb_control);
391                         ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
392                         ctrl[i]->critical = crit;
393                         control = talloc(ctrl[i], struct ldb_extended_dn_control);
394                         control->type = type;
395                         ctrl[i]->data = control;
396
397                         continue;
398                 }
399
400                 if (strncmp(control_strings[i], "sd_flags:", 9) == 0) {
401                         struct ldb_sd_flags_control *control;
402                         const char *p;
403                         int crit, ret;
404                         unsigned secinfo_flags;
405
406                         p = &(control_strings[i][9]);
407                         ret = sscanf(p, "%d:%u", &crit, &secinfo_flags);
408                         if ((ret != 2) || (crit < 0) || (crit > 1) || (secinfo_flags < 0) || (secinfo_flags > 0xF)) {
409                                 fprintf(stderr, "invalid sd_flags control syntax\n");
410                                 fprintf(stderr, " syntax: crit(b):secinfo_flags(n)\n");
411                                 fprintf(stderr, "   note: b = boolean, n = number\n");
412                                 return NULL;
413                         }
414
415                         ctrl[i] = talloc(ctrl, struct ldb_control);
416                         ctrl[i]->oid = LDB_CONTROL_SD_FLAGS_OID;
417                         ctrl[i]->critical = crit;
418                         control = talloc(ctrl[i], struct ldb_sd_flags_control);
419                         control->secinfo_flags = secinfo_flags;
420                         ctrl[i]->data = control;
421
422                         continue;
423                 }
424
425                 if (strncmp(control_strings[i], "search_options:", 15) == 0) {
426                         struct ldb_search_options_control *control;
427                         const char *p;
428                         int crit, ret;
429                         unsigned search_options;
430
431                         p = &(control_strings[i][15]);
432                         ret = sscanf(p, "%d:%u", &crit, &search_options);
433                         if ((ret != 2) || (crit < 0) || (crit > 1) || (search_options < 0) || (search_options > 0xF)) {
434                                 fprintf(stderr, "invalid search_options control syntax\n");
435                                 fprintf(stderr, " syntax: crit(b):search_options(n)\n");
436                                 fprintf(stderr, "   note: b = boolean, n = number\n");
437                                 return NULL;
438                         }
439
440                         ctrl[i] = talloc(ctrl, struct ldb_control);
441                         ctrl[i]->oid = LDB_CONTROL_SEARCH_OPTIONS_OID;
442                         ctrl[i]->critical = crit;
443                         control = talloc(ctrl[i], struct ldb_search_options_control);
444                         control->search_options = search_options;
445                         ctrl[i]->data = control;
446
447                         continue;
448                 }
449
450                 if (strncmp(control_strings[i], "domain_scope:", 13) == 0) {
451                         const char *p;
452                         int crit, ret;
453
454                         p = &(control_strings[i][13]);
455                         ret = sscanf(p, "%d", &crit);
456                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
457                                 fprintf(stderr, "invalid domain_scope control syntax\n");
458                                 fprintf(stderr, " syntax: crit(b)\n");
459                                 fprintf(stderr, "   note: b = boolean\n");
460                                 return NULL;
461                         }
462
463                         ctrl[i] = talloc(ctrl, struct ldb_control);
464                         ctrl[i]->oid = LDB_CONTROL_DOMAIN_SCOPE_OID;
465                         ctrl[i]->critical = crit;
466                         ctrl[i]->data = NULL;
467
468                         continue;
469                 }
470
471                 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
472                         struct ldb_paged_control *control;
473                         const char *p;
474                         int crit, size, ret;
475                        
476                         p = &(control_strings[i][14]);
477                         ret = sscanf(p, "%d:%d", &crit, &size);
478
479                         if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
480                                 fprintf(stderr, "invalid paged_results control syntax\n");
481                                 fprintf(stderr, " syntax: crit(b):size(n)\n");
482                                 fprintf(stderr, "   note: b = boolean, n = number\n");
483                                 return NULL;
484                         }
485
486                         ctrl[i] = talloc(ctrl, struct ldb_control);
487                         ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
488                         ctrl[i]->critical = crit;
489                         control = talloc(ctrl[i], struct ldb_paged_control);
490                         control->size = size;
491                         control->cookie = NULL;
492                         control->cookie_len = 0;
493                         ctrl[i]->data = control;
494
495                         continue;
496                 }
497
498                 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
499                         struct ldb_server_sort_control **control;
500                         const char *p;
501                         char attr[256];
502                         char rule[128];
503                         int crit, rev, ret;
504
505                         attr[0] = '\0';
506                         rule[0] = '\0';
507                         p = &(control_strings[i][12]);
508                         ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
509                         if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
510                                 fprintf(stderr, "invalid server_sort control syntax\n");
511                                 fprintf(stderr, " syntax: crit(b):rev(b):attr(s)[:rule(s)]\n");
512                                 fprintf(stderr, "   note: b = boolean, s = string\n");
513                                 return NULL;
514                         }
515                         ctrl[i] = talloc(ctrl, struct ldb_control);
516                         ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
517                         ctrl[i]->critical = crit;
518                         control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
519                         control[0] = talloc(control, struct ldb_server_sort_control);
520                         control[0]->attributeName = talloc_strdup(control, attr);
521                         if (rule[0])
522                                 control[0]->orderingRule = talloc_strdup(control, rule);
523                         else
524                                 control[0]->orderingRule = NULL;
525                         control[0]->reverse = rev;
526                         control[1] = NULL;
527                         ctrl[i]->data = control;
528
529                         continue;
530                 }
531
532                 if (strncmp(control_strings[i], "notification:", 13) == 0) {
533                         const char *p;
534                         int crit, ret;
535
536                         p = &(control_strings[i][13]);
537                         ret = sscanf(p, "%d", &crit);
538                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
539                                 fprintf(stderr, "invalid notification control syntax\n");
540                                 fprintf(stderr, " syntax: crit(b)\n");
541                                 fprintf(stderr, "   note: b = boolean\n");
542                                 return NULL;
543                         }
544
545                         ctrl[i] = talloc(ctrl, struct ldb_control);
546                         ctrl[i]->oid = LDB_CONTROL_NOTIFICATION_OID;
547                         ctrl[i]->critical = crit;
548                         ctrl[i]->data = NULL;
549
550                         continue;
551                 }
552
553                 if (strncmp(control_strings[i], "show_deleted:", 13) == 0) {
554                         const char *p;
555                         int crit, ret;
556
557                         p = &(control_strings[i][13]);
558                         ret = sscanf(p, "%d", &crit);
559                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
560                                 fprintf(stderr, "invalid show_deleted control syntax\n");
561                                 fprintf(stderr, " syntax: crit(b)\n");
562                                 fprintf(stderr, "   note: b = boolean\n");
563                                 return NULL;
564                         }
565
566                         ctrl[i] = talloc(ctrl, struct ldb_control);
567                         ctrl[i]->oid = LDB_CONTROL_SHOW_DELETED_OID;
568                         ctrl[i]->critical = crit;
569                         ctrl[i]->data = NULL;
570
571                         continue;
572                 }
573
574                 if (strncmp(control_strings[i], "permissive_modify:", 18) == 0) {
575                         const char *p;
576                         int crit, ret;
577
578                         p = &(control_strings[i][18]);
579                         ret = sscanf(p, "%d", &crit);
580                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
581                                 fprintf(stderr, "invalid permissive_modify control syntax\n");
582                                 fprintf(stderr, " syntax: crit(b)\n");
583                                 fprintf(stderr, "   note: b = boolean\n");
584                                 return NULL;
585                         }
586
587                         ctrl[i] = talloc(ctrl, struct ldb_control);
588                         ctrl[i]->oid = LDB_CONTROL_PERMISSIVE_MODIFY_OID;
589                         ctrl[i]->critical = crit;
590                         ctrl[i]->data = NULL;
591
592                         continue;
593                 }
594
595                 /* no controls matched, throw an error */
596                 fprintf(stderr, "Invalid control name: '%s'\n", control_strings[i]);
597                 return NULL;
598         }
599
600         ctrl[i] = NULL;
601
602         return ctrl;
603 }
604
605
606 /* this function check controls reply and determines if more
607  * processing is needed setting up the request controls correctly
608  *
609  * returns:
610  *      -1 error
611  *      0 all ok
612  *      1 all ok, more processing required
613  */
614 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
615 {
616         int i, j;
617         int ret = 0;
618
619         if (reply == NULL || request == NULL) return -1;
620         
621         for (i = 0; reply[i]; i++) {
622                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
623                         struct ldb_vlv_resp_control *rep_control;
624
625                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
626                         
627                         /* check we have a matching control in the request */
628                         for (j = 0; request[j]; j++) {
629                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
630                                         break;
631                         }
632                         if (! request[j]) {
633                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
634                                 continue;
635                         }
636
637                         /* check the result */
638                         if (rep_control->vlv_result != 0) {
639                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
640                         } else {
641                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
642                         }
643
644                         continue;
645                 }
646
647                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
648                         struct ldb_asq_control *rep_control;
649
650                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
651
652                         /* check the result */
653                         if (rep_control->result != 0) {
654                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
655                         }
656
657                         continue;
658                 }
659
660                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
661                         struct ldb_paged_control *rep_control, *req_control;
662
663                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
664                         if (rep_control->cookie_len == 0) /* we are done */
665                                 break;
666
667                         /* more processing required */
668                         /* let's fill in the request control with the new cookie */
669
670                         for (j = 0; request[j]; j++) {
671                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
672                                         break;
673                         }
674                         /* if there's a reply control we must find a request
675                          * control matching it */
676                         if (! request[j]) return -1;
677
678                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
679
680                         if (req_control->cookie)
681                                 talloc_free(req_control->cookie);
682                         req_control->cookie = (char *)talloc_memdup(
683                                 req_control, rep_control->cookie,
684                                 rep_control->cookie_len);
685                         req_control->cookie_len = rep_control->cookie_len;
686
687                         ret = 1;
688
689                         continue;
690                 }
691
692                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
693                         struct ldb_sort_resp_control *rep_control;
694
695                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
696
697                         /* check we have a matching control in the request */
698                         for (j = 0; request[j]; j++) {
699                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
700                                         break;
701                         }
702                         if (! request[j]) {
703                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
704                                 continue;
705                         }
706
707                         /* check the result */
708                         if (rep_control->result != 0) {
709                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
710                         }
711
712                         continue;
713                 }
714
715                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
716                         struct ldb_dirsync_control *rep_control, *req_control;
717                         char *cookie;
718
719                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
720                         if (rep_control->cookie_len == 0) /* we are done */
721                                 break;
722
723                         /* more processing required */
724                         /* let's fill in the request control with the new cookie */
725
726                         for (j = 0; request[j]; j++) {
727                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
728                                         break;
729                         }
730                         /* if there's a reply control we must find a request
731                          * control matching it */
732                         if (! request[j]) return -1;
733
734                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
735
736                         if (req_control->cookie)
737                                 talloc_free(req_control->cookie);
738                         req_control->cookie = (char *)talloc_memdup(
739                                 req_control, rep_control->cookie,
740                                 rep_control->cookie_len);
741                         req_control->cookie_len = rep_control->cookie_len;
742
743                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
744                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
745
746                         continue;
747                 }
748
749                 /* no controls matched, throw a warning */
750                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
751         }
752
753         return ret;
754 }
755