]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
Multi-threaded slapindex
[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 #include "ldif.h"
40
41 tool_vars tool_globals;
42
43 #ifdef CSRIMALLOC
44 static char *leakfilename;
45 static FILE *leakfile;
46 #endif
47
48 static LDIFFP dummy;
49
50 static void
51 usage( int tool, const char *progname )
52 {
53         char *options = NULL;
54         fprintf( stderr,
55                 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir]",
56                 progname );
57
58         switch( tool ) {
59         case SLAPACL:
60                 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
61                         "\n\t-b DN -o <var>[=<val>] [-u] [attr[/access][:value]] [...]\n";
62                 break;
63
64         case SLAPADD:
65                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
66                         "\t[-l ldiffile] [-q] [-u] [-w]\n";
67                 break;
68
69         case SLAPAUTH:
70                 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
71                 break;
72
73         case SLAPCAT:
74                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
75                         " [-l ldiffile] [-a filter]\n";
76                 break;
77
78         case SLAPDN:
79                 options = "\n\t[-N | -P] DN [...]\n";
80                 break;
81
82         case SLAPINDEX:
83                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [-q]\n";
84                 break;
85
86         case SLAPTEST:
87                 options = " [-u]\n";
88                 break;
89         }
90
91         if ( options != NULL ) {
92                 fputs( options, stderr );
93         }
94         exit( EXIT_FAILURE );
95 }
96
97 static int
98 parse_slapacl( void )
99 {
100         size_t  len;
101         char    *p;
102
103         p = strchr( optarg, '=' );
104         if ( p == NULL ) {
105                 return -1;
106         }
107
108         len = p - optarg;
109         p++;
110
111         if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
112                 if ( !BER_BVISNULL( &listener_url ) ) {
113                         ber_memfree( listener_url.bv_val );
114                 }
115                 ber_str2bv( p, 0, 1, &listener_url );
116
117         } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
118                 if ( !BER_BVISNULL( &peer_domain ) ) {
119                         ber_memfree( peer_domain.bv_val );
120                 }
121                 ber_str2bv( p, 0, 1, &peer_domain );
122
123         } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
124                 if ( !BER_BVISNULL( &peer_name ) ) {
125                         ber_memfree( peer_name.bv_val );
126                 }
127                 ber_str2bv( p, 0, 1, &peer_name );
128
129         } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
130                 if ( !BER_BVISNULL( &sock_name ) ) {
131                         ber_memfree( sock_name.bv_val );
132                 }
133                 ber_str2bv( p, 0, 1, &sock_name );
134
135         } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
136                 ssf = atoi( p );
137
138         } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
139                 transport_ssf = atoi( p );
140
141         } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
142                 tls_ssf = atoi( p );
143
144         } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
145                 sasl_ssf = atoi( p );
146
147         } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
148                 ber_str2bv( p, 0, 1, &authzDN );
149
150         } else {
151                 return -1;
152         }
153
154         return 0;
155 }
156
157 /*
158  * slap_tool_init - initialize slap utility, handle program options.
159  * arguments:
160  *      name            program name
161  *      tool            tool code
162  *      argc, argv      command line arguments
163  */
164
165 void
166 slap_tool_init(
167         const char* progname,
168         int tool,
169         int argc, char **argv )
170 {
171         char *options;
172         char *conffile = NULL;
173         char *confdir = NULL;
174         struct berval base = BER_BVNULL;
175         char *filterstr = NULL;
176         char *subtree = NULL;
177         char *ldiffile  = NULL;
178         int rc, i, dbnum;
179         int mode = SLAP_TOOL_MODE;
180         int truncatemode = 0;
181         int use_glue = 1;
182
183 #ifdef CSRIMALLOC
184         leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
185         sprintf( leakfilename, "%s.leak", progname );
186         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
187                 leakfile = stderr;
188         }
189         free( leakfilename );
190 #endif
191
192         switch( tool ) {
193         case SLAPADD:
194                 options = "b:cd:f:F:gl:n:qtuvw";
195                 break;
196
197         case SLAPCAT:
198                 options = "a:b:cd:f:F:gl:n:s:v";
199                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
200                 break;
201
202         case SLAPDN:
203                 options = "d:f:F:NPv";
204                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
205                 break;
206
207         case SLAPTEST:
208                 options = "d:f:F:uv";
209                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
210                 break;
211
212         case SLAPAUTH:
213                 options = "d:f:F:M:R:U:vX:";
214                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
215                 break;
216
217         case SLAPINDEX:
218                 options = "b:cd:f:F:gn:qv";
219                 mode |= SLAP_TOOL_READMAIN;
220                 break;
221
222         case SLAPACL:
223                 options = "b:D:d:f:F:o:uU:vX:";
224                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
225                 break;
226
227         default:
228                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
229                 exit( EXIT_FAILURE );
230         }
231
232         dbnum = -1;
233         while ( (i = getopt( argc, argv, options )) != EOF ) {
234                 switch ( i ) {
235                 case 'a':
236                         filterstr = strdup( optarg );
237                         break;
238
239                 case 'b':
240                         ber_str2bv( optarg, 0, 1, &base );
241                         break;
242
243                 case 'c':       /* enable continue mode */
244                         continuemode++;
245                         break;
246
247                 case 'd':       /* turn on debugging */
248                         ldap_debug += atoi( optarg );
249                         break;
250
251                 case 'D':
252                         ber_str2bv( optarg, 0, 1, &authcDN );
253                         break;
254
255                 case 'f':       /* specify a conf file */
256                         conffile = strdup( optarg );
257                         break;
258
259                 case 'F':       /* specify a conf dir */
260                         confdir = strdup( optarg );
261                         break;
262
263                 case 'g':       /* disable subordinate glue */
264                         use_glue = 0;
265                         break;
266
267                 case 'l':       /* LDIF file */
268                         ldiffile = strdup( optarg );
269                         break;
270
271                 case 'M':
272                         ber_str2bv( optarg, 0, 0, &mech );
273                         break;
274
275                 case 'N':
276                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
277                                 usage( tool, progname );
278                         }
279                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
280                         break;
281
282                 case 'n':       /* which config file db to index */
283                         dbnum = atoi( optarg );
284                         break;
285
286                 case 'o':
287                         if ( parse_slapacl() ) {
288                                 usage( tool, progname );
289                         }
290                         break;
291
292                 case 'P':
293                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
294                                 usage( tool, progname );
295                         }
296                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
297                         break;
298
299                 case 'q':       /* turn on quick */
300                         mode |= SLAP_TOOL_QUICK;
301                         break;
302
303                 case 'R':
304                         realm = optarg;
305                         break;
306
307                 case 's':       /* dump subtree */
308                         subtree = strdup( optarg );
309                         break;
310
311                 case 't':       /* turn on truncate */
312                         truncatemode++;
313                         mode |= SLAP_TRUNCATE_MODE;
314                         break;
315
316                 case 'U':
317                         ber_str2bv( optarg, 0, 0, &authcID );
318                         break;
319
320                 case 'u':       /* dry run */
321                         dryrun++;
322                         break;
323
324                 case 'v':       /* turn on verbose */
325                         verbose++;
326                         break;
327
328                 case 'w':       /* write context csn at the end */
329                         update_ctxcsn++;
330                         break;
331
332                 case 'X':
333                         ber_str2bv( optarg, 0, 0, &authzID );
334                         break;
335
336                 default:
337                         usage( tool, progname );
338                         break;
339                 }
340         }
341
342         switch ( tool ) {
343         case SLAPADD:
344         case SLAPCAT:
345         case SLAPINDEX:
346                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
347                         usage( tool, progname );
348                 }
349
350                 break;
351
352         case SLAPDN:
353                 if ( argc == optind ) {
354                         usage( tool, progname );
355                 }
356                 break;
357
358         case SLAPAUTH:
359                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
360                         usage( tool, progname );
361                 }
362                 break;
363
364         case SLAPTEST:
365                 if ( argc != optind ) {
366                         usage( tool, progname );
367                 }
368                 break;
369
370         case SLAPACL:
371                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
372                         usage( tool, progname );
373                 }
374                 if ( BER_BVISNULL( &base ) ) {
375                         usage( tool, progname );
376                 }
377                 ber_dupbv( &baseDN, &base );
378                 break;
379
380         default:
381                 break;
382         }
383
384         ldap_syslog = 0;
385
386         if ( ldiffile == NULL ) {
387                 dummy.fp = tool == SLAPCAT ? stdout : stdin;
388                 ldiffp = &dummy;
389
390         } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
391                 == NULL )
392         {
393                 perror( ldiffile );
394                 exit( EXIT_FAILURE );
395         }
396
397         /*
398          * initialize stuff and figure out which backend we're dealing with
399          */
400
401         rc = slap_init( mode, progname );
402         if ( rc != 0 ) {
403                 fprintf( stderr, "%s: slap_init failed!\n", progname );
404                 exit( EXIT_FAILURE );
405         }
406
407         rc = read_config( conffile, confdir );
408
409         if ( rc != 0 ) {
410                 fprintf( stderr, "%s: bad configuration %s!\n",
411                         progname, confdir ? "directory" : "file" );
412                 exit( EXIT_FAILURE );
413         }
414
415         at_oc_cache = 1;
416
417         switch ( tool ) {
418         case SLAPADD:
419         case SLAPCAT:
420         case SLAPINDEX:
421                 if ( !nbackends ) {
422                         fprintf( stderr, "No databases found "
423                                         "in config file\n" );
424                         exit( EXIT_FAILURE );
425                 }
426                 break;
427
428         default:
429                 break;
430         }
431
432         if ( use_glue ) {
433                 rc = glue_sub_attach();
434
435                 if ( rc != 0 ) {
436                         fprintf( stderr,
437                                 "%s: subordinate configuration error\n", progname );
438                         exit( EXIT_FAILURE );
439                 }
440         }
441
442         rc = slap_schema_check();
443
444         if ( rc != 0 ) {
445                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
446                 exit( EXIT_FAILURE );
447         }
448
449         switch ( tool ) {
450         case SLAPDN:
451         case SLAPTEST:
452         case SLAPAUTH:
453                 be = NULL;
454                 goto startup;
455
456         default:
457                 break;
458         }
459
460         if( filterstr ) {
461                 filter = str2filter( filterstr );
462
463                 if( filter == NULL ) {
464                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
465                         exit( EXIT_FAILURE );
466                 }
467         }
468
469         if( subtree ) {
470                 struct berval val;
471                 ber_str2bv( subtree, 0, 0, &val );
472                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
473                 if( rc != LDAP_SUCCESS ) {
474                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
475                         exit( EXIT_FAILURE );
476                 }
477
478                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
479                         base = val;
480                 } else {
481                         free( subtree );
482                 }
483         }
484
485         if( base.bv_val != NULL ) {
486                 struct berval nbase;
487
488                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
489                 if( rc != LDAP_SUCCESS ) {
490                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
491                                 progname, base.bv_val );
492                         exit( EXIT_FAILURE );
493                 }
494
495                 be = select_backend( &nbase, 0, 0 );
496                 ber_memfree( nbase.bv_val );
497
498                 switch ( tool ) {
499                 case SLAPACL:
500                         goto startup;
501
502                 default:
503                         break;
504                 }
505
506                 if( be == NULL ) {
507                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
508                                 progname, base.bv_val );
509                         exit( EXIT_FAILURE );
510                 }
511                 /* If the named base is a glue master, operate on the
512                  * entire context
513                  */
514                 if ( SLAP_GLUE_INSTANCE( be ) ) {
515                         nosubordinates = 1;
516                 }
517
518         } else if ( dbnum == -1 ) {
519                 /* no suffix and no dbnum specified, just default to
520                  * the first available database
521                  */
522                 if ( nbackends <= 0 ) {
523                         fprintf( stderr, "No available databases\n" );
524                         exit( EXIT_FAILURE );
525                 }
526                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
527                         dbnum++;
528
529                         /* db #0 is cn=config, don't select it as a default */
530                         if ( dbnum < 1 ) continue;
531                 
532                         if ( SLAP_MONITOR(be))
533                                 continue;
534
535                 /* If just doing the first by default and it is a
536                  * glue subordinate, find the master.
537                  */
538                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
539                                 nosubordinates = 1;
540                                 continue;
541                         }
542                         break;
543                 }
544
545                 if ( !be ) {
546                         fprintf( stderr, "Available database(s) "
547                                         "do not allow %s\n", progname );
548                         exit( EXIT_FAILURE );
549                 }
550                 
551                 if ( nosubordinates == 0 && dbnum > 1 ) {
552                         Debug( LDAP_DEBUG_ANY,
553                                 "The first database does not allow %s;"
554                                 " using the first available one (%d)\n",
555                                 progname, dbnum, 0 );
556                 }
557
558         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
559                 fprintf( stderr,
560                         "Database number selected via -n is out of range\n"
561                         "Must be in the range 0 to %d"
562                         " (number of configured databases)\n",
563                         nbackends-1 );
564                 exit( EXIT_FAILURE );
565
566         } else {
567                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
568                         if ( dbnum == 0 ) break;
569                         dbnum--;
570                 }
571         }
572
573 startup:;
574
575 #ifdef CSRIMALLOC
576         mal_leaktrace(1);
577 #endif
578
579         if ( conffile != NULL ) {
580                 ch_free( conffile );
581         }
582
583         if ( ldiffile != NULL ) {
584                 ch_free( ldiffile );
585         }
586
587         /* slapdn doesn't specify a backend to startup */
588         if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
589                 switch ( tool ) {
590                 case SLAPTEST:
591                         fprintf( stderr, "slap_startup failed "
592                                         "(test would succeed using "
593                                         "the -u switch)\n" );
594                         break;
595
596                 default:
597                         fprintf( stderr, "slap_startup failed\n" );
598                         break;
599                 }
600                 
601                 exit( EXIT_FAILURE );
602         }
603 }
604
605 void slap_tool_destroy( void )
606 {
607         if ( !dryrun ) {
608                 slap_shutdown( be );
609                 slap_destroy();
610         }
611 #ifdef SLAPD_MODULES
612         if ( slapMode == SLAP_SERVER_MODE ) {
613         /* always false. just pulls in necessary symbol references. */
614                 lutil_uuidstr(NULL, 0);
615         }
616         module_kill();
617 #endif
618         schema_destroy();
619 #ifdef HAVE_TLS
620         ldap_pvt_tls_destroy();
621 #endif
622         config_destroy();
623
624 #ifdef CSRIMALLOC
625         mal_dumpleaktrace( leakfile );
626 #endif
627
628         if ( !BER_BVISNULL( &authcDN ) ) {
629                 ch_free( authcDN.bv_val );
630         }
631 }