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