r12499: Move smb_build.h out of includes.h
[ab/samba.git/.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 "smb_build.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         ldbsearch_init_subsystems;
75         r = ldb_register_samba_handlers(ldb);
76         if (r != 0) {
77                 goto failed;
78         }
79
80 #endif
81
82         ret = talloc_zero(ldb, struct ldb_cmdline);
83         if (ret == NULL) {
84                 ldb_oom(ldb);
85                 goto failed;
86         }
87
88         options = *ret;
89         
90         /* pull in URL */
91         options.url = getenv("LDB_URL");
92
93         /* and editor (used by ldbedit) */
94         options.editor = getenv("VISUAL");
95         if (!options.editor) {
96                 options.editor = getenv("EDITOR");
97         }
98         if (!options.editor) {
99                 options.editor = "vi";
100         }
101
102         options.scope = LDB_SCOPE_DEFAULT;
103
104         pc = poptGetContext(argv[0], argc, argv, popt_options, 
105                             POPT_CONTEXT_KEEP_FIRST);
106
107         while((opt = poptGetNextOpt(pc)) != -1) {
108                 switch (opt) {
109                 case 's': {
110                         const char *arg = poptGetOptArg(pc);
111                         if (strcmp(arg, "base") == 0) {
112                                 options.scope = LDB_SCOPE_BASE;
113                         } else if (strcmp(arg, "sub") == 0) {
114                                 options.scope = LDB_SCOPE_SUBTREE;
115                         } else if (strcmp(arg, "one") == 0) {
116                                 options.scope = LDB_SCOPE_ONELEVEL;
117                         } else {
118                                 fprintf(stderr, "Invalid scope '%s'\n", arg);
119                                 goto failed;
120                         }
121                         break;
122                 }
123
124                 case 'v':
125                         options.verbose++;
126                         break;
127
128                 case 'o':
129                         options.options = talloc_realloc(ret, options.options, 
130                                                          const char *, num_options+3);
131                         if (options.options == NULL) {
132                                 ldb_oom(ldb);
133                                 goto failed;
134                         }
135                         options.options[num_options] = poptGetOptArg(pc);
136                         options.options[num_options+1] = NULL;
137                         num_options++;
138                         break;
139                         
140                 default:
141                         fprintf(stderr, "Invalid option %s: %s\n", 
142                                 poptBadOption(pc, 0), poptStrerror(opt));
143                         if (usage) usage();
144                         goto failed;
145                 }
146         }
147
148         /* setup the remaining options for the main program to use */
149         options.argv = poptGetArgs(pc);
150         if (options.argv) {
151                 options.argv++;
152                 while (options.argv[options.argc]) options.argc++;
153         }
154
155         *ret = options;
156
157         /* all utils need some option */
158         if (ret->url == NULL) {
159                 fprintf(stderr, "You must supply a url with -H or with $LDB_URL\n");
160                 if (usage) usage();
161                 goto failed;
162         }
163
164         if (strcmp(ret->url, "NONE") != 0) {
165                 int flags = 0;
166                 if (options.nosync) {
167                         flags |= LDB_FLG_NOSYNC;
168                 }
169
170 #ifdef _SAMBA_BUILD_
171                 if (ldb_set_opaque(ldb, "sessionInfo", system_session(ldb))) {
172                         goto failed;
173                 }
174                 if (ldb_set_opaque(ldb, "credentials", cmdline_credentials)) {
175                         goto failed;
176                 }
177 #endif
178                 if (ldb_connect(ldb, ret->url, flags, ret->options) != 0) {
179                         fprintf(stderr, "Failed to connect to %s - %s\n", 
180                                 ret->url, ldb_errstring(ldb));
181                         goto failed;
182                 }
183         }
184
185         return ret;
186
187 failed:
188         talloc_free(ret);
189         exit(1);
190         return NULL;
191 }