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