1 /******************************************************************************
3 * Copyright (C) 2000 Pierangelo Masarati, <ando@sys-net.it>
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:
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.
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.
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.
21 * 4. This notice may not be removed or altered.
23 ******************************************************************************/
33 #include "rewrite-int.h"
34 #include "rewrite-map.h"
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 */
46 * NOTE: these are old-fashion maps; new maps will be parsed on separate
47 * config lines, and referred by name.
51 struct rewrite_info *info,
56 struct rewrite_map *map;
58 assert( info != NULL );
60 assert( currpos != NULL );
62 Debug( LDAP_DEBUG_ARGS, "rewrite_xmap_parse: %s\n%s%s",
67 map = calloc( sizeof( struct rewrite_map ), 1 );
69 Debug( LDAP_DEBUG_ANY, "rewrite_xmap_parse:"
70 " calloc failed\n%s%s%s", "", "", "" );
75 * Experimental passwd map:
76 * replaces the uid with the matching gecos from /etc/passwd file
78 if ( strncasecmp(s, "xpasswd", 7 ) == 0 ) {
79 map->lm_type = REWRITE_MAP_XPWDMAP;
80 map->lm_name = strdup( "xpasswd" );
82 assert( s[7] == '}' );
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 ) ) {
93 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
95 /* Don't really care if fails */
99 * Experimental file map:
100 * looks up key in a `key value' ascii file
102 } else if ( strncasecmp(s, "xfile", 5 ) == 0 ) {
108 map->lm_type = REWRITE_MAP_XFILEMAP;
110 if ( s[ c ] != '(' ) {
115 /* Must start with '/' for security concerns */
117 if ( s[ c ] != '/' ) {
122 for ( p = s + c; p[ 0 ] != '\0' && p[ 0 ] != ')'; p++ );
123 if ( p[ 0 ] != ')' ) {
129 filename = calloc( sizeof( char ), l + 1 );
130 AC_MEMCPY( filename, s + c, l );
131 filename[ l ] = '\0';
133 map->lm_args = ( void * )fopen( filename, "r" );
136 if ( map->lm_args == NULL ) {
143 #ifdef USE_REWRITE_LDAP_PVT_THREADS
144 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
145 fclose( ( FILE * )map->lm_args );
149 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
154 * Experimental ldap map:
155 * looks up key on the fly (not implemented!)
157 } else if ( strncasecmp(s, "xldap", 5 ) == 0 ) {
164 if ( s[ c ] != '(' ) {
170 p = strchr( s, '}' );
180 * Add two bytes for urlencoding of '%s'
183 url = calloc( sizeof( char ), l + 3 );
184 AC_MEMCPY( url, s + c, l );
188 * Urlencodes the '%s' for ldap_url_parse
190 p = strchr( url, '%' );
192 AC_MEMCPY( p + 3, p + 1, strlen( p + 1 ) + 1 );
197 rc = ldap_url_parse( url, &lud );
200 if ( rc != LDAP_SUCCESS ) {
204 assert( lud != NULL );
206 map->lm_args = ( void * )lud;
207 map->lm_type = REWRITE_MAP_XLDAPMAP;
209 #ifdef USE_REWRITE_LDAP_PVT_THREADS
210 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
211 ldap_free_urldesc( lud );
215 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
227 struct rewrite_info *info,
232 struct rewrite_map *map = NULL;
233 struct rewrite_subst *subst = NULL;
234 char *s, *begin = NULL, *end;
238 assert( info != NULL );
239 assert( string != NULL );
240 assert( currpos != NULL );
245 * Go to the end of the map invocation (the right closing brace)
247 for ( p = string, cnt = 1; p[ 0 ] != '\0' && cnt > 0; p++ ) {
248 if ( p[ 0 ] == REWRITE_SUBMATCH_ESCAPE ) {
250 * '%' marks the beginning of a new map
252 if ( p[ 1 ] == '{' ) {
255 * '%' followed by a digit may mark the beginning
258 } else if ( isdigit( (unsigned char) p[ 1 ] ) && p[ 2 ] == '{' ) {
262 if ( p[ 1 ] != '\0' )
264 } else if ( p[ 0 ] == '}' ) {
274 * Copy the map invocation
277 s = calloc( sizeof( char ), l + 1 );
278 AC_MEMCPY( s, string, l );
282 * Isolate the map name (except for variable deref)
285 case REWRITE_OPERATOR_VARIABLE_GET:
286 case REWRITE_OPERATOR_PARAM_GET:
289 begin = strchr( s, '(' );
290 if ( begin == NULL ) {
300 * Check for special map types
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:
314 * Variable set and get may be repeated to indicate session-wide
315 * instead of operation-wide variables
318 case REWRITE_OPERATOR_VARIABLE_SET:
319 case REWRITE_OPERATOR_VARIABLE_GET:
325 * Variable get token can be appended to variable set to mean store
328 if ( p[ 0 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
333 * Check the syntax of the variable name
335 if ( !isalpha( (unsigned char) p[ 0 ] ) ) {
339 for ( p++; p[ 0 ] != '\0'; p++ ) {
340 if ( !isalnum( (unsigned char) p[ 0 ] ) ) {
347 * Isolate the argument of the map (except for variable deref)
350 case REWRITE_OPERATOR_VARIABLE_GET:
351 case REWRITE_OPERATOR_PARAM_GET:
354 end = strrchr( begin, ')' );
362 * Compile the substitution pattern of the map argument
364 subst = rewrite_subst_compile( info, begin );
365 if ( subst == NULL ) {
375 map = calloc( sizeof( struct rewrite_map ), 1 );
377 if ( subst != NULL ) {
384 #ifdef USE_REWRITE_LDAP_PVT_THREADS
385 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
386 if ( subst != NULL ) {
393 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
396 * No subst for variable deref
399 case REWRITE_OPERATOR_VARIABLE_GET:
400 case REWRITE_OPERATOR_PARAM_GET:
403 map->lm_subst = subst;
408 * Parses special map types
415 case REWRITE_OPERATOR_SUBCONTEXT: /* '>' */
418 * Fetch the rewrite context
419 * it MUST have been defined previously
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 ) {
434 case REWRITE_OPERATOR_COMMAND: /* '|' */
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 );
448 map->lm_type = REWRITE_MAP_SET_SESN_VAR;
449 map->lm_name = strdup( s + 2 );
452 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
453 map->lm_type = REWRITE_MAP_SETW_OP_VAR;
454 map->lm_name = strdup( s + 2 );
456 map->lm_type = REWRITE_MAP_SET_OP_VAR;
457 map->lm_name = strdup( s + 1 );
463 * Variable dereference
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 );
470 map->lm_type = REWRITE_MAP_GET_OP_VAR;
471 map->lm_name = strdup( s + 1 );
478 case REWRITE_OPERATOR_PARAM_GET: /* '$' */
479 map->lm_type = REWRITE_MAP_GET_PARAM;
480 map->lm_name = strdup( s + 1 );
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 ) {
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.
508 struct rewrite_info *info,
509 struct rewrite_op *op,
510 struct rewrite_map *map,
515 int rc = REWRITE_SUCCESS;
517 assert( info != NULL );
518 assert( op != NULL );
519 assert( map != NULL );
520 assert( key != NULL );
521 assert( val != NULL );
526 switch ( map->lm_type ) {
528 case REWRITE_MAP_XPWDMAP: {
531 #ifdef USE_REWRITE_LDAP_PVT_THREADS
532 ldap_pvt_thread_mutex_lock( &xpasswd_mutex );
533 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
535 pwd = getpwnam( key->bv_val );
538 #ifdef USE_REWRITE_LDAP_PVT_THREADS
539 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
540 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
542 rc = REWRITE_NO_SUCH_OBJECT;
547 if ( pwd->pw_gecos != NULL && pwd->pw_gecos[0] != '\0' ) {
548 int l = strlen( pwd->pw_gecos );
550 val->bv_val = strdup( pwd->pw_gecos );
551 if ( val->bv_val == NULL ) {
553 #ifdef USE_REWRITE_LDAP_PVT_THREADS
554 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
555 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
562 #endif /* HAVE_PW_GECOS */
564 val->bv_val = strdup( key->bv_val );
565 val->bv_len = key->bv_len;
568 #ifdef USE_REWRITE_LDAP_PVT_THREADS
569 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
570 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
574 #endif /* HAVE_GETPWNAM*/
576 case REWRITE_MAP_XFILEMAP: {
579 if ( map->lm_args == NULL ) {
584 #ifdef USE_REWRITE_LDAP_PVT_THREADS
585 ldap_pvt_thread_mutex_lock( &map->lm_mutex );
586 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
588 rewind( ( FILE * )map->lm_args );
590 while ( fgets( buf, sizeof( buf ), ( FILE * )map->lm_args ) ) {
594 blen = strlen( buf );
595 if ( buf[ blen - 1 ] == '\n' ) {
596 buf[ blen - 1 ] = '\0';
599 p = strtok( buf, " " );
601 #ifdef USE_REWRITE_LDAP_PVT_THREADS
602 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
603 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
607 if ( strcasecmp( p, key->bv_val ) == 0
608 && ( p = strtok( NULL, "" ) ) ) {
609 val->bv_val = strdup( p );
610 if ( val->bv_val == NULL ) {
614 val->bv_len = strlen( p );
616 #ifdef USE_REWRITE_LDAP_PVT_THREADS
617 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
618 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
624 #ifdef USE_REWRITE_LDAP_PVT_THREADS
625 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
626 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
633 case REWRITE_MAP_XLDAPMAP: {
636 LDAPMessage *res = NULL, *entry;
637 LDAPURLDesc *lud = ( LDAPURLDesc * )map->lm_args;
641 assert( lud != NULL );
644 * No mutex because there is no write on the map data
647 ld = ldap_init( lud->lud_host, lud->lud_port );
653 snprintf( filter, sizeof( filter ), lud->lud_filter,
656 if ( strcasecmp( lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
659 rc = ldap_search_s( ld, lud->lud_dn, lud->lud_scope,
660 filter, lud->lud_attrs, attrsonly, &res );
661 if ( rc != LDAP_SUCCESS ) {
667 if ( ldap_count_entries( ld, res ) != 1 ) {
673 entry = ldap_first_entry( ld, res );
674 if ( entry == NULL ) {
680 if ( attrsonly == 1 ) {
681 val->bv_val = ldap_get_dn( ld, entry );
682 if ( val->bv_val == NULL ) {
689 values = ldap_get_values( ld, entry,
691 if ( values == NULL ) {
697 val->bv_val = strdup( values[ 0 ] );
698 ldap_value_free( values );
700 val->bv_len = strlen( val->bv_val );
705 rc = REWRITE_SUCCESS;
714 * Applies the new map type
718 struct rewrite_info *info,
719 struct rewrite_op *op,
720 struct rewrite_map *map,
725 int rc = REWRITE_SUCCESS;
727 assert( info != NULL );
728 assert( op != NULL );
729 assert( map != NULL );
730 assert( key != NULL );
731 assert( val != NULL );
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 );
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,
750 ? REWRITE_SUCCESS : REWRITE_ERR;
751 if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
752 val->bv_val = strdup( "" );
754 val->bv_val = strdup( key->bv_val );
755 val->bv_len = key->bv_len;
759 case REWRITE_MAP_GET_OP_VAR: {
760 struct rewrite_var *var;
762 var = rewrite_var_find( op->lo_vars, map->lm_name );
766 val->bv_val = strdup( var->lv_value.bv_val );
767 val->bv_len = var->lv_value.bv_len;
772 case REWRITE_MAP_SET_SESN_VAR:
773 case REWRITE_MAP_SETW_SESN_VAR:
774 if ( op->lo_cookie == NULL ) {
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( "" );
783 val->bv_val = strdup( key->bv_val );
784 val->bv_len = key->bv_len;
788 case REWRITE_MAP_GET_SESN_VAR:
789 rc = rewrite_session_var_get( info, op->lo_cookie,
793 case REWRITE_MAP_GET_PARAM:
794 rc = rewrite_param_get( info, map->lm_name, val );
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 );