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