]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
import referral chaing options from back-ldap
[openldap] / servers / slapd / back-meta / config.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "lutil.h"
32 #include "../back-ldap/back-ldap.h"
33 #undef ldap_debug       /* silence a warning in ldap-int.h */
34 #include "../../../libraries/libldap/ldap-int.h"
35 #include "back-meta.h"
36
37 static struct metatarget *
38 new_target( void )
39 {
40         struct metatarget *lt;
41         struct ldapmapping *mapping;
42
43         lt = ch_calloc( sizeof( struct metatarget ), 1 );
44         if ( lt == NULL ) {
45                 return NULL;
46         }
47
48         lt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
49         if ( lt->mt_rwmap.rwm_rw == NULL ) {
50                 free( lt );
51                 return NULL;
52         }
53
54         {
55                 char    *rargv[3];
56
57                 /*
58                  * the filter rewrite as a string must be disabled
59                  * by default; it can be re-enabled by adding rules;
60                  * this creates an empty rewriteContext
61                  */
62                 rargv[ 0 ] = "rewriteContext";
63                 rargv[ 1 ] = "searchFilter";
64                 rargv[ 2 ] = NULL;
65                 rewrite_parse( lt->mt_rwmap.rwm_rw, "<suffix massage>", 
66                                 1, 2, rargv );
67
68                 rargv[ 0 ] = "rewriteContext";
69                 rargv[ 1 ] = "default";
70                 rargv[ 2 ] = NULL;
71                 rewrite_parse( lt->mt_rwmap.rwm_rw, "<suffix massage>", 
72                                 1, 2, rargv );
73         }
74
75         ldap_back_map_init( &lt->mt_rwmap.rwm_at, &mapping );
76
77         return lt;
78 }
79
80 int
81 meta_back_db_config(
82                 BackendDB       *be,
83                 const char      *fname,
84                 int             lineno,
85                 int             argc,
86                 char            **argv
87 )
88 {
89         struct metainfo *li = ( struct metainfo * )be->be_private;
90
91         if ( li == NULL ) {
92                 fprintf( stderr, 
93         "%s: line %d: meta backend info is null!\n",
94                     fname, lineno );
95                 return 1;
96         }
97
98         /* URI of server to query */
99         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
100                 int             i = li->ntargets;
101 #if 0
102                 int             j;
103 #endif /* uncomment if uri MUST be a branch of suffix */
104                 LDAPURLDesc     *ludp, *tmpludp;
105                 struct berval   dn;
106                 int             rc;
107                 
108                 if ( argc != 2 ) {
109                         fprintf( stderr,
110         "%s: line %d: missing address"
111         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
112                                 fname, lineno );
113                         return 1;
114                 }
115                 
116                 ++li->ntargets;
117
118                 li->targets = ch_realloc( li->targets, 
119                         sizeof( struct metatarget *)*li->ntargets );
120                 if ( li->targets == NULL ) {
121                         fprintf( stderr,
122         "%s: line %d: out of memory while storing server name"
123         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
124                                 fname, lineno );
125                         return 1;
126                 }
127
128                 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
129                         fprintf( stderr,
130         "%s: line %d: unable to init server"
131         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
132                                 fname, lineno );
133                         return 1;
134                 }
135
136                 /*
137                  * uri MUST be legal!
138                  */
139                 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
140                         fprintf( stderr,
141         "%s: line %d: unable to parse URI"
142         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
143                                 fname, lineno );
144                         return 1;
145                 }
146
147                 /*
148                  * uri MUST have the <dn> part!
149                  */
150                 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
151                         fprintf( stderr,
152         "%s: line %d: missing <naming context> "
153         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
154                                 fname, lineno );
155                         return 1;
156                 }
157
158                 /*
159                  * copies and stores uri and suffix
160                  */
161                 dn.bv_val = ludp->lud_dn;
162                 dn.bv_len = strlen( ludp->lud_dn );
163
164                 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->mt_psuffix,
165                         &li->targets[ i ]->mt_nsuffix, NULL );
166                 if( rc != LDAP_SUCCESS ) {
167                         fprintf( stderr, "%s: line %d: "
168                                         "target '%s' DN is invalid\n",
169                                         fname, lineno, argv[ 1 ] );
170                         return( 1 );
171                 }
172
173                 ludp->lud_dn[ 0 ] = '\0';
174
175                 /* check all, to apply the scope check on the first one */
176                 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
177                         if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
178                                 fprintf( stderr, "%s: line %d: "
179                                                 "multiple URIs must have "
180                                                 "no DN part\n",
181                                         fname, lineno );
182                                 return( 1 );
183
184                         }
185
186                         if ( tmpludp->lud_scope == LDAP_SCOPE_BASE ) {
187                                 tmpludp->lud_scope = LDAP_SCOPE_DEFAULT;
188                         }
189                 }
190
191                 li->targets[ i ]->mt_uri = ldap_url_list2urls( ludp );
192                 ldap_free_urllist( ludp );
193                 if ( li->targets[ i ]->mt_uri == NULL) {
194                         fprintf( stderr, "%s: line %d: no memory?\n",
195                                         fname, lineno );
196                         return( 1 );
197                 }
198                 
199                 /*
200                  * uri MUST be a branch of suffix!
201                  */
202 #if 0 /* too strict a constraint */
203                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
204                         fprintf( stderr,
205         "%s: line %d: <naming context> of URI does not refer to current backend"
206         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
207                                 fname, lineno );
208                         return 1;
209                 }
210 #else
211                 /*
212                  * uri MUST be a branch of a suffix!
213                  */
214                 if ( select_backend( &li->targets[ i ]->mt_nsuffix, 0, 0 ) == NULL ) {
215                         fprintf( stderr,
216         "%s: line %d: <naming context> of URI does not resolve to a backend"
217         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
218                                 fname, lineno );
219                         return 1;
220                 }
221 #endif
222
223 #if 0
224                 /*
225                  * uri MUST not be used by other URIs!
226                  *
227                  * FIXME: this limitation may be removed,
228                  * or worked out, at least, in some manner
229                  */
230                 for ( j = 0; j < i-1; j++ ) {
231                         if ( dn_match( &li->targets[ i ]->suffix,
232                                         &li->targets[ j ]->suffix ) ) {
233                                 fprintf( stderr,
234         "%s: line %d: naming context \"%s\" already used"
235         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
236                                         fname, lineno, last+1 );
237                                 return 1;
238                         }
239                 }
240 #endif
241
242 #if 0
243                 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
244                         fname, lineno, li->targets[ i ]->uri, 
245                         li->targets[ i ]->psuffix.bv_val );
246 #endif
247                 
248         /* default target directive */
249         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
250                 int             i = li->ntargets-1;
251                 
252                 if ( argc == 1 ) {
253                         if ( i < 0 ) {
254                                 fprintf( stderr,
255         "%s: line %d: \"default-target\" alone need be"
256         " inside a \"uri\" directive\n",
257                                         fname, lineno );
258                                 return 1;
259                         }
260                         li->defaulttarget = i;
261                 } else {
262                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
263                                 if ( i >= 0 ) {
264                                         fprintf( stderr,
265         "%s: line %d: \"default-target none\""
266         " should go before uri definitions\n",
267                                                 fname, lineno );
268                                 }
269                                 li->defaulttarget = META_DEFAULT_TARGET_NONE;
270                         } else {
271                                 int n = atoi( argv[ 1 ] );
272                                 if ( n < 1 || n >= i ) {
273                                         fprintf( stderr,
274         "%s: line %d: illegal target number %d\n",
275                                                 fname, lineno, n );
276                                         return 1;
277                                 }
278                                 li->defaulttarget = n-1;
279                         }
280                 }
281                 
282         /* ttl of dn cache */
283         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
284                 if ( argc != 2 ) {
285                         fprintf( stderr,
286         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
287                                 fname, lineno );
288                         return 1;
289                 }
290                 
291                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
292                         li->cache.ttl = META_DNCACHE_FOREVER;
293                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
294                         li->cache.ttl = META_DNCACHE_DISABLED;
295                 } else {
296                         li->cache.ttl = atol( argv[ 1 ] );
297                 }
298
299         /* network timeout when connecting to ldap servers */
300         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
301                 if ( argc != 2 ) {
302                         fprintf( stderr,
303         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
304                                 fname, lineno );
305                         return 1;
306                 }
307                 li->network_timeout = atol(argv[ 1 ]);
308
309         /* name to use for meta_back_group */
310         } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
311                         || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
312         {
313                 int             i = li->ntargets-1;
314                 struct berval   dn;
315
316                 if ( i < 0 ) {
317                         fprintf( stderr,
318         "%s: line %d: need \"uri\" directive first\n",
319                                 fname, lineno );
320                         return 1;
321                 }
322                 
323                 if ( argc != 2 ) {
324                         fprintf( stderr,
325         "%s: line %d: missing name in \"binddn <name>\" line\n",
326                                 fname, lineno );
327                         return 1;
328                 }
329
330                 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
331                         fprintf( stderr, "%s: line %d: "
332                                 "\"binddn\" statement is deprecated; "
333                                 "use \"acl-authcDN\" instead\n",
334                                 fname, lineno );
335                         /* FIXME: some day we'll need to throw an error */
336                 }
337
338                 dn.bv_val = argv[ 1 ];
339                 dn.bv_len = strlen( argv[ 1 ] );
340                 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->mt_binddn,
341                         NULL ) != LDAP_SUCCESS )
342                 {
343                         fprintf( stderr, "%s: line %d: "
344                                         "bind DN '%s' is invalid\n",
345                                         fname, lineno, argv[ 1 ] );
346                         return( 1 );
347                 }
348
349         /* password to use for meta_back_group */
350         } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
351                         || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
352         {
353                 int             i = li->ntargets-1;
354
355                 if ( i < 0 ) {
356                         fprintf( stderr,
357         "%s: line %d: need \"uri\" directive first\n",
358                                 fname, lineno );
359                         return 1;
360                 }
361                 
362                 if ( argc != 2 ) {
363                         fprintf( stderr,
364         "%s: line %d: missing password in \"bindpw <password>\" line\n",
365                             fname, lineno );
366                         return 1;
367                 }
368
369                 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
370                         fprintf( stderr, "%s: line %d: "
371                                 "\"bindpw\" statement is deprecated; "
372                                 "use \"acl-passwd\" instead\n",
373                                 fname, lineno );
374                         /* FIXME: some day we'll need to throw an error */
375                 }
376
377                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_bindpw );
378                 
379         /* save bind creds for referral rebinds? */
380         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
381                 if (argc != 1) {
382                         fprintf( stderr,
383         "%s: line %d: rebind-as-user takes no arguments\n",
384                             fname, lineno );
385                         return( 1 );
386                 }
387
388                 li->flags |= LDAP_BACK_F_SAVECRED;
389
390         } else if ( strcasecmp( argv[0], "chase-referrals" ) == 0 ) {
391                 if ( argc != 1 ) {
392                         fprintf( stderr,
393         "%s: line %d: \"chase-referrals\" takes no arguments\n",
394                                         fname, lineno );
395                         return( 1 );
396                 }
397
398                 li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
399
400         } else if ( strcasecmp( argv[0], "dont-chase-referrals" ) == 0 ) {
401                 if ( argc != 1 ) {
402                         fprintf( stderr,
403         "%s: line %d: \"dont-chase-referrals\" takes no arguments\n",
404                                         fname, lineno );
405                         return( 1 );
406                 }
407
408                 li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
409
410         /* name to use as pseudo-root dn */
411         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
412                 int             i = li->ntargets-1;
413                 struct berval   dn;
414
415                 if ( i < 0 ) {
416                         fprintf( stderr,
417         "%s: line %d: need \"uri\" directive first\n",
418                                 fname, lineno );
419                         return 1;
420                 }
421                 
422                 if ( argc != 2 ) {
423                         fprintf( stderr,
424         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
425                                 fname, lineno );
426                         return 1;
427                 }
428
429                 dn.bv_val = argv[ 1 ];
430                 dn.bv_len = strlen( argv[ 1 ] );
431                 if ( dnNormalize( 0, NULL, NULL, &dn,
432                         &li->targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
433                 {
434                         fprintf( stderr, "%s: line %d: "
435                                         "pseudoroot DN '%s' is invalid\n",
436                                         fname, lineno, argv[ 1 ] );
437                         return( 1 );
438                 }
439
440         /* password to use as pseudo-root */
441         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
442                 int             i = li->ntargets-1;
443
444                 if ( i < 0 ) {
445                         fprintf( stderr,
446         "%s: line %d: need \"uri\" directive first\n",
447                                 fname, lineno );
448                         return 1;
449                 }
450                 
451                 if ( argc != 2 ) {
452                         fprintf( stderr,
453         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
454                             fname, lineno );
455                         return 1;
456                 }
457                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_pseudorootpw );
458         
459         /* dn massaging */
460         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
461                 BackendDB       *tmp_be;
462                 int             i = li->ntargets-1;
463                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
464
465                 if ( i < 0 ) {
466                         fprintf( stderr,
467         "%s: line %d: need \"uri\" directive first\n",
468                                 fname, lineno );
469                         return 1;
470                 }
471                 
472                 /*
473                  * syntax:
474                  * 
475                  *      suffixmassage <suffix> <massaged suffix>
476                  *
477                  * the <suffix> field must be defined as a valid suffix
478                  * (or suffixAlias?) for the current database;
479                  * the <massaged suffix> shouldn't have already been
480                  * defined as a valid suffix or suffixAlias for the 
481                  * current server
482                  */
483                 if ( argc != 3 ) {
484                         fprintf( stderr,
485         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
486                                 fname, lineno );
487                         return 1;
488                 }
489
490                 dn.bv_val = argv[ 1 ];
491                 dn.bv_len = strlen( argv[ 1 ] );
492                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
493                         fprintf( stderr, "%s: line %d: "
494                                         "suffix '%s' is invalid\n",
495                                         fname, lineno, argv[ 1 ] );
496                         return 1;
497                 }
498                 
499                 tmp_be = select_backend( &nvnc, 0, 0 );
500                 if ( tmp_be != NULL && tmp_be != be ) {
501                         fprintf( stderr, 
502         "%s: line %d: suffix already in use by another backend in"
503         " \"suffixMassage <suffix> <massaged suffix>\"\n",
504                                 fname, lineno );
505                         free( pvnc.bv_val );
506                         free( nvnc.bv_val );
507                         return 1;                                               
508                 }
509
510                 dn.bv_val = argv[ 2 ];
511                 dn.bv_len = strlen( argv[ 2 ] );
512                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
513                         fprintf( stderr, "%s: line %d: "
514                                         "massaged suffix '%s' is invalid\n",
515                                         fname, lineno, argv[ 2 ] );
516                         free( pvnc.bv_val );
517                         free( nvnc.bv_val );
518                         return 1;
519                 }
520         
521 #if 0   
522                 tmp_be = select_backend( &nrnc, 0, 0 );
523                 if ( tmp_be != NULL ) {
524                         fprintf( stderr,
525         "%s: line %d: massaged suffix already in use by another backend in" 
526         " \"suffixMassage <suffix> <massaged suffix>\"\n",
527                                 fname, lineno );
528                         free( pvnc.bv_val );
529                         free( nvnc.bv_val );
530                         free( prnc.bv_val );
531                         free( nrnc.bv_val );
532                         return 1;
533                 }
534 #endif
535                 
536                 /*
537                  * The suffix massaging is emulated by means of the
538                  * rewrite capabilities
539                  * FIXME: no extra rewrite capabilities should be added
540                  * to the database
541                  */
542                 return suffix_massage_config( li->targets[ i ]->mt_rwmap.rwm_rw,
543                                 &pvnc, &nvnc, &prnc, &nrnc );
544                 
545         /* rewrite stuff ... */
546         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
547                 int             i = li->ntargets-1;
548
549                 if ( i < 0 ) {
550                         if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
551                                 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
552                         }
553                         return rewrite_parse( li->rwinfo, fname, lineno,
554                                         argc, argv ); 
555                 }
556                 
557                 return rewrite_parse( li->targets[ i ]->mt_rwmap.rwm_rw,
558                                 fname, lineno, argc, argv );
559
560         /* objectclass/attribute mapping */
561         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
562                 int             i = li->ntargets-1;
563
564                 if ( i < 0 ) {
565                         fprintf( stderr,
566         "%s: line %d: need \"uri\" directive first\n",
567                                 fname, lineno );
568                         return 1;
569                 }
570
571                 return ldap_back_map_config( &li->targets[ i ]->mt_rwmap.rwm_oc, 
572                                 &li->targets[ i ]->mt_rwmap.rwm_at,
573                                 fname, lineno, argc, argv );
574         /* anything else */
575         } else {
576                 return SLAP_CONF_UNKNOWN;
577         }
578         return 0;
579 }
580
581 int
582 ldap_back_map_config(
583                 struct ldapmap  *oc_map,
584                 struct ldapmap  *at_map,
585                 const char      *fname,
586                 int             lineno,
587                 int             argc,
588                 char            **argv )
589 {
590         struct ldapmap          *map;
591         struct ldapmapping      *mapping;
592         char                    *src, *dst;
593         int                     is_oc = 0;
594
595         if ( argc < 3 || argc > 4 ) {
596                 fprintf( stderr,
597         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
598                         fname, lineno );
599                 return 1;
600         }
601
602         if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
603                 map = oc_map;
604                 is_oc = 1;
605
606         } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
607                 map = at_map;
608
609         } else {
610                 fprintf( stderr, "%s: line %d: syntax is "
611                         "\"map {objectclass | attribute} [<local> | *] "
612                         "{<foreign> | *}\"\n",
613                         fname, lineno );
614                 return 1;
615         }
616
617         if ( strcmp( argv[2], "*" ) == 0 ) {
618                 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
619                         map->drop_missing = ( argc < 4 );
620                         return 0;
621                 }
622                 src = dst = argv[3];
623
624         } else if ( argc < 4 ) {
625                 src = "";
626                 dst = argv[2];
627
628         } else {
629                 src = argv[2];
630                 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
631         }
632
633         if ( ( map == at_map )
634                         && ( strcasecmp( src, "objectclass" ) == 0
635                         || strcasecmp( dst, "objectclass" ) == 0 ) )
636         {
637                 fprintf( stderr,
638                         "%s: line %d: objectclass attribute cannot be mapped\n",
639                         fname, lineno );
640         }
641
642         mapping = (struct ldapmapping *)ch_calloc( 2,
643                 sizeof(struct ldapmapping) );
644         if ( mapping == NULL ) {
645                 fprintf( stderr,
646                         "%s: line %d: out of memory\n",
647                         fname, lineno );
648                 return 1;
649         }
650         ber_str2bv( src, 0, 1, &mapping->src );
651         ber_str2bv( dst, 0, 1, &mapping->dst );
652         mapping[1].src = mapping->dst;
653         mapping[1].dst = mapping->src;
654
655         /*
656          * schema check
657          */
658         if ( is_oc ) {
659                 if ( src[0] != '\0' ) {
660                         if ( oc_bvfind( &mapping->src ) == NULL ) {
661                                 fprintf( stderr,
662         "%s: line %d: warning, source objectClass '%s' "
663         "should be defined in schema\n",
664                                         fname, lineno, src );
665
666                                 /*
667                                  * FIXME: this should become an err
668                                  */
669                                 goto error_return;
670                         }
671                 }
672
673                 if ( oc_bvfind( &mapping->dst ) == NULL ) {
674                         fprintf( stderr,
675         "%s: line %d: warning, destination objectClass '%s' "
676         "is not defined in schema\n",
677                                 fname, lineno, dst );
678                 }
679         } else {
680                 int                     rc;
681                 const char              *text = NULL;
682                 AttributeDescription    *ad = NULL;
683
684                 if ( src[0] != '\0' ) {
685                         rc = slap_bv2ad( &mapping->src, &ad, &text );
686                         if ( rc != LDAP_SUCCESS ) {
687                                 fprintf( stderr,
688         "%s: line %d: warning, source attributeType '%s' "
689         "should be defined in schema\n",
690                                         fname, lineno, src );
691
692                                 /*
693                                  * FIXME: this should become an err
694                                  */
695                                 goto error_return;
696                         }
697
698                         ad = NULL;
699                 }
700
701                 rc = slap_bv2ad( &mapping->dst, &ad, &text );
702                 if ( rc != LDAP_SUCCESS ) {
703                         fprintf( stderr,
704         "%s: line %d: warning, destination attributeType '%s' "
705         "is not defined in schema\n",
706                                 fname, lineno, dst );
707                 }
708         }
709
710         if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
711                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
712         {
713                 fprintf( stderr,
714                         "%s: line %d: duplicate mapping found (ignored)\n",
715                         fname, lineno );
716                 goto error_return;
717         }
718
719         if ( src[0] != '\0' ) {
720                 avl_insert( &map->map, (caddr_t)mapping,
721                                         mapping_cmp, mapping_dup );
722         }
723         avl_insert( &map->remap, (caddr_t)&mapping[1],
724                                 mapping_cmp, mapping_dup );
725
726         return 0;
727
728 error_return:;
729         if ( mapping ) {
730                 ch_free( mapping->src.bv_val );
731                 ch_free( mapping->dst.bv_val );
732                 ch_free( mapping );
733         }
734
735         return 1;
736 }
737
738
739 #ifdef ENABLE_REWRITE
740 static char *
741 suffix_massage_regexize( const char *s )
742 {
743         char *res, *ptr;
744         const char *p, *r;
745         int i;
746
747         for ( i = 0, p = s; 
748                         ( r = strchr( p, ',' ) ) != NULL; 
749                         p = r + 1, i++ )
750                 ;
751
752         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
753
754         ptr = lutil_strcopy( res, "(.*)" );
755         for ( i = 0, p = s;
756                         ( r = strchr( p, ',' ) ) != NULL;
757                         p = r + 1 , i++ ) {
758                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
759                 ptr = lutil_strcopy( ptr, "[ ]?" );
760
761                 if ( r[ 1 ] == ' ' ) {
762                         r++;
763                 }
764         }
765         lutil_strcopy( ptr, p );
766
767         return res;
768 }
769
770 static char *
771 suffix_massage_patternize( const char *s )
772 {
773         ber_len_t       len;
774         char            *res;
775
776         len = strlen( s );
777
778         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
779         if ( res == NULL ) {
780                 return NULL;
781         }
782
783         strcpy( res, "%1" );
784         strcpy( res + sizeof( "%1" ) - 1, s );
785
786         return res;
787 }
788
789 int
790 suffix_massage_config( 
791                 struct rewrite_info *info,
792                 struct berval *pvnc,
793                 struct berval *nvnc,
794                 struct berval *prnc,
795                 struct berval *nrnc
796 )
797 {
798         char *rargv[ 5 ];
799         int line = 0;
800
801         rargv[ 0 ] = "rewriteEngine";
802         rargv[ 1 ] = "on";
803         rargv[ 2 ] = NULL;
804         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
805
806         rargv[ 0 ] = "rewriteContext";
807         rargv[ 1 ] = "default";
808         rargv[ 2 ] = NULL;
809         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
810
811         rargv[ 0 ] = "rewriteRule";
812         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
813         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
814         rargv[ 3 ] = ":";
815         rargv[ 4 ] = NULL;
816         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
817         ch_free( rargv[ 1 ] );
818         ch_free( rargv[ 2 ] );
819         
820         rargv[ 0 ] = "rewriteContext";
821         rargv[ 1 ] = "searchEntryDN";
822         rargv[ 2 ] = NULL;
823         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
824
825         rargv[ 0 ] = "rewriteRule";
826         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
827         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
828         rargv[ 3 ] = ":";
829         rargv[ 4 ] = NULL;
830         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
831         ch_free( rargv[ 1 ] );
832         ch_free( rargv[ 2 ] );
833
834         /* backward compatibility */
835         rargv[ 0 ] = "rewriteContext";
836         rargv[ 1 ] = "searchResult";
837         rargv[ 2 ] = "alias";
838         rargv[ 3 ] = "searchEntryDN";
839         rargv[ 4 ] = NULL;
840         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
841         
842         rargv[ 0 ] = "rewriteContext";
843         rargv[ 1 ] = "matchedDN";
844         rargv[ 2 ] = "alias";
845         rargv[ 3 ] = "searchEntryDN";
846         rargv[ 4 ] = NULL;
847         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
848
849         rargv[ 0 ] = "rewriteContext";
850         rargv[ 1 ] = "searchAttrDN";
851         rargv[ 2 ] = "alias";
852         rargv[ 3 ] = "searchEntryDN";
853         rargv[ 4 ] = NULL;
854         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
855
856         /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
857          * see servers/slapd/overlays/rwm.h for details */
858         rargv[ 0 ] = "rewriteContext";
859         rargv[ 1 ] = "referralAttrDN";
860         rargv[ 2 ] = NULL;
861         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
862
863         rargv[ 0 ] = "rewriteContext";
864         rargv[ 1 ] = "referralDN";
865         rargv[ 2 ] = NULL;
866         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
867         
868         return 0;
869 }
870 #endif /* ENABLE_REWRITE */
871