]> git.sur5r.net Git - openldap/blob - libraries/librewrite/xmap.c
0448c8ee964a5bf3c901e72b6358df8cb3ac03fc
[openldap] / libraries / librewrite / xmap.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENT:
16  * This work was initially developed by Pierangelo Masarati for
17  * inclusion in OpenLDAP Software.
18  */
19
20 #include <portable.h>
21
22 #include <stdio.h>
23
24 #ifdef HAVE_PWD_H
25 #include <pwd.h>
26 #endif
27
28 #define LDAP_DEPRECATED 1
29 #include "rewrite-int.h"
30 #include "rewrite-map.h"
31
32 /*
33  * Global data
34  */
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 */
39
40 /*
41  * Map parsing
42  * NOTE: these are old-fashion maps; new maps will be parsed on separate
43  * config lines, and referred by name.
44  */
45 struct rewrite_map *
46 rewrite_xmap_parse(
47                 struct rewrite_info *info,
48                 const char *s,
49                 const char **currpos
50 )
51 {
52         struct rewrite_map *map;
53
54         assert( info != NULL );
55         assert( s != NULL );
56         assert( currpos != NULL );
57
58         Debug( LDAP_DEBUG_ARGS, "rewrite_xmap_parse: %s\n%s%s",
59                         s, "", "" );
60
61         *currpos = NULL;
62
63         map = calloc( sizeof( struct rewrite_map ), 1 );
64         if ( map == NULL ) {
65                 Debug( LDAP_DEBUG_ANY, "rewrite_xmap_parse:"
66                                 " calloc failed\n%s%s%s", "", "", "" );
67                 return NULL;
68         }
69
70         /*
71          * Experimental passwd map:
72          * replaces the uid with the matching gecos from /etc/passwd file 
73          */
74         if ( strncasecmp(s, "xpasswd", 7 ) == 0 ) {
75                 map->lm_type = REWRITE_MAP_XPWDMAP;
76                 map->lm_name = strdup( "xpasswd" );
77
78                 assert( s[7] == '}' );
79                 *currpos = s + 8;
80
81 #ifdef USE_REWRITE_LDAP_PVT_THREADS
82                 if ( !xpasswd_mutex_init ) {
83                         if ( ldap_pvt_thread_mutex_init( &xpasswd_mutex ) ) {
84                                 free( map );
85                                 return NULL;
86                         }
87                 }
88                 ++xpasswd_mutex_init;
89 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
90
91                 /* Don't really care if fails */
92                 return map;
93         
94         /*
95          * Experimental file map:
96          * looks up key in a `key value' ascii file
97          */
98         } else if ( strncasecmp( s, "xfile", 5 ) == 0 ) {
99                 char *filename;
100                 const char *p;
101                 int l;
102                 int c = 5;
103                 
104                 map->lm_type = REWRITE_MAP_XFILEMAP;
105                 
106                 if ( s[ c ] != '(' ) {
107                         free( map );
108                         return NULL;
109                 }
110
111                 /* Must start with '/' for security concerns */
112                 c++;
113                 if ( s[ c ] != '/' ) {
114                         free( map );
115                         return NULL;
116                 }
117
118                 for ( p = s + c; p[ 0 ] != '\0' && p[ 0 ] != ')'; p++ );
119                 if ( p[ 0 ] != ')' ) {
120                         free( map );
121                         return NULL;
122                 }
123
124                 l = p - s - c;
125                 filename = calloc( sizeof( char ), l + 1 );
126                 AC_MEMCPY( filename, s + c, l );
127                 filename[ l ] = '\0';
128                 
129                 map->lm_args = ( void * )fopen( filename, "r" );
130                 free( filename );
131
132                 if ( map->lm_args == NULL ) {
133                         free( map );
134                         return NULL;
135                 }
136
137                 *currpos = p + 1;
138
139 #ifdef USE_REWRITE_LDAP_PVT_THREADS
140                 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
141                         fclose( ( FILE * )map->lm_args );
142                         free( map );
143                         return NULL;
144                 }
145 #endif /* USE_REWRITE_LDAP_PVT_THREADS */       
146                 
147                 return map;
148
149         /*
150          * Experimental ldap map:
151          * looks up key on the fly (not implemented!)
152          */
153         } else if ( strncasecmp(s, "xldap", 5 ) == 0 ) {
154                 char *p;
155                 char *url;
156                 int l, rc;
157                 int c = 5;
158                 LDAPURLDesc *lud;
159
160                 if ( s[ c ] != '(' ) {
161                         free( map );
162                         return NULL;
163                 }
164                 c++;
165                 
166                 p = strchr( s, '}' );
167                 if ( p == NULL ) {
168                         free( map );
169                         return NULL;
170                 }
171                 p--;
172
173                 *currpos = p + 2;
174         
175                 /*
176                  * Add two bytes for urlencoding of '%s'
177                  */
178                 l = p - s - c;
179                 url = calloc( sizeof( char ), l + 3 );
180                 AC_MEMCPY( url, s + c, l );
181                 url[ l ] = '\0';
182
183                 /*
184                  * Urlencodes the '%s' for ldap_url_parse
185                  */
186                 p = strchr( url, '%' );
187                 if ( p != NULL ) {
188                         AC_MEMCPY( p + 3, p + 1, strlen( p + 1 ) + 1 );
189                         p[ 1 ] = '2';
190                         p[ 2 ] = '5';
191                 }
192
193                 rc =  ldap_url_parse( url, &lud );
194                 free( url );
195
196                 if ( rc != LDAP_SUCCESS ) {
197                         free( map );
198                         return NULL;
199                 }
200                 assert( lud != NULL );
201
202                 map->lm_args = ( void * )lud;
203                 map->lm_type = REWRITE_MAP_XLDAPMAP;
204
205 #ifdef USE_REWRITE_LDAP_PVT_THREADS
206                 if ( ldap_pvt_thread_mutex_init( &map->lm_mutex ) ) {
207                         ldap_free_urldesc( lud );
208                         free( map );
209                         return NULL;
210                 }
211 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
212
213                 return map;
214         
215         /* Unhandled map */
216         }
217         
218         return NULL;
219 }
220
221 /*
222  * Map key -> value resolution
223  * NOTE: these are old-fashion maps; new maps will be parsed on separate
224  * config lines, and referred by name.
225  */
226 int
227 rewrite_xmap_apply(
228                 struct rewrite_info *info,
229                 struct rewrite_op *op,
230                 struct rewrite_map *map,
231                 struct berval *key,
232                 struct berval *val
233 )
234 {
235         int rc = REWRITE_SUCCESS;
236         
237         assert( info != NULL );
238         assert( op != NULL );
239         assert( map != NULL );
240         assert( key != NULL );
241         assert( val != NULL );
242         
243         val->bv_val = NULL;
244         val->bv_len = 0;
245         
246         switch ( map->lm_type ) {
247 #ifdef HAVE_GETPWNAM
248         case REWRITE_MAP_XPWDMAP: {
249                 struct passwd *pwd;
250
251 #ifdef USE_REWRITE_LDAP_PVT_THREADS
252                 ldap_pvt_thread_mutex_lock( &xpasswd_mutex );
253 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
254                 
255                 pwd = getpwnam( key->bv_val );
256                 if ( pwd == NULL ) {
257
258 #ifdef USE_REWRITE_LDAP_PVT_THREADS
259                         ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
260 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
261
262                         rc = REWRITE_NO_SUCH_OBJECT;
263                         break;
264                 }
265
266 #ifdef HAVE_PW_GECOS
267                 if ( pwd->pw_gecos != NULL && pwd->pw_gecos[0] != '\0' ) {
268                         int l = strlen( pwd->pw_gecos );
269                         
270                         val->bv_val = strdup( pwd->pw_gecos );
271                         if ( val->bv_val == NULL ) {
272
273 #ifdef USE_REWRITE_LDAP_PVT_THREADS
274                                 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
275 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
276
277                                 rc = REWRITE_ERR;
278                                 break;
279                         }
280                         val->bv_len = l;
281                 } else
282 #endif /* HAVE_PW_GECOS */
283                 {
284                         val->bv_val = strdup( key->bv_val );
285                         val->bv_len = key->bv_len;
286                 }
287
288 #ifdef USE_REWRITE_LDAP_PVT_THREADS
289                 ldap_pvt_thread_mutex_unlock( &xpasswd_mutex );
290 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
291                         
292                 break;
293         }
294 #endif /* HAVE_GETPWNAM*/
295         
296         case REWRITE_MAP_XFILEMAP: {
297                 char buf[1024];
298                 
299                 if ( map->lm_args == NULL ) {
300                         rc = REWRITE_ERR;
301                         break;
302                 }
303                 
304 #ifdef USE_REWRITE_LDAP_PVT_THREADS
305                 ldap_pvt_thread_mutex_lock( &map->lm_mutex );
306 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
307
308                 rewind( ( FILE * )map->lm_args );
309                 
310                 while ( fgets( buf, sizeof( buf ), ( FILE * )map->lm_args ) ) {
311                         char *p;
312                         int blen;
313                         
314                         blen = strlen( buf );
315                         if ( buf[ blen - 1 ] == '\n' ) {
316                                 buf[ blen - 1 ] = '\0';
317                         }
318                         
319                         p = strtok( buf, " " );
320                         if ( p == NULL ) {
321 #ifdef USE_REWRITE_LDAP_PVT_THREADS
322                                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
323 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
324                                 rc = REWRITE_ERR;
325                                 goto rc_return;
326                         }
327                         if ( strcasecmp( p, key->bv_val ) == 0 
328                                         && ( p = strtok( NULL, "" ) ) ) {
329                                 val->bv_val = strdup( p );
330                                 if ( val->bv_val == NULL ) {
331                                         return REWRITE_ERR;
332                                 }
333
334                                 val->bv_len = strlen( p );
335                                 
336 #ifdef USE_REWRITE_LDAP_PVT_THREADS
337                                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
338 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
339                                 
340                                 goto rc_return;
341                         }
342                 }
343
344 #ifdef USE_REWRITE_LDAP_PVT_THREADS
345                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
346 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
347
348                 rc = REWRITE_ERR;
349                 
350                 break;
351         }
352
353         case REWRITE_MAP_XLDAPMAP: {
354                 LDAP *ld;
355                 char filter[1024];
356                 LDAPMessage *res = NULL, *entry;
357                 LDAPURLDesc *lud = ( LDAPURLDesc * )map->lm_args;
358                 int attrsonly = 0;
359                 char **values;
360
361                 assert( lud != NULL );
362
363                 /*
364                  * No mutex because there is no write on the map data
365                  */
366                 
367                 ld = ldap_init( lud->lud_host, lud->lud_port );
368                 if ( ld == NULL ) {
369                         rc = REWRITE_ERR;
370                         goto rc_return;
371                 }
372
373                 snprintf( filter, sizeof( filter ), lud->lud_filter,
374                                 key->bv_val );
375
376                 if ( strcasecmp( lud->lud_attrs[ 0 ], "dn" ) == 0 ) {
377                         attrsonly = 1;
378                 }
379                 rc = ldap_search_s( ld, lud->lud_dn, lud->lud_scope,
380                                 filter, lud->lud_attrs, attrsonly, &res );
381                 if ( rc != LDAP_SUCCESS ) {
382                         ldap_unbind( ld );
383                         rc = REWRITE_ERR;
384                         goto rc_return;
385                 }
386
387                 if ( ldap_count_entries( ld, res ) != 1 ) {
388                         ldap_unbind( ld );
389                         rc = REWRITE_ERR;
390                         goto rc_return;
391                 }
392
393                 entry = ldap_first_entry( ld, res );
394                 if ( entry == NULL ) {
395                         ldap_msgfree( res );
396                         ldap_unbind( ld );
397                         rc = REWRITE_ERR;
398                         goto rc_return;
399                 }
400                 if ( attrsonly == 1 ) {
401                         val->bv_val = ldap_get_dn( ld, entry );
402                         if ( val->bv_val == NULL ) {
403                                 ldap_msgfree( res );
404                                 ldap_unbind( ld );
405                                 rc = REWRITE_ERR;
406                                 goto rc_return;
407                         }
408                 } else {
409                         values = ldap_get_values( ld, entry,
410                                         lud->lud_attrs[0] );
411                         if ( values == NULL ) {
412                                 ldap_msgfree( res );
413                                 ldap_unbind( ld );
414                                 rc = REWRITE_ERR;
415                                 goto rc_return;
416                         }
417                         val->bv_val = strdup( values[ 0 ] );
418                         ldap_value_free( values );
419                 }
420                 val->bv_len = strlen( val->bv_val );
421
422                 ldap_msgfree( res );
423                 ldap_unbind( ld );
424                 
425                 rc = REWRITE_SUCCESS;
426         }
427         }
428
429 rc_return:;
430         return rc;
431 }
432
433 int
434 rewrite_xmap_destroy(
435                 struct rewrite_map **pmap
436 )
437 {
438         struct rewrite_map *map;
439
440         assert( pmap );
441         assert( *pmap );
442
443         map = *pmap;
444
445         switch ( map->lm_type ) {
446         case REWRITE_MAP_XPWDMAP:
447 #ifdef USE_REWRITE_LDAP_PVT_THREADS
448                 --xpasswd_mutex_init;
449                 if ( !xpasswd_mutex_init ) {
450                         ldap_pvt_thread_mutex_destroy( &xpasswd_mutex );
451                 }
452 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
453
454                 break;
455
456         case REWRITE_MAP_XFILEMAP:
457 #ifdef USE_REWRITE_LDAP_PVT_THREADS
458                 ldap_pvt_thread_mutex_lock( &map->lm_mutex );
459 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
460
461                 if ( map->lm_args ) {
462                         fclose( ( FILE * )map->lm_args );
463                         map->lm_args = NULL;
464                 }
465
466 #ifdef USE_REWRITE_LDAP_PVT_THREADS
467                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
468                 ldap_pvt_thread_mutex_destroy( &map->lm_mutex );
469 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
470                 break;
471
472         case REWRITE_MAP_XLDAPMAP:
473 #ifdef USE_REWRITE_LDAP_PVT_THREADS
474                 ldap_pvt_thread_mutex_lock( &map->lm_mutex );
475 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
476
477                 if ( map->lm_args ) {
478                         ldap_free_urldesc( ( LDAPURLDesc * )map->lm_args );
479                         map->lm_args = NULL;
480                 }
481
482 #ifdef USE_REWRITE_LDAP_PVT_THREADS
483                 ldap_pvt_thread_mutex_unlock( &map->lm_mutex );
484                 ldap_pvt_thread_mutex_destroy( &map->lm_mutex );
485 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
486                 break;
487
488         default:
489                 break;
490
491         }
492
493         free( map->lm_name );
494         free( map );
495         *pmap = NULL;
496
497         return 0;
498 }
499