]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
153310f2916b262c2374bc19c4d187c67b9bf579
[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-2013 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 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
51 int start_syslog;
52 static char **syslog_unknowns;
53 #ifdef LOG_LOCAL4
54 static int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
55 #endif /* LOG_LOCAL4 */
56 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
57
58 static void
59 usage( int tool, const char *progname )
60 {
61         char *options = NULL;
62         fprintf( stderr,
63                 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir] [-o <name>[=<value>]]",
64                 progname );
65
66         switch( tool ) {
67         case SLAPACL:
68                 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
69                         "\n\t-b DN [-u] [attr[/access][:value]] [...]\n";
70                 break;
71
72         case SLAPADD:
73                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
74                         "\t[-l ldiffile] [-j linenumber] [-q] [-u] [-s] [-w]\n";
75                 break;
76
77         case SLAPAUTH:
78                 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
79                 break;
80
81         case SLAPCAT:
82                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
83                         " [-l ldiffile] [-a filter] [-s subtree] [-H url]\n";
84                 break;
85
86         case SLAPDN:
87                 options = "\n\t[-N | -P] DN [...]\n";
88                 break;
89
90         case SLAPINDEX:
91                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [attr ...] [-q] [-t]\n";
92                 break;
93
94         case SLAPTEST:
95                 options = " [-n databasenumber] [-u] [-Q]\n";
96                 break;
97
98         case SLAPSCHEMA:
99                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
100                         " [-l errorfile] [-a filter] [-s subtree] [-H url]\n";
101                 break;
102         }
103
104         if ( options != NULL ) {
105                 fputs( options, stderr );
106         }
107         exit( EXIT_FAILURE );
108 }
109
110 static int
111 parse_slapopt( int tool, int *mode )
112 {
113         size_t  len = 0;
114         char    *p;
115
116         p = strchr( optarg, '=' );
117         if ( p != NULL ) {
118                 len = p - optarg;
119                 p++;
120         }
121
122         if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
123                 if ( !BER_BVISNULL( &listener_url ) ) {
124                         ber_memfree( listener_url.bv_val );
125                 }
126                 ber_str2bv( p, 0, 1, &listener_url );
127
128         } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
129                 if ( !BER_BVISNULL( &peer_domain ) ) {
130                         ber_memfree( peer_domain.bv_val );
131                 }
132                 ber_str2bv( p, 0, 1, &peer_domain );
133
134         } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
135                 if ( !BER_BVISNULL( &peer_name ) ) {
136                         ber_memfree( peer_name.bv_val );
137                 }
138                 ber_str2bv( p, 0, 1, &peer_name );
139
140         } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
141                 if ( !BER_BVISNULL( &sock_name ) ) {
142                         ber_memfree( sock_name.bv_val );
143                 }
144                 ber_str2bv( p, 0, 1, &sock_name );
145
146         } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
147                 if ( lutil_atou( &ssf, p ) ) {
148                         Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
149                         return -1;
150                 }
151
152         } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
153                 if ( lutil_atou( &transport_ssf, p ) ) {
154                         Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
155                         return -1;
156                 }
157
158         } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
159                 if ( lutil_atou( &tls_ssf, p ) ) {
160                         Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
161                         return -1;
162                 }
163
164         } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
165                 if ( lutil_atou( &sasl_ssf, p ) ) {
166                         Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
167                         return -1;
168                 }
169
170         } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
171                 ber_str2bv( p, 0, 1, &authzDN );
172
173 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
174         } else if ( strncasecmp( optarg, "syslog", len ) == 0 ) {
175                 if ( parse_debug_level( p, &ldap_syslog, &syslog_unknowns ) ) {
176                         return -1;
177                 }
178                 start_syslog = 1;
179
180         } else if ( strncasecmp( optarg, "syslog-level", len ) == 0 ) {
181                 if ( parse_syslog_level( p, &ldap_syslog_level ) ) {
182                         return -1;
183                 }
184                 start_syslog = 1;
185
186 #ifdef LOG_LOCAL4
187         } else if ( strncasecmp( optarg, "syslog-user", len ) == 0 ) {
188                 if ( parse_syslog_user( p, &syslogUser ) ) {
189                         return -1;
190                 }
191                 start_syslog = 1;
192 #endif /* LOG_LOCAL4 */
193 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
194
195         } else if ( strncasecmp( optarg, "schema-check", len ) == 0 ) {
196                 switch ( tool ) {
197                 case SLAPADD:
198                         if ( strcasecmp( p, "yes" ) == 0 ) {
199                                 *mode &= ~SLAP_TOOL_NO_SCHEMA_CHECK;
200                         } else if ( strcasecmp( p, "no" ) == 0 ) {
201                                 *mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
202                         } else {
203                                 Debug( LDAP_DEBUG_ANY, "unable to parse schema-check=\"%s\".\n", p, 0, 0 );
204                                 return -1;
205                         }
206                         break;
207
208                 default:
209                         Debug( LDAP_DEBUG_ANY, "schema-check meaningless for tool.\n", 0, 0, 0 );
210                         break;
211                 }
212
213         } else if ( strncasecmp( optarg, "value-check", len ) == 0 ) {
214                 switch ( tool ) {
215                 case SLAPADD:
216                         if ( strcasecmp( p, "yes" ) == 0 ) {
217                                 *mode |= SLAP_TOOL_VALUE_CHECK;
218                         } else if ( strcasecmp( p, "no" ) == 0 ) {
219                                 *mode &= ~SLAP_TOOL_VALUE_CHECK;
220                         } else {
221                                 Debug( LDAP_DEBUG_ANY, "unable to parse value-check=\"%s\".\n", p, 0, 0 );
222                                 return -1;
223                         }
224                         break;
225
226                 default:
227                         Debug( LDAP_DEBUG_ANY, "value-check meaningless for tool.\n", 0, 0, 0 );
228                         break;
229                 }
230
231         } else if ( strncasecmp( optarg, "ldif-wrap", len ) == 0 ) {
232                 switch ( tool ) {
233                 case SLAPCAT:
234                         if ( strcasecmp( p, "no" ) == 0 ) {
235                                 ldif_wrap = LDIF_LINE_WIDTH_MAX;
236
237                         } else {
238                                 unsigned int u;
239                                 if ( lutil_atou( &u, p ) ) {
240                                         Debug( LDAP_DEBUG_ANY, "unable to parse ldif-wrap=\"%s\".\n", p, 0, 0 );
241                                         return -1;
242                                 }
243                                 ldif_wrap = (ber_len_t)u;
244                         }
245                         break;
246
247                 default:
248                         Debug( LDAP_DEBUG_ANY, "value-check meaningless for tool.\n", 0, 0, 0 );
249                         break;
250                 }
251
252         } else {
253                 return -1;
254         }
255
256         return 0;
257 }
258
259 /*
260  * slap_tool_init - initialize slap utility, handle program options.
261  * arguments:
262  *      name            program name
263  *      tool            tool code
264  *      argc, argv      command line arguments
265  */
266
267 static int need_shutdown;
268
269 void
270 slap_tool_init(
271         const char* progname,
272         int tool,
273         int argc, char **argv )
274 {
275         char *options;
276         char *conffile = NULL;
277         char *confdir = NULL;
278         struct berval base = BER_BVNULL;
279         char *filterstr = NULL;
280         char *subtree = NULL;
281         char *ldiffile  = NULL;
282         char **debug_unknowns = NULL;
283         int rc, i;
284         int mode = SLAP_TOOL_MODE;
285         int truncatemode = 0;
286         int use_glue = 1;
287         int writer;
288
289 #ifdef LDAP_DEBUG
290         /* tools default to "none", so that at least LDAP_DEBUG_ANY 
291          * messages show up; use -d 0 to reset */
292         slap_debug = LDAP_DEBUG_NONE;
293         ldif_debug = slap_debug;
294 #endif
295         ldap_syslog = 0;
296
297 #ifdef CSRIMALLOC
298         leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
299         sprintf( leakfilename, "%s.leak", progname );
300         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
301                 leakfile = stderr;
302         }
303         free( leakfilename );
304         leakfilename = NULL;
305 #endif
306
307         ldif_wrap = LDIF_LINE_WIDTH;
308
309         scope = LDAP_SCOPE_DEFAULT;
310
311         switch( tool ) {
312         case SLAPADD:
313                 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
314                 break;
315
316         case SLAPCAT:
317                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
318                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
319                 break;
320
321         case SLAPDN:
322                 options = "d:f:F:No:Pv";
323                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
324                 break;
325
326         case SLAPMODIFY:
327                 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
328                 break;
329
330         case SLAPSCHEMA:
331                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
332                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
333                 break;
334
335         case SLAPTEST:
336                 options = "d:f:F:n:o:Quv";
337                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
338                 break;
339
340         case SLAPAUTH:
341                 options = "d:f:F:M:o:R:U:vX:";
342                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
343                 break;
344
345         case SLAPINDEX:
346                 options = "b:cd:f:F:gn:o:qtv";
347                 mode |= SLAP_TOOL_READMAIN;
348                 break;
349
350         case SLAPACL:
351                 options = "b:D:d:f:F:o:uU:vX:";
352                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
353                 break;
354
355         default:
356                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
357                 exit( EXIT_FAILURE );
358         }
359
360         dbnum = -1;
361         while ( (i = getopt( argc, argv, options )) != EOF ) {
362                 switch ( i ) {
363                 case 'a':
364                         filterstr = ch_strdup( optarg );
365                         break;
366
367                 case 'b':
368                         ber_str2bv( optarg, 0, 1, &base );
369                         break;
370
371                 case 'c':       /* enable continue mode */
372                         continuemode++;
373                         break;
374
375                 case 'd': {     /* turn on debugging */
376                         int     level = 0;
377
378                         if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
379                                 usage( tool, progname );
380                         }
381 #ifdef LDAP_DEBUG
382                         if ( level == 0 ) {
383                                 /* allow to reset log level */
384                                 slap_debug = 0;
385
386                         } else {
387                                 slap_debug |= level;
388                         }
389 #else
390                         if ( level != 0 )
391                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
392                                        stderr );
393 #endif
394                         } break;
395
396                 case 'D':
397                         ber_str2bv( optarg, 0, 1, &authcDN );
398                         break;
399
400                 case 'f':       /* specify a conf file */
401                         conffile = ch_strdup( optarg );
402                         break;
403
404                 case 'F':       /* specify a conf dir */
405                         confdir = ch_strdup( optarg );
406                         break;
407
408                 case 'g':       /* disable subordinate glue */
409                         use_glue = 0;
410                         break;
411
412                 case 'H': {
413                         LDAPURLDesc *ludp;
414                         int rc;
415
416                         rc = ldap_url_parse_ext( optarg, &ludp,
417                                 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | LDAP_PVT_URL_PARSE_NOEMPTY_DN );
418                         if ( rc != LDAP_URL_SUCCESS ) {
419                                 usage( tool, progname );
420                         }
421
422                         /* don't accept host, port, attrs, extensions */
423                         if ( ldap_pvt_url_scheme2proto( ludp->lud_scheme ) != LDAP_PROTO_TCP ) {
424                                 usage( tool, progname );
425                         }
426
427                         if ( ludp->lud_host != NULL ) {
428                                 usage( tool, progname );
429                         }
430
431                         if ( ludp->lud_port != 0 ) {
432                                 usage( tool, progname );
433                         }
434
435                         if ( ludp->lud_attrs != NULL ) {
436                                 usage( tool, progname );
437                         }
438
439                         if ( ludp->lud_exts != NULL ) {
440                                 usage( tool, progname );
441                         }
442
443                         if ( ludp->lud_dn != NULL && ludp->lud_dn[0] != '\0' ) {
444                                 subtree = ludp->lud_dn;
445                                 ludp->lud_dn = NULL;
446                         }
447
448                         if ( ludp->lud_filter != NULL && ludp->lud_filter[0] != '\0' ) {
449                                 filterstr = ludp->lud_filter;
450                                 ludp->lud_filter = NULL;
451                         }
452
453                         scope = ludp->lud_scope;
454
455                         ldap_free_urldesc( ludp );
456                         } break;
457
458                 case 'j':       /* jump to linenumber */
459                         if ( lutil_atoul( &jumpline, optarg ) ) {
460                                 usage( tool, progname );
461                         }
462                         break;
463
464                 case 'l':       /* LDIF file */
465                         ldiffile = ch_strdup( optarg );
466                         break;
467
468                 case 'M':
469                         ber_str2bv( optarg, 0, 0, &mech );
470                         break;
471
472                 case 'N':
473                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
474                                 usage( tool, progname );
475                         }
476                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
477                         break;
478
479                 case 'n':       /* which config file db to index */
480                         if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
481                                 usage( tool, progname );
482                         }
483                         break;
484
485                 case 'o':
486                         if ( parse_slapopt( tool, &mode ) ) {
487                                 usage( tool, progname );
488                         }
489                         break;
490
491                 case 'P':
492                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
493                                 usage( tool, progname );
494                         }
495                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
496                         break;
497
498                 case 'Q':
499                         quiet++;
500                         slap_debug = 0;
501                         break;
502
503                 case 'q':       /* turn on quick */
504                         mode |= SLAP_TOOL_QUICK;
505                         break;
506
507                 case 'R':
508                         realm = optarg;
509                         break;
510
511                 case 'S':
512                         if ( lutil_atou( &csnsid, optarg )
513                                 || csnsid > SLAP_SYNC_SID_MAX )
514                         {
515                                 usage( tool, progname );
516                         }
517                         break;
518
519                 case 's':
520                         switch ( tool ) {
521                         case SLAPADD:
522                         case SLAPMODIFY:
523                                 /* no schema check */
524                                 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
525                                 break;
526
527                         case SLAPCAT:
528                         case SLAPSCHEMA:
529                                 /* dump subtree */
530                                 subtree = ch_strdup( optarg );
531                                 break;
532                         }
533                         break;
534
535                 case 't':       /* turn on truncate */
536                         truncatemode++;
537                         mode |= SLAP_TRUNCATE_MODE;
538                         break;
539
540                 case 'U':
541                         ber_str2bv( optarg, 0, 0, &authcID );
542                         break;
543
544                 case 'u':       /* dry run */
545                         dryrun++;
546                         break;
547
548                 case 'v':       /* turn on verbose */
549                         verbose++;
550                         break;
551
552                 case 'w':       /* write context csn at the end */
553                         update_ctxcsn++;
554                         break;
555
556                 case 'X':
557                         ber_str2bv( optarg, 0, 0, &authzID );
558                         break;
559
560                 default:
561                         usage( tool, progname );
562                         break;
563                 }
564         }
565
566 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
567         if ( start_syslog ) {
568                 char *logName;
569 #ifdef HAVE_EBCDIC
570                 logName = ch_strdup( progname );
571                 __atoe( logName );
572 #else
573                 logName = (char *)progname;
574 #endif
575
576 #ifdef LOG_LOCAL4
577                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
578 #elif defined LOG_DEBUG
579                 openlog( logName, OPENLOG_OPTIONS );
580 #endif
581 #ifdef HAVE_EBCDIC
582                 free( logName );
583                 logName = NULL;
584 #endif
585         }
586 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
587
588         switch ( tool ) {
589         case SLAPCAT:
590         case SLAPSCHEMA:
591                 writer = 1;
592                 break;
593
594         default:
595                 writer = 0;
596                 break;
597         }
598
599         switch ( tool ) {
600         case SLAPADD:
601         case SLAPCAT:
602         case SLAPMODIFY:
603         case SLAPSCHEMA:
604                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
605                         usage( tool, progname );
606                 }
607
608                 break;
609
610         case SLAPINDEX:
611                 if ( dbnum >= 0 && base.bv_val != NULL ) {
612                         usage( tool, progname );
613                 }
614
615                 break;
616
617         case SLAPDN:
618                 if ( argc == optind ) {
619                         usage( tool, progname );
620                 }
621                 break;
622
623         case SLAPAUTH:
624                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
625                         usage( tool, progname );
626                 }
627                 break;
628
629         case SLAPTEST:
630                 if ( argc != optind ) {
631                         usage( tool, progname );
632                 }
633                 break;
634
635         case SLAPACL:
636                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
637                         usage( tool, progname );
638                 }
639                 if ( BER_BVISNULL( &base ) ) {
640                         usage( tool, progname );
641                 }
642                 ber_dupbv( &baseDN, &base );
643                 break;
644
645         default:
646                 break;
647         }
648
649         if ( ldiffile == NULL ) {
650                 dummy.fp = writer ? stdout : stdin;
651                 ldiffp = &dummy;
652
653         } else if ((ldiffp = ldif_open( ldiffile, writer ? "w" : "r" ))
654                 == NULL )
655         {
656                 perror( ldiffile );
657                 exit( EXIT_FAILURE );
658         }
659
660         /*
661          * initialize stuff and figure out which backend we're dealing with
662          */
663
664         rc = slap_init( mode, progname );
665         if ( rc != 0 ) {
666                 fprintf( stderr, "%s: slap_init failed!\n", progname );
667                 exit( EXIT_FAILURE );
668         }
669
670         rc = read_config( conffile, confdir );
671
672         if ( rc != 0 ) {
673                 fprintf( stderr, "%s: bad configuration %s!\n",
674                         progname, confdir ? "directory" : "file" );
675                 exit( EXIT_FAILURE );
676         }
677
678         if ( debug_unknowns ) {
679                 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
680                 ldap_charray_free( debug_unknowns );
681                 debug_unknowns = NULL;
682                 if ( rc )
683                         exit( EXIT_FAILURE );
684         }
685
686 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
687         if ( syslog_unknowns ) {
688                 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
689                 ldap_charray_free( syslog_unknowns );
690                 syslog_unknowns = NULL;
691                 if ( rc )
692                         exit( EXIT_FAILURE );
693         }
694 #endif
695
696         at_oc_cache = 1;
697
698         switch ( tool ) {
699         case SLAPADD:
700         case SLAPCAT:
701         case SLAPINDEX:
702         case SLAPMODIFY:
703         case SLAPSCHEMA:
704                 if ( !nbackends ) {
705                         fprintf( stderr, "No databases found "
706                                         "in config file\n" );
707                         exit( EXIT_FAILURE );
708                 }
709                 break;
710
711         default:
712                 break;
713         }
714
715         if ( use_glue ) {
716                 rc = glue_sub_attach( 0 );
717
718                 if ( rc != 0 ) {
719                         fprintf( stderr,
720                                 "%s: subordinate configuration error\n", progname );
721                         exit( EXIT_FAILURE );
722                 }
723         }
724
725         rc = slap_schema_check();
726
727         if ( rc != 0 ) {
728                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
729                 exit( EXIT_FAILURE );
730         }
731
732         switch ( tool ) {
733         case SLAPTEST:
734                 if ( dbnum >= 0 )
735                         goto get_db;
736                 /* FALLTHRU */
737         case SLAPDN:
738         case SLAPAUTH:
739                 be = NULL;
740                 goto startup;
741
742         default:
743                 break;
744         }
745
746         if( filterstr ) {
747                 filter = str2filter( filterstr );
748
749                 if( filter == NULL ) {
750                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
751                         exit( EXIT_FAILURE );
752                 }
753
754                 ch_free( filterstr );
755                 filterstr = NULL;
756         }
757
758         if( subtree ) {
759                 struct berval val;
760                 ber_str2bv( subtree, 0, 0, &val );
761                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
762                 if( rc != LDAP_SUCCESS ) {
763                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
764                         exit( EXIT_FAILURE );
765                 }
766
767                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
768                         base = val;
769                 } else {
770                         free( subtree );
771                         subtree = NULL;
772                 }
773         }
774
775         if( base.bv_val != NULL ) {
776                 struct berval nbase;
777
778                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
779                 if( rc != LDAP_SUCCESS ) {
780                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
781                                 progname, base.bv_val );
782                         exit( EXIT_FAILURE );
783                 }
784
785                 be = select_backend( &nbase, 0 );
786                 ber_memfree( nbase.bv_val );
787                 BER_BVZERO( &nbase );
788
789                 switch ( tool ) {
790                 case SLAPACL:
791                         goto startup;
792
793                 default:
794                         break;
795                 }
796
797                 if( be == NULL ) {
798                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
799                                 progname, base.bv_val );
800                         exit( EXIT_FAILURE );
801                 }
802                 /* If the named base is a glue master, operate on the
803                  * entire context
804                  */
805                 if ( SLAP_GLUE_INSTANCE( be ) ) {
806                         nosubordinates = 1;
807                 }
808
809                 ch_free( base.bv_val );
810                 BER_BVZERO( &base );
811
812         } else if ( dbnum == -1 ) {
813                 /* no suffix and no dbnum specified, just default to
814                  * the first available database
815                  */
816                 if ( nbackends <= 0 ) {
817                         fprintf( stderr, "No available databases\n" );
818                         exit( EXIT_FAILURE );
819                 }
820                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
821                         dbnum++;
822
823                         /* db #0 is cn=config, don't select it as a default */
824                         if ( dbnum < 1 ) continue;
825                 
826                         if ( SLAP_MONITOR(be))
827                                 continue;
828
829                 /* If just doing the first by default and it is a
830                  * glue subordinate, find the master.
831                  */
832                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
833                                 nosubordinates = 1;
834                                 continue;
835                         }
836                         break;
837                 }
838
839                 if ( !be ) {
840                         fprintf( stderr, "Available database(s) "
841                                         "do not allow %s\n", progname );
842                         exit( EXIT_FAILURE );
843                 }
844                 
845                 if ( nosubordinates == 0 && dbnum > 1 ) {
846                         Debug( LDAP_DEBUG_ANY,
847                                 "The first database does not allow %s;"
848                                 " using the first available one (%d)\n",
849                                 progname, dbnum, 0 );
850                 }
851
852         } else if ( dbnum >= nbackends ) {
853                 fprintf( stderr,
854                         "Database number selected via -n is out of range\n"
855                         "Must be in the range 0 to %d"
856                         " (the number of configured databases)\n",
857                         nbackends - 1 );
858                 exit( EXIT_FAILURE );
859
860         } else {
861 get_db:
862                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
863                         if ( dbnum == 0 ) break;
864                         dbnum--;
865                 }
866         }
867
868         if ( scope != LDAP_SCOPE_DEFAULT && BER_BVISNULL( &sub_ndn ) ) {
869                 if ( be && be->be_nsuffix ) {
870                         ber_dupbv( &sub_ndn, be->be_nsuffix );
871
872                 } else {
873                         fprintf( stderr,
874                                 "<scope> needs a DN or a valid database\n" );
875                         exit( EXIT_FAILURE );
876                 }
877         }
878
879 startup:;
880         if ( be ) {
881                 BackendDB *bdtmp;
882
883                 dbnum = 0;
884                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
885                         if ( bdtmp == be ) break;
886                         dbnum++;
887                 }
888         }
889
890 #ifdef CSRIMALLOC
891         mal_leaktrace(1);
892 #endif
893
894         if ( conffile != NULL ) {
895                 ch_free( conffile );
896                 conffile = NULL;
897         }
898
899         if ( confdir != NULL ) {
900                 ch_free( confdir );
901                 confdir = NULL;
902         }
903
904         if ( ldiffile != NULL ) {
905                 ch_free( ldiffile );
906                 ldiffile = NULL;
907         }
908
909         /* slapdn doesn't specify a backend to startup */
910         if ( !dryrun && tool != SLAPDN ) {
911                 need_shutdown = 1;
912
913                 if ( slap_startup( be ) ) {
914                         switch ( tool ) {
915                         case SLAPTEST:
916                                 fprintf( stderr, "slap_startup failed "
917                                                 "(test would succeed using "
918                                                 "the -u switch)\n" );
919                                 break;
920
921                         default:
922                                 fprintf( stderr, "slap_startup failed\n" );
923                                 break;
924                         }
925
926                         exit( EXIT_FAILURE );
927                 }
928         }
929 }
930
931 int slap_tool_destroy( void )
932 {
933         int rc = 0;
934         if ( !dryrun ) {
935                 if ( need_shutdown ) {
936                         if ( slap_shutdown( be ))
937                                 rc = EXIT_FAILURE;
938                 }
939                 if ( slap_destroy())
940                         rc = EXIT_FAILURE;
941         }
942 #ifdef SLAPD_MODULES
943         if ( slapMode == SLAP_SERVER_MODE ) {
944         /* always false. just pulls in necessary symbol references. */
945                 lutil_uuidstr(NULL, 0);
946         }
947         module_kill();
948 #endif
949         schema_destroy();
950 #ifdef HAVE_TLS
951         ldap_pvt_tls_destroy();
952 #endif
953         config_destroy();
954
955 #ifdef CSRIMALLOC
956         mal_dumpleaktrace( leakfile );
957 #endif
958
959         if ( !BER_BVISNULL( &authcDN ) ) {
960                 ch_free( authcDN.bv_val );
961                 BER_BVZERO( &authcDN );
962         }
963
964         if ( ldiffp && ldiffp != &dummy ) {
965                 ldif_close( ldiffp );
966         }
967         return rc;
968 }
969
970 int
971 slap_tool_update_ctxcsn(
972         const char *progname,
973         unsigned long sid,
974         struct berval *bvtext )
975 {
976         struct berval ctxdn;
977         ID ctxcsn_id;
978         Entry *ctxcsn_e;
979         int rc = EXIT_SUCCESS;
980
981         if ( !(update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1) ) {
982                 return rc;
983         }
984
985         if ( SLAP_SYNC_SUBENTRY( be )) {
986                 build_new_dn( &ctxdn, &be->be_nsuffix[0],
987                         (struct berval *)&slap_ldapsync_cn_bv, NULL );
988         } else {
989                 ctxdn = be->be_nsuffix[0];
990         }
991         ctxcsn_id = be->be_dn2id_get( be, &ctxdn );
992         if ( ctxcsn_id == NOID ) {
993                 if ( SLAP_SYNC_SUBENTRY( be )) {
994                         ctxcsn_e = slap_create_context_csn_entry( be, NULL );
995                         for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
996                                 if ( maxcsn[ sid ].bv_len ) {
997                                         attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN,
998                                                 &maxcsn[ sid ], NULL );
999                                 }
1000                         }
1001                         ctxcsn_id = be->be_entry_put( be, ctxcsn_e, bvtext );
1002                         if ( ctxcsn_id == NOID ) {
1003                                 fprintf( stderr, "%s: couldn't create context entry\n", progname );
1004                                 rc = EXIT_FAILURE;
1005                         }
1006                 } else {
1007                         fprintf( stderr, "%s: context entry is missing\n", progname );
1008                         rc = EXIT_FAILURE;
1009                 }
1010         } else {
1011                 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
1012                 if ( ctxcsn_e != NULL ) {
1013                         Entry *e = entry_dup( ctxcsn_e );
1014                         int change;
1015                         Attribute *attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
1016                         if ( attr ) {
1017                                 int             i;
1018
1019                                 change = 0;
1020
1021                                 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
1022                                         int rc_sid;
1023                                         int match;
1024                                         const char *text = NULL;
1025
1026                                         rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
1027                                         if ( rc_sid < 0 ) {
1028                                                 Debug( LDAP_DEBUG_ANY,
1029                                                         "%s: unable to extract SID "
1030                                                         "from #%d contextCSN=%s\n",
1031                                                         progname, i,
1032                                                         attr->a_nvals[ i ].bv_val );
1033                                                 continue;
1034                                         }
1035
1036                                         assert( rc_sid <= SLAP_SYNC_SID_MAX );
1037
1038                                         sid = (unsigned)rc_sid;
1039
1040                                         if ( maxcsn[ sid ].bv_len == 0 ) {
1041                                                 match = -1;
1042
1043                                         } else {
1044                                                 value_match( &match, slap_schema.si_ad_entryCSN,
1045                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
1046                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1047                                                         &maxcsn[ sid ], &attr->a_nvals[i], &text );
1048                                         }
1049
1050                                         if ( match > 0 ) {
1051                                                 change = 1;
1052                                         } else {
1053                                                 AC_MEMCPY( maxcsn[ sid ].bv_val,
1054                                                         attr->a_nvals[ i ].bv_val,
1055                                                         attr->a_nvals[ i ].bv_len );
1056                                                 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
1057                                                 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
1058                                         }
1059                                 }
1060
1061                                 if ( change ) {
1062                                         if ( attr->a_nvals != attr->a_vals ) {
1063                                                 ber_bvarray_free( attr->a_nvals );
1064                                         }
1065                                         attr->a_nvals = NULL;
1066                                         ber_bvarray_free( attr->a_vals );
1067                                         attr->a_vals = NULL;
1068                                         attr->a_numvals = 0;
1069                                 }
1070                         } else {
1071                                 change = 1;
1072                         }
1073
1074                         if ( change ) {
1075                                 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
1076                                         if ( maxcsn[ sid ].bv_len ) {
1077                                                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
1078                                                         &maxcsn[ sid], NULL );
1079                                         }
1080                                 }
1081
1082                                 ctxcsn_id = be->be_entry_modify( be, e, bvtext );
1083                                 if( ctxcsn_id == NOID ) {
1084                                         fprintf( stderr, "%s: could not modify ctxcsn (%s)\n",
1085                                                 progname, bvtext->bv_val ? bvtext->bv_val : "" );
1086                                         rc = EXIT_FAILURE;
1087                                 } else if ( verbose ) {
1088                                         fprintf( stderr, "modified: \"%s\" (%08lx)\n",
1089                                                 e->e_dn, (long) ctxcsn_id );
1090                                 }
1091                         }
1092                         entry_free( e );
1093                 }
1094         } 
1095
1096         return rc;
1097 }
1098
1099 /*
1100  * return value:
1101  *      -1:                     update_ctxcsn == 0
1102  *      SLAP_SYNC_SID_MAX + 1:  unable to extract SID
1103  *      0 <= SLAP_SYNC_SID_MAX: the SID
1104  */
1105 unsigned long
1106 slap_tool_update_ctxcsn_check(
1107         const char *progname,
1108         Entry *e )
1109 {
1110         if ( update_ctxcsn ) {
1111                 unsigned long sid = SLAP_SYNC_SID_MAX + 1;
1112                 int rc_sid;
1113                 Attribute *attr;
1114
1115                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
1116                 assert( attr != NULL );
1117
1118                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
1119                 if ( rc_sid < 0 ) {
1120                         Debug( LDAP_DEBUG_ANY, "%s: could not "
1121                                 "extract SID from entryCSN=%s, entry dn=\"%s\"\n",
1122                                 progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val );
1123                         return (unsigned long)(-1);
1124
1125                 } else {
1126                         int match;
1127                         const char *text = NULL;
1128
1129                         assert( rc_sid <= SLAP_SYNC_SID_MAX );
1130
1131                         sid = (unsigned)rc_sid;
1132                         if ( maxcsn[ sid ].bv_len != 0 ) {
1133                                 match = 0;
1134                                 value_match( &match, slap_schema.si_ad_entryCSN,
1135                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
1136                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1137                                         &maxcsn[ sid ], &attr->a_nvals[0], &text );
1138                         } else {
1139                                 match = -1;
1140                         }
1141                         if ( match < 0 ) {
1142                                 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
1143                                 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
1144                         }
1145                 }
1146         }
1147
1148         return (unsigned long)(-1);
1149 }
1150
1151 int
1152 slap_tool_update_ctxcsn_init(void)
1153 {
1154         if ( update_ctxcsn ) {
1155                 unsigned long sid;
1156                 maxcsn[ 0 ].bv_val = maxcsnbuf;
1157                 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
1158                         maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_PVT_CSNSTR_BUFSIZE;
1159                         maxcsn[ sid ].bv_len = 0;
1160                 }
1161         }
1162
1163         return 0;
1164 }
1165
1166 int
1167 slap_tool_entry_check(
1168         const char *progname,
1169         Operation *op,
1170         Entry *e,
1171         int lineno,
1172         const char **text,
1173         char *textbuf,
1174         size_t textlen )
1175 {
1176         /* NOTE: we may want to conditionally enable manage */
1177         int manage = 0;
1178
1179         Attribute *oc = attr_find( e->e_attrs,
1180                 slap_schema.si_ad_objectClass );
1181
1182         if( oc == NULL ) {
1183                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
1184                         progname, e->e_dn, lineno,
1185                         "no objectClass attribute");
1186                 return LDAP_NO_SUCH_ATTRIBUTE;
1187         }
1188
1189         /* check schema */
1190         op->o_bd = be;
1191
1192         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
1193                 int rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
1194                         text, textbuf, textlen );
1195
1196                 if( rc != LDAP_SUCCESS ) {
1197                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
1198                                 progname, e->e_dn, lineno, rc, *text );
1199                         return rc;
1200                 }
1201                 textbuf[ 0 ] = '\0';
1202         }
1203
1204         if ( (slapMode & SLAP_TOOL_VALUE_CHECK) != 0) {
1205                 Modifications *ml = NULL;
1206
1207                 int rc = slap_entry2mods( e, &ml, text, textbuf, textlen );
1208                 if ( rc != LDAP_SUCCESS ) {
1209                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
1210                                 progname, e->e_dn, lineno, rc, *text );
1211                         return rc;
1212                 }
1213                 textbuf[ 0 ] = '\0';
1214
1215                 rc = slap_mods_check( op, ml, text, textbuf, textlen, NULL );
1216                 slap_mods_free( ml, 1 );
1217                 if ( rc != LDAP_SUCCESS ) {
1218                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
1219                                 progname, e->e_dn, lineno, rc, *text );
1220                         return rc;
1221                 }
1222                 textbuf[ 0 ] = '\0';
1223         }
1224
1225         return LDAP_SUCCESS;
1226 }
1227