]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
9c9bf65975b0a17270b1fedd72969ce23800ac86
[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] [-u] [-p [-w] | -r [-i syncreplidlist] [-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] [-m] [-k]\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]\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:i:l:n:prtuvWw";
131                 break;
132
133         case SLAPCAT:
134                 options = "a:b:cd:f:kl:mn: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:v";
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 'i': /* specify syncrepl id list */
196                         replica_id_string = strdup( optarg );
197                         if ( !isdigit( (unsigned char) *replica_id_string )) {
198                                 usage( tool, progname );
199                                 exit( EXIT_FAILURE );
200                         }
201                         slap_str2clist( &replica_id_strlist, replica_id_string, "," );
202                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
203                         replica_id_list = ch_calloc( i + 1, sizeof( int ) );
204                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
205                                 replica_id_list[i] = atoi( replica_id_strlist[i] );
206                                 if ( replica_id_list[i] >= 1000 ) {
207                                         fprintf(stderr,
208                                                 "%s: syncrepl id %d is out of range [0..999]\n",
209                                                 progname, replica_id_list[i] );
210                                         exit( EXIT_FAILURE );
211                                 }
212                         }
213                         replica_id_list[i] = -1;
214                         break;
215
216                 case 'k':       /* Retrieve sync cookie entry */
217                         retrieve_synccookie = 1;
218                         break;
219
220                 case 'l':       /* LDIF file */
221                         ldiffile = strdup( optarg );
222                         break;
223
224                 case 'm':       /* Retrieve ldapsync entry */
225                         retrieve_ctxcsn = 1;
226                         break;
227
228                 case 'M':
229                         ber_str2bv( optarg, 0, 0, &mech );
230                         break;
231
232                 case 'n':       /* which config file db to index */
233                         dbnum = atoi( optarg ) - 1;
234                         break;
235
236                 case 'p':       /* replica promotion */
237                         replica_promotion = 1;          
238                         break;
239
240                 case 'r':       /* replica demotion */
241                         replica_demotion = 1;           
242                         break;
243
244                 case 'R':
245                         realm = optarg;
246                         break;
247
248                 case 's':       /* dump subtree */
249                         subtree = strdup( optarg );
250                         break;
251
252                 case 't':       /* turn on truncate */
253                         truncatemode++;
254                         mode |= SLAP_TRUNCATE_MODE;
255                         break;
256
257                 case 'U':
258                         ber_str2bv( optarg, 0, 0, &authcID );
259                         break;
260
261                 case 'u':       /* dry run */
262                         dryrun++;
263                         break;
264
265                 case 'v':       /* turn on verbose */
266                         verbose++;
267                         break;
268
269                 case 'W':       /* write context csn on every entry add */
270                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
271                         /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
272                         break;
273
274                 case 'w':       /* write context csn on at the end */
275                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
276                         break;
277
278                 case 'X':
279                         ber_str2bv( optarg, 0, 0, &authzID );
280                         break;
281
282                 default:
283                         usage( tool, progname );
284                         break;
285                 }
286         }
287
288         switch ( tool ) {
289         case SLAPADD:
290         case SLAPCAT:
291         case SLAPINDEX:
292                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
293                         usage( tool, progname );
294                 }
295
296                 if ( replica_promotion && replica_demotion ) {
297                         usage( tool, progname );
298
299                 } else if ( !replica_promotion && !replica_demotion ) {
300                         if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
301                                 usage( tool, progname );
302                         }
303                 }
304                 break;
305
306         case SLAPDN:
307                 if ( argc == optind ) {
308                         usage( tool, progname );
309                 }
310                 break;
311
312         case SLAPAUTH:
313                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
314                         usage( tool, progname );
315                 }
316                 break;
317
318         case SLAPTEST:
319                 if ( argc != optind ) {
320                         usage( tool, progname );
321                 }
322                 break;
323
324         case SLAPACL:
325                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
326                         usage( tool, progname );
327                 }
328                 if ( BER_BVISNULL( &base ) ) {
329                         usage( tool, progname );
330                 }
331                 ber_dupbv( &baseDN, &base );
332                 break;
333
334         default:
335                 break;
336         }
337
338         if ( ldiffile == NULL ) {
339                 ldiffp = tool == SLAPCAT ? stdout : stdin;
340
341         } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
342                 == NULL )
343         {
344                 perror( ldiffile );
345                 exit( EXIT_FAILURE );
346         }
347
348         /*
349          * initialize stuff and figure out which backend we're dealing with
350          */
351
352 #ifdef SLAPD_MODULES
353         if ( module_init() != 0 ) {
354                 fprintf( stderr, "%s: module_init failed!\n", progname );
355                 exit( EXIT_FAILURE );
356         }
357 #endif
358                 
359         rc = slap_init( mode, progname );
360
361         if ( rc != 0 ) {
362                 fprintf( stderr, "%s: slap_init failed!\n", progname );
363                 exit( EXIT_FAILURE );
364         }
365
366         rc = slap_schema_init();
367
368         if ( rc != 0 ) {
369                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
370                 exit( EXIT_FAILURE );
371         }
372
373         if ( frontend_init() ) {
374                 fprintf( stderr, "%s: frontend_init failed!\n", progname );
375                 exit( EXIT_FAILURE );
376         }
377
378         if ( overlay_init() ) {
379                 fprintf( stderr, "%s: overlay_init failed!\n", progname );
380                 exit( EXIT_FAILURE );
381         }
382
383         rc = read_config( conffile, 0 );
384
385         if ( rc != 0 ) {
386                 fprintf( stderr, "%s: bad configuration file!\n", progname );
387                 exit( EXIT_FAILURE );
388         }
389
390         ldap_syslog = 0;
391
392         switch ( tool ) {
393         case SLAPADD:
394         case SLAPCAT:
395         case SLAPINDEX:
396                 if ( !nbackends ) {
397                         fprintf( stderr, "No databases found "
398                                         "in config file\n" );
399                         exit( EXIT_FAILURE );
400                 }
401                 break;
402
403         default:
404                 break;
405         }
406
407         rc = slap_schema_check();
408
409         if ( rc != 0 ) {
410                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
411                 exit( EXIT_FAILURE );
412         }
413
414         switch ( tool ) {
415         case SLAPDN:
416         case SLAPTEST:
417         case SLAPAUTH:
418                 be = NULL;
419                 goto startup;
420
421         default:
422                 break;
423         }
424
425         if( filterstr ) {
426                 filter = str2filter( filterstr );
427
428                 if( filter == NULL ) {
429                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
430                         exit( EXIT_FAILURE );
431                 }
432         }
433
434         if( subtree ) {
435                 struct berval val;
436                 ber_str2bv( subtree, 0, 0, &val );
437                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
438                 if( rc != LDAP_SUCCESS ) {
439                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
440                         exit( EXIT_FAILURE );
441                 }
442
443                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
444                         base = val;
445                 } else {
446                         free( subtree );
447                 }
448         }
449
450         if( base.bv_val != NULL ) {
451                 struct berval nbase;
452
453                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
454                 if( rc != LDAP_SUCCESS ) {
455                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
456                                 progname, base.bv_val );
457                         exit( EXIT_FAILURE );
458                 }
459
460                 be = select_backend( &nbase, 0, 0 );
461                 ber_memfree( nbase.bv_val );
462
463                 switch ( tool ) {
464                 case SLAPACL:
465                         goto startup;
466
467                 default:
468                         break;
469                 }
470
471                 if( be == NULL ) {
472                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
473                                 progname, base.bv_val );
474                         exit( EXIT_FAILURE );
475                 }
476                 /* If the named base is a glue master, operate on the
477                  * entire context
478                  */
479                 if (SLAP_GLUE_INSTANCE(be)) {
480                         nosubordinates = 1;
481                 }
482
483         } else if ( dbnum == -1 ) {
484                 if ( nbackends <= 0 ) {
485                         fprintf( stderr, "No available databases\n" );
486                         exit( EXIT_FAILURE );
487                 }
488                 
489                 be = &backends[dbnum=0];
490                 /* If just doing the first by default and it is a
491                  * glue subordinate, find the master.
492                  */
493                 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
494                         if (SLAP_GLUE_SUBORDINATE(be)) {
495                                 nosubordinates = 1;
496                         }
497                         be++;
498                         dbnum++;
499                 }
500
501
502                 if ( dbnum >= nbackends ) {
503                         fprintf( stderr, "Available database(s) "
504                                         "do not allow %s\n", progname );
505                         exit( EXIT_FAILURE );
506                 }
507                 
508                 if ( nosubordinates == 0 && dbnum > 0 ) {
509                         Debug( LDAP_DEBUG_ANY,
510                                 "The first database does not allow %s;"
511                                 " using the first available one (%d)\n",
512                                 progname, dbnum + 1, 0 );
513                 }
514
515         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
516                 fprintf( stderr,
517                         "Database number selected via -n is out of range\n"
518                         "Must be in the range 1 to %d"
519                         " (number of databases in the config file)\n",
520                         nbackends );
521                 exit( EXIT_FAILURE );
522
523         } else {
524                 be = &backends[dbnum];
525         }
526
527 startup:;
528
529 #ifdef CSRIMALLOC
530         mal_leaktrace(1);
531 #endif
532
533         if ( !dryrun && slap_startup( be ) ) {
534
535                 switch ( tool ) {
536                 case SLAPTEST:
537                         fprintf( stderr, "slap_startup failed "
538                                         "(test would succeed using "
539                                         "the -u switch)\n" );
540                         break;
541
542                 default:
543                         fprintf( stderr, "slap_startup failed\n" );
544                         break;
545                 }
546                 
547                 exit( EXIT_FAILURE );
548         }
549 }
550
551 void slap_tool_destroy( void )
552 {
553         if ( !dryrun && be != NULL ) {
554                 slap_shutdown( be );
555         }
556         slap_destroy();
557 #ifdef SLAPD_MODULES
558         if ( slapMode == SLAP_SERVER_MODE ) {
559         /* always false. just pulls in necessary symbol references. */
560                 lutil_uuidstr(NULL, 0);
561         }
562         module_kill();
563 #endif
564         schema_destroy();
565 #ifdef HAVE_TLS
566         ldap_pvt_tls_destroy();
567 #endif
568         config_destroy();
569
570 #ifdef CSRIMALLOC
571         mal_dumpleaktrace( leakfile );
572 #endif
573
574         if ( !BER_BVISNULL( &authcDN ) ) {
575                 ch_free( authcDN.bv_val );
576         }
577 }