e27ca5b6a6c25cad2d83a48c462326410fbb6f6c
[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 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 #include "ldb_includes.h"
25 #include "ldb.h"
26 #include "tools/cmdline.h"
27
28 #if (_SAMBA_BUILD_ >= 4)
29 #include "includes.h"
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 "ldb_wrap.h"
35 #include "param/param.h"
36 #endif
37
38 static struct ldb_cmdline options; /* needs to be static for older compilers */
39
40 static struct poptOption popt_options[] = {
41         POPT_AUTOHELP
42         { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
43         { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
44         { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
45         { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
46         { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
47         { "trace",     0,   POPT_ARG_NONE, &options.tracing, 0, "enable tracing", NULL },
48         { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
49         { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
50         { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
51         { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
52         { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
53         { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
54         { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
55         { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
56         { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
57         { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
58         { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
59         { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
60         { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
61         { "paged", 0, POPT_ARG_NONE, NULL, 'P', "use a paged search", NULL },
62         { "show-deleted", 0, POPT_ARG_NONE, NULL, 'D', "show deleted objects", NULL },
63         { "show-recycled", 0, POPT_ARG_NONE, NULL, 'R', "show recycled objects", NULL },
64         { "cross-ncs", 0, POPT_ARG_NONE, NULL, 'N', "search across NC boundaries", NULL },
65         { "extended-dn", 0, POPT_ARG_NONE, NULL, 'E', "show extended DNs", NULL },
66 #if (_SAMBA_BUILD_ >= 4)
67         POPT_COMMON_SAMBA
68         POPT_COMMON_CREDENTIALS
69         POPT_COMMON_CONNECTION
70         POPT_COMMON_VERSION
71 #endif
72         { NULL }
73 };
74
75 void ldb_cmdline_help(const char *cmdname, FILE *f)
76 {
77         poptContext pc;
78         pc = poptGetContext(cmdname, 0, NULL, popt_options, 
79                             POPT_CONTEXT_KEEP_FIRST);
80         poptPrintHelp(pc, f, 0);
81 }
82
83 /*
84   add a control to the options structure
85  */
86 static bool add_control(TALLOC_CTX *mem_ctx, const char *control)
87 {
88         int i;
89
90         /* count how many controls we already have */
91         for (i=0; options.controls && options.controls[i]; i++) ;
92
93         options.controls = talloc_realloc(mem_ctx, options.controls, const char *, i + 2);
94         if (options.controls == NULL) {
95                 return false;
96         }
97         options.controls[i] = control;
98         options.controls[i+1] = NULL;
99         return true;
100 }
101
102 /**
103   process command line options
104 */
105 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
106                                         int argc, const char **argv,
107                                         void (*usage)(void))
108 {
109         struct ldb_cmdline *ret=NULL;
110         poptContext pc;
111 #if (_SAMBA_BUILD_ >= 4)
112         int r;
113 #endif
114         int num_options = 0;
115         int opt;
116         int flags = 0;
117
118 #if (_SAMBA_BUILD_ >= 4)
119         r = ldb_register_samba_handlers(ldb);
120         if (r != 0) {
121                 goto failed;
122         }
123
124 #endif
125
126         /* make the ldb utilities line buffered */
127         setlinebuf(stdout);
128
129         ret = talloc_zero(ldb, struct ldb_cmdline);
130         if (ret == NULL) {
131                 fprintf(stderr, "Out of memory!\n");
132                 goto failed;
133         }
134
135         options = *ret;
136         
137         /* pull in URL */
138         options.url = getenv("LDB_URL");
139
140         /* and editor (used by ldbedit) */
141         options.editor = getenv("VISUAL");
142         if (!options.editor) {
143                 options.editor = getenv("EDITOR");
144         }
145         if (!options.editor) {
146                 options.editor = "vi";
147         }
148
149         options.scope = LDB_SCOPE_DEFAULT;
150
151         pc = poptGetContext(argv[0], argc, argv, popt_options, 
152                             POPT_CONTEXT_KEEP_FIRST);
153
154         while((opt = poptGetNextOpt(pc)) != -1) {
155                 switch (opt) {
156                 case 's': {
157                         const char *arg = poptGetOptArg(pc);
158                         if (strcmp(arg, "base") == 0) {
159                                 options.scope = LDB_SCOPE_BASE;
160                         } else if (strcmp(arg, "sub") == 0) {
161                                 options.scope = LDB_SCOPE_SUBTREE;
162                         } else if (strcmp(arg, "one") == 0) {
163                                 options.scope = LDB_SCOPE_ONELEVEL;
164                         } else {
165                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
166                                 goto failed;
167                         }
168                         break;
169                 }
170
171                 case 'v':
172                         options.verbose++;
173                         break;
174
175                 case 'o':
176                         options.options = talloc_realloc(ret, options.options, 
177                                                          const char *, num_options+3);
178                         if (options.options == NULL) {
179                                 fprintf(stderr, "Out of memory!\n");
180                                 goto failed;
181                         }
182                         options.options[num_options] = poptGetOptArg(pc);
183                         options.options[num_options+1] = NULL;
184                         num_options++;
185                         break;
186
187                 case 'c': {
188                         const char *cs = poptGetOptArg(pc);
189                         const char *p;
190
191                         for (p = cs; p != NULL; ) {
192                                 const char *t, *c;
193
194                                 t = strchr(p, ',');
195                                 if (t == NULL) {
196                                         c = talloc_strdup(options.controls, p);
197                                         p = NULL;
198                                 } else {
199                                         c = talloc_strndup(options.controls, p, t-p);
200                                         p = t + 1;
201                                 }
202                                 if (c == NULL || !add_control(ret, c)) {
203                                         fprintf(stderr, __location__ ": out of memory\n");
204                                         goto failed;
205                                 }
206                         }
207
208                         break;    
209                 }
210                 case 'P':
211                         if (!add_control(ret, "paged_results:1:1024")) {
212                                 fprintf(stderr, __location__ ": out of memory\n");
213                                 goto failed;
214                         }
215                         break;
216                 case 'D':
217                         if (!add_control(ret, "show_deleted:1")) {
218                                 fprintf(stderr, __location__ ": out of memory\n");
219                                 goto failed;
220                         }
221                         break;
222                 case 'R':
223                         if (!add_control(ret, "show_recycled:1")) {
224                                 fprintf(stderr, __location__ ": out of memory\n");
225                                 goto failed;
226                         }
227                         break;
228                 case 'N':
229                         if (!add_control(ret, "search_options:1:2")) {
230                                 fprintf(stderr, __location__ ": out of memory\n");
231                                 goto failed;
232                         }
233                         break;
234                 case 'E':
235                         if (!add_control(ret, "extended_dn:1:1")) {
236                                 fprintf(stderr, __location__ ": out of memory\n");
237                                 goto failed;
238                         }
239                         break;
240                 default:
241                         fprintf(stderr, "Invalid option %s: %s\n", 
242                                 poptBadOption(pc, 0), poptStrerror(opt));
243                         if (usage) usage();
244                         goto failed;
245                 }
246         }
247
248         /* setup the remaining options for the main program to use */
249         options.argv = poptGetArgs(pc);
250         if (options.argv) {
251                 options.argv++;
252                 while (options.argv[options.argc]) options.argc++;
253         }
254
255         *ret = options;
256
257         /* all utils need some option */
258         if (ret->url == NULL) {
259                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
260                 if (usage) usage();
261                 goto failed;
262         }
263
264         if (strcmp(ret->url, "NONE") == 0) {
265                 return ret;
266         }
267
268         if (options.nosync) {
269                 flags |= LDB_FLG_NOSYNC;
270         }
271
272         if (options.show_binary) {
273                 flags |= LDB_FLG_SHOW_BINARY;
274         }
275
276         if (options.tracing) {
277                 flags |= LDB_FLG_ENABLE_TRACING;
278         }
279
280 #if (_SAMBA_BUILD_ >= 4)
281         /* Must be after we have processed command line options */
282         gensec_init(cmdline_lp_ctx); 
283         
284         if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
285                 goto failed;
286         }
287         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
288                 goto failed;
289         }
290         if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
291                 goto failed;
292         }
293
294         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
295 #endif
296
297         if (options.modules_path != NULL) {
298                 ldb_set_modules_dir(ldb, options.modules_path);
299         } else if (getenv("LDB_MODULES_PATH") != NULL) {
300                 ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
301         }
302
303         /* now connect to the ldb */
304         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
305                 fprintf(stderr, "Failed to connect to %s - %s\n", 
306                         ret->url, ldb_errstring(ldb));
307                 goto failed;
308         }
309
310         return ret;
311
312 failed:
313         talloc_free(ret);
314         exit(1);
315         return NULL;
316 }
317
318 /* this function check controls reply and determines if more
319  * processing is needed setting up the request controls correctly
320  *
321  * returns:
322  *      -1 error
323  *      0 all ok
324  *      1 all ok, more processing required
325  */
326 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
327 {
328         int i, j;
329         int ret = 0;
330
331         if (reply == NULL || request == NULL) return -1;
332         
333         for (i = 0; reply[i]; i++) {
334                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
335                         struct ldb_vlv_resp_control *rep_control;
336
337                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
338                         
339                         /* check we have a matching control in the request */
340                         for (j = 0; request[j]; j++) {
341                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
342                                         break;
343                         }
344                         if (! request[j]) {
345                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
346                                 continue;
347                         }
348
349                         /* check the result */
350                         if (rep_control->vlv_result != 0) {
351                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
352                         } else {
353                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
354                         }
355
356                         continue;
357                 }
358
359                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
360                         struct ldb_asq_control *rep_control;
361
362                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
363
364                         /* check the result */
365                         if (rep_control->result != 0) {
366                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
367                         }
368
369                         continue;
370                 }
371
372                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
373                         struct ldb_paged_control *rep_control, *req_control;
374
375                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
376                         if (rep_control->cookie_len == 0) /* we are done */
377                                 break;
378
379                         /* more processing required */
380                         /* let's fill in the request control with the new cookie */
381
382                         for (j = 0; request[j]; j++) {
383                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
384                                         break;
385                         }
386                         /* if there's a reply control we must find a request
387                          * control matching it */
388                         if (! request[j]) return -1;
389
390                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
391
392                         if (req_control->cookie)
393                                 talloc_free(req_control->cookie);
394                         req_control->cookie = (char *)talloc_memdup(
395                                 req_control, rep_control->cookie,
396                                 rep_control->cookie_len);
397                         req_control->cookie_len = rep_control->cookie_len;
398
399                         ret = 1;
400
401                         continue;
402                 }
403
404                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
405                         struct ldb_sort_resp_control *rep_control;
406
407                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
408
409                         /* check we have a matching control in the request */
410                         for (j = 0; request[j]; j++) {
411                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
412                                         break;
413                         }
414                         if (! request[j]) {
415                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
416                                 continue;
417                         }
418
419                         /* check the result */
420                         if (rep_control->result != 0) {
421                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
422                         }
423
424                         continue;
425                 }
426
427                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
428                         struct ldb_dirsync_control *rep_control, *req_control;
429                         char *cookie;
430
431                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
432                         if (rep_control->cookie_len == 0) /* we are done */
433                                 break;
434
435                         /* more processing required */
436                         /* let's fill in the request control with the new cookie */
437
438                         for (j = 0; request[j]; j++) {
439                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
440                                         break;
441                         }
442                         /* if there's a reply control we must find a request
443                          * control matching it */
444                         if (! request[j]) return -1;
445
446                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
447
448                         if (req_control->cookie)
449                                 talloc_free(req_control->cookie);
450                         req_control->cookie = (char *)talloc_memdup(
451                                 req_control, rep_control->cookie,
452                                 rep_control->cookie_len);
453                         req_control->cookie_len = rep_control->cookie_len;
454
455                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
456                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
457
458                         continue;
459                 }
460
461                 /* no controls matched, throw a warning */
462                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
463         }
464
465         return ret;
466 }
467