r13340: The gensec_init() needs to be after the popt processing, as it
[metze/samba/wip.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 #ifdef _SAMBA_BUILD_
75         r = ldb_register_samba_handlers(ldb);
76         if (r != 0) {
77                 goto failed;
78         }
79
80 #endif
81
82         ret = talloc_zero(ldb, struct ldb_cmdline);
83         if (ret == NULL) {
84                 ldb_oom(ldb);
85                 goto failed;
86         }
87
88         options = *ret;
89         
90         /* pull in URL */
91         options.url = getenv("LDB_URL");
92
93         /* and editor (used by ldbedit) */
94         options.editor = getenv("VISUAL");
95         if (!options.editor) {
96                 options.editor = getenv("EDITOR");
97         }
98         if (!options.editor) {
99                 options.editor = "vi";
100         }
101
102         options.scope = LDB_SCOPE_DEFAULT;
103
104         pc = poptGetContext(argv[0], argc, argv, popt_options, 
105                             POPT_CONTEXT_KEEP_FIRST);
106
107         while((opt = poptGetNextOpt(pc)) != -1) {
108                 switch (opt) {
109                 case 's': {
110                         const char *arg = poptGetOptArg(pc);
111                         if (strcmp(arg, "base") == 0) {
112                                 options.scope = LDB_SCOPE_BASE;
113                         } else if (strcmp(arg, "sub") == 0) {
114                                 options.scope = LDB_SCOPE_SUBTREE;
115                         } else if (strcmp(arg, "one") == 0) {
116                                 options.scope = LDB_SCOPE_ONELEVEL;
117                         } else {
118                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
119                                 goto failed;
120                         }
121                         break;
122                 }
123
124                 case 'v':
125                         options.verbose++;
126                         break;
127
128                 case 'o':
129                         options.options = talloc_realloc(ret, options.options, 
130                                                          const char *, num_options+3);
131                         if (options.options == NULL) {
132                                 ldb_oom(ldb);
133                                 goto failed;
134                         }
135                         options.options[num_options] = poptGetOptArg(pc);
136                         options.options[num_options+1] = NULL;
137                         num_options++;
138                         break;
139
140                 case 'c': {
141                         const char *cs = poptGetOptArg(pc);
142                         const char *p, *q;
143                         int cc;
144
145                         for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
146
147                         options.controls = talloc_array(ret, char *, cc + 1);
148                         if (options.controls == NULL) {
149                                 ldb_oom(ldb);
150                                 goto failed;
151                         }
152                         for (p = cs, cc = 0; p != NULL; cc++) {
153                                 const char *t;
154
155                                 t = strchr(p, ',');
156                                 if (t == NULL) {
157                                         options.controls[cc] = talloc_strdup(options.controls, p);
158                                         p = NULL;
159                                 } else {
160                                         options.controls[cc] = talloc_strndup(options.controls, p, t-p);
161                                         p = t + 1;
162                                 }
163                         }
164                         options.controls[cc] = NULL;
165
166                         break;    
167                 }
168                 default:
169                         fprintf(stderr, "Invalid option %s: %s\n", 
170                                 poptBadOption(pc, 0), poptStrerror(opt));
171                         if (usage) usage();
172                         goto failed;
173                 }
174         }
175
176         /* setup the remaining options for the main program to use */
177         options.argv = poptGetArgs(pc);
178         if (options.argv) {
179                 options.argv++;
180                 while (options.argv[options.argc]) options.argc++;
181         }
182
183         *ret = options;
184
185         /* all utils need some option */
186         if (ret->url == NULL) {
187                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
188                 if (usage) usage();
189                 goto failed;
190         }
191
192         if (strcmp(ret->url, "NONE") != 0) {
193                 int flags = 0;
194                 if (options.nosync) {
195                         flags |= LDB_FLG_NOSYNC;
196                 }
197
198 #ifdef _SAMBA_BUILD_
199                 /* Must be after we have processed command line options */
200                 gensec_init(); 
201
202                 if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
203                         goto failed;
204                 }
205                 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
206                         goto failed;
207                 }
208                 ldb_set_utf8_fns(ldb, NULL, wrap_caseless_cmp, wrap_casefold);
209 #endif
210                 if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
211                         fprintf(stderr, "Failed to connect to %s - %s\n", 
212                                 ret->url, ldb_errstring(ldb));
213                         goto failed;
214                 }
215         }
216
217         return ret;
218
219 failed:
220         talloc_free(ret);
221         exit(1);
222         return NULL;
223 }
224
225 struct ldb_control **parse_controls(void *mem_ctx, char **control_strings)
226 {
227         int i;
228         struct ldb_control **ctrl;
229
230         if (control_strings == NULL || control_strings[0] == NULL)
231                 return NULL;
232
233         for (i = 0; control_strings[i]; i++);
234
235         ctrl = talloc_array(mem_ctx, struct ldb_control *, i + 1);
236
237         for (i = 0; control_strings[i]; i++) {
238                 if (strncmp(control_strings[i], "dirsync:", 8) == 0) {
239                         struct ldb_dirsync_control *control;
240                         const char *p;
241                         char cookie[1024];
242                         int crit, flags, max_attrs, ret;
243                        
244                         cookie[0] = '\0';
245                         p = &(control_strings[i][8]);
246                         ret = sscanf(p, "%d:%d:%d:%1023[^$]", &crit, &flags, &max_attrs, cookie);
247
248                         if ((ret < 3) || (crit < 0) || (crit > 1) || (flags < 0) || (max_attrs < 0)) {
249                                 fprintf(stderr, "invalid dirsync control syntax\n");
250                                 return NULL;
251                         }
252
253                         /* w2k3 seems to ignore the parameter,
254                          * but w2k sends a wrong cookie when this value is to small
255                          * this would cause looping forever, while getting
256                          * the same data and same cookie forever
257                          */
258                         if (max_attrs == 0) max_attrs = 0x0FFFFFFF;
259
260                         ctrl[i] = talloc(ctrl, struct ldb_control);
261                         ctrl[i]->oid = LDB_CONTROL_DIRSYNC_OID;
262                         ctrl[i]->critical = crit;
263                         control = talloc(ctrl[i], struct ldb_dirsync_control);
264                         control->flags = flags;
265                         control->max_attributes = max_attrs;
266                         if (*cookie) {
267                                 control->cookie_len = ldb_base64_decode(cookie);
268                                 control->cookie = talloc_memdup(control, cookie, control->cookie_len);
269                         } else {
270                                 control->cookie = NULL;
271                                 control->cookie_len = 0;
272                         }
273                         ctrl[i]->data = control;
274
275                         continue;
276                 }
277
278                 if (strncmp(control_strings[i], "asq:", 4) == 0) {
279                         struct ldb_asq_control *control;
280                         const char *p;
281                         char attr[256];
282                         int crit, ret;
283
284                         attr[0] = '\0';
285                         p = &(control_strings[i][4]);
286                         ret = sscanf(p, "%d:%255[^$]", &crit, attr);
287                         if ((ret != 2) || (crit < 0) || (crit > 1) || (attr[0] == '\0')) {
288                                 fprintf(stderr, "invalid asq control syntax\n");
289                                 return NULL;
290                         }
291
292                         ctrl[i] = talloc(ctrl, struct ldb_control);
293                         ctrl[i]->oid = LDB_CONTROL_ASQ_OID;
294                         ctrl[i]->critical = crit;
295                         control = talloc(ctrl[i], struct ldb_asq_control);
296                         control->request = 1;
297                         control->source_attribute = talloc_strdup(control, attr);
298                         control->src_attr_len = strlen(attr);
299                         ctrl[i]->data = control;
300
301                         continue;
302                 }
303
304                 if (strncmp(control_strings[i], "extended_dn:", 12) == 0) {
305                         struct ldb_extended_dn_control *control;
306                         const char *p;
307                         int crit, type, ret;
308
309                         p = &(control_strings[i][12]);
310                         ret = sscanf(p, "%d:%d", &crit, &type);
311                         if ((ret != 2) || (crit < 0) || (crit > 1) || (type < 0) || (type > 1)) {
312                                 fprintf(stderr, "invalid extended_dn control syntax\n");
313                                 return NULL;
314                         }
315
316                         ctrl[i] = talloc(ctrl, struct ldb_control);
317                         ctrl[i]->oid = LDB_CONTROL_EXTENDED_DN_OID;
318                         ctrl[i]->critical = crit;
319                         control = talloc(ctrl[i], struct ldb_extended_dn_control);
320                         control->type = type;
321                         ctrl[i]->data = control;
322
323                         continue;
324                 }
325
326                 if (strncmp(control_strings[i], "paged_results:", 14) == 0) {
327                         struct ldb_paged_control *control;
328                         const char *p;
329                         int crit, size, ret;
330                        
331                         p = &(control_strings[i][14]);
332                         ret = sscanf(p, "%d:%d", &crit, &size);
333
334                         if ((ret != 2) || (crit < 0) || (crit > 1) || (size < 0)) {
335                                 fprintf(stderr, "invalid paged_results control syntax\n");
336                                 return NULL;
337                         }
338
339                         ctrl[i] = talloc(ctrl, struct ldb_control);
340                         ctrl[i]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
341                         ctrl[i]->critical = crit;
342                         control = talloc(ctrl[i], struct ldb_paged_control);
343                         control->size = size;
344                         control->cookie = NULL;
345                         control->cookie_len = 0;
346                         ctrl[i]->data = control;
347
348                         continue;
349                 }
350
351                 if (strncmp(control_strings[i], "server_sort:", 12) == 0) {
352                         struct ldb_server_sort_control **control;
353                         const char *p;
354                         char attr[256];
355                         char rule[128];
356                         int crit, rev, ret;
357
358                         attr[0] = '\0';
359                         rule[0] = '\0';
360                         p = &(control_strings[i][12]);
361                         ret = sscanf(p, "%d:%d:%255[^:]:%127[^:]", &crit, &rev, attr, rule);
362                         if ((ret < 3) || (crit < 0) || (crit > 1) || (rev < 0 ) || (rev > 1) ||attr[0] == '\0') {
363                                 fprintf(stderr, "invalid server_sort control syntax\n");
364                                 return NULL;
365                         }
366                         ctrl[i] = talloc(ctrl, struct ldb_control);
367                         ctrl[i]->oid = LDB_CONTROL_SERVER_SORT_OID;
368                         ctrl[i]->critical = crit;
369                         control = talloc_array(ctrl[i], struct ldb_server_sort_control *, 2);
370                         control[0] = talloc(control, struct ldb_server_sort_control);
371                         control[0]->attributeName = talloc_strdup(control, attr);
372                         if (rule[0])
373                                 control[0]->orderingRule = talloc_strdup(control, rule);
374                         else
375                                 control[0]->orderingRule = NULL;
376                         control[0]->reverse = rev;
377                         control[1] = NULL;
378                         ctrl[i]->data = control;
379
380                         continue;
381                 }
382
383                 if (strncmp(control_strings[i], "notification:", 13) == 0) {
384                         const char *p;
385                         int crit, ret;
386
387                         p = &(control_strings[i][13]);
388                         ret = sscanf(p, "%d", &crit);
389                         if ((ret != 1) || (crit < 0) || (crit > 1)) {
390                                 fprintf(stderr, "invalid notification control syntax\n");
391                                 return NULL;
392                         }
393
394                         ctrl[i] = talloc(ctrl, struct ldb_control);
395                         ctrl[i]->oid = LDB_CONTROL_NOTIFICATION_OID;
396                         ctrl[i]->critical = crit;
397                         ctrl[i]->data = NULL;
398
399                         continue;
400                 }
401
402                 /* no controls matched, throw an error */
403                 fprintf(stderr, "Invalid control name\n");
404                 return NULL;
405         }
406
407         ctrl[i] = NULL;
408
409         return ctrl;
410 }
411
412
413 /* this function check controls reply and determines if more
414  * processing is needed setting up the request controls correctly
415  *
416  * returns:
417  *      -1 error
418  *      0 all ok
419  *      1 all ok, more processing required
420  */
421 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
422 {
423         int i, j;
424         int ret = 0;
425
426         if (reply == NULL || request == NULL) return -1;
427         
428         for (i = 0; reply[i]; i++) {
429                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
430                         struct ldb_asq_control *rep_control;
431
432                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
433
434                         /* check the result */
435                         if (rep_control->result != 0) {
436                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
437                         }
438
439                         continue;
440                 }
441                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
442                         struct ldb_paged_control *rep_control, *req_control;
443
444                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
445                         if (rep_control->cookie_len == 0) /* we are done */
446                                 break;
447
448                         /* more processing required */
449                         /* let's fill in the request control with the new cookie */
450
451                         for (j = 0; request[j]; j++) {
452                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
453                                         break;
454                         }
455                         /* if there's a reply control we must find a request
456                          * control matching it */
457                         if (! request[j]) return -1;
458
459                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
460
461                         if (req_control->cookie)
462                                 talloc_free(req_control->cookie);
463                         req_control->cookie = talloc_memdup(req_control,
464                                                             rep_control->cookie,
465                                                             rep_control->cookie_len);
466                         req_control->cookie_len = rep_control->cookie_len;
467
468                         ret = 1;
469
470                         continue;
471                 }
472
473                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
474                         struct ldb_sort_resp_control *rep_control;
475
476                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
477
478                         /* check we have a matching control in the request */
479                         for (j = 0; request[j]; j++) {
480                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
481                                         break;
482                         }
483                         if (! request[j]) {
484                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
485                                 continue;
486                         }
487
488                         /* check the result */
489                         if (rep_control->result != 0) {
490                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
491                         }
492
493                         continue;
494                 }
495
496                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
497                         struct ldb_dirsync_control *rep_control, *req_control;
498                         char *cookie;
499
500                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
501                         if (rep_control->cookie_len == 0) /* we are done */
502                                 break;
503
504                         /* more processing required */
505                         /* let's fill in the request control with the new cookie */
506
507                         for (j = 0; request[j]; j++) {
508                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
509                                         break;
510                         }
511                         /* if there's a reply control we must find a request
512                          * control matching it */
513                         if (! request[j]) return -1;
514
515                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
516
517                         if (req_control->cookie)
518                                 talloc_free(req_control->cookie);
519                         req_control->cookie = talloc_memdup(req_control, 
520                                                             rep_control->cookie,
521                                                             rep_control->cookie_len);
522                         req_control->cookie_len = rep_control->cookie_len;
523
524                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
525                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
526
527                         sleep(120);
528                         
529                         ret = 1;
530
531                         continue;
532                 }
533
534                 /* no controls matched, throw a warning */
535                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
536         }
537
538         return ret;
539 }
540