]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
Sync with HEAD
[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:tuvw";
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 'R':
208                         realm = optarg;
209                         break;
210
211                 case 's':       /* dump subtree */
212                         subtree = strdup( optarg );
213                         break;
214
215                 case 't':       /* turn on truncate */
216                         truncatemode++;
217                         mode |= SLAP_TRUNCATE_MODE;
218                         break;
219
220                 case 'U':
221                         ber_str2bv( optarg, 0, 0, &authcID );
222                         break;
223
224                 case 'u':       /* dry run */
225                         dryrun++;
226                         break;
227
228                 case 'v':       /* turn on verbose */
229                         verbose++;
230                         break;
231
232                 case 'w':       /* write context csn at the end */
233                         update_ctxcsn++;
234                         break;
235
236                 case 'X':
237                         ber_str2bv( optarg, 0, 0, &authzID );
238                         break;
239
240                 default:
241                         usage( tool, progname );
242                         break;
243                 }
244         }
245
246         switch ( tool ) {
247         case SLAPADD:
248         case SLAPCAT:
249         case SLAPINDEX:
250                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
251                         usage( tool, progname );
252                 }
253
254                 break;
255
256         case SLAPDN:
257                 if ( argc == optind ) {
258                         usage( tool, progname );
259                 }
260                 break;
261
262         case SLAPAUTH:
263                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
264                         usage( tool, progname );
265                 }
266                 break;
267
268         case SLAPTEST:
269                 if ( argc != optind ) {
270                         usage( tool, progname );
271                 }
272                 break;
273
274         case SLAPACL:
275                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
276                         usage( tool, progname );
277                 }
278                 if ( BER_BVISNULL( &base ) ) {
279                         usage( tool, progname );
280                 }
281                 ber_dupbv( &baseDN, &base );
282                 break;
283
284         default:
285                 break;
286         }
287
288         if ( ldiffile == NULL ) {
289                 ldiffp = tool == SLAPCAT ? stdout : stdin;
290
291         } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
292                 == NULL )
293         {
294                 perror( ldiffile );
295                 exit( EXIT_FAILURE );
296         }
297
298         /*
299          * initialize stuff and figure out which backend we're dealing with
300          */
301
302 #ifdef SLAPD_MODULES
303         if ( module_init() != 0 ) {
304                 fprintf( stderr, "%s: module_init failed!\n", progname );
305                 exit( EXIT_FAILURE );
306         }
307 #endif
308                 
309         rc = slap_init( mode, progname );
310
311         if ( rc != 0 ) {
312                 fprintf( stderr, "%s: slap_init failed!\n", progname );
313                 exit( EXIT_FAILURE );
314         }
315
316         rc = slap_schema_init();
317
318         if ( rc != 0 ) {
319                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
320                 exit( EXIT_FAILURE );
321         }
322
323         if ( frontend_init() ) {
324                 fprintf( stderr, "%s: frontend_init failed!\n", progname );
325                 exit( EXIT_FAILURE );
326         }
327
328         if ( overlay_init() ) {
329                 fprintf( stderr, "%s: overlay_init failed!\n", progname );
330                 exit( EXIT_FAILURE );
331         }
332
333         rc = read_config( conffile, 0 );
334
335         if ( rc != 0 ) {
336                 fprintf( stderr, "%s: bad configuration file!\n", progname );
337                 exit( EXIT_FAILURE );
338         }
339
340         ldap_syslog = 0;
341
342         switch ( tool ) {
343         case SLAPADD:
344         case SLAPCAT:
345         case SLAPINDEX:
346                 if ( !nbackends ) {
347                         fprintf( stderr, "No databases found "
348                                         "in config file\n" );
349                         exit( EXIT_FAILURE );
350                 }
351                 break;
352
353         default:
354                 break;
355         }
356
357         rc = slap_schema_check();
358
359         if ( rc != 0 ) {
360                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
361                 exit( EXIT_FAILURE );
362         }
363
364         switch ( tool ) {
365         case SLAPDN:
366         case SLAPTEST:
367         case SLAPAUTH:
368                 be = NULL;
369                 goto startup;
370
371         default:
372                 break;
373         }
374
375         if( filterstr ) {
376                 filter = str2filter( filterstr );
377
378                 if( filter == NULL ) {
379                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
380                         exit( EXIT_FAILURE );
381                 }
382         }
383
384         if( subtree ) {
385                 struct berval val;
386                 ber_str2bv( subtree, 0, 0, &val );
387                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
388                 if( rc != LDAP_SUCCESS ) {
389                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
390                         exit( EXIT_FAILURE );
391                 }
392
393                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
394                         base = val;
395                 } else {
396                         free( subtree );
397                 }
398         }
399
400         if( base.bv_val != NULL ) {
401                 struct berval nbase;
402
403                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
404                 if( rc != LDAP_SUCCESS ) {
405                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
406                                 progname, base.bv_val );
407                         exit( EXIT_FAILURE );
408                 }
409
410                 be = select_backend( &nbase, 0, 0 );
411                 ber_memfree( nbase.bv_val );
412
413                 switch ( tool ) {
414                 case SLAPACL:
415                         goto startup;
416
417                 default:
418                         break;
419                 }
420
421                 if( be == NULL ) {
422                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
423                                 progname, base.bv_val );
424                         exit( EXIT_FAILURE );
425                 }
426                 /* If the named base is a glue master, operate on the
427                  * entire context
428                  */
429                 if (SLAP_GLUE_INSTANCE(be)) {
430                         nosubordinates = 1;
431                 }
432
433         } else if ( dbnum == -1 ) {
434                 if ( nbackends <= 0 ) {
435                         fprintf( stderr, "No available databases\n" );
436                         exit( EXIT_FAILURE );
437                 }
438                 
439                 be = &backends[dbnum=0];
440                 /* If just doing the first by default and it is a
441                  * glue subordinate, find the master.
442                  */
443                 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
444                         if (SLAP_GLUE_SUBORDINATE(be)) {
445                                 nosubordinates = 1;
446                         }
447                         be++;
448                         dbnum++;
449                 }
450
451
452                 if ( dbnum >= nbackends ) {
453                         fprintf( stderr, "Available database(s) "
454                                         "do not allow %s\n", progname );
455                         exit( EXIT_FAILURE );
456                 }
457                 
458                 if ( nosubordinates == 0 && dbnum > 0 ) {
459                         Debug( LDAP_DEBUG_ANY,
460                                 "The first database does not allow %s;"
461                                 " using the first available one (%d)\n",
462                                 progname, dbnum + 1, 0 );
463                 }
464
465         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
466                 fprintf( stderr,
467                         "Database number selected via -n is out of range\n"
468                         "Must be in the range 1 to %d"
469                         " (number of databases in the config file)\n",
470                         nbackends );
471                 exit( EXIT_FAILURE );
472
473         } else {
474                 be = &backends[dbnum];
475         }
476
477 startup:;
478
479 #ifdef CSRIMALLOC
480         mal_leaktrace(1);
481 #endif
482
483         if ( !dryrun && slap_startup( be ) ) {
484
485                 switch ( tool ) {
486                 case SLAPTEST:
487                         fprintf( stderr, "slap_startup failed "
488                                         "(test would succeed using "
489                                         "the -u switch)\n" );
490                         break;
491
492                 default:
493                         fprintf( stderr, "slap_startup failed\n" );
494                         break;
495                 }
496                 
497                 exit( EXIT_FAILURE );
498         }
499 }
500
501 void slap_tool_destroy( void )
502 {
503         if ( !dryrun && be != NULL ) {
504                 slap_shutdown( be );
505         }
506         slap_destroy();
507 #ifdef SLAPD_MODULES
508         if ( slapMode == SLAP_SERVER_MODE ) {
509         /* always false. just pulls in necessary symbol references. */
510                 lutil_uuidstr(NULL, 0);
511         }
512         module_kill();
513 #endif
514         schema_destroy();
515 #ifdef HAVE_TLS
516         ldap_pvt_tls_destroy();
517 #endif
518         config_destroy();
519
520 #ifdef CSRIMALLOC
521         mal_dumpleaktrace( leakfile );
522 #endif
523
524         if ( !BER_BVISNULL( &authcDN ) ) {
525                 ch_free( authcDN.bv_val );
526         }
527 }