]> git.sur5r.net Git - openldap/blob - libraries/librewrite/map.c
Added thread-pool getkey/setkey functions
[openldap] / libraries / librewrite / map.c
1 /******************************************************************************
2  *
3  * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
4  * All rights reserved.
5  *
6  * Permission is granted to anyone to use this software for any purpose
7  * on any computer system, and to alter it and redistribute it, subject
8  * to the following restrictions:
9  *
10  * 1. The author is not responsible for the consequences of use of this
11  * software, no matter how awful, even if they arise from flaws in it.
12  *
13  * 2. The origin of this software must not be misrepresented, either by
14  * explicit claim or by omission.  Since few users ever read sources,
15  * credits should appear in the documentation.
16  *
17  * 3. Altered versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.  Since few users
19  * ever read sources, credits should appear in the documentation.
20  * 
21  * 4. This notice may not be removed or altered.
22  *
23  ******************************************************************************/
24
25 #include <portable.h>
26
27 #include <stdio.h>
28
29 #ifdef HAVE_PWD_H
30 #include <pwd.h>
31 #endif
32
33 #include "rewrite-int.h"
34 #include "rewrite-map.h"
35
36 /*
37  * Global data
38  */
39 #ifdef USE_REWRITE_LDAP_PVT_THREADS
40 ldap_pvt_thread_mutex_t xpasswd_mutex;
41 static int xpasswd_mutex_init = 0;
42 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
43
44 /*
45  * Map parsing
46  * NOTE: these are old-fashion maps; new maps will be parsed on separate
47  * config lines, and referred by name.
48  */
49 struct rewrite_map *
50 rewrite_xmap_parse(
51                 struct rewrite_info *info,
52                 const char *s,
53                 const char **currpos
54 )
55 {
56         struct rewrite_map *map;
57
58         assert( info != NULL );
59         assert( s != NULL );
60         assert( currpos != NULL );
61
62         Debug( LDAP_DEBUG_ARGS, "rewrite_xmap_parse: %s\n%s%s",
63                         s, "", "" );
64
65         *currpos = NULL;
66
67         map = calloc( sizeof( struct rewrite_map ), 1 );
68         if ( map == NULL ) {
69                 Debug( LDAP_DEBUG_ANY, "rewrite_xmap_parse:"
70                                 " calloc failed\n%s%s%s", "", "", "" );
71                 return NULL;
72         }
73
74         /*
75          * Experimental passwd map:
76          * replaces the uid with the matching gecos from /etc/passwd file 
77          */
78         if ( strncasecmp(s, "xpasswd", 7 ) == 0 ) {
79                 map->lm_type = REWRITE_MAP_XPWDMAP;
80                 map->lm_name = strdup( "xpasswd" );
81
82                 assert( s[7] == '}' );
83                 *currpos = s + 8;
84
85 #ifdef USE_REWRITE_LDAP_PVT_THREADS
86                 if ( !xpasswd_mutex_init ) {
87                         xpasswd_mutex_init = 1;
88                         if ( ldap_pvt_thread_mutex_init( &xpasswd_mutex ) ) {
89                                 free( map );
90                                 return NULL;
91                         }
92                 }
93 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
94
95                 /* Don't really care if fails */
96                 return map;
97         
98         /*
99          * Experimental file map:
100          * looks up key in a `key value' ascii file
101          */
102         } else if ( strncasecmp(s, "xfile", 5 ) == 0 ) {
103                 char *filename;
104                 const char *p;
105                 int l;
106                 int c = 5;
107                 
108                 map->lm_type = REWRITE_MAP_XFILEMAP;
109                 
110                 if ( s[ c ] != '(' ) {
111                         free( map );
112                         return NULL;
113                 }
114
115                 /* Must start with '/' for security concerns */
116                 c++;
117                 if ( s[ c ] != '/' ) {
118                         free( map );
119                         return NULL;
120                 }
121
122                 for ( p = s + c; p[ 0 ] != '\0' && p[ 0 ] != ')'; p++ );
123                 if ( p[ 0 ] != ')' ) {
124                         free( map );
125                         return NULL;
126                 }
127
128                 l = p - s - c;
129                 filename = calloc( sizeof( char ), l + 1 );
130                 AC_MEMCPY( filename, s + c, l );
131                 filename[ l ] = '\0';
132                 
133                 map->lm_args = ( void * )fopen( filename, "r" );
134                 free( filename );
135
136                 if ( map->lm_args == NULL ) {
137                         free( map );
138                         return NULL;
139                 }
140
141                 *currpos = p + 1;
142
143 #ifdef USE_REWRITE_LDAP_PVT_THREADS
144                 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
145                         fclose( ( FILE * )map->lm_args );
146                         free( map );
147                         return NULL;
148                 }
149 #endif /* USE_REWRITE_LDAP_PVT_THREADS */       
150                 
151                 return map;
152
153         /*
154          * Experimental ldap map:
155          * looks up key on the fly (not implemented!)
156          */
157         } else if ( strncasecmp(s, "xldap", 5 ) == 0 ) {
158                 char *p;
159                 char *url;
160                 int l, rc;
161                 int c = 5;
162                 LDAPURLDesc *lud;
163
164                 if ( s[ c ] != '(' ) {
165                         free( map );
166                         return NULL;
167                 }
168                 c++;
169                 
170                 p = strchr( s, '}' );
171                 if ( p == NULL ) {
172                         free( map );
173                         return NULL;
174                 }
175                 p--;
176
177                 *currpos = p + 2;
178         
179                 /*
180                  * Add two bytes for urlencoding of '%s'
181                  */
182                 l = p - s - c;
183                 url = calloc( sizeof( char ), l + 3 );
184                 AC_MEMCPY( url, s + c, l );
185                 url[ l ] = '\0';
186
187                 /*
188                  * Urlencodes the '%s' for ldap_url_parse
189                  */
190                 p = strchr( url, '%' );
191                 if ( p != NULL ) {
192                         AC_MEMCPY( p + 3, p + 1, strlen( p + 1 ) + 1 );
193                         p[ 1 ] = '2';
194                         p[ 2 ] = '5';
195                 }
196
197                 rc =  ldap_url_parse( url, &lud );
198                 free( url );
199
200                 if ( rc != LDAP_SUCCESS ) {
201                         free( map );
202                         return NULL;
203                 }
204                 assert( lud != NULL );
205
206                 map->lm_args = ( void * )lud;
207                 map->lm_type = REWRITE_MAP_XLDAPMAP;
208
209 #ifdef USE_REWRITE_LDAP_PVT_THREADS
210                 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
211                         ldap_free_urldesc( lud );
212                         free( map );
213                         return NULL;
214                 }
215 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
216
217                 return map;
218         
219         /* Unhandled map */
220         }
221         
222         return NULL;
223 }
224
225 struct rewrite_map *
226 rewrite_map_parse(
227                 struct rewrite_info *info,
228                 const char *string,
229                 const char **currpos
230 )
231 {
232         struct rewrite_map *map = NULL;
233         struct rewrite_subst *subst = NULL;
234         char *s, *begin = NULL, *end;
235         const char *p;
236         int l, cnt;
237
238         assert( info != NULL );
239         assert( string != NULL );
240         assert( currpos != NULL );
241
242         *currpos = NULL;
243
244         /*
245          * Go to the end of the map invocation (the right closing brace)
246          */
247         for ( p = string, cnt = 1; p[ 0 ] != '\0' && cnt > 0; p++ ) {
248                 if ( p[ 0 ] == REWRITE_SUBMATCH_ESCAPE ) {
249                         /*
250                          * '%' marks the beginning of a new map
251                          */
252                         if ( p[ 1 ] == '{' ) {
253                                 cnt++;
254                         /*
255                          * '%' followed by a digit may mark the beginning
256                          * of an old map
257                          */
258                         } else if ( isdigit( (unsigned char) p[ 1 ] ) && p[ 2 ] == '{' ) {
259                                 cnt++;
260                                 p++;
261                         }
262                         if ( p[ 1 ] != '\0' )
263                                 p++;
264                 } else if ( p[ 0 ] == '}' ) {
265                         cnt--;
266                 }
267         }
268         if ( cnt != 0 ) {
269                 return NULL;
270         }
271         *currpos = p;
272         
273         /*
274          * Copy the map invocation
275          */
276         l = p - string - 1;
277         s = calloc( sizeof( char ), l + 1 );
278         AC_MEMCPY( s, string, l );
279         s[ l ] = 0;
280
281         /*
282          * Isolate the map name (except for variable deref)
283          */
284         switch ( s[ 0 ] ) {
285         case REWRITE_OPERATOR_VARIABLE_GET:
286         case REWRITE_OPERATOR_PARAM_GET:
287                 break;
288         default:
289                 begin = strchr( s, '(' );
290                 if ( begin == NULL ) {
291                         free( s );
292                         return NULL;
293                 }
294                 begin[ 0 ] = '\0';
295                 begin++;
296                 break;
297         }
298
299         /*
300          * Check for special map types
301          */
302         p = s;
303         switch ( p[ 0 ] ) {
304         case REWRITE_OPERATOR_SUBCONTEXT:
305         case REWRITE_OPERATOR_COMMAND:
306         case REWRITE_OPERATOR_VARIABLE_SET:
307         case REWRITE_OPERATOR_VARIABLE_GET:
308         case REWRITE_OPERATOR_PARAM_GET:
309                 p++;
310                 break;
311         }
312
313         /*
314          * Variable set and get may be repeated to indicate session-wide
315          * instead of operation-wide variables
316          */
317         switch ( p[ 0 ] ) {
318         case REWRITE_OPERATOR_VARIABLE_SET:
319         case REWRITE_OPERATOR_VARIABLE_GET:
320                 p++;
321                 break;
322         }
323
324         /*
325          * Variable get token can be appended to variable set to mean store
326          * AND rewrite
327          */
328         if ( p[ 0 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
329                 p++;
330         }
331         
332         /*
333          * Check the syntax of the variable name
334          */
335         if ( !isalpha( (unsigned char) p[ 0 ] ) ) {
336                 free( s );
337                 return NULL;
338         }
339         for ( p++; p[ 0 ] != '\0'; p++ ) {
340                 if ( !isalnum( (unsigned char) p[ 0 ] ) ) {
341                         free( s );
342                         return NULL;
343                 }
344         }
345
346         /*
347          * Isolate the argument of the map (except for variable deref)
348          */
349         switch ( s[ 0 ] ) {
350         case REWRITE_OPERATOR_VARIABLE_GET:
351         case REWRITE_OPERATOR_PARAM_GET:
352                 break;
353         default:
354                 end = strrchr( begin, ')' );
355                 if ( end == NULL ) {
356                         free( s );
357                         return NULL;
358                 }
359                 end[ 0 ] = '\0';
360
361                 /*
362                  * Compile the substitution pattern of the map argument
363                  */
364                 subst = rewrite_subst_compile( info, begin );
365                 if ( subst == NULL ) {
366                         free( s );
367                         return NULL;
368                 }
369                 break;
370         }
371
372         /*
373          * Create the map
374          */
375         map = calloc( sizeof( struct rewrite_map ), 1 );
376         if ( map == NULL ) {
377                 if ( subst != NULL ) {
378                         free( subst );
379                 }
380                 free( s );
381                 return NULL;
382         }
383         
384 #ifdef USE_REWRITE_LDAP_PVT_THREADS
385         if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
386                 if ( subst != NULL ) {
387                         free( subst );
388                 }
389                 free( s );
390                 free( map );
391                 return NULL;
392         }
393 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
394                         
395         /*
396          * No subst for variable deref
397          */
398         switch ( s[ 0 ] ) {
399         case REWRITE_OPERATOR_VARIABLE_GET:
400         case REWRITE_OPERATOR_PARAM_GET:
401                 break;
402         default:
403                 map->lm_subst = subst;
404                 break;
405         }
406
407         /*
408          * Parses special map types
409          */
410         switch ( s[ 0 ] ) {
411         
412         /*
413          * Subcontext
414          */
415         case REWRITE_OPERATOR_SUBCONTEXT:               /* '>' */
416
417                 /*
418                  * Fetch the rewrite context
419                  * it MUST have been defined previously
420                  */
421                 map->lm_type = REWRITE_MAP_SUBCONTEXT;
422                 map->lm_name = strdup( s + 1 );
423                 map->lm_data = rewrite_context_find( info, s + 1 );
424                 if ( map->lm_data == NULL ) {
425                         free( s );
426                         free( map );
427                         return NULL;
428                 }
429                 break;
430
431         /*
432          * External command
433          */
434         case REWRITE_OPERATOR_COMMAND:          /* '|' */
435                 free( map );
436                 map = NULL;
437                 break;
438         
439         /*
440          * Variable set
441          */
442         case REWRITE_OPERATOR_VARIABLE_SET:     /* '&' */
443                 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_SET ) {
444                         if ( s[ 2 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
445                                 map->lm_type = REWRITE_MAP_SETW_SESN_VAR;
446                                 map->lm_name = strdup( s + 3 );
447                         } else {
448                                 map->lm_type = REWRITE_MAP_SET_SESN_VAR;
449                                 map->lm_name = strdup( s + 2 );
450                         }
451                 } else {
452                         if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
453                                 map->lm_type = REWRITE_MAP_SETW_OP_VAR;
454                                 map->lm_name = strdup( s + 2 );
455                         } else {
456                                 map->lm_type = REWRITE_MAP_SET_OP_VAR;
457                                 map->lm_name = strdup( s + 1 );
458                         }
459                 }
460                 break;
461         
462         /*
463          * Variable dereference
464          */
465         case REWRITE_OPERATOR_VARIABLE_GET:     /* '*' */
466                 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
467                         map->lm_type = REWRITE_MAP_GET_SESN_VAR;
468                         map->lm_name = strdup( s + 2 );
469                 } else {
470                         map->lm_type = REWRITE_MAP_GET_OP_VAR;
471                         map->lm_name = strdup( s + 1 );
472                 }
473                 break;
474         
475         /*
476          * Parameter
477          */
478         case REWRITE_OPERATOR_PARAM_GET:                /* '$' */
479                 map->lm_type = REWRITE_MAP_GET_PARAM;
480                 map->lm_name = strdup( s + 1 );
481                 break;
482         
483         /*
484          * Built-in map
485          */
486         default:
487                 map->lm_type = REWRITE_MAP_BUILTIN;
488                 map->lm_name = strdup( s );
489                 map->lm_data = rewrite_builtin_map_find( info, s );
490                 if ( map->lm_data == NULL ) {
491                         return NULL;
492                 }
493                 break;
494
495         }
496         
497         free( s );
498         return map;
499 }
500
501 /*
502  * Map key -> value resolution
503  * NOTE: these are old-fashion maps; new maps will be parsed on separate
504  * config lines, and referred by name.
505  */
506 int
507 rewrite_xmap_apply(
508                 struct rewrite_info *info,
509                 struct rewrite_op *op,
510                 struct rewrite_map *map,
511                 struct berval *key,
512                 struct berval *val
513 )
514 {
515         int rc = REWRITE_SUCCESS;
516         
517         assert( info != NULL );
518         assert( op != NULL );
519         assert( map != NULL );
520         assert( key != NULL );
521         assert( val != NULL );
522         
523         val->bv_val = NULL;
524         val->bv_len = 0;
525         
526         switch ( map->lm_type ) {
527 #ifdef HAVE_GETPWNAM
528         case REWRITE_MAP_XPWDMAP: {
529                 struct passwd *pwd;
530
531 #ifdef USE_REWRITE_LDAP_PVT_THREADS
532                 ldap_pvt_thread_mutex_lock( &xpasswd_mutex );
533 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
534                 
535                 pwd = getpwnam( key->bv_val );
536                 if ( pwd == NULL ) {
537
538 #ifdef USE_REWRITE_LDAP_PVT_THREADS
539                         ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
540 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
541
542                         rc = REWRITE_NO_SUCH_OBJECT;
543                         break;
544                 }
545
546 #ifdef HAVE_PW_GECOS
547                 if ( pwd->pw_gecos != NULL && pwd->pw_gecos[0] != '\0' ) {
548                         int l = strlen( pwd->pw_gecos );
549                         
550                         val->bv_val = strdup( pwd->pw_gecos );
551                         if ( val->bv_val == NULL ) {
552
553 #ifdef USE_REWRITE_LDAP_PVT_THREADS
554                                 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
555 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
556
557                                 rc = REWRITE_ERR;
558                                 break;
559                         }
560                         val->bv_len = l;
561                 } else
562 #endif /* HAVE_PW_GECOS */
563                 {
564                         val->bv_val = strdup( key->bv_val );
565                         val->bv_len = key->bv_len;
566                 }
567
568 #ifdef USE_REWRITE_LDAP_PVT_THREADS
569                 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
570 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
571                         
572                 break;
573         }
574 #endif /* HAVE_GETPWNAM*/
575         
576         case REWRITE_MAP_XFILEMAP: {
577                 char buf[1024];
578                 
579                 if ( map->lm_args == NULL ) {
580                         rc = REWRITE_ERR;
581                         break;
582                 }
583                 
584 #ifdef USE_REWRITE_LDAP_PVT_THREADS
585                 ldap_pvt_thread_mutex_lock( &map->lm_mutex );
586 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
587
588                 rewind( ( FILE * )map->lm_args );
589                 
590                 while ( fgets( buf, sizeof( buf ), ( FILE * )map->lm_args ) ) {
591                         char *p;
592                         int blen;
593                         
594                         blen = strlen( buf );
595                         if ( buf[ blen - 1 ] == '\n' ) {
596                                 buf[ blen - 1 ] = '\0';
597                         }
598                         
599                         p = strtok( buf, " " );
600                         if ( p == NULL ) {
601 #ifdef USE_REWRITE_LDAP_PVT_THREADS
602                                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
603 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
604                                 rc = REWRITE_ERR;
605                                 goto rc_return;
606                         }
607                         if ( strcasecmp( p, key->bv_val ) == 0 
608                                         && ( p = strtok( NULL, "" ) ) ) {
609                                 val->bv_val = strdup( p );
610                                 if ( val->bv_val == NULL ) {
611                                         return REWRITE_ERR;
612                                 }
613
614                                 val->bv_len = strlen( p );
615                                 
616 #ifdef USE_REWRITE_LDAP_PVT_THREADS
617                                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
618 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
619                                 
620                                 goto rc_return;
621                         }
622                 }
623
624 #ifdef USE_REWRITE_LDAP_PVT_THREADS
625                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
626 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
627
628                 rc = REWRITE_ERR;
629                 
630                 break;
631         }
632
633         case REWRITE_MAP_XLDAPMAP: {
634                 LDAP *ld;
635                 char filter[1024];
636                 LDAPMessage *res = NULL, *entry;
637                 LDAPURLDesc *lud = ( LDAPURLDesc * )map->lm_args;
638                 int attrsonly = 0;
639                 char **values;
640
641                 assert( lud != NULL );
642
643                 /*
644                  * No mutex because there is no write on the map data
645                  */
646                 
647                 ld = ldap_init( lud->lud_host, lud->lud_port );
648                 if ( ld == NULL ) {
649                         rc = REWRITE_ERR;
650                         goto rc_return;
651                 }
652
653                 snprintf( filter, sizeof( filter ), lud->lud_filter,
654                                 key->bv_val );
655
656                 if ( strcasecmp( lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
657                         attrsonly = 1;
658                 }
659                 rc = ldap_search_s( ld, lud->lud_dn, lud->lud_scope,
660                                 filter, lud->lud_attrs, attrsonly, &res );
661                 if ( rc != LDAP_SUCCESS ) {
662                         ldap_unbind( ld );
663                         rc = REWRITE_ERR;
664                         goto rc_return;
665                 }
666
667                 if ( ldap_count_entries( ld, res ) != 1 ) {
668                         ldap_unbind( ld );
669                         rc = REWRITE_ERR;
670                         goto rc_return;
671                 }
672
673                 entry = ldap_first_entry( ld, res );
674                 if ( entry == NULL ) {
675                         ldap_msgfree( res );
676                         ldap_unbind( ld );
677                         rc = REWRITE_ERR;
678                         goto rc_return;
679                 }
680                 if ( attrsonly == 1 ) {
681                         val->bv_val = ldap_get_dn( ld, entry );
682                         if ( val->bv_val == NULL ) {
683                                 ldap_msgfree( res );
684                                 ldap_unbind( ld );
685                                 rc = REWRITE_ERR;
686                                 goto rc_return;
687                         }
688                 } else {
689                         values = ldap_get_values( ld, entry,
690                                         lud->lud_attrs[0] );
691                         if ( values == NULL ) {
692                                 ldap_msgfree( res );
693                                 ldap_unbind( ld );
694                                 rc = REWRITE_ERR;
695                                 goto rc_return;
696                         }
697                         val->bv_val = strdup( values[ 0 ] );
698                         ldap_value_free( values );
699                 }
700                 val->bv_len = strlen( val->bv_val );
701
702                 ldap_msgfree( res );
703                 ldap_unbind( ld );
704                 
705                 rc = REWRITE_SUCCESS;
706         }
707         }
708
709 rc_return:;
710         return rc;
711 }
712
713 /*
714  * Applies the new map type
715  */
716 int
717 rewrite_map_apply(
718                 struct rewrite_info *info,
719                 struct rewrite_op *op,
720                 struct rewrite_map *map,
721                 struct berval *key,
722                 struct berval *val
723 )
724 {
725         int rc = REWRITE_SUCCESS;
726
727         assert( info != NULL );
728         assert( op != NULL );
729         assert( map != NULL );
730         assert( key != NULL );
731         assert( val != NULL );
732
733         val->bv_val = NULL;
734         val->bv_len = 0;
735         
736         switch ( map->lm_type ) {
737         case REWRITE_MAP_SUBCONTEXT:
738                 rc = rewrite_context_apply( info, op, 
739                                 ( struct rewrite_context * )map->lm_data,
740                                 key->bv_val, &val->bv_val );
741                 if ( val->bv_val != NULL ) {
742                         val->bv_len = strlen( val->bv_val );
743                 }
744                 break;
745
746         case REWRITE_MAP_SET_OP_VAR:
747         case REWRITE_MAP_SETW_OP_VAR:
748                 rc = rewrite_var_set( &op->lo_vars, map->lm_name,
749                                 key->bv_val, 1 )
750                         ? REWRITE_SUCCESS : REWRITE_ERR;
751                 if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
752                         val->bv_val = strdup( "" );
753                 } else {
754                         val->bv_val = strdup( key->bv_val );
755                         val->bv_len = key->bv_len;
756                 }
757                 break;
758         
759         case REWRITE_MAP_GET_OP_VAR: {
760                 struct rewrite_var *var;
761
762                 var = rewrite_var_find( op->lo_vars, map->lm_name );
763                 if ( var == NULL ) {
764                         rc = REWRITE_ERR;
765                 } else {
766                         val->bv_val = strdup( var->lv_value.bv_val );
767                         val->bv_len = var->lv_value.bv_len;
768                 }
769                 break;  
770         }
771
772         case REWRITE_MAP_SET_SESN_VAR:
773         case REWRITE_MAP_SETW_SESN_VAR:
774                 if ( op->lo_cookie == NULL ) {
775                         rc = REWRITE_ERR;
776                         break;
777                 }
778                 rc = rewrite_session_var_set( info, op->lo_cookie, 
779                                 map->lm_name, key->bv_val );
780                 if ( map->lm_type == REWRITE_MAP_SET_SESN_VAR ) {
781                         val->bv_val = strdup( "" );
782                 } else {
783                         val->bv_val = strdup( key->bv_val );
784                         val->bv_len = key->bv_len;
785                 }
786                 break;
787
788         case REWRITE_MAP_GET_SESN_VAR:
789                 rc = rewrite_session_var_get( info, op->lo_cookie,
790                                 map->lm_name, val );
791                 break;          
792
793         case REWRITE_MAP_GET_PARAM:
794                 rc = rewrite_param_get( info, map->lm_name, val );
795                 break;
796
797         case REWRITE_MAP_BUILTIN: {
798                 struct rewrite_builtin_map *bmap = map->lm_data;
799                 switch ( bmap->lb_type ) {
800                 case REWRITE_BUILTIN_MAP_LDAP:
801                         rc = map_ldap_apply( bmap, key->bv_val, val );
802                         break;
803                 default:
804                         rc = REWRITE_ERR;
805                         break;
806                 }
807                 break;
808         }
809
810         default:
811                 rc = REWRITE_ERR;
812                 break;
813         }
814
815         return rc;
816 }
817