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 ******************************************************************************/
29 #include "rewrite-int.h"
30 #include "rewrite-map.h"
35 #ifdef USE_REWRITE_LDAP_PVT_THREADS
36 ldap_pvt_thread_mutex_t xpasswd_mutex;
37 static int xpasswd_mutex_init = 0;
38 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
42 * NOTE: these are old-fashion maps; new maps will be parsed on separate
43 * config lines, and referred by name.
47 struct rewrite_info *info,
52 struct rewrite_map *map;
54 assert( info != NULL );
56 assert( currpos != NULL );
58 Debug( LDAP_DEBUG_ARGS, "rewrite_xmap_parse: %s\n%s%s",
63 map = calloc( sizeof( struct rewrite_map ), 1 );
65 Debug( LDAP_DEBUG_ANY, "rewrite_xmap_parse:"
66 " calloc failed\n%s%s%s", "", "", "" );
71 * Experimental passwd map:
72 * replaces the uid with the matching gecos from /etc/passwd file
74 if ( strncasecmp(s, "xpasswd", 7 ) == 0 ) {
75 map->lm_type = REWRITE_MAP_XPWDMAP;
76 map->lm_name = strdup( "xpasswd" );
78 assert( s[7] == '}' );
81 #ifdef USE_REWRITE_LDAP_PVT_THREADS
82 if ( !xpasswd_mutex_init ) {
83 xpasswd_mutex_init = 1;
84 if ( ldap_pvt_thread_mutex_init( &xpasswd_mutex ) ) {
89 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
91 /* Don't really care if fails */
95 * Experimental file map:
96 * looks up key in a `key value' ascii file
98 } else if ( strncasecmp(s, "xfile", 5 ) == 0 ) {
104 map->lm_type = REWRITE_MAP_XFILEMAP;
106 if ( s[ c ] != '(' ) {
111 /* Must start with '/' for security concerns */
113 if ( s[ c ] != '/' ) {
118 for ( p = s + c; p[ 0 ] != '\0' && p[ 0 ] != ')'; p++ );
119 if ( p[ 0 ] != ')' ) {
125 filename = calloc( sizeof( char ), l + 1 );
126 strncpy( filename, s + c, l );
127 filename[ l ] = '\0';
129 map->lm_args = ( void * )fopen( filename, "r" );
132 if ( map->lm_args == NULL ) {
139 #ifdef USE_REWRITE_LDAP_PVT_THREADS
140 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
141 fclose( ( FILE * )map->lm_args );
145 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
150 * Experimental ldap map:
151 * looks up key on the fly (not implemented!)
153 } else if ( strncasecmp(s, "xldap", 5 ) == 0 ) {
160 if ( s[ c ] != '(' ) {
166 p = strchr( s, '}' );
176 * Add two bytes for urlencoding of '%s'
179 url = calloc( sizeof( char ), l + 3 );
180 strncpy( url, s + c, l );
184 * Urlencodes the '%s' for ldap_url_parse
186 p = strchr( url, '%' );
188 memmove( p + 3, p + 1, strlen( p + 1 ) + 1 );
193 rc = ldap_url_parse( url, &lud );
196 if ( rc != LDAP_SUCCESS ) {
200 assert( lud != NULL );
202 map->lm_args = ( void * )lud;
203 map->lm_type = REWRITE_MAP_XLDAPMAP;
205 #ifdef USE_REWRITE_LDAP_PVT_THREADS
206 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
207 ldap_free_urldesc( lud );
211 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
223 struct rewrite_info *info,
228 struct rewrite_map *map = NULL;
229 struct rewrite_subst *subst = NULL;
230 char *s, *begin = NULL, *end;
234 assert( info != NULL );
235 assert( string != NULL );
236 assert( currpos != NULL );
241 * Go to the end of the map invocation (the right closing brace)
243 for ( p = string, cnt = 1; p[ 0 ] != '\0' && cnt > 0; p++ ) {
244 if ( p[ 0 ] == REWRITE_SUBMATCH_ESCAPE ) {
246 * '\' marks the beginning of a new map
248 if ( p[ 1 ] == '{' ) {
251 * '\' followed by a digit may mark the beginning
254 } else if ( isdigit( p[ 1 ] ) && p[ 2 ] == '{' ) {
259 } else if ( p[ 0 ] == '}' ) {
269 * Copy the map invocation
272 s = calloc( sizeof( char ), l + 1 );
273 strncpy( s, string, l );
277 * Isolate the map name (except for variable deref)
280 case REWRITE_OPERATOR_VARIABLE_GET:
281 case REWRITE_OPERATOR_PARAM_GET:
284 begin = strchr( s, '(' );
285 if ( begin == NULL ) {
295 * Check for special map types
299 case REWRITE_OPERATOR_SUBCONTEXT:
300 case REWRITE_OPERATOR_COMMAND:
301 case REWRITE_OPERATOR_VARIABLE_SET:
302 case REWRITE_OPERATOR_VARIABLE_GET:
303 case REWRITE_OPERATOR_PARAM_GET:
309 * Variable set and get may be repeated to indicate session-wide
310 * instead of operation-wide variables
313 case REWRITE_OPERATOR_VARIABLE_SET:
314 case REWRITE_OPERATOR_VARIABLE_GET:
320 * Variable get token can be appended to variable set to mean store
323 if ( p[ 0 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
328 * Check the syntax of the variable name
330 if ( !isalpha( p[ 0 ] ) ) {
334 for ( p++; p[ 0 ] != '\0'; p++ ) {
335 if ( !isalnum( p[ 0 ] ) ) {
342 * Isolate the argument of the map (except for variable deref)
345 case REWRITE_OPERATOR_VARIABLE_GET:
346 case REWRITE_OPERATOR_PARAM_GET:
349 end = strrchr( begin, ')' );
357 * Compile the substitution pattern of the map argument
359 subst = rewrite_subst_compile( info, begin );
360 if ( subst == NULL ) {
370 map = calloc( sizeof( struct rewrite_map ), 1 );
372 if ( subst != NULL ) {
379 #ifdef USE_REWRITE_LDAP_PVT_THREADS
380 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
381 if ( subst != NULL ) {
388 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
391 * No subst for variable deref
394 case REWRITE_OPERATOR_VARIABLE_GET:
395 case REWRITE_OPERATOR_PARAM_GET:
398 map->lm_subst = subst;
403 * Parses special map types
410 case REWRITE_OPERATOR_SUBCONTEXT: /* '>' */
413 * Fetch the rewrite context
414 * it MUST have been defined previously
416 map->lm_type = REWRITE_MAP_SUBCONTEXT;
417 map->lm_name = strdup( s + 1 );
418 map->lm_data = rewrite_context_find( info, s + 1 );
419 if ( map->lm_data == NULL ) {
429 case REWRITE_OPERATOR_COMMAND: /* '|' */
437 case REWRITE_OPERATOR_VARIABLE_SET: /* '&' */
438 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_SET ) {
439 if ( s[ 2 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
440 map->lm_type = REWRITE_MAP_SETW_SESN_VAR;
441 map->lm_name = strdup( s + 3 );
443 map->lm_type = REWRITE_MAP_SET_SESN_VAR;
444 map->lm_name = strdup( s + 2 );
447 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
448 map->lm_type = REWRITE_MAP_SETW_OP_VAR;
449 map->lm_name = strdup( s + 2 );
451 map->lm_type = REWRITE_MAP_SET_OP_VAR;
452 map->lm_name = strdup( s + 1 );
458 * Variable dereference
460 case REWRITE_OPERATOR_VARIABLE_GET: /* '*' */
461 if ( s[ 1 ] == REWRITE_OPERATOR_VARIABLE_GET ) {
462 map->lm_type = REWRITE_MAP_GET_SESN_VAR;
463 map->lm_name = strdup( s + 2 );
465 map->lm_type = REWRITE_MAP_GET_OP_VAR;
466 map->lm_name = strdup( s + 1 );
473 case REWRITE_OPERATOR_PARAM_GET: /* '$' */
474 map->lm_type = REWRITE_MAP_GET_PARAM;
475 map->lm_name = strdup( s + 1 );
482 map->lm_type = REWRITE_MAP_BUILTIN;
483 map->lm_name = strdup( s );
484 map->lm_data = rewrite_builtin_map_find( info, s );
485 if ( map->lm_data == NULL ) {
497 * Map key -> value resolution
498 * NOTE: these are old-fashion maps; new maps will be parsed on separate
499 * config lines, and referred by name.
503 struct rewrite_info *info,
504 struct rewrite_op *op,
505 struct rewrite_map *map,
510 int rc = REWRITE_SUCCESS;
512 assert( info != NULL );
513 assert( op != NULL );
514 assert( map != NULL );
515 assert( key != NULL );
516 assert( val != NULL );
521 switch ( map->lm_type ) {
522 case REWRITE_MAP_XPWDMAP: {
525 #ifdef USE_REWRITE_LDAP_PVT_THREADS
526 ldap_pvt_thread_mutex_lock( &xpasswd_mutex );
527 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
529 pwd = getpwnam( key->bv_val );
532 #ifdef USE_REWRITE_LDAP_PVT_THREADS
533 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
534 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
536 rc = REWRITE_NO_SUCH_OBJECT;
540 if ( pwd->pw_gecos != NULL && pwd->pw_gecos[0] != '\0' ) {
541 int l = strlen( pwd->pw_gecos );
543 val->bv_val = strdup( pwd->pw_gecos );
544 if ( val->bv_val == NULL ) {
546 #ifdef USE_REWRITE_LDAP_PVT_THREADS
547 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
548 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
555 val->bv_val = strdup( key->bv_val );
556 val->bv_len = key->bv_len;
559 #ifdef USE_REWRITE_LDAP_PVT_THREADS
560 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
561 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
566 case REWRITE_MAP_XFILEMAP: {
569 if ( map->lm_args == NULL ) {
574 #ifdef USE_REWRITE_LDAP_PVT_THREADS
575 ldap_pvt_thread_mutex_lock( &map->lm_mutex );
576 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
578 rewind( ( FILE * )map->lm_args );
580 while ( fgets( buf, sizeof( buf ), ( FILE * )map->lm_args ) ) {
584 blen = strlen( buf );
585 if ( buf[ blen - 1 ] == '\n' ) {
586 buf[ blen - 1 ] = '\0';
589 p = strtok( buf, " " );
591 #ifdef USE_REWRITE_LDAP_PVT_THREADS
592 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
593 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
597 if ( strcasecmp( p, key->bv_val ) == 0
598 && ( p = strtok( NULL, "" ) ) ) {
599 val->bv_val = strdup( p );
600 if ( val->bv_val == NULL ) {
604 val->bv_len = strlen( p );
606 #ifdef USE_REWRITE_LDAP_PVT_THREADS
607 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
608 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
614 #ifdef USE_REWRITE_LDAP_PVT_THREADS
615 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
616 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
623 case REWRITE_MAP_XLDAPMAP: {
625 char filter[ LDAP_FILT_MAXSIZ ];
626 LDAPMessage *res = NULL, *entry;
627 LDAPURLDesc *lud = ( LDAPURLDesc * )map->lm_args;
631 assert( lud != NULL );
634 * No mutex because there is no write on the map data
637 ld = ldap_init( lud->lud_host, lud->lud_port );
643 snprintf( filter, sizeof( filter ), lud->lud_filter,
646 if ( strcasecmp( lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
649 rc = ldap_search_s( ld, lud->lud_dn, lud->lud_scope,
650 filter, lud->lud_attrs, attrsonly, &res );
651 if ( rc != LDAP_SUCCESS ) {
657 if ( ldap_count_entries( ld, res ) != 1 ) {
663 entry = ldap_first_entry( ld, res );
664 if ( entry == NULL ) {
670 if ( attrsonly == 1 ) {
671 val->bv_val = ldap_get_dn( ld, entry );
672 if ( val->bv_val == NULL ) {
679 values = ldap_get_values( ld, entry,
681 if ( values == NULL ) {
687 val->bv_val = strdup( values[ 0 ] );
688 ldap_value_free( values );
690 val->bv_len = strlen( val->bv_val );
695 rc = REWRITE_SUCCESS;
704 * Applies the new map type
708 struct rewrite_info *info,
709 struct rewrite_op *op,
710 struct rewrite_map *map,
715 int rc = REWRITE_SUCCESS;
717 assert( info != NULL );
718 assert( op != NULL );
719 assert( map != NULL );
720 assert( key != NULL );
721 assert( val != NULL );
726 switch ( map->lm_type ) {
727 case REWRITE_MAP_SUBCONTEXT:
728 rc = rewrite_context_apply( info, op,
729 ( struct rewrite_context * )map->lm_data,
730 key->bv_val, &val->bv_val );
731 if ( val->bv_val != NULL ) {
732 val->bv_len = strlen( val->bv_val );
736 case REWRITE_MAP_SET_OP_VAR:
737 case REWRITE_MAP_SETW_OP_VAR:
738 rc = rewrite_var_set( &op->lo_vars, map->lm_name,
740 ? REWRITE_SUCCESS : REWRITE_ERR;
741 if ( map->lm_type == REWRITE_MAP_SET_OP_VAR ) {
742 val->bv_val = strdup( "" );
744 val->bv_val = strdup( key->bv_val );
745 val->bv_len = key->bv_len;
749 case REWRITE_MAP_GET_OP_VAR: {
750 struct rewrite_var *var;
752 var = rewrite_var_find( op->lo_vars, map->lm_name );
756 val->bv_val = strdup( var->lv_value.bv_val );
757 val->bv_len = var->lv_value.bv_len;
762 case REWRITE_MAP_SET_SESN_VAR:
763 case REWRITE_MAP_SETW_SESN_VAR:
764 if ( op->lo_cookie == NULL ) {
768 rc = rewrite_session_var_set( info, op->lo_cookie,
769 map->lm_name, key->bv_val );
770 if ( map->lm_type == REWRITE_MAP_SET_SESN_VAR ) {
771 val->bv_val = strdup( "" );
773 val->bv_val = strdup( key->bv_val );
774 val->bv_len = key->bv_len;
778 case REWRITE_MAP_GET_SESN_VAR:
779 rc = rewrite_session_var_get( info, op->lo_cookie,
783 case REWRITE_MAP_GET_PARAM:
784 rc = rewrite_param_get( info, map->lm_name, val );
787 case REWRITE_MAP_BUILTIN: {
788 struct rewrite_builtin_map *bmap = map->lm_data;
789 switch ( bmap->lb_type ) {
790 case REWRITE_BUILTIN_MAP_LDAP:
791 rc = map_ldap_apply( bmap, key->bv_val, val );