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