]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / servers / slapd / tools / slapcommon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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             noschemacheck = 0;
27 int             continuemode = 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[-s] [-l ldiffile]\n";
50                 break;
51
52         case SLAPCAT:
53                 options = "\t[-l ldiffile]\n";
54                 break;
55
56         case SLAPINDEX:
57                 options = "\tattributetype\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:stv";
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 's':       /* disable schema checking */
146                         noschemacheck++;
147                         break;
148
149                 case 't':       /* turn on truncate */
150                         truncatemode++;
151                         mode |= SLAP_TRUNCATE_MODE;
152                         break;
153
154                 case 'v':       /* turn on verbose */
155                         verbose++;
156                         break;
157
158                 default:
159                         usage( tool );
160                         break;
161                 }
162         }
163
164         if ( ( argc != optind + (tool == SLAPINDEX ? 1 : 0) )
165                 || (dbnum >= 0 && base != NULL ) )
166         {
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         read_config( conffile );
192
193         if ( !nbackends ) {
194                 fprintf( stderr, "No databases found in config file\n" );
195                 exit( EXIT_FAILURE );
196         }
197
198         if( base != NULL ) {
199                 char *tbase = ch_strdup( base );
200
201                 if( dn_normalize_case( tbase ) == NULL ) {
202                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
203                                 progname, base );
204                         exit( EXIT_FAILURE );
205                 }
206
207                 be = select_backend( tbase );
208                 free( tbase );
209
210                 if( be == NULL ) {
211                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
212                                 progname, base );
213                         exit( EXIT_FAILURE );
214                 }
215
216         } else if ( dbnum == -1 ) {
217                 be = &backends[dbnum=0];
218
219         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
220                 fprintf( stderr,
221                         "Database number selected via -n is out of range\n"
222                         "Must be in the range 1 to %d (number of databases in the config file)\n",
223                         nbackends );
224                 exit( EXIT_FAILURE );
225
226         } else {
227                 be = &backends[dbnum];
228         }
229
230 #ifdef CSRIMALLOC
231         mal_leaktrace(1);
232 #endif
233
234         slap_startup( be );
235 }
236
237 void slap_tool_destroy( void )
238 {
239         slap_shutdown( be );
240         slap_destroy();
241
242 #ifdef CSRIMALLOC
243         mal_dumpleaktrace( leakfile );
244 #endif
245 }