]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
improve parsing of acl-authcDN/acl-passwd
[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                 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
176                         if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
177                                 fprintf( stderr, "%s: line %d: "
178                                                 "multiple URIs must have "
179                                                 "no DN part\n",
180                                         fname, lineno );
181                                 return( 1 );
182
183                         }
184                 }
185
186                 li->targets[ i ]->mt_uri = ldap_url_list2urls( ludp );
187                 ldap_free_urllist( ludp );
188                 if ( li->targets[ i ]->mt_uri == NULL) {
189                         fprintf( stderr, "%s: line %d: no memory?\n",
190                                         fname, lineno );
191                         return( 1 );
192                 }
193                 
194                 /*
195                  * uri MUST be a branch of suffix!
196                  */
197 #if 0 /* too strict a constraint */
198                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
199                         fprintf( stderr,
200         "%s: line %d: <naming context> of URI does not refer to current backend"
201         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
202                                 fname, lineno );
203                         return 1;
204                 }
205 #else
206                 /*
207                  * uri MUST be a branch of a suffix!
208                  */
209                 if ( select_backend( &li->targets[ i ]->mt_nsuffix, 0, 0 ) == NULL ) {
210                         fprintf( stderr,
211         "%s: line %d: <naming context> of URI does not resolve to a backend"
212         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
213                                 fname, lineno );
214                         return 1;
215                 }
216 #endif
217
218 #if 0
219                 /*
220                  * uri MUST not be used by other URIs!
221                  *
222                  * FIXME: this limitation may be removed,
223                  * or worked out, at least, in some manner
224                  */
225                 for ( j = 0; j < i-1; j++ ) {
226                         if ( dn_match( &li->targets[ i ]->suffix,
227                                         &li->targets[ j ]->suffix ) ) {
228                                 fprintf( stderr,
229         "%s: line %d: naming context \"%s\" already used"
230         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
231                                         fname, lineno, last+1 );
232                                 return 1;
233                         }
234                 }
235 #endif
236
237 #if 0
238                 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
239                         fname, lineno, li->targets[ i ]->uri, 
240                         li->targets[ i ]->psuffix.bv_val );
241 #endif
242                 
243         /* default target directive */
244         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
245                 int             i = li->ntargets-1;
246                 
247                 if ( argc == 1 ) {
248                         if ( i < 0 ) {
249                                 fprintf( stderr,
250         "%s: line %d: \"default-target\" alone need be"
251         " inside a \"uri\" directive\n",
252                                         fname, lineno );
253                                 return 1;
254                         }
255                         li->defaulttarget = i;
256                 } else {
257                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
258                                 if ( i >= 0 ) {
259                                         fprintf( stderr,
260         "%s: line %d: \"default-target none\""
261         " should go before uri definitions\n",
262                                                 fname, lineno );
263                                 }
264                                 li->defaulttarget = META_DEFAULT_TARGET_NONE;
265                         } else {
266                                 int n = atoi( argv[ 1 ] );
267                                 if ( n < 1 || n >= i ) {
268                                         fprintf( stderr,
269         "%s: line %d: illegal target number %d\n",
270                                                 fname, lineno, n );
271                                         return 1;
272                                 }
273                                 li->defaulttarget = n-1;
274                         }
275                 }
276                 
277         /* ttl of dn cache */
278         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
279                 if ( argc != 2 ) {
280                         fprintf( stderr,
281         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
282                                 fname, lineno );
283                         return 1;
284                 }
285                 
286                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
287                         li->cache.ttl = META_DNCACHE_FOREVER;
288                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
289                         li->cache.ttl = META_DNCACHE_DISABLED;
290                 } else {
291                         li->cache.ttl = atol( argv[ 1 ] );
292                 }
293
294         /* network timeout when connecting to ldap servers */
295         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
296                 if ( argc != 2 ) {
297                         fprintf( stderr,
298         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
299                                 fname, lineno );
300                         return 1;
301                 }
302                 li->network_timeout = atol(argv[ 1 ]);
303
304         /* name to use for meta_back_group */
305         } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
306                         || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
307         {
308                 int             i = li->ntargets-1;
309                 struct berval   dn;
310
311                 if ( i < 0 ) {
312                         fprintf( stderr,
313         "%s: line %d: need \"uri\" directive first\n",
314                                 fname, lineno );
315                         return 1;
316                 }
317                 
318                 if ( argc != 2 ) {
319                         fprintf( stderr,
320         "%s: line %d: missing name in \"binddn <name>\" line\n",
321                                 fname, lineno );
322                         return 1;
323                 }
324
325                 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
326                         fprintf( stderr, "%s: line %d: "
327                                 "\"binddn\" statement is deprecated; "
328                                 "use \"acl-authcDN\" instead\n",
329                                 fname, lineno );
330                         /* FIXME: some day we'll need to throw an error */
331                 }
332
333                 dn.bv_val = argv[ 1 ];
334                 dn.bv_len = strlen( argv[ 1 ] );
335                 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->mt_binddn,
336                         NULL ) != LDAP_SUCCESS )
337                 {
338                         fprintf( stderr, "%s: line %d: "
339                                         "bind DN '%s' is invalid\n",
340                                         fname, lineno, argv[ 1 ] );
341                         return( 1 );
342                 }
343
344         /* password to use for meta_back_group */
345         } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
346                         || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
347         {
348                 int             i = li->ntargets-1;
349
350                 if ( i < 0 ) {
351                         fprintf( stderr,
352         "%s: line %d: need \"uri\" directive first\n",
353                                 fname, lineno );
354                         return 1;
355                 }
356                 
357                 if ( argc != 2 ) {
358                         fprintf( stderr,
359         "%s: line %d: missing password in \"bindpw <password>\" line\n",
360                             fname, lineno );
361                         return 1;
362                 }
363
364                 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
365                         fprintf( stderr, "%s: line %d: "
366                                 "\"bindpw\" statement is deprecated; "
367                                 "use \"acl-passwd\" instead\n",
368                                 fname, lineno );
369                         /* FIXME: some day we'll need to throw an error */
370                 }
371
372                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_bindpw );
373                 
374         /* save bind creds for referral rebinds? */
375         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
376                 if (argc != 1) {
377                         fprintf( stderr,
378         "%s: line %d: rebind-as-user takes no arguments\n",
379                             fname, lineno );
380                         return( 1 );
381                 }
382                 li->savecred = 1;
383         
384         /* name to use as pseudo-root dn */
385         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
386                 int             i = li->ntargets-1;
387                 struct berval   dn;
388
389                 if ( i < 0 ) {
390                         fprintf( stderr,
391         "%s: line %d: need \"uri\" directive first\n",
392                                 fname, lineno );
393                         return 1;
394                 }
395                 
396                 if ( argc != 2 ) {
397                         fprintf( stderr,
398         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
399                                 fname, lineno );
400                         return 1;
401                 }
402
403                 dn.bv_val = argv[ 1 ];
404                 dn.bv_len = strlen( argv[ 1 ] );
405                 if ( dnNormalize( 0, NULL, NULL, &dn,
406                         &li->targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
407                 {
408                         fprintf( stderr, "%s: line %d: "
409                                         "pseudoroot DN '%s' is invalid\n",
410                                         fname, lineno, argv[ 1 ] );
411                         return( 1 );
412                 }
413
414         /* password to use as pseudo-root */
415         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
416                 int             i = li->ntargets-1;
417
418                 if ( i < 0 ) {
419                         fprintf( stderr,
420         "%s: line %d: need \"uri\" directive first\n",
421                                 fname, lineno );
422                         return 1;
423                 }
424                 
425                 if ( argc != 2 ) {
426                         fprintf( stderr,
427         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
428                             fname, lineno );
429                         return 1;
430                 }
431                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_pseudorootpw );
432         
433         /* dn massaging */
434         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
435                 BackendDB       *tmp_be;
436                 int             i = li->ntargets-1;
437                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
438
439                 if ( i < 0 ) {
440                         fprintf( stderr,
441         "%s: line %d: need \"uri\" directive first\n",
442                                 fname, lineno );
443                         return 1;
444                 }
445                 
446                 /*
447                  * syntax:
448                  * 
449                  *      suffixmassage <suffix> <massaged suffix>
450                  *
451                  * the <suffix> field must be defined as a valid suffix
452                  * (or suffixAlias?) for the current database;
453                  * the <massaged suffix> shouldn't have already been
454                  * defined as a valid suffix or suffixAlias for the 
455                  * current server
456                  */
457                 if ( argc != 3 ) {
458                         fprintf( stderr,
459         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
460                                 fname, lineno );
461                         return 1;
462                 }
463
464                 dn.bv_val = argv[ 1 ];
465                 dn.bv_len = strlen( argv[ 1 ] );
466                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
467                         fprintf( stderr, "%s: line %d: "
468                                         "suffix '%s' is invalid\n",
469                                         fname, lineno, argv[ 1 ] );
470                         return 1;
471                 }
472                 
473                 tmp_be = select_backend( &nvnc, 0, 0 );
474                 if ( tmp_be != NULL && tmp_be != be ) {
475                         fprintf( stderr, 
476         "%s: line %d: suffix already in use by another backend in"
477         " \"suffixMassage <suffix> <massaged suffix>\"\n",
478                                 fname, lineno );
479                         free( pvnc.bv_val );
480                         free( nvnc.bv_val );
481                         return 1;                                               
482                 }
483
484                 dn.bv_val = argv[ 2 ];
485                 dn.bv_len = strlen( argv[ 2 ] );
486                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
487                         fprintf( stderr, "%s: line %d: "
488                                         "massaged suffix '%s' is invalid\n",
489                                         fname, lineno, argv[ 2 ] );
490                         free( pvnc.bv_val );
491                         free( nvnc.bv_val );
492                         return 1;
493                 }
494         
495 #if 0   
496                 tmp_be = select_backend( &nrnc, 0, 0 );
497                 if ( tmp_be != NULL ) {
498                         fprintf( stderr,
499         "%s: line %d: massaged suffix already in use by another backend in" 
500         " \"suffixMassage <suffix> <massaged suffix>\"\n",
501                                 fname, lineno );
502                         free( pvnc.bv_val );
503                         free( nvnc.bv_val );
504                         free( prnc.bv_val );
505                         free( nrnc.bv_val );
506                         return 1;
507                 }
508 #endif
509                 
510                 /*
511                  * The suffix massaging is emulated by means of the
512                  * rewrite capabilities
513                  * FIXME: no extra rewrite capabilities should be added
514                  * to the database
515                  */
516                 return suffix_massage_config( li->targets[ i ]->mt_rwmap.rwm_rw,
517                                 &pvnc, &nvnc, &prnc, &nrnc );
518                 
519         /* rewrite stuff ... */
520         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
521                 int             i = li->ntargets-1;
522
523                 if ( i < 0 ) {
524                         if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
525                                 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
526                         }
527                         return rewrite_parse( li->rwinfo, fname, lineno,
528                                         argc, argv ); 
529                 }
530                 
531                 return rewrite_parse( li->targets[ i ]->mt_rwmap.rwm_rw,
532                                 fname, lineno, argc, argv );
533
534         /* objectclass/attribute mapping */
535         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
536                 int             i = li->ntargets-1;
537
538                 if ( i < 0 ) {
539                         fprintf( stderr,
540         "%s: line %d: need \"uri\" directive first\n",
541                                 fname, lineno );
542                         return 1;
543                 }
544
545                 return ldap_back_map_config( &li->targets[ i ]->mt_rwmap.rwm_oc, 
546                                 &li->targets[ i ]->mt_rwmap.rwm_at,
547                                 fname, lineno, argc, argv );
548         /* anything else */
549         } else {
550                 return SLAP_CONF_UNKNOWN;
551         }
552         return 0;
553 }
554
555 int
556 ldap_back_map_config(
557                 struct ldapmap  *oc_map,
558                 struct ldapmap  *at_map,
559                 const char      *fname,
560                 int             lineno,
561                 int             argc,
562                 char            **argv )
563 {
564         struct ldapmap          *map;
565         struct ldapmapping      *mapping;
566         char                    *src, *dst;
567         int                     is_oc = 0;
568
569         if ( argc < 3 || argc > 4 ) {
570                 fprintf( stderr,
571         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
572                         fname, lineno );
573                 return 1;
574         }
575
576         if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
577                 map = oc_map;
578                 is_oc = 1;
579
580         } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
581                 map = at_map;
582
583         } else {
584                 fprintf( stderr, "%s: line %d: syntax is "
585                         "\"map {objectclass | attribute} [<local> | *] "
586                         "{<foreign> | *}\"\n",
587                         fname, lineno );
588                 return 1;
589         }
590
591         if ( strcmp( argv[2], "*" ) == 0 ) {
592                 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
593                         map->drop_missing = ( argc < 4 );
594                         return 0;
595                 }
596                 src = dst = argv[3];
597
598         } else if ( argc < 4 ) {
599                 src = "";
600                 dst = argv[2];
601
602         } else {
603                 src = argv[2];
604                 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
605         }
606
607         if ( ( map == at_map )
608                         && ( strcasecmp( src, "objectclass" ) == 0
609                         || strcasecmp( dst, "objectclass" ) == 0 ) )
610         {
611                 fprintf( stderr,
612                         "%s: line %d: objectclass attribute cannot be mapped\n",
613                         fname, lineno );
614         }
615
616         mapping = (struct ldapmapping *)ch_calloc( 2,
617                 sizeof(struct ldapmapping) );
618         if ( mapping == NULL ) {
619                 fprintf( stderr,
620                         "%s: line %d: out of memory\n",
621                         fname, lineno );
622                 return 1;
623         }
624         ber_str2bv( src, 0, 1, &mapping->src );
625         ber_str2bv( dst, 0, 1, &mapping->dst );
626         mapping[1].src = mapping->dst;
627         mapping[1].dst = mapping->src;
628
629         /*
630          * schema check
631          */
632         if ( is_oc ) {
633                 if ( src[0] != '\0' ) {
634                         if ( oc_bvfind( &mapping->src ) == NULL ) {
635                                 fprintf( stderr,
636         "%s: line %d: warning, source objectClass '%s' "
637         "should be defined in schema\n",
638                                         fname, lineno, src );
639
640                                 /*
641                                  * FIXME: this should become an err
642                                  */
643                                 goto error_return;
644                         }
645                 }
646
647                 if ( oc_bvfind( &mapping->dst ) == NULL ) {
648                         fprintf( stderr,
649         "%s: line %d: warning, destination objectClass '%s' "
650         "is not defined in schema\n",
651                                 fname, lineno, dst );
652                 }
653         } else {
654                 int                     rc;
655                 const char              *text = NULL;
656                 AttributeDescription    *ad = NULL;
657
658                 if ( src[0] != '\0' ) {
659                         rc = slap_bv2ad( &mapping->src, &ad, &text );
660                         if ( rc != LDAP_SUCCESS ) {
661                                 fprintf( stderr,
662         "%s: line %d: warning, source attributeType '%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                         ad = NULL;
673                 }
674
675                 rc = slap_bv2ad( &mapping->dst, &ad, &text );
676                 if ( rc != LDAP_SUCCESS ) {
677                         fprintf( stderr,
678         "%s: line %d: warning, destination attributeType '%s' "
679         "is not defined in schema\n",
680                                 fname, lineno, dst );
681                 }
682         }
683
684         if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
685                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
686         {
687                 fprintf( stderr,
688                         "%s: line %d: duplicate mapping found (ignored)\n",
689                         fname, lineno );
690                 goto error_return;
691         }
692
693         if ( src[0] != '\0' ) {
694                 avl_insert( &map->map, (caddr_t)mapping,
695                                         mapping_cmp, mapping_dup );
696         }
697         avl_insert( &map->remap, (caddr_t)&mapping[1],
698                                 mapping_cmp, mapping_dup );
699
700         return 0;
701
702 error_return:;
703         if ( mapping ) {
704                 ch_free( mapping->src.bv_val );
705                 ch_free( mapping->dst.bv_val );
706                 ch_free( mapping );
707         }
708
709         return 1;
710 }
711
712
713 #ifdef ENABLE_REWRITE
714 static char *
715 suffix_massage_regexize( const char *s )
716 {
717         char *res, *ptr;
718         const char *p, *r;
719         int i;
720
721         for ( i = 0, p = s; 
722                         ( r = strchr( p, ',' ) ) != NULL; 
723                         p = r + 1, i++ )
724                 ;
725
726         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
727
728         ptr = lutil_strcopy( res, "(.*)" );
729         for ( i = 0, p = s;
730                         ( r = strchr( p, ',' ) ) != NULL;
731                         p = r + 1 , i++ ) {
732                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
733                 ptr = lutil_strcopy( ptr, "[ ]?" );
734
735                 if ( r[ 1 ] == ' ' ) {
736                         r++;
737                 }
738         }
739         lutil_strcopy( ptr, p );
740
741         return res;
742 }
743
744 static char *
745 suffix_massage_patternize( const char *s )
746 {
747         ber_len_t       len;
748         char            *res;
749
750         len = strlen( s );
751
752         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
753         if ( res == NULL ) {
754                 return NULL;
755         }
756
757         strcpy( res, "%1" );
758         strcpy( res + sizeof( "%1" ) - 1, s );
759
760         return res;
761 }
762
763 int
764 suffix_massage_config( 
765                 struct rewrite_info *info,
766                 struct berval *pvnc,
767                 struct berval *nvnc,
768                 struct berval *prnc,
769                 struct berval *nrnc
770 )
771 {
772         char *rargv[ 5 ];
773         int line = 0;
774
775         rargv[ 0 ] = "rewriteEngine";
776         rargv[ 1 ] = "on";
777         rargv[ 2 ] = NULL;
778         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
779
780         rargv[ 0 ] = "rewriteContext";
781         rargv[ 1 ] = "default";
782         rargv[ 2 ] = NULL;
783         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
784
785         rargv[ 0 ] = "rewriteRule";
786         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
787         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
788         rargv[ 3 ] = ":";
789         rargv[ 4 ] = NULL;
790         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
791         ch_free( rargv[ 1 ] );
792         ch_free( rargv[ 2 ] );
793         
794         rargv[ 0 ] = "rewriteContext";
795         rargv[ 1 ] = "searchEntryDN";
796         rargv[ 2 ] = NULL;
797         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
798
799         rargv[ 0 ] = "rewriteRule";
800         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
801         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
802         rargv[ 3 ] = ":";
803         rargv[ 4 ] = NULL;
804         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
805         ch_free( rargv[ 1 ] );
806         ch_free( rargv[ 2 ] );
807
808         /* backward compatibility */
809         rargv[ 0 ] = "rewriteContext";
810         rargv[ 1 ] = "searchResult";
811         rargv[ 2 ] = "alias";
812         rargv[ 3 ] = "searchEntryDN";
813         rargv[ 4 ] = NULL;
814         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
815         
816         rargv[ 0 ] = "rewriteContext";
817         rargv[ 1 ] = "matchedDN";
818         rargv[ 2 ] = "alias";
819         rargv[ 3 ] = "searchEntryDN";
820         rargv[ 4 ] = NULL;
821         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
822
823         rargv[ 0 ] = "rewriteContext";
824         rargv[ 1 ] = "searchAttrDN";
825         rargv[ 2 ] = "alias";
826         rargv[ 3 ] = "searchEntryDN";
827         rargv[ 4 ] = NULL;
828         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
829
830         /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
831          * see servers/slapd/overlays/rwm.h for details */
832         rargv[ 0 ] = "rewriteContext";
833         rargv[ 1 ] = "referralAttrDN";
834         rargv[ 2 ] = NULL;
835         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
836
837         rargv[ 0 ] = "rewriteContext";
838         rargv[ 1 ] = "referralDN";
839         rargv[ 2 ] = NULL;
840         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
841         
842         return 0;
843 }
844 #endif /* ENABLE_REWRITE */
845