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