]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
a1d8c0d9d819d92a9324a15dd299b9a0287eabc6
[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-2006 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 #include "ldif.h"
40
41 tool_vars tool_globals;
42
43 #ifdef CSRIMALLOC
44 static char *leakfilename;
45 static FILE *leakfile;
46 #endif
47
48 static LDIFFP dummy;
49
50 static void
51 usage( int tool, const char *progname )
52 {
53         char *options = NULL;
54         fprintf( stderr,
55                 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir]",
56                 progname );
57
58         switch( tool ) {
59         case SLAPACL:
60                 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
61                         "\n\t-b DN -o <var>[=<val>] [-u] [attr[/access][:value]] [...]\n";
62                 break;
63
64         case SLAPADD:
65                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
66                         "\t[-l ldiffile] [-q] [-u] [-s] [-w]\n";
67                 break;
68
69         case SLAPAUTH:
70                 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
71                 break;
72
73         case SLAPCAT:
74                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
75                         " [-l ldiffile] [-a filter]\n";
76                 break;
77
78         case SLAPDN:
79                 options = "\n\t[-N | -P] DN [...]\n";
80                 break;
81
82         case SLAPINDEX:
83                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [-q]\n";
84                 break;
85
86         case SLAPTEST:
87                 options = " [-u]\n";
88                 break;
89         }
90
91         if ( options != NULL ) {
92                 fputs( options, stderr );
93         }
94         exit( EXIT_FAILURE );
95 }
96
97 static int
98 parse_slapacl( void )
99 {
100         size_t  len;
101         char    *p;
102
103         p = strchr( optarg, '=' );
104         if ( p == NULL ) {
105                 return -1;
106         }
107
108         len = p - optarg;
109         p++;
110
111         if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
112                 if ( !BER_BVISNULL( &listener_url ) ) {
113                         ber_memfree( listener_url.bv_val );
114                 }
115                 ber_str2bv( p, 0, 1, &listener_url );
116
117         } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
118                 if ( !BER_BVISNULL( &peer_domain ) ) {
119                         ber_memfree( peer_domain.bv_val );
120                 }
121                 ber_str2bv( p, 0, 1, &peer_domain );
122
123         } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
124                 if ( !BER_BVISNULL( &peer_name ) ) {
125                         ber_memfree( peer_name.bv_val );
126                 }
127                 ber_str2bv( p, 0, 1, &peer_name );
128
129         } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
130                 if ( !BER_BVISNULL( &sock_name ) ) {
131                         ber_memfree( sock_name.bv_val );
132                 }
133                 ber_str2bv( p, 0, 1, &sock_name );
134
135         } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
136                 if ( lutil_atou( &ssf, p ) ) {
137                         Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
138                         return -1;
139                 }
140
141         } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
142                 if ( lutil_atou( &transport_ssf, p ) ) {
143                         Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
144                         return -1;
145                 }
146
147         } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
148                 if ( lutil_atou( &tls_ssf, p ) ) {
149                         Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
150                         return -1;
151                 }
152
153         } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
154                 if ( lutil_atou( &sasl_ssf, p ) ) {
155                         Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
156                         return -1;
157                 }
158
159         } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
160                 ber_str2bv( p, 0, 1, &authzDN );
161
162         } else {
163                 return -1;
164         }
165
166         return 0;
167 }
168
169 /*
170  * slap_tool_init - initialize slap utility, handle program options.
171  * arguments:
172  *      name            program name
173  *      tool            tool code
174  *      argc, argv      command line arguments
175  */
176
177 static int need_shutdown;
178
179 void
180 slap_tool_init(
181         const char* progname,
182         int tool,
183         int argc, char **argv )
184 {
185         char *options;
186         char *conffile = NULL;
187         char *confdir = NULL;
188         struct berval base = BER_BVNULL;
189         char *filterstr = NULL;
190         char *subtree = NULL;
191         char *ldiffile  = NULL;
192         int rc, i, dbnum;
193         int mode = SLAP_TOOL_MODE;
194         int truncatemode = 0;
195         int use_glue = 1;
196
197 #ifdef LDAP_DEBUG
198         /* tools default to "none", so that at least LDAP_DEBUG_ANY 
199          * messages show up; use -d 0 to reset */
200         slap_debug = LDAP_DEBUG_NONE;
201 #endif
202
203 #ifdef CSRIMALLOC
204         leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
205         sprintf( leakfilename, "%s.leak", progname );
206         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
207                 leakfile = stderr;
208         }
209         free( leakfilename );
210 #endif
211
212         switch( tool ) {
213         case SLAPADD:
214                 options = "b:cd:f:F:gl:n:qstuvw";
215                 break;
216
217         case SLAPCAT:
218                 options = "a:b:cd:f:F:gl:n:s:v";
219                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
220                 break;
221
222         case SLAPDN:
223                 options = "d:f:F:NPv";
224                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
225                 break;
226
227         case SLAPTEST:
228                 options = "d:f:F:uv";
229                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
230                 break;
231
232         case SLAPAUTH:
233                 options = "d:f:F:M:R:U:vX:";
234                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
235                 break;
236
237         case SLAPINDEX:
238                 options = "b:cd:f:F:gn:qv";
239                 mode |= SLAP_TOOL_READMAIN;
240                 break;
241
242         case SLAPACL:
243                 options = "b:D:d:f:F:o:uU:vX:";
244                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
245                 break;
246
247         default:
248                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
249                 exit( EXIT_FAILURE );
250         }
251
252         dbnum = -1;
253         while ( (i = getopt( argc, argv, options )) != EOF ) {
254                 switch ( i ) {
255                 case 'a':
256                         filterstr = strdup( optarg );
257                         break;
258
259                 case 'b':
260                         ber_str2bv( optarg, 0, 1, &base );
261                         break;
262
263                 case 'c':       /* enable continue mode */
264                         continuemode++;
265                         break;
266
267                 case 'd': {     /* turn on debugging */
268                         int     level = 0;
269
270                         if ( parse_debug_level( optarg, &level ) ) {
271                                 usage( tool, progname );
272                         }
273 #ifdef LDAP_DEBUG
274                         if ( level == 0 ) {
275                                 /* allow to reset log level */
276                                 slap_debug = 0;
277
278                         } else {
279                                 slap_debug |= level;
280                         }
281 #else
282                         if ( level != 0 )
283                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
284                                        stderr );
285 #endif
286                         } break;
287
288                 case 'D':
289                         ber_str2bv( optarg, 0, 1, &authcDN );
290                         break;
291
292                 case 'f':       /* specify a conf file */
293                         conffile = strdup( optarg );
294                         break;
295
296                 case 'F':       /* specify a conf dir */
297                         confdir = strdup( optarg );
298                         break;
299
300                 case 'g':       /* disable subordinate glue */
301                         use_glue = 0;
302                         break;
303
304                 case 'l':       /* LDIF file */
305                         ldiffile = strdup( optarg );
306                         break;
307
308                 case 'M':
309                         ber_str2bv( optarg, 0, 0, &mech );
310                         break;
311
312                 case 'N':
313                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
314                                 usage( tool, progname );
315                         }
316                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
317                         break;
318
319                 case 'n':       /* which config file db to index */
320                         if ( lutil_atoi( &dbnum, optarg ) ) {
321                                 usage( tool, progname );
322                         }
323                         break;
324
325                 case 'o':
326                         if ( parse_slapacl() ) {
327                                 usage( tool, progname );
328                         }
329                         break;
330
331                 case 'P':
332                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
333                                 usage( tool, progname );
334                         }
335                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
336                         break;
337
338                 case 'q':       /* turn on quick */
339                         mode |= SLAP_TOOL_QUICK;
340                         break;
341
342                 case 'R':
343                         realm = optarg;
344                         break;
345
346                 case 's':       /* dump subtree */
347                         if ( tool == SLAPADD )
348                                 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
349                         else if ( tool == SLAPCAT )
350                                 subtree = strdup( optarg );
351                         break;
352
353                 case 't':       /* turn on truncate */
354                         truncatemode++;
355                         mode |= SLAP_TRUNCATE_MODE;
356                         break;
357
358                 case 'U':
359                         ber_str2bv( optarg, 0, 0, &authcID );
360                         break;
361
362                 case 'u':       /* dry run */
363                         dryrun++;
364                         break;
365
366                 case 'v':       /* turn on verbose */
367                         verbose++;
368                         break;
369
370                 case 'w':       /* write context csn at the end */
371                         update_ctxcsn++;
372                         break;
373
374                 case 'X':
375                         ber_str2bv( optarg, 0, 0, &authzID );
376                         break;
377
378                 default:
379                         usage( tool, progname );
380                         break;
381                 }
382         }
383
384         switch ( tool ) {
385         case SLAPADD:
386         case SLAPCAT:
387         case SLAPINDEX:
388                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
389                         usage( tool, progname );
390                 }
391
392                 break;
393
394         case SLAPDN:
395                 if ( argc == optind ) {
396                         usage( tool, progname );
397                 }
398                 break;
399
400         case SLAPAUTH:
401                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
402                         usage( tool, progname );
403                 }
404                 break;
405
406         case SLAPTEST:
407                 if ( argc != optind ) {
408                         usage( tool, progname );
409                 }
410                 break;
411
412         case SLAPACL:
413                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
414                         usage( tool, progname );
415                 }
416                 if ( BER_BVISNULL( &base ) ) {
417                         usage( tool, progname );
418                 }
419                 ber_dupbv( &baseDN, &base );
420                 break;
421
422         default:
423                 break;
424         }
425
426         ldap_syslog = 0;
427
428         if ( ldiffile == NULL ) {
429                 dummy.fp = tool == SLAPCAT ? stdout : stdin;
430                 ldiffp = &dummy;
431
432         } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
433                 == NULL )
434         {
435                 perror( ldiffile );
436                 exit( EXIT_FAILURE );
437         }
438
439         /*
440          * initialize stuff and figure out which backend we're dealing with
441          */
442
443         rc = slap_init( mode, progname );
444         if ( rc != 0 ) {
445                 fprintf( stderr, "%s: slap_init failed!\n", progname );
446                 exit( EXIT_FAILURE );
447         }
448
449         rc = read_config( conffile, confdir );
450
451         if ( rc != 0 ) {
452                 fprintf( stderr, "%s: bad configuration %s!\n",
453                         progname, confdir ? "directory" : "file" );
454                 exit( EXIT_FAILURE );
455         }
456
457         at_oc_cache = 1;
458
459         switch ( tool ) {
460         case SLAPADD:
461         case SLAPCAT:
462         case SLAPINDEX:
463                 if ( !nbackends ) {
464                         fprintf( stderr, "No databases found "
465                                         "in config file\n" );
466                         exit( EXIT_FAILURE );
467                 }
468                 break;
469
470         default:
471                 break;
472         }
473
474         if ( use_glue ) {
475                 rc = glue_sub_attach();
476
477                 if ( rc != 0 ) {
478                         fprintf( stderr,
479                                 "%s: subordinate configuration error\n", progname );
480                         exit( EXIT_FAILURE );
481                 }
482         }
483
484         rc = slap_schema_check();
485
486         if ( rc != 0 ) {
487                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
488                 exit( EXIT_FAILURE );
489         }
490
491         switch ( tool ) {
492         case SLAPDN:
493         case SLAPTEST:
494         case SLAPAUTH:
495                 be = NULL;
496                 goto startup;
497
498         default:
499                 break;
500         }
501
502         if( filterstr ) {
503                 filter = str2filter( filterstr );
504
505                 if( filter == NULL ) {
506                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
507                         exit( EXIT_FAILURE );
508                 }
509         }
510
511         if( subtree ) {
512                 struct berval val;
513                 ber_str2bv( subtree, 0, 0, &val );
514                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
515                 if( rc != LDAP_SUCCESS ) {
516                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
517                         exit( EXIT_FAILURE );
518                 }
519
520                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
521                         base = val;
522                 } else {
523                         free( subtree );
524                 }
525         }
526
527         if( base.bv_val != NULL ) {
528                 struct berval nbase;
529
530                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
531                 if( rc != LDAP_SUCCESS ) {
532                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
533                                 progname, base.bv_val );
534                         exit( EXIT_FAILURE );
535                 }
536
537                 be = select_backend( &nbase, 0, 0 );
538                 ber_memfree( nbase.bv_val );
539
540                 switch ( tool ) {
541                 case SLAPACL:
542                         goto startup;
543
544                 default:
545                         break;
546                 }
547
548                 if( be == NULL ) {
549                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
550                                 progname, base.bv_val );
551                         exit( EXIT_FAILURE );
552                 }
553                 /* If the named base is a glue master, operate on the
554                  * entire context
555                  */
556                 if ( SLAP_GLUE_INSTANCE( be ) ) {
557                         nosubordinates = 1;
558                 }
559
560         } else if ( dbnum == -1 ) {
561                 /* no suffix and no dbnum specified, just default to
562                  * the first available database
563                  */
564                 if ( nbackends <= 0 ) {
565                         fprintf( stderr, "No available databases\n" );
566                         exit( EXIT_FAILURE );
567                 }
568                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
569                         dbnum++;
570
571                         /* db #0 is cn=config, don't select it as a default */
572                         if ( dbnum < 1 ) continue;
573                 
574                         if ( SLAP_MONITOR(be))
575                                 continue;
576
577                 /* If just doing the first by default and it is a
578                  * glue subordinate, find the master.
579                  */
580                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
581                                 nosubordinates = 1;
582                                 continue;
583                         }
584                         break;
585                 }
586
587                 if ( !be ) {
588                         fprintf( stderr, "Available database(s) "
589                                         "do not allow %s\n", progname );
590                         exit( EXIT_FAILURE );
591                 }
592                 
593                 if ( nosubordinates == 0 && dbnum > 1 ) {
594                         Debug( LDAP_DEBUG_ANY,
595                                 "The first database does not allow %s;"
596                                 " using the first available one (%d)\n",
597                                 progname, dbnum, 0 );
598                 }
599
600         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
601                 fprintf( stderr,
602                         "Database number selected via -n is out of range\n"
603                         "Must be in the range 0 to %d"
604                         " (number of configured databases)\n",
605                         nbackends-1 );
606                 exit( EXIT_FAILURE );
607
608         } else {
609                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
610                         if ( dbnum == 0 ) break;
611                         dbnum--;
612                 }
613         }
614
615 startup:;
616
617 #ifdef CSRIMALLOC
618         mal_leaktrace(1);
619 #endif
620
621         if ( conffile != NULL ) {
622                 ch_free( conffile );
623         }
624
625         if ( ldiffile != NULL ) {
626                 ch_free( ldiffile );
627         }
628
629         /* slapdn doesn't specify a backend to startup */
630         if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
631                 need_shutdown = 1;
632
633                 switch ( tool ) {
634                 case SLAPTEST:
635                         fprintf( stderr, "slap_startup failed "
636                                         "(test would succeed using "
637                                         "the -u switch)\n" );
638                         break;
639
640                 default:
641                         fprintf( stderr, "slap_startup failed\n" );
642                         break;
643                 }
644                 
645                 exit( EXIT_FAILURE );
646         }
647 }
648
649 void slap_tool_destroy( void )
650 {
651         if ( !dryrun ) {
652                 if ( need_shutdown ) {
653                         slap_shutdown( be );
654                 }
655                 slap_destroy();
656         }
657 #ifdef SLAPD_MODULES
658         if ( slapMode == SLAP_SERVER_MODE ) {
659         /* always false. just pulls in necessary symbol references. */
660                 lutil_uuidstr(NULL, 0);
661         }
662         module_kill();
663 #endif
664         schema_destroy();
665 #ifdef HAVE_TLS
666         ldap_pvt_tls_destroy();
667 #endif
668         config_destroy();
669
670 #ifdef CSRIMALLOC
671         mal_dumpleaktrace( leakfile );
672 #endif
673
674         if ( !BER_BVISNULL( &authcDN ) ) {
675                 ch_free( authcDN.bv_val );
676         }
677 }