r21496: A number of ldb control and LDAP changes, surrounding the
[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 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 #if (_SAMBA_BUILD_ >= 4)
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 "db_wrap.h"
35 #endif
36
37
38
39 /*
40   process command line options
41 */
42 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
43                                         void (*usage)(void))
44 {
45         static struct ldb_cmdline options; /* needs to be static for older compilers */
46         struct ldb_cmdline *ret=NULL;
47         poptContext pc;
48 #if (_SAMBA_BUILD_ >= 4)
49         int r;
50 #endif
51         int num_options = 0;
52         int opt;
53         int flags = 0;
54
55         struct poptOption popt_options[] = {
56                 POPT_AUTOHELP
57                 { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
58                 { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
59                 { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
60                 { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
61                 { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
62                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
63                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
64                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
65                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
66                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
67                 { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
68                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
69                 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
70                 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
71                 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
72                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
73                 { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
74 #if (_SAMBA_BUILD_ >= 4)
75                 POPT_COMMON_SAMBA
76                 POPT_COMMON_CREDENTIALS
77                 POPT_COMMON_VERSION
78 #endif
79                 { NULL }
80         };
81
82         ldb_global_init();
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                 ldb_oom(ldb);
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                                 ldb_oom(ldb);
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                                 ldb_oom(ldb);
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(); 
213         
214         if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
215                 goto failed;
216         }
217         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
218                 goto failed;
219         }
220         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
221 #endif
222
223         /* now connect to the ldb */
224         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
225                 fprintf(stderr, "Failed to connect to %s - %s\n", 
226                         ret->url, ldb_errstring(ldb));
227                 goto failed;
228         }
229
230         return ret;
231
232 failed:
233         talloc_free(ret);
234         exit(1);
235         return NULL;
236 }
237
238 /* this function check controls reply and determines if more
239  * processing is needed setting up the request controls correctly
240  *
241  * returns:
242  *      -1 error
243  *      0 all ok
244  *      1 all ok, more processing required
245  */
246 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
247 {
248         int i, j;
249         int ret = 0;
250
251         if (reply == NULL || request == NULL) return -1;
252         
253         for (i = 0; reply[i]; i++) {
254                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
255                         struct ldb_vlv_resp_control *rep_control;
256
257                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
258                         
259                         /* check we have a matching control in the request */
260                         for (j = 0; request[j]; j++) {
261                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
262                                         break;
263                         }
264                         if (! request[j]) {
265                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
266                                 continue;
267                         }
268
269                         /* check the result */
270                         if (rep_control->vlv_result != 0) {
271                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
272                         } else {
273                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
274                         }
275
276                         continue;
277                 }
278
279                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
280                         struct ldb_asq_control *rep_control;
281
282                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
283
284                         /* check the result */
285                         if (rep_control->result != 0) {
286                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
287                         }
288
289                         continue;
290                 }
291
292                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
293                         struct ldb_paged_control *rep_control, *req_control;
294
295                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
296                         if (rep_control->cookie_len == 0) /* we are done */
297                                 break;
298
299                         /* more processing required */
300                         /* let's fill in the request control with the new cookie */
301
302                         for (j = 0; request[j]; j++) {
303                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
304                                         break;
305                         }
306                         /* if there's a reply control we must find a request
307                          * control matching it */
308                         if (! request[j]) return -1;
309
310                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
311
312                         if (req_control->cookie)
313                                 talloc_free(req_control->cookie);
314                         req_control->cookie = (char *)talloc_memdup(
315                                 req_control, rep_control->cookie,
316                                 rep_control->cookie_len);
317                         req_control->cookie_len = rep_control->cookie_len;
318
319                         ret = 1;
320
321                         continue;
322                 }
323
324                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
325                         struct ldb_sort_resp_control *rep_control;
326
327                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
328
329                         /* check we have a matching control in the request */
330                         for (j = 0; request[j]; j++) {
331                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
332                                         break;
333                         }
334                         if (! request[j]) {
335                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
336                                 continue;
337                         }
338
339                         /* check the result */
340                         if (rep_control->result != 0) {
341                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
342                         }
343
344                         continue;
345                 }
346
347                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
348                         struct ldb_dirsync_control *rep_control, *req_control;
349                         char *cookie;
350
351                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
352                         if (rep_control->cookie_len == 0) /* we are done */
353                                 break;
354
355                         /* more processing required */
356                         /* let's fill in the request control with the new cookie */
357
358                         for (j = 0; request[j]; j++) {
359                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
360                                         break;
361                         }
362                         /* if there's a reply control we must find a request
363                          * control matching it */
364                         if (! request[j]) return -1;
365
366                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
367
368                         if (req_control->cookie)
369                                 talloc_free(req_control->cookie);
370                         req_control->cookie = (char *)talloc_memdup(
371                                 req_control, rep_control->cookie,
372                                 rep_control->cookie_len);
373                         req_control->cookie_len = rep_control->cookie_len;
374
375                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
376                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
377
378                         continue;
379                 }
380
381                 /* no controls matched, throw a warning */
382                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
383         }
384
385         return ret;
386 }
387