]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
Fix ldif-wrap errmsg typo
[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-2018 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, "ldif-wrap 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 SLAPSCHEMA:
327                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
328                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
329                 break;
330
331         case SLAPTEST:
332                 options = "d:f:F:n:o:Quv";
333                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
334                 break;
335
336         case SLAPAUTH:
337                 options = "d:f:F:M:o:R:U:vX:";
338                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
339                 break;
340
341         case SLAPINDEX:
342                 options = "b:cd:f:F:gn:o:qtv";
343                 mode |= SLAP_TOOL_READMAIN;
344                 break;
345
346         case SLAPACL:
347                 options = "b:D:d:f:F:o:uU:vX:";
348                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
349                 break;
350
351         default:
352                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
353                 exit( EXIT_FAILURE );
354         }
355
356         dbnum = -1;
357         while ( (i = getopt( argc, argv, options )) != EOF ) {
358                 switch ( i ) {
359                 case 'a':
360                         filterstr = ch_strdup( optarg );
361                         break;
362
363                 case 'b':
364                         ber_str2bv( optarg, 0, 1, &base );
365                         break;
366
367                 case 'c':       /* enable continue mode */
368                         continuemode++;
369                         break;
370
371                 case 'd': {     /* turn on debugging */
372                         int     level = 0;
373
374                         if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
375                                 usage( tool, progname );
376                         }
377 #ifdef LDAP_DEBUG
378                         if ( level == 0 ) {
379                                 /* allow to reset log level */
380                                 slap_debug = 0;
381
382                         } else {
383                                 slap_debug |= level;
384                         }
385 #else
386                         if ( level != 0 )
387                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
388                                        stderr );
389 #endif
390                         } break;
391
392                 case 'D':
393                         ber_str2bv( optarg, 0, 1, &authcDN );
394                         break;
395
396                 case 'f':       /* specify a conf file */
397                         conffile = ch_strdup( optarg );
398                         break;
399
400                 case 'F':       /* specify a conf dir */
401                         confdir = ch_strdup( optarg );
402                         break;
403
404                 case 'g':       /* disable subordinate glue */
405                         use_glue = 0;
406                         break;
407
408                 case 'H': {
409                         LDAPURLDesc *ludp;
410                         int rc;
411
412                         rc = ldap_url_parse_ext( optarg, &ludp,
413                                 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | LDAP_PVT_URL_PARSE_NOEMPTY_DN );
414                         if ( rc != LDAP_URL_SUCCESS ) {
415                                 usage( tool, progname );
416                         }
417
418                         /* don't accept host, port, attrs, extensions */
419                         if ( ldap_pvt_url_scheme2proto( ludp->lud_scheme ) != LDAP_PROTO_TCP ) {
420                                 usage( tool, progname );
421                         }
422
423                         if ( ludp->lud_host != NULL ) {
424                                 usage( tool, progname );
425                         }
426
427                         if ( ludp->lud_port != 0 ) {
428                                 usage( tool, progname );
429                         }
430
431                         if ( ludp->lud_attrs != NULL ) {
432                                 usage( tool, progname );
433                         }
434
435                         if ( ludp->lud_exts != NULL ) {
436                                 usage( tool, progname );
437                         }
438
439                         if ( ludp->lud_dn != NULL && ludp->lud_dn[0] != '\0' ) {
440                                 ch_free( subtree );
441                                 subtree = ludp->lud_dn;
442                                 ludp->lud_dn = NULL;
443                         }
444
445                         if ( ludp->lud_filter != NULL && ludp->lud_filter[0] != '\0' ) {
446                                 filterstr = ludp->lud_filter;
447                                 ludp->lud_filter = NULL;
448                         }
449
450                         scope = ludp->lud_scope;
451
452                         ldap_free_urldesc( ludp );
453                         } break;
454
455                 case 'j':       /* jump to linenumber */
456                         if ( lutil_atoul( &jumpline, optarg ) ) {
457                                 usage( tool, progname );
458                         }
459                         break;
460
461                 case 'l':       /* LDIF file */
462                         ldiffile = ch_strdup( optarg );
463                         break;
464
465                 case 'M':
466                         ber_str2bv( optarg, 0, 0, &mech );
467                         break;
468
469                 case 'N':
470                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
471                                 usage( tool, progname );
472                         }
473                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
474                         break;
475
476                 case 'n':       /* which config file db to index */
477                         if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
478                                 usage( tool, progname );
479                         }
480                         break;
481
482                 case 'o':
483                         if ( parse_slapopt( tool, &mode ) ) {
484                                 usage( tool, progname );
485                         }
486                         break;
487
488                 case 'P':
489                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
490                                 usage( tool, progname );
491                         }
492                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
493                         break;
494
495                 case 'Q':
496                         quiet++;
497                         slap_debug = 0;
498                         break;
499
500                 case 'q':       /* turn on quick */
501                         mode |= SLAP_TOOL_QUICK;
502                         break;
503
504                 case 'R':
505                         realm = optarg;
506                         break;
507
508                 case 'S':
509                         if ( lutil_atou( &csnsid, optarg )
510                                 || csnsid > SLAP_SYNC_SID_MAX )
511                         {
512                                 usage( tool, progname );
513                         }
514                         break;
515
516                 case 's':
517                         switch ( tool ) {
518                         case SLAPADD:
519                                 /* no schema check */
520                                 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
521                                 break;
522
523                         case SLAPCAT:
524                         case SLAPSCHEMA:
525                                 /* dump subtree */
526                                 ch_free( subtree );
527                                 subtree = ch_strdup( optarg );
528                                 break;
529                         }
530                         break;
531
532                 case 't':       /* turn on truncate */
533                         truncatemode++;
534                         mode |= SLAP_TRUNCATE_MODE;
535                         break;
536
537                 case 'U':
538                         ber_str2bv( optarg, 0, 0, &authcID );
539                         break;
540
541                 case 'u':       /* dry run */
542                         dryrun++;
543                         break;
544
545                 case 'v':       /* turn on verbose */
546                         verbose++;
547                         break;
548
549                 case 'w':       /* write context csn at the end */
550                         update_ctxcsn++;
551                         break;
552
553                 case 'X':
554                         ber_str2bv( optarg, 0, 0, &authzID );
555                         break;
556
557                 default:
558                         usage( tool, progname );
559                         break;
560                 }
561         }
562
563 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
564         if ( start_syslog ) {
565                 char *logName;
566 #ifdef HAVE_EBCDIC
567                 logName = ch_strdup( progname );
568                 __atoe( logName );
569 #else
570                 logName = (char *)progname;
571 #endif
572
573 #ifdef LOG_LOCAL4
574                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
575 #elif defined LOG_DEBUG
576                 openlog( logName, OPENLOG_OPTIONS );
577 #endif
578 #ifdef HAVE_EBCDIC
579                 free( logName );
580                 logName = NULL;
581 #endif
582         }
583 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
584
585         switch ( tool ) {
586         case SLAPCAT:
587         case SLAPSCHEMA:
588                 writer = 1;
589                 break;
590
591         default:
592                 writer = 0;
593                 break;
594         }
595
596         switch ( tool ) {
597         case SLAPADD:
598         case SLAPCAT:
599         case SLAPSCHEMA:
600                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
601                         usage( tool, progname );
602                 }
603
604                 break;
605
606         case SLAPINDEX:
607                 if ( dbnum >= 0 && base.bv_val != NULL ) {
608                         usage( tool, progname );
609                 }
610
611                 break;
612
613         case SLAPDN:
614                 if ( argc == optind ) {
615                         usage( tool, progname );
616                 }
617                 break;
618
619         case SLAPAUTH:
620                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
621                         usage( tool, progname );
622                 }
623                 break;
624
625         case SLAPTEST:
626                 if ( argc != optind ) {
627                         usage( tool, progname );
628                 }
629                 break;
630
631         case SLAPACL:
632                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
633                         usage( tool, progname );
634                 }
635                 if ( BER_BVISNULL( &base ) ) {
636                         usage( tool, progname );
637                 }
638                 ber_dupbv( &baseDN, &base );
639                 break;
640
641         default:
642                 break;
643         }
644
645         if ( ldiffile == NULL ) {
646                 dummy.fp = writer ? stdout : stdin;
647                 ldiffp = &dummy;
648
649         } else if ((ldiffp = ldif_open( ldiffile, writer ? "w" : "r" ))
650                 == NULL )
651         {
652                 perror( ldiffile );
653                 exit( EXIT_FAILURE );
654         }
655
656         /*
657          * initialize stuff and figure out which backend we're dealing with
658          */
659
660         rc = slap_init( mode, progname );
661         if ( rc != 0 ) {
662                 fprintf( stderr, "%s: slap_init failed!\n", progname );
663                 exit( EXIT_FAILURE );
664         }
665
666         rc = read_config( conffile, confdir );
667
668         if ( rc != 0 ) {
669                 fprintf( stderr, "%s: bad configuration %s!\n",
670                         progname, confdir ? "directory" : "file" );
671                 exit( EXIT_FAILURE );
672         }
673
674         if ( debug_unknowns ) {
675                 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
676                 ldap_charray_free( debug_unknowns );
677                 debug_unknowns = NULL;
678                 if ( rc )
679                         exit( EXIT_FAILURE );
680         }
681
682 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
683         if ( syslog_unknowns ) {
684                 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
685                 ldap_charray_free( syslog_unknowns );
686                 syslog_unknowns = NULL;
687                 if ( rc )
688                         exit( EXIT_FAILURE );
689         }
690 #endif
691
692         at_oc_cache = 1;
693
694         switch ( tool ) {
695         case SLAPADD:
696         case SLAPCAT:
697         case SLAPINDEX:
698         case SLAPSCHEMA:
699                 if ( !nbackends ) {
700                         fprintf( stderr, "No databases found "
701                                         "in config file\n" );
702                         exit( EXIT_FAILURE );
703                 }
704                 break;
705
706         default:
707                 break;
708         }
709
710         if ( use_glue ) {
711                 rc = glue_sub_attach( 0 );
712
713                 if ( rc != 0 ) {
714                         fprintf( stderr,
715                                 "%s: subordinate configuration error\n", progname );
716                         exit( EXIT_FAILURE );
717                 }
718         }
719
720         rc = slap_schema_check();
721
722         if ( rc != 0 ) {
723                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
724                 exit( EXIT_FAILURE );
725         }
726
727         switch ( tool ) {
728         case SLAPTEST:
729                 if ( dbnum >= 0 )
730                         goto get_db;
731                 /* FALLTHRU */
732         case SLAPDN:
733         case SLAPAUTH:
734                 be = NULL;
735                 goto startup;
736
737         default:
738                 break;
739         }
740
741         if( filterstr ) {
742                 filter = str2filter( filterstr );
743
744                 if( filter == NULL ) {
745                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
746                         exit( EXIT_FAILURE );
747                 }
748
749                 ch_free( filterstr );
750                 filterstr = NULL;
751         }
752
753         if( subtree ) {
754                 struct berval val;
755                 ber_str2bv( subtree, 0, 0, &val );
756                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
757                 if( rc != LDAP_SUCCESS ) {
758                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
759                         exit( EXIT_FAILURE );
760                 }
761
762                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
763                         base = val;
764                 } else {
765                         free( subtree );
766                         subtree = NULL;
767                 }
768         }
769
770         if( base.bv_val != NULL ) {
771                 struct berval nbase;
772
773                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
774                 if( rc != LDAP_SUCCESS ) {
775                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
776                                 progname, base.bv_val );
777                         exit( EXIT_FAILURE );
778                 }
779
780                 be = select_backend( &nbase, 0 );
781                 ber_memfree( nbase.bv_val );
782                 BER_BVZERO( &nbase );
783
784                 if( be == NULL ) {
785                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
786                                 progname, base.bv_val );
787                         exit( EXIT_FAILURE );
788                 }
789                 switch ( tool ) {
790                 case SLAPACL:
791                         goto startup;
792
793                 default:
794                         break;
795                 }
796
797                 /* If the named base is a glue master, operate on the
798                  * entire context
799                  */
800                 if ( SLAP_GLUE_INSTANCE( be ) ) {
801                         nosubordinates = 1;
802                 }
803
804                 ch_free( base.bv_val );
805                 BER_BVZERO( &base );
806
807         } else if ( dbnum == -1 ) {
808                 /* no suffix and no dbnum specified, just default to
809                  * the first available database
810                  */
811                 if ( nbackends <= 0 ) {
812                         fprintf( stderr, "No available databases\n" );
813                         exit( EXIT_FAILURE );
814                 }
815                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
816                         dbnum++;
817
818                         /* db #0 is cn=config, don't select it as a default */
819                         if ( dbnum < 1 ) continue;
820                 
821                         if ( SLAP_MONITOR(be))
822                                 continue;
823
824                 /* If just doing the first by default and it is a
825                  * glue subordinate, find the master.
826                  */
827                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
828                                 nosubordinates = 1;
829                                 continue;
830                         }
831                         break;
832                 }
833
834                 if ( !be ) {
835                         fprintf( stderr, "Available database(s) "
836                                         "do not allow %s\n", progname );
837                         exit( EXIT_FAILURE );
838                 }
839                 
840                 if ( nosubordinates == 0 && dbnum > 1 ) {
841                         Debug( LDAP_DEBUG_ANY,
842                                 "The first database does not allow %s;"
843                                 " using the first available one (%d)\n",
844                                 progname, dbnum, 0 );
845                 }
846
847         } else if ( dbnum >= nbackends ) {
848                 fprintf( stderr,
849                         "Database number selected via -n is out of range\n"
850                         "Must be in the range 0 to %d"
851                         " (the number of configured databases)\n",
852                         nbackends - 1 );
853                 exit( EXIT_FAILURE );
854
855         } else {
856 get_db:
857                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
858                         if ( dbnum == 0 ) break;
859                         dbnum--;
860                 }
861         }
862
863         if ( scope != LDAP_SCOPE_DEFAULT && BER_BVISNULL( &sub_ndn ) ) {
864                 if ( be && be->be_nsuffix ) {
865                         ber_dupbv( &sub_ndn, be->be_nsuffix );
866
867                 } else {
868                         fprintf( stderr,
869                                 "<scope> needs a DN or a valid database\n" );
870                         exit( EXIT_FAILURE );
871                 }
872         }
873
874 startup:;
875         if ( be ) {
876                 BackendDB *bdtmp;
877
878                 dbnum = 0;
879                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
880                         if ( bdtmp == be ) break;
881                         dbnum++;
882                 }
883         }
884
885 #ifdef CSRIMALLOC
886         mal_leaktrace(1);
887 #endif
888
889         if ( conffile != NULL ) {
890                 ch_free( conffile );
891                 conffile = NULL;
892         }
893
894         if ( confdir != NULL ) {
895                 ch_free( confdir );
896                 confdir = NULL;
897         }
898
899         if ( ldiffile != NULL ) {
900                 ch_free( ldiffile );
901                 ldiffile = NULL;
902         }
903
904         /* slapdn doesn't specify a backend to startup */
905         if ( !dryrun && tool != SLAPDN ) {
906                 need_shutdown = 1;
907
908                 if ( slap_startup( be ) ) {
909                         switch ( tool ) {
910                         case SLAPTEST:
911                                 fprintf( stderr, "slap_startup failed "
912                                                 "(test would succeed using "
913                                                 "the -u switch)\n" );
914                                 break;
915
916                         default:
917                                 fprintf( stderr, "slap_startup failed\n" );
918                                 break;
919                         }
920
921                         exit( EXIT_FAILURE );
922                 }
923         }
924 }
925
926 int slap_tool_destroy( void )
927 {
928         int rc = 0;
929         if ( !dryrun ) {
930                 if ( need_shutdown ) {
931                         if ( slap_shutdown( be ))
932                                 rc = EXIT_FAILURE;
933                 }
934                 if ( slap_destroy())
935                         rc = EXIT_FAILURE;
936         }
937 #ifdef SLAPD_MODULES
938         if ( slapMode == SLAP_SERVER_MODE ) {
939         /* always false. just pulls in necessary symbol references. */
940                 lutil_uuidstr(NULL, 0);
941         }
942         module_kill();
943 #endif
944         schema_destroy();
945 #ifdef HAVE_TLS
946         ldap_pvt_tls_destroy();
947 #endif
948         config_destroy();
949
950 #ifdef CSRIMALLOC
951         mal_dumpleaktrace( leakfile );
952 #endif
953
954         if ( !BER_BVISNULL( &authcDN ) ) {
955                 ch_free( authcDN.bv_val );
956                 BER_BVZERO( &authcDN );
957         }
958
959         if ( ldiffp && ldiffp != &dummy ) {
960                 ldif_close( ldiffp );
961         }
962         return rc;
963 }
964
965 int
966 slap_tool_update_ctxcsn(
967         const char *progname,
968         unsigned long sid,
969         struct berval *bvtext )
970 {
971         struct berval ctxdn;
972         ID ctxcsn_id;
973         Entry *ctxcsn_e;
974         int rc = EXIT_SUCCESS;
975
976         if ( !(update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1) ) {
977                 return rc;
978         }
979
980         if ( SLAP_SYNC_SUBENTRY( be )) {
981                 build_new_dn( &ctxdn, &be->be_nsuffix[0],
982                         (struct berval *)&slap_ldapsync_cn_bv, NULL );
983         } else {
984                 ctxdn = be->be_nsuffix[0];
985         }
986         ctxcsn_id = be->be_dn2id_get( be, &ctxdn );
987         if ( ctxcsn_id == NOID ) {
988                 if ( SLAP_SYNC_SUBENTRY( be )) {
989                         ctxcsn_e = slap_create_context_csn_entry( be, NULL );
990                         for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
991                                 if ( maxcsn[ sid ].bv_len ) {
992                                         attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN,
993                                                 &maxcsn[ sid ], NULL );
994                                 }
995                         }
996                         ctxcsn_id = be->be_entry_put( be, ctxcsn_e, bvtext );
997                         if ( ctxcsn_id == NOID ) {
998                                 fprintf( stderr, "%s: couldn't create context entry\n", progname );
999                                 rc = EXIT_FAILURE;
1000                         }
1001                         entry_free( ctxcsn_e );
1002                 } else {
1003                         fprintf( stderr, "%s: context entry is missing\n", progname );
1004                         rc = EXIT_FAILURE;
1005                 }
1006         } else {
1007                 ctxcsn_e = be->be_entry_get( be, ctxcsn_id );
1008                 if ( ctxcsn_e != NULL ) {
1009                         Operation op = { 0 };
1010                         Entry *e = entry_dup( ctxcsn_e );
1011                         Attribute *attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
1012
1013                         int change;
1014                         op.o_bd = be;
1015                         be_entry_release_r( &op, ctxcsn_e );
1016
1017                         if ( attr ) {
1018                                 int             i;
1019
1020                                 change = 0;
1021
1022                                 for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) {
1023                                         int rc_sid;
1024                                         int match;
1025                                         const char *text = NULL;
1026
1027                                         rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] );
1028                                         if ( rc_sid < 0 ) {
1029                                                 Debug( LDAP_DEBUG_ANY,
1030                                                         "%s: unable to extract SID "
1031                                                         "from #%d contextCSN=%s\n",
1032                                                         progname, i,
1033                                                         attr->a_nvals[ i ].bv_val );
1034                                                 continue;
1035                                         }
1036
1037                                         assert( rc_sid <= SLAP_SYNC_SID_MAX );
1038
1039                                         sid = (unsigned)rc_sid;
1040
1041                                         if ( maxcsn[ sid ].bv_len == 0 ) {
1042                                                 match = -1;
1043
1044                                         } else {
1045                                                 value_match( &match, slap_schema.si_ad_entryCSN,
1046                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
1047                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1048                                                         &maxcsn[ sid ], &attr->a_nvals[i], &text );
1049                                         }
1050
1051                                         if ( match > 0 ) {
1052                                                 change = 1;
1053                                         } else {
1054                                                 AC_MEMCPY( maxcsn[ sid ].bv_val,
1055                                                         attr->a_nvals[ i ].bv_val,
1056                                                         attr->a_nvals[ i ].bv_len );
1057                                                 maxcsn[ sid ].bv_val[ attr->a_nvals[ i ].bv_len ] = '\0';
1058                                                 maxcsn[ sid ].bv_len = attr->a_nvals[ i ].bv_len;
1059                                         }
1060                                 }
1061
1062                                 if ( change ) {
1063                                         if ( attr->a_nvals != attr->a_vals ) {
1064                                                 ber_bvarray_free( attr->a_nvals );
1065                                         }
1066                                         attr->a_nvals = NULL;
1067                                         ber_bvarray_free( attr->a_vals );
1068                                         attr->a_vals = NULL;
1069                                         attr->a_numvals = 0;
1070                                 }
1071                         } else {
1072                                 change = 1;
1073                         }
1074
1075                         if ( change ) {
1076                                 for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
1077                                         if ( maxcsn[ sid ].bv_len ) {
1078                                                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
1079                                                         &maxcsn[ sid], NULL );
1080                                         }
1081                                 }
1082
1083                                 ctxcsn_id = be->be_entry_modify( be, e, bvtext );
1084                                 if( ctxcsn_id == NOID ) {
1085                                         fprintf( stderr, "%s: could not modify ctxcsn (%s)\n",
1086                                                 progname, bvtext->bv_val ? bvtext->bv_val : "" );
1087                                         rc = EXIT_FAILURE;
1088                                 } else if ( verbose ) {
1089                                         fprintf( stderr, "modified: \"%s\" (%08lx)\n",
1090                                                 e->e_dn, (long) ctxcsn_id );
1091                                 }
1092                         }
1093                         entry_free( e );
1094                 }
1095         } 
1096
1097         return rc;
1098 }
1099
1100 /*
1101  * return value:
1102  *      -1:                     update_ctxcsn == 0
1103  *      SLAP_SYNC_SID_MAX + 1:  unable to extract SID
1104  *      0 <= SLAP_SYNC_SID_MAX: the SID
1105  */
1106 unsigned long
1107 slap_tool_update_ctxcsn_check(
1108         const char *progname,
1109         Entry *e )
1110 {
1111         if ( update_ctxcsn ) {
1112                 unsigned long sid = SLAP_SYNC_SID_MAX + 1;
1113                 int rc_sid;
1114                 Attribute *attr;
1115
1116                 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
1117                 assert( attr != NULL );
1118
1119                 rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] );
1120                 if ( rc_sid < 0 ) {
1121                         Debug( LDAP_DEBUG_ANY, "%s: could not "
1122                                 "extract SID from entryCSN=%s, entry dn=\"%s\"\n",
1123                                 progname, attr->a_nvals[ 0 ].bv_val, e->e_name.bv_val );
1124                         return (unsigned long)(-1);
1125
1126                 } else {
1127                         int match;
1128                         const char *text = NULL;
1129
1130                         assert( rc_sid <= SLAP_SYNC_SID_MAX );
1131
1132                         sid = (unsigned)rc_sid;
1133                         if ( maxcsn[ sid ].bv_len != 0 ) {
1134                                 match = 0;
1135                                 value_match( &match, slap_schema.si_ad_entryCSN,
1136                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
1137                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1138                                         &maxcsn[ sid ], &attr->a_nvals[0], &text );
1139                         } else {
1140                                 match = -1;
1141                         }
1142                         if ( match < 0 ) {
1143                                 strcpy( maxcsn[ sid ].bv_val, attr->a_nvals[0].bv_val );
1144                                 maxcsn[ sid ].bv_len = attr->a_nvals[0].bv_len;
1145                         }
1146                 }
1147         }
1148
1149         return (unsigned long)(-1);
1150 }
1151
1152 int
1153 slap_tool_update_ctxcsn_init(void)
1154 {
1155         if ( update_ctxcsn ) {
1156                 unsigned long sid;
1157                 maxcsn[ 0 ].bv_val = maxcsnbuf;
1158                 for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) {
1159                         maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_PVT_CSNSTR_BUFSIZE;
1160                         maxcsn[ sid ].bv_len = 0;
1161                 }
1162         }
1163
1164         return 0;
1165 }
1166
1167 int
1168 slap_tool_entry_check(
1169         const char *progname,
1170         Operation *op,
1171         Entry *e,
1172         int lineno,
1173         const char **text,
1174         char *textbuf,
1175         size_t textlen )
1176 {
1177         /* NOTE: we may want to conditionally enable manage */
1178         int manage = 0;
1179
1180         Attribute *oc = attr_find( e->e_attrs,
1181                 slap_schema.si_ad_objectClass );
1182
1183         if( oc == NULL ) {
1184                 fprintf( stderr, "%s: dn=\"%s\" (line=%d): %s\n",
1185                         progname, e->e_dn, lineno,
1186                         "no objectClass attribute");
1187                 return LDAP_NO_SUCH_ATTRIBUTE;
1188         }
1189
1190         /* check schema */
1191         op->o_bd = be;
1192
1193         if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) {
1194                 int rc = entry_schema_check( op, e, NULL, manage, 1, NULL,
1195                         text, textbuf, textlen );
1196
1197                 if( rc != LDAP_SUCCESS ) {
1198                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
1199                                 progname, e->e_dn, lineno, rc, *text );
1200                         return rc;
1201                 }
1202                 textbuf[ 0 ] = '\0';
1203         }
1204
1205         if ( (slapMode & SLAP_TOOL_VALUE_CHECK) != 0) {
1206                 Modifications *ml = NULL;
1207
1208                 int rc = slap_entry2mods( e, &ml, text, textbuf, textlen );
1209                 if ( rc != LDAP_SUCCESS ) {
1210                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
1211                                 progname, e->e_dn, lineno, rc, *text );
1212                         return rc;
1213                 }
1214                 textbuf[ 0 ] = '\0';
1215
1216                 rc = slap_mods_check( op, ml, text, textbuf, textlen, NULL );
1217                 slap_mods_free( ml, 1 );
1218                 if ( rc != LDAP_SUCCESS ) {
1219                         fprintf( stderr, "%s: dn=\"%s\" (line=%d): (%d) %s\n",
1220                                 progname, e->e_dn, lineno, rc, *text );
1221                         return rc;
1222                 }
1223                 textbuf[ 0 ] = '\0';
1224         }
1225
1226         return LDAP_SUCCESS;
1227 }
1228