]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
596cbc3182f60a34ed567f90a4511dd8ffb94c64
[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_init( mode, progname );
404
405         if ( rc != 0 ) {
406                 fprintf( stderr, "%s: slap_init failed!\n", progname );
407                 exit( EXIT_FAILURE );
408         }
409
410         if ( frontend_init() ) {
411                 fprintf( stderr, "%s: frontend_init failed!\n", progname );
412                 exit( EXIT_FAILURE );
413         }
414
415         if ( overlay_init() ) {
416                 fprintf( stderr, "%s: overlay_init failed!\n", progname );
417                 exit( EXIT_FAILURE );
418         }
419
420         rc = read_config( conffile, confdir );
421
422         if ( rc != 0 ) {
423                 fprintf( stderr, "%s: bad configuration file!\n", progname );
424                 exit( EXIT_FAILURE );
425         }
426
427         at_oc_cache = 1;
428
429         switch ( tool ) {
430         case SLAPADD:
431         case SLAPCAT:
432         case SLAPINDEX:
433                 if ( !nbackends ) {
434                         fprintf( stderr, "No databases found "
435                                         "in config file\n" );
436                         exit( EXIT_FAILURE );
437                 }
438                 break;
439
440         default:
441                 break;
442         }
443
444         rc = slap_schema_check();
445
446         if ( rc != 0 ) {
447                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
448                 exit( EXIT_FAILURE );
449         }
450
451         switch ( tool ) {
452         case SLAPDN:
453         case SLAPTEST:
454         case SLAPAUTH:
455                 be = NULL;
456                 goto startup;
457
458         default:
459                 break;
460         }
461
462         if( filterstr ) {
463                 filter = str2filter( filterstr );
464
465                 if( filter == NULL ) {
466                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
467                         exit( EXIT_FAILURE );
468                 }
469         }
470
471         if( subtree ) {
472                 struct berval val;
473                 ber_str2bv( subtree, 0, 0, &val );
474                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
475                 if( rc != LDAP_SUCCESS ) {
476                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
477                         exit( EXIT_FAILURE );
478                 }
479
480                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
481                         base = val;
482                 } else {
483                         free( subtree );
484                 }
485         }
486
487         if( base.bv_val != NULL ) {
488                 struct berval nbase;
489
490                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
491                 if( rc != LDAP_SUCCESS ) {
492                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
493                                 progname, base.bv_val );
494                         exit( EXIT_FAILURE );
495                 }
496
497                 be = select_backend( &nbase, 0, 0 );
498                 ber_memfree( nbase.bv_val );
499
500                 switch ( tool ) {
501                 case SLAPACL:
502                         goto startup;
503
504                 default:
505                         break;
506                 }
507
508                 if( be == NULL ) {
509                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
510                                 progname, base.bv_val );
511                         exit( EXIT_FAILURE );
512                 }
513                 /* If the named base is a glue master, operate on the
514                  * entire context
515                  */
516                 if ( SLAP_GLUE_INSTANCE( be ) ) {
517                         nosubordinates = 1;
518                 }
519
520         } else if ( dbnum == -1 ) {
521                 if ( nbackends <= 0 ) {
522                         fprintf( stderr, "No available databases\n" );
523                         exit( EXIT_FAILURE );
524                 }
525                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
526                         dbnum++;
527                         if ( dbnum < 1 ) continue;
528                 
529                         if ( SLAP_MONITOR(be))
530                                 continue;
531
532                 /* If just doing the first by default and it is a
533                  * glue subordinate, find the master.
534                  */
535                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
536                                 nosubordinates = 1;
537                                 continue;
538                         }
539                         break;
540                 }
541
542                 if ( !be ) {
543                         fprintf( stderr, "Available database(s) "
544                                         "do not allow %s\n", progname );
545                         exit( EXIT_FAILURE );
546                 }
547                 
548                 if ( nosubordinates == 0 && dbnum > 0 ) {
549                         Debug( LDAP_DEBUG_ANY,
550                                 "The first database does not allow %s;"
551                                 " using the first available one (%d)\n",
552                                 progname, dbnum + 1, 0 );
553                 }
554
555         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
556                 fprintf( stderr,
557                         "Database number selected via -n is out of range\n"
558                         "Must be in the range 1 to %d"
559                         " (number of databases in the config file)\n",
560                         nbackends );
561                 exit( EXIT_FAILURE );
562
563         } else {
564                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
565                         if ( dbnum == 0 ) break;
566                         dbnum--;
567                 }
568         }
569
570 startup:;
571
572 #ifdef CSRIMALLOC
573         mal_leaktrace(1);
574 #endif
575
576         /* slapdn doesn't specify a backend to startup */
577         if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
578                 switch ( tool ) {
579                 case SLAPTEST:
580                         fprintf( stderr, "slap_startup failed "
581                                         "(test would succeed using "
582                                         "the -u switch)\n" );
583                         break;
584
585                 default:
586                         fprintf( stderr, "slap_startup failed\n" );
587                         break;
588                 }
589                 
590                 exit( EXIT_FAILURE );
591         }
592 }
593
594 void slap_tool_destroy( void )
595 {
596         if ( !dryrun ) {
597                 slap_shutdown( be );
598                 slap_destroy();
599         }
600 #ifdef SLAPD_MODULES
601         if ( slapMode == SLAP_SERVER_MODE ) {
602         /* always false. just pulls in necessary symbol references. */
603                 lutil_uuidstr(NULL, 0);
604         }
605         module_kill();
606 #endif
607         schema_destroy();
608 #ifdef HAVE_TLS
609         ldap_pvt_tls_destroy();
610 #endif
611         config_destroy();
612
613 #ifdef CSRIMALLOC
614         mal_dumpleaktrace( leakfile );
615 #endif
616
617         if ( !BER_BVISNULL( &authcDN ) ) {
618                 ch_free( authcDN.bv_val );
619         }
620 }