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