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