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