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