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