]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
fix database operations in tool mode (ITS#3622)
[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 = " 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:v";
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':       /* which config file db to index */
266                         dbnum = atoi( optarg );
267                         break;
268
269                 case 'o':
270                         if ( parse_slapacl() ) {
271                                 usage( tool, progname );
272                         }
273                         break;
274
275                 case 'q':       /* turn on quick */
276                         mode |= SLAP_TOOL_QUICK;
277                         break;
278
279                 case 'R':
280                         realm = optarg;
281                         break;
282
283                 case 's':       /* dump subtree */
284                         subtree = strdup( optarg );
285                         break;
286
287                 case 't':       /* turn on truncate */
288                         truncatemode++;
289                         mode |= SLAP_TRUNCATE_MODE;
290                         break;
291
292                 case 'U':
293                         ber_str2bv( optarg, 0, 0, &authcID );
294                         break;
295
296                 case 'u':       /* dry run */
297                         dryrun++;
298                         break;
299
300                 case 'v':       /* turn on verbose */
301                         verbose++;
302                         break;
303
304                 case 'w':       /* write context csn at the end */
305                         update_ctxcsn++;
306                         break;
307
308                 case 'X':
309                         ber_str2bv( optarg, 0, 0, &authzID );
310                         break;
311
312                 default:
313                         usage( tool, progname );
314                         break;
315                 }
316         }
317
318         switch ( tool ) {
319         case SLAPADD:
320         case SLAPCAT:
321         case SLAPINDEX:
322                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
323                         usage( tool, progname );
324                 }
325
326                 break;
327
328         case SLAPDN:
329                 if ( argc == optind ) {
330                         usage( tool, progname );
331                 }
332                 break;
333
334         case SLAPAUTH:
335                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
336                         usage( tool, progname );
337                 }
338                 break;
339
340         case SLAPTEST:
341                 if ( argc != optind ) {
342                         usage( tool, progname );
343                 }
344                 break;
345
346         case SLAPACL:
347                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
348                         usage( tool, progname );
349                 }
350                 if ( BER_BVISNULL( &base ) ) {
351                         usage( tool, progname );
352                 }
353                 ber_dupbv( &baseDN, &base );
354                 break;
355
356         default:
357                 break;
358         }
359
360         ldap_syslog = 0;
361
362         if ( ldiffile == NULL ) {
363                 ldiffp = tool == SLAPCAT ? stdout : stdin;
364
365         } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
366                 == NULL )
367         {
368                 perror( ldiffile );
369                 exit( EXIT_FAILURE );
370         }
371
372         /*
373          * initialize stuff and figure out which backend we're dealing with
374          */
375
376 #ifdef SLAPD_MODULES
377         if ( module_init() != 0 ) {
378                 fprintf( stderr, "%s: module_init failed!\n", progname );
379                 exit( EXIT_FAILURE );
380         }
381 #endif
382                 
383         rc = slap_schema_init();
384
385         if ( rc != 0 ) {
386                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
387                 exit( EXIT_FAILURE );
388         }
389
390         rc = slap_init( mode, progname );
391
392         if ( rc != 0 ) {
393                 fprintf( stderr, "%s: slap_init failed!\n", progname );
394                 exit( EXIT_FAILURE );
395         }
396
397         if ( frontend_init() ) {
398                 fprintf( stderr, "%s: frontend_init failed!\n", progname );
399                 exit( EXIT_FAILURE );
400         }
401
402         if ( overlay_init() ) {
403                 fprintf( stderr, "%s: overlay_init failed!\n", progname );
404                 exit( EXIT_FAILURE );
405         }
406
407         rc = read_config( conffile, confdir );
408
409         if ( rc != 0 ) {
410                 fprintf( stderr, "%s: bad configuration file!\n", progname );
411                 exit( EXIT_FAILURE );
412         }
413
414         at_oc_cache = 1;
415
416         switch ( tool ) {
417         case SLAPADD:
418         case SLAPCAT:
419         case SLAPINDEX:
420                 if ( !nbackends ) {
421                         fprintf( stderr, "No databases found "
422                                         "in config file\n" );
423                         exit( EXIT_FAILURE );
424                 }
425                 break;
426
427         default:
428                 break;
429         }
430
431         rc = slap_schema_check();
432
433         if ( rc != 0 ) {
434                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
435                 exit( EXIT_FAILURE );
436         }
437
438         switch ( tool ) {
439         case SLAPDN:
440         case SLAPTEST:
441         case SLAPAUTH:
442                 be = NULL;
443                 goto startup;
444
445         default:
446                 break;
447         }
448
449         if( filterstr ) {
450                 filter = str2filter( filterstr );
451
452                 if( filter == NULL ) {
453                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
454                         exit( EXIT_FAILURE );
455                 }
456         }
457
458         if( subtree ) {
459                 struct berval val;
460                 ber_str2bv( subtree, 0, 0, &val );
461                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
462                 if( rc != LDAP_SUCCESS ) {
463                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
464                         exit( EXIT_FAILURE );
465                 }
466
467                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
468                         base = val;
469                 } else {
470                         free( subtree );
471                 }
472         }
473
474         if( base.bv_val != NULL ) {
475                 struct berval nbase;
476
477                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
478                 if( rc != LDAP_SUCCESS ) {
479                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
480                                 progname, base.bv_val );
481                         exit( EXIT_FAILURE );
482                 }
483
484                 be = select_backend( &nbase, 0, 0 );
485                 ber_memfree( nbase.bv_val );
486
487                 switch ( tool ) {
488                 case SLAPACL:
489                         goto startup;
490
491                 default:
492                         break;
493                 }
494
495                 if( be == NULL ) {
496                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
497                                 progname, base.bv_val );
498                         exit( EXIT_FAILURE );
499                 }
500                 /* If the named base is a glue master, operate on the
501                  * entire context
502                  */
503                 if (SLAP_GLUE_INSTANCE(be)) {
504                         nosubordinates = 1;
505                 }
506
507         } else if ( dbnum == -1 ) {
508                 if ( nbackends <= 0 ) {
509                         fprintf( stderr, "No available databases\n" );
510                         exit( EXIT_FAILURE );
511                 }
512                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
513                         dbnum++;
514                         if ( dbnum < 1 ) continue;
515                 
516                         if ( SLAP_MONITOR(be))
517                                 continue;
518
519                 /* If just doing the first by default and it is a
520                  * glue subordinate, find the master.
521                  */
522                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
523                                 nosubordinates = 1;
524                                 continue;
525                         }
526                         break;
527                 }
528
529                 if ( !be ) {
530                         fprintf( stderr, "Available database(s) "
531                                         "do not allow %s\n", progname );
532                         exit( EXIT_FAILURE );
533                 }
534                 
535                 if ( nosubordinates == 0 && dbnum > 0 ) {
536                         Debug( LDAP_DEBUG_ANY,
537                                 "The first database does not allow %s;"
538                                 " using the first available one (%d)\n",
539                                 progname, dbnum + 1, 0 );
540                 }
541
542         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
543                 fprintf( stderr,
544                         "Database number selected via -n is out of range\n"
545                         "Must be in the range 1 to %d"
546                         " (number of databases in the config file)\n",
547                         nbackends );
548                 exit( EXIT_FAILURE );
549
550         } else {
551                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
552                         if ( dbnum == 0 ) break;
553                         dbnum--;
554                 }
555         }
556
557 startup:;
558
559 #ifdef CSRIMALLOC
560         mal_leaktrace(1);
561 #endif
562
563         if ( !dryrun && slap_startup( be ) ) {
564
565                 switch ( tool ) {
566                 case SLAPTEST:
567                         fprintf( stderr, "slap_startup failed "
568                                         "(test would succeed using "
569                                         "the -u switch)\n" );
570                         break;
571
572                 default:
573                         fprintf( stderr, "slap_startup failed\n" );
574                         break;
575                 }
576                 
577                 exit( EXIT_FAILURE );
578         }
579 }
580
581 void slap_tool_destroy( void )
582 {
583         if ( !dryrun ) {
584                 slap_shutdown( be );
585                 slap_destroy();
586         }
587 #ifdef SLAPD_MODULES
588         if ( slapMode == SLAP_SERVER_MODE ) {
589         /* always false. just pulls in necessary symbol references. */
590                 lutil_uuidstr(NULL, 0);
591         }
592         module_kill();
593 #endif
594         schema_destroy();
595 #ifdef HAVE_TLS
596         ldap_pvt_tls_destroy();
597 #endif
598         config_destroy();
599
600 #ifdef CSRIMALLOC
601         mal_dumpleaktrace( leakfile );
602 #endif
603
604         if ( !BER_BVISNULL( &authcDN ) ) {
605                 ch_free( authcDN.bv_val );
606         }
607 }