r12620: Get rid of automatically generated lists of init functions of subsystems.
[metze/samba/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 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 #include "auth/auth.h"
32 #endif
33
34 /*
35   process command line options
36 */
37 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc, const char **argv,
38                                         void (*usage)(void))
39 {
40         struct ldb_cmdline options, *ret=NULL;
41         poptContext pc;
42 #ifdef _SAMBA_BUILD_
43         int r;
44 #endif
45         int num_options = 0;
46         int opt;
47         struct poptOption popt_options[] = {
48                 POPT_AUTOHELP
49                 { "url",       'H', POPT_ARG_STRING, &options.url, 0, "database URL", "URL" },
50                 { "basedn",    'b', POPT_ARG_STRING, &options.basedn, 0, "base DN", "DN" },
51                 { "editor",    'e', POPT_ARG_STRING, &options.editor, 0, "external editor", "PROGRAM" },
52                 { "scope",     's', POPT_ARG_STRING, NULL, 's', "search scope", "SCOPE" },
53                 { "verbose",   'v', POPT_ARG_NONE, NULL, 'v', "increase verbosity", NULL },
54                 { "interactive", 'i', POPT_ARG_NONE, &options.interactive, 0, "input from stdin", NULL },
55                 { "recursive", 'r', POPT_ARG_NONE, &options.recursive, 0, "recursive delete", NULL },
56                 { "num-searches", 0, POPT_ARG_INT, &options.num_searches, 0, "number of test searches", NULL },
57                 { "num-records", 0, POPT_ARG_INT, &options.num_records, 0, "number of test records", NULL },
58                 { "all", 'a',    POPT_ARG_NONE, &options.all_records, 0, "objectClass=*", NULL },
59                 { "nosync", 0,   POPT_ARG_NONE, &options.nosync, 0, "non-synchronous transactions", NULL },
60                 { "sorted", 'S', POPT_ARG_NONE, &options.sorted, 0, "sort attributes", NULL },
61                 { "sasl-mechanism", 0, POPT_ARG_STRING, &options.sasl_mechanism, 0, "choose SASL mechanism", "MECHANISM" },
62                 { "input", 'I', POPT_ARG_STRING, &options.input, 0, "Input File", "Input" },
63                 { "output", 'O', POPT_ARG_STRING, &options.output, 0, "Output File", "Output" },
64                 { NULL,    'o', POPT_ARG_STRING, NULL, 'o', "ldb_connect option", "OPTION" },
65 #ifdef _SAMBA_BUILD_
66                 POPT_COMMON_SAMBA
67                 POPT_COMMON_CREDENTIALS
68                 POPT_COMMON_VERSION
69 #endif
70                 POPT_TABLEEND
71         };
72
73 #ifdef _SAMBA_BUILD_
74         gensec_init(); 
75
76         r = ldb_register_samba_handlers(ldb);
77         if (r != 0) {
78                 goto failed;
79         }
80
81 #endif
82
83         ret = talloc_zero(ldb, struct ldb_cmdline);
84         if (ret == NULL) {
85                 ldb_oom(ldb);
86                 goto failed;
87         }
88
89         options = *ret;
90         
91         /* pull in URL */
92         options.url = getenv("LDB_URL");
93
94         /* and editor (used by ldbedit) */
95         options.editor = getenv("VISUAL");
96         if (!options.editor) {
97                 options.editor = getenv("EDITOR");
98         }
99         if (!options.editor) {
100                 options.editor = "vi";
101         }
102
103         options.scope = LDB_SCOPE_DEFAULT;
104
105         pc = poptGetContext(argv[0], argc, argv, popt_options, 
106                             POPT_CONTEXT_KEEP_FIRST);
107
108         while((opt = poptGetNextOpt(pc)) != -1) {
109                 switch (opt) {
110                 case 's': {
111                         const char *arg = poptGetOptArg(pc);
112                         if (strcmp(arg, "base") == 0) {
113                                 options.scope = LDB_SCOPE_BASE;
114                         } else if (strcmp(arg, "sub") == 0) {
115                                 options.scope = LDB_SCOPE_SUBTREE;
116                         } else if (strcmp(arg, "one") == 0) {
117                                 options.scope = LDB_SCOPE_ONELEVEL;
118                         } else {
119                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
120                                 goto failed;
121                         }
122                         break;
123                 }
124
125                 case 'v':
126                         options.verbose++;
127                         break;
128
129                 case 'o':
130                         options.options = talloc_realloc(ret, options.options, 
131                                                          const char *, num_options+3);
132                         if (options.options == NULL) {
133                                 ldb_oom(ldb);
134                                 goto failed;
135                         }
136                         options.options[num_options] = poptGetOptArg(pc);
137                         options.options[num_options+1] = NULL;
138                         num_options++;
139                         break;
140                         
141                 default:
142                         fprintf(stderr, "Invalid option %s: %s\n", 
143                                 poptBadOption(pc, 0), poptStrerror(opt));
144                         if (usage) usage();
145                         goto failed;
146                 }
147         }
148
149         /* setup the remaining options for the main program to use */
150         options.argv = poptGetArgs(pc);
151         if (options.argv) {
152                 options.argv++;
153                 while (options.argv[options.argc]) options.argc++;
154         }
155
156         *ret = options;
157
158         /* all utils need some option */
159         if (ret->url == NULL) {
160                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
161                 if (usage) usage();
162                 goto failed;
163         }
164
165         if (strcmp(ret->url, "NONE") != 0) {
166                 int flags = 0;
167                 if (options.nosync) {
168                         flags |= LDB_FLG_NOSYNC;
169                 }
170
171 #ifdef _SAMBA_BUILD_
172                 if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
173                         goto failed;
174                 }
175                 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
176                         goto failed;
177                 }
178 #endif
179                 if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
180                         fprintf(stderr, "Failed to connect to %s - %s\n", 
181                                 ret->url, ldb_errstring(ldb));
182                         goto failed;
183                 }
184         }
185
186         return ret;
187
188 failed:
189         talloc_free(ret);
190         exit(1);
191         return NULL;
192 }