]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
Fix schema init sequence
[openldap] / servers / slapd / slapcommon.c
1 /* slapcommon.c - common routine for the slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 2003 IBM Corporation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by Kurt Zeilenga for inclusion
20  * in OpenLDAP Software.  Additional signficant contributors include
21  *    Jong Hyuk Choi
22  *    Hallvard B. Furuseth
23  *    Howard Chu
24  *    Pierangelo Masarati
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/stdlib.h>
32 #include <ac/ctype.h>
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 #include <ac/unistd.h>
36
37 #include "slapcommon.h"
38 #include "lutil.h"
39
40 tool_vars tool_globals;
41
42 #ifdef CSRIMALLOC
43 static char *leakfilename;
44 static FILE *leakfile;
45 #endif
46
47 static void
48 usage( int tool, const char *progname )
49 {
50         char *options = NULL;
51         fprintf( stderr,
52                 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]",
53                 progname );
54
55         switch( tool ) {
56         case SLAPACL:
57                 options = "\n\t[-U authcID | -D authcDN]"
58                         " -b DN [attr[/access][:value]] [...]\n";
59                 break;
60
61         case SLAPADD:
62                 options = "\n\t[-n databasenumber | -b suffix]\n"
63                         "\t[-l ldiffile] [-q] [-u] [-w]\n";
64                 break;
65
66         case SLAPAUTH:
67                 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
68                 break;
69
70         case SLAPCAT:
71                 options = "\n\t[-n databasenumber | -b suffix]"
72                         " [-l ldiffile] [-a filter]\n";
73                 break;
74
75         case SLAPDN:
76                 options = " DN [...]\n";
77                 break;
78
79         case SLAPINDEX:
80                 options = "\n\t[-n databasenumber | -b suffix] [-q]\n";
81                 break;
82
83         case SLAPTEST:
84                 options = " [-u]\n";
85                 break;
86         }
87
88         if ( options != NULL ) {
89                 fputs( options, stderr );
90         }
91         exit( EXIT_FAILURE );
92 }
93
94
95 /*
96  * slap_tool_init - initialize slap utility, handle program options.
97  * arguments:
98  *      name            program name
99  *      tool            tool code
100  *      argc, argv      command line arguments
101  */
102
103 void
104 slap_tool_init(
105         const char* progname,
106         int tool,
107         int argc, char **argv )
108 {
109         char *options;
110         char *conffile = SLAPD_DEFAULT_CONFIGFILE;
111         struct berval base = BER_BVNULL;
112         char *filterstr = NULL;
113         char *subtree = NULL;
114         char *ldiffile  = NULL;
115         int rc, i, dbnum;
116         int mode = SLAP_TOOL_MODE;
117         int truncatemode = 0;
118
119 #ifdef CSRIMALLOC
120         leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
121         sprintf( leakfilename, "%s.leak", progname );
122         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
123                 leakfile = stderr;
124         }
125         free( leakfilename );
126 #endif
127
128         switch( tool ) {
129         case SLAPADD:
130                 options = "b:cd:f:l:n:qtuvw";
131                 break;
132
133         case SLAPCAT:
134                 options = "a:b:cd:f:l:n:s:v";
135                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
136                 break;
137
138         case SLAPDN:
139                 options = "d:f:v";
140                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
141                 break;
142
143         case SLAPTEST:
144                 options = "d:f:uv";
145                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
146                 break;
147
148         case SLAPAUTH:
149                 options = "d:f:M:R:U:vX:";
150                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
151                 break;
152
153         case SLAPINDEX:
154                 options = "b:cd:f:n:qv";
155                 mode |= SLAP_TOOL_READMAIN;
156                 break;
157
158         case SLAPACL:
159                 options = "b:D:d:f:U:v";
160                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
161                 break;
162
163         default:
164                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
165                 exit( EXIT_FAILURE );
166         }
167
168         dbnum = -1;
169         while ( (i = getopt( argc, argv, options )) != EOF ) {
170                 switch ( i ) {
171                 case 'a':
172                         filterstr = strdup( optarg );
173                         break;
174
175                 case 'b':
176                         ber_str2bv( optarg, 0, 1, &base );
177                         break;
178
179                 case 'c':       /* enable continue mode */
180                         continuemode++;
181                         break;
182
183                 case 'd':       /* turn on debugging */
184                         ldap_debug += atoi( optarg );
185                         break;
186
187                 case 'D':
188                         ber_str2bv( optarg, 0, 1, &authcDN );
189                         break;
190
191                 case 'f':       /* specify a conf file */
192                         conffile = strdup( optarg );
193                         break;
194
195                 case 'l':       /* LDIF file */
196                         ldiffile = strdup( optarg );
197                         break;
198
199                 case 'M':
200                         ber_str2bv( optarg, 0, 0, &mech );
201                         break;
202
203                 case 'n':       /* which config file db to index */
204                         dbnum = atoi( optarg );
205                         break;
206
207                 case 'q':       /* turn on quick */
208                         mode |= SLAP_TOOL_QUICK;
209                         break;
210
211                 case 'R':
212                         realm = optarg;
213                         break;
214
215                 case 's':       /* dump subtree */
216                         subtree = strdup( optarg );
217                         break;
218
219                 case 't':       /* turn on truncate */
220                         truncatemode++;
221                         mode |= SLAP_TRUNCATE_MODE;
222                         break;
223
224                 case 'U':
225                         ber_str2bv( optarg, 0, 0, &authcID );
226                         break;
227
228                 case 'u':       /* dry run */
229                         dryrun++;
230                         break;
231
232                 case 'v':       /* turn on verbose */
233                         verbose++;
234                         break;
235
236                 case 'w':       /* write context csn at the end */
237                         update_ctxcsn++;
238                         break;
239
240                 case 'X':
241                         ber_str2bv( optarg, 0, 0, &authzID );
242                         break;
243
244                 default:
245                         usage( tool, progname );
246                         break;
247                 }
248         }
249
250         switch ( tool ) {
251         case SLAPADD:
252         case SLAPCAT:
253         case SLAPINDEX:
254                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
255                         usage( tool, progname );
256                 }
257
258                 break;
259
260         case SLAPDN:
261                 if ( argc == optind ) {
262                         usage( tool, progname );
263                 }
264                 break;
265
266         case SLAPAUTH:
267                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
268                         usage( tool, progname );
269                 }
270                 break;
271
272         case SLAPTEST:
273                 if ( argc != optind ) {
274                         usage( tool, progname );
275                 }
276                 break;
277
278         case SLAPACL:
279                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
280                         usage( tool, progname );
281                 }
282                 if ( BER_BVISNULL( &base ) ) {
283                         usage( tool, progname );
284                 }
285                 ber_dupbv( &baseDN, &base );
286                 break;
287
288         default:
289                 break;
290         }
291
292         ldap_syslog = 0;
293
294         if ( ldiffile == NULL ) {
295                 ldiffp = tool == SLAPCAT ? stdout : stdin;
296
297         } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
298                 == NULL )
299         {
300                 perror( ldiffile );
301                 exit( EXIT_FAILURE );
302         }
303
304         /*
305          * initialize stuff and figure out which backend we're dealing with
306          */
307
308 #ifdef SLAPD_MODULES
309         if ( module_init() != 0 ) {
310                 fprintf( stderr, "%s: module_init failed!\n", progname );
311                 exit( EXIT_FAILURE );
312         }
313 #endif
314                 
315         rc = slap_schema_init();
316
317         if ( rc != 0 ) {
318                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
319                 exit( EXIT_FAILURE );
320         }
321
322         rc = slap_init( mode, progname );
323
324         if ( rc != 0 ) {
325                 fprintf( stderr, "%s: slap_init failed!\n", progname );
326                 exit( EXIT_FAILURE );
327         }
328
329         if ( frontend_init() ) {
330                 fprintf( stderr, "%s: frontend_init failed!\n", progname );
331                 exit( EXIT_FAILURE );
332         }
333
334         if ( overlay_init() ) {
335                 fprintf( stderr, "%s: overlay_init failed!\n", progname );
336                 exit( EXIT_FAILURE );
337         }
338
339         rc = read_config( conffile, 0 );
340
341         if ( rc != 0 ) {
342                 fprintf( stderr, "%s: bad configuration file!\n", progname );
343                 exit( EXIT_FAILURE );
344         }
345
346         at_oc_cache = 1;
347
348         switch ( tool ) {
349         case SLAPADD:
350         case SLAPCAT:
351         case SLAPINDEX:
352                 if ( !nbackends ) {
353                         fprintf( stderr, "No databases found "
354                                         "in config file\n" );
355                         exit( EXIT_FAILURE );
356                 }
357                 break;
358
359         default:
360                 break;
361         }
362
363         rc = slap_schema_check();
364
365         if ( rc != 0 ) {
366                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
367                 exit( EXIT_FAILURE );
368         }
369
370         switch ( tool ) {
371         case SLAPDN:
372         case SLAPTEST:
373         case SLAPAUTH:
374                 be = NULL;
375                 goto startup;
376
377         default:
378                 break;
379         }
380
381         if( filterstr ) {
382                 filter = str2filter( filterstr );
383
384                 if( filter == NULL ) {
385                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
386                         exit( EXIT_FAILURE );
387                 }
388         }
389
390         if( subtree ) {
391                 struct berval val;
392                 ber_str2bv( subtree, 0, 0, &val );
393                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
394                 if( rc != LDAP_SUCCESS ) {
395                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
396                         exit( EXIT_FAILURE );
397                 }
398
399                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
400                         base = val;
401                 } else {
402                         free( subtree );
403                 }
404         }
405
406         if( base.bv_val != NULL ) {
407                 struct berval nbase;
408
409                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
410                 if( rc != LDAP_SUCCESS ) {
411                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
412                                 progname, base.bv_val );
413                         exit( EXIT_FAILURE );
414                 }
415
416                 be = select_backend( &nbase, 0, 0 );
417                 ber_memfree( nbase.bv_val );
418
419                 switch ( tool ) {
420                 case SLAPACL:
421                         goto startup;
422
423                 default:
424                         break;
425                 }
426
427                 if( be == NULL ) {
428                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
429                                 progname, base.bv_val );
430                         exit( EXIT_FAILURE );
431                 }
432                 /* If the named base is a glue master, operate on the
433                  * entire context
434                  */
435                 if (SLAP_GLUE_INSTANCE(be)) {
436                         nosubordinates = 1;
437                 }
438
439         } else if ( dbnum == -1 ) {
440                 if ( nbackends <= 0 ) {
441                         fprintf( stderr, "No available databases\n" );
442                         exit( EXIT_FAILURE );
443                 }
444                 
445                 be = &backends[dbnum=1];
446                 /* If just doing the first by default and it is a
447                  * glue subordinate, find the master.
448                  */
449                 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
450                         if (SLAP_GLUE_SUBORDINATE(be)) {
451                                 nosubordinates = 1;
452                         }
453                         be++;
454                         dbnum++;
455                 }
456
457
458                 if ( dbnum >= nbackends ) {
459                         fprintf( stderr, "Available database(s) "
460                                         "do not allow %s\n", progname );
461                         exit( EXIT_FAILURE );
462                 }
463                 
464                 if ( nosubordinates == 0 && dbnum > 0 ) {
465                         Debug( LDAP_DEBUG_ANY,
466                                 "The first database does not allow %s;"
467                                 " using the first available one (%d)\n",
468                                 progname, dbnum + 1, 0 );
469                 }
470
471         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
472                 fprintf( stderr,
473                         "Database number selected via -n is out of range\n"
474                         "Must be in the range 1 to %d"
475                         " (number of databases in the config file)\n",
476                         nbackends );
477                 exit( EXIT_FAILURE );
478
479         } else {
480                 be = &backends[dbnum];
481         }
482
483 startup:;
484
485 #ifdef CSRIMALLOC
486         mal_leaktrace(1);
487 #endif
488
489         if ( !dryrun && slap_startup( be ) ) {
490
491                 switch ( tool ) {
492                 case SLAPTEST:
493                         fprintf( stderr, "slap_startup failed "
494                                         "(test would succeed using "
495                                         "the -u switch)\n" );
496                         break;
497
498                 default:
499                         fprintf( stderr, "slap_startup failed\n" );
500                         break;
501                 }
502                 
503                 exit( EXIT_FAILURE );
504         }
505 }
506
507 void slap_tool_destroy( void )
508 {
509         if ( !dryrun && be != NULL ) {
510                 slap_shutdown( be );
511         }
512         slap_destroy();
513 #ifdef SLAPD_MODULES
514         if ( slapMode == SLAP_SERVER_MODE ) {
515         /* always false. just pulls in necessary symbol references. */
516                 lutil_uuidstr(NULL, 0);
517         }
518         module_kill();
519 #endif
520         schema_destroy();
521 #ifdef HAVE_TLS
522         ldap_pvt_tls_destroy();
523 #endif
524         config_destroy();
525
526 #ifdef CSRIMALLOC
527         mal_dumpleaktrace( leakfile );
528 #endif
529
530         if ( !BER_BVISNULL( &authcDN ) ) {
531                 ch_free( authcDN.bv_val );
532         }
533 }