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 ******************************************************************************/
31 #include "rewrite-int.h"
32 #include "rewrite-map.h"
37 #ifdef USE_REWRITE_LDAP_PVT_THREADS
38 ldap_pvt_thread_mutex_t xpasswd_mutex;
39 static int xpasswd_mutex_init = 0;
40 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
44 * NOTE: these are old-fashion maps; new maps will be parsed on separate
45 * config lines, and referred by name.
49 struct rewrite_info *info,
54 struct rewrite_map *map;
56 assert( info != NULL );
58 assert( currpos != NULL );
60 Debug( LDAP_DEBUG_ARGS, "rewrite_xmap_parse: %s\n%s%s",
65 map = calloc( sizeof( struct rewrite_map ), 1 );
67 Debug( LDAP_DEBUG_ANY, "rewrite_xmap_parse:"
68 " calloc failed\n%s%s%s", "", "", "" );
73 * Experimental passwd map:
74 * replaces the uid with the matching gecos from /etc/passwd file
76 if ( strncasecmp(s, "xpasswd", 7 ) == 0 ) {
77 map->lm_type = REWRITE_MAP_XPWDMAP;
78 map->lm_name = strdup( "xpasswd" );
80 assert( s[7] == '}' );
83 #ifdef USE_REWRITE_LDAP_PVT_THREADS
84 if ( !xpasswd_mutex_init ) {
85 xpasswd_mutex_init = 1;
86 if ( ldap_pvt_thread_mutex_init( &xpasswd_mutex ) ) {
91 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
93 /* Don't really care if fails */
97 * Experimental file map:
98 * looks up key in a `key value' ascii file
100 } else if ( strncasecmp(s, "xfile", 5 ) == 0 ) {
106 map->lm_type = REWRITE_MAP_XFILEMAP;
108 if ( s[ c ] != '(' ) {
113 /* Must start with '/' for security concerns */
115 if ( s[ c ] != '/' ) {
120 for ( p = s + c; p[ 0 ] != '\0' && p[ 0 ] != ')'; p++ );
121 if ( p[ 0 ] != ')' ) {
127 filename = calloc( sizeof( char ), l + 1 );
128 AC_MEMCPY( filename, s + c, l );
129 filename[ l ] = '\0';
131 map->lm_args = ( void * )fopen( filename, "r" );
134 if ( map->lm_args == NULL ) {
141 #ifdef USE_REWRITE_LDAP_PVT_THREADS
142 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
143 fclose( ( FILE * )map->lm_args );
147 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
152 * Experimental ldap map:
153 * looks up key on the fly (not implemented!)
155 } else if ( strncasecmp(s, "xldap", 5 ) == 0 ) {
162 if ( s[ c ] != '(' ) {
168 p = strchr( s, '}' );
178 * Add two bytes for urlencoding of '%s'
181 url = calloc( sizeof( char ), l + 3 );
182 AC_MEMCPY( url, s + c, l );
186 * Urlencodes the '%s' for ldap_url_parse
188 p = strchr( url, '%' );
190 AC_MEMCPY( p + 3, p + 1, strlen( p + 1 ) + 1 );
195 rc = ldap_url_parse( url, &lud );
198 if ( rc != LDAP_SUCCESS ) {
202 assert( lud != NULL );
204 map->lm_args = ( void * )lud;
205 map->lm_type = REWRITE_MAP_XLDAPMAP;
207 #ifdef USE_REWRITE_LDAP_PVT_THREADS
208 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
209 ldap_free_urldesc( lud );
213 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
225 struct rewrite_info *info,
230 struct rewrite_map *map = NULL;
231 struct rewrite_subst *subst = NULL;
232 char *s, *begin = NULL, *end;
236 assert( info != NULL );
237 assert( string != NULL );
238 assert( currpos != NULL );
243 * Go to the end of the map invocation (the right closing brace)
245 for ( p = string, cnt = 1; p[ 0 ] != '\0' && cnt > 0; p++ ) {
246 if ( p[ 0 ] == REWRITE_SUBMATCH_ESCAPE ) {
248 * '\' marks the beginning of a new map
250 if ( p[ 1 ] == '{' ) {
253 * '\' followed by a digit may mark the beginning
256 } else if ( isdigit( (unsigned char) p[ 1 ] ) && p[ 2 ] == '{' ) {
261 } else if ( p[ 0 ] == '}' ) {
271 * Copy the map invocation
274 s = calloc( sizeof( char ), l + 1 );
275 AC_MEMCPY( s, string, l );
279 * Isolate the map name (except for variable deref)
282 case REWRITE_OPERATOR_VARIABLE_GET:
283 case REWRITE_OPERATOR_PARAM_GET:
286 begin = strchr( s, '(' );
287 if ( begin == NULL ) {
297 * Check for special map types
301 case REWRITE_OPERATOR_SUBCONTEXT:
302 case REWRITE_OPERATOR_COMMAND:
303 case REWRITE_OPERATOR_VARIABLE_SET:
304 case REWRITE_OPERATOR_VARIABLE_GET:
305 case REWRITE_OPERATOR_PARAM_GET:
311 * Variable set and get may be repeated to indicate session-wide
312 * instead of operation-wide variables
315 case REWRITE_OPERATOR_VARIABLE_SET:
316 case REWRITE_OPERATOR_VARIABLE_GET:
322 * Variable get token can be appended to variable set to mean store
325 if ( p[ 0 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
330 * Check the syntax of the variable name
332 if ( !isalpha( (unsigned char) p[ 0 ] ) ) {
336 for ( p++; p[ 0 ] != '\0'; p++ ) {
337 if ( !isalnum( (unsigned char) p[ 0 ] ) ) {
344 * Isolate the argument of the map (except for variable deref)
347 case REWRITE_OPERATOR_VARIABLE_GET:
348 case REWRITE_OPERATOR_PARAM_GET:
351 end = strrchr( begin, ')' );
359 * Compile the substitution pattern of the map argument
361 subst = rewrite_subst_compile( info, begin );
362 if ( subst == NULL ) {
372 map = calloc( sizeof( struct rewrite_map ), 1 );
374 if ( subst != NULL ) {
381 #ifdef USE_REWRITE_LDAP_PVT_THREADS
382 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
383 if ( subst != NULL ) {
390 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
393 * No subst for variable deref
396 case REWRITE_OPERATOR_VARIABLE_GET:
397 case REWRITE_OPERATOR_PARAM_GET:
400 map->lm_subst = subst;
405 * Parses special map types
412 case REWRITE_OPERATOR_SUBCONTEXT: /* '>' */
415 * Fetch the rewrite context
416 * it MUST have been defined previously
418 map->lm_type = REWRITE_MAP_SUBCONTEXT;
419 map->lm_name = strdup( s + 1 );
420 map->lm_data = rewrite_context_find( info, s + 1 );
421 if ( map->lm_data == NULL ) {
431 case REWRITE_OPERATOR_COMMAND: /* '|' */
439 case REWRITE_OPERATOR_VARIABLE_SET: /* '&' */
440 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_SET ) {
441 if ( s[ 2 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
442 map->lm_type = REWRITE_MAP_SETW_SESN_VAR;
443 map->lm_name = strdup( s + 3 );
445 map->lm_type = REWRITE_MAP_SET_SESN_VAR;
446 map->lm_name = strdup( s + 2 );
449 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
450 map->lm_type = REWRITE_MAP_SETW_OP_VAR;
451 map->lm_name = strdup( s + 2 );
453 map->lm_type = REWRITE_MAP_SET_OP_VAR;
454 map->lm_name = strdup( s + 1 );
460 * Variable dereference
462 case REWRITE_OPERATOR_VARIABLE_GET: /* '*' */
463 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
464 map->lm_type = REWRITE_MAP_GET_SESN_VAR;
465 map->lm_name = strdup( s + 2 );
467 map->lm_type = REWRITE_MAP_GET_OP_VAR;
468 map->lm_name = strdup( s + 1 );
475 case REWRITE_OPERATOR_PARAM_GET: /* '$' */
476 map->lm_type = REWRITE_MAP_GET_PARAM;
477 map->lm_name = strdup( s + 1 );
484 map->lm_type = REWRITE_MAP_BUILTIN;
485 map->lm_name = strdup( s );
486 map->lm_data = rewrite_builtin_map_find( info, s );
487 if ( map->lm_data == NULL ) {
499 * Map key -> value resolution
500 * NOTE: these are old-fashion maps; new maps will be parsed on separate
501 * config lines, and referred by name.
505 struct rewrite_info *info,
506 struct rewrite_op *op,
507 struct rewrite_map *map,
512 int rc = REWRITE_SUCCESS;
514 assert( info != NULL );
515 assert( op != NULL );
516 assert( map != NULL );
517 assert( key != NULL );
518 assert( val != NULL );
523 switch ( map->lm_type ) {
525 case REWRITE_MAP_XPWDMAP: {
528 #ifdef USE_REWRITE_LDAP_PVT_THREADS
529 ldap_pvt_thread_mutex_lock( &xpasswd_mutex );
530 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
532 pwd = getpwnam( key->bv_val );
535 #ifdef USE_REWRITE_LDAP_PVT_THREADS
536 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
537 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
539 rc = REWRITE_NO_SUCH_OBJECT;
543 if ( pwd->pw_gecos != NULL && pwd->pw_gecos[0] != '\0' ) {
544 int l = strlen( pwd->pw_gecos );
546 val->bv_val = strdup( pwd->pw_gecos );
547 if ( val->bv_val == NULL ) {
549 #ifdef USE_REWRITE_LDAP_PVT_THREADS
550 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
551 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
558 val->bv_val = strdup( key->bv_val );
559 val->bv_len = key->bv_len;
562 #ifdef USE_REWRITE_LDAP_PVT_THREADS
563 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
564 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
568 #endif /* HAVE_GETPWNAM*/
570 case REWRITE_MAP_XFILEMAP: {
573 if ( map->lm_args == NULL ) {
578 #ifdef USE_REWRITE_LDAP_PVT_THREADS
579 ldap_pvt_thread_mutex_lock( &map->lm_mutex );
580 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
582 rewind( ( FILE * )map->lm_args );
584 while ( fgets( buf, sizeof( buf ), ( FILE * )map->lm_args ) ) {
588 blen = strlen( buf );
589 if ( buf[ blen - 1 ] == '\n' ) {
590 buf[ blen - 1 ] = '\0';
593 p = strtok( buf, " " );
595 #ifdef USE_REWRITE_LDAP_PVT_THREADS
596 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
597 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
601 if ( strcasecmp( p, key->bv_val ) == 0
602 && ( p = strtok( NULL, "" ) ) ) {
603 val->bv_val = strdup( p );
604 if ( val->bv_val == NULL ) {
608 val->bv_len = strlen( p );
610 #ifdef USE_REWRITE_LDAP_PVT_THREADS
611 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
612 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
618 #ifdef USE_REWRITE_LDAP_PVT_THREADS
619 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
620 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
627 case REWRITE_MAP_XLDAPMAP: {
629 char filter[ LDAP_FILT_MAXSIZ ];
630 LDAPMessage *res = NULL, *entry;
631 LDAPURLDesc *lud = ( LDAPURLDesc * )map->lm_args;
635 assert( lud != NULL );
638 * No mutex because there is no write on the map data
641 ld = ldap_init( lud->lud_host, lud->lud_port );
647 snprintf( filter, sizeof( filter ), lud->lud_filter,
650 if ( strcasecmp( lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
653 rc = ldap_search_s( ld, lud->lud_dn, lud->lud_scope,
654 filter, lud->lud_attrs, attrsonly, &res );
655 if ( rc != LDAP_SUCCESS ) {
661 if ( ldap_count_entries( ld, res ) != 1 ) {
667 entry = ldap_first_entry( ld, res );
668 if ( entry == NULL ) {
674 if ( attrsonly == 1 ) {
675 val->bv_val = ldap_get_dn( ld, entry );
676 if ( val->bv_val == NULL ) {
683 values = ldap_get_values( ld, entry,
685 if ( values == NULL ) {
691 val->bv_val = strdup( values[ 0 ] );
692 ldap_value_free( values );
694 val->bv_len = strlen( val->bv_val );
699 rc = REWRITE_SUCCESS;
708 * Applies the new map type
712 struct rewrite_info *info,
713 struct rewrite_op *op,
714 struct rewrite_map *map,
719 int rc = REWRITE_SUCCESS;
721 assert( info != NULL );
722 assert( op != NULL );
723 assert( map != NULL );
724 assert( key != NULL );
725 assert( val != NULL );
730 switch ( map->lm_type ) {
731 case REWRITE_MAP_SUBCONTEXT:
732 rc = rewrite_context_apply( info, op,
733 ( struct rewrite_context * )map->lm_data,
734 key->bv_val, &val->bv_val );
735 if ( val->bv_val != NULL ) {
736 val->bv_len = strlen( val->bv_val );
740 case REWRITE_MAP_SET_OP_VAR:
741 case REWRITE_MAP_SETW_OP_VAR:
742 rc = rewrite_var_set( &op->lo_vars, map->lm_name,
744 ? REWRITE_SUCCESS : REWRITE_ERR;
745 if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
746 val->bv_val = strdup( "" );
748 val->bv_val = strdup( key->bv_val );
749 val->bv_len = key->bv_len;
753 case REWRITE_MAP_GET_OP_VAR: {
754 struct rewrite_var *var;
756 var = rewrite_var_find( op->lo_vars, map->lm_name );
760 val->bv_val = strdup( var->lv_value.bv_val );
761 val->bv_len = var->lv_value.bv_len;
766 case REWRITE_MAP_SET_SESN_VAR:
767 case REWRITE_MAP_SETW_SESN_VAR:
768 if ( op->lo_cookie == NULL ) {
772 rc = rewrite_session_var_set( info, op->lo_cookie,
773 map->lm_name, key->bv_val );
774 if ( map->lm_type == REWRITE_MAP_SET_SESN_VAR ) {
775 val->bv_val = strdup( "" );
777 val->bv_val = strdup( key->bv_val );
778 val->bv_len = key->bv_len;
782 case REWRITE_MAP_GET_SESN_VAR:
783 rc = rewrite_session_var_get( info, op->lo_cookie,
787 case REWRITE_MAP_GET_PARAM:
788 rc = rewrite_param_get( info, map->lm_name, val );
791 case REWRITE_MAP_BUILTIN: {
792 struct rewrite_builtin_map *bmap = map->lm_data;
793 switch ( bmap->lb_type ) {
794 case REWRITE_BUILTIN_MAP_LDAP:
795 rc = map_ldap_apply( bmap, key->bv_val, val );