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