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