s4-ldb: make ldb tools line buffered
[ira/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 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 #if (_SAMBA_BUILD_ >= 4)
62         POPT_COMMON_SAMBA
63         POPT_COMMON_CREDENTIALS
64         POPT_COMMON_CONNECTION
65         POPT_COMMON_VERSION
66 #endif
67         { NULL }
68 };
69
70 void ldb_cmdline_help(const char *cmdname, FILE *f)
71 {
72         poptContext pc;
73         pc = poptGetContext(cmdname, 0, NULL, popt_options, 
74                             POPT_CONTEXT_KEEP_FIRST);
75         poptPrintHelp(pc, f, 0);
76 }
77
78 /**
79   process command line options
80 */
81 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
82                                         int argc, const char **argv,
83                                         void (*usage)(void))
84 {
85         struct ldb_cmdline *ret=NULL;
86         poptContext pc;
87 #if (_SAMBA_BUILD_ >= 4)
88         int r;
89 #endif
90         int num_options = 0;
91         int opt;
92         int flags = 0;
93
94 #if (_SAMBA_BUILD_ >= 4)
95         r = ldb_register_samba_handlers(ldb);
96         if (r != 0) {
97                 goto failed;
98         }
99
100 #endif
101
102         /* make the ldb utilities line buffered */
103         setlinebuf(stdout);
104
105         ret = talloc_zero(ldb, struct ldb_cmdline);
106         if (ret == NULL) {
107                 fprintf(stderr, "Out of memory!\n");
108                 goto failed;
109         }
110
111         options = *ret;
112         
113         /* pull in URL */
114         options.url = getenv("LDB_URL");
115
116         /* and editor (used by ldbedit) */
117         options.editor = getenv("VISUAL");
118         if (!options.editor) {
119                 options.editor = getenv("EDITOR");
120         }
121         if (!options.editor) {
122                 options.editor = "vi";
123         }
124
125         options.scope = LDB_SCOPE_DEFAULT;
126
127         pc = poptGetContext(argv[0], argc, argv, popt_options, 
128                             POPT_CONTEXT_KEEP_FIRST);
129
130         while((opt = poptGetNextOpt(pc)) != -1) {
131                 switch (opt) {
132                 case 's': {
133                         const char *arg = poptGetOptArg(pc);
134                         if (strcmp(arg, "base") == 0) {
135                                 options.scope = LDB_SCOPE_BASE;
136                         } else if (strcmp(arg, "sub") == 0) {
137                                 options.scope = LDB_SCOPE_SUBTREE;
138                         } else if (strcmp(arg, "one") == 0) {
139                                 options.scope = LDB_SCOPE_ONELEVEL;
140                         } else {
141                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
142                                 goto failed;
143                         }
144                         break;
145                 }
146
147                 case 'v':
148                         options.verbose++;
149                         break;
150
151                 case 'o':
152                         options.options = talloc_realloc(ret, options.options, 
153                                                          const char *, num_options+3);
154                         if (options.options == NULL) {
155                                 fprintf(stderr, "Out of memory!\n");
156                                 goto failed;
157                         }
158                         options.options[num_options] = poptGetOptArg(pc);
159                         options.options[num_options+1] = NULL;
160                         num_options++;
161                         break;
162
163                 case 'c': {
164                         const char *cs = poptGetOptArg(pc);
165                         const char *p, *q;
166                         int cc;
167
168                         for (p = cs, cc = 1; (q = strchr(p, ',')); cc++, p = q + 1) ;
169
170                         options.controls = talloc_array(ret, char *, cc + 1);
171                         if (options.controls == NULL) {
172                                 fprintf(stderr, "Out of memory!\n");
173                                 goto failed;
174                         }
175                         for (p = cs, cc = 0; p != NULL; cc++) {
176                                 const char *t;
177
178                                 t = strchr(p, ',');
179                                 if (t == NULL) {
180                                         options.controls[cc] = talloc_strdup(options.controls, p);
181                                         p = NULL;
182                                 } else {
183                                         options.controls[cc] = talloc_strndup(options.controls, p, t-p);
184                                         p = t + 1;
185                                 }
186                         }
187                         options.controls[cc] = NULL;
188
189                         break;    
190                 }
191                 default:
192                         fprintf(stderr, "Invalid option %s: %s\n", 
193                                 poptBadOption(pc, 0), poptStrerror(opt));
194                         if (usage) usage();
195                         goto failed;
196                 }
197         }
198
199         /* setup the remaining options for the main program to use */
200         options.argv = poptGetArgs(pc);
201         if (options.argv) {
202                 options.argv++;
203                 while (options.argv[options.argc]) options.argc++;
204         }
205
206         *ret = options;
207
208         /* all utils need some option */
209         if (ret->url == NULL) {
210                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
211                 if (usage) usage();
212                 goto failed;
213         }
214
215         if (strcmp(ret->url, "NONE") == 0) {
216                 return ret;
217         }
218
219         if (options.nosync) {
220                 flags |= LDB_FLG_NOSYNC;
221         }
222
223         if (options.show_binary) {
224                 flags |= LDB_FLG_SHOW_BINARY;
225         }
226
227         if (options.tracing) {
228                 flags |= LDB_FLG_ENABLE_TRACING;
229         }
230
231 #if (_SAMBA_BUILD_ >= 4)
232         /* Must be after we have processed command line options */
233         gensec_init(cmdline_lp_ctx); 
234         
235         if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
236                 goto failed;
237         }
238         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
239                 goto failed;
240         }
241         if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
242                 goto failed;
243         }
244
245         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
246 #endif
247
248         if (options.modules_path != NULL) {
249                 ldb_set_modules_dir(ldb, options.modules_path);
250         } else if (getenv("LDB_MODULES_PATH") != NULL) {
251                 ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
252         }
253
254         /* now connect to the ldb */
255         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
256                 fprintf(stderr, "Failed to connect to %s - %s\n", 
257                         ret->url, ldb_errstring(ldb));
258                 goto failed;
259         }
260
261         return ret;
262
263 failed:
264         talloc_free(ret);
265         exit(1);
266         return NULL;
267 }
268
269 /* this function check controls reply and determines if more
270  * processing is needed setting up the request controls correctly
271  *
272  * returns:
273  *      -1 error
274  *      0 all ok
275  *      1 all ok, more processing required
276  */
277 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
278 {
279         int i, j;
280         int ret = 0;
281
282         if (reply == NULL || request == NULL) return -1;
283         
284         for (i = 0; reply[i]; i++) {
285                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
286                         struct ldb_vlv_resp_control *rep_control;
287
288                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
289                         
290                         /* check we have a matching control in the request */
291                         for (j = 0; request[j]; j++) {
292                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
293                                         break;
294                         }
295                         if (! request[j]) {
296                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
297                                 continue;
298                         }
299
300                         /* check the result */
301                         if (rep_control->vlv_result != 0) {
302                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
303                         } else {
304                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
305                         }
306
307                         continue;
308                 }
309
310                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
311                         struct ldb_asq_control *rep_control;
312
313                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
314
315                         /* check the result */
316                         if (rep_control->result != 0) {
317                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
318                         }
319
320                         continue;
321                 }
322
323                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
324                         struct ldb_paged_control *rep_control, *req_control;
325
326                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
327                         if (rep_control->cookie_len == 0) /* we are done */
328                                 break;
329
330                         /* more processing required */
331                         /* let's fill in the request control with the new cookie */
332
333                         for (j = 0; request[j]; j++) {
334                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
335                                         break;
336                         }
337                         /* if there's a reply control we must find a request
338                          * control matching it */
339                         if (! request[j]) return -1;
340
341                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
342
343                         if (req_control->cookie)
344                                 talloc_free(req_control->cookie);
345                         req_control->cookie = (char *)talloc_memdup(
346                                 req_control, rep_control->cookie,
347                                 rep_control->cookie_len);
348                         req_control->cookie_len = rep_control->cookie_len;
349
350                         ret = 1;
351
352                         continue;
353                 }
354
355                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
356                         struct ldb_sort_resp_control *rep_control;
357
358                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
359
360                         /* check we have a matching control in the request */
361                         for (j = 0; request[j]; j++) {
362                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
363                                         break;
364                         }
365                         if (! request[j]) {
366                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
367                                 continue;
368                         }
369
370                         /* check the result */
371                         if (rep_control->result != 0) {
372                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
373                         }
374
375                         continue;
376                 }
377
378                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
379                         struct ldb_dirsync_control *rep_control, *req_control;
380                         char *cookie;
381
382                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
383                         if (rep_control->cookie_len == 0) /* we are done */
384                                 break;
385
386                         /* more processing required */
387                         /* let's fill in the request control with the new cookie */
388
389                         for (j = 0; request[j]; j++) {
390                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
391                                         break;
392                         }
393                         /* if there's a reply control we must find a request
394                          * control matching it */
395                         if (! request[j]) return -1;
396
397                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
398
399                         if (req_control->cookie)
400                                 talloc_free(req_control->cookie);
401                         req_control->cookie = (char *)talloc_memdup(
402                                 req_control, rep_control->cookie,
403                                 rep_control->cookie_len);
404                         req_control->cookie_len = rep_control->cookie_len;
405
406                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
407                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
408
409                         continue;
410                 }
411
412                 /* no controls matched, throw a warning */
413                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
414         }
415
416         return ret;
417 }
418