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