r7804: added the samba specific ldif handlers into the tree, but don't enable
[sfrench/samba-autobuild/.git] / source / 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/ldb.h"
27 #include "ldb/include/ldb_private.h"
28 #include "ldb/tools/cmdline.h"
29 #ifdef _SAMBA_BUILD_
30 #include "lib/cmdline/popt_common.h"
31 #endif
32
33 /*
34   process command line options
35 */
36 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
37                                         void (*usage)(void))
38 {
39         struct ldb_cmdline options, *ret=NULL;
40         poptContext pc;
41         int r, num_options = 0;
42         char opt;
43         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                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
51                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
52                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
53                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
54                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "dn=*", NULL },
55                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
56                 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
57                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
58 #ifdef _SAMBA_BUILD_
59                 POPT_COMMON_SAMBA
60                 POPT_COMMON_CREDENTIALS
61                 POPT_COMMON_VERSION
62 #endif
63                 POPT_TABLEEND
64         };
65
66 #ifdef _SAMBA_BUILD_
67         ldbsearch_init_subsystems;
68         r = ldb_register_samba_handlers(ldb);
69         if (r != 0) {
70                 goto failed;
71         }
72 #endif
73
74         ret = talloc_zero(ldb, struct ldb_cmdline);
75         if (ret == NULL) {
76                 ldb_oom(ldb);
77                 goto failed;
78         }
79
80         options = *ret;
81         
82         /* pull in URL */
83         options.url = getenv("LDB_URL");
84
85         /* and editor (used by ldbedit) */
86         options.editor = getenv("VISUAL");
87         if (!options.editor) {
88                 options.editor = getenv("EDITOR");
89         }
90         if (!options.editor) {
91                 options.editor = "vi";
92         }
93
94         options.scope = LDB_SCOPE_DEFAULT;
95
96         pc = poptGetContext(argv[0], argc, argv, popt_options, 
97                             POPT_CONTEXT_KEEP_FIRST);
98
99         while((opt = poptGetNextOpt(pc)) != -1) {
100                 switch (opt) {
101                 case 's': {
102                         const char *arg = poptGetOptArg(pc);
103                         if (strcmp(arg, "base") == 0) {
104                                 options.scope = LDB_SCOPE_BASE;
105                         } else if (strcmp(arg, "sub") == 0) {
106                                 options.scope = LDB_SCOPE_SUBTREE;
107                         } else if (strcmp(arg, "one") == 0) {
108                                 options.scope = LDB_SCOPE_ONELEVEL;
109                         } else {
110                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
111                                 goto failed;
112                         }
113                         break;
114                 }
115
116                 case 'v':
117                         options.verbose++;
118                         break;
119
120                 case 'o':
121                         options.options = talloc_realloc(ret, options.options, 
122                                                          const char *, num_options+2);
123                         if (options.options == NULL) {
124                                 ldb_oom(ldb);
125                                 goto failed;
126                         }
127                         options.options[num_options++] = poptGetOptArg(pc);
128                         options.options[num_options+1] = NULL;
129                         break;
130                         
131                 default:
132                         fprintf(stderr, "Invalid option %s: %s\n", 
133                                 poptBadOption(pc, 0), poptStrerror(opt));
134                         if (usage) usage();
135                         goto failed;
136                 }
137         }
138
139         /* setup the remaining options for the main program to use */
140         options.argv = poptGetArgs(pc);
141         if (options.argv) {
142                 options.argv++;
143                 while (options.argv[options.argc]) options.argc++;
144         }
145
146         *ret = options;
147
148         /* all utils need some option */
149         if (ret->url == NULL) {
150                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
151                 if (usage) usage();
152                 goto failed;
153         }
154
155         if (ldb_connect(ldb, ret->url, 0, ret->options) != 0) {
156                 fprintf(stderr, "Failed to connect to %s - %s\n", 
157                         ret->url, ldb_errstring(ldb));
158                 goto failed;
159         }
160
161         return ret;
162
163 failed:
164         talloc_free(ret);
165         exit(1);
166         return NULL;
167 }