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