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