]>
Commit | Line | Data |
---|---|---|
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 | * | |
dc0eacd4 | 5 | * Copyright 1998-2005 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 | |
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 | ||
40 | tool_vars tool_globals; | |
41 | ||
42 | #ifdef CSRIMALLOC | |
43 | static char *leakfilename; | |
44 | static FILE *leakfile; | |
45 | #endif | |
46 | ||
47 | static void | |
48 | usage( int tool, const char *progname ) | |
49 | { | |
50 | char *options = NULL; | |
51 | fprintf( stderr, | |
764aa5d9 PM |
52 | "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n", |
53 | progname ); | |
57ba4cac HC |
54 | |
55 | switch( tool ) { | |
56 | case SLAPADD: | |
764aa5d9 PM |
57 | options = "\t[-n databasenumber | -b suffix]\n" |
58 | "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n"; | |
57ba4cac HC |
59 | break; |
60 | ||
61 | case SLAPCAT: | |
cdd94c7a KZ |
62 | options = "\t[-n databasenumber | -b suffix]" |
63 | " [-l ldiffile] [-a filter] [-m] [-k]\n"; | |
764aa5d9 PM |
64 | break; |
65 | ||
66 | case SLAPDN: | |
67 | options = "\tDN [...]\n"; | |
57ba4cac HC |
68 | break; |
69 | ||
70 | case SLAPINDEX: | |
764aa5d9 | 71 | options = "\t[-n databasenumber | -b suffix]\n"; |
57ba4cac | 72 | break; |
3ea43689 | 73 | |
a54900be | 74 | case SLAPAUTH: |
cc78fb52 | 75 | options = "\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n"; |
3ea43689 | 76 | break; |
7b65d46b PM |
77 | |
78 | case SLAPACL: | |
cdd94c7a | 79 | options = "\t[-U authcID | -D authcDN]" |
437bd421 | 80 | " -b DN [attr[/access][:value]] [...]\n"; |
7b65d46b | 81 | break; |
57ba4cac HC |
82 | } |
83 | ||
764aa5d9 | 84 | if ( options != NULL ) { |
57ba4cac HC |
85 | fputs( options, stderr ); |
86 | } | |
87 | exit( EXIT_FAILURE ); | |
88 | } | |
89 | ||
90 | ||
91 | /* | |
92 | * slap_tool_init - initialize slap utility, handle program options. | |
93 | * arguments: | |
94 | * name program name | |
95 | * tool tool code | |
96 | * argc, argv command line arguments | |
97 | */ | |
98 | ||
99 | void | |
100 | slap_tool_init( | |
101 | const char* progname, | |
102 | int tool, | |
103 | int argc, char **argv ) | |
104 | { | |
105 | char *options; | |
106 | char *conffile = SLAPD_DEFAULT_CONFIGFILE; | |
44725e73 | 107 | struct berval base = BER_BVNULL; |
cdd94c7a | 108 | char *filterstr = NULL; |
57ba4cac HC |
109 | char *subtree = NULL; |
110 | char *ldiffile = NULL; | |
111 | int rc, i, dbnum; | |
112 | int mode = SLAP_TOOL_MODE; | |
113 | int truncatemode = 0; | |
114 | ||
115 | #ifdef CSRIMALLOC | |
3ea43689 | 116 | leakfilename = malloc( strlen( progname ) + STRLEOF( ".leak" ) - 1 ); |
57ba4cac HC |
117 | sprintf( leakfilename, "%s.leak", progname ); |
118 | if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) { | |
119 | leakfile = stderr; | |
120 | } | |
121 | free( leakfilename ); | |
122 | #endif | |
123 | ||
124 | switch( tool ) { | |
125 | case SLAPADD: | |
126 | options = "b:cd:f:i:l:n:prtuvWw"; | |
127 | break; | |
128 | ||
57ba4cac | 129 | case SLAPCAT: |
cdd94c7a | 130 | options = "a:b:cd:f:kl:mn:s:v"; |
57ba4cac HC |
131 | mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY; |
132 | break; | |
133 | ||
764aa5d9 PM |
134 | case SLAPDN: |
135 | case SLAPTEST: | |
136 | options = "d:f:v"; | |
854863f0 | 137 | mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY; |
764aa5d9 PM |
138 | break; |
139 | ||
a54900be | 140 | case SLAPAUTH: |
cc78fb52 | 141 | options = "d:f:M:R:U:vX:"; |
854863f0 | 142 | mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY; |
3ea43689 PM |
143 | break; |
144 | ||
764aa5d9 PM |
145 | case SLAPINDEX: |
146 | options = "b:cd:f:n:v"; | |
147 | mode |= SLAP_TOOL_READMAIN; | |
148 | break; | |
149 | ||
7b65d46b PM |
150 | case SLAPACL: |
151 | options = "b:D:d:f:U:v"; | |
854863f0 | 152 | mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY; |
7b65d46b PM |
153 | break; |
154 | ||
57ba4cac | 155 | default: |
cdd94c7a | 156 | fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool ); |
57ba4cac HC |
157 | exit( EXIT_FAILURE ); |
158 | } | |
159 | ||
160 | dbnum = -1; | |
161 | while ( (i = getopt( argc, argv, options )) != EOF ) { | |
162 | switch ( i ) { | |
cdd94c7a KZ |
163 | case 'a': |
164 | filterstr = strdup( optarg ); | |
165 | break; | |
166 | ||
57ba4cac | 167 | case 'b': |
3ea43689 | 168 | ber_str2bv( optarg, 0, 1, &base ); |
57ba4cac HC |
169 | break; |
170 | ||
171 | case 'c': /* enable continue mode */ | |
172 | continuemode++; | |
173 | break; | |
174 | ||
175 | case 'd': /* turn on debugging */ | |
176 | ldap_debug += atoi( optarg ); | |
177 | break; | |
178 | ||
7b65d46b | 179 | case 'D': |
854863f0 | 180 | ber_str2bv( optarg, 0, 1, &authcDN ); |
7b65d46b PM |
181 | break; |
182 | ||
57ba4cac HC |
183 | case 'f': /* specify a conf file */ |
184 | conffile = strdup( optarg ); | |
185 | break; | |
186 | ||
187 | case 'i': /* specify syncrepl id list */ | |
188 | replica_id_string = strdup( optarg ); | |
189 | if ( !isdigit( (unsigned char) *replica_id_string )) { | |
190 | usage( tool, progname ); | |
191 | exit( EXIT_FAILURE ); | |
192 | } | |
ffbb5d4e | 193 | slap_str2clist( &replica_id_strlist, replica_id_string, "," ); |
57ba4cac HC |
194 | for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ; |
195 | replica_id_list = ch_calloc( i + 1, sizeof( int ) ); | |
196 | for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) { | |
197 | replica_id_list[i] = atoi( replica_id_strlist[i] ); | |
198 | if ( replica_id_list[i] >= 1000 ) { | |
199 | fprintf(stderr, | |
200 | "%s: syncrepl id %d is out of range [0..999]\n", | |
201 | progname, replica_id_list[i] ); | |
202 | exit( EXIT_FAILURE ); | |
203 | } | |
204 | } | |
205 | replica_id_list[i] = -1; | |
206 | break; | |
207 | ||
208 | case 'k': /* Retrieve sync cookie entry */ | |
209 | retrieve_synccookie = 1; | |
210 | break; | |
211 | ||
212 | case 'l': /* LDIF file */ | |
213 | ldiffile = strdup( optarg ); | |
214 | break; | |
215 | ||
216 | case 'm': /* Retrieve ldapsync entry */ | |
217 | retrieve_ctxcsn = 1; | |
218 | break; | |
219 | ||
cc78fb52 PM |
220 | case 'M': |
221 | ber_str2bv( optarg, 0, 0, &mech ); | |
222 | break; | |
223 | ||
57ba4cac HC |
224 | case 'n': /* which config file db to index */ |
225 | dbnum = atoi( optarg ) - 1; | |
226 | break; | |
227 | ||
228 | case 'p': /* replica promotion */ | |
229 | replica_promotion = 1; | |
230 | break; | |
231 | ||
232 | case 'r': /* replica demotion */ | |
233 | replica_demotion = 1; | |
234 | break; | |
235 | ||
cc78fb52 PM |
236 | case 'R': |
237 | realm = optarg; | |
238 | break; | |
239 | ||
57ba4cac HC |
240 | case 's': /* dump subtree */ |
241 | subtree = strdup( optarg ); | |
242 | break; | |
243 | ||
244 | case 't': /* turn on truncate */ | |
245 | truncatemode++; | |
246 | mode |= SLAP_TRUNCATE_MODE; | |
247 | break; | |
248 | ||
3ea43689 PM |
249 | case 'U': |
250 | ber_str2bv( optarg, 0, 0, &authcID ); | |
251 | break; | |
252 | ||
57ba4cac HC |
253 | case 'u': /* dry run */ |
254 | dryrun++; | |
255 | break; | |
256 | ||
257 | case 'v': /* turn on verbose */ | |
258 | verbose++; | |
259 | break; | |
260 | ||
261 | case 'W': /* write context csn on every entry add */ | |
262 | update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH; | |
263 | /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */ | |
264 | break; | |
265 | ||
266 | case 'w': /* write context csn on at the end */ | |
267 | update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH; | |
268 | break; | |
269 | ||
3ea43689 PM |
270 | case 'X': |
271 | ber_str2bv( optarg, 0, 0, &authzID ); | |
272 | break; | |
273 | ||
57ba4cac HC |
274 | default: |
275 | usage( tool, progname ); | |
276 | break; | |
277 | } | |
278 | } | |
279 | ||
764aa5d9 PM |
280 | switch ( tool ) { |
281 | case SLAPADD: | |
282 | case SLAPCAT: | |
283 | case SLAPINDEX: | |
284 | if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) { | |
285 | usage( tool, progname ); | |
286 | } | |
287 | ||
288 | if ( replica_promotion && replica_demotion ) { | |
289 | usage( tool, progname ); | |
290 | ||
291 | } else if ( !replica_promotion && !replica_demotion ) { | |
292 | if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) { | |
293 | usage( tool, progname ); | |
294 | } | |
295 | } | |
296 | break; | |
57ba4cac | 297 | |
764aa5d9 PM |
298 | case SLAPDN: |
299 | if ( argc == optind ) { | |
57ba4cac HC |
300 | usage( tool, progname ); |
301 | } | |
764aa5d9 PM |
302 | break; |
303 | ||
a54900be | 304 | case SLAPAUTH: |
3ea43689 PM |
305 | if ( argc == optind && BER_BVISNULL( &authcID ) ) { |
306 | usage( tool, progname ); | |
307 | } | |
308 | break; | |
309 | ||
764aa5d9 PM |
310 | case SLAPTEST: |
311 | if ( argc != optind ) { | |
312 | usage( tool, progname ); | |
313 | } | |
314 | break; | |
315 | ||
7b65d46b PM |
316 | case SLAPACL: |
317 | if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) { | |
318 | usage( tool, progname ); | |
319 | } | |
320 | if ( BER_BVISNULL( &base ) ) { | |
321 | usage( tool, progname ); | |
322 | } | |
323 | ber_dupbv( &baseDN, &base ); | |
324 | break; | |
325 | ||
764aa5d9 PM |
326 | default: |
327 | break; | |
57ba4cac HC |
328 | } |
329 | ||
330 | if ( ldiffile == NULL ) { | |
331 | ldiffp = tool == SLAPCAT ? stdout : stdin; | |
332 | ||
cdd94c7a | 333 | } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" )) |
57ba4cac HC |
334 | == NULL ) |
335 | { | |
336 | perror( ldiffile ); | |
337 | exit( EXIT_FAILURE ); | |
338 | } | |
339 | ||
340 | /* | |
341 | * initialize stuff and figure out which backend we're dealing with | |
342 | */ | |
343 | ||
344 | #ifdef SLAPD_MODULES | |
345 | if ( module_init() != 0 ) { | |
346 | fprintf( stderr, "%s: module_init failed!\n", progname ); | |
347 | exit( EXIT_FAILURE ); | |
348 | } | |
349 | #endif | |
350 | ||
351 | rc = slap_init( mode, progname ); | |
352 | ||
353 | if ( rc != 0 ) { | |
354 | fprintf( stderr, "%s: slap_init failed!\n", progname ); | |
355 | exit( EXIT_FAILURE ); | |
356 | } | |
357 | ||
358 | rc = slap_schema_init(); | |
359 | ||
360 | if ( rc != 0 ) { | |
361 | fprintf( stderr, "%s: slap_schema_init failed!\n", progname ); | |
362 | exit( EXIT_FAILURE ); | |
363 | } | |
364 | ||
cd9a9c62 PM |
365 | if ( frontend_init() ) { |
366 | fprintf( stderr, "%s: frontend_init failed!\n", progname ); | |
367 | exit( EXIT_FAILURE ); | |
368 | } | |
369 | ||
53e69064 PM |
370 | if ( overlay_init() ) { |
371 | fprintf( stderr, "%s: overlay_init failed!\n", progname ); | |
372 | exit( EXIT_FAILURE ); | |
373 | } | |
374 | ||
57ba4cac HC |
375 | rc = read_config( conffile, 0 ); |
376 | ||
377 | if ( rc != 0 ) { | |
378 | fprintf( stderr, "%s: bad configuration file!\n", progname ); | |
379 | exit( EXIT_FAILURE ); | |
380 | } | |
381 | ||
75725a7a HC |
382 | ldap_syslog = 0; |
383 | ||
764aa5d9 PM |
384 | switch ( tool ) { |
385 | case SLAPADD: | |
386 | case SLAPCAT: | |
387 | case SLAPINDEX: | |
388 | if ( !nbackends ) { | |
389 | fprintf( stderr, "No databases found " | |
390 | "in config file\n" ); | |
391 | exit( EXIT_FAILURE ); | |
392 | } | |
393 | break; | |
394 | ||
395 | default: | |
396 | break; | |
57ba4cac HC |
397 | } |
398 | ||
57ba4cac HC |
399 | rc = slap_schema_check(); |
400 | ||
401 | if ( rc != 0 ) { | |
402 | fprintf( stderr, "%s: slap_schema_prep failed!\n", progname ); | |
403 | exit( EXIT_FAILURE ); | |
404 | } | |
405 | ||
764aa5d9 PM |
406 | switch ( tool ) { |
407 | case SLAPDN: | |
408 | case SLAPTEST: | |
a54900be | 409 | case SLAPAUTH: |
3ea43689 PM |
410 | be = NULL; |
411 | goto startup; | |
412 | ||
764aa5d9 PM |
413 | default: |
414 | break; | |
415 | } | |
416 | ||
cdd94c7a KZ |
417 | if( filterstr ) { |
418 | filter = str2filter( filterstr ); | |
419 | ||
420 | if( filter == NULL ) { | |
421 | fprintf( stderr, "Invalid filter '%s'\n", filterstr ); | |
422 | exit( EXIT_FAILURE ); | |
423 | } | |
424 | } | |
425 | ||
57ba4cac HC |
426 | if( subtree ) { |
427 | struct berval val; | |
3ea43689 | 428 | ber_str2bv( subtree, 0, 0, &val ); |
57ba4cac HC |
429 | rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL ); |
430 | if( rc != LDAP_SUCCESS ) { | |
cdd94c7a | 431 | fprintf( stderr, "Invalid subtree DN '%s'\n", subtree ); |
57ba4cac HC |
432 | exit( EXIT_FAILURE ); |
433 | } | |
434 | ||
cdd94c7a | 435 | if ( BER_BVISNULL( &base ) && dbnum == -1 ) { |
57ba4cac | 436 | base = val; |
cdd94c7a | 437 | } else { |
57ba4cac | 438 | free( subtree ); |
cdd94c7a | 439 | } |
57ba4cac HC |
440 | } |
441 | ||
442 | if( base.bv_val != NULL ) { | |
443 | struct berval nbase; | |
444 | ||
445 | rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL ); | |
446 | if( rc != LDAP_SUCCESS ) { | |
447 | fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n", | |
448 | progname, base.bv_val ); | |
449 | exit( EXIT_FAILURE ); | |
450 | } | |
451 | ||
452 | be = select_backend( &nbase, 0, 0 ); | |
453 | ber_memfree( nbase.bv_val ); | |
454 | ||
854863f0 PM |
455 | switch ( tool ) { |
456 | case SLAPACL: | |
4d3a49b3 | 457 | goto startup; |
854863f0 PM |
458 | |
459 | default: | |
460 | break; | |
4d3a49b3 PM |
461 | } |
462 | ||
57ba4cac HC |
463 | if( be == NULL ) { |
464 | fprintf( stderr, "%s: slap_init no backend for \"%s\"\n", | |
465 | progname, base.bv_val ); | |
466 | exit( EXIT_FAILURE ); | |
467 | } | |
468 | /* If the named base is a glue master, operate on the | |
469 | * entire context | |
470 | */ | |
471 | if (SLAP_GLUE_INSTANCE(be)) { | |
472 | nosubordinates = 1; | |
473 | } | |
474 | ||
475 | } else if ( dbnum == -1 ) { | |
476 | if ( nbackends <= 0 ) { | |
477 | fprintf( stderr, "No available databases\n" ); | |
478 | exit( EXIT_FAILURE ); | |
479 | } | |
480 | ||
481 | be = &backends[dbnum=0]; | |
482 | /* If just doing the first by default and it is a | |
483 | * glue subordinate, find the master. | |
484 | */ | |
485 | while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) { | |
486 | if (SLAP_GLUE_SUBORDINATE(be)) { | |
487 | nosubordinates = 1; | |
488 | } | |
489 | be++; | |
490 | dbnum++; | |
491 | } | |
492 | ||
493 | ||
494 | if ( dbnum >= nbackends ) { | |
495 | fprintf( stderr, "Available database(s) " | |
496 | "do not allow %s\n", progname ); | |
497 | exit( EXIT_FAILURE ); | |
498 | } | |
499 | ||
500 | if ( nosubordinates == 0 && dbnum > 0 ) { | |
57ba4cac | 501 | Debug( LDAP_DEBUG_ANY, |
cdd94c7a KZ |
502 | "The first database does not allow %s;" |
503 | " using the first available one (%d)\n", | |
57ba4cac | 504 | progname, dbnum + 1, 0 ); |
57ba4cac HC |
505 | } |
506 | ||
507 | } else if ( dbnum < 0 || dbnum > (nbackends-1) ) { | |
508 | fprintf( stderr, | |
509 | "Database number selected via -n is out of range\n" | |
510 | "Must be in the range 1 to %d" | |
cdd94c7a | 511 | " (number of databases in the config file)\n", |
57ba4cac HC |
512 | nbackends ); |
513 | exit( EXIT_FAILURE ); | |
514 | ||
515 | } else { | |
516 | be = &backends[dbnum]; | |
517 | } | |
518 | ||
3ea43689 PM |
519 | startup:; |
520 | ||
57ba4cac HC |
521 | #ifdef CSRIMALLOC |
522 | mal_leaktrace(1); | |
523 | #endif | |
524 | ||
47415023 | 525 | if ( !dryrun && slap_startup( be ) ) { |
57ba4cac HC |
526 | fprintf( stderr, "slap_startup failed\n" ); |
527 | exit( EXIT_FAILURE ); | |
528 | } | |
529 | } | |
530 | ||
531 | void slap_tool_destroy( void ) | |
532 | { | |
0fa3715e | 533 | if ( !dryrun && be != NULL ) { |
47415023 PM |
534 | slap_shutdown( be ); |
535 | } | |
57ba4cac HC |
536 | slap_destroy(); |
537 | #ifdef SLAPD_MODULES | |
538 | if ( slapMode == SLAP_SERVER_MODE ) { | |
539 | /* always false. just pulls in necessary symbol references. */ | |
540 | lutil_uuidstr(NULL, 0); | |
541 | } | |
542 | module_kill(); | |
543 | #endif | |
544 | schema_destroy(); | |
545 | #ifdef HAVE_TLS | |
546 | ldap_pvt_tls_destroy(); | |
547 | #endif | |
548 | config_destroy(); | |
549 | ||
550 | #ifdef CSRIMALLOC | |
551 | mal_dumpleaktrace( leakfile ); | |
552 | #endif | |
854863f0 PM |
553 | |
554 | if ( !BER_BVISNULL( &authcDN ) ) { | |
555 | ch_free( authcDN.bv_val ); | |
556 | } | |
57ba4cac | 557 | } |