]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
import stuff removed 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-2004 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->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
49         if ( lt->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->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->rwmap.rwm_rw, "<suffix massage>", 
72                                 1, 2, rargv );
73         }
74
75         ldap_back_map_init( &lt->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 ]->psuffix,
165                         &li->targets[ i ]->suffix, 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 ]->uri = ldap_url_list2urls( ludp );
187                 ldap_free_urllist( ludp );
188                 if ( li->targets[ i ]->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 ]->suffix, 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 ], "binddn" ) == 0 ) {
306                 int             i = li->ntargets-1;
307                 struct berval   dn;
308
309                 if ( i < 0 ) {
310                         fprintf( stderr,
311         "%s: line %d: need \"uri\" directive first\n",
312                                 fname, lineno );
313                         return 1;
314                 }
315                 
316                 if ( argc != 2 ) {
317                         fprintf( stderr,
318         "%s: line %d: missing name in \"binddn <name>\" line\n",
319                                 fname, lineno );
320                         return 1;
321                 }
322
323                 dn.bv_val = argv[ 1 ];
324                 dn.bv_len = strlen( argv[ 1 ] );
325                 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->binddn,
326                         NULL ) != LDAP_SUCCESS )
327                 {
328                         fprintf( stderr, "%s: line %d: "
329                                         "bind DN '%s' is invalid\n",
330                                         fname, lineno, argv[ 1 ] );
331                         return( 1 );
332                 }
333
334         /* password to use for meta_back_group */
335         } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
336                 int             i = li->ntargets-1;
337
338                 if ( i < 0 ) {
339                         fprintf( stderr,
340         "%s: line %d: need \"uri\" directive first\n",
341                                 fname, lineno );
342                         return 1;
343                 }
344                 
345                 if ( argc != 2 ) {
346                         fprintf( stderr,
347         "%s: line %d: missing password in \"bindpw <password>\" line\n",
348                             fname, lineno );
349                         return 1;
350                 }
351                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
352                 
353         /* save bind creds for referral rebinds? */
354         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
355                 if (argc != 1) {
356                         fprintf( stderr,
357         "%s: line %d: rebind-as-user takes no arguments\n",
358                             fname, lineno );
359                         return( 1 );
360                 }
361                 li->savecred = 1;
362         
363         /* name to use as pseudo-root dn */
364         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
365                 int             i = li->ntargets-1;
366                 struct berval   dn;
367
368                 if ( i < 0 ) {
369                         fprintf( stderr,
370         "%s: line %d: need \"uri\" directive first\n",
371                                 fname, lineno );
372                         return 1;
373                 }
374                 
375                 if ( argc != 2 ) {
376                         fprintf( stderr,
377         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
378                                 fname, lineno );
379                         return 1;
380                 }
381
382                 dn.bv_val = argv[ 1 ];
383                 dn.bv_len = strlen( argv[ 1 ] );
384                 if ( dnNormalize( 0, NULL, NULL, &dn,
385                         &li->targets[ i ]->pseudorootdn, NULL ) != LDAP_SUCCESS )
386                 {
387                         fprintf( stderr, "%s: line %d: "
388                                         "pseudoroot DN '%s' is invalid\n",
389                                         fname, lineno, argv[ 1 ] );
390                         return( 1 );
391                 }
392
393         /* password to use as pseudo-root */
394         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
395                 int             i = li->ntargets-1;
396
397                 if ( i < 0 ) {
398                         fprintf( stderr,
399         "%s: line %d: need \"uri\" directive first\n",
400                                 fname, lineno );
401                         return 1;
402                 }
403                 
404                 if ( argc != 2 ) {
405                         fprintf( stderr,
406         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
407                             fname, lineno );
408                         return 1;
409                 }
410                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
411         
412         /* dn massaging */
413         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
414                 BackendDB       *tmp_be;
415                 int             i = li->ntargets-1;
416                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
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                 /*
426                  * syntax:
427                  * 
428                  *      suffixmassage <suffix> <massaged suffix>
429                  *
430                  * the <suffix> field must be defined as a valid suffix
431                  * (or suffixAlias?) for the current database;
432                  * the <massaged suffix> shouldn't have already been
433                  * defined as a valid suffix or suffixAlias for the 
434                  * current server
435                  */
436                 if ( argc != 3 ) {
437                         fprintf( stderr,
438         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
439                                 fname, lineno );
440                         return 1;
441                 }
442
443                 dn.bv_val = argv[ 1 ];
444                 dn.bv_len = strlen( argv[ 1 ] );
445                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
446                         fprintf( stderr, "%s: line %d: "
447                                         "suffix '%s' is invalid\n",
448                                         fname, lineno, argv[ 1 ] );
449                         return 1;
450                 }
451                 
452                 tmp_be = select_backend( &nvnc, 0, 0 );
453                 if ( tmp_be != NULL && tmp_be != be ) {
454                         fprintf( stderr, 
455         "%s: line %d: suffix already in use by another backend in"
456         " \"suffixMassage <suffix> <massaged suffix>\"\n",
457                                 fname, lineno );
458                         free( pvnc.bv_val );
459                         free( nvnc.bv_val );
460                         return 1;                                               
461                 }
462
463                 dn.bv_val = argv[ 2 ];
464                 dn.bv_len = strlen( argv[ 2 ] );
465                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
466                         fprintf( stderr, "%s: line %d: "
467                                         "massaged suffix '%s' is invalid\n",
468                                         fname, lineno, argv[ 2 ] );
469                         free( pvnc.bv_val );
470                         free( nvnc.bv_val );
471                         return 1;
472                 }
473         
474 #if 0   
475                 tmp_be = select_backend( &nrnc, 0, 0 );
476                 if ( tmp_be != NULL ) {
477                         fprintf( stderr,
478         "%s: line %d: massaged suffix already in use by another backend in" 
479         " \"suffixMassage <suffix> <massaged suffix>\"\n",
480                                 fname, lineno );
481                         free( pvnc.bv_val );
482                         free( nvnc.bv_val );
483                         free( prnc.bv_val );
484                         free( nrnc.bv_val );
485                         return 1;
486                 }
487 #endif
488                 
489                 /*
490                  * The suffix massaging is emulated by means of the
491                  * rewrite capabilities
492                  * FIXME: no extra rewrite capabilities should be added
493                  * to the database
494                  */
495                 return suffix_massage_config( li->targets[ i ]->rwmap.rwm_rw,
496                                 &pvnc, &nvnc, &prnc, &nrnc );
497                 
498         /* rewrite stuff ... */
499         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
500                 int             i = li->ntargets-1;
501
502                 if ( i < 0 ) {
503                         if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
504                                 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
505                         }
506                         return rewrite_parse(li->rwinfo, fname, lineno,
507                                         argc, argv); 
508                 }
509                 
510                 return rewrite_parse( li->targets[ i ]->rwmap.rwm_rw, fname, lineno,
511                                 argc, argv );
512
513         /* objectclass/attribute mapping */
514         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
515                 int             i = li->ntargets-1;
516
517                 if ( i < 0 ) {
518                         fprintf( stderr,
519         "%s: line %d: need \"uri\" directive first\n",
520                                 fname, lineno );
521                         return 1;
522                 }
523
524                 return ldap_back_map_config( &li->targets[ i ]->rwmap.rwm_oc, 
525                                 &li->targets[ i ]->rwmap.rwm_at,
526                                 fname, lineno, argc, argv );
527         /* anything else */
528         } else {
529                 return SLAP_CONF_UNKNOWN;
530         }
531         return 0;
532 }
533
534 int
535 ldap_back_map_config(
536                 struct ldapmap  *oc_map,
537                 struct ldapmap  *at_map,
538                 const char      *fname,
539                 int             lineno,
540                 int             argc,
541                 char            **argv )
542 {
543         struct ldapmap          *map;
544         struct ldapmapping      *mapping;
545         char                    *src, *dst;
546         int                     is_oc = 0;
547
548         if ( argc < 3 || argc > 4 ) {
549                 fprintf( stderr,
550         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
551                         fname, lineno );
552                 return 1;
553         }
554
555         if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
556                 map = oc_map;
557                 is_oc = 1;
558
559         } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
560                 map = at_map;
561
562         } else {
563                 fprintf( stderr, "%s: line %d: syntax is "
564                         "\"map {objectclass | attribute} [<local> | *] "
565                         "{<foreign> | *}\"\n",
566                         fname, lineno );
567                 return 1;
568         }
569
570         if ( strcmp( argv[2], "*" ) == 0 ) {
571                 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
572                         map->drop_missing = ( argc < 4 );
573                         return 0;
574                 }
575                 src = dst = argv[3];
576
577         } else if ( argc < 4 ) {
578                 src = "";
579                 dst = argv[2];
580
581         } else {
582                 src = argv[2];
583                 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
584         }
585
586         if ( ( map == at_map )
587                         && ( strcasecmp( src, "objectclass" ) == 0
588                         || strcasecmp( dst, "objectclass" ) == 0 ) )
589         {
590                 fprintf( stderr,
591                         "%s: line %d: objectclass attribute cannot be mapped\n",
592                         fname, lineno );
593         }
594
595         mapping = (struct ldapmapping *)ch_calloc( 2,
596                 sizeof(struct ldapmapping) );
597         if ( mapping == NULL ) {
598                 fprintf( stderr,
599                         "%s: line %d: out of memory\n",
600                         fname, lineno );
601                 return 1;
602         }
603         ber_str2bv( src, 0, 1, &mapping->src );
604         ber_str2bv( dst, 0, 1, &mapping->dst );
605         mapping[1].src = mapping->dst;
606         mapping[1].dst = mapping->src;
607
608         /*
609          * schema check
610          */
611         if ( is_oc ) {
612                 if ( src[0] != '\0' ) {
613                         if ( oc_bvfind( &mapping->src ) == NULL ) {
614                                 fprintf( stderr,
615         "%s: line %d: warning, source objectClass '%s' "
616         "should be defined in schema\n",
617                                         fname, lineno, src );
618
619                                 /*
620                                  * FIXME: this should become an err
621                                  */
622                                 goto error_return;
623                         }
624                 }
625
626                 if ( oc_bvfind( &mapping->dst ) == NULL ) {
627                         fprintf( stderr,
628         "%s: line %d: warning, destination objectClass '%s' "
629         "is not defined in schema\n",
630                                 fname, lineno, dst );
631                 }
632         } else {
633                 int                     rc;
634                 const char              *text = NULL;
635                 AttributeDescription    *ad = NULL;
636
637                 if ( src[0] != '\0' ) {
638                         rc = slap_bv2ad( &mapping->src, &ad, &text );
639                         if ( rc != LDAP_SUCCESS ) {
640                                 fprintf( stderr,
641         "%s: line %d: warning, source attributeType '%s' "
642         "should be defined in schema\n",
643                                         fname, lineno, src );
644
645                                 /*
646                                  * FIXME: this should become an err
647                                  */
648                                 goto error_return;
649                         }
650
651                         ad = NULL;
652                 }
653
654                 rc = slap_bv2ad( &mapping->dst, &ad, &text );
655                 if ( rc != LDAP_SUCCESS ) {
656                         fprintf( stderr,
657         "%s: line %d: warning, destination attributeType '%s' "
658         "is not defined in schema\n",
659                                 fname, lineno, dst );
660                 }
661         }
662
663         if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
664                         || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
665         {
666                 fprintf( stderr,
667                         "%s: line %d: duplicate mapping found (ignored)\n",
668                         fname, lineno );
669                 goto error_return;
670         }
671
672         if ( src[0] != '\0' ) {
673                 avl_insert( &map->map, (caddr_t)mapping,
674                                         mapping_cmp, mapping_dup );
675         }
676         avl_insert( &map->remap, (caddr_t)&mapping[1],
677                                 mapping_cmp, mapping_dup );
678
679         return 0;
680
681 error_return:;
682         if ( mapping ) {
683                 ch_free( mapping->src.bv_val );
684                 ch_free( mapping->dst.bv_val );
685                 ch_free( mapping );
686         }
687
688         return 1;
689 }
690
691
692 #ifdef ENABLE_REWRITE
693 static char *
694 suffix_massage_regexize( const char *s )
695 {
696         char *res, *ptr;
697         const char *p, *r;
698         int i;
699
700         for ( i = 0, p = s; 
701                         ( r = strchr( p, ',' ) ) != NULL; 
702                         p = r + 1, i++ )
703                 ;
704
705         res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
706
707         ptr = lutil_strcopy( res, "(.*)" );
708         for ( i = 0, p = s;
709                         ( r = strchr( p, ',' ) ) != NULL;
710                         p = r + 1 , i++ ) {
711                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
712                 ptr = lutil_strcopy( ptr, "[ ]?" );
713
714                 if ( r[ 1 ] == ' ' ) {
715                         r++;
716                 }
717         }
718         lutil_strcopy( ptr, p );
719
720         return res;
721 }
722
723 static char *
724 suffix_massage_patternize( const char *s )
725 {
726         ber_len_t       len;
727         char            *res;
728
729         len = strlen( s );
730
731         res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
732         if ( res == NULL ) {
733                 return NULL;
734         }
735
736         strcpy( res, "%1" );
737         strcpy( res + sizeof( "%1" ) - 1, s );
738
739         return res;
740 }
741
742 int
743 suffix_massage_config( 
744                 struct rewrite_info *info,
745                 struct berval *pvnc,
746                 struct berval *nvnc,
747                 struct berval *prnc,
748                 struct berval *nrnc
749 )
750 {
751         char *rargv[ 5 ];
752         int line = 0;
753
754         rargv[ 0 ] = "rewriteEngine";
755         rargv[ 1 ] = "on";
756         rargv[ 2 ] = NULL;
757         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
758
759         rargv[ 0 ] = "rewriteContext";
760         rargv[ 1 ] = "default";
761         rargv[ 2 ] = NULL;
762         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
763
764         rargv[ 0 ] = "rewriteRule";
765         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
766         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
767         rargv[ 3 ] = ":";
768         rargv[ 4 ] = NULL;
769         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
770         ch_free( rargv[ 1 ] );
771         ch_free( rargv[ 2 ] );
772         
773         rargv[ 0 ] = "rewriteContext";
774         rargv[ 1 ] = "searchResult";
775         rargv[ 2 ] = NULL;
776         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
777         
778         rargv[ 0 ] = "rewriteRule";
779         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
780         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
781         rargv[ 3 ] = ":";
782         rargv[ 4 ] = NULL;
783         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
784         ch_free( rargv[ 1 ] );
785         ch_free( rargv[ 2 ] );
786
787         rargv[ 0 ] = "rewriteContext";
788         rargv[ 1 ] = "matchedDN";
789         rargv[ 2 ] = "alias";
790         rargv[ 3 ] = "searchResult";
791         rargv[ 4 ] = NULL;
792         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
793
794         rargv[ 0 ] = "rewriteContext";
795         rargv[ 1 ] = "searchAttrDN";
796         rargv[ 2 ] = "alias";
797         rargv[ 3 ] = "searchResult";
798         rargv[ 4 ] = NULL;
799         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
800
801         return 0;
802 }
803 #endif /* ENABLE_REWRITE */
804