]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
5026e41de48cb3d38702655e240087ccf6882737
[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         ldap_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                         {
267 #ifdef LDAP_DEBUG
268                         int     level;
269
270                         if ( optarg != NULL && optarg[ 0 ] != '-' && !isdigit( optarg[ 0 ] ) )
271                         {
272                                 if ( str2loglevel( optarg, &level ) ) {
273                                         fprintf( stderr,
274                                                 "unrecognized log level "
275                                                 "\"%s\"\n", optarg );
276                                         exit( EXIT_FAILURE );
277                                 }
278
279                         } else {
280                                 char    *next = NULL;
281
282                                 level = strtol( optarg, &next, 0 );
283                                 if ( next == NULL || next[ 0 ] != '\0' ) {
284                                         fprintf( stderr,
285                                                 "unrecognized log level "
286                                                 "\"%s\"\n", optarg );
287                                         exit( EXIT_FAILURE );
288                                 }
289                         }
290
291                         if ( level ) {
292                                 ldap_debug |= level;
293                         } else {
294                                 /* allow to reset log level */
295                                 ldap_debug = 0;
296                         }
297 #else
298                         if ( lutil_atoi( &level, optarg ) != 0 || level != 0 )
299                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
300                                        stderr );
301 #endif
302                         } break;
303
304                 case 'D':
305                         ber_str2bv( optarg, 0, 1, &authcDN );
306                         break;
307
308                 case 'f':       /* specify a conf file */
309                         conffile = strdup( optarg );
310                         break;
311
312                 case 'F':       /* specify a conf dir */
313                         confdir = strdup( optarg );
314                         break;
315
316                 case 'g':       /* disable subordinate glue */
317                         use_glue = 0;
318                         break;
319
320                 case 'l':       /* LDIF file */
321                         ldiffile = strdup( optarg );
322                         break;
323
324                 case 'M':
325                         ber_str2bv( optarg, 0, 0, &mech );
326                         break;
327
328                 case 'N':
329                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
330                                 usage( tool, progname );
331                         }
332                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
333                         break;
334
335                 case 'n':       /* which config file db to index */
336                         if ( lutil_atoi( &dbnum, optarg ) ) {
337                                 usage( tool, progname );
338                         }
339                         break;
340
341                 case 'o':
342                         if ( parse_slapacl() ) {
343                                 usage( tool, progname );
344                         }
345                         break;
346
347                 case 'P':
348                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
349                                 usage( tool, progname );
350                         }
351                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
352                         break;
353
354                 case 'q':       /* turn on quick */
355                         mode |= SLAP_TOOL_QUICK;
356                         break;
357
358                 case 'R':
359                         realm = optarg;
360                         break;
361
362                 case 's':       /* dump subtree */
363                         subtree = strdup( optarg );
364                         break;
365
366                 case 't':       /* turn on truncate */
367                         truncatemode++;
368                         mode |= SLAP_TRUNCATE_MODE;
369                         break;
370
371                 case 'U':
372                         ber_str2bv( optarg, 0, 0, &authcID );
373                         break;
374
375                 case 'u':       /* dry run */
376                         dryrun++;
377                         break;
378
379                 case 'v':       /* turn on verbose */
380                         verbose++;
381                         break;
382
383                 case 'w':       /* write context csn at the end */
384                         update_ctxcsn++;
385                         break;
386
387                 case 'X':
388                         ber_str2bv( optarg, 0, 0, &authzID );
389                         break;
390
391                 default:
392                         usage( tool, progname );
393                         break;
394                 }
395         }
396
397         switch ( tool ) {
398         case SLAPADD:
399         case SLAPCAT:
400         case SLAPINDEX:
401                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
402                         usage( tool, progname );
403                 }
404
405                 break;
406
407         case SLAPDN:
408                 if ( argc == optind ) {
409                         usage( tool, progname );
410                 }
411                 break;
412
413         case SLAPAUTH:
414                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
415                         usage( tool, progname );
416                 }
417                 break;
418
419         case SLAPTEST:
420                 if ( argc != optind ) {
421                         usage( tool, progname );
422                 }
423                 break;
424
425         case SLAPACL:
426                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
427                         usage( tool, progname );
428                 }
429                 if ( BER_BVISNULL( &base ) ) {
430                         usage( tool, progname );
431                 }
432                 ber_dupbv( &baseDN, &base );
433                 break;
434
435         default:
436                 break;
437         }
438
439         ldap_syslog = 0;
440
441         if ( ldiffile == NULL ) {
442                 dummy.fp = tool == SLAPCAT ? stdout : stdin;
443                 ldiffp = &dummy;
444
445         } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
446                 == NULL )
447         {
448                 perror( ldiffile );
449                 exit( EXIT_FAILURE );
450         }
451
452         /*
453          * initialize stuff and figure out which backend we're dealing with
454          */
455
456         rc = slap_init( mode, progname );
457         if ( rc != 0 ) {
458                 fprintf( stderr, "%s: slap_init failed!\n", progname );
459                 exit( EXIT_FAILURE );
460         }
461
462         rc = read_config( conffile, confdir );
463
464         if ( rc != 0 ) {
465                 fprintf( stderr, "%s: bad configuration %s!\n",
466                         progname, confdir ? "directory" : "file" );
467                 exit( EXIT_FAILURE );
468         }
469
470         at_oc_cache = 1;
471
472         switch ( tool ) {
473         case SLAPADD:
474         case SLAPCAT:
475         case SLAPINDEX:
476                 if ( !nbackends ) {
477                         fprintf( stderr, "No databases found "
478                                         "in config file\n" );
479                         exit( EXIT_FAILURE );
480                 }
481                 break;
482
483         default:
484                 break;
485         }
486
487         if ( use_glue ) {
488                 rc = glue_sub_attach();
489
490                 if ( rc != 0 ) {
491                         fprintf( stderr,
492                                 "%s: subordinate configuration error\n", progname );
493                         exit( EXIT_FAILURE );
494                 }
495         }
496
497         rc = slap_schema_check();
498
499         if ( rc != 0 ) {
500                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
501                 exit( EXIT_FAILURE );
502         }
503
504         switch ( tool ) {
505         case SLAPDN:
506         case SLAPTEST:
507         case SLAPAUTH:
508                 be = NULL;
509                 goto startup;
510
511         default:
512                 break;
513         }
514
515         if( filterstr ) {
516                 filter = str2filter( filterstr );
517
518                 if( filter == NULL ) {
519                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
520                         exit( EXIT_FAILURE );
521                 }
522         }
523
524         if( subtree ) {
525                 struct berval val;
526                 ber_str2bv( subtree, 0, 0, &val );
527                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
528                 if( rc != LDAP_SUCCESS ) {
529                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
530                         exit( EXIT_FAILURE );
531                 }
532
533                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
534                         base = val;
535                 } else {
536                         free( subtree );
537                 }
538         }
539
540         if( base.bv_val != NULL ) {
541                 struct berval nbase;
542
543                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
544                 if( rc != LDAP_SUCCESS ) {
545                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
546                                 progname, base.bv_val );
547                         exit( EXIT_FAILURE );
548                 }
549
550                 be = select_backend( &nbase, 0, 0 );
551                 ber_memfree( nbase.bv_val );
552
553                 switch ( tool ) {
554                 case SLAPACL:
555                         goto startup;
556
557                 default:
558                         break;
559                 }
560
561                 if( be == NULL ) {
562                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
563                                 progname, base.bv_val );
564                         exit( EXIT_FAILURE );
565                 }
566                 /* If the named base is a glue master, operate on the
567                  * entire context
568                  */
569                 if ( SLAP_GLUE_INSTANCE( be ) ) {
570                         nosubordinates = 1;
571                 }
572
573         } else if ( dbnum == -1 ) {
574                 /* no suffix and no dbnum specified, just default to
575                  * the first available database
576                  */
577                 if ( nbackends <= 0 ) {
578                         fprintf( stderr, "No available databases\n" );
579                         exit( EXIT_FAILURE );
580                 }
581                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
582                         dbnum++;
583
584                         /* db #0 is cn=config, don't select it as a default */
585                         if ( dbnum < 1 ) continue;
586                 
587                         if ( SLAP_MONITOR(be))
588                                 continue;
589
590                 /* If just doing the first by default and it is a
591                  * glue subordinate, find the master.
592                  */
593                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
594                                 nosubordinates = 1;
595                                 continue;
596                         }
597                         break;
598                 }
599
600                 if ( !be ) {
601                         fprintf( stderr, "Available database(s) "
602                                         "do not allow %s\n", progname );
603                         exit( EXIT_FAILURE );
604                 }
605                 
606                 if ( nosubordinates == 0 && dbnum > 1 ) {
607                         Debug( LDAP_DEBUG_ANY,
608                                 "The first database does not allow %s;"
609                                 " using the first available one (%d)\n",
610                                 progname, dbnum, 0 );
611                 }
612
613         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
614                 fprintf( stderr,
615                         "Database number selected via -n is out of range\n"
616                         "Must be in the range 0 to %d"
617                         " (number of configured databases)\n",
618                         nbackends-1 );
619                 exit( EXIT_FAILURE );
620
621         } else {
622                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
623                         if ( dbnum == 0 ) break;
624                         dbnum--;
625                 }
626         }
627
628 startup:;
629
630 #ifdef CSRIMALLOC
631         mal_leaktrace(1);
632 #endif
633
634         if ( conffile != NULL ) {
635                 ch_free( conffile );
636         }
637
638         if ( ldiffile != NULL ) {
639                 ch_free( ldiffile );
640         }
641
642         /* slapdn doesn't specify a backend to startup */
643         if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
644                 switch ( tool ) {
645                 case SLAPTEST:
646                         fprintf( stderr, "slap_startup failed "
647                                         "(test would succeed using "
648                                         "the -u switch)\n" );
649                         break;
650
651                 default:
652                         fprintf( stderr, "slap_startup failed\n" );
653                         break;
654                 }
655                 
656                 exit( EXIT_FAILURE );
657         }
658 }
659
660 void slap_tool_destroy( void )
661 {
662         if ( !dryrun ) {
663                 slap_shutdown( be );
664                 slap_destroy();
665         }
666 #ifdef SLAPD_MODULES
667         if ( slapMode == SLAP_SERVER_MODE ) {
668         /* always false. just pulls in necessary symbol references. */
669                 lutil_uuidstr(NULL, 0);
670         }
671         module_kill();
672 #endif
673         schema_destroy();
674 #ifdef HAVE_TLS
675         ldap_pvt_tls_destroy();
676 #endif
677         config_destroy();
678
679 #ifdef CSRIMALLOC
680         mal_dumpleaktrace( leakfile );
681 #endif
682
683         if ( !BER_BVISNULL( &authcDN ) ) {
684                 ch_free( authcDN.bv_val );
685         }
686 }