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