LDB:tools - change counters to be unsigned
[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 #include "librpc/gen_ndr/drsblobs.h"
37 #include "dsdb/schema/schema.h"
38 #include "dsdb/common/proto.h"
39 #endif
40
41 static struct ldb_cmdline options; /* needs to be static for older compilers */
42
43 static struct poptOption popt_options[] = {
44         POPT_AUTOHELP
45         { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
46         { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
47         { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
48         { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
49         { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
50         { "trace",     0,   POPT_ARG_NONE, &options.tracing, 0, "enable tracing", NULL },
51         { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
52         { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
53         { "modules-path", 0, POPT_ARG_STRING, &options.modules_path, 0, "modules path", "PATH" },
54         { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
55         { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
56         { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "(|(objectClass=*)(distinguishedName=*))", NULL },
57         { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
58         { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
59         { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
60         { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
61         { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
62         { "controls", 0, POPT_ARG_STRING, NULL, 'c', "controls", NULL },
63         { "show-binary", 0, POPT_ARG_NONE, &options.show_binary, 0, "display binary LDIF", NULL },
64         { "paged", 0, POPT_ARG_NONE, NULL, 'P', "use a paged search", NULL },
65         { "show-deleted", 0, POPT_ARG_NONE, NULL, 'D', "show deleted objects", NULL },
66         { "show-recycled", 0, POPT_ARG_NONE, NULL, 'R', "show recycled objects", NULL },
67         { "show-deactivated-link", 0, POPT_ARG_NONE, NULL, 'd', "show deactivated links", NULL },
68         { "reveal", 0, POPT_ARG_NONE, NULL, 'r', "reveal ldb internals", NULL },
69         { "cross-ncs", 0, POPT_ARG_NONE, NULL, 'N', "search across NC boundaries", NULL },
70         { "extended-dn", 0, POPT_ARG_NONE, NULL, 'E', "show extended DNs", NULL },
71 #if (_SAMBA_BUILD_ >= 4)
72         POPT_COMMON_SAMBA
73         POPT_COMMON_CREDENTIALS
74         POPT_COMMON_CONNECTION
75         POPT_COMMON_VERSION
76 #endif
77         { NULL }
78 };
79
80 void ldb_cmdline_help(const char *cmdname, FILE *f)
81 {
82         poptContext pc;
83         pc = poptGetContext(cmdname, 0, NULL, popt_options, 
84                             POPT_CONTEXT_KEEP_FIRST);
85         poptPrintHelp(pc, f, 0);
86 }
87
88 /*
89   add a control to the options structure
90  */
91 static bool add_control(TALLOC_CTX *mem_ctx, const char *control)
92 {
93         int i;
94
95         /* count how many controls we already have */
96         for (i=0; options.controls && options.controls[i]; i++) ;
97
98         options.controls = talloc_realloc(mem_ctx, options.controls, const char *, i + 2);
99         if (options.controls == NULL) {
100                 return false;
101         }
102         options.controls[i] = control;
103         options.controls[i+1] = NULL;
104         return true;
105 }
106
107 /**
108   process command line options
109 */
110 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, 
111                                         int argc, const char **argv,
112                                         void (*usage)(void))
113 {
114         struct ldb_cmdline *ret=NULL;
115         poptContext pc;
116 #if (_SAMBA_BUILD_ >= 4)
117         int r;
118 #endif
119         int num_options = 0;
120         int opt;
121         int flags = 0;
122
123 #if (_SAMBA_BUILD_ >= 4)
124         r = ldb_register_samba_handlers(ldb);
125         if (r != 0) {
126                 goto failed;
127         }
128
129 #endif
130
131         /* make the ldb utilities line buffered */
132         setlinebuf(stdout);
133
134         ret = talloc_zero(ldb, struct ldb_cmdline);
135         if (ret == NULL) {
136                 fprintf(stderr, "Out of memory!\n");
137                 goto failed;
138         }
139
140         options = *ret;
141         
142         /* pull in URL */
143         options.url = getenv("LDB_URL");
144
145         /* and editor (used by ldbedit) */
146         options.editor = getenv("VISUAL");
147         if (!options.editor) {
148                 options.editor = getenv("EDITOR");
149         }
150         if (!options.editor) {
151                 options.editor = "vi";
152         }
153
154         options.scope = LDB_SCOPE_DEFAULT;
155
156         pc = poptGetContext(argv[0], argc, argv, popt_options, 
157                             POPT_CONTEXT_KEEP_FIRST);
158
159         while((opt = poptGetNextOpt(pc)) != -1) {
160                 switch (opt) {
161                 case 's': {
162                         const char *arg = poptGetOptArg(pc);
163                         if (strcmp(arg, "base") == 0) {
164                                 options.scope = LDB_SCOPE_BASE;
165                         } else if (strcmp(arg, "sub") == 0) {
166                                 options.scope = LDB_SCOPE_SUBTREE;
167                         } else if (strcmp(arg, "one") == 0) {
168                                 options.scope = LDB_SCOPE_ONELEVEL;
169                         } else {
170                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
171                                 goto failed;
172                         }
173                         break;
174                 }
175
176                 case 'v':
177                         options.verbose++;
178                         break;
179
180                 case 'o':
181                         options.options = talloc_realloc(ret, options.options, 
182                                                          const char *, num_options+3);
183                         if (options.options == NULL) {
184                                 fprintf(stderr, "Out of memory!\n");
185                                 goto failed;
186                         }
187                         options.options[num_options] = poptGetOptArg(pc);
188                         options.options[num_options+1] = NULL;
189                         num_options++;
190                         break;
191
192                 case 'c': {
193                         const char *cs = poptGetOptArg(pc);
194                         const char *p;
195
196                         for (p = cs; p != NULL; ) {
197                                 const char *t, *c;
198
199                                 t = strchr(p, ',');
200                                 if (t == NULL) {
201                                         c = talloc_strdup(options.controls, p);
202                                         p = NULL;
203                                 } else {
204                                         c = talloc_strndup(options.controls, p, t-p);
205                                         p = t + 1;
206                                 }
207                                 if (c == NULL || !add_control(ret, c)) {
208                                         fprintf(stderr, __location__ ": out of memory\n");
209                                         goto failed;
210                                 }
211                         }
212
213                         break;    
214                 }
215                 case 'P':
216                         if (!add_control(ret, "paged_results:1:1024")) {
217                                 fprintf(stderr, __location__ ": out of memory\n");
218                                 goto failed;
219                         }
220                         break;
221                 case 'D':
222                         if (!add_control(ret, "show_deleted:1")) {
223                                 fprintf(stderr, __location__ ": out of memory\n");
224                                 goto failed;
225                         }
226                         break;
227                 case 'R':
228                         if (!add_control(ret, "show_recycled:0")) {
229                                 fprintf(stderr, __location__ ": out of memory\n");
230                                 goto failed;
231                         }
232                         break;
233                 case 'd':
234                         if (!add_control(ret, "show_deactivated_link:0")) {
235                                 fprintf(stderr, __location__ ": out of memory\n");
236                                 goto failed;
237                         }
238                         break;
239                 case 'r':
240                         if (!add_control(ret, "reveal_internals:0")) {
241                                 fprintf(stderr, __location__ ": out of memory\n");
242                                 goto failed;
243                         }
244                         break;
245                 case 'N':
246                         if (!add_control(ret, "search_options:1:2")) {
247                                 fprintf(stderr, __location__ ": out of memory\n");
248                                 goto failed;
249                         }
250                         break;
251                 case 'E':
252                         if (!add_control(ret, "extended_dn:1:1")) {
253                                 fprintf(stderr, __location__ ": out of memory\n");
254                                 goto failed;
255                         }
256                         break;
257                 default:
258                         fprintf(stderr, "Invalid option %s: %s\n", 
259                                 poptBadOption(pc, 0), poptStrerror(opt));
260                         if (usage) usage();
261                         goto failed;
262                 }
263         }
264
265         /* setup the remaining options for the main program to use */
266         options.argv = poptGetArgs(pc);
267         if (options.argv) {
268                 options.argv++;
269                 while (options.argv[options.argc]) options.argc++;
270         }
271
272         *ret = options;
273
274         /* all utils need some option */
275         if (ret->url == NULL) {
276                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
277                 if (usage) usage();
278                 goto failed;
279         }
280
281         if (strcmp(ret->url, "NONE") == 0) {
282                 return ret;
283         }
284
285         if (options.nosync) {
286                 flags |= LDB_FLG_NOSYNC;
287         }
288
289         if (options.show_binary) {
290                 flags |= LDB_FLG_SHOW_BINARY;
291         }
292
293         if (options.tracing) {
294                 flags |= LDB_FLG_ENABLE_TRACING;
295         }
296
297 #if (_SAMBA_BUILD_ >= 4)
298         /* Must be after we have processed command line options */
299         gensec_init(cmdline_lp_ctx); 
300         
301         if (ldb_set_opaque(ldb, "sessionInfo", system_session(cmdline_lp_ctx))) {
302                 goto failed;
303         }
304         if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
305                 goto failed;
306         }
307         if (ldb_set_opaque(ldb, "loadparm", cmdline_lp_ctx)) {
308                 goto failed;
309         }
310
311         ldb_set_utf8_fns(ldb, NULL, wrap_casefold);
312 #endif
313
314         if (options.modules_path != NULL) {
315                 ldb_set_modules_dir(ldb, options.modules_path);
316         } else if (getenv("LDB_MODULES_PATH") != NULL) {
317                 ldb_set_modules_dir(ldb, getenv("LDB_MODULES_PATH"));
318         }
319
320         /* now connect to the ldb */
321         if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
322                 fprintf(stderr, "Failed to connect to %s - %s\n", 
323                         ret->url, ldb_errstring(ldb));
324                 goto failed;
325         }
326
327 #if (_SAMBA_BUILD_ >= 4)
328         /* get the domain SID into the cache for SDDL processing */
329         samdb_domain_sid(ldb);
330 #endif
331
332         return ret;
333
334 failed:
335         talloc_free(ret);
336         exit(1);
337         return NULL;
338 }
339
340 /* this function check controls reply and determines if more
341  * processing is needed setting up the request controls correctly
342  *
343  * returns:
344  *      -1 error
345  *      0 all ok
346  *      1 all ok, more processing required
347  */
348 int handle_controls_reply(struct ldb_control **reply, struct ldb_control **request)
349 {
350         unsigned int i, j;
351         int ret = 0;
352
353         if (reply == NULL || request == NULL) return -1;
354         
355         for (i = 0; reply[i]; i++) {
356                 if (strcmp(LDB_CONTROL_VLV_RESP_OID, reply[i]->oid) == 0) {
357                         struct ldb_vlv_resp_control *rep_control;
358
359                         rep_control = talloc_get_type(reply[i]->data, struct ldb_vlv_resp_control);
360                         
361                         /* check we have a matching control in the request */
362                         for (j = 0; request[j]; j++) {
363                                 if (strcmp(LDB_CONTROL_VLV_REQ_OID, request[j]->oid) == 0)
364                                         break;
365                         }
366                         if (! request[j]) {
367                                 fprintf(stderr, "Warning VLV reply received but no request have been made\n");
368                                 continue;
369                         }
370
371                         /* check the result */
372                         if (rep_control->vlv_result != 0) {
373                                 fprintf(stderr, "Warning: VLV not performed with error: %d\n", rep_control->vlv_result);
374                         } else {
375                                 fprintf(stderr, "VLV Info: target position = %d, content count = %d\n", rep_control->targetPosition, rep_control->contentCount);
376                         }
377
378                         continue;
379                 }
380
381                 if (strcmp(LDB_CONTROL_ASQ_OID, reply[i]->oid) == 0) {
382                         struct ldb_asq_control *rep_control;
383
384                         rep_control = talloc_get_type(reply[i]->data, struct ldb_asq_control);
385
386                         /* check the result */
387                         if (rep_control->result != 0) {
388                                 fprintf(stderr, "Warning: ASQ not performed with error: %d\n", rep_control->result);
389                         }
390
391                         continue;
392                 }
393
394                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, reply[i]->oid) == 0) {
395                         struct ldb_paged_control *rep_control, *req_control;
396
397                         rep_control = talloc_get_type(reply[i]->data, struct ldb_paged_control);
398                         if (rep_control->cookie_len == 0) /* we are done */
399                                 break;
400
401                         /* more processing required */
402                         /* let's fill in the request control with the new cookie */
403
404                         for (j = 0; request[j]; j++) {
405                                 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, request[j]->oid) == 0)
406                                         break;
407                         }
408                         /* if there's a reply control we must find a request
409                          * control matching it */
410                         if (! request[j]) return -1;
411
412                         req_control = talloc_get_type(request[j]->data, struct ldb_paged_control);
413
414                         if (req_control->cookie)
415                                 talloc_free(req_control->cookie);
416                         req_control->cookie = (char *)talloc_memdup(
417                                 req_control, rep_control->cookie,
418                                 rep_control->cookie_len);
419                         req_control->cookie_len = rep_control->cookie_len;
420
421                         ret = 1;
422
423                         continue;
424                 }
425
426                 if (strcmp(LDB_CONTROL_SORT_RESP_OID, reply[i]->oid) == 0) {
427                         struct ldb_sort_resp_control *rep_control;
428
429                         rep_control = talloc_get_type(reply[i]->data, struct ldb_sort_resp_control);
430
431                         /* check we have a matching control in the request */
432                         for (j = 0; request[j]; j++) {
433                                 if (strcmp(LDB_CONTROL_SERVER_SORT_OID, request[j]->oid) == 0)
434                                         break;
435                         }
436                         if (! request[j]) {
437                                 fprintf(stderr, "Warning Server Sort reply received but no request found\n");
438                                 continue;
439                         }
440
441                         /* check the result */
442                         if (rep_control->result != 0) {
443                                 fprintf(stderr, "Warning: Sorting not performed with error: %d\n", rep_control->result);
444                         }
445
446                         continue;
447                 }
448
449                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, reply[i]->oid) == 0) {
450                         struct ldb_dirsync_control *rep_control, *req_control;
451                         char *cookie;
452
453                         rep_control = talloc_get_type(reply[i]->data, struct ldb_dirsync_control);
454                         if (rep_control->cookie_len == 0) /* we are done */
455                                 break;
456
457                         /* more processing required */
458                         /* let's fill in the request control with the new cookie */
459
460                         for (j = 0; request[j]; j++) {
461                                 if (strcmp(LDB_CONTROL_DIRSYNC_OID, request[j]->oid) == 0)
462                                         break;
463                         }
464                         /* if there's a reply control we must find a request
465                          * control matching it */
466                         if (! request[j]) return -1;
467
468                         req_control = talloc_get_type(request[j]->data, struct ldb_dirsync_control);
469
470                         if (req_control->cookie)
471                                 talloc_free(req_control->cookie);
472                         req_control->cookie = (char *)talloc_memdup(
473                                 req_control, rep_control->cookie,
474                                 rep_control->cookie_len);
475                         req_control->cookie_len = rep_control->cookie_len;
476
477                         cookie = ldb_base64_encode(req_control, rep_control->cookie, rep_control->cookie_len);
478                         printf("# DIRSYNC cookie returned was:\n# %s\n", cookie);
479
480                         continue;
481                 }
482
483                 /* no controls matched, throw a warning */
484                 fprintf(stderr, "Unknown reply control oid: %s\n", reply[i]->oid);
485         }
486
487         return ret;
488 }
489