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