]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
minor cleanup
[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                 if ( lutil_atou( &ssf, p ) ) {
137                         Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
138                         return -1;
139                 }
140
141         } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
142                 if ( lutil_atou( &transport_ssf, p ) ) {
143                         Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
144                         return -1;
145                 }
146
147         } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
148                 if ( lutil_atou( &tls_ssf, p ) ) {
149                         Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
150                         return -1;
151                 }
152
153         } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
154                 if ( lutil_atou( &sasl_ssf, p ) ) {
155                         Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
156                         return -1;
157                 }
158
159         } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
160                 ber_str2bv( p, 0, 1, &authzDN );
161
162         } else {
163                 return -1;
164         }
165
166         return 0;
167 }
168
169 /*
170  * slap_tool_init - initialize slap utility, handle program options.
171  * arguments:
172  *      name            program name
173  *      tool            tool code
174  *      argc, argv      command line arguments
175  */
176
177 void
178 slap_tool_init(
179         const char* progname,
180         int tool,
181         int argc, char **argv )
182 {
183         char *options;
184         char *conffile = NULL;
185         char *confdir = NULL;
186         struct berval base = BER_BVNULL;
187         char *filterstr = NULL;
188         char *subtree = NULL;
189         char *ldiffile  = NULL;
190         int rc, i, dbnum;
191         int mode = SLAP_TOOL_MODE;
192         int truncatemode = 0;
193         int use_glue = 1;
194
195 #ifdef LDAP_DEBUG
196         /* tools default to "none", so that at least LDAP_DEBUG_ANY 
197          * messages show up; use -d 0 to reset */
198         slap_debug = LDAP_DEBUG_NONE;
199 #endif
200
201 #ifdef CSRIMALLOC
202         leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
203         sprintf( leakfilename, "%s.leak", progname );
204         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
205                 leakfile = stderr;
206         }
207         free( leakfilename );
208 #endif
209
210         switch( tool ) {
211         case SLAPADD:
212                 options = "b:cd:f:F:gl:n:qtuvw";
213                 break;
214
215         case SLAPCAT:
216                 options = "a:b:cd:f:F:gl:n:s:v";
217                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
218                 break;
219
220         case SLAPDN:
221                 options = "d:f:F:NPv";
222                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
223                 break;
224
225         case SLAPTEST:
226                 options = "d:f:F:uv";
227                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
228                 break;
229
230         case SLAPAUTH:
231                 options = "d:f:F:M:R:U:vX:";
232                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
233                 break;
234
235         case SLAPINDEX:
236                 options = "b:cd:f:F:gn:qv";
237                 mode |= SLAP_TOOL_READMAIN;
238                 break;
239
240         case SLAPACL:
241                 options = "b:D:d:f:F:o:uU:vX:";
242                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
243                 break;
244
245         default:
246                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
247                 exit( EXIT_FAILURE );
248         }
249
250         dbnum = -1;
251         while ( (i = getopt( argc, argv, options )) != EOF ) {
252                 switch ( i ) {
253                 case 'a':
254                         filterstr = strdup( optarg );
255                         break;
256
257                 case 'b':
258                         ber_str2bv( optarg, 0, 1, &base );
259                         break;
260
261                 case 'c':       /* enable continue mode */
262                         continuemode++;
263                         break;
264
265                 case 'd': {     /* turn on debugging */
266                         int     level = 0;
267
268 #ifdef LDAP_DEBUG
269                         if ( optarg != NULL && optarg[ 0 ] != '-' && !isdigit( optarg[ 0 ] ) )
270                         {
271                                 int     i, goterr = 0;
272                                 char    **levels;
273
274                                 levels = ldap_str2charray( optarg, "," );
275
276                                 for ( i = 0; levels[ i ] != NULL; i++ ) {
277                                         level = 0;
278
279                                         if ( str2loglevel( levels[ i ], &level ) ) {
280                                                 fprintf( stderr,
281                                                         "unrecognized log level "
282                                                         "\"%s\"\n", levels[ i ] );
283                                                 goterr = 1;
284                                                 /* but keep parsing... */
285
286                                         } else {
287                                                 if ( level != 0 ) {
288                                                         slap_debug |= level;
289
290                                                 } else {
291                                                         /* allow to reset log level */
292                                                         slap_debug = 0;
293                                                 }
294                                         }
295                                 }
296
297                                 ldap_charray_free( levels );
298
299                                 if ( goterr ) {
300                                         usage( tool, progname );
301                                 }
302
303                         } else {
304                                 if ( lutil_atoix( &level, optarg, 0 ) != 0 ) {
305                                         fprintf( stderr,
306                                                 "unrecognized log level "
307                                                 "\"%s\"\n", optarg );
308                                         usage( tool, progname );
309                                 }
310
311                                 if ( level != 0 ) {
312                                         slap_debug |= level;
313
314                                 } else {
315                                         /* allow to reset log level */
316                                         slap_debug = 0;
317                                 }
318                         }
319 #else
320                         if ( lutil_atoi( &level, optarg ) != 0 || level != 0 )
321                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
322                                        stderr );
323 #endif
324                         } break;
325
326                 case 'D':
327                         ber_str2bv( optarg, 0, 1, &authcDN );
328                         break;
329
330                 case 'f':       /* specify a conf file */
331                         conffile = strdup( optarg );
332                         break;
333
334                 case 'F':       /* specify a conf dir */
335                         confdir = strdup( optarg );
336                         break;
337
338                 case 'g':       /* disable subordinate glue */
339                         use_glue = 0;
340                         break;
341
342                 case 'l':       /* LDIF file */
343                         ldiffile = strdup( optarg );
344                         break;
345
346                 case 'M':
347                         ber_str2bv( optarg, 0, 0, &mech );
348                         break;
349
350                 case 'N':
351                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
352                                 usage( tool, progname );
353                         }
354                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
355                         break;
356
357                 case 'n':       /* which config file db to index */
358                         if ( lutil_atoi( &dbnum, optarg ) ) {
359                                 usage( tool, progname );
360                         }
361                         break;
362
363                 case 'o':
364                         if ( parse_slapacl() ) {
365                                 usage( tool, progname );
366                         }
367                         break;
368
369                 case 'P':
370                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
371                                 usage( tool, progname );
372                         }
373                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
374                         break;
375
376                 case 'q':       /* turn on quick */
377                         mode |= SLAP_TOOL_QUICK;
378                         break;
379
380                 case 'R':
381                         realm = optarg;
382                         break;
383
384                 case 's':       /* dump subtree */
385                         subtree = strdup( optarg );
386                         break;
387
388                 case 't':       /* turn on truncate */
389                         truncatemode++;
390                         mode |= SLAP_TRUNCATE_MODE;
391                         break;
392
393                 case 'U':
394                         ber_str2bv( optarg, 0, 0, &authcID );
395                         break;
396
397                 case 'u':       /* dry run */
398                         dryrun++;
399                         break;
400
401                 case 'v':       /* turn on verbose */
402                         verbose++;
403                         break;
404
405                 case 'w':       /* write context csn at the end */
406                         update_ctxcsn++;
407                         break;
408
409                 case 'X':
410                         ber_str2bv( optarg, 0, 0, &authzID );
411                         break;
412
413                 default:
414                         usage( tool, progname );
415                         break;
416                 }
417         }
418
419         switch ( tool ) {
420         case SLAPADD:
421         case SLAPCAT:
422         case SLAPINDEX:
423                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
424                         usage( tool, progname );
425                 }
426
427                 break;
428
429         case SLAPDN:
430                 if ( argc == optind ) {
431                         usage( tool, progname );
432                 }
433                 break;
434
435         case SLAPAUTH:
436                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
437                         usage( tool, progname );
438                 }
439                 break;
440
441         case SLAPTEST:
442                 if ( argc != optind ) {
443                         usage( tool, progname );
444                 }
445                 break;
446
447         case SLAPACL:
448                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
449                         usage( tool, progname );
450                 }
451                 if ( BER_BVISNULL( &base ) ) {
452                         usage( tool, progname );
453                 }
454                 ber_dupbv( &baseDN, &base );
455                 break;
456
457         default:
458                 break;
459         }
460
461         ldap_syslog = 0;
462
463         if ( ldiffile == NULL ) {
464                 dummy.fp = tool == SLAPCAT ? stdout : stdin;
465                 ldiffp = &dummy;
466
467         } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
468                 == NULL )
469         {
470                 perror( ldiffile );
471                 exit( EXIT_FAILURE );
472         }
473
474         /*
475          * initialize stuff and figure out which backend we're dealing with
476          */
477
478         rc = slap_init( mode, progname );
479         if ( rc != 0 ) {
480                 fprintf( stderr, "%s: slap_init failed!\n", progname );
481                 exit( EXIT_FAILURE );
482         }
483
484         rc = read_config( conffile, confdir );
485
486         if ( rc != 0 ) {
487                 fprintf( stderr, "%s: bad configuration %s!\n",
488                         progname, confdir ? "directory" : "file" );
489                 exit( EXIT_FAILURE );
490         }
491
492         at_oc_cache = 1;
493
494         switch ( tool ) {
495         case SLAPADD:
496         case SLAPCAT:
497         case SLAPINDEX:
498                 if ( !nbackends ) {
499                         fprintf( stderr, "No databases found "
500                                         "in config file\n" );
501                         exit( EXIT_FAILURE );
502                 }
503                 break;
504
505         default:
506                 break;
507         }
508
509         if ( use_glue ) {
510                 rc = glue_sub_attach();
511
512                 if ( rc != 0 ) {
513                         fprintf( stderr,
514                                 "%s: subordinate configuration error\n", progname );
515                         exit( EXIT_FAILURE );
516                 }
517         }
518
519         rc = slap_schema_check();
520
521         if ( rc != 0 ) {
522                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
523                 exit( EXIT_FAILURE );
524         }
525
526         switch ( tool ) {
527         case SLAPDN:
528         case SLAPTEST:
529         case SLAPAUTH:
530                 be = NULL;
531                 goto startup;
532
533         default:
534                 break;
535         }
536
537         if( filterstr ) {
538                 filter = str2filter( filterstr );
539
540                 if( filter == NULL ) {
541                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
542                         exit( EXIT_FAILURE );
543                 }
544         }
545
546         if( subtree ) {
547                 struct berval val;
548                 ber_str2bv( subtree, 0, 0, &val );
549                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
550                 if( rc != LDAP_SUCCESS ) {
551                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
552                         exit( EXIT_FAILURE );
553                 }
554
555                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
556                         base = val;
557                 } else {
558                         free( subtree );
559                 }
560         }
561
562         if( base.bv_val != NULL ) {
563                 struct berval nbase;
564
565                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
566                 if( rc != LDAP_SUCCESS ) {
567                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
568                                 progname, base.bv_val );
569                         exit( EXIT_FAILURE );
570                 }
571
572                 be = select_backend( &nbase, 0, 0 );
573                 ber_memfree( nbase.bv_val );
574
575                 switch ( tool ) {
576                 case SLAPACL:
577                         goto startup;
578
579                 default:
580                         break;
581                 }
582
583                 if( be == NULL ) {
584                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
585                                 progname, base.bv_val );
586                         exit( EXIT_FAILURE );
587                 }
588                 /* If the named base is a glue master, operate on the
589                  * entire context
590                  */
591                 if ( SLAP_GLUE_INSTANCE( be ) ) {
592                         nosubordinates = 1;
593                 }
594
595         } else if ( dbnum == -1 ) {
596                 /* no suffix and no dbnum specified, just default to
597                  * the first available database
598                  */
599                 if ( nbackends <= 0 ) {
600                         fprintf( stderr, "No available databases\n" );
601                         exit( EXIT_FAILURE );
602                 }
603                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
604                         dbnum++;
605
606                         /* db #0 is cn=config, don't select it as a default */
607                         if ( dbnum < 1 ) continue;
608                 
609                         if ( SLAP_MONITOR(be))
610                                 continue;
611
612                 /* If just doing the first by default and it is a
613                  * glue subordinate, find the master.
614                  */
615                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
616                                 nosubordinates = 1;
617                                 continue;
618                         }
619                         break;
620                 }
621
622                 if ( !be ) {
623                         fprintf( stderr, "Available database(s) "
624                                         "do not allow %s\n", progname );
625                         exit( EXIT_FAILURE );
626                 }
627                 
628                 if ( nosubordinates == 0 && dbnum > 1 ) {
629                         Debug( LDAP_DEBUG_ANY,
630                                 "The first database does not allow %s;"
631                                 " using the first available one (%d)\n",
632                                 progname, dbnum, 0 );
633                 }
634
635         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
636                 fprintf( stderr,
637                         "Database number selected via -n is out of range\n"
638                         "Must be in the range 0 to %d"
639                         " (number of configured databases)\n",
640                         nbackends-1 );
641                 exit( EXIT_FAILURE );
642
643         } else {
644                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
645                         if ( dbnum == 0 ) break;
646                         dbnum--;
647                 }
648         }
649
650 startup:;
651
652 #ifdef CSRIMALLOC
653         mal_leaktrace(1);
654 #endif
655
656         if ( conffile != NULL ) {
657                 ch_free( conffile );
658         }
659
660         if ( ldiffile != NULL ) {
661                 ch_free( ldiffile );
662         }
663
664         /* slapdn doesn't specify a backend to startup */
665         if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
666                 switch ( tool ) {
667                 case SLAPTEST:
668                         fprintf( stderr, "slap_startup failed "
669                                         "(test would succeed using "
670                                         "the -u switch)\n" );
671                         break;
672
673                 default:
674                         fprintf( stderr, "slap_startup failed\n" );
675                         break;
676                 }
677                 
678                 exit( EXIT_FAILURE );
679         }
680 }
681
682 void slap_tool_destroy( void )
683 {
684         if ( !dryrun ) {
685                 slap_shutdown( be );
686                 slap_destroy();
687         }
688 #ifdef SLAPD_MODULES
689         if ( slapMode == SLAP_SERVER_MODE ) {
690         /* always false. just pulls in necessary symbol references. */
691                 lutil_uuidstr(NULL, 0);
692         }
693         module_kill();
694 #endif
695         schema_destroy();
696 #ifdef HAVE_TLS
697         ldap_pvt_tls_destroy();
698 #endif
699         config_destroy();
700
701 #ifdef CSRIMALLOC
702         mal_dumpleaktrace( leakfile );
703 #endif
704
705         if ( !BER_BVISNULL( &authcDN ) ) {
706                 ch_free( authcDN.bv_val );
707         }
708 }