]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
ITS#1716 is_entry_subentr/ies/y/
[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         rc = slap_init( mode, progname );
185
186         if (rc != 0 ) {
187                 fprintf( stderr, "%s: slap_init failed!\n", progname );
188                 exit( EXIT_FAILURE );
189         }
190
191         rc = slap_schema_init();
192
193         if (rc != 0 ) {
194                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
195                 exit( EXIT_FAILURE );
196         }
197
198         read_config( conffile );
199
200         if ( !nbackends ) {
201                 fprintf( stderr, "No databases found in config file\n" );
202                 exit( EXIT_FAILURE );
203         }
204
205         rc = glue_sub_init();
206
207         if (rc != 0 ) {
208                 fprintf( stderr, "Subordinate configuration error\n" );
209                 exit( EXIT_FAILURE );
210         }
211
212         rc = slap_schema_check();
213
214         if (rc != 0 ) {
215                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
216                 exit( EXIT_FAILURE );
217         }
218
219         if( base.bv_val != NULL ) {
220                 struct berval *nbase = NULL;
221
222                 rc = dnNormalize( NULL, &base, &nbase );
223                 if( rc != LDAP_SUCCESS ) {
224                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
225                                 progname, base.bv_val );
226                         exit( EXIT_FAILURE );
227                 }
228
229                 be = select_backend( nbase, 0, 0 );
230                 ber_bvfree( nbase );
231
232                 if( be == NULL ) {
233                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
234                                 progname, base.bv_val );
235                         exit( EXIT_FAILURE );
236                 }
237                 /* If the named base is a glue master, operate on the
238                  * entire context
239                  */
240                 if (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE)
241                         nosubordinates = 1;
242
243         } else if ( dbnum == -1 ) {
244                 be = &backends[dbnum=0];
245                 /* If just doing the first by default and it is a
246                  * glue subordinate, find the master.
247                  */
248                 while (be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
249                         nosubordinates = 1;
250                         be++;
251                 }
252
253         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
254                 fprintf( stderr,
255                         "Database number selected via -n is out of range\n"
256                         "Must be in the range 1 to %d"
257                                 " (number of databases in the config file)\n",
258                         nbackends );
259                 exit( EXIT_FAILURE );
260
261         } else {
262                 be = &backends[dbnum];
263         }
264
265 #ifdef CSRIMALLOC
266         mal_leaktrace(1);
267 #endif
268
269         slap_startup( be );
270 }
271
272 void slap_tool_destroy( void )
273 {
274         slap_shutdown( be );
275         slap_destroy();
276
277 #ifdef CSRIMALLOC
278         mal_dumpleaktrace( leakfile );
279 #endif
280 }