]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
Added ppolicy_hide_lockout keyword
[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-2004 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
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,
52                 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n",
53                 progname );
54
55         switch( tool ) {
56         case SLAPADD:
57                 options = "\t[-n databasenumber | -b suffix]\n"
58                         "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
59                 break;
60
61         case SLAPCAT:
62                 options = "\t[-n databasenumber | -b suffix] [-l ldiffile] [-m] [-k]\n";
63                 break;
64
65         case SLAPDN:
66                 options = "\tDN [...]\n";
67                 break;
68
69         case SLAPINDEX:
70                 options = "\t[-n databasenumber | -b suffix]\n";
71                 break;
72         }
73
74         if ( options != NULL ) {
75                 fputs( options, stderr );
76         }
77         exit( EXIT_FAILURE );
78 }
79
80
81 /*
82  * slap_tool_init - initialize slap utility, handle program options.
83  * arguments:
84  *      name            program name
85  *      tool            tool code
86  *      argc, argv      command line arguments
87  */
88
89 void
90 slap_tool_init(
91         const char* progname,
92         int tool,
93         int argc, char **argv )
94 {
95         char *options;
96         char *conffile = SLAPD_DEFAULT_CONFIGFILE;
97         struct berval base = { 0, NULL };
98         char *subtree = NULL;
99         char *ldiffile  = NULL;
100         int rc, i, dbnum;
101         int mode = SLAP_TOOL_MODE;
102         int truncatemode = 0;
103
104 #ifdef CSRIMALLOC
105         leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
106         sprintf( leakfilename, "%s.leak", progname );
107         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
108                 leakfile = stderr;
109         }
110         free( leakfilename );
111 #endif
112
113         switch( tool ) {
114         case SLAPADD:
115                 options = "b:cd:f:i:l:n:prtuvWw";
116                 break;
117
118         case SLAPCAT:
119                 options = "b:cd:f:kl:mn:s:v";
120                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
121                 break;
122
123         case SLAPDN:
124         case SLAPTEST:
125                 options = "d:f:v";
126                 break;
127
128         case SLAPINDEX:
129                 options = "b:cd:f:n:v";
130                 mode |= SLAP_TOOL_READMAIN;
131                 break;
132
133         default:
134                 fprintf( stderr, "%s: unknown tool mode (%d)\n",
135                          progname, tool );
136                 exit( EXIT_FAILURE );
137         }
138
139         dbnum = -1;
140         while ( (i = getopt( argc, argv, options )) != EOF ) {
141                 switch ( i ) {
142                 case 'b':
143                         base.bv_val = strdup( optarg );
144                         base.bv_len = strlen( base.bv_val );
145                         break;
146
147                 case 'c':       /* enable continue mode */
148                         continuemode++;
149                         break;
150
151                 case 'd':       /* turn on debugging */
152                         ldap_debug += atoi( optarg );
153                         break;
154
155                 case 'f':       /* specify a conf file */
156                         conffile = strdup( optarg );
157                         break;
158
159                 case 'i': /* specify syncrepl id list */
160                         replica_id_string = strdup( optarg );
161                         if ( !isdigit( (unsigned char) *replica_id_string )) {
162                                 usage( tool, progname );
163                                 exit( EXIT_FAILURE );
164                         }
165                         str2clist( &replica_id_strlist, replica_id_string, "," );
166                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
167                         replica_id_list = ch_calloc( i + 1, sizeof( int ) );
168                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
169                                 replica_id_list[i] = atoi( replica_id_strlist[i] );
170                                 if ( replica_id_list[i] >= 1000 ) {
171                                         fprintf(stderr,
172                                                 "%s: syncrepl id %d is out of range [0..999]\n",
173                                                 progname, replica_id_list[i] );
174                                         exit( EXIT_FAILURE );
175                                 }
176                         }
177                         replica_id_list[i] = -1;
178                         break;
179
180                 case 'k':       /* Retrieve sync cookie entry */
181                         retrieve_synccookie = 1;
182                         break;
183
184                 case 'l':       /* LDIF file */
185                         ldiffile = strdup( optarg );
186                         break;
187
188                 case 'm':       /* Retrieve ldapsync entry */
189                         retrieve_ctxcsn = 1;
190                         break;
191
192                 case 'n':       /* which config file db to index */
193                         dbnum = atoi( optarg ) - 1;
194                         break;
195
196                 case 'p':       /* replica promotion */
197                         replica_promotion = 1;          
198                         break;
199
200                 case 'r':       /* replica demotion */
201                         replica_demotion = 1;           
202                         break;
203
204                 case 's':       /* dump subtree */
205                         subtree = strdup( optarg );
206                         break;
207
208                 case 't':       /* turn on truncate */
209                         truncatemode++;
210                         mode |= SLAP_TRUNCATE_MODE;
211                         break;
212
213                 case 'u':       /* dry run */
214                         dryrun++;
215                         break;
216
217                 case 'v':       /* turn on verbose */
218                         verbose++;
219                         break;
220
221                 case 'W':       /* write context csn on every entry add */
222                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
223                         /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
224                         break;
225
226                 case 'w':       /* write context csn on at the end */
227                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
228                         break;
229
230                 default:
231                         usage( tool, progname );
232                         break;
233                 }
234         }
235
236         switch ( tool ) {
237         case SLAPADD:
238         case SLAPCAT:
239         case SLAPINDEX:
240                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
241                         usage( tool, progname );
242                 }
243
244                 if ( replica_promotion && replica_demotion ) {
245                         usage( tool, progname );
246
247                 } else if ( !replica_promotion && !replica_demotion ) {
248                         if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
249                                 usage( tool, progname );
250                         }
251                 }
252                 break;
253
254         case SLAPDN:
255                 if ( argc == optind ) {
256                         usage( tool, progname );
257                 }
258                 break;
259
260         case SLAPTEST:
261                 if ( argc != optind ) {
262                         usage( tool, progname );
263                 }
264                 break;
265
266         default:
267                 break;
268         }
269
270         if ( ldiffile == NULL ) {
271                 ldiffp = tool == SLAPCAT ? stdout : stdin;
272
273         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
274                 == NULL )
275         {
276                 perror( ldiffile );
277                 exit( EXIT_FAILURE );
278         }
279
280         /*
281          * initialize stuff and figure out which backend we're dealing with
282          */
283
284 #ifdef SLAPD_MODULES
285         if ( module_init() != 0 ) {
286                 fprintf( stderr, "%s: module_init failed!\n", progname );
287                 exit( EXIT_FAILURE );
288         }
289 #endif
290                 
291         rc = slap_init( mode, progname );
292
293         if ( rc != 0 ) {
294                 fprintf( stderr, "%s: slap_init failed!\n", progname );
295                 exit( EXIT_FAILURE );
296         }
297
298         rc = slap_schema_init();
299
300         if ( rc != 0 ) {
301                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
302                 exit( EXIT_FAILURE );
303         }
304
305         rc = read_config( conffile, 0 );
306
307         if ( rc != 0 ) {
308                 fprintf( stderr, "%s: bad configuration file!\n", progname );
309                 exit( EXIT_FAILURE );
310         }
311
312         ldap_syslog = 0;
313
314         switch ( tool ) {
315         case SLAPADD:
316         case SLAPCAT:
317         case SLAPINDEX:
318                 if ( !nbackends ) {
319                         fprintf( stderr, "No databases found "
320                                         "in config file\n" );
321                         exit( EXIT_FAILURE );
322                 }
323                 break;
324
325         default:
326                 break;
327         }
328
329         rc = glue_sub_init();
330
331         if ( rc != 0 ) {
332                 fprintf( stderr, "Subordinate configuration error\n" );
333                 exit( EXIT_FAILURE );
334         }
335
336         rc = slap_schema_check();
337
338         if ( rc != 0 ) {
339                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
340                 exit( EXIT_FAILURE );
341         }
342
343         switch ( tool ) {
344         case SLAPDN:
345         case SLAPTEST:
346                 return;
347
348         default:
349                 break;
350         }
351
352         if( subtree ) {
353                 struct berval val;
354                 val.bv_val = subtree;
355                 val.bv_len = strlen( subtree );
356                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
357                 if( rc != LDAP_SUCCESS ) {
358                         fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
359                         exit( EXIT_FAILURE );
360                 }
361
362                 if( base.bv_val == NULL && dbnum == -1 )
363                         base = val;
364                 else
365                         free( subtree );
366         }
367
368         if( base.bv_val != NULL ) {
369                 struct berval nbase;
370
371                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
372                 if( rc != LDAP_SUCCESS ) {
373                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
374                                 progname, base.bv_val );
375                         exit( EXIT_FAILURE );
376                 }
377
378                 be = select_backend( &nbase, 0, 0 );
379                 ber_memfree( nbase.bv_val );
380
381                 if( be == NULL ) {
382                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
383                                 progname, base.bv_val );
384                         exit( EXIT_FAILURE );
385                 }
386                 /* If the named base is a glue master, operate on the
387                  * entire context
388                  */
389                 if (SLAP_GLUE_INSTANCE(be)) {
390                         nosubordinates = 1;
391                 }
392
393         } else if ( dbnum == -1 ) {
394                 if ( nbackends <= 0 ) {
395                         fprintf( stderr, "No available databases\n" );
396                         exit( EXIT_FAILURE );
397                 }
398                 
399                 be = &backends[dbnum=0];
400                 /* If just doing the first by default and it is a
401                  * glue subordinate, find the master.
402                  */
403                 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
404                         if (SLAP_GLUE_SUBORDINATE(be)) {
405                                 nosubordinates = 1;
406                         }
407                         be++;
408                         dbnum++;
409                 }
410
411
412                 if ( dbnum >= nbackends ) {
413                         fprintf( stderr, "Available database(s) "
414                                         "do not allow %s\n", progname );
415                         exit( EXIT_FAILURE );
416                 }
417                 
418                 if ( nosubordinates == 0 && dbnum > 0 ) {
419 #ifdef NEW_LOGGING
420                         LDAP_LOG( BACKEND, ERR, 
421 "The first database does not allow %s; using the first available one (%d)\n",
422                                 progname, dbnum + 1, 0 );
423 #else
424                         Debug( LDAP_DEBUG_ANY,
425 "The first database does not allow %s; using the first available one (%d)\n",
426                                 progname, dbnum + 1, 0 );
427 #endif
428                 }
429
430         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
431                 fprintf( stderr,
432                         "Database number selected via -n is out of range\n"
433                         "Must be in the range 1 to %d"
434                                 " (number of databases in the config file)\n",
435                         nbackends );
436                 exit( EXIT_FAILURE );
437
438         } else {
439                 be = &backends[dbnum];
440         }
441
442 #ifdef CSRIMALLOC
443         mal_leaktrace(1);
444 #endif
445
446         if ( slap_startup( be ) ) {
447                 fprintf( stderr, "slap_startup failed\n" );
448                 exit( EXIT_FAILURE );
449         }
450 }
451
452 void slap_tool_destroy( void )
453 {
454         slap_shutdown( be );
455         slap_destroy();
456 #ifdef SLAPD_MODULES
457         if ( slapMode == SLAP_SERVER_MODE ) {
458         /* always false. just pulls in necessary symbol references. */
459                 lutil_uuidstr(NULL, 0);
460         }
461         module_kill();
462 #endif
463         schema_destroy();
464 #ifdef HAVE_TLS
465         ldap_pvt_tls_destroy();
466 #endif
467         config_destroy();
468
469 #ifdef CSRIMALLOC
470         mal_dumpleaktrace( leakfile );
471 #endif
472 }