]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
change due to removal of servers/slapd/lcup.c
[openldap] / servers / slapd / tools / slapcommon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* slapcommon.c - common routine for the slap tools */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13 #include <ac/ctype.h>
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/unistd.h>
17
18 #include "slapcommon.h"
19 #include "lutil.h"
20
21
22 char    *progname       = NULL;
23 char    *conffile       = SLAPD_DEFAULT_CONFIGFILE;
24 int             truncatemode = 0;
25 int             verbose         = 0;
26 int             continuemode = 0;
27 int             nosubordinates = 0;
28 int             dryrun = 0;
29
30 char    *ldiffile       = NULL;
31 FILE    *ldiffp         = NULL;
32
33 #ifdef CSRIMALLOC
34         char *leakfilename;
35         FILE *leakfile;
36 #endif
37
38 Backend *be = NULL;
39
40 static void
41 usage( int tool )
42 {
43         char *options = NULL;
44         fprintf( stderr,
45                 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
46                         "\t[-n databasenumber | -b suffix]", progname );
47
48         switch( tool ) {
49         case SLAPADD:
50                 options = "\t[-l ldiffile]\n";
51                 break;
52
53         case SLAPCAT:
54                 options = "\t[-l ldiffile]\n";
55                 break;
56
57         case SLAPINDEX:
58                 options = "\n";
59                 break;
60         }
61
62         if( options != NULL ) {
63                 fputs( options, stderr );
64         }
65         exit( EXIT_FAILURE );
66 }
67
68
69 /*
70  * slap_tool_init - initialize slap utility, handle program options.
71  * arguments:
72  *      name            program name
73  *      tool            tool code
74  *      argc, argv      command line arguments
75  */
76
77 void
78 slap_tool_init(
79         const char* name,
80         int tool,
81         int argc, char **argv )
82 {
83         char *options;
84         struct berval base = { 0, NULL };
85         int rc, i, dbnum;
86         int mode = SLAP_TOOL_MODE;
87
88         progname = lutil_progname( name, argc, argv );
89
90 #ifdef CSRIMALLOC
91         leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
92         sprintf( leakfilename, "%s.leak", progname );
93         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
94                 leakfile = stderr;
95         }
96         free( leakfilename );
97 #endif
98
99         switch( tool ) {
100         case SLAPADD:
101                 options = "b:cd:f:l:n:tuv";
102                 break;
103
104         case SLAPINDEX:
105                 options = "b:cd:f:n:v";
106                 break;
107
108         case SLAPCAT:
109                 options = "b:cd:f:l:n:v";
110                 break;
111
112         default:
113                 fprintf( stderr, "%s: unknown tool mode (%d)\n",
114                          progname, tool );
115                 exit( EXIT_FAILURE );
116         }
117
118         ldiffile = NULL;
119         conffile = SLAPD_DEFAULT_CONFIGFILE;
120         dbnum = -1;
121         while ( (i = getopt( argc, argv, options )) != EOF ) {
122                 switch ( i ) {
123                 case 'b':
124                         base.bv_val = strdup( optarg );
125                         base.bv_len = strlen( base.bv_val );
126
127                 case 'c':       /* enable continue mode */
128                         continuemode++;
129                         break;
130
131                 case 'd':       /* turn on debugging */
132                         ldap_debug += atoi( optarg );
133                         break;
134
135                 case 'f':       /* specify a conf file */
136                         conffile = strdup( optarg );
137                         break;
138
139                 case 'l':       /* LDIF file */
140                         ldiffile = strdup( optarg );
141                         break;
142
143                 case 'n':       /* which config file db to index */
144                         dbnum = atoi( optarg ) - 1;
145                         break;
146
147                 case 't':       /* turn on truncate */
148                         truncatemode++;
149                         mode |= SLAP_TRUNCATE_MODE;
150                         break;
151
152                 case 'u':       /* dry run */
153                         dryrun++;
154                         break;
155
156                 case 'v':       /* turn on verbose */
157                         verbose++;
158                         break;
159
160                 default:
161                         usage( tool );
162                         break;
163                 }
164         }
165
166         if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
167                 usage( tool );
168         }
169
170         if ( ldiffile == NULL ) {
171                 ldiffp = tool == SLAPCAT ? stdout : stdin;
172
173         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
174                 == NULL )
175         {
176                 perror( ldiffile );
177                 exit( EXIT_FAILURE );
178         }
179
180         /*
181          * initialize stuff and figure out which backend we're dealing with
182          */
183
184 #ifdef SLAPD_MODULES
185         if ( module_init() != 0 ) {
186                 fprintf( stderr, "%s: module_init failed!\n", progname );
187                 exit( EXIT_FAILURE );
188         }
189 #endif
190                 
191         rc = slap_init( mode, progname );
192
193         if (rc != 0 ) {
194                 fprintf( stderr, "%s: slap_init failed!\n", progname );
195                 exit( EXIT_FAILURE );
196         }
197
198         rc = slap_schema_init();
199
200         if (rc != 0 ) {
201                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
202                 exit( EXIT_FAILURE );
203         }
204
205         rc = read_config( conffile, 0 );
206
207         if ( rc != 0 ) {
208                 fprintf( stderr, "%s: bad configuration file!\n", progname );
209                 exit( EXIT_FAILURE );
210         }
211
212         if ( !nbackends ) {
213                 fprintf( stderr, "No databases found in config file\n" );
214                 exit( EXIT_FAILURE );
215         }
216
217         rc = glue_sub_init();
218
219         if (rc != 0 ) {
220                 fprintf( stderr, "Subordinate configuration error\n" );
221                 exit( EXIT_FAILURE );
222         }
223
224         rc = slap_schema_check();
225
226         if (rc != 0 ) {
227                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
228                 exit( EXIT_FAILURE );
229         }
230
231         if( base.bv_val != NULL ) {
232                 struct berval *nbase = NULL;
233
234                 rc = dnNormalize( NULL, &base, &nbase );
235                 if( rc != LDAP_SUCCESS ) {
236                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
237                                 progname, base.bv_val );
238                         exit( EXIT_FAILURE );
239                 }
240
241                 be = select_backend( nbase, 0, 0 );
242                 ber_bvfree( nbase );
243
244                 if( be == NULL ) {
245                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
246                                 progname, base.bv_val );
247                         exit( EXIT_FAILURE );
248                 }
249                 /* If the named base is a glue master, operate on the
250                  * entire context
251                  */
252                 if (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE)
253                         nosubordinates = 1;
254
255         } else if ( dbnum == -1 ) {
256                 be = &backends[dbnum=0];
257                 /* If just doing the first by default and it is a
258                  * glue subordinate, find the master.
259                  */
260                 while (be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
261                         nosubordinates = 1;
262                         be++;
263                 }
264
265         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
266                 fprintf( stderr,
267                         "Database number selected via -n is out of range\n"
268                         "Must be in the range 1 to %d"
269                                 " (number of databases in the config file)\n",
270                         nbackends );
271                 exit( EXIT_FAILURE );
272
273         } else {
274                 be = &backends[dbnum];
275         }
276
277 #ifdef CSRIMALLOC
278         mal_leaktrace(1);
279 #endif
280
281         slap_startup( be );
282 }
283
284 void slap_tool_destroy( void )
285 {
286         slap_shutdown( be );
287         slap_destroy();
288 #ifdef SLAPD_MODULES
289         if ( slapMode == SLAP_SERVER_MODE ) {
290         /* always false. just pulls in necessary symbol references. */
291                 lutil_uuidstr(NULL, 0);
292         }
293         module_kill();
294 #endif
295         schema_destroy();
296 #ifdef HAVE_TLS
297         ldap_pvt_tls_destroy();
298 #endif
299         config_destroy();
300
301 #ifdef CSRIMALLOC
302         mal_dumpleaktrace( leakfile );
303 #endif
304 }