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