]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
3143550611ceee2d0502344d11e19d8b5162ec5e
[openldap] / servers / slapd / tools / slapcommon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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     nosubs          = 0;
28
29 char    *ldiffile       = NULL;
30 FILE    *ldiffp         = NULL;
31
32 #ifdef CSRIMALLOC
33         char *leakfilename;
34         FILE *leakfile;
35 #endif
36
37 Backend *be = NULL;
38
39 static void
40 usage( int tool )
41 {
42         char *options = NULL;
43         fprintf( stderr,
44                 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
45                         "\t[-n databasenumber | -b suffix]", progname );
46
47         switch( tool ) {
48         case SLAPADD:
49                 options = "\t[-l ldiffile]\n";
50                 break;
51
52         case SLAPCAT:
53                 options = "\t[-l ldiffile]\n";
54                 break;
55
56         case SLAPINDEX:
57                 options = "\n";
58                 break;
59         }
60
61         if( options != NULL ) {
62                 fputs( options, stderr );
63         }
64         exit( EXIT_FAILURE );
65 }
66
67
68 /*
69  * slap_tool_init - initialize slap utility, handle program options.
70  * arguments:
71  *      name            program name
72  *      tool            tool code
73  *      argc, argv      command line arguments
74  */
75
76 void
77 slap_tool_init(
78         const char* name,
79         int tool,
80         int argc, char **argv )
81 {
82         char *options;
83         char *base = NULL;
84         int rc, i, dbnum;
85         int mode = SLAP_TOOL_MODE;
86
87         progname = lutil_progname( name, argc, argv );
88
89 #ifdef CSRIMALLOC
90         leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
91         sprintf( leakfilename, "%s.leak", progname );
92         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
93                 leakfile = stderr;
94         }
95         free( leakfilename );
96 #endif
97
98         switch( tool ) {
99         case SLAPADD:
100                 options = "b:cd:f:l:n:tv";
101                 break;
102
103         case SLAPINDEX:
104                 options = "b:cd:f:n:v";
105                 break;
106
107         case SLAPCAT:
108                 options = "b:cd:f:l:n:v";
109                 break;
110
111         default:
112                 fprintf( stderr, "%s: unknown tool mode (%d)\n",
113                          progname, tool );
114                 exit( EXIT_FAILURE );
115         }
116
117         ldiffile = NULL;
118         conffile = SLAPD_DEFAULT_CONFIGFILE;
119         dbnum = -1;
120         while ( (i = getopt( argc, argv, options )) != EOF ) {
121                 switch ( i ) {
122                 case 'b':
123                         base = strdup( optarg );
124
125                 case 'c':       /* enable continue mode */
126                         continuemode++;
127                         break;
128
129                 case 'd':       /* turn on debugging */
130                         ldap_debug += atoi( optarg );
131                         break;
132
133                 case 'f':       /* specify a conf file */
134                         conffile = strdup( optarg );
135                         break;
136
137                 case 'l':       /* LDIF file */
138                         ldiffile = strdup( optarg );
139                         break;
140
141                 case 'n':       /* which config file db to index */
142                         dbnum = atoi( optarg ) - 1;
143                         break;
144
145                 case 't':       /* turn on truncate */
146                         truncatemode++;
147                         mode |= SLAP_TRUNCATE_MODE;
148                         break;
149
150                 case 'v':       /* turn on verbose */
151                         verbose++;
152                         break;
153
154                 default:
155                         usage( tool );
156                         break;
157                 }
158         }
159
160         if ( ( argc != optind ) || (dbnum >= 0 && base != NULL ) ) {
161                 usage( tool );
162         }
163
164         if ( ldiffile == NULL ) {
165                 ldiffp = tool == SLAPCAT ? stdout : stdin;
166
167         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
168                 == NULL )
169         {
170                 perror( ldiffile );
171                 exit( EXIT_FAILURE );
172         }
173
174         /*
175          * initialize stuff and figure out which backend we're dealing with
176          */
177
178         rc = slap_init( mode, progname );
179
180         if (rc != 0 ) {
181                 fprintf( stderr, "%s: slap_init failed!\n", progname );
182                 exit( EXIT_FAILURE );
183         }
184
185         rc = schema_init();
186
187         if (rc != 0 ) {
188                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
189                 exit( EXIT_FAILURE );
190         }
191
192         read_config( conffile );
193
194         if ( !nbackends ) {
195                 fprintf( stderr, "No databases found in config file\n" );
196                 exit( EXIT_FAILURE );
197         }
198
199         rc = glue_sub_init();
200
201         if (rc != 0 ) {
202                 fprintf( stderr, "Subordinate configuration error\n" );
203                 exit( EXIT_FAILURE );
204         }
205
206         rc = schema_prep();
207
208         if (rc != 0 ) {
209                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
210                 exit( EXIT_FAILURE );
211         }
212
213         if( base != NULL ) {
214                 char *tbase = ch_strdup( base );
215
216                 if( dn_normalize( tbase ) == NULL ) {
217                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
218                                 progname, base );
219                         exit( EXIT_FAILURE );
220                 }
221
222                 be = select_backend( tbase, 0, 0 );
223                 free( tbase );
224
225                 if( be == NULL ) {
226                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
227                                 progname, base );
228                         exit( EXIT_FAILURE );
229                 }
230                 /* If the named base is a glue master, operate on the
231                  * entire context
232                  */
233                 if (be->be_glueflags & SLAP_GLUE_INSTANCE)
234                         nosubs = 1;
235
236         } else if ( dbnum == -1 ) {
237                 be = &backends[dbnum=0];
238                 /* If just doing the first by default and it is a
239                  * glue subordinate, find the master.
240                  */
241                 while (be->be_glueflags & SLAP_GLUE_SUBORDINATE) {
242                         nosubs = 1;
243                         be++;
244                 }
245
246         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
247                 fprintf( stderr,
248                         "Database number selected via -n is out of range\n"
249                         "Must be in the range 1 to %d (number of databases in the config file)\n",
250                         nbackends );
251                 exit( EXIT_FAILURE );
252
253         } else {
254                 be = &backends[dbnum];
255         }
256
257 #ifdef CSRIMALLOC
258         mal_leaktrace(1);
259 #endif
260
261         slap_startup( be );
262 }
263
264 void slap_tool_destroy( void )
265 {
266         slap_shutdown( be );
267         slap_destroy();
268
269 #ifdef CSRIMALLOC
270         mal_dumpleaktrace( leakfile );
271 #endif
272 }