]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
ITS#3353 consolidate slapd globals into a single struct
[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-2004 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]\n",
53                 progname );
54
55         switch( tool ) {
56         case SLAPADD:
57                 options = "\t[-n databasenumber | -b suffix]\n"
58                         "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
59                 break;
60
61         case SLAPCAT:
62                 options = "\t[-n databasenumber | -b suffix]"
63                         " [-l ldiffile] [-a filter] [-m] [-k]\n";
64                 break;
65
66         case SLAPDN:
67                 options = "\tDN [...]\n";
68                 break;
69
70         case SLAPINDEX:
71                 options = "\t[-n databasenumber | -b suffix]\n";
72                 break;
73
74         case SLAPAUTH:
75                 options = "\t[-U authcID] [-X authzID] ID [...]\n";
76                 break;
77
78         case SLAPACL:
79                 options = "\t[-U authcID | -D authcDN]"
80                         " -b DN attr[/level][:value] [...]\n";
81                 break;
82         }
83
84         if ( options != NULL ) {
85                 fputs( options, stderr );
86         }
87         exit( EXIT_FAILURE );
88 }
89
90
91 /*
92  * slap_tool_init - initialize slap utility, handle program options.
93  * arguments:
94  *      name            program name
95  *      tool            tool code
96  *      argc, argv      command line arguments
97  */
98
99 void
100 slap_tool_init(
101         const char* progname,
102         int tool,
103         int argc, char **argv )
104 {
105         char *options;
106         char *conffile = SLAPD_DEFAULT_CONFIGFILE;
107         struct berval base = BER_BVNULL;
108         char *filterstr = NULL;
109         char *subtree = NULL;
110         char *ldiffile  = NULL;
111         int rc, i, dbnum;
112         int mode = SLAP_TOOL_MODE;
113         int truncatemode = 0;
114
115 #ifdef CSRIMALLOC
116         leakfilename = malloc( strlen( progname ) + STRLEOF( ".leak" ) - 1 );
117         sprintf( leakfilename, "%s.leak", progname );
118         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
119                 leakfile = stderr;
120         }
121         free( leakfilename );
122 #endif
123
124         config_init();  /* sets frontendDB */
125
126         switch( tool ) {
127         case SLAPADD:
128                 options = "b:cd:f:i:l:n:prtuvWw";
129                 break;
130
131         case SLAPCAT:
132                 options = "a:b:cd:f:kl:mn:s:v";
133                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
134                 break;
135
136         case SLAPDN:
137         case SLAPTEST:
138                 options = "d:f:v";
139                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
140                 break;
141
142         case SLAPAUTH:
143                 options = "d:f:U:vX:";
144                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
145                 break;
146
147         case SLAPINDEX:
148                 options = "b:cd:f:n:v";
149                 mode |= SLAP_TOOL_READMAIN;
150                 break;
151
152         case SLAPACL:
153                 options = "b:D:d:f:U:v";
154                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
155                 break;
156
157         default:
158                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
159                 exit( EXIT_FAILURE );
160         }
161
162         dbnum = -1;
163         while ( (i = getopt( argc, argv, options )) != EOF ) {
164                 switch ( i ) {
165                 case 'a':
166                         filterstr = strdup( optarg );
167                         break;
168
169                 case 'b':
170                         ber_str2bv( optarg, 0, 1, &base );
171                         break;
172
173                 case 'c':       /* enable continue mode */
174                         continuemode++;
175                         break;
176
177                 case 'd':       /* turn on debugging */
178                         ldap_debug += atoi( optarg );
179                         break;
180
181                 case 'D':
182                         ber_str2bv( optarg, 0, 1, &authcDN );
183                         break;
184
185                 case 'f':       /* specify a conf file */
186                         conffile = strdup( optarg );
187                         break;
188
189                 case 'i': /* specify syncrepl id list */
190                         replica_id_string = strdup( optarg );
191                         if ( !isdigit( (unsigned char) *replica_id_string )) {
192                                 usage( tool, progname );
193                                 exit( EXIT_FAILURE );
194                         }
195                         slap_str2clist( &replica_id_strlist, replica_id_string, "," );
196                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
197                         replica_id_list = ch_calloc( i + 1, sizeof( int ) );
198                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
199                                 replica_id_list[i] = atoi( replica_id_strlist[i] );
200                                 if ( replica_id_list[i] >= 1000 ) {
201                                         fprintf(stderr,
202                                                 "%s: syncrepl id %d is out of range [0..999]\n",
203                                                 progname, replica_id_list[i] );
204                                         exit( EXIT_FAILURE );
205                                 }
206                         }
207                         replica_id_list[i] = -1;
208                         break;
209
210                 case 'k':       /* Retrieve sync cookie entry */
211                         retrieve_synccookie = 1;
212                         break;
213
214                 case 'l':       /* LDIF file */
215                         ldiffile = strdup( optarg );
216                         break;
217
218                 case 'm':       /* Retrieve ldapsync entry */
219                         retrieve_ctxcsn = 1;
220                         break;
221
222                 case 'n':       /* which config file db to index */
223                         dbnum = atoi( optarg ) - 1;
224                         break;
225
226                 case 'p':       /* replica promotion */
227                         replica_promotion = 1;          
228                         break;
229
230                 case 'r':       /* replica demotion */
231                         replica_demotion = 1;           
232                         break;
233
234                 case 's':       /* dump subtree */
235                         subtree = strdup( optarg );
236                         break;
237
238                 case 't':       /* turn on truncate */
239                         truncatemode++;
240                         mode |= SLAP_TRUNCATE_MODE;
241                         break;
242
243                 case 'U':
244                         ber_str2bv( optarg, 0, 0, &authcID );
245                         break;
246
247                 case 'u':       /* dry run */
248                         dryrun++;
249                         break;
250
251                 case 'v':       /* turn on verbose */
252                         verbose++;
253                         break;
254
255                 case 'W':       /* write context csn on every entry add */
256                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
257                         /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
258                         break;
259
260                 case 'w':       /* write context csn on at the end */
261                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
262                         break;
263
264                 case 'X':
265                         ber_str2bv( optarg, 0, 0, &authzID );
266                         break;
267
268                 default:
269                         usage( tool, progname );
270                         break;
271                 }
272         }
273
274         switch ( tool ) {
275         case SLAPADD:
276         case SLAPCAT:
277         case SLAPINDEX:
278                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
279                         usage( tool, progname );
280                 }
281
282                 if ( replica_promotion && replica_demotion ) {
283                         usage( tool, progname );
284
285                 } else if ( !replica_promotion && !replica_demotion ) {
286                         if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
287                                 usage( tool, progname );
288                         }
289                 }
290                 break;
291
292         case SLAPDN:
293                 if ( argc == optind ) {
294                         usage( tool, progname );
295                 }
296                 break;
297
298         case SLAPAUTH:
299                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
300                         usage( tool, progname );
301                 }
302                 break;
303
304         case SLAPTEST:
305                 if ( argc != optind ) {
306                         usage( tool, progname );
307                 }
308                 break;
309
310         case SLAPACL:
311                 if ( argc == optind ) {
312                         usage( tool, progname );
313                 }
314                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
315                         usage( tool, progname );
316                 }
317                 if ( BER_BVISNULL( &base ) ) {
318                         usage( tool, progname );
319                 }
320                 ber_dupbv( &baseDN, &base );
321                 break;
322
323         default:
324                 break;
325         }
326
327         if ( ldiffile == NULL ) {
328                 ldiffp = tool == SLAPCAT ? stdout : stdin;
329
330         } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
331                 == NULL )
332         {
333                 perror( ldiffile );
334                 exit( EXIT_FAILURE );
335         }
336
337         /*
338          * initialize stuff and figure out which backend we're dealing with
339          */
340
341 #ifdef SLAPD_MODULES
342         if ( module_init() != 0 ) {
343                 fprintf( stderr, "%s: module_init failed!\n", progname );
344                 exit( EXIT_FAILURE );
345         }
346 #endif
347                 
348         rc = slap_init( mode, progname );
349
350         if ( rc != 0 ) {
351                 fprintf( stderr, "%s: slap_init failed!\n", progname );
352                 exit( EXIT_FAILURE );
353         }
354
355         rc = slap_schema_init();
356
357         if ( rc != 0 ) {
358                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
359                 exit( EXIT_FAILURE );
360         }
361
362         if ( frontend_init() ) {
363                 fprintf( stderr, "%s: frontend_init failed!\n", progname );
364                 exit( EXIT_FAILURE );
365         }
366
367         if ( overlay_init() ) {
368                 fprintf( stderr, "%s: overlay_init failed!\n", progname );
369                 exit( EXIT_FAILURE );
370         }
371
372         rc = read_config( conffile, 0 );
373
374         if ( rc != 0 ) {
375                 fprintf( stderr, "%s: bad configuration file!\n", progname );
376                 exit( EXIT_FAILURE );
377         }
378
379         ldap_syslog = 0;
380
381         switch ( tool ) {
382         case SLAPADD:
383         case SLAPCAT:
384         case SLAPINDEX:
385                 if ( !nbackends ) {
386                         fprintf( stderr, "No databases found "
387                                         "in config file\n" );
388                         exit( EXIT_FAILURE );
389                 }
390                 break;
391
392         default:
393                 break;
394         }
395
396         rc = glue_sub_init();
397
398         if ( rc != 0 ) {
399                 fprintf( stderr, "Subordinate configuration error\n" );
400                 exit( EXIT_FAILURE );
401         }
402
403         rc = slap_schema_check();
404
405         if ( rc != 0 ) {
406                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
407                 exit( EXIT_FAILURE );
408         }
409
410         switch ( tool ) {
411         case SLAPDN:
412         case SLAPTEST:
413         case SLAPAUTH:
414                 be = NULL;
415                 goto startup;
416
417         default:
418                 break;
419         }
420
421         if( filterstr ) {
422                 filter = str2filter( filterstr );
423
424                 if( filter == NULL ) {
425                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
426                         exit( EXIT_FAILURE );
427                 }
428         }
429
430         if( subtree ) {
431                 struct berval val;
432                 ber_str2bv( subtree, 0, 0, &val );
433                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
434                 if( rc != LDAP_SUCCESS ) {
435                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
436                         exit( EXIT_FAILURE );
437                 }
438
439                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
440                         base = val;
441                 } else {
442                         free( subtree );
443                 }
444         }
445
446         if( base.bv_val != NULL ) {
447                 struct berval nbase;
448
449                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
450                 if( rc != LDAP_SUCCESS ) {
451                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
452                                 progname, base.bv_val );
453                         exit( EXIT_FAILURE );
454                 }
455
456                 be = select_backend( &nbase, 0, 0 );
457                 ber_memfree( nbase.bv_val );
458
459                 switch ( tool ) {
460                 case SLAPACL:
461                         goto startup;
462
463                 default:
464                         break;
465                 }
466
467                 if( be == NULL ) {
468                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
469                                 progname, base.bv_val );
470                         exit( EXIT_FAILURE );
471                 }
472                 /* If the named base is a glue master, operate on the
473                  * entire context
474                  */
475                 if (SLAP_GLUE_INSTANCE(be)) {
476                         nosubordinates = 1;
477                 }
478
479         } else if ( dbnum == -1 ) {
480                 if ( nbackends <= 0 ) {
481                         fprintf( stderr, "No available databases\n" );
482                         exit( EXIT_FAILURE );
483                 }
484                 
485                 be = &backends[dbnum=0];
486                 /* If just doing the first by default and it is a
487                  * glue subordinate, find the master.
488                  */
489                 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
490                         if (SLAP_GLUE_SUBORDINATE(be)) {
491                                 nosubordinates = 1;
492                         }
493                         be++;
494                         dbnum++;
495                 }
496
497
498                 if ( dbnum >= nbackends ) {
499                         fprintf( stderr, "Available database(s) "
500                                         "do not allow %s\n", progname );
501                         exit( EXIT_FAILURE );
502                 }
503                 
504                 if ( nosubordinates == 0 && dbnum > 0 ) {
505                         Debug( LDAP_DEBUG_ANY,
506                                 "The first database does not allow %s;"
507                                 " using the first available one (%d)\n",
508                                 progname, dbnum + 1, 0 );
509                 }
510
511         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
512                 fprintf( stderr,
513                         "Database number selected via -n is out of range\n"
514                         "Must be in the range 1 to %d"
515                         " (number of databases in the config file)\n",
516                         nbackends );
517                 exit( EXIT_FAILURE );
518
519         } else {
520                 be = &backends[dbnum];
521         }
522
523 startup:;
524
525 #ifdef CSRIMALLOC
526         mal_leaktrace(1);
527 #endif
528
529         if ( !dryrun && slap_startup( be ) ) {
530                 fprintf( stderr, "slap_startup failed\n" );
531                 exit( EXIT_FAILURE );
532         }
533 }
534
535 void slap_tool_destroy( void )
536 {
537         if ( !dryrun && be != NULL ) {
538                 slap_shutdown( be );
539         }
540         slap_destroy();
541 #ifdef SLAPD_MODULES
542         if ( slapMode == SLAP_SERVER_MODE ) {
543         /* always false. just pulls in necessary symbol references. */
544                 lutil_uuidstr(NULL, 0);
545         }
546         module_kill();
547 #endif
548         schema_destroy();
549 #ifdef HAVE_TLS
550         ldap_pvt_tls_destroy();
551 #endif
552         config_destroy();
553
554 #ifdef CSRIMALLOC
555         mal_dumpleaktrace( leakfile );
556 #endif
557
558         if ( !BER_BVISNULL( &authcDN ) ) {
559                 ch_free( authcDN.bv_val );
560         }
561 }